|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 V& C& g. d5 i+ z2 M8 _
- o& K5 x; {, I一.准备工作
) d: G5 W+ z$ M& g9 z- N t4 M" F' }! k
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: ~3 T0 Q3 D: L6 l g
( ^: h. x: P: e% e( K5 htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ S$ N0 ^" g5 E
- T# K- J1 h- D j/ {7 b$ j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% O: I+ E8 G6 H) S* l- h% {' ]+ C- |% m
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) m8 s" P; `1 N( S. A
5 u3 w5 p9 P* A e# ^依赖关系:
' g. c7 e) R& ^1 X5 i* Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' {/ v+ c, g9 |+ F# v- m& A
3 e: u% ^6 Z$ ^+ Q0 Y7 ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
3 V7 _4 U) G8 l) {4 X" `" ~modsecurty依赖的包:pcre httpd-devel libxml2 apr- R% y7 E' \2 h& E0 v0 h, c+ Z
% W* R, R! `# {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
S( I; K1 M) G1 w" S二.启用standalone模块并编译
7 I5 p% g$ a7 Q8 b4 a U& z! v/ y3 {' ]' T
下载modsecurity for nginx 解压,进入解压后目录执行:% s; m$ R$ p, z3 U* O4 t) |
; K: b5 {; B% I; Z- d
./autogen.sh0 U) u$ L1 E2 X, X7 |/ _
./configure --enable-standalone-module --disable-mlogc# V/ B# F0 b ]! M! L. j- B7 T- n
make {% H9 Q; H* K) Y) x* q: _1 d
三.nginx添加modsecurity模块
( z% I( J0 l1 E" E4 ]: c
5 d2 K. I8 M2 W8 {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 v$ t- q" z( }5 [8 c0 d5 ?6 r, H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
4 z1 t; ]: Y2 X7 Lmake && make install
) {$ I7 P B: r四.添加规则& u# Y7 _) ?* S
3 T; h8 J* h2 G6 P
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' c/ R( C, r# C. _/ }( H5 V
" I% ] l$ A$ v# a+ J0 W1.下载OWASP规则:$ F& Y' }5 F; y. y; j" d
) {1 o1 c5 ~4 l8 ^git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ U7 x4 z# {$ L
3 Q5 d( N Q5 P- A' A6 Xmv owasp-modsecurity-crs /opt/tengine/conf/
6 u- J! _& ^3 e1 ~% S! K# G7 ^, R5 m) D9 x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! ^0 e, f* {" H& V1 A2.启用OWASP规则:. I6 H" ?2 V/ f3 c2 @
6 y3 n) e' E5 o
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ l' M0 e0 g1 \' F
' Q) D# ?$ a/ S' ]2 l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& s- Q l& W9 p4 P, K, z- K! X }
8 }- e2 O" e" `8 t9 `. f4 v7 kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 O2 C1 W% [- Z6 V2 C! x, m0 N4 C, L1 r6 @. p
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! O6 C& ], x# l, N8 W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ [3 W( s+ c( k* RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" j M& r1 p4 F3 I+ | ~+ i9 g; _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) O9 l3 G/ I+ _3 f$ dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 F+ }) n7 X9 R- @$ v0 CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
, }0 _0 }6 P) b0 D2 _Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: F) }) G1 Y/ o; H8 `: c T# m2 I五.配置nginx2 K e6 J! T0 g3 f3 ]
: [% O0 s* y: |; h9 s
在需要启用modsecurity的主机的location下面加入下面两行即可:- U" @; v$ y* b5 }* R/ M7 U! g
. I) [4 j% c! I4 @4 g
ModSecurityEnabled on;
5 `) ~# F! h% eModSecurityConfig modsecurity.conf;
3 S/ a+ R% z: d, [下面是两个示例配置,php虚拟主机:
( @3 P1 W! D) ^# r( h$ a
# a/ c5 I% F2 {' S9 aserver {
( f L" V/ k7 ?( E- o: W, y5 @4 | i listen 80;
4 x C+ c0 s; Q' p; f server_name 52os.net www.52os.net;) B! h& y2 q4 U$ Q: z3 }
; w8 _# D9 ?0 h location ~ \.php$ {
+ j1 V5 F' y7 `' b" q& i ModSecurityEnabled on; / X' R% u& w6 q: ?9 Z% j* P) J
ModSecurityConfig modsecurity.conf;
1 ^- R8 z* [' E% M, A9 p/ c/ S; c3 @, N
root /web/wordpress;
6 Y/ R6 X+ [( x8 I/ n6 c index index.php index.html index.htm;$ \& [' A8 X1 @0 I0 D( d
3 y& y$ f! ]: @ S4 ]- _ fastcgi_pass 127.0.0.1:9000;
4 }3 [4 l7 G) D$ j! J8 j fastcgi_index index.php;# l3 F h4 g4 W& N5 M4 F: [
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;2 \: ~/ P/ p6 C' v( t
include fastcgi_params;
3 E% r& g/ o9 { }
+ y+ _% G9 x4 M6 I1 R4 d4 a) e }
- X8 ~6 k, c1 ]( H, @upstream负载均衡:
4 x+ J- K) ~ w0 J; K+ t0 T
& D* e9 r! u7 B$ i4 L7 [9 Bupstream 52os.net {
' [; U# P3 N, j: | server 192.168.1.100:8080;# B; g2 E" O @* r% Q
server 192.168.1.101:8080 backup;9 n9 P6 `/ u9 `) J. G
}
+ ~5 ]) }6 b4 j2 @
6 L0 K O, t! N* [9 hserver {1 i2 l# P5 N* S* |9 k% S8 j1 }
listen 80;/ z& d+ M9 J' c, E6 C8 R
server_name 52os.net www.52os.net;
5 S- T6 C% V$ I3 |
- l8 ~8 e2 b& G3 Y+ P' zlocation / {
; h& C5 H: t( w8 @7 q% W! G' l: X ModSecurityEnabled on; * u; `4 {$ M/ r6 L+ l
ModSecurityConfig modsecurity.conf;
+ }7 K$ p: W% {6 t( G# m2 V3 G! K- c2 R' m
proxy_pass http://online;2 D) t% w/ x3 n1 H0 F& |3 E
proxy_redirect off; k" k- p7 @4 u. L; [2 M6 \
proxy_set_header Host $host;; u, p7 ]6 f& @5 B+ b
proxy_set_header X-Real-IP $remote_addr;
& \- T. {7 {$ z# s0 Y. T; @$ J& ]! { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 p8 |, r. E+ s8 u4 o }2 `: T* @& e6 ^$ K9 q$ x
}
* }6 z. i6 C+ {4 |六.测试
z8 X: C& ^) {. q. l
2 c4 H' u* L6 h% G: Z) i' B( o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
K# H6 w% F3 W, k8 |0 Q; u l5 A5 M# x- U: J
<?php3 E ~. i; z+ N! V2 j) Z
phpinfo();
' ^* F) Y2 q4 j% s% ??>* b# }8 ], Z. T5 ~7 f9 { @. h: l/ K
在浏览器中访问:
: w- P, L, m" Z9 Q) \
1 c; q4 ^) O" I4 y7 fhttp://www.52os.net/phpinfo.php?id=1 正常显示。* r$ C) o' v% z1 b) g9 [, S. K
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! _ S* v9 K5 T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
i6 p* X/ A6 [: Y* q b' h说明sql注入和xss已经被过滤了4 w8 k ` M6 v3 s
/ [! @8 G; y) a9 b% I
七、安装过程中排错
$ q, `8 K; S% Q, h! w4 Q: a% I" Z1 C: S5 A( e$ ^
1.缺少APXS会报错
% M$ q- X1 M7 J
8 K0 h( y7 D! U }9 t- d) _( kconfigure: looking for Apache module support via DSO through APXS
. ]' o' Y+ D( P) a8 Y+ fconfigure: error: couldn't find APXS
- G3 B1 X% J: tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! }% L8 J, s2 o8 Y5 E
解决方法:
: x7 e# G* g: K2 g1 V' O6 v- I4 L. x8 J& l( `* ~! o. Q8 o
yum install httpd-devel$ g. m" {0 `3 O
2.没有pcre
$ L0 O9 }( T8 v& l4 ^# W$ X6 T9 _) H8 W
configure: *** pcre library not found.
7 X! J9 e3 x, p6 N+ T5 X X$ z( I' Pconfigure: error: pcre library is required# C3 G. \! `* X9 w2 w1 Q
解决方法:4 L0 {2 p/ h0 Y! M& | b
3 B- R4 W( ]: z0 z
yum install pcre pcre-devel( _0 l$ m' V) i7 W* T
3.没有libxml2
" r' ~+ y" z" s6 Z, E3 V ` L0 q( I5 T) P6 K
: j9 G( F) T# r8 e, _; a
configure: *** xml library not found.
5 i; d) R, v0 q( A2 pconfigure: error: libxml2 is required
/ s, }) j+ d0 B解决方法:
3 F; Z. g3 K* j+ l/ E' v5 M
! x0 Z: D0 H- S( byum install libxml2 libxml2-devel# f8 g; b$ m/ b$ n% m) @
4.执行 /opt/tengine/sbin/nginx -m 时有警告
# t f& d$ \. V! a$ @* \6 n6 \$ z4 U; t/ l: I4 T8 |/ i* t
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. N4 n9 X0 o# J: J& J7 u* jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 U1 r; e& h6 r+ H5 }' e9 a, ?原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. q0 e3 Z) @+ K8 L1 v5 w9 E
5 y) d# P9 N9 ~7 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." Z; ]" C% N4 c% R0 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": V: z/ I2 `7 Q% n- S% b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 ? i" Z) _% S- {+ H, @; A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- v, I2 b3 a3 U+ i+ [+ A8 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 o. q5 y% r. z2 f/ i/ g2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" M1 ~; a% w1 M) O0 H. @6 D解决方法,移除低版本的APR (1.3.9)
; i2 F: i3 w! m0 ^% u: e
! y5 g' A, Y: I8 |# t& Q1 d$ i1 O# |$ Kyum remove apr
4 s# m2 J( _0 ~8 }) i$ v: X! P1 h5.Error.log中有: Audit log: Failed to lock global mutex8 w: }) ^" F$ r \& [ x
6 D9 e f2 {& V5 s [) @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 S% a; V2 N) N# q* v: N4 ~global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 u5 }; h( I0 A0 ]! C7 x
解决方法:
2 q- ~- b$ K! y8 z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 g8 W9 v0 `, e/ b( o$ c# Q8 y4 h$ P6 Z* q2 M$ n9 @) k
SecAuditLogDirMode 0777# j9 u. B. y/ M$ s
SecAuditLogFileMode 0550
& n3 ]2 G; {( |SecAuditLogStorageDir /var/log/modsecurity; ~, x2 h l Z! E( A* @, h& x
SecAuditLogType Concurrent
0 z8 i& d9 s2 E4 v1 @1 l参考文章:( }1 Z+ ?: I8 a" q/ Y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 t+ X5 O' b. N) _% Z+ D! ?http://drops.wooyun.org/tips/2614 |
|