|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( Y: S/ B, b2 v! u7 P) |; P, L. j, f' @- u, p
一.准备工作
8 _9 E9 \2 s k$ Y, E3 i7 S1 S- g# ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! j1 o: ?0 a$ L A4 M1 H' E$ W" ]7 L1 p( @3 F: I( Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 d3 g, L: k) Z. Q
$ t: {* @% L; z6 e+ ~# _3 pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. K# R# E" ]: f
( [) T, S! ~: G' U; ]/ X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 Y* r) @' a1 S" n3 V4 f2 P
7 O: z3 V! u3 h p/ I
依赖关系:0 ]' R, i$ A/ T4 C- I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 S* I; R3 G% q; h
* ^6 ?8 k8 [; d0 E& P2 G' K0 |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 N. l" D/ O- Z3 O% D7 cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 Q# A4 ~0 p, L: {$ U( m1 R* E; E. ]4 s' `! W2 O! c( a/ m
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* E V2 F) ^2 H T% b& y0 ^
二.启用standalone模块并编译
) K* ^* q: Z- t2 }. }! m; f# Q& X& z, Q4 \, ~! V
下载modsecurity for nginx 解压,进入解压后目录执行:- G9 }( g: m! [
7 z& |6 u1 S% u1 w$ X; s I
./autogen.sh
$ _( x' ~: k8 \4 x, E& w; a./configure --enable-standalone-module --disable-mlogc
3 e; z) A) V. P, a7 b, n1 x( Hmake
# {* n5 b6 M9 I4 ~7 s* G8 i/ i, K& b三.nginx添加modsecurity模块$ T8 K1 u" k7 C* T
2 N4 n% ]# ?3 A) v4 c
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: o6 `+ ?* Q8 C0 ~) h
0 _! R3 k4 ?. `' p' `# z" u! J8 B9 O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine- `% i" L- p; r# k0 G
make && make install
' l& Z! f! Z/ g: M8 P四.添加规则
; a# Q( [2 n% E# T6 x& S( l! Z0 b+ E! v
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 n* I. c+ C1 R/ e+ q1 F* S- w# U5 N% Z4 t
1.下载OWASP规则:
( [* f8 \1 L7 S6 `# y( R. X& X, E! i* v: j/ {7 l+ I
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 j* J$ Z( E* V8 a
2 U/ C9 N3 h7 }+ i- ~mv owasp-modsecurity-crs /opt/tengine/conf/ {5 K/ F F# K! _6 H
* S) V5 c% b8 o; ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
H) ~. u% M. Y: u2.启用OWASP规则:
. L) [. C% ?' c* E! Q8 j# W b: I' V" J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 l- Q) p! t% s) L5 m; \
4 F% [0 i( _( {' N9 Q6 V; ? o( {$ i- e
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 F Q4 P% Q+ N4 z% Y
8 L2 L# G- {5 t6 L4 i& S& bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。 A$ _/ a1 e0 v3 X
. r9 w9 m) h& y0 e4 b- \Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 T2 J. q" Q$ m. M# `. p7 [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% `! A) Q. _7 qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" g4 R# H+ }1 DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 O) D5 Z0 ?) \8 v$ ^% b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& S" ]) i2 A2 l9 ]) s+ ?( d+ i* fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 ^& M! p6 d2 }: U' N' z* SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% x3 c1 t) G7 ~五.配置nginx( s4 C3 @7 _* t$ J7 ?9 \' Y: f
0 A9 i4 G3 G/ z/ q$ {6 y! P$ I在需要启用modsecurity的主机的location下面加入下面两行即可:
/ k) T2 ~# B& ~$ h! D
& [8 Y8 @9 |; v' m- p( x8 l$ I! o* o& BModSecurityEnabled on;
& }7 n* \2 q3 z" ^5 C; ?ModSecurityConfig modsecurity.conf;$ F! U" A x- g1 d# T E+ y: r. j
下面是两个示例配置,php虚拟主机:5 f- z* o0 \ G/ i' Z) R" | ^
( p$ O- B' \7 S/ g0 T7 j5 E e
server {
; p5 i$ d0 ?0 Z6 I: {2 s listen 80;8 J0 d; s {3 k! H
server_name 52os.net www.52os.net;
. l+ N/ y9 T+ P" G+ c9 I9 q8 o 9 S# A$ ^/ H# }! l, M+ y
location ~ \.php$ {7 L, a* m5 n% P9 ~4 N! q( _* j5 h+ ^
ModSecurityEnabled on;
6 [2 j* S0 g5 w, n9 t: H ModSecurityConfig modsecurity.conf;
0 u: L4 F: A% e) G/ `% y' F' G9 z+ C' p; g" l
root /web/wordpress;
4 S) K# C' L6 T g index index.php index.html index.htm;0 r! ?# k! ^& b- B# C$ b7 f1 D
1 i; ~/ }8 R& x* Y
fastcgi_pass 127.0.0.1:9000;
, v) M5 d! a* O9 N+ } P' \7 N fastcgi_index index.php;2 c! Q. n b) M2 z9 x) i
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;: b, \: B: e: f% p& b0 G& d5 j
include fastcgi_params;
' h9 f& a/ u& s& [ }
$ Z% K$ ~, I# {1 l( f( ?6 P }, l! ^' Y" ~ ~! a. o# N
upstream负载均衡:/ x+ u, C3 C; N# ^2 u: o
3 t* F2 c: E0 I- U! o6 Yupstream 52os.net {
0 S/ ]9 T' q9 F4 \7 @ q9 I server 192.168.1.100:8080;
( Q E1 I( ^! n+ W1 A server 192.168.1.101:8080 backup;, O3 a1 @! `% `( V$ d7 _
}
6 {: ]0 K. D: [$ k7 t
3 p i3 d, a+ I/ q( [" e" S+ t" Y7 Hserver {1 C2 ?4 O4 A; }" U
listen 80;
1 S1 L9 k4 P3 D) u- E P* @server_name 52os.net www.52os.net;: s) @% N0 [8 M- P# r* d$ { j
. C3 t+ N9 x+ d" C
location / {; B/ [3 T' g) Y0 T% z$ z. t* l
ModSecurityEnabled on; ( V; C/ z y5 c8 ]9 c# Q4 I" y$ N5 u
ModSecurityConfig modsecurity.conf; 6 A: {7 z' A- h1 z
8 N F% R) ^5 a, v
proxy_pass http://online;+ H) c9 F+ m9 E2 L- h& R( J4 }
proxy_redirect off;
! z! v: ^' m7 n- b4 H proxy_set_header Host $host;
1 ]5 N4 ?) p1 Q0 a3 Q0 m; ~ proxy_set_header X-Real-IP $remote_addr;$ W2 K9 @$ X8 x K7 D% F) z
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" x: l# V7 e2 Q% _5 \
}. ?( a* Y' x: M8 d2 }3 V
}
# Z( e a5 L% W+ l; K3 e六.测试, B; O! [. d4 I: ]: e+ ?& F% x
& ]0 t3 _) Q: c+ [6 q Z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ n7 l7 K: U% H/ G$ x3 S" i7 x: ]1 _* H$ C7 p( V7 Q& E5 }
<?php8 y5 b! k" I5 Q$ F' H+ P
phpinfo(); ; {4 ]# ^& u W& y% N
?>
3 `3 d1 c9 @( f2 ]& J在浏览器中访问:
- |- Y" ~6 l! I# `* I% {5 Q; \% E5 i- ^# F5 j
http://www.52os.net/phpinfo.php?id=1 正常显示。1 M6 o& j6 O: a! I) \- ?3 \
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。+ _1 E4 a( J# U" L' W( F" r
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( w1 p( d' s1 `2 U$ c- I+ V
说明sql注入和xss已经被过滤了
$ n! Y/ _2 b3 G9 B7 t6 R8 t- _2 [1 H! n4 Z! u9 @5 L5 ^
七、安装过程中排错# U4 {9 z/ K8 F
5 f9 I; }+ L9 B" ? c
1.缺少APXS会报错5 L* S8 P- B: U' n
# M p( k5 a$ F* g0 @5 Bconfigure: looking for Apache module support via DSO through APXS
* U; c, x0 u2 T# _& V; U! ^4 g; J. [configure: error: couldn't find APXS4 q. w/ j) U; K
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 T. T, X7 O& ]
解决方法:
( W+ x# b/ j0 o/ T4 G) p q7 \: G! e1 W1 r; i% ^
yum install httpd-devel
+ b3 p6 Z4 g; E# S. U, V6 x* `( l6 E$ |2.没有pcre5 N' O K3 V/ z( i o
# v( G& z1 ^3 p2 Y5 c) ]
configure: *** pcre library not found.
4 O$ u# K# j( J$ y3 |+ Hconfigure: error: pcre library is required
2 p+ \" b2 Q, l8 _* Y2 d解决方法:! |9 S( g! u* Q
; ?5 {" V1 a: }% G4 d' f
yum install pcre pcre-devel8 O9 z6 I4 R" A0 D' j
3.没有libxml2/ k9 p" |/ I4 y9 N
O6 M8 X/ }7 f% ~
6 F, P6 S) q, u x& H; A5 T: Jconfigure: *** xml library not found.
5 W2 ~& _+ z. Oconfigure: error: libxml2 is required
9 b& |& d0 b% A. X4 j2 }解决方法:6 S* U$ z3 p* _. T" o: u
8 o% V$ D# l7 x. k: x
yum install libxml2 libxml2-devel
) d3 t: W% v+ T: R4.执行 /opt/tengine/sbin/nginx -m 时有警告$ M7 } I5 r- B2 l; u; J: M0 b
# F& a5 x$ E4 M. N& c9 M; X
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
: }! q5 Z+ }& c/ T% v/ I. j1 B! gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% O! m7 j& \0 ~& F( l4 b& P; Q' P O原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log, ^& s7 l' @! ~% U! |/ C% a$ R
: v, c, [6 ~- a2 v3 F, R7 w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 n( o. e( V( a4 N3 s! b/ U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 h0 S+ t1 h) P* `6 o) M, `) I5 b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 }+ _1 {( r; i1 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" k9 w/ L. A" B% P, m- a/ {& q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 z# F, t# |7 V, Q, d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ ^$ b5 ? n; \ c解决方法,移除低版本的APR (1.3.9): m9 q7 i- a+ w! [0 d
5 K$ ?5 @: j" C1 R# s r: {$ u& q% F$ g
yum remove apr
c. ?. Y, |8 l& h6 a5.Error.log中有: Audit log: Failed to lock global mutex8 h) r; Y* t" A/ H. ~% }. m
; t& ~0 e1 v5 H5 B
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 t4 T8 g4 x) q$ I* `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- z( r7 ^/ a( D# B解决方法:5 g5 L. l3 p o* ^5 u5 \
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 e7 P2 m; ?/ n' T7 G1 a# K8 Y3 v2 s% U
SecAuditLogDirMode 0777
9 C' Q q) G) R+ h0 D$ kSecAuditLogFileMode 0550. E2 y) R# L% P; G
SecAuditLogStorageDir /var/log/modsecurity0 u* O( S* c# N/ G2 I- G m5 }8 m8 o
SecAuditLogType Concurrent4 [9 F# W' f+ Z9 o! W: t3 m6 z
参考文章:& \8 n5 ?. s' T( H: T
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 R8 d/ I* k' Z: j& w+ shttp://drops.wooyun.org/tips/2614 |
|