|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; Z% x! C8 }+ p3 q' ]9 D4 u' u( B9 B7 ?+ p g9 P
一.准备工作- m* h- g5 ?2 `3 [* \& f
) R2 o: w# }' i2 W; z9 ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% Q& w6 t7 k: z
4 T+ |8 ^1 b3 S: R. Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% G0 b9 m% S: L. V! m X* e. {4 p3 {, q' b! T2 J
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* L. d, ?1 I3 D( S7 _0 L0 H) a: X' ], n( n! q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs C3 V2 Z# }* N( A
# o* |. S( ^ j; B% M, A! x
依赖关系:
& o3 e! b& t( `1 @% I, @, l3 \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 p6 A; Z8 r6 n# `* c8 J7 k
) Y6 J# s8 H4 ~" Q- M) Z- tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. k# x. B& O* @3 Y3 Umodsecurty依赖的包:pcre httpd-devel libxml2 apr
& _- |1 U, u3 n6 U
3 n" U! p! l' v6 Y: zyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 P0 Q4 f2 v+ P; b4 L二.启用standalone模块并编译1 P3 N2 S4 h0 d8 m' S
, `7 G* [: w0 g9 b* Q
下载modsecurity for nginx 解压,进入解压后目录执行:( o& n6 A/ t' Z( I9 b( g$ R
1 m' `& O) i+ W- e./autogen.sh
9 c3 b9 \' p, q./configure --enable-standalone-module --disable-mlogc& V' V: r/ }$ T) w
make
! ]" o M8 X% T8 _2 U: J. ^三.nginx添加modsecurity模块
2 U" D$ O. m( b: s. i, D) U
% [4 |2 _+ X- h4 v( t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 E& r% V q% o: R* T
9 x5 t6 y; g. u4 b# _( U8 E3 K# @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine+ w N, R4 A' Y, k8 f
make && make install$ v% Q I t) y+ D
四.添加规则
4 r1 p! N. c5 R9 K6 `+ |' }+ _! G; ~; x* q! `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 u$ _2 d c. F; B5 B+ q, H' w7 e7 v
1.下载OWASP规则:- C' |! V/ ?7 f7 v' ?3 Z# D$ z8 T
* `0 r; H& A- N, D# t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs, `9 j) W2 |) }$ Q2 m- W
4 {* P3 L" V% B( G4 d5 P8 Y9 `
mv owasp-modsecurity-crs /opt/tengine/conf/
0 }1 F3 h2 y3 m9 k0 {7 P" a* O \2 V9 i6 R9 o( t
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 p2 T1 r; t3 ?( n" o/ |! G$ |% o2.启用OWASP规则:
% k, W3 k. R& v
9 j, j# |- z# f# g0 U, v! m( u. [复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# v9 ^, M$ M# j$ y" x6 z) k
2 ^0 p8 h$ T6 O6 e9 b( E" s编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ C2 z* Q" d% K+ P) L% w
9 K5 l: w& O0 E2 J! U
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: V) i. I7 p% ^0 l% ^& i
% n9 }5 \$ V6 V: a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 s1 @0 N0 D$ y1 P; K' `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& q& o7 d4 B) z. H: n" SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 t& M/ i e" n+ u% EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! i; n. R# m- _* T( v# yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 F" a) h, U2 f# X# q- f0 `+ Z6 l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 P( Y# o$ B r$ CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 B0 }( B3 h- O* b2 H五.配置nginx
" p$ @: }, ^+ y# l& y/ I) Q6 r0 [: k+ H4 H' ]
在需要启用modsecurity的主机的location下面加入下面两行即可:
0 j' m. P3 }4 B0 |3 M) ~: x4 z% ^/ G0 |8 Q! q" r
ModSecurityEnabled on; 8 {6 V# C* k4 A8 z1 `' ^
ModSecurityConfig modsecurity.conf;
s, A3 Y3 B% q( H3 t6 ]& {下面是两个示例配置,php虚拟主机:& T/ e6 N0 T. C& q/ e( M
3 E) z7 n, b5 R1 U1 J! hserver {. ` O) s; \) a4 J' ~
listen 80;
+ X9 w4 k& w' o server_name 52os.net www.52os.net;; q4 k: @4 b U
2 G9 X. ?8 t5 {4 }8 P location ~ \.php$ {
8 d+ V6 T9 C% X ModSecurityEnabled on;
7 `7 m9 Z: \+ L1 _' d$ Q4 D% U ModSecurityConfig modsecurity.conf;$ g+ W1 H- ~. y' |
$ G" y+ F4 H$ [1 p% u& G root /web/wordpress;
" a, I* P e5 D+ d0 b' l( ?# {. J index index.php index.html index.htm;7 K% G& @* J5 n
% u a( G2 w* y fastcgi_pass 127.0.0.1:9000;- U1 p5 X. {# q* N# C9 d2 i
fastcgi_index index.php;
6 \# k" @' w0 G& c A fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% a, o" T7 o; d7 b- w( s9 ~" N. B
include fastcgi_params;' M1 }/ I8 r8 V
}' }& |* d+ L: R B( e' S% c
}
+ i' Q/ I! P8 E1 J; P. tupstream负载均衡:
& s% L% ~ y$ c. @" F( o+ X
; A" _7 O. N, J" Mupstream 52os.net {' Y5 q; _4 a9 Q% x. s
server 192.168.1.100:8080;
9 N# @" F% y" Q( U( C; C3 a server 192.168.1.101:8080 backup;
: J7 P4 [4 k. ?% V0 v2 j" M2 H& J}. w0 b7 A) ?8 T, j
, W; p" g' k1 ]$ g% Y" |/ j
server {4 t! y, i7 k" Z- x
listen 80;
1 _$ A6 j I9 V# J% O, Kserver_name 52os.net www.52os.net;
) Z. n8 u3 I1 @6 y8 b r
8 a: f) O* F" glocation / {6 T7 m. n" ]3 t3 N
ModSecurityEnabled on; + C* c" Q" i- l; R& V7 w. O) K
ModSecurityConfig modsecurity.conf;
8 T# P& q l3 ^ l; J' `% Q8 ^) o8 r# k3 m/ _% H1 N
proxy_pass http://online;
8 y$ K* o+ X/ ] @ {0 C proxy_redirect off;0 \( L8 f. m: i, s
proxy_set_header Host $host;. A9 d, v6 e, ` k/ `- ]. C- N
proxy_set_header X-Real-IP $remote_addr;
# ?) ^+ z. p# Z' o! A' y2 Q0 f proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 ~0 U/ X9 r( j }7 Z, r6 T6 {/ G4 e U
}
9 v8 f& |, X6 }% ^# Z六.测试
: q+ G: Y0 v; w e( ^: d- D; c, i9 u0 H8 } u6 ?+ [/ n" T8 c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 ]" f, u0 _- [ r' b+ G
4 Q6 p" ~. g c
<?php# i3 G1 d" \1 ]) b1 n
phpinfo(); - ~9 |$ l5 M' u6 T
?>9 C' Z2 R( G8 S4 I# n
在浏览器中访问:, B4 K! |6 v) g+ L0 E, E6 S
: ~0 y% }: N" ^* I3 Z! _
http://www.52os.net/phpinfo.php?id=1 正常显示。
9 E, y2 ~' G* U8 I f4 ^http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。# U0 m& n: c" f) L7 \
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- {9 K4 t# e; V9 H& W$ ?8 y6 T
说明sql注入和xss已经被过滤了+ a- {7 t$ ]( U! i% p' z6 t
0 c4 B0 J' Q4 j: ]/ H
七、安装过程中排错
: T% E: L2 X' U/ ]; Z! S2 G8 @: f& Z4 C3 {; O2 ~) J( U
1.缺少APXS会报错- T$ t5 F. U, i
6 ?8 c1 K/ b9 j5 Econfigure: looking for Apache module support via DSO through APXS
b- d# [' i+ _; x+ P. @configure: error: couldn't find APXS, c3 R& m9 [0 t1 F2 t1 t+ c" {2 |/ G& o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
- M2 |3 j+ U9 o. V解决方法:
. {7 F( x" \! V
) |) ^- r7 U0 C5 F" w6 z$ Ayum install httpd-devel
. t* m/ s* g* n" _$ f' Z2.没有pcre4 E* P* s. ]9 U
+ r+ q! e7 f7 U) nconfigure: *** pcre library not found.
. W$ {0 ^ u7 D: _7 V' S* [) {! Jconfigure: error: pcre library is required
7 _/ }1 |2 N7 s8 z# y b1 p解决方法:
0 V* U6 h: P- a' Y- C7 ~
$ d. t7 A0 u6 u) Xyum install pcre pcre-devel
) X$ H) v3 p+ l: J+ F7 m; j3.没有libxml2
) n: }. F- f: a) q
! C" S5 A# Z, X
! w( j" l" ? f) H3 Q: L0 Y* mconfigure: *** xml library not found.
' w# O* f+ \ g6 \" \( u' J% oconfigure: error: libxml2 is required5 i8 y5 Z0 m) y! B4 y( T* O
解决方法:0 p# c& z) f, h- S: ~
, V: n1 S! ^$ @) Z6 kyum install libxml2 libxml2-devel
8 h2 n5 `* P5 V9 C0 s9 F4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 l& L7 _( r( |, B
6 b: d. y% z" | I' K' JTengine version: Tengine/2.1.0 (nginx/1.6.2)* _0 e# W. Y" z* l* A! Z7 Z5 ]
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ a2 v4 j. v* G+ f
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- ]) V# O* T+ Z2 h' Y* i- L, k3 v0 o9 h1 r8 H0 g6 |2 k; r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- ]; v$ w1 T$ p- b2 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 R: S; c2 P6 y4 k2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! {: ~' J" }4 k7 F$ \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* ~2 c3 `) o7 t, V4 W7 }) E3 ]- H$ l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! t* U* e' y( D2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& e0 o. Q4 n+ j: q1 M
解决方法,移除低版本的APR (1.3.9)& f. J: A, s |1 b2 v8 ^
# I) m% [ Y+ ayum remove apr
) q$ A5 |6 o8 Y c* C" t3 r5.Error.log中有: Audit log: Failed to lock global mutex! ?6 u/ L2 Y- N( d. L: R8 y' J/ o
* }* v% D+ D. k
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 N" C/ l* w9 c8 s9 ]
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ B4 P, C0 ^5 z0 U解决方法:) y+ @; F. w0 q8 j% W
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# e& C B& x: {7 c" P( w
8 ~; u- u0 f4 F! W- E" q
SecAuditLogDirMode 0777. {4 B$ D1 Q1 i' i8 H" z7 z& ]
SecAuditLogFileMode 0550" } \) ]# ?) {% _5 P3 ?' ?% u" Y
SecAuditLogStorageDir /var/log/modsecurity4 i+ E; m! C* p1 c b4 l) X! |5 p# T
SecAuditLogType Concurrent- G, [! f9 Y- O o1 \
参考文章:
0 |( X7 J0 s/ ?+ C0 Q6 Y. @% R, Whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& q# \# e: f) N; h; Z4 }
http://drops.wooyun.org/tips/2614 |
|