|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" \0 ?# w$ t" Y5 p- V4 F( R
' T+ Q3 B& H' [! e& f4 d/ j一.准备工作$ a1 ^1 a# T4 ?- e9 j6 I6 `
) z) k H# k" U. S$ `系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- R8 t8 ^# A9 I
2 ]" }0 y5 h! Y' P5 T* H+ ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ y8 n3 l) W# k
0 |/ y5 L) i% ^4 cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ K, x) Q; v F/ _) }; M
W$ }3 l- R' i$ L. ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 I! R% }' F1 M% a2 S. X) F& d
# S# C3 w8 E; ~! k8 q# f依赖关系:, u( v6 u* n( J1 Z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
, z0 \) O& y# s' J& t. d! [9 n1 o4 X" D
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
( P5 j3 u+ i! k' d# y' K2 p; tmodsecurty依赖的包:pcre httpd-devel libxml2 apr# Y( d5 O2 a9 w3 K( q, T
6 G$ E, K8 Z5 T$ l6 q
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel& f' Y( @7 Q1 W8 `
二.启用standalone模块并编译. K) w; L5 n) i* p9 R r2 g
) P6 J5 |( M6 y3 Z, A* L0 h e0 ]
下载modsecurity for nginx 解压,进入解压后目录执行:3 {0 R# \/ R+ J+ M5 Q6 V7 S
# m& T: ^- S. V7 T8 [4 W./autogen.sh
] \0 f, s, Y9 B- \; t0 C./configure --enable-standalone-module --disable-mlogc
* n) w) d0 z$ W: Dmake
; G& a2 X+ N5 ]+ r7 Q三.nginx添加modsecurity模块* z: P0 ]+ E( C9 F! y) S
6 c/ q3 E* y0 B# `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
l! G9 K5 G, x1 `1 c
$ ?6 [) l4 P. q* ^5 ~3 L) b2 P./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
1 M, M' P( |: F- K9 `9 v& ]6 g z( Hmake && make install
' f* N+ M$ c) ^. N3 e0 \四.添加规则
) _ ]3 k4 P$ n5 y
9 P9 Q+ R* W9 P3 A" |! Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- y; {) D4 ]# V# S O
6 p: W8 Q& j7 d* T/ \
1.下载OWASP规则:
) z- f5 }. L9 s- d
$ J# L7 r5 x( e3 l' I0 y. hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! t- X2 X" S% m# T
3 H" E6 d: H6 N5 v Z2 n! lmv owasp-modsecurity-crs /opt/tengine/conf/) c/ C d, ~0 u8 j
' x* n8 @6 |+ I3 S9 o4 rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 J/ b8 |. a6 x! {5 r# {. k1 X
2.启用OWASP规则:
8 V j% Y6 @+ W; a8 ^% ` ?
$ M/ ^, G4 T! f* b复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 l7 N" ?0 o* X2 m- K- O% F; H8 |
7 F, C+ Z w8 h, ^7 @( j9 n编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% T$ W8 P0 D! c ~
' B' t% @6 E! H8 f( ?( Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
/ h' S: z3 ?& L2 D
& A+ H" r- [$ s" ?- Z1 v; [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* u7 [5 ~- r4 \+ L% f- d# \8 w: I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) x, a% V7 a O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! c" G2 q% C& C# p6 a) Q) C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 H' h' u) A! _" f/ \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 s& K6 Z6 `# i' q& c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% t8 D6 O) v1 K) d( r4 s: PInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ H: T6 x) E2 V5 {" Y! |
五.配置nginx# T% G. r2 q4 a
5 U: }; ~; G2 p0 y, z) V
在需要启用modsecurity的主机的location下面加入下面两行即可:
3 y. L8 [0 f1 U: H7 C! S1 Q b+ N
5 N3 K( l+ r8 r' S7 qModSecurityEnabled on;
' X% d J7 b! oModSecurityConfig modsecurity.conf;
, _ y. a' ^ R9 X& f下面是两个示例配置,php虚拟主机:
) H* m- f( z& c# S6 ^. e$ V" F X
1 `' Z# ~ e; Vserver {3 A$ P; i' v" O* h' z4 S
listen 80;9 g0 c, }; s- c) k8 h
server_name 52os.net www.52os.net;( w/ w. N3 N, o- A$ I7 v
' h' [; z4 @' ^/ `7 ~" t% v* } location ~ \.php$ {
' m$ |- ` v, ~+ e7 `& Y ModSecurityEnabled on; 1 `8 h+ `8 u( k( @, m
ModSecurityConfig modsecurity.conf;. k0 \1 K/ r/ J+ `
" u: ]! l* t. t6 ~. C- W' y' A
root /web/wordpress;
8 O3 a; c# {( I) C index index.php index.html index.htm;& E. e& v, _ e
4 n% E X+ S" ~0 ^# h: l& \: l
fastcgi_pass 127.0.0.1:9000;
], N5 Y6 }, J* I/ v a fastcgi_index index.php;
2 B/ Y4 h" l- w' K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ I2 T, Z, U6 A) k3 ^+ {: J" @
include fastcgi_params;# {/ G+ k5 {* o6 Y0 e
}( G0 ~$ @$ Y/ f' r) j& h
}
. l3 I' s, q6 W1 i; R5 @upstream负载均衡:
) s0 _$ t$ }) f( F" t1 D
4 k c3 G9 E* M' ?1 A: b" o8 uupstream 52os.net {& m$ {6 {" M1 A
server 192.168.1.100:8080;
0 _2 W! ^3 i S" F$ ?4 y server 192.168.1.101:8080 backup;- Z6 e; R. c0 R
}
1 m5 [$ L7 [! d$ G0 P7 V+ I4 y/ \
( }. C4 l# @9 I, [) A# e( Cserver {4 n/ ?3 V$ i, Z3 m# V
listen 80;
4 L5 k9 h. A- ~: T( Vserver_name 52os.net www.52os.net;
0 z; |/ @: J0 a# [6 ]
: P' C) G X0 l% t Q- flocation / {
; _5 i( [/ m7 e# T ModSecurityEnabled on;
% P8 l" K1 \: f0 A% j0 Q ModSecurityConfig modsecurity.conf;
+ f) ~1 n- v5 g$ `9 s# D8 Z/ i6 b
proxy_pass http://online;
2 O# [% l/ i; G proxy_redirect off;
0 D! x$ o5 M2 G2 |* @: n# x) O( \ proxy_set_header Host $host;, _+ H% Q3 L4 u
proxy_set_header X-Real-IP $remote_addr;% O0 g; E- O" S7 B8 M
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
' V9 w7 H# [" o \ }' k8 C- Y- r9 w6 Q
}# [; T! q! G8 ~. @
六.测试* A, ~0 o6 f- @3 r
2 U+ J6 D6 B3 ^- n7 A r5 s2 p0 ~% P ^: g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 k6 l5 v# e6 U% A
) Y: U4 R: J! S( h
<?php
8 S o, [8 m1 E) y) m; L phpinfo(); $ l X n9 T0 I L
?>
6 Z4 X/ P2 d- R {3 _& ?1 {, G+ N在浏览器中访问:0 A' I8 K9 C& b: w0 @
% V1 \. h' u/ G3 B* n9 k+ s
http://www.52os.net/phpinfo.php?id=1 正常显示。 l1 q% D2 ?) g4 b
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。: A' J+ \% F6 g/ u! m1 M7 `
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: X( N3 j0 i( M3 H6 h! H
说明sql注入和xss已经被过滤了$ c; V9 X$ s" }# l. y
4 Q" l) Q# O: K, d七、安装过程中排错
% \+ o6 R! _ e( w. w
/ j8 F* w& {) G# |1.缺少APXS会报错, A2 n9 p# t) i! W9 f
/ ]# b1 O0 ` e, y
configure: looking for Apache module support via DSO through APXS, H1 i: u- x- m, H
configure: error: couldn't find APXS
1 j) O m7 G6 t& K# P3 capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- p! L. v+ X7 s! Y$ S1 @9 `
解决方法:
# O$ N) W% A- }% `0 j* V
! n# a# C% B) E8 e6 Myum install httpd-devel
. i! H8 U+ W- T% ^2.没有pcre( k0 E* i& Q; P; [
- ~) m& |3 v; ^8 r4 xconfigure: *** pcre library not found.
6 H0 e5 E* I! |! D( Nconfigure: error: pcre library is required
6 O0 F5 C4 t# ~2 W9 I解决方法:. z+ X" }/ t7 Q! ]
, b, g/ ~* S$ ?# Z4 V" P- m% h
yum install pcre pcre-devel4 y P( s0 S. w( Z( X; c: P8 @9 m: _
3.没有libxml2
- ~) T* G* l/ J8 c! E% j U8 R* W
4 t- W( Q9 Y' B# X6 w
7 g1 \) C- J# p# k$ _( lconfigure: *** xml library not found.- V8 G1 E$ X# G- G, v8 {
configure: error: libxml2 is required( c! P' O$ p7 \* A* q9 A* S" U
解决方法:9 R+ Q$ L6 i# B v3 I" A
( R5 t [( ^* D# X3 D P% V
yum install libxml2 libxml2-devel
" i! c9 V; j% @. ~1 x, i7 {9 B4.执行 /opt/tengine/sbin/nginx -m 时有警告. Q4 {0 q/ n" ?+ _6 V I3 o
$ p$ n: A+ l# ?9 i0 h0 J: s- k- o$ `
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, O% L7 t4 q3 g% b$ ]' C5 c1 C. Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 F4 F/ F$ e! M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% F5 r8 R7 h, k z" o: }3 `
2 O# e( h( A( a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured. W8 J6 y4 |9 L1 E$ K( g$ w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. C# Z2 _: X# z! C2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 f" F# y) P$ k; z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" d# o) H' |5 |8 e4 B) m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( ~ e9 c/ H2 |( o2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# U' x. D( j. j
解决方法,移除低版本的APR (1.3.9)
3 D5 W8 G* A/ z; b
T4 j/ R4 J. Z# Z0 e3 B4 Q' ryum remove apr
" j5 y) o! O: G% l! v5.Error.log中有: Audit log: Failed to lock global mutex8 H/ Z0 Y0 c$ R/ A/ h/ z
S. h/ e8 v C( s% r
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ U# [0 C3 t" q3 s4 {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 e3 ?" Y' ?% i* z& K解决方法:
& }5 B" a+ k# f# L$ i( x1 L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; S( }9 y+ h5 E1 c9 n. E* ~( i; `) s3 A/ L ~- Q3 E7 ?1 R/ w
SecAuditLogDirMode 0777" F; K- I; Z6 w" Y: X0 T; |
SecAuditLogFileMode 0550
* T" Q5 n/ w7 P* zSecAuditLogStorageDir /var/log/modsecurity) {- q5 O2 k9 \- q
SecAuditLogType Concurrent
% L3 N W; J! y0 }# C4 T) p8 o* m参考文章:
' U, j* ]9 @0 xhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 ?% K( T& |8 j0 f2 @: chttp://drops.wooyun.org/tips/2614 |
|