|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! ]& u9 s" x* s) c! l- D- d. Q9 x1 `- y
6 X% b4 K+ l' w$ u1 U/ S7 o一.准备工作" r& T; b% D0 F: I+ M8 U; ~+ t: \/ S
1 y6 S: W' V4 M ?5 w
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 j+ g: m: L! \7 m
; ]& {3 X; h5 |tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( G( o8 s) \: \
& Q; x( s& P- m) P0 P4 O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; z8 ^& Z( d; }
3 Z5 \! Z$ F8 X% R- C7 d/ |OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) b# t$ r4 e% G1 h* ]) t( d- L! G5 g. ^. V/ I4 ?0 M
依赖关系:
2 q ?( `# \" r6 u/ t4 z. I) Htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 x* q# G1 t2 _% {3 F
, u7 [! d, U- g$ n% [) k* _yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel6 c5 G% k1 c4 [* `3 S
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. I+ K- l( } F
. _0 f! U6 u( V# j8 Z0 U( iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" m/ A- W! X' E! f
二.启用standalone模块并编译- H- `7 N& q9 H; P& f4 x
: w# E5 g6 R' d! y1 s
下载modsecurity for nginx 解压,进入解压后目录执行:+ D' p- g) G8 V; N" z: F& P$ D! f
/ [ B: V6 X1 _5 g* X./autogen.sh+ y1 r6 c' C/ W4 l6 P
./configure --enable-standalone-module --disable-mlogc
9 d/ @; f' p+ Z0 g% I. M6 H. rmake
o: u7 w; e8 N5 Y/ w" _; v# |三.nginx添加modsecurity模块
9 q m) p6 x4 z: K5 V- T- H& h3 ?' G
0 ~4 f) g) C, Q5 j5 c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 G' _* g9 {- a- x5 j, @
" W( c. w& z/ G0 S8 O/ [./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! B+ A/ Y& f+ @4 {( `7 ?
make && make install/ p0 O" q$ ~/ w! R1 |
四.添加规则) ` e, E1 o0 U& e3 Y/ F+ c" a
5 j+ o# q K( J
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. |! W. H8 m0 u* G1 \/ W
" s4 m% d9 m& A/ R* m1.下载OWASP规则:3 I" N, C* [; X) _" \# g. w
) [( R0 N' F/ I* P8 R5 rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) Z5 u/ `9 ?- e, D1 x& a# d0 j& ]7 J- U! i) O
mv owasp-modsecurity-crs /opt/tengine/conf/8 G) i8 [- U+ j# o4 F
6 `- ^6 S# k8 o! H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 h/ I8 {4 {6 J5 h8 q" F# v) S2.启用OWASP规则:
* V, b- K5 d& G5 @) Y' x- |: S
% }& n+ E* N9 f# c3 }) ~" e- C; q复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( Y+ Y M/ s+ ^! h- F5 L, N
" v0 z9 t1 | u编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
Z" `3 M. b7 z0 J$ M% p8 k" W' g: ^. |. v* N$ ?8 I1 f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' L: |6 O7 W; x. Q& Y+ V
- X u' f& T& [4 l3 P
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 E! T& h J G' ?4 [! w1 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 P% n4 q& |' }0 {; a: c; D# jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 r& {& i9 {' L6 o0 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" ]8 P9 Q4 h! ^5 d% X: I6 B g0 kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 g/ k% o% d2 g" P7 |% u0 O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 F g7 L( d6 i# w. C( y! a
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; \$ T9 x; j& r
五.配置nginx
: l) _/ ~+ @5 a+ Z
% A. r3 \0 ~- ^& t: ?5 w在需要启用modsecurity的主机的location下面加入下面两行即可:! E) k5 m% p9 ?) a' y% t( x2 e
# s$ N5 S8 w+ Z! w e( P
ModSecurityEnabled on;
# m& e, w5 D7 xModSecurityConfig modsecurity.conf;
% M4 @- s& z& q/ }4 k# `" s下面是两个示例配置,php虚拟主机:! u4 f! l7 n" M3 s6 N# Y
5 y2 `. ]% B+ q# P# H8 f! F5 rserver {7 M6 A( {; b& ^. A5 j0 {% t1 u* n
listen 80;6 _, D# x3 g9 t3 U; E/ s; w
server_name 52os.net www.52os.net;" s" [3 i/ h9 h, B+ S2 s
# u+ h9 v+ {2 d' X location ~ \.php$ {
2 X9 E$ g$ [5 { ModSecurityEnabled on; 7 {* t4 H3 O2 H b3 r
ModSecurityConfig modsecurity.conf;0 a! A. \, I6 p- P4 Q/ H! k! N3 T
9 {) m* C4 B6 p" i9 M1 ^ root /web/wordpress;
4 D7 e8 m% N: K8 t index index.php index.html index.htm;: s# m, a! y; [- H; E: X7 m8 D& r
; H' a# `7 H& K: A% i( p& q* U fastcgi_pass 127.0.0.1:9000;7 ~6 J; i6 z/ Z8 l) J! G# s
fastcgi_index index.php;4 n$ ?8 M k4 B) }7 r, ]
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;1 I& P! E* R& c3 y
include fastcgi_params;1 S: ]2 `( r. D4 b3 |- Z
}! M0 f J0 ]9 L$ C( ?
}
0 ?* E% F- p- W6 c5 t9 qupstream负载均衡:: X: s* F( `3 U3 t+ F9 s6 }6 l
! E+ T2 `0 }0 O, M) g! S- s' W
upstream 52os.net {; L) k$ m" g, x4 l$ v J k# @7 }
server 192.168.1.100:8080;
/ N+ p b) D' q) I& [1 \" k server 192.168.1.101:8080 backup;
# m" b& d+ F* C5 P& j0 [! P5 j. N}
; _4 e8 D y4 R7 y) [7 \ G: r9 F! m+ d$ x" ?6 f; Z
server {' K& ^) z6 Z4 I( z+ N! f8 o
listen 80;8 s/ D0 ?+ d }/ ]! k
server_name 52os.net www.52os.net; ^# |; Q8 L- C I! K! x
. `/ d) U" o6 T9 B" {- R7 _
location / {
4 m" x, l/ q8 O: x) z) w ModSecurityEnabled on; 9 M5 F: ~9 D& S/ T9 O, Q6 I) @
ModSecurityConfig modsecurity.conf;
1 v# z$ x/ _) F+ @5 `' t0 t4 N$ y3 m2 Y: N( l5 F
proxy_pass http://online;
9 e) C9 ]; C, n+ b0 ? proxy_redirect off;
$ V1 ^ I& n# r3 C proxy_set_header Host $host; n5 [5 g4 L; K: S$ ?; [' e0 C% D
proxy_set_header X-Real-IP $remote_addr;( o. }0 q4 l$ s. Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 `5 ^& B+ T; o, U# p
}
. H! E3 t. M: b2 X! x8 W# d}
9 a1 p- ^% V6 U六.测试
. @; w7 C! H0 c! Q3 g, T- g" Z1 O. ?+ n/ R3 j# ^2 T4 {+ w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* }' n- K+ {, J( e
- Q2 g% X3 K' o9 [9 ~
<?php4 N9 N+ d2 m' I3 |! W2 `. _2 @
phpinfo(); 5 e5 w3 R/ B0 R j7 y
?> _: o U3 `% o" ~) ]
在浏览器中访问:) v% b0 w# ~ L! A' m# ?
* g: v7 f3 K: `3 k- w% H% g! X
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 C, B! H/ H1 u7 a {7 q+ \# C4 F* Ahttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
8 i7 R9 B; X9 A; F( I6 ?& r) hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 ~0 T# f' Q5 P8 b. u
说明sql注入和xss已经被过滤了! j: B. K' t( F
% ~( @0 R4 C5 I
七、安装过程中排错$ Z1 T+ F7 Y+ F5 B7 E
9 m. L0 M9 R/ l" A- X% S7 t1.缺少APXS会报错/ ~, A7 i n0 [/ h+ n i+ d* C
$ x+ k# h) m B
configure: looking for Apache module support via DSO through APXS3 r& M* @$ d* ^( M/ P# C& D/ k
configure: error: couldn't find APXS
+ j$ Q& E6 m" }. Y7 ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 p7 A8 Q, u/ u解决方法:: _# z8 F8 a4 w- Q
4 a. }# @. V1 k2 {% O1 ryum install httpd-devel% C. x1 q' ?5 l. h1 V
2.没有pcre: O+ }5 u( y; d) ~) U
. t' o3 Y5 B. o- u% o) D9 R. Iconfigure: *** pcre library not found.: R/ |4 ?0 R4 A7 E
configure: error: pcre library is required
" C* G2 }8 r- q; F6 l解决方法:6 l7 n! ^. G+ M T9 ?0 E
1 D) U5 N1 a! Z
yum install pcre pcre-devel
/ N+ |1 l5 D N% S2 ]; f3.没有libxml2
& g8 b1 D# p1 I( H. [/ U% |' G8 S5 F# J) Q
/ m! N' `' v1 U2 C! I R' v
configure: *** xml library not found." H, w# i0 o4 u
configure: error: libxml2 is required9 T* b/ D$ j3 w5 c/ m0 A* }
解决方法:
: p7 U" P: [* B. j
5 ]1 |. N+ c3 I' }, G- V( h% cyum install libxml2 libxml2-devel7 B) W3 d8 Q; b, z K3 Z$ G
4.执行 /opt/tengine/sbin/nginx -m 时有警告8 h1 H# d! ?- ]: q9 `& Y; ]
6 x( l9 D+ ?! E% i. z* X
Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ o8 N( M; {' o% e1 D
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 e% d' v `" \; c7 F4 X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 ^! d4 |1 ? Z% ]6 P
: z6 \, A' Z5 ?# F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 g$ V1 o0 ^% _" d- D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 Z7 T2 P, O! Z" W4 y) g% j. r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. P; D) b* X" Q% O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% v" L1 K9 a9 ^" ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# J3 C* J% X3 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 ~5 e0 q, h& f- l a" t2 q解决方法,移除低版本的APR (1.3.9)
}9 m+ Z x# F1 S3 }( ~2 d: w; i3 C% y2 P, i& C
yum remove apr( |. f, [2 k( O) I3 [9 v
5.Error.log中有: Audit log: Failed to lock global mutex
6 E9 E. d$ T0 \! D
" M2 Z1 x' R" P k/ u1 S2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " B( K! z- j5 z! \. W$ Y* D
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 W+ o; r$ P, l3 p' }3 }8 g# o+ x解决方法:
! Z6 k! [+ r! ~0 ~编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 ]5 X6 A0 F8 b" N6 U0 ~
! v, S# H1 C4 W) c; Y* ]" x
SecAuditLogDirMode 07773 m. `* ]- r" A
SecAuditLogFileMode 05508 i2 E9 A/ f9 h8 X! L# w' m* A2 @
SecAuditLogStorageDir /var/log/modsecurity' T* R, r; T! V$ F6 C2 s I
SecAuditLogType Concurrent& |& t1 {2 O5 d" ?' j% h& R
参考文章:! \ L# Q: N( b& D
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 ?, |; d9 v( w) c/ bhttp://drops.wooyun.org/tips/2614 |
|