|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 k5 F! q. o, v. O1 |9 U7 P) t# w, ^
一.准备工作
6 b* {9 p3 R" M" N8 C* n6 k7 {5 ]& q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 _, h9 ^" R, a8 ?
% p. C5 ^, I. g3 e+ ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz m% P) ?7 x- Q7 a6 _5 h
7 }* M3 ?& `7 c. U7 ]" K# J2 [modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 k2 L% |6 M) E: B; d+ u" |# { {
; c J% |9 g! n3 U& m8 H6 E$ ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* W+ x% p0 S$ j) U( r2 F" x9 g4 z2 U2 c. v" j- B
依赖关系:/ K9 h' h( K: q: Z7 }0 p0 I, f4 _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) `7 T4 ^1 l! \2 }0 t- r
$ o/ c3 G5 y/ @' lyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 ]3 h2 Y% z: m9 f0 Lmodsecurty依赖的包:pcre httpd-devel libxml2 apr# F. K9 k5 M8 A Z; B% [
5 n' [8 {3 |/ l" ^2 `; g q1 v3 Fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# Y' V* l5 ^; k
二.启用standalone模块并编译
* H, T) i9 `' u) B! W
# s# Z+ L& F1 T! {4 T# ~6 ]1 x( B. l下载modsecurity for nginx 解压,进入解压后目录执行:
: i; @8 U" {8 w( w( a6 X; x1 Y4 D
./autogen.sh
6 W6 q! N3 ? q3 a' z, s: q5 ^./configure --enable-standalone-module --disable-mlogc {) w4 @% {, F3 a( p3 G% W- K d" T, {( i
make
, Y: @# H+ i5 {4 d/ G" z三.nginx添加modsecurity模块! w% W% ~( P) _( R0 x
_+ v2 W. `, T9 B) x
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' R$ N' e- |0 `, C
K* y. p- i7 ~
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 o6 {. x1 u: b
make && make install5 \. ]9 k0 n! b: k& ~2 X* _
四.添加规则9 p& n- l3 ^; B9 m* d6 V8 C
2 i- }) Q4 x2 J% g5 j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 P2 @" z! w! ~
: v# @( O1 B, M1 d1.下载OWASP规则:! H, ^8 n' f& H% h& D
# b# G8 `9 Z; O3 U, B7 ]
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ p& K* q! B9 o1 i- C
& m8 k) F; n- K- h* Bmv owasp-modsecurity-crs /opt/tengine/conf/
8 G$ N9 H2 i2 }8 ~/ K* y
' }) h4 U: I) z3 D7 D6 \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 X' x% h, P B; g1 {
2.启用OWASP规则:2 a. u' u+ k1 F8 d, V
* {- |. Q, ?$ u1 I复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ o- w( D/ [ Z: L3 R9 z! [, ]- G E8 h6 d" c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ q" ]7 f* W4 m/ s1 X' v$ h# ]4 g
8 S5 X# D* @$ w4 S, F; L& A2 `& g
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; N$ e9 |' Z$ K' m: A
9 _; o6 [( i* r* kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( q. d" ]7 M$ S$ s" I9 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, T, Y- ]2 ]% }( j" w) i$ @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ t# q: q% v6 M+ B* l) c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 V3 }; \. f/ d3 ~: j5 h( LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ D' n) G" X2 U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 d- E3 t% n8 m) P0 TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 V2 J8 W4 F5 n# O) V
五.配置nginx+ p& C& b4 U0 K1 [! e6 ~% n# W
( i% A- t1 [) {% e2 H- P6 V' ^在需要启用modsecurity的主机的location下面加入下面两行即可:( s" e. z, \ C
$ N/ |/ J y* V8 H- O9 Q% ]
ModSecurityEnabled on;
, I. J" e2 V' ZModSecurityConfig modsecurity.conf;+ y* g- g# s/ T; M& g
下面是两个示例配置,php虚拟主机:) Q- z) W: J0 U! Q9 {# c' j% v
+ `2 o. }/ m- ~0 V0 \+ T
server {
- G \/ b2 F9 h- r listen 80;5 A1 ~% ]) E3 B+ Y; L X" K
server_name 52os.net www.52os.net;( C5 z3 U2 l$ C/ e
! R8 p P+ E1 T$ [8 F location ~ \.php$ {8 @9 w7 |/ Z" x, ? o1 a. z
ModSecurityEnabled on; $ N$ n( S0 Y2 c
ModSecurityConfig modsecurity.conf;
, K5 y$ y+ x& [4 V3 Y
9 c5 a0 O* L1 v$ Z2 p root /web/wordpress;
9 }% a# {: {' z! k; M index index.php index.html index.htm;- ^2 U9 @! B' f, F# o/ r
2 D1 n4 ]2 ^/ q4 A5 s9 z! R fastcgi_pass 127.0.0.1:9000;
6 o2 U0 b4 ^2 A fastcgi_index index.php;
$ P- G7 A- J# X fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" W/ J( M% }; B m7 k, q
include fastcgi_params;
* N( W) } H' G1 i+ |* N& F1 X }
) w9 @6 T% E, {) E5 x6 Q }
3 _; }6 J0 D: _. ^/ Jupstream负载均衡:2 x. ?; V/ a6 i. V; W
2 D9 } ~( Q& T5 ~% M5 P' Mupstream 52os.net {
L# {" p7 D8 Q4 M7 O server 192.168.1.100:8080;
: v. ~6 ~) N: k4 a( N, c) E3 ~7 S server 192.168.1.101:8080 backup;
' i* o+ _3 O' }2 C" z: S. o}' H" D5 }" e- S2 b
' H* u+ I0 W! p# e: K0 }! L
server {% c9 e; @7 Y# x" r6 \- S
listen 80;7 F8 V6 \" q/ \* i$ r# ^
server_name 52os.net www.52os.net;) |7 r. w1 s! z( E
( h/ ?: b, F6 @: F) Glocation / {
+ L) G* m: Q$ x4 B$ d ModSecurityEnabled on;
( x1 [, k) u2 d$ `0 h, B ModSecurityConfig modsecurity.conf;
' F8 Z' {: n) T+ P% O- X. y: F4 @6 s n" ]" Z
proxy_pass http://online;
0 Y' F4 W* x% g7 h4 G proxy_redirect off;2 Y# _( O2 v+ n4 T
proxy_set_header Host $host;3 `4 Q B/ z$ s4 n
proxy_set_header X-Real-IP $remote_addr;
8 s0 V4 g: Q, t. A; T proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; u, [8 O3 H6 ] }
9 [! h% f1 F' e% W- y}
3 d7 y' J5 m5 i六.测试, }6 d, a% m z; l$ L9 Y. W
1 ]5 Z6 ]$ s/ a, |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. T+ g9 k) v! q9 T) X/ }; n% X& b
, P. i' r* Y9 p5 B' L<?php, N3 R4 X$ A3 k/ t% W5 G
phpinfo(); ' k$ n+ x3 ^8 D5 P! }& O
?>9 p2 l) v6 a/ n) B
在浏览器中访问:; W p: A1 f7 S: X3 Q, S
( g8 S& v- H( h$ i/ `/ a- r! }' ahttp://www.52os.net/phpinfo.php?id=1 正常显示。
/ |% N" Y5 R* {. ~: Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 ]( a7 } I5 A2 e. k7 b3 m G) X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' d% X/ j" v4 j3 G b# h3 `! I
说明sql注入和xss已经被过滤了
g" K9 S9 K# ^+ x9 ?' k3 y/ R- U' H$ a$ Y
七、安装过程中排错: ^; c4 x+ G4 `8 p
A! Q3 S) B- j. n/ l1 I* O. c
1.缺少APXS会报错
9 S7 d& H4 k& `1 l5 w0 A, o) p. ]) ^, }& |: I
configure: looking for Apache module support via DSO through APXS
* h, ~+ b+ m! ^: Vconfigure: error: couldn't find APXS
+ G4 c' Z! l1 @6 t% p2 ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) [8 s m! K" f/ r; Z* a) C Q$ S& i+ T
解决方法:& D7 J7 Q# g; i! k$ }+ C( v
9 O. d; Z `& x, B# ~- A+ |yum install httpd-devel
4 V8 R4 P9 i2 C( \) \6 F2.没有pcre: I0 Q# G$ {/ q$ E# Q: y
9 L+ Y& B! ]8 o5 v
configure: *** pcre library not found.
, y% |" r4 }& @5 Z2 L* E3 @- ^configure: error: pcre library is required l7 X5 S7 K+ d* z
解决方法:1 f+ B$ L# ~* [; i& m
5 n9 `( C# G; `$ f; }: l Z
yum install pcre pcre-devel
2 _' V) y* i Y+ `7 n* ]0 b3.没有libxml2
" d: g. {! M! q2 {# ]: ^$ ]: i0 }- E$ z
5 {6 n$ S0 X8 j& P5 U! s
configure: *** xml library not found.- `: u0 ~9 ~: _8 z7 m3 y: e
configure: error: libxml2 is required( g: }2 u1 A6 j9 F4 u5 d& n% @9 \
解决方法:
5 Z! t) n) X( [/ K" F {1 L$ f1 j# H# x( x3 S( N5 M5 L+ \
yum install libxml2 libxml2-devel
! v( @! X& _. p! j$ U' U- I2 I4.执行 /opt/tengine/sbin/nginx -m 时有警告9 V0 f7 F1 L, g2 C; N, `0 l# e7 C
6 `3 {! [& |$ E2 p2 }' b& I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* A. A8 Q+ {0 u* G% S9 M! P+ l; vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 W) O$ E9 ?) t: p
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( w# ~" O( o$ I& S+ N
7 Q4 f& t+ y5 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 r" E4 u& w( t2 F# F. g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": y# v5 h% O L
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 v/ `( a& ^7 G" x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( ^- Z' P6 J) ~6 U2 @; U; B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) l1 t; V" D9 W+ q" h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 ?. M6 m0 e; ~: f( I
解决方法,移除低版本的APR (1.3.9)8 i/ e4 Z% a, z3 u/ O
5 G5 }' I1 L9 U! t
yum remove apr
* u+ x8 i2 ]2 ]' W9 L! d0 h5.Error.log中有: Audit log: Failed to lock global mutex6 W3 Q/ Q( o4 Q5 P6 M; |
7 c7 k h. ?3 l |* a Q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 B6 \( ]- `0 a4 W/ s& Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ g' e) t+ Y9 n6 e- p0 \6 K解决方法:
1 @3 ^# {5 C: i+ R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 T0 k& a: u2 t3 z z- U4 q% ~. N
, F5 n; P! F, k' o& ]3 T: D
SecAuditLogDirMode 0777
1 h! ^: K' x) i0 fSecAuditLogFileMode 0550
! e7 J( o; K6 X t# J6 PSecAuditLogStorageDir /var/log/modsecurity/ D4 {) Z u: J1 }
SecAuditLogType Concurrent
) n4 t- y. D$ j! @8 e4 Y参考文章:8 ` S6 P. c3 K& R. \: y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. l! T9 Q) O5 o6 W& }4 X
http://drops.wooyun.org/tips/2614 |
|