|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 j: G! C" _$ U: G( Z8 I; ~2 V
& J; a) D% `% w一.准备工作" x) H- S5 |1 q5 F( f
/ B0 c( S2 \: T) u2 q2 ^: j) _
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, Y; A! k* E x; X1 S' e
4 j) Y# S2 F' e Jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 t+ y9 Z" m! N v* w
9 h) E% h+ D% Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# P: U) F0 h E/ q
/ P8 T$ E- T+ N( M1 f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) w2 @! _- {& } x$ e0 M. K+ D) J$ h. D" ~3 h
依赖关系:0 Z4 o% v5 v: |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 k( q3 k4 h r( o, e$ H' M
. A' T! C( T f- b1 C( M; {yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& [/ h% s" j; W# Y" b
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: }8 s0 t5 R8 f, r4 ]% [' f1 [! k [0 S5 y D; u. I
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 z! L8 n! f: }7 n0 [0 W0 U# K+ t二.启用standalone模块并编译' E$ x* S" h: T# t7 R7 Z1 Z5 p, g
- j. H1 D, r: l7 f
下载modsecurity for nginx 解压,进入解压后目录执行:3 d) C( P7 r; S
- C% L t7 q9 v; p; n# v
./autogen.sh% n3 Y% V, r8 H
./configure --enable-standalone-module --disable-mlogc
3 e% M3 f; l$ nmake
' H( D% v" |* x- V三.nginx添加modsecurity模块
8 H( k; Y$ }2 y. H, K5 T% i" @6 F: a" d' ]/ M% c: s6 d9 u8 t/ x
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ z/ i6 W) l1 n
' a; e4 O. y3 m- x2 [+ c) a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine [* O# m4 t. H: [2 Z/ x
make && make install) M6 w) c9 q! {! F
四.添加规则
! r, d1 M) F8 Y; o! z j% |& y& O k# h5 j: m* }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 { S+ G1 y& A4 I8 e( j4 P( u& n
3 ?+ {5 K7 l1 n( J0 x& N. w+ d1.下载OWASP规则:
- C) W" U6 k8 p( A
9 K& x; J7 t* k8 z9 v: ?6 d2 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( `) Q' D; s& @+ o$ z* i" f6 a$ h
mv owasp-modsecurity-crs /opt/tengine/conf/- F- h$ |9 Q6 c) T
8 ^1 G9 ~0 x* l! Q& fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# }. ?' a- `0 N/ e' M4 P
2.启用OWASP规则:
# O; h8 N) @$ V/ C. w
! W# l9 |( k9 o- H& a( e6 A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# ?' i& v' Y# T$ E. P/ x' E* S% ^0 B' E
+ x6 [6 Q b) w编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 F+ O: f, Y" [4 C- e# m+ x" W# b+ f: q3 G2 m# F
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; X- N5 J% ]+ Y6 Q7 w, h @$ Z8 a; k O
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 e" o# ?: m. \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' u" F/ \8 J' z& p5 F: n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 G# h$ c% H2 | \! e2 UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( a" k/ u0 _1 [0 q* t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- m9 {! E0 E: yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
@1 B1 P' q9 B4 V, O- ]* N2 {0 S) NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: }7 c/ |# O# M/ C8 h五.配置nginx2 V7 }: i. a4 u3 R6 q5 q
. ^5 k& P5 G6 ?2 `5 s$ m* }在需要启用modsecurity的主机的location下面加入下面两行即可:
: S# L" E# K2 ~4 v
# y- H; E/ ~( ?' T& N) ?# s0 NModSecurityEnabled on;
! d, m) {. r# a) h- G5 I1 s$ G7 nModSecurityConfig modsecurity.conf;
" k" k% ~2 t4 w2 j4 ]2 X下面是两个示例配置,php虚拟主机:
1 u8 B: V7 g9 l/ V: A+ g
4 x: Z6 J0 c4 L3 T/ _3 |9 S6 lserver {% B( A# t: g% n/ p
listen 80;
3 z. p/ F( I4 v" i; w server_name 52os.net www.52os.net;
; E0 A" k* j5 T& `! P! Q* R# Y * p1 g' K/ F2 }( i4 T# M
location ~ \.php$ {* Y6 C' {! G( F. g+ S. l
ModSecurityEnabled on;
' M' ^1 j5 d2 C8 E/ {5 X ModSecurityConfig modsecurity.conf;5 |- r6 D7 } j) w
0 X& s4 i; ]6 } root /web/wordpress;
6 m0 ^: Y2 d) z& |! G0 @8 U/ s( M index index.php index.html index.htm;+ ~- U& W" l3 E) }" f
) q- ~9 b% r3 b( A! s5 r fastcgi_pass 127.0.0.1:9000;' V( L" h: s" J
fastcgi_index index.php;* t5 l# i+ f' F
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 m* ?, c" H8 `2 O+ E include fastcgi_params;8 p1 o3 f# N" B8 M6 v
}
& a/ p3 g' y0 r0 e }
( n* `( T2 D/ y2 Uupstream负载均衡:
$ K0 t% @, f5 t* l ?
3 k$ L' x" P, R2 `; E/ rupstream 52os.net {" v9 w4 y9 v! C: k z" }1 C
server 192.168.1.100:8080;
- s; R% B- I: u- t$ [' | server 192.168.1.101:8080 backup;
, M5 F5 Z$ g$ R7 Y# P}
% p, b6 Z6 z1 x+ m V7 C* H. B) d8 m# c' b# x8 @" }+ B) J
server {
+ }( o4 G: r4 H- Alisten 80;) E, s* B8 Y5 v. ]$ S4 j
server_name 52os.net www.52os.net;
% P X. `/ f% |6 u% j5 ~5 p8 z1 g4 d. t J" H
location / {' k! Z6 ?1 Q. F" W9 Y
ModSecurityEnabled on; , D' U |- K9 } y5 {+ x( T
ModSecurityConfig modsecurity.conf;
4 ?, L& M1 K) c! t( U& {
: r$ `; r- r* @8 G/ C proxy_pass http://online;6 {, U5 F* Z* P
proxy_redirect off;6 `! R) u) b3 m H" o8 E
proxy_set_header Host $host;
/ B3 @8 o4 ~3 _$ |( v proxy_set_header X-Real-IP $remote_addr;6 S# h9 o+ y. x% b) C
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
G* F' A. [- n9 q# B2 ?* B }
1 I; l! n6 w5 q, m- T}& N7 K ]! H! w. |9 O1 g) O* ]
六.测试7 C. H5 X0 S, z0 A+ q# k: Y" b m1 ^
4 N" ` Q7 B+ |0 z/ ]7 q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# z& X# ~9 _2 E3 j' Z9 S2 x6 n5 Y& A$ E( H5 Z% Y
<?php/ ?# ^0 Z7 S! z" O* C
phpinfo();
$ f+ F( z. H+ Z) C6 a?>
0 H. P8 f0 R4 N7 r) K在浏览器中访问:+ @( I- [ c+ T! |; u+ _- h; \
9 M4 y5 F$ S; ^1 o8 m: ihttp://www.52os.net/phpinfo.php?id=1 正常显示。% s" w, C. r4 Y& l' H
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 m$ g* q, `) H( m* g7 W2 b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
7 q- n5 ]" S: [0 t! }说明sql注入和xss已经被过滤了
( r' Y* {4 X7 w! n7 _% _. c1 ^+ g% Z4 a& L) a
七、安装过程中排错
& C0 }1 G9 a# e- f8 J& |
6 }4 ^ V5 y8 j8 S4 j+ g5 s# _1.缺少APXS会报错; }5 Y" Y* ]3 Q! ~! x$ D0 D) y# L
. [5 t ?8 y4 _1 Y! k& Vconfigure: looking for Apache module support via DSO through APXS2 V6 i8 \& W7 I( E. E, Z
configure: error: couldn't find APXS+ e3 m3 c" [$ d/ M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& V$ K9 Z9 _; X8 |( [
解决方法:
! o6 H/ X! W8 N8 Y
. R/ G( o$ |! o' c0 ?: w! V2 oyum install httpd-devel
) Z& Z! K5 p9 {+ b2.没有pcre
3 _' S" y, L% n/ u5 L% O9 E) Y+ f, R3 u+ @% I+ n, o5 o6 W
configure: *** pcre library not found.3 _" u! A; U6 P4 s" x
configure: error: pcre library is required# X1 s( b B; e2 J# t! k \
解决方法:
3 Z3 q+ r% _: L1 p; D. f) W1 a* E( @; q, N6 k& G. x
yum install pcre pcre-devel
6 w. Z% z( ^% l3.没有libxml2
]& K- I2 g: s% s' `# ]
) }" _* C& g1 E% g4 d8 @7 K
+ s2 C1 y% M. [# }configure: *** xml library not found.
- @, c9 X9 [" e- R' q8 O! ~configure: error: libxml2 is required
- g4 U; p6 k" w3 W3 C解决方法:2 Y; P9 }" e; E( E& f
3 V, o p1 t( Q# _) J% a1 ?yum install libxml2 libxml2-devel/ L2 P+ a0 x$ }+ L; c
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ }- `: s( d. y7 `
5 P7 ]8 n' _: CTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ I' Q# @' s1 \5 g4 y' enginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 c; z) j) Z: U9 x# u% z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. U4 i) o& T) t
+ U" q% O; n$ K$ @' H& q) ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 z* \) f3 X7 _; b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
# M( T2 C- R( v+ L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! m6 \ F9 t! f6 D! t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ {& R2 H% k0 I- w! V3 i! Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 d ]: b8 a% r* U, _3 j
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# S% ^4 v' m% Y5 o3 y
解决方法,移除低版本的APR (1.3.9)
# P3 s% R( G/ C) _
2 v2 ~: B) [( i# p1 w' g. syum remove apr+ i, S8 S' p& J# Y
5.Error.log中有: Audit log: Failed to lock global mutex
" T+ |/ X, b' [: s$ L0 E+ e3 L3 v* K6 b7 I' i9 t
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " ~: S1 K `. R$ R
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 N* C% J' r: Y% s/ k解决方法:
$ d% \2 @/ m g3 `编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; u0 p* N2 l* W: o& E/ P5 _$ O. B$ p4 G; y/ V i
SecAuditLogDirMode 0777
. x/ `9 K* r: {SecAuditLogFileMode 0550! _/ ], v% E0 d" g
SecAuditLogStorageDir /var/log/modsecurity j' m5 a0 e4 J d4 I
SecAuditLogType Concurrent' B" V3 k7 p* U. @6 Z; _
参考文章:
6 b( |) g5 x) jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& \3 f% A9 F- r
http://drops.wooyun.org/tips/2614 |
|