|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 d/ M* W/ j- |* I' h9 d$ ^! ^5 X$ Y1 A& I
一.准备工作
2 Z% N" k+ ?; w3 D0 x/ M# }7 O
- E% F3 B$ h& c1 Z7 F g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: g+ l/ j: r0 u! f- O& y8 y+ Q, n5 z& Q5 N! r5 o9 y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% ^, T2 A( I! r, T% E8 Q7 e" Z5 i6 V1 G9 D7 l2 _
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 p2 N9 O4 K& R" X& P5 C
; }: W8 o8 X; s' N3 i
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; O" y6 }4 \7 c; l) M! U* {
; c% T% K* B& r' t' w: p3 i& r
依赖关系:
3 N3 W% |) S3 s5 l0 ~$ d# Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. S2 h0 [! \0 c7 a' o: k
' F) [! B; ^. I/ D, eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 C+ T6 E& }- o) w
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! i& T# n0 Y1 O1 |7 ?8 a% _) h: C( Z" X5 j
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! g' z7 Y4 `9 _* b& c二.启用standalone模块并编译 R5 }" {, Z+ S4 X* _, G
& r; k4 w+ \6 H! p# H下载modsecurity for nginx 解压,进入解压后目录执行:' {" [; D" p9 H
) U* m4 d* z1 o5 T A
./autogen.sh
5 n! R: y: Q6 y6 r./configure --enable-standalone-module --disable-mlogc7 [! g" v) n1 r* Z
make & D- H/ {* L4 A3 y) ]# J
三.nginx添加modsecurity模块
$ k" X |2 A$ C; b9 s
8 Y6 M6 N$ L' U' b0 A/ s( q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' e4 D5 X* K/ Y4 n" N! `4 P
& J8 e) S* e2 v7 C
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
! ?- X& H2 r) z9 ]( Imake && make install
0 t2 h7 I1 h$ x9 }* [2 U1 L四.添加规则
z2 B1 k* M. b5 i- M6 }
1 \4 E8 H9 u5 s9 C0 t' X& Nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& n+ y: `/ N/ M# w
3 o& S- m' b2 \0 K+ u& V' f1.下载OWASP规则:
* i" O0 D$ x" B' o/ i+ e
( W; x: h4 k" Q/ xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 Q0 c$ R7 g o1 }% u( A7 X4 O( [2 v, u$ i! e+ S, }, U, G
mv owasp-modsecurity-crs /opt/tengine/conf/7 C. X9 R$ V* l
0 c& S" X: n) [/ n8 s/ M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; `2 y! ^/ v, _! Q, A
2.启用OWASP规则:
9 V) b4 L; N) q
3 `; a- [' T P$ h5 {复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ _2 r1 @+ I& V' D2 X
. W6 H; l$ j% v& Z8 @% d3 F
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ e) r9 J0 _6 x4 ?0 ]/ D% l3 V# C' ^, e- D( C/ I9 \0 y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 T9 X' ~2 _9 W. D" L
5 K: n; b+ _5 U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 q/ l% [% D: EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( ~$ E( r3 s8 @& R0 {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, k) b; {; ~- T! O' n( tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* r: A- [0 `' dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; i5 T( n# D& U% W5 T7 o. f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 {* H) g: ?6 E% [) a3 Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# X2 p r" M: N/ ]3 T五.配置nginx: p) s3 j9 R3 T0 X- ^1 M
1 X7 |( u' g* F$ {
在需要启用modsecurity的主机的location下面加入下面两行即可:
0 q4 I4 |# M/ A) X! @7 y, R4 _2 ~: W2 B1 w9 s
ModSecurityEnabled on; 8 x" d- W* v3 _* o
ModSecurityConfig modsecurity.conf;0 p+ ]9 F* w# U+ w! O# h
下面是两个示例配置,php虚拟主机:
+ i; K0 l Z1 U* F
- j6 D7 y4 f* | F$ Userver {: k1 L6 f$ b) m$ ~1 C3 b7 W. i
listen 80;: Q7 y7 `' {; ~1 m1 g4 C
server_name 52os.net www.52os.net;
% E' k6 M7 H1 W7 |5 Y! A " a9 I% k3 N" @0 p& s8 i% {& R, Q
location ~ \.php$ {5 L: U4 N; i4 j e
ModSecurityEnabled on;
) J# S. W4 f3 F! Z$ F* E ModSecurityConfig modsecurity.conf;# d l" `5 k3 {+ s3 [( W0 s
9 k' E$ l: n- G7 |( R root /web/wordpress;
( p. N' T: X# x1 d! d1 n) h index index.php index.html index.htm;* T: @ e1 P% R7 M) H
% \' F2 T' ?% ~- n& ]( L" C# j" Y* I
fastcgi_pass 127.0.0.1:9000;2 c3 Y2 Q* h' H" d: E
fastcgi_index index.php; B# _: _% z5 h' |3 p
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
! h; e) P* B; d* u p include fastcgi_params;
3 O* }2 R" [( B# ` }% S4 q& P* k" ]
}
' |- J' s+ G3 h/ B o2 W- _% [7 Gupstream负载均衡:+ @& q) j: q# x, ^5 U
4 z* _$ n# e0 Y% R* Tupstream 52os.net { t! i" Q- K+ S" a; Q
server 192.168.1.100:8080;
) h0 u: i4 p4 M5 s server 192.168.1.101:8080 backup;$ u. w3 H' c! P, i
}
/ z- Y1 Q; R1 [/ m4 P1 c5 G3 e5 K
' U0 e) h) ?: m: Y. U' Pserver {
* u4 L3 m+ P. {3 r/ i$ dlisten 80;- e' U3 o1 ^# y" G Z( S7 G( u# n
server_name 52os.net www.52os.net;* Z8 M, C$ N/ |& ]5 i6 q
g' L. z2 y7 |9 L
location / {
" H0 _$ l4 h) ?# k# M& n2 U ModSecurityEnabled on; , ` J0 y* a% @, r" Z( v& l
ModSecurityConfig modsecurity.conf; " [3 f% @" M1 W
+ B/ a; {/ w& a* U/ l( y5 G proxy_pass http://online;& ?/ L' T- R. U
proxy_redirect off;
]( C0 m& d, E' D( s$ s) z: l proxy_set_header Host $host;
. Z( O6 x6 O3 x* ]( q( M/ E proxy_set_header X-Real-IP $remote_addr;
* {+ D" E& T! m proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ h4 j; X3 I) t$ }
}
& ~& c3 x4 ~# Q+ z* O0 |1 |" ]# b}
9 M1 m4 y+ j+ l& {* j0 h六.测试
- N2 P& j; v( V& j" H) p, x; x
]: ?$ }4 z* I" N: w我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 d+ {4 H4 \8 Z* h. z% w3 _5 K
- G- P! W$ e8 Z3 q0 R; ?<?php; X! G# J$ Y+ x) t) r
phpinfo(); 8 A4 s5 Z5 I. U8 | O
?>/ e- j/ m; f5 p0 y7 U
在浏览器中访问:
% p( g& h4 E* I3 L+ f
% ]( ^3 u+ U( b1 v$ v8 k, f& Chttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ a* e( S3 W# d! e0 _http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- D& I ^) [8 khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& }$ V& P& \: V- w说明sql注入和xss已经被过滤了5 s! q5 t- B' _( r
$ R# O3 ]1 ^; G) |8 U$ Z" `9 g
七、安装过程中排错
$ j5 T0 R k+ h/ U# U* Q5 X' ?/ ]' n. r k9 Q$ k* W
1.缺少APXS会报错
. Z L6 K! t7 H
, w6 y) d9 p" m. F3 }+ [ Wconfigure: looking for Apache module support via DSO through APXS2 D# S v% S' @% U
configure: error: couldn't find APXS
k4 d4 h) X$ ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- w' ~* ~; Y/ x7 Z解决方法:/ x7 {3 P+ d$ J/ S0 K% v J, O
: h% _0 P9 [! a1 n/ a" [yum install httpd-devel8 c9 c% i: `) E, L- t$ @2 j
2.没有pcre
2 D0 Q9 P9 C4 j4 K& w4 I
- x; |% b" M4 ~( t& |: }configure: *** pcre library not found.- @, l/ Z+ q+ [$ j
configure: error: pcre library is required
4 \8 {; V' Y, F' p* R! E: o3 o' P解决方法:
* _3 G" m S; s% U; n, J& q; \
yum install pcre pcre-devel1 S3 ~5 @; m) O4 [, z: B- h3 U
3.没有libxml2. f$ y; _2 g# C1 O' d* \( c
( w, _+ ~* ^' o, Z' k
1 G8 S1 N. s% f1 x `7 k! xconfigure: *** xml library not found.& m( Y7 h' s2 j& U2 Q* d: @
configure: error: libxml2 is required
6 u, e5 C7 w% { n O1 r9 f$ A解决方法:# \, n( l2 S: v' R- n
, K' f6 Q8 g1 B$ w: [! s8 ~
yum install libxml2 libxml2-devel
+ j% G0 c4 Z: Y) b! u" J8 e4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ c( Z0 B8 `4 c3 P; v5 B% D% F6 R* ^9 U6 N3 Q" \0 \+ F! m0 r
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, S2 m* M6 e% L! snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 W& b( b$ q# D9 w7 i Z: r; b2 B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. I& B: G! o! H( J. ]: f; f6 t. X3 B
7 K# P5 ?2 {/ R4 O) r$ n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 `) [ t' B1 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 s1 m* t2 r# l+ G! ]2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& \7 z$ [! f3 S; x0 w) Y! s s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( \9 n: E2 j8 C7 \1 O8 A$ Q9 k) q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- |6 a' l0 G+ J* I6 W* i
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." V+ K& I1 b/ g8 M7 W) z+ c
解决方法,移除低版本的APR (1.3.9)
( m) }) z& p3 K& t. W
/ e! N3 a- f2 x+ Z/ b0 H6 Q0 qyum remove apr& [' K5 D/ H, u J7 d3 L% I: d' r
5.Error.log中有: Audit log: Failed to lock global mutex( L4 ]9 k5 C+ t2 Y4 ^. C
- M( g3 L+ H% O3 s* r2 o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 1 `) o6 y; j: y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ n1 {0 X* o# W. G2 c; V解决方法:* z5 @5 m) j. A( K9 d E! s( O- Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ W% S* Q: N+ t+ }
3 U d* ]' v; N8 C' HSecAuditLogDirMode 07774 C" N. H- K \. _4 n7 A5 Q
SecAuditLogFileMode 0550
8 H$ ]( t+ y$ Z' n+ ESecAuditLogStorageDir /var/log/modsecurity
& Z3 r$ {7 R+ dSecAuditLogType Concurrent6 @. [- w- Y( c
参考文章:
) ]; v+ w7 @7 c5 T1 R/ v$ }2 J7 {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, s2 l) q2 T T* H# A( ~4 Z% q. \
http://drops.wooyun.org/tips/2614 |
|