|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 L3 M+ C+ E, V: Y' Q7 D
4 j5 X( s6 i: F1 D1 T0 Y! R4 J
一.准备工作
/ w, [4 ?! C. C" y Q/ x& f; |% X# l$ y. {7 B9 O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' }: i& x# z. D6 G
3 N% H) L: `" {' M) d* S
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 `9 p; F( Z/ `( B/ \+ F, n
7 R0 L* e+ q8 I+ S/ N1 P' m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 k! d6 j! o/ ]9 i- H, {0 V2 n! X+ x7 `! [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. k& L7 }& m+ H, H" p2 g
a8 V" C+ r- w! Y+ \' z
依赖关系:4 Q2 z; E& ?+ y" {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ k5 u' u! @( B) A9 d. z, _, t* l, g+ L7 @
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 T; }- L3 y/ N/ {' _3 Y* f% H
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 i, P, u9 F- f2 z; H; \" R7 V5 _8 l" Q2 ^* k0 b' f( {: G6 k
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ i3 y9 L! }6 X* d! f4 b二.启用standalone模块并编译3 q" j8 v: t5 ]2 O+ g) A# j
, G% h& I# d! _+ m0 S5 {下载modsecurity for nginx 解压,进入解压后目录执行:
7 R$ W/ k+ S( x1 }3 |
7 G5 n4 F5 u* o( D P./autogen.sh
% J2 e$ u) l% ]& X./configure --enable-standalone-module --disable-mlogc" M; m+ x. l, p' J) i: a
make " k7 f/ x* e+ s+ P) B
三.nginx添加modsecurity模块
6 i& ?# ], z2 P9 ~; ~+ ~
7 V5 O+ o {3 l, d7 E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 M9 O* K+ N; K: j6 | l1 `: Q
! d" m6 d2 G4 ^+ J# L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 D9 S4 v* v$ X# R# m0 }( lmake && make install) O9 |+ ~& `# i- {+ O7 }/ c7 E
四.添加规则" z b- R2 C% Z- K! X6 Q8 P
0 B# \3 h6 |) C- ^5 zmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# [% U. m5 Y* b' X
8 |2 Z6 H* b1 e, ^$ X1.下载OWASP规则: T) _+ c7 _0 ~/ N9 T- }
7 {0 O; T. z( A5 g1 q3 o- S: B! X' C
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 [4 r8 M/ C2 T7 T i- Q9 M- \7 f; A9 ?% b# k1 }+ W4 k
mv owasp-modsecurity-crs /opt/tengine/conf// D2 [+ C) P; |3 i8 C4 g
2 {% ~, U7 L! }. Q- j( ~* bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 j- G- j) h0 u! u d
2.启用OWASP规则:" @6 f( n- R' h' J
" V, ~% a( j$ n
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 v) z8 ?& Z ^3 |$ R
7 I) J, \ u$ E# P& ~; d: ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 F5 T3 q `, G! d9 x! Q( m% T, u9 T `3 @3 P
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 }/ ]7 V+ e. }' l
, l1 [/ f8 I5 X- G$ o. r0 Z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 w! x3 Y' D: P9 S2 u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ ^6 H, s, U: r/ _- j0 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ b! T" p3 D% t7 {4 bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
I# l P# E2 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 }4 P2 Y8 Q( M/ g- K3 vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
/ d- m. ~6 x- Z/ ^( {$ p0 r; hInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* v1 }: U3 e7 I$ _五.配置nginx+ ~' j7 f2 P, `. E# B
6 Y2 O5 g" f: u( @+ U1 p
在需要启用modsecurity的主机的location下面加入下面两行即可:2 j- g6 d$ m+ x/ o7 q. r9 q
1 I6 \4 J2 j$ {8 D* FModSecurityEnabled on;
. |/ a$ ^# ~. `2 r' W. YModSecurityConfig modsecurity.conf;* a4 ~$ Q8 a0 }
下面是两个示例配置,php虚拟主机:8 T! q! R) `# E. U6 U
/ ?6 o; E- z8 [* A% Xserver {
% Y, k: m" T0 H, ?) }9 W/ @5 G& U' G listen 80;
0 \' w6 B2 @: H- q/ ~( u% \7 N server_name 52os.net www.52os.net;
/ n c' i& j$ q / D( v( U& L. m, K0 @
location ~ \.php$ {$ a* O" Q3 T0 m, B
ModSecurityEnabled on;
8 ?% d: {7 d5 K4 G( e" b! Z ModSecurityConfig modsecurity.conf;
! j3 u. v& s. J" p5 V: D- i4 C1 ]. n' J: h6 p: Y9 V
root /web/wordpress;+ V% a6 R" D4 T
index index.php index.html index.htm;
% q( `+ B0 X3 Q6 Q. ?* J
% L) U4 `' ?; [% u) Z( Z4 P fastcgi_pass 127.0.0.1:9000;. t6 z% `# y6 a7 T4 R7 ?
fastcgi_index index.php;3 Y( J( w& ]* {6 f
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
/ v' r" }: R* r$ k include fastcgi_params;8 b& o" [$ }* Q- w6 _& P& {
}
* w; |) [9 J" C2 i2 R }/ S5 w9 n) d: Y! I7 c j
upstream负载均衡:- Z+ L E# ~: {. U3 I
" ~+ U9 g, D' ^4 T" y. F5 \
upstream 52os.net { V5 C* Z9 l' @8 k0 ~* R
server 192.168.1.100:8080;2 T* [' `4 A/ t2 q
server 192.168.1.101:8080 backup;: h3 ]' a" c4 Q- v
}2 [5 b( n8 T* F4 c3 b* A8 E; F
: n0 B7 A; k* T* L2 l: i
server {- L7 p+ X+ V3 T
listen 80;' y P+ X% u! R/ l! m
server_name 52os.net www.52os.net;$ P% J. Q F M9 W- ?
. [+ r1 N/ m/ \2 ?" Nlocation / {
+ d! g! T c+ y" @ ModSecurityEnabled on; / C/ j8 @# `6 K3 D4 ], T
ModSecurityConfig modsecurity.conf; 7 T. m( k) {3 x$ p$ b
2 t2 S+ o! M- J7 A/ G8 J* z2 k
proxy_pass http://online;0 N1 ~; O/ ^/ O+ y% G
proxy_redirect off;) X' q7 h7 o8 w5 M- F, F4 O. d3 H
proxy_set_header Host $host;7 U. {7 B% a6 k0 ^ i0 ^
proxy_set_header X-Real-IP $remote_addr;
) q( {0 q6 z. x3 b! h" h: D2 u, \ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 d- `( q a8 i% k& i1 V }# o$ p1 a# W+ q+ t& s0 O
}# A5 `' W) ?7 m5 y. g. m. i5 m9 T
六.测试" c+ q, a* ^5 R. b) G: j
: p( D- v3 X) ^3 H+ @
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, c0 m: c. i# C, z! T8 T9 m- G' }! @ f/ k$ S7 s% b$ L; j
<?php
( o- G8 l7 h: D) T7 }3 {' r phpinfo(); % }3 @4 i4 k7 X' ~
?>7 E% Y0 S+ _ B# M: _! `0 H3 F
在浏览器中访问:' X8 V+ z- s& Q
" B9 @# r u/ [0 x4 m' w5 Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
8 g, } N q( {7 w8 I2 S# Ghttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ D; _7 X) t3 C
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- w8 u" I( ~) e) ^1 m
说明sql注入和xss已经被过滤了. q3 s# K9 V8 @! j: E
5 a# c3 k: g H W
七、安装过程中排错
% A4 c1 \* l/ C1 l+ R( W- q' }0 H$ C7 v+ k/ [- N
1.缺少APXS会报错* d; h* }. m U# O
( t+ B* P, B+ o9 A5 c
configure: looking for Apache module support via DSO through APXS: [% ?. i9 V( M) L% r
configure: error: couldn't find APXS
* j0 J6 C; [+ fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: c( m) T H( F0 [, ?$ _* @解决方法:' @( Z* d4 U2 C: B3 E, a( H4 \8 B
* H9 E2 [- G' x* u' `
yum install httpd-devel5 z: U. W; A/ t$ |: r* _3 H- }) N
2.没有pcre; s' p! s. e% y6 H5 u7 G. [: A6 y
( C' [2 W; ~3 f7 m# N1 j1 }
configure: *** pcre library not found.
6 ]; b! \. q6 X+ P4 U& j+ Bconfigure: error: pcre library is required
& N$ e) h3 n7 U! q: }# j% _解决方法:; J0 I0 }& v/ r7 B1 N2 d
) E0 H; D# m; p- k1 g' ?! Oyum install pcre pcre-devel8 x) L2 D" ~+ ~ t9 v, L
3.没有libxml2
! [1 \8 ?+ Q: l( h' f
, x% L( p4 i- _ C0 B! A
" i9 W+ ^! Y, {1 a& H Tconfigure: *** xml library not found.
; [1 e% ^/ n r5 u+ Q4 w- m$ `configure: error: libxml2 is required
% s6 n1 c$ G& O2 i& i- w `解决方法:! q3 x$ ?: q3 e: x+ Q+ s5 P
# H% z( ~% O* [# \0 L1 \, U- Tyum install libxml2 libxml2-devel2 J' v1 }' }9 E$ \( h; d
4.执行 /opt/tengine/sbin/nginx -m 时有警告+ J4 \" p4 Q* a) N+ h* `( Y9 _
: B3 ^8 \/ d, v* V' w! m$ ~, z! hTengine version: Tengine/2.1.0 (nginx/1.6.2)
% m, {6 s* l0 |" g/ Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) j' h# \& d5 C4 f6 u$ ?原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log l( G Q6 J- _$ f
$ h$ k1 a5 o6 j2 s: C) s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 i$ O: ?. b" H' }0 [9 T; X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 t5 u% a7 |& S9 r7 R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) S: V6 O! K, I9 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( W/ b- {- u* C8 [! S3 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* `; q! w$ C7 c7 B$ ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. @" M6 \$ u2 h; Q4 t0 m1 y v" Y解决方法,移除低版本的APR (1.3.9)+ n7 v7 |8 j" Q* ?7 ]" {
3 C1 b; C) N7 R! z# eyum remove apr
; ~6 C: ^1 a2 p: Z& |5.Error.log中有: Audit log: Failed to lock global mutex. {8 E0 m5 q2 R" F0 q3 D
7 S' C3 J# l( ]( K, a( W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock + U# X- P0 m/ F9 O& ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 x9 D- C1 F/ ^ h
解决方法:7 U/ K0 C2 k" z2 Z, g, n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% T0 d$ O+ q. R O' i# H) m8 ^8 L& n; j. R
SecAuditLogDirMode 0777, `/ q/ a& K0 v4 y; ?" Q- g
SecAuditLogFileMode 0550
; S/ U0 Z7 y, A: ] ~. [( `SecAuditLogStorageDir /var/log/modsecurity
, W/ Y! h( k/ B& N9 x2 @" rSecAuditLogType Concurrent
+ e% K% ?: U, U+ v. J参考文章:
! c$ e6 Z' K4 _8 thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
$ K3 |: v5 B. p2 B7 d8 e) uhttp://drops.wooyun.org/tips/2614 |
|