|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 L: v: ^' K' T1 N [; g4 N& Q, D \* A) E5 {" A, ~8 F
一.准备工作$ L# Z' K0 j+ I4 k* D1 N
3 [7 l8 q: n5 z3 A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; z! y# U c5 I
6 P( j# G4 ^: {. z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 y. F8 y' N; r. q
! R \5 K/ O4 n8 omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
3 H% P# y# I* g" \
: u0 P8 H& R% }- _' F f) c4 l, FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, B# Q( K& \3 A# a
+ J* p6 P8 B8 T, s依赖关系:
/ O+ l/ `, }; y& mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 C5 r) s$ J; b4 u/ l0 {. e
8 K3 {* d" M" K0 K6 cyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
; `7 |* ?4 O9 L8 E: [modsecurty依赖的包:pcre httpd-devel libxml2 apr
: l% e' D( _, Y$ G9 g0 V% l$ v2 K/ v, r
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ e/ f- J: B3 @% {& u4 b二.启用standalone模块并编译; z# `" l! V8 |1 `) G3 c
# s1 e+ X" \5 @! A/ J/ k- L
下载modsecurity for nginx 解压,进入解压后目录执行:! L/ j$ }: L/ \# _- u
6 P; B: |" r3 l. J- j# Y) Z1 O F) [( ~
./autogen.sh
) _3 }% Q. I- U* j8 @./configure --enable-standalone-module --disable-mlogc
8 d. W% S! d3 y, h* F. u$ ?: bmake 1 T& V& V3 q5 u* ~& W! L" q
三.nginx添加modsecurity模块% ^" ^( r9 o" @! m6 S
/ ^& x) q" f, n8 m7 h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) j# h+ @# p, w) }! U, h+ F. R
7 C* O$ W( a4 J( u% O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ t# T% ]0 B' g$ I- g4 ~3 E. Wmake && make install2 H0 m" i, Y7 c
四.添加规则
, l, A5 Z/ R- o) V
' B4 z3 G8 D. N5 @modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# N M, l: P. C( ]0 p
* _" N+ _# ^) ?/ {
1.下载OWASP规则:( H* P* G s D; b$ o& \- S1 d# B
5 f8 m0 {6 F: w" j/ U% n
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% ?; F3 t! k$ i
! |, E B \& k: o( H9 emv owasp-modsecurity-crs /opt/tengine/conf/4 R4 U3 c. B( o: N
5 z2 j$ Q* _. o2 K2 Z/ P' Z6 K7 F' G
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% T7 h$ h4 Q( {, S# ]
2.启用OWASP规则:, v- c, _ ~7 [% k: {* F
* T7 z5 F- {& ~复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; B( A5 C4 U I
- V# {5 J1 Q8 A2 V) d* u3 B$ e编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 L) h$ j5 J5 {& k
/ O2 R3 s+ {$ J
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. u h) I( w3 U- F6 z) P2 E
0 R; `: M2 `3 q3 s: R5 Z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* M+ b8 s) e+ d e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" G/ ~, Y& B. W9 P3 M0 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 I( R8 f8 I5 H+ G' R. S1 J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: Q- i9 n# o% y7 |: g1 u( U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! n) q& K7 }, `/ xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf Y7 h: Y4 N- p9 P8 W4 ?6 E# t6 N
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 o4 p$ S3 c7 c& G# I1 _
五.配置nginx
4 n- u R" d4 U8 U' Y8 L' |! }' m: M+ v6 ]
在需要启用modsecurity的主机的location下面加入下面两行即可:
l" e0 u2 C6 \9 d# ]
' X* X, ~# y- {* jModSecurityEnabled on;
1 b" N( i2 n8 ^; J/ L3 E2 AModSecurityConfig modsecurity.conf;7 H4 y/ L( j. B' S# H' g
下面是两个示例配置,php虚拟主机:! q- S& [& K2 n8 D( J3 C, i
* K J$ N/ [9 A
server {
( M/ E+ u/ p, [2 C) L5 H listen 80;
' U0 z" q M# e7 g server_name 52os.net www.52os.net;
: v/ J; {+ H: a) l
+ \+ v, j0 S8 D/ C8 W6 k3 @* ` location ~ \.php$ {7 k0 g+ [* J" l6 }5 a
ModSecurityEnabled on; ' _/ R. Q: Z5 `' X+ G
ModSecurityConfig modsecurity.conf;
! v2 I7 m' q3 U
* h2 ^ G1 E( L% E( w! ` root /web/wordpress;
' f" l7 @( T) u4 i index index.php index.html index.htm;
9 C) a( D, R6 M3 S) D/ L5 b6 w
6 o `8 a# v/ g3 |. ^+ X fastcgi_pass 127.0.0.1:9000;
' C3 h1 O( d9 u& p! j2 L) s fastcgi_index index.php;' ?+ ]$ x1 U- g* @# z# R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;) J! u7 ~ e3 E' ~9 i
include fastcgi_params;
3 }( w. H' q8 J0 n- w7 Q }
: ]8 M; g8 G f0 m1 l) X0 e }
) R2 _5 _0 l3 R# } |upstream负载均衡:
( G1 G: c' I) @8 @- p8 `. k
$ \9 A5 l# h s* oupstream 52os.net {" f1 M' y" c b- F2 c5 v8 M7 b1 O
server 192.168.1.100:8080;6 Y8 N9 b5 J2 T( {7 Z5 f! y* H0 s
server 192.168.1.101:8080 backup;
$ W. W2 y0 y, ?- p0 R}9 K# [1 t1 y7 S% T3 {) a
/ \+ }6 a# S8 L7 aserver {
; l# z( F2 {3 ~: q6 H# h7 alisten 80;; Z+ b9 A; J8 D2 g! A
server_name 52os.net www.52os.net;* Z5 z# k* c% T& p8 j. c- j1 E
( M+ S6 u# n+ a+ p) Q7 \location / {5 y9 S2 t8 F- U2 f$ q
ModSecurityEnabled on; ! b! C A* K V- K7 Y3 ~
ModSecurityConfig modsecurity.conf; - e& y0 w9 o6 k$ Y0 v/ x
6 [2 I+ W% X2 }9 `* l proxy_pass http://online;
) i. P$ ]% p2 K; b proxy_redirect off;
6 A" r* {0 ^, r) T7 M" T3 \0 F proxy_set_header Host $host;
" \2 P0 e. u" I% _) W proxy_set_header X-Real-IP $remote_addr;0 q# T# @' |+ `! W
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 C: D2 @1 |6 g5 ^4 |% s- J" _
}' |- ~) c/ L% N _- r% s2 D" H! z2 H- t
}
1 \* Y# t+ x8 t4 p3 U8 @; n六.测试2 F5 [8 F) E5 a9 \
7 O! `9 G# X% N @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# k8 s, k0 s" g% q! V
7 S+ C% }, j" ~- ^
<?php1 x* d" J1 o. i2 n+ I5 j
phpinfo(); ) f% g- E2 A8 W4 q
?>( t1 Z, s e9 w5 K" ^
在浏览器中访问:
6 h6 F' N2 a. k% X- a: a: Y( w, t! K: p2 Z7 S B5 o
http://www.52os.net/phpinfo.php?id=1 正常显示。
; \& g; J& J$ `4 j" `http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。+ k& ]! j% V3 L& ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ r& o/ H7 F: |0 x+ J: R
说明sql注入和xss已经被过滤了% a5 f |+ q. X8 Z( m
" f( P4 e0 x2 C, p七、安装过程中排错; e; u) W. J; Q+ z* S0 S5 T
* C, w6 H$ k6 L" ?8 d8 y3 X6 h; D
1.缺少APXS会报错+ j2 A Q8 H) K
, d5 x0 P* ?5 \5 u9 O' j. Q
configure: looking for Apache module support via DSO through APXS- t6 d' Y7 t& }! }( c
configure: error: couldn't find APXS8 f; v8 y4 N- V6 V z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 j4 o" c8 s& l4 F" c- w: D2 ^解决方法:9 |: {0 i$ o& C/ X7 J6 x) q
8 k- S& r5 \1 f# a6 V3 Uyum install httpd-devel
6 P$ R0 J4 l) q* q2.没有pcre% r1 ?. a" u) p* D9 ~
: m# T. c" S# m+ k
configure: *** pcre library not found.. V7 S i; Z8 \! v' g
configure: error: pcre library is required
- Z4 I4 y( _. U3 O0 {" z/ t解决方法:
$ Z$ o0 B9 w' y$ K3 `1 F) M" r t6 V8 P" Q7 I' h- h
yum install pcre pcre-devel" _% p" O C$ N2 y
3.没有libxml2: \/ q ~" e% e# i$ }* E
7 C, Y; t. m% z x; S. }9 ^% o5 { o$ |9 b; i
configure: *** xml library not found.
: L9 b% Z7 i' Q: v2 p i: kconfigure: error: libxml2 is required$ C3 E. Y. L) g; [! ^8 u7 _4 y
解决方法:; g* H+ @$ o$ J% O: F+ a; ^7 I
- Y& z' D& K' n8 lyum install libxml2 libxml2-devel7 M# p( e" _+ Q0 @, o. C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ R. Q: V2 I! O6 K- U; u. E" |: s& u. Q. f. p! D% y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 B( Q+ S! T% S# ]
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. F8 z' w! s$ W' r3 m2 {, O
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# S8 L) [# L5 X: n, a* v4 l" h
h( k4 n j: I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 J% W- Q5 }+ B7 Z V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 h% I- [* q3 q' c" e( `2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 g4 p% I. c z, l4 i! U3 g1 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ e# e2 ]! D. o6 E' K3 s% C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 G- W- {" k$ G: d$ E7 F
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 P% T3 W# I: D: A: \# g
解决方法,移除低版本的APR (1.3.9)
* P5 _; O( C$ ]' u6 T: ` S0 A$ Y$ \) g6 s& D! J
yum remove apr
0 z5 t7 b3 Q! W5 [0 h" w: g5.Error.log中有: Audit log: Failed to lock global mutex# _" k& V0 S* F$ N! h" a% Z6 n3 y
6 F6 B. N( K9 S! U$ e( R8 @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 x. G- q5 k" a5 X' }5 z* sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 ^4 }$ }# W7 K# v/ @
解决方法:% k) ?$ _. k* V+ E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- h4 m+ b7 _' ]) h2 H5 F5 e4 s. A; H& o: v1 v! v
SecAuditLogDirMode 0777# b. F; b; K7 Y& w& a& |
SecAuditLogFileMode 0550) B' ^0 B' J0 z0 `
SecAuditLogStorageDir /var/log/modsecurity
$ O* w+ p6 F9 g" N5 d( HSecAuditLogType Concurrent
. J" ^: a0 v( W4 K( G N4 w参考文章:
% r8 Z6 A8 t9 x# `- j: P0 thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 S! W: ~; p3 R, c% Q
http://drops.wooyun.org/tips/2614 |
|