|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: v# |+ K$ R0 g% [/ P7 R
A6 g$ e/ U3 Y: m6 e+ l一.准备工作' h( y; G1 k. W# N
! M! N, {/ {, j6 _; F8 y0 _* F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& R7 Z2 T* K. S% @- t3 B: ~; E9 G, K8 }7 s$ Z/ [' K0 R- e& \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ U8 Z' w5 F; P$ V: e! y" t) a' A, r* y5 F$ E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ ]/ u5 l6 Q" `5 L0 j# ^( x. }/ S' V' z- \$ {0 b1 L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( o/ S7 z) Q) f" m
6 C/ ?8 L, [) [2 j& Q, w' X依赖关系:0 B4 r1 W3 T- b+ H
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 g; M4 I% E W
/ U$ }# B1 i$ @9 e: d3 ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel' w+ ~; L* Z* Q7 X
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ {9 u3 Q# {' z% L4 R% E$ [5 s/ Z7 y% v5 X( T
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
6 x$ M: }. O+ w/ j1 d5 N" G二.启用standalone模块并编译/ P4 y5 y* c6 { `8 E/ n- t
) F% h" J+ M2 b8 |1 \2 Q& g) h/ [
下载modsecurity for nginx 解压,进入解压后目录执行:) p- x6 j! t% m, h# r
: o. o+ W J; t3 J- f; C5 I./autogen.sh
) Y% v$ Z2 _" h4 p./configure --enable-standalone-module --disable-mlogc
. T: z8 u0 D y) ^5 L* l3 Hmake
+ x% c# }0 c4 w W- |三.nginx添加modsecurity模块6 H0 W# N6 `$ h0 _3 ?0 X5 x
6 ^9 I1 t: v; r2 h* s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 q5 O9 E3 r- \/ Q# F9 G: F9 y
1 c" V) I; ~7 [5 r" b./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine) k$ K P, \+ I: E7 e4 q
make && make install
0 ~- R% g* K0 x: w四.添加规则
8 R& E: e4 U+ ^
Q$ B9 W$ v9 F7 B. ?" j8 rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。3 y2 G" M+ r: l$ t( E+ G
, Q9 ~# J$ G2 x/ o& W1 [
1.下载OWASP规则:
4 ^) V. m% ?/ a) j0 |" \
; K9 R! F/ K/ agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. ~) ]) C" o$ K- B8 s. a% Y
# L0 l7 t/ d* H4 `$ Lmv owasp-modsecurity-crs /opt/tengine/conf/
* ^4 L" g. }# J5 d1 ^# X" Z$ a( ~9 c3 f# u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. l+ p* z9 u# o i( t4 `
2.启用OWASP规则:
1 {" X6 G+ e' i( V: F7 N0 s
+ b; p5 ^8 T( n) r) S0 y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; H7 i! X" t4 b3 T1 ~9 l" t
6 ^+ j9 A' s0 e' B* Q3 a* A) v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! K* D" |5 U+ `# d$ p" h
4 `4 A) E- @4 oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( [) d1 V' f, O. e- _" U& p6 e2 [3 U% P/ r4 }7 w
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf G% G( U7 a! ]7 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' B$ T! I6 _/ O! f8 i* I( F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 e; Y" q& [6 O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* p- P& C' |# y/ n4 }' p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% f4 j% y) K' S0 [
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ E A/ r5 `. b9 h0 \+ w) oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 r3 K4 e5 J) r; A/ l- G. d
五.配置nginx2 K& P# p4 u. v7 J4 l
1 ~4 A0 R% C G! K0 U
在需要启用modsecurity的主机的location下面加入下面两行即可:
. p; v! Y- M, N+ v- K
- r8 W: `% v, W# \( R/ wModSecurityEnabled on; d: L! D8 J" {5 G3 \2 O4 C
ModSecurityConfig modsecurity.conf;
+ d3 m& l' E, U$ K0 [下面是两个示例配置,php虚拟主机:# m' ]( y( P5 U+ D1 g1 o( X
, i/ w% ~* k$ f8 b& Zserver {
o1 l8 [! G' v* ~. R listen 80;. ~5 d7 Y6 k. G K9 }; w) u- V/ @
server_name 52os.net www.52os.net;
* O4 R' H2 L9 j: @; I6 F7 ]# p 3 v8 V& `* R7 {+ K+ s! K! \
location ~ \.php$ {" j# I4 F/ ?6 h
ModSecurityEnabled on;
2 T; p+ W- q8 T. o ModSecurityConfig modsecurity.conf;
/ L8 u. f: P$ _! s4 T9 e5 J8 y, _& G5 g$ ]5 p9 {* A
root /web/wordpress;
" P' p/ r/ m5 g% Q8 @ index index.php index.html index.htm;) s# c+ I. |5 M9 b
; a& R9 ^! n+ H8 y* j4 Q fastcgi_pass 127.0.0.1:9000;
- \+ r1 H3 p+ z5 k0 G' Q fastcgi_index index.php;
1 y! F m! Y% l fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& O2 H4 ?( b+ `4 B1 _ include fastcgi_params;
# K: N: `) u! ?( z" n' A0 n3 n* e }. p* |, G8 `7 t) h
}
- m7 c+ C- f4 r0 W& |# X; Q* W2 Xupstream负载均衡:9 W% N$ ]6 t' [( a! \
o7 g( p' Y' c6 H1 t# q, a
upstream 52os.net {2 C9 N1 }5 [! C, Q* d
server 192.168.1.100:8080;
8 N7 c$ X! [9 a p server 192.168.1.101:8080 backup;1 {2 ^; q3 K4 B& ] _: I
}
* P5 x% l9 u5 M* i, U
+ `3 V! s* Y& z+ o$ ]# C- ~. G3 ]server {9 h4 Q4 F# z% F& l+ {
listen 80;) q0 \ C; l6 N" {; J6 ]( w9 W1 A2 S
server_name 52os.net www.52os.net;/ M& t% J& M4 @/ V4 ?
+ H# U$ ?3 _! p% plocation / {
- _2 K# I) ^7 C0 i ModSecurityEnabled on; ; P: {( ^7 k" I/ J
ModSecurityConfig modsecurity.conf; 6 I4 z& ~ b+ y9 ` W d
' ]% s: i4 c3 S' W' l: V' Z6 |
proxy_pass http://online;+ |% U) G5 f( d( w
proxy_redirect off;
7 L1 I5 v8 E5 G' J6 M. J3 h proxy_set_header Host $host;
/ H) O# H. ~* ~* P, S proxy_set_header X-Real-IP $remote_addr;$ u H6 Q( {" T2 m! O, T! i
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 x8 p3 Y- j8 I3 |9 O }: e: s e2 Q$ y; X4 M
}
T' ~' h- y- P$ `六.测试
9 i# E6 h: _% x* {+ q* ^/ ^+ ?0 n, x; S! g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 |( r" e% N$ C& E. y; Y+ O0 Y! S& F
<?php, \8 e, s4 r& B! r& ^
phpinfo(); $ F! G5 ?7 e, \( Q) l
?>1 j D9 B i. B
在浏览器中访问:' I) Q) }/ R/ q! {
! S: `, M0 R, n( }7 Y
http://www.52os.net/phpinfo.php?id=1 正常显示。
' Y/ `9 M3 p6 ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 u3 l7 C+ v0 ?0 k, T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, g; j) S# ~2 z2 p# b+ Z
说明sql注入和xss已经被过滤了% V) C; z. c9 G2 `4 q$ E1 P% M$ @! b
! K) K. i3 H, m# A1 Q4 ]七、安装过程中排错1 o* l& m4 d* O7 y5 f8 o
0 M9 v8 l: n' u1.缺少APXS会报错6 Q/ ^ j! J& I1 G+ [, @- u% o
/ A z3 _" P) V9 G, a6 H, Nconfigure: looking for Apache module support via DSO through APXS" X) Z: a8 F7 V' m4 {! b
configure: error: couldn't find APXS) Z2 o$ R1 f0 q. P& Y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, H& ]" X9 N \, k
解决方法:
; J* a6 u( N. q8 ?
1 I C2 l) [# A$ [) ^3 [1 j- M5 Ryum install httpd-devel
}2 `/ m- R$ e/ q" m6 h. d2.没有pcre
+ H d0 }3 N- c4 P: t: M- [) l$ v, E4 e7 L
configure: *** pcre library not found.
) L0 y0 h+ S8 Yconfigure: error: pcre library is required
, T& m: H- s- v' x! f1 d% k+ k5 ~解决方法:
6 J8 w+ a/ D, k' {* j& V- P3 U9 u
yum install pcre pcre-devel+ R% O3 y+ `6 |% b
3.没有libxml2
! _; O( |; R3 y* H, P( v H
- D( S4 R2 c$ U: P
& q$ T/ S! o* x3 Q' [/ M/ [) @configure: *** xml library not found.; N) p x8 P+ X$ O/ L
configure: error: libxml2 is required
% K: o |+ L! o# ^# s解决方法:
2 S+ `! p$ k2 B0 W0 X' _2 K" {/ A* ]; `7 E- |9 @! {
yum install libxml2 libxml2-devel( |/ I0 @, T3 G3 Y) c
4.执行 /opt/tengine/sbin/nginx -m 时有警告
, O! t/ q* G% b( @5 d/ V1 |/ y
" d1 N* Z% r+ N1 |% MTengine version: Tengine/2.1.0 (nginx/1.6.2)
i( h$ D8 {: `" P5 vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 v |1 m0 W& |! g! a5 m7 @; y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ c" E" E; r3 W6 i" y
K! {& Z9 [& J7 ^# q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 f& h) U4 L# ^) ]8 F+ G+ c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ L% }. K2 n ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: E4 ]% I& y9 X" N& ^; @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 \5 P8 n; p) j! b! t/ Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* U# g9 U7 K0 M* o4 j2 l' M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 n2 |1 g$ }2 a' J6 [解决方法,移除低版本的APR (1.3.9) i4 @, T# c+ d/ @; D6 o
9 D o. |4 J) Q' Oyum remove apr& t2 C0 z! I" V" c' d6 N8 Y- P' L9 `
5.Error.log中有: Audit log: Failed to lock global mutex
; a7 ~7 _( A/ X7 E9 B, r6 {1 j' k* b! ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 3 {3 ?- P5 z/ x, w5 Z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 m2 ] A$ i' Y# P解决方法:
2 q \5 G; c$ p7 N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' V, K ?' d: h" t% o$ i6 H7 G- u- L! C6 U, \) }
SecAuditLogDirMode 0777: Q' Y6 U% [: J5 @, \, @% y. \ t
SecAuditLogFileMode 0550
# s% E' b& A2 m6 k% _% eSecAuditLogStorageDir /var/log/modsecurity
0 x* ?8 F# v% n+ W$ `SecAuditLogType Concurrent7 q% M9 n& @1 y7 K" p
参考文章:
( }; H! G: l' c: u1 K, C2 h+ Fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; E! A0 {; g1 Q L) N
http://drops.wooyun.org/tips/2614 |
|