|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# L3 G" m% }0 \
7 C6 A% S, J; H3 t
一.准备工作7 O" I4 Q5 D5 E7 d- `: m8 O
4 M8 W) m+ u4 u6 R2 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- k1 p! W# o) z( s% C6 S6 U2 H$ O
% k: _) ^% c7 J0 Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 o7 U( l& h, N+ c4 z
# z0 f1 o7 x) y" mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 O8 d( G+ R. w& h2 G$ O$ Y
5 Q; {5 B3 {$ R6 ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ o$ d. C, ?" ?
6 }& C" `7 e# Z! F4 j
依赖关系:
8 W* z. {) \4 jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 t4 o% v0 Q1 w' X7 P
& I! a* K6 j6 \/ N1 Z
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
9 G4 w- i: j5 T9 k* `# `modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 H: Z3 b' [0 H6 n% w% {* f5 ~1 V- v ] @
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) M9 `/ i/ `# T
二.启用standalone模块并编译
" z# ?. d( _2 Q$ z# M) s
@ i9 Q; P/ M" J) h下载modsecurity for nginx 解压,进入解压后目录执行:
: e* |( q( w, S" ?/ A4 |; `$ N
! {7 M8 h8 y- O0 O( z& D8 [./autogen.sh
% q, @( V6 z u! M./configure --enable-standalone-module --disable-mlogc) ~. k/ B0 F4 B$ E/ W
make
3 ~( @) R2 F; M+ t/ f* r7 z- G- j7 [三.nginx添加modsecurity模块
* j" y) G+ _/ S5 D! W |5 O0 f, b/ n5 N9 [6 N: u
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) V6 Q, g% d3 D2 M* n+ m
# {, T* G: H# G7 E! a3 `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% r& y( F* J7 |: r' t2 B' @make && make install' U- L" g: A' \3 e/ o) Y" y$ d- M& O
四.添加规则 j3 y8 i" |! K: x7 |6 r/ r
+ o/ x. O5 g2 r9 Z; w0 N+ z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! n. x# u8 _5 A, y- V5 y) d
9 c1 V- G3 M0 i1 e
1.下载OWASP规则:: Q9 }, l: z# ^3 a$ ^
2 R w" o# B- t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs- Z: E! H4 f4 x* N9 D
$ L8 }. g" H- U' }
mv owasp-modsecurity-crs /opt/tengine/conf/( p* u7 v1 `- t/ e" |
$ z& V5 R! ]- fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 h* d0 {1 m5 a( B" S8 I7 |
2.启用OWASP规则:3 ]% k/ W; z* U1 a# k
; c: W4 K: L9 u复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ X' z" P' O% E0 E
+ F# Y* v) o6 q+ ^
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! Y6 K' T; i/ X0 i* g1 N% U
' a. N( |; U$ Q4 _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. C* q# {" G; `7 Q, u
T4 x$ m) A# o3 Q6 ~" @Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: g8 C5 D8 s( h; c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: B& |- s/ \$ }3 ^+ b8 `4 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ v( D+ r) W+ T; P& F0 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, ~- v- N* Y- H. WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 Z5 z! o$ T, {2 U- _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 r9 s6 D u, N$ T% W" ~6 rInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 q& m4 d- S7 I% _
五.配置nginx3 F& ]* ^6 i7 I! H
9 J* S% Y. w6 \- N0 g: v, b, R7 I' n在需要启用modsecurity的主机的location下面加入下面两行即可:
+ y0 b6 t- o! [7 c. F& s" G- p
7 q5 P$ w" O) m% P, \2 ]ModSecurityEnabled on;
" n; |( ]7 Y' i RModSecurityConfig modsecurity.conf;8 U- W& r' }+ w5 |
下面是两个示例配置,php虚拟主机:
( \" X/ @) R% [2 I; I' T: n
) Z: m0 k3 ^) m; ~- F4 qserver {
! B. O& x( Y3 s listen 80;
: E2 F7 L1 s! Q) T0 ?0 T# x server_name 52os.net www.52os.net;
7 Z7 x0 h2 g% |1 l+ G1 _
& K! G: X) l" f2 g location ~ \.php$ {
& [; S9 y7 U+ ^6 X ModSecurityEnabled on; * p# R9 b G9 O0 M( x4 A
ModSecurityConfig modsecurity.conf;
$ f0 q7 V6 @/ P. {/ |3 ]1 ^& D4 V9 i Z; J
4 z' z3 @ o' n5 I' ~ root /web/wordpress;
) _% i9 Z! \& i' [- v index index.php index.html index.htm;
8 C6 L! G6 k' o/ |7 ? & G1 ~; D; Y+ |6 M, V$ R; I
fastcgi_pass 127.0.0.1:9000;) Y/ y$ e' h$ }: }* E# p; j
fastcgi_index index.php;
; f) f H; ]6 _4 e6 T- z: z. }: S, x fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 q2 E' D( ]0 v* J2 L1 |) ~ include fastcgi_params;) S) m7 I5 w0 A* G; g$ D
}7 W6 U' ~5 k& y# U. _" I: x4 h
}& Y, I/ `! ?0 ]5 u
upstream负载均衡:, `7 @) }9 M a, `
/ K n6 i: ~3 g/ C' o( D3 q- H/ X
upstream 52os.net {$ Z0 l) i7 Q( h, A7 c X3 U
server 192.168.1.100:8080;4 Z3 o- t8 s/ ?' w% [ I+ Q
server 192.168.1.101:8080 backup;
( M' j5 ~ s& ~, a( S, h: H- n}
$ N4 P8 y- x$ S1 W( x4 m0 W0 Y3 q7 R0 v; C, D* h! Y
server {
7 X) B4 D+ n! S7 Olisten 80;6 s. n0 |8 K( H( X% w2 Q
server_name 52os.net www.52os.net;
. p/ Z+ W: j6 p* {. b6 f4 B7 H. U" q, C1 f/ {4 u7 B* X
location / {
4 L1 z" p7 T, g* L. k1 x4 g6 N9 m; { ModSecurityEnabled on; + x- b) B5 }: U& y4 Q' }
ModSecurityConfig modsecurity.conf;
; |; G- g- i7 M. J/ Q0 c" V' z& x5 f
6 E0 O; B! X* R: L" O: J proxy_pass http://online;% H6 m, a* a/ z/ ^' k* D
proxy_redirect off;6 G+ i9 a% R! k2 H, G
proxy_set_header Host $host;
0 @4 f* X$ B/ ~5 [" l# F proxy_set_header X-Real-IP $remote_addr;1 X& {/ {" w8 x, w7 P Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 ^1 {0 P0 P/ Y( ^/ x
}: p' c8 ^ X: F5 k4 C5 S/ d
}
( d( n8 X1 T, U% N* H- @0 Q% D六.测试
5 w6 z; P4 w3 N4 V! H
$ j2 q l$ _* I( J5 G* O9 ?) Y% T我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ o. J& Q% l% m# c; ]; e' |/ F: k5 }6 C6 \3 T3 {
<?php1 \' |7 `& n8 g R2 E% Y |
phpinfo(); 0 m% h& ~6 S& O, U8 I) B4 [. y
?>
3 Y; Q7 F. R3 w( O# m0 B在浏览器中访问:, _# W$ Y! j0 F# a+ s6 G2 ?6 \& T
8 l, Q! d6 D3 m! n! n( ?$ j0 K
http://www.52os.net/phpinfo.php?id=1 正常显示。% }) V0 h1 o5 v$ G6 O1 h
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* k* A7 O; a& r" U; W& O: ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
* M3 f1 { ~" z1 W3 E) q6 S说明sql注入和xss已经被过滤了& L( N$ P& w" L6 k8 h
0 | b7 q/ `1 W, @
七、安装过程中排错9 b. S+ O* i7 B+ C1 ]- ?
: G% i; n6 D3 I3 H& @, B2 `
1.缺少APXS会报错! q. `; `/ L. R+ Y. ?$ ]+ e
. w" G9 ^! a- _- q* V- ? V; r \configure: looking for Apache module support via DSO through APXS4 z$ @) E v% o/ j' I5 F$ @
configure: error: couldn't find APXS) }1 T8 H+ k6 u6 c2 o8 n, X# d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 I1 b, _$ w w- e- o9 g1 p9 l解决方法:- q! ]( F" |8 Y' g/ y
4 n! \8 C8 n: W, k* [1 V; r3 y% }% i
yum install httpd-devel6 E* t" i5 Y4 p# {: [1 B
2.没有pcre7 b2 ^& H+ E$ c
) w! P9 t, T! p9 J% @% F, M$ l! ?0 {
configure: *** pcre library not found.
! r# e$ W T- w( T: hconfigure: error: pcre library is required
: m/ F0 u* B! y0 m解决方法:
$ W8 C* b9 C$ ]! N9 b& c
7 J# s9 v& _8 Y3 J7 t6 _6 Lyum install pcre pcre-devel
3 F5 @% R }3 y. \6 d* E) X3.没有libxml25 d( [$ I% w0 r9 U
! l9 d* W+ D: K0 }4 e3 D. N
9 e2 y6 c' Z3 ?, Y f# nconfigure: *** xml library not found., t" _* R2 v% t+ g( ~0 A$ C9 K
configure: error: libxml2 is required
: c/ M, o p9 i解决方法:: o5 ] H( s9 p! q
+ w! U: g$ O9 O/ A/ z
yum install libxml2 libxml2-devel7 J6 V( o7 H, Z- R( }( R8 C3 T
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) ]. A' Y" } e r* u
7 G* F4 d. l! n2 g' C) iTengine version: Tengine/2.1.0 (nginx/1.6.2)
- i7 n; }7 U9 Y2 e& [* ~" [nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 M) \, T7 s3 N5 K ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 C, m5 A2 H- X. ~2 m
1 v8 d0 b, \3 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 L% I3 N( h% C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 m q, j7 ~9 y3 N
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: J4 C. ]: q' Z; X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- ^* g' B4 g+ Z5 H: W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' Z5 |5 w+ g; t( u5 I# N
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
o( P3 B' [1 `- L! h2 g! Z2 q+ p解决方法,移除低版本的APR (1.3.9)$ _: x: l/ R4 ~5 C# ~' e# B
; ?$ m2 [$ X( F* E) d2 E: t8 xyum remove apr% h! E$ `2 N* |: x
5.Error.log中有: Audit log: Failed to lock global mutex
3 r' Q1 u6 b e8 |1 B) _- |% R( |: W6 h: b
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) \/ k' L/ H) K( C& f, V& r0 V% m& _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 }; C5 S) d x$ f解决方法:
' x. e! Z+ a$ u编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- Z1 X' t& e9 c# Y: T3 F
7 m* v. }- g# Q2 e( X. oSecAuditLogDirMode 0777' u' k6 m8 i J* S8 M
SecAuditLogFileMode 0550
0 c# [5 g' H7 d) b$ k- X) `SecAuditLogStorageDir /var/log/modsecurity
1 n+ F% r: U E a$ fSecAuditLogType Concurrent6 A( ~$ I1 x! H5 T9 |0 A
参考文章:* H4 X& A8 M9 v2 v8 {$ a; k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 s$ [0 u' K# l5 G# U t+ P8 qhttp://drops.wooyun.org/tips/2614 |
|