|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( W8 n5 u+ ~: _ s4 Z4 ~( M
) }) t* m8 O# B7 @, e一.准备工作0 w# [7 e) r" e% W7 H! I
. \8 C" }4 |, o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 R* I. \: {' W) \/ H: l: m% B- D) M5 {6 a' t" v* l9 f0 n( l
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! _" S, G" ]4 p5 X* G' `' t" d7 D- l7 S# B: n5 Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% t+ b* K# d: J/ y. j- b. _5 e! v+ q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: z- r9 r: m* g0 _ E b1 D' a; K6 _) g/ m1 ^1 n' y- V
依赖关系:
7 n, [! R+ M8 Ntengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" o. |- M% E* J& C* [! M, e6 z
' }5 B# c" |7 F0 a5 y, }& ?
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* s+ C) d0 `# |! [+ umodsecurty依赖的包:pcre httpd-devel libxml2 apr$ w, A! d- d+ i! h5 L) w: J6 ?4 ?& j
2 w6 x" Z$ O3 V' F" Nyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel5 ?! s/ w5 q+ z* V: f8 b4 x
二.启用standalone模块并编译$ y+ A; R! Y, E5 @
2 g. Y$ G" W5 v0 x9 P5 t下载modsecurity for nginx 解压,进入解压后目录执行:/ _! O8 H5 ^5 E% \+ F# {
# W+ T6 J1 G! ~9 A, B. P* Z; W
./autogen.sh
- g8 i. z% f4 B S4 ^1 T./configure --enable-standalone-module --disable-mlogc
( v" c8 J! l. D* i( rmake / K1 I# S& b8 V- [$ p
三.nginx添加modsecurity模块* D, O1 K! |% w E- E
: K' D* i! U0 A% Q- I$ t6 H( Q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) @& L8 i7 T: |# q
- r' U7 y# a+ O9 ~" f7 r
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
. S) p3 _. _& ^5 r' p4 I% umake && make install# u2 p9 Z7 N# g Z8 g, j
四.添加规则, l4 ~' P1 q4 y6 S. ^
# m- c* V% q6 Y" P& x3 pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ j; x% {% O1 j: ^ R3 Z: ~( ~# {" p
1.下载OWASP规则:) l- o% g; O9 {5 k
6 @# x% ]% ?9 l7 a4 l; n) Ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 ^& O ~. w4 a0 B' x Q1 T5 h: V0 M. ~! T# q4 Y
mv owasp-modsecurity-crs /opt/tengine/conf/
% A6 r& l; g' a% n R9 L4 I4 Z3 J7 i" O5 X! c: N8 k' o& S4 s! }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# b( {. ~0 {1 I w6 h; l) j! n2 d
2.启用OWASP规则:2 ?, ^. g f. z: ]0 }
# c8 z9 J3 B% T- \$ W
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 I/ z8 k! g3 m* J; ]+ {# H; `
: L0 E* g w. G/ \( e: a: @! M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! e9 r2 s4 h Q# q4 [. k
6 M% ~6 f) o# [, a
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 d9 m) l" {, ?- y+ h
& W2 d# k/ w" b+ M, ^Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 b0 v. C4 S3 C- `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% r+ d" }9 ~& y5 P7 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 T* g, w7 }0 f) N: ^- o9 DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% x# C) h& \ B- `: k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 L% |9 D t: S' M& g# k/ X3 Z, [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" w3 \) E3 {8 ~- AInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) X) F8 m( v V
五.配置nginx3 w9 c* s3 d$ U5 j0 V8 \& }
# X1 b, Q: G+ t6 w7 @在需要启用modsecurity的主机的location下面加入下面两行即可:' D) Y7 r1 |5 }0 }* Y& x; e* U
2 j( a6 k9 h" [# ?5 s- yModSecurityEnabled on;
: z, E' {- u1 VModSecurityConfig modsecurity.conf;
* X q3 {- y- V" W! M! v下面是两个示例配置,php虚拟主机:+ w9 b) }2 v' c1 b0 h
' B( ]8 A- n, k3 n$ o* X
server {/ a& \* n! ^) M% T
listen 80;0 F3 {' h* ^5 `; v7 P
server_name 52os.net www.52os.net;3 Q) f& d; m6 U7 o& V
, X: h: a) M, n6 n0 V location ~ \.php$ {& G- ?9 L1 m! |7 g, {
ModSecurityEnabled on;
' l n5 @: v6 U. f8 s ModSecurityConfig modsecurity.conf; Q5 {: s7 G7 A1 ~, C) C0 C3 }
; x8 _- Z& ]) C6 n, g- k7 [ root /web/wordpress;
3 W. C% o6 j8 S$ N3 u. C5 e index index.php index.html index.htm;
. \, o2 w4 }* r/ V
6 a8 o. u c9 Z* l* r. v# w" f fastcgi_pass 127.0.0.1:9000;$ R, f6 P! k7 G; n) h/ j* y
fastcgi_index index.php;& o. E4 q+ J: p4 e8 S8 ^
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
+ d2 |& ~7 j7 N5 [/ @ include fastcgi_params;) m. k( I3 L; T* i0 J. t+ @9 Q* ]6 }
}
- t N* f* w2 [% e7 a; e) P }" m: d* P3 M; w/ P! ]
upstream负载均衡:& e( G( k( B5 A0 N. w1 ]1 o
" I/ V9 n1 T1 s. w1 b; c7 D5 ]
upstream 52os.net {0 ^& W8 ^0 r, N/ Q$ i U; E
server 192.168.1.100:8080;8 F" r; G0 w/ k8 W6 H
server 192.168.1.101:8080 backup;
1 b. r& a9 E( y}
$ {9 O5 _6 V, I9 D; I( m1 D {) ^$ U
server {9 H9 m; N7 t: Y! Q/ S- U. [' A
listen 80;
1 q- ?" w( O/ f. Y. oserver_name 52os.net www.52os.net;* ?- A X8 L: o5 m6 w
! _- v! i- R5 K7 b
location / {
0 ]. x/ [! w% ^$ q% ^ ModSecurityEnabled on; 1 e# z" W& k \+ O2 p1 k5 |3 c
ModSecurityConfig modsecurity.conf;
- j% L% j- o% C- B0 D3 E* _4 L+ N# z# r; S; G8 }7 y+ L* M
proxy_pass http://online;
- r5 u6 W: j0 z5 Y. E( E7 Y proxy_redirect off;! }, X7 Z: t/ \8 W: V
proxy_set_header Host $host;" W/ n$ S( B% p+ G- ?& e3 d- O! v
proxy_set_header X-Real-IP $remote_addr;
L2 c. S8 c" s proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;2 ^$ }. j+ F8 L8 U$ x4 v) N
}5 G% d* e7 @! n, ^: G* r
}$ g2 ^! g' G5 F0 X& k( q! R( @! N. ]
六.测试3 t' ]. h2 s X1 w. k# G4 N# d
! V5 L0 R* P: B% [0 M& r, u我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 g0 V/ E: J3 O
) r- k* V9 z g<?php5 K. {$ `5 }: w8 @* Z4 {
phpinfo(); # P5 a1 {9 ^# h# N: [7 ^
?> h$ }9 F+ ^4 _& h
在浏览器中访问:
% Y7 A+ P2 {4 n, B; e/ v( f: m% n/ E7 R
http://www.52os.net/phpinfo.php?id=1 正常显示。
) h1 c8 \& i* p8 Q: M' yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。2 E, i7 e9 I+ B/ l$ X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ h% O4 Q* g$ G! ^' k F4 f说明sql注入和xss已经被过滤了
3 X' f+ |/ w: Y
! J0 U5 P: E; f8 L% A1 q9 r6 t# w七、安装过程中排错" z B0 j8 v. ?7 @/ L1 ~
9 C3 u G4 ]4 a4 o3 r
1.缺少APXS会报错! r4 J* Y$ e( Q* P
* k# A/ [& H8 F5 [/ a
configure: looking for Apache module support via DSO through APXS2 W( Q- p8 |& n2 F
configure: error: couldn't find APXS
6 ~9 R2 F4 P' y0 Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 c; \9 P- g% l+ e
解决方法: [: c$ ^" c e5 k
8 w& S. `! |5 ~) _
yum install httpd-devel
7 O- @1 M. D9 p, C; h* v- h2.没有pcre
4 c/ m- ^+ W( |* {( y# J" t1 B& |3 u/ L; u) L$ `# U- |, v
configure: *** pcre library not found.
! S7 {* q- X( ?2 y; U6 i: Nconfigure: error: pcre library is required' n0 i; S# C& I- l
解决方法:7 D7 K' W' L( R
; o- x) ~: q. S- U
yum install pcre pcre-devel5 V6 c+ ?% f0 c
3.没有libxml2
2 d; I/ a; E& ]4 G. O, Y( T a* `0 Z3 m) Z# O
" b0 [' P' c! Q/ o K4 a9 `6 Kconfigure: *** xml library not found.
2 [/ O# A+ {2 Hconfigure: error: libxml2 is required
9 y' [- h9 w5 W+ }# V* T解决方法:
9 e9 \+ e( }! a$ i! W7 s" I. M# y7 `0 B
yum install libxml2 libxml2-devel" Q: E- o6 i ?8 q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
@% Q+ G& P: g- w8 O( v, N& E3 |$ `' b h: @
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 A! |2 M3 m! V' e, n
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" J! \8 i* X9 {& h# U) Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# W2 o h7 i3 E+ n/ S7 v4 i/ H0 l
9 ]8 E8 i5 h0 _5 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
9 T8 U5 A: Q& p9 p& W# N5 J8 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. ^; S: V1 n1 s2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ a/ F- t E& g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". f- ]# l5 O6 ~) _! S7 F8 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* o1 x! h4 r! u% E$ N# X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." F( X& Y1 B' Y7 V0 X! o
解决方法,移除低版本的APR (1.3.9)
" F) E* H$ Y0 G* o$ {6 v/ V
: i* Z: t, b6 ^4 {$ ~& S! W, X- O1 ~yum remove apr
" V& L; t: T$ X& T6 W, o5.Error.log中有: Audit log: Failed to lock global mutex
0 C0 \. Z5 b: m; C/ X. `+ n
6 W7 E. X( H) H) y& H+ v) r; c8 h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( A& h( e7 @. v
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( b8 M- i& ?7 q- g" E解决方法:! z$ W9 o+ E& a6 m0 E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ ^# J1 U5 U: g7 u* A# R6 l1 R! M1 Z, ^# {* m
SecAuditLogDirMode 0777
2 c5 u" U& i4 K: d* r: ISecAuditLogFileMode 05506 l5 ], ^, J ~% r4 ~, ]
SecAuditLogStorageDir /var/log/modsecurity/ l3 D, O3 d1 J7 k: o4 w
SecAuditLogType Concurrent
) r8 {7 Y4 h) p) [参考文章:
s' q) N: S( Bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
L$ S1 I4 D& u: k# |6 Chttp://drops.wooyun.org/tips/2614 |
|