|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 Z' M1 P# P2 d$ ~, G s) r g9 t
4 S9 ^/ I+ Y9 `! f9 u5 t) V
一.准备工作8 S4 N5 M" A* V* d3 i! c& W
J* G! r1 _+ M- u! C系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% I0 G! n8 Z* O+ ?
) O. i9 R* q3 w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* O4 y3 J5 W% ?9 v3 W' M& l
6 V. Z0 R+ F$ m7 J+ L' \4 A, _- R
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( u; e4 k& h+ I) p3 y8 v& c
2 B* P* h1 O/ m* }* |) VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 A, `' N' i1 B& d, m+ H4 D
' i; `& K1 k% F4 t* ]+ I3 v
依赖关系:/ R; Y z% x, X6 }
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) Y! Z0 D+ u0 O, K2 H% ^; x% J
; b/ b* I; ?9 P( }3 d/ s3 o/ Jyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 _6 C( P; E3 h. I! f; ~modsecurty依赖的包:pcre httpd-devel libxml2 apr
! F% p P! ?4 v* u
1 h8 p! a+ T! i* y2 Ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' l! c% L" v6 }+ [! C二.启用standalone模块并编译
6 m4 S; c; R5 L. S
v& ` ^7 i1 f* V' M) _' S下载modsecurity for nginx 解压,进入解压后目录执行:' Z- O. e8 g' S) H8 U M0 _
6 L; H5 L4 g5 E./autogen.sh: v! l( @" Z$ q) h7 g
./configure --enable-standalone-module --disable-mlogc
1 J- x$ Q+ a7 \& `make ' ]. P- D2 F B, j: h) ?
三.nginx添加modsecurity模块
8 V. z0 Q, ?- H) T9 |! N; X, | q8 M0 g0 S) v
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 F) A* A8 `1 y3 \1 o7 b& Q' ^
; v3 N* [) d$ }# ~8 `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 k5 X2 D2 n& I7 P% w" O0 Cmake && make install( _, j4 g7 P \: `/ q/ B' T
四.添加规则
* m) |0 z# C% l' J' [- I7 F# w0 B! C4 c4 b) C
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 [' s+ Z) B9 b2 j6 r: z& Y y4 W! j
, {( }1 `6 _" ^3 }3 C6 `, L6 L1.下载OWASP规则:
6 d; d7 ]) X% |8 ~ X& B5 _# W, s6 ^- U' G" x. c+ y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" Z8 w* `# ]9 A* \. A
$ X$ e& D) ~5 C/ {- ~4 o# G+ `mv owasp-modsecurity-crs /opt/tengine/conf/
9 b6 t9 f& w( A( X! S
" ~5 A k4 X* `& a& [* G, pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; B+ F6 ?/ V! z
2.启用OWASP规则:
; M9 C* T1 u. |3 x9 ?9 b: L& S) i2 ?" J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: G( Z/ d z- \# H+ y2 Y8 ?! s8 W: }- l1 j5 x' T, d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- d6 {/ g2 Y4 n
( h9 _5 H, l0 M- x3 f' Cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, n, T1 r; B% ]: r. `: r9 E
/ H8 w- R0 p6 n- e- kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( R! F! b2 q8 R) w* i# c, I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 P T' l* n) q& h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. w5 H8 J( I1 p' |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 H6 u' R6 _6 O# ?# JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% }, T, A% z# iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
' t! l7 ? G# H2 qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# Y1 l2 r. x2 L6 f5 Z7 |9 b8 F五.配置nginx
% ]' }4 {" e1 J6 s
b2 f7 W0 b% i# i在需要启用modsecurity的主机的location下面加入下面两行即可:
+ ^1 v y1 l! Q9 Z+ h5 E
, c) H( Z7 d* J+ G2 B; Y2 bModSecurityEnabled on; ) O# `. \! I& G8 n
ModSecurityConfig modsecurity.conf;
5 t6 X- J7 g' _. ^3 p下面是两个示例配置,php虚拟主机:. n" p& \0 l2 B0 _" }
; c# M+ |! x4 g$ `, Qserver {& u# `* L* W7 E, P! L" d* L! A& L5 L
listen 80;3 d0 ]. b5 A1 A" j# s
server_name 52os.net www.52os.net;
/ l1 R& V1 l |, h ! ~: n* S& U( c* u; I* G3 I3 a
location ~ \.php$ {) w( ?0 p ^' X" m2 o
ModSecurityEnabled on;
/ o/ F' \( d* L; H* w ModSecurityConfig modsecurity.conf;, W7 u6 h% R, C/ y7 q! K6 n
1 _7 _7 M: h7 D6 S( Y$ f root /web/wordpress;
+ l {4 J B2 W% U. j E) y# ^ index index.php index.html index.htm;
* e- R- ?+ H$ \& | y5 D
' B) h0 q8 u: Q+ H# R fastcgi_pass 127.0.0.1:9000;( o: ~( z$ z) o. k' h: K
fastcgi_index index.php;8 i& i7 W. B3 Y% L' ` N" b+ ^1 N
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, k& C; s3 g# N: Q5 J include fastcgi_params;' b2 ~6 [% m3 w& k
}
5 v7 F3 w& Q. u( i1 H6 \$ ?, B }
& k% p* O" W3 B7 z$ Gupstream负载均衡:
2 q' Z. Z1 i$ n* u5 r
+ D; Q: [8 W7 Q3 ]: dupstream 52os.net { }+ P% \0 Z+ g1 w1 s
server 192.168.1.100:8080;6 T' U, {3 X$ l$ k# d! E
server 192.168.1.101:8080 backup;7 [: ~5 G) F4 N6 _0 C* f! K
}# c0 k" N* C. P3 Y+ G" r2 \& \
) G5 Y6 ^# P& D( D1 tserver {
$ z: i0 S8 S: O$ t9 ?4 W& e$ mlisten 80;
/ \. C9 R7 n3 c m, Y& Sserver_name 52os.net www.52os.net;
4 \, z+ l% z3 p& h% ^* o
: i2 i" p" n4 T- Zlocation / {
{" K; }6 y2 H ModSecurityEnabled on; " W: @: M# Z6 |2 }8 l/ w! d9 |1 N
ModSecurityConfig modsecurity.conf; " l% ?/ E1 n3 o+ F4 K3 {
1 T* R; `- T* t/ X" a5 a: w2 n proxy_pass http://online;" A& {8 O) S' _5 x" J( A) Z" z
proxy_redirect off;/ O% i" u4 m' v9 @2 {( R1 l& `# D
proxy_set_header Host $host;
4 c n8 O- P7 I1 m2 u4 F/ E proxy_set_header X-Real-IP $remote_addr;4 ^ N3 D6 h$ }2 x
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 V* k, Y% E7 U3 j/ P" {0 b
} B" [( q; B9 t* A9 d
}
2 H& s* R/ L" f: s* G六.测试8 f' o) r9 G0 |& B
3 x& U* G' Q. P7 T% e8 l$ ~; W我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: O! Z6 B( L& F! A( @* { K% \ o% t& V- `& y; |
<?php
; e; s! K- [7 @: |3 _: c phpinfo();
% a# q9 k+ S5 S?>
3 V6 E" ` d7 J5 W在浏览器中访问:4 O- x! Y. S8 ?7 K' ?2 ~
1 a* I6 l) X* n! {% w0 Khttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 I" A# `* L- r$ D3 e# p3 lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& l2 _( S% {- S
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。2 B$ b8 [2 d( B2 a- T. B- f6 A
说明sql注入和xss已经被过滤了 g! [, [: F2 r
: E. ?' M* e2 u$ U七、安装过程中排错1 ]) _1 E5 F _4 Y# n% q+ ~
* c/ ~8 @) ]' o, Z" }1.缺少APXS会报错
8 D/ ~7 p r8 L) ~6 x) Z/ }, b
/ Y8 o' [6 w9 f5 ^9 q! T% kconfigure: looking for Apache module support via DSO through APXS
# `- B& R' ~' W7 V/ _) g, d! p4 Nconfigure: error: couldn't find APXS( d5 }9 g% J' u+ G" D: v; E/ b
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 k% ?2 R. P# h/ I
解决方法:; b2 |0 N. B0 a( ~) d
~" B$ q8 Q& Z# ~( Y9 J: ?) \* i% N
yum install httpd-devel
; r) m1 L' _+ h. ^# b7 V `, L2.没有pcre
8 V+ I. L$ _5 E/ ~3 t$ Y) _ `. G" y2 @0 n( `0 u* d
configure: *** pcre library not found.* i2 N6 O3 A# f% v. ]
configure: error: pcre library is required
|" Z3 u) q# y1 z+ H0 q解决方法:
l; ]0 |! _7 m
6 `1 H. Z2 v3 I8 G) z- ~* dyum install pcre pcre-devel
; S3 r4 f) R( I4 S3.没有libxml2# ^2 _) p* M$ W; N$ l( Y
5 H1 t) N; }5 z2 y4 [" f; D& F' {6 v/ Q
configure: *** xml library not found.
+ [! Y. e2 W. Iconfigure: error: libxml2 is required
$ W7 H M8 \0 [7 F" Q9 b解决方法:
0 N) }5 X0 Y; [. [2 L" k* r
9 Z" R5 R* a/ R, ?$ Cyum install libxml2 libxml2-devel
; f0 I1 k6 k% K8 a, c; u3 V4.执行 /opt/tengine/sbin/nginx -m 时有警告- S$ J1 D& f$ |# S$ d4 h
& M2 P9 n& M; t" V1 z* H- b2 {Tengine version: Tengine/2.1.0 (nginx/1.6.2)& x9 r9 s% z+ k, x! R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, B) R; ]. F: J% X4 L
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 r8 k+ P5 X$ s7 s6 B! x P8 u
$ K& h' E4 t8 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( C0 @, A+ E: A) x6 [ \# Y) T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) F0 R% d( N: G9 o3 m+ I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& V- V6 c& G4 L ]5 T1 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' B. }5 a7 _" O; o. T" B0 e" X8 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ |/ I4 E7 i1 l. M7 Y2 p) n' y% H2 u5 }2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: J& o( B7 G \0 e' b
解决方法,移除低版本的APR (1.3.9)
; E& v/ k2 B$ ~; n' M0 l: ^+ y% H5 q( p) U- z5 R& F9 d) | ?7 N0 f
yum remove apr
1 x/ a) N D* c) P' H5 G0 |. u5.Error.log中有: Audit log: Failed to lock global mutex& Y' G2 e5 g3 T. e' K) S( S" d& r
& u8 o5 B- q. t6 }! j3 H5 j, `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 g8 r: h- y; {: | Q& Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& A9 L+ x$ c7 e解决方法:$ K. p! U: Q# _# w" E& l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& k( R9 W( t4 v8 K1 X
/ d& Y: D. N/ d3 g& r: v
SecAuditLogDirMode 0777
" q3 B2 K5 ^7 ?/ u& r1 l+ x# sSecAuditLogFileMode 0550, J5 V" M/ F; h7 t
SecAuditLogStorageDir /var/log/modsecurity
6 q1 Z* u* a" x8 o! z. xSecAuditLogType Concurrent
6 I( n- ?) V1 j' H/ ~+ E参考文章:
6 W4 ?6 o O6 g( w z. yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 h* Z: o% @ L7 P2 j1 r* _http://drops.wooyun.org/tips/2614 |
|