|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' r' T( c8 i0 g1 S5 w" E) _0 j" j+ j2 _. I
一.准备工作
$ a3 ]2 ~; u7 o2 s5 z. |7 u
5 |* E' D) l) o* o7 Z) Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- Q1 u6 o9 g X# ^) v% v: z& p/ O. ^
0 [' _0 V& L0 m0 [6 z! `/ h) btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& v- _1 |. D ]- w
4 \) r& W" \% V! G8 M* C- a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% T0 Z3 J7 `6 K9 _8 b8 j5 n( w( S; T* B F: H- D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ W% ~, e3 t2 y& `! W4 w8 l' i9 R) x# U
依赖关系:5 m: X, T& O/ D6 U
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ L+ o0 X/ x2 h3 B5 x
3 M4 H& ?( b3 Q; Gyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& {0 q% V! a- y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. {% H2 n9 Z2 l
0 R! i7 D6 @1 i6 Myum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ T! j) l5 Z+ h! [3 j4 u
二.启用standalone模块并编译7 ~" W8 J. b7 H. C. T; r: W
4 U" z) \4 I2 D. |5 U; i6 V( B; j下载modsecurity for nginx 解压,进入解压后目录执行:
# |4 P+ Z' G3 d1 s4 K
- s' u* _0 L |8 [ P7 o' \./autogen.sh) W/ S3 x& F! z l' l$ p
./configure --enable-standalone-module --disable-mlogc& l2 Y3 f! ]0 s
make * O* D# S1 Z8 e9 r. Q: E5 }$ I& _
三.nginx添加modsecurity模块( L. @; T5 c) X/ Y, ]$ j+ l3 e- U8 L
8 v* a* f1 B/ Z' n7 k9 k& R' e在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 c+ A: R( o _) g+ o
3 X' W& T) n' v# x
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
! B4 P# B: C) Y8 L% j4 [& t/ _7 X1 P: C6 Qmake && make install. ~8 d! [8 b# A" d6 }0 q
四.添加规则& E; K+ ^, \8 Z" @ V* [
+ l$ @/ }2 }5 u1 D- A0 E5 nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 W! p3 P9 [; J. c% G
& P( k) f2 T9 E- z A( G" @1.下载OWASP规则:
3 X* B) A% j' s5 }' C
! e, v/ W, c5 K0 w% C5 B4 ^5 \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ a2 E$ j3 b) C6 s2 [8 V3 F) h: r9 x8 V
mv owasp-modsecurity-crs /opt/tengine/conf/
$ P2 _ n/ i; t8 W1 \ x; ~ ~# S0 g% X. b4 L' j s$ |6 B; ~# B
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 W7 W6 t* D9 ]% V* c2.启用OWASP规则:4 m& T0 a" Q1 s
" C) |1 f/ J1 @复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# q8 Q- u# c5 M# A# t. o" L
, z" v8 C5 m2 H+ H/ p% M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: i$ Z' y0 `- L3 e& m% g
# w1 `1 H2 P j Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ Q+ G0 z' H' Y5 N( J+ M
% G! M+ c" w2 ~( F# o2 f0 ~7 p1 JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ [! V: t3 |/ X7 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ D! j+ c/ Z c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ E$ |% M: o/ b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" a$ f( F: R' U* z, QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& h3 S2 M4 d$ p8 ~* }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- \+ D! U$ N- A' k, b6 q7 j7 \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ E' U, |: X" k0 g. ?5 Q
五.配置nginx& U2 A1 s6 R3 H: U8 P; e) y' q2 g+ f
# }6 I7 C) m& H" a, i
在需要启用modsecurity的主机的location下面加入下面两行即可:
. O9 z" M/ b8 w: t! H8 T- V) x) N e b" ?6 H! M. i
ModSecurityEnabled on;
' R) k' @# b5 w* ~- XModSecurityConfig modsecurity.conf;
8 t9 F) K, I' f) i0 ?, f下面是两个示例配置,php虚拟主机:
4 c) K: V% ~' b6 {/ i
+ c/ ]* p3 K+ X6 M" x& Aserver {, L) S: S3 ?3 p$ d: H& n
listen 80;/ C9 X9 w2 z9 ?* v E
server_name 52os.net www.52os.net;
& Z2 y3 F4 B4 j9 J 1 y8 s1 s% C7 N, R$ o
location ~ \.php$ {% P, V( w4 _; M- T( f
ModSecurityEnabled on;
, \7 {1 @0 R B+ k; e ModSecurityConfig modsecurity.conf;
i1 G8 }5 }* p2 g. t
( u t f9 S1 @4 A; ^/ m root /web/wordpress;
: @- [7 g& i) C, m, l index index.php index.html index.htm;, l! |1 |/ K8 o c1 ], a6 s* a
( U4 {: }. o9 _: Y
fastcgi_pass 127.0.0.1:9000;- W! w! q: l2 V1 ]
fastcgi_index index.php;
8 c2 {) [2 a Y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
+ G. C Y- O5 ?2 _: |- B include fastcgi_params;
' H& ~" }# j/ z6 k* e! L8 ^ }- J+ s! R4 i5 p$ V7 P) j* J7 n {
}
6 |" Z& u& Q6 H7 Mupstream负载均衡:8 r# }2 P0 B0 Z% ]. L
* A5 O' c& n# kupstream 52os.net {
" \' G% p* }+ d( `$ S& v server 192.168.1.100:8080;
4 P' f# a, Z1 \8 U+ H server 192.168.1.101:8080 backup;0 i' X' Y# u7 B, }
}) u: u+ e2 i, |$ L
- C% J/ a! F/ D( Fserver {4 H% I: m7 k7 t' u
listen 80;$ D: j# [& u$ l! _
server_name 52os.net www.52os.net;4 {4 j& z. Q& H" x% z
" ^& Y- x' e# J. D
location / {2 l1 X2 s$ K, B+ U" V! h6 a
ModSecurityEnabled on;
: c; |1 r z! X7 N ModSecurityConfig modsecurity.conf; 5 i* H% U* |/ Z# K
# r8 D+ {- x `+ s proxy_pass http://online;
# ^& w8 o1 N. f: O proxy_redirect off;
4 ]/ M3 V% o- G8 | proxy_set_header Host $host;
% c$ W1 `6 L0 q3 b) M' K proxy_set_header X-Real-IP $remote_addr;2 }1 T2 ` w- S1 O
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% ?- y* Y- y% ?/ Y }9 @1 i9 G0 B4 q9 F5 w; P
}6 t, P0 L" Y* n& ~7 ]% B: R( _
六.测试: F0 d, Z' F9 g5 f8 T
7 Z T3 R3 m0 m2 q; Y7 ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- c, s& b$ Y) V6 l/ [* @# R; X) p$ z d- \7 M# M, S) ]
<?php& {! s- Q) V& ?6 X& r
phpinfo(); 2 k5 ]6 z, ~; f) b( q
?>
& b4 S; E J& D6 I; l" m; L在浏览器中访问:
- w: Y8 @# j, ]+ A$ V+ E5 G/ a1 `" l% q9 m5 q( d, [
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 P" l" r* q, v) ~. |http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 U! v( ?! J4 t& R* mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。3 D& r" K0 s& U8 c
说明sql注入和xss已经被过滤了0 n' m4 C7 X9 a7 S4 N2 c# C
% F! J! W0 T0 `0 R n
七、安装过程中排错
' h* e# X0 ]5 ]7 [1 ]/ a0 \
/ c2 t+ a( F2 t" s% @! z* R9 w r1.缺少APXS会报错
# j/ X) F! b4 ~0 Q( Y* {8 p$ k4 Q$ v+ \, X- Q
configure: looking for Apache module support via DSO through APXS/ f. [, w# F+ j- K7 d$ [0 P
configure: error: couldn't find APXS3 Y: W% Q+ V0 S
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' b5 r( |6 @4 R, F解决方法:
. }* } F5 X' E/ b- O+ s7 y/ g# ~/ N9 R. G- J0 V
yum install httpd-devel' n$ o* P, H4 G; h5 I- \ ^
2.没有pcre$ A" |! z" Z" E8 K- b
c# o7 r! L+ d$ Fconfigure: *** pcre library not found.; m; f5 c3 V @& F# M7 Q: M
configure: error: pcre library is required
8 I& p6 ?, s# B# N! z- |- n解决方法:! K3 ?' ?. N9 X" L$ g6 `& E
s! G+ d b7 r; T
yum install pcre pcre-devel
8 k5 e9 }- O U1 }3.没有libxml28 _9 ?+ l8 | ~+ }6 [" F
8 x+ m" }( z8 b K2 ~( D" f4 r! q1 p. y% Y. I. _5 T
configure: *** xml library not found.
0 z. }* [7 L- i9 @# D5 t. Gconfigure: error: libxml2 is required. s4 I' ^8 V7 b Z. ?' P
解决方法:, C$ O7 g. e! r/ f: c! o5 n
% [/ _/ P4 E! s+ S+ f7 A# ^& G7 Ayum install libxml2 libxml2-devel; D% w/ c9 g" L; c7 g% Z+ z
4.执行 /opt/tengine/sbin/nginx -m 时有警告7 v- e, _8 k3 ^. P$ d2 T1 |+ H
" i7 C$ S \+ @2 z S- n$ p
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
3 f I' Q4 p# }; G6 [nginx: [warn] ModSecurity: Loaded APR do not match with compiled!) g' ^- v/ V! M- I8 w
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 J j8 [5 A9 G8 C/ ~& L3 N4 w1 e$ Y6 y" u! R6 Z, g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- ?. [+ e' |7 x0 p' R8 P7 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! X1 e; n& t6 f: v, F& d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) `# g5 F; s* O7 G4 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* E8 t G$ c8 @( B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# A) x+ F. l4 W* _2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 R- [! u$ L }- B3 p/ T
解决方法,移除低版本的APR (1.3.9)% w k) G' }9 s4 p9 a
. ^6 y n- W, I7 O, u+ K: jyum remove apr! q% \# F5 v4 f! s
5.Error.log中有: Audit log: Failed to lock global mutex
. @; V7 c" b# [3 c# P& Z% \+ |. O: z
- i" v8 `4 @' |: `, W2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) j1 f& t* W, q0 E* u2 J8 aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& B$ A, n( J. b+ x" W' C: |6 L5 i解决方法:; v" ]. ]/ S ?+ c; F8 {+ o
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 k7 ?. d: T: y# d
+ k- i1 v! P& O" YSecAuditLogDirMode 0777
# \6 | R0 F9 C' b' [SecAuditLogFileMode 0550- z$ W3 p- y* F' r" j
SecAuditLogStorageDir /var/log/modsecurity, X2 O6 ?% s# ~8 G* E' [
SecAuditLogType Concurrent
8 |( p) E; V. n9 V1 Z参考文章:! d0 ^! b: E( H) J
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# _: S" \5 \/ w6 H8 l( r; |9 Uhttp://drops.wooyun.org/tips/2614 |
|