|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 H- }' s+ T0 ?1 ~7 ]% C& O* Q5 Y+ n4 d+ [8 [* u$ g5 L5 m7 n( e( m
一.准备工作- P3 k6 x, U; C. p
. _4 F* |: T$ N5 ` _- t9 B0 a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) h- G: g5 i. }
" d5 T) b# f3 M, k6 O6 a, x Ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 Y( {6 b5 v9 t, _" S. r2 W- `3 G; S$ z2 `/ u8 K2 L
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" j' D* @+ [0 `& F3 w8 j$ F3 M1 c
$ l9 v: O9 W% g# P* {; x* wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs y E: b9 k7 k* C& Q+ i1 ]
6 L; L/ m+ l. D' I k6 N+ v1 f依赖关系:# i) c0 N4 _- X0 ]$ P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 U) |* X9 V/ x3 L1 h9 {8 b
! o ?: W0 l8 z U; D% m2 I0 Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* u3 d, @/ [/ w2 j6 [8 p0 m3 R: U8 wmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. x5 C. l; z: `& B+ v! X
9 J9 ^3 K$ X+ a9 @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, K$ I. Q4 r% O% n) L* i
二.启用standalone模块并编译% i7 D" g% S( |( Q
+ `7 z1 _4 ^& E
下载modsecurity for nginx 解压,进入解压后目录执行:. l# R' W; [8 z8 K" k
- k( K, S4 }% O2 O$ ?" O/ e+ y; `
./autogen.sh, p' H7 Z- x. a* H _% y6 ]
./configure --enable-standalone-module --disable-mlogc
+ ^! S) ~* ]4 h6 a2 l# k. omake ' P7 r p* J6 X- g1 l
三.nginx添加modsecurity模块0 ~ ]: N+ Z9 _2 u/ x( v
) M* N- i% D2 v- f# k
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; G4 y- S* p+ [& E& {# m( _
' c- m" E4 L" D5 R; c5 T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ z I: y c8 u5 c7 m. W; @
make && make install y9 {# r) z+ i
四.添加规则0 q% `# i# q1 N+ Z. X) a' [% \6 r7 e2 L
9 g, a! f) M/ K$ k7 ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" e# t2 m% @. ~$ \3 e9 }7 t
# w! m& P* b t1 i0 g8 K* I% J1.下载OWASP规则:6 M) J. H L; v/ Y8 J
6 j. o! H% f, |0 jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ O1 w4 S. Q8 j. A8 m. R3 Y$ F) ?* b9 q8 B* h) F4 Y" l9 Z
mv owasp-modsecurity-crs /opt/tengine/conf/
8 `7 \( V+ t2 j* U3 }. a' o! M) w. D J) b( a& \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 U0 M* i. D* c' f7 ~5 l2 f4 y
2.启用OWASP规则:6 [% d3 P) [) P7 [# L- ]! G
4 [+ Q' T' V4 A1 Z X+ q) ~
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ y5 U* F! G9 \
8 H0 ^" R$ e* v5 W3 q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ E5 g, T/ J4 k; {5 e
- @* ?( b2 b: h s7 Y& Z- P' ^owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
+ L; e2 S& q& E: L0 R
7 ^, O6 k$ s' r3 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 X/ J" y2 U/ T; G" j2 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, x& l U: O" p6 K I) \* r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; o% H n* o" z7 f0 nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf B/ y9 ]0 _9 }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: ?) j7 X. D) K: G; ?, J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 z# f L6 i" PInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. Z0 v3 y: y; e# l0 ]7 L5 d% m五.配置nginx
8 M7 G8 ~0 t7 N0 R
7 S( w3 t$ u4 z" l/ }0 i6 w9 S在需要启用modsecurity的主机的location下面加入下面两行即可:
5 `; s/ G# T; x6 f: p# j* }+ p( B
' i4 E7 f6 Z3 j2 G6 aModSecurityEnabled on;
+ k+ O' h; F9 t6 w J; EModSecurityConfig modsecurity.conf;% H9 |6 |+ q' \/ D# x( H* ]5 I& ^
下面是两个示例配置,php虚拟主机:# l7 _! C2 x5 W% ^ \! @. g
+ J- W. N+ k. m6 p% K* V$ Gserver {% R0 F2 g! ]% {: {, q" F
listen 80;' V# P: P) ?9 b! p, p8 o, v- _
server_name 52os.net www.52os.net;( Q8 L; E+ P; z2 \: I2 Q3 q
! R( Z6 R7 ]- h: ?' e5 a- G
location ~ \.php$ {7 d4 |/ n ]- s) b" x, ~
ModSecurityEnabled on; O- z: B9 T- f$ l2 d7 v! }! D
ModSecurityConfig modsecurity.conf;
) d5 f3 z" u/ B# ?0 x
8 u( O$ @0 T) A4 o' l6 e" s/ Q3 u2 H root /web/wordpress;( Z+ c8 V$ m5 ]) [, v# H
index index.php index.html index.htm;. O) t# x7 w& u1 h% ~. e
) W7 ]& T( `8 L! q4 s6 }) h5 Y
fastcgi_pass 127.0.0.1:9000;! [% o* I8 H$ J( v% M" O
fastcgi_index index.php;0 t* v, B$ a$ L3 t" b7 b
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;2 i$ A2 c0 ]& a* T- s5 K
include fastcgi_params;
2 }% m6 a' N2 [( B }% S8 {5 U: f5 e. g* y
}! Y6 C5 U- Z; z$ W! B
upstream负载均衡:
+ X9 K6 w/ l9 |, X- Z* e' y& R4 m
upstream 52os.net {6 J! i7 l. W) Q; q
server 192.168.1.100:8080;
1 ^7 O* t0 U2 x# B6 k server 192.168.1.101:8080 backup;
; e0 G, I @3 Z& I8 Q}
* n( v- [" `2 h4 d7 g* c$ }. Z! i! ?+ B5 F
server {
7 q# p2 D4 p( N) jlisten 80;) }3 V$ j' n0 u$ e9 O) h
server_name 52os.net www.52os.net;% t2 N! f, G$ Z5 |* b
4 {: r' O! F' k# o! H
location / {9 l3 e% v, @' ?$ b( H' i. I
ModSecurityEnabled on; / S8 i& s2 w( [& s
ModSecurityConfig modsecurity.conf; & f+ ^7 O$ i) W. X/ A; G
% T. b" `3 n2 C' g" j
proxy_pass http://online;
. `3 j- c* M+ d4 m# g" r proxy_redirect off;, t) H; I9 H. Q2 |5 `
proxy_set_header Host $host;6 m) J/ r+ B6 |+ t; m
proxy_set_header X-Real-IP $remote_addr;' x7 g: G: @4 A: G* R
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: E" T t" M5 U
}
/ d+ B" x/ Z6 d6 z) e( j/ n, \: z. _}
% {+ t3 f! p" G% f( J六.测试
% [" Q& v$ I5 P A1 |' a. J0 `* R9 O9 e. ?
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# q, L+ u8 E w J" e" z4 l7 A5 `
4 x A! ^* t. @: r% u9 E+ Z, h
<?php- G& d2 t1 {, I0 \' G( u
phpinfo(); - l0 E( z2 ^( c* x3 G* q
?>% O& I3 K, T# P, {$ |
在浏览器中访问:
# O1 d9 C: b$ e4 M( c3 a) H5 s1 f6 F3 [' W9 U& Z8 k+ }* d
http://www.52os.net/phpinfo.php?id=1 正常显示。
# G! d$ p/ A( A( e3 O k; yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& \& p. I) t2 w2 A
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: O' G; l0 _5 e
说明sql注入和xss已经被过滤了
B: V: v% K) {" h- A# A- S1 [' o' I8 b# a
七、安装过程中排错
6 M, ~( Q% h Y4 X. S, n% _( }( n3 q* L6 P; ?2 N1 l
1.缺少APXS会报错! z/ I* V$ ^ f/ ^. m* K
- G# Q4 U) E, e. \6 V
configure: looking for Apache module support via DSO through APXS
. Q% o. l/ `" a0 ?% s5 oconfigure: error: couldn't find APXS
7 y( R$ y1 F* V; Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ h0 Z8 b' B- H2 s K解决方法:4 _0 Y) p! W6 s$ X3 p( Y
1 e3 ~9 E, u( k9 m4 u U' c' e3 ayum install httpd-devel6 @9 ?0 Q# f7 t0 ]. |$ G2 C
2.没有pcre
2 b5 A& J1 N: I% }
/ Q! p6 d* q: F2 x! D7 |8 y' K, fconfigure: *** pcre library not found.% z% _8 I' B/ G6 I* q Z
configure: error: pcre library is required- w* l$ c" D1 ^3 f# q, z! `& z
解决方法:! [; [; {5 C9 h) o/ ^" h, q2 ^
" c _# O" X, F, c8 M% X7 X
yum install pcre pcre-devel
$ ]) x! _, E0 R9 a/ Y% d3.没有libxml2+ f& J; Z8 E5 m- U4 q: K2 o* u
: o( ]2 ]! \! z0 e
( u5 |, F' D7 L# K+ f" o" J1 c$ p1 Oconfigure: *** xml library not found.+ S+ i' s5 n, }' Y7 J/ i) L
configure: error: libxml2 is required
( u4 ?' ?' N% J, j4 f8 h解决方法:
8 `0 k1 ?) s' R
5 C6 d% X ~, ]6 @yum install libxml2 libxml2-devel
5 M; d* T! f b4 Y4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 w2 D: |: P' |( s1 [
7 Y% w F; E1 d9 a$ HTengine version: Tengine/2.1.0 (nginx/1.6.2)
0 L+ T" ? }5 \6 snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 P8 w) w0 s4 U5 J原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& J2 n7 N1 q9 ?8 @9 D
4 N% M' g/ z6 q2 x4 H [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, _6 D; q% D. w6 R3 {' p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) A% K) z$ s# x7 k# Z6 T5 B9 Q8 O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' y/ Z: ~' [( R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 s) z2 u2 E" t4 {, s8 H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ `; B! w4 x# b* K$ |4 M9 p' [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., A( t+ @ W4 ?% d1 P! ~
解决方法,移除低版本的APR (1.3.9)
, D% |# M& e. ^0 s4 j! s# M
5 m6 X6 A; B2 t/ Lyum remove apr
N) Q9 X! G' I4 g5 V5 n5.Error.log中有: Audit log: Failed to lock global mutex
" q7 t2 @( W* g6 q
5 d, [6 T8 i& N0 s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 ]0 j6 W$ P! }1 Iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; V' i7 C; B/ y. d3 j
解决方法:; }! [1 [/ `; a# y/ L
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. C- w+ e: |! {8 Y3 A
) q) j0 r+ j! J+ W0 `2 ]7 E( YSecAuditLogDirMode 0777
0 f4 u# O: q4 o1 F: B/ ^3 e0 S! DSecAuditLogFileMode 0550
! B0 F1 `: s3 |4 ZSecAuditLogStorageDir /var/log/modsecurity; v8 H( k; x# {/ S! h$ @
SecAuditLogType Concurrent d& X$ S' `$ N$ w( a
参考文章:
5 J& ^4 S0 E' p4 @% \https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ ]% q- ?$ G9 i5 }0 b* p8 i
http://drops.wooyun.org/tips/2614 |
|