|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ s1 h$ z& l" }- s) Y7 M; q: a
9 G) [0 W2 f5 ]8 n/ F一.准备工作& W; A1 u( S" P6 B' ~, w
8 L1 a7 f1 n% W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. G3 q6 y0 L1 u
) E7 z! C" r% I; N9 Z: k1 ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( h8 Q. i5 O7 b( T. X3 s9 R7 D
8 H6 _4 g* f5 x y' K: kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 M' F9 @. G+ I6 {
$ \/ [' ?/ M& j. Z) Q, m: a* `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 \3 A5 F! h! r: {6 P# [1 I
' t( o! a! ?7 K r& _3 ^9 c依赖关系:
" O, b8 F6 [5 U. e' }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* s" E' H2 V+ e7 P A. T2 \8 r x4 b6 o$ Z) m6 b
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 W3 E$ W9 z4 J
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 T& r) E( z5 h; P4 p& p
1 Q% O6 y( |1 M) X p- \$ b* Q
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ D& [& k- P: m1 X二.启用standalone模块并编译3 Q ^. M( r5 ~+ L+ s+ {& Y
: |1 m l( k# x( q
下载modsecurity for nginx 解压,进入解压后目录执行:
`/ b: p9 W# a+ p) ]( Z5 P/ X
. s6 ~5 q- N6 J./autogen.sh
. n0 \" Z1 |; W1 ~$ W./configure --enable-standalone-module --disable-mlogc/ o7 N$ i( y' S/ Y+ X
make
' d" Z" k7 _0 S, ^% O T三.nginx添加modsecurity模块# J( _6 c+ r" L! u' z2 k
$ N1 H: G3 U) l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& C u0 x' d& g: n; \0 A3 c# P: U
- j: k5 n4 Z5 D1 G. t2 l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
) y5 P' r; {- x" W! hmake && make install. m% N" r- k* B1 ~
四.添加规则
& ]# m: X6 }, b; i8 F7 P- G0 C& B- I+ S! [4 p! f. P5 N5 t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 U. h1 _7 }& ?. ]: z8 r/ s+ w9 Y) q3 j* [+ \0 G! ~
1.下载OWASP规则:
2 d/ ^! k$ V1 g7 B6 p# m
7 |( X7 M% ]7 V( K" }git clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 f8 z9 _" W1 r/ _# y( }# _/ k
2 @/ {9 V0 U- e9 A: }: I& ?+ cmv owasp-modsecurity-crs /opt/tengine/conf/) u- m0 Z4 w) } Q
/ F3 V3 N W, C- o5 r
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* ~9 ^! \* z. l. V
2.启用OWASP规则:$ ?9 a/ L& e% E5 o) M2 a( l
8 q1 E& u+ g% I7 T; P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) R; |. ~9 R! x9 a7 v* x- f
$ O1 C* x$ Q' Y& E. J8 [9 j6 X( X8 d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ m3 c( ]* t* x2 c# V
! u n9 e7 }5 d9 J: k7 Bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
+ D2 ~1 U x6 l3 Z4 b9 {% t2 K( k7 ?( l3 S% U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- E, |5 \+ ]8 V" a6 @& l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; D, G- z: o% ~) |0 _8 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 B; k3 O! {$ |) S1 I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# l- S- y9 B/ U, W* v" y0 _0 s Q# YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ A4 Y5 ?* m8 C$ h7 cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 W) {0 J, |3 M9 C8 M( X% }Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 y3 w* G$ _0 r7 e5 B; K3 s
五.配置nginx
+ s {) G. a; \$ v# k/ e7 Y3 H- K9 _9 e. n
在需要启用modsecurity的主机的location下面加入下面两行即可:* R3 b+ k$ V! {+ V9 x6 ]" Y3 w
/ A! ^7 e1 _' @ @ M$ x* _ModSecurityEnabled on;
B9 ~2 } P9 D1 j7 ?ModSecurityConfig modsecurity.conf;
% z1 z$ l2 Y% @. L6 c4 w; A/ T* [下面是两个示例配置,php虚拟主机:
) O# Y* T8 g6 {. U; ]0 s3 I0 m. K$ B
server {
. O- m! E$ S/ N. X listen 80;
0 K' C/ R" M8 L% T+ ~: f: K server_name 52os.net www.52os.net;7 u6 f- T9 U" I" _8 G! [, L
5 D. x( ?! }( q
location ~ \.php$ {
" {# | R+ m7 x6 W( o ModSecurityEnabled on;
. r( z4 u o% s* q ModSecurityConfig modsecurity.conf;
( S3 [% E3 ]/ B# N9 L& l- x- P# [ E4 u
root /web/wordpress;
! Q, z8 H. _( N2 A index index.php index.html index.htm;
0 {, Y4 \9 {% E/ ~3 F; Y" G1 Z 6 z7 Q Y7 B' v" J2 F( u
fastcgi_pass 127.0.0.1:9000;
! h, `6 J1 I/ s! ]& C3 n+ y. r _ fastcgi_index index.php;/ Q% V6 n+ _. P% K
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
" j3 K! z1 G$ J* ], k/ @1 L0 O. z include fastcgi_params;
" {) _! @) i2 K" h }* ^ Q, @) x% n1 ?
}6 g; J' s9 _$ Z- H6 F$ y
upstream负载均衡:
0 e3 x3 h4 J7 W6 d4 e+ o6 d% b! k
upstream 52os.net {0 k6 C0 T6 m9 H0 K
server 192.168.1.100:8080;
8 x% s X1 {/ B* o3 k server 192.168.1.101:8080 backup;
- b# X' v- b: r0 Q4 k' e5 I}
7 W5 S, f8 ]. _7 h3 G
. G+ d% ^) q5 B* y* eserver {9 o$ r1 N& |% M, c% `, U, k
listen 80;
! ~( x- [0 j) F: V$ hserver_name 52os.net www.52os.net;
) E* U" R" W. B, _3 J! m+ }9 `; z
) }, ], ]0 L, n% k) {$ Plocation / {
/ C+ q/ i8 h% ~* F ModSecurityEnabled on; ( W$ R! g9 ~4 B& b, R) M# Q9 e
ModSecurityConfig modsecurity.conf; # g. _( C A) [! B- ?
M% E( S; z5 R. n4 U
proxy_pass http://online;
Q/ O4 g* [. L2 b/ [ proxy_redirect off;
' R! q& C: v% ?+ C proxy_set_header Host $host;: B! o4 U; q" Z1 Y. g' d
proxy_set_header X-Real-IP $remote_addr;
: q4 U E4 J1 |$ I proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ s" s0 W v( n' V; {0 N+ y" @ }- c# `; x- \' z( Q5 C2 ~8 w
}
: N# Z9 o9 E" T* V3 M六.测试
4 \7 Q# X: Q& I% R; l, Z" s0 b
% \% i: Z) I8 J+ Z- i0 `5 c- \我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ A' S0 s) c- f q5 n" h2 L9 [% l4 Q) K. r" a- g0 q8 |8 p
<?php5 G: \: N" K* R# n, Q0 C3 O; q
phpinfo();
, [5 @0 V! O2 y( t% y?>5 n. h# X8 L: J, r6 \+ `
在浏览器中访问:1 T8 Z( b5 D8 ?/ R* z/ h
2 f( `) A( W9 {* X8 Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
, c. J4 c4 G' H* ?+ Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
0 a* a# s8 P' khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. b# J3 t4 J% M2 c+ P- j
说明sql注入和xss已经被过滤了
( w5 |3 ^6 k3 A9 _( g) M( Q! H2 k; m7 I7 ~3 x
七、安装过程中排错7 h% @# K9 f7 H, H' H4 u3 @
" ?9 \4 c% s7 z
1.缺少APXS会报错
, i5 e/ G, |# [$ {; Q6 y
, ^) G+ p* z8 L8 h$ b0 k3 Econfigure: looking for Apache module support via DSO through APXS/ L! c6 C2 c7 o6 y X. w
configure: error: couldn't find APXS
6 E5 I4 E3 T8 |6 y/ ]& eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' B2 @6 t: ?4 X" A3 e) B: Q- J9 ]( B
解决方法:3 O! j5 e- P& X$ q _( q5 z
6 E% M9 I+ ~3 b* g. {; E
yum install httpd-devel8 @- q" B& e7 Q8 `$ R: u( g
2.没有pcre
R W/ H* M1 V5 x& Q: J0 D
* v( i6 c+ a- f: _5 g8 Cconfigure: *** pcre library not found.
( ]% ^+ r) b* u" Kconfigure: error: pcre library is required q1 e/ M! A n' A6 |% A: q
解决方法:4 E$ l( E! @* k6 v; V; C W' g, o( r
& d" S: O2 g1 W, q2 M& N9 pyum install pcre pcre-devel
% [7 l: e q6 |( y( Z5 @3.没有libxml2
: [! w$ L( I, M5 y! G$ g: B1 |/ }* T" i$ ~. T. O0 A( E [( G' }7 ~
" ^* t: }4 r- D! ~2 m! @' c( Xconfigure: *** xml library not found.
$ J& J$ ~* k) J1 K+ Tconfigure: error: libxml2 is required
* T6 f K) j/ F) w解决方法:( Y) X1 w0 c+ L7 P1 U& ~
; H, a% l0 m" M+ ?2 ^3 }$ K- Hyum install libxml2 libxml2-devel# Z' F' x. Q0 {* g5 e
4.执行 /opt/tengine/sbin/nginx -m 时有警告
* n5 ^2 y6 G0 P' k O
0 o. Q+ @+ W1 h3 |4 v) L0 g4 RTengine version: Tengine/2.1.0 (nginx/1.6.2)
4 V* y9 y/ r5 F$ G' Snginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 x: R* \, W+ j8 s* c+ v( o3 l
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* \- J* L% \6 r& u5 U
+ u* d% j. q4 Y& y- T- U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- A0 R$ g0 u/ m y p- ], U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 r3 D! J' `/ y7 ]1 l. h1 [2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; r5 S5 E! z( H5 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
; n. ]5 c, h; Z g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ P5 S: V: q. `5 G# n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 G7 p# Q1 b* j6 Q8 I' |
解决方法,移除低版本的APR (1.3.9)# o' k+ y# ~ L% Z5 T: e
$ q# N, v; G* S( U! zyum remove apr6 u7 N" O: G- v: z1 T; `
5.Error.log中有: Audit log: Failed to lock global mutex
: A0 m" E1 [' ]( G' Y; C+ O, w- [! X& b2 y8 E
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 s4 z2 [7 Y* U9 p& f1 Y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* }3 n s8 T% n f
解决方法:
+ v5 d# Q- k9 ]% b3 N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 }, D% M8 u0 A4 J5 \
' U6 K8 j( L3 p* DSecAuditLogDirMode 0777( H2 U1 e( u* v7 X% }
SecAuditLogFileMode 0550* I+ D% p" S ~" ^; U3 U6 _! i/ D4 T7 J
SecAuditLogStorageDir /var/log/modsecurity
; }" L- |" | [+ s6 t3 p! aSecAuditLogType Concurrent# [8 j0 V& A0 D/ _6 x& |
参考文章:" G2 c$ B. y$ L; I! @% y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* W: R* K: d* z3 y
http://drops.wooyun.org/tips/2614 |
|