|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: O- k6 ?5 [" }. a# @5 k$ y, E$ Y8 z
# o) a* s# V: v- \一.准备工作* m P/ m& |6 q. H! X9 i; H
; R0 `# l& Z8 E: {* g7 {
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 X$ ]+ c7 L e2 y8 C7 Q# @
' m2 y& r' ~# a# ?2 otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 B# x% H- ~' n5 t7 g
* T8 R y% G) _- Y: H& dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# J) A" w1 h% R0 v% Q8 A$ X* V
, i& G! ]$ c) R! ^5 y! DOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, \1 g4 b' J8 F& w' Z( n
9 [" h% D+ a) K0 ~8 a/ l
依赖关系:# f. S u0 B% s+ C. t: }/ t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( ~4 @8 d4 x, h6 B0 J4 ?# i; c
0 Q# u" W5 Z: L+ \! k2 C; _: Eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' J8 u" U- M' ~& g2 I- e0 n5 amodsecurty依赖的包:pcre httpd-devel libxml2 apr
. f5 A) {9 t4 F# p3 y3 n) M1 k6 g V+ g0 e
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' x1 r0 o; h% o二.启用standalone模块并编译
& v0 ]; Q+ x8 m! o3 I4 ?
7 P3 r% C) e" Q6 [" N; Y下载modsecurity for nginx 解压,进入解压后目录执行:
- p1 n" k% a$ m6 i2 I1 @4 u! U Z
./autogen.sh( ?8 m7 r! J4 P/ Y$ ]6 k' e% x4 K
./configure --enable-standalone-module --disable-mlogc' P9 ~( L; s9 K! t+ S+ o
make 2 D! J* \: A& r+ @6 y; A. O5 n
三.nginx添加modsecurity模块0 g( V ?4 @- i! W/ n8 l, i
* [. u/ l0 a; ~ g/ P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. v2 _$ h: }* I9 N, s: |+ o
) [7 U. H" y5 _! f# P* r./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine; j. }8 w9 w! `' h- Q' e
make && make install
( {; v8 v A) Q$ X( k5 r四.添加规则. t9 |8 ?/ z9 @/ H# h
5 q0 g( M; [5 w$ I2 W$ hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# U( @# F/ k2 ^5 g) C( N3 f$ p% P' R! C8 N
1.下载OWASP规则:5 O2 x6 Y; a2 k9 ^( u, x2 T0 `# y7 [
; R Y$ `8 m5 f' |# }3 t8 O5 H' G
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) Y. U4 z9 m6 m% s; n
7 W7 _$ G) s: imv owasp-modsecurity-crs /opt/tengine/conf/
, T+ U2 g- n' D* y7 \! p( A7 W
# X D- A5 c5 h) S/ k$ b# s7 fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 A/ j0 ~% i3 S5 K' l- |2.启用OWASP规则:
( x* j- T5 ]5 h; g* x: W# ?
2 j6 `5 o7 Z& r, l: V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 t' z" n1 a& D3 a6 R) k: D
m9 h' O: |/ P4 g, q! P编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( O2 } g2 [* g9 i, A( U1 _ b
S+ ?" j+ R* K/ s. powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 v" W! h0 Q: T: d2 d
+ c+ a2 G8 U1 Z3 P6 IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ l- q# N; @9 f0 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 }* k' v) c6 r! ^; mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ i/ W( ?: x' p, L2 x, H' x( mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& F, Y0 H. {: u& p/ C K1 T1 Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! }" Z5 }& n8 k% @5 s0 ]: Z( GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 m5 F- }. K- p* b0 DInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ p- e7 ?! H& ?" I- E0 S五.配置nginx
7 u2 j8 Z5 J. ?+ _! p. X* ]) a: U4 C0 d2 A; H: ^
在需要启用modsecurity的主机的location下面加入下面两行即可:! T, F3 j6 h- q* ~ W9 s
) {/ J( ?/ K( A6 rModSecurityEnabled on;
9 v9 F& q5 z$ l4 J EModSecurityConfig modsecurity.conf;3 V4 X0 q; N5 ]' ^
下面是两个示例配置,php虚拟主机:
* b$ l. e A2 i/ i8 u: T+ f9 D: A7 j% x2 l' e
server {
- d) t+ o! R1 h |9 A listen 80;+ _3 f! e' [& }5 f# f9 E( _
server_name 52os.net www.52os.net;
6 ? I- _! f: E0 w + A& ]% W& q" F4 S
location ~ \.php$ {( l; S! X& R; ^8 R
ModSecurityEnabled on; 2 S. d8 c- N; ~, q
ModSecurityConfig modsecurity.conf;1 r5 F5 x/ ]; j; P
9 W$ _& U* U+ e root /web/wordpress;9 E7 s7 I9 }5 T! P
index index.php index.html index.htm;1 [) c m% l, } Q
/ S9 J4 H' f0 A8 r; S
fastcgi_pass 127.0.0.1:9000;
$ }9 R" {6 c8 V' V0 w fastcgi_index index.php;
' N. H. \- g- r2 ?4 `. S8 C# K, I! i fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;- Y# R- {' s3 m, q1 _. e
include fastcgi_params;
- U0 K6 c' ]4 D/ H7 k# z }9 c- t k8 W- p# Q( _# H _) o: a) C
}" J( w8 q; X! [6 ]1 L2 \
upstream负载均衡:; L1 p7 D: h* s$ e! }* p- O
% G0 u+ K( ^( i# |0 s+ u1 Nupstream 52os.net {' G/ W! t- N" N& C7 R e
server 192.168.1.100:8080;# y( O" c( S# O, c: X2 y
server 192.168.1.101:8080 backup;
* k- G4 r8 R" Z% _! `. F5 A- D3 z}' ~1 h% B) a6 G) X+ H' U
! d) A0 e( c9 ?2 q# \+ aserver {) V2 y0 a5 [. D& B
listen 80;, n5 g+ j. I0 P# o2 M/ {0 @
server_name 52os.net www.52os.net;, R, R. t1 y$ O
( V; a% k! v+ _- jlocation / {
$ y5 N9 t3 R, x3 c$ J0 a5 j ModSecurityEnabled on; s( l: X) j9 g4 g& W
ModSecurityConfig modsecurity.conf;
: ~, ]+ a, m: }" L+ R; V/ @( F, M8 z8 i2 r, f
proxy_pass http://online;
, @) m9 s- H- C9 z7 l5 r& x6 [6 M proxy_redirect off;/ V6 ?) h d% q- l, N
proxy_set_header Host $host;: z0 I. f7 [, Q. @
proxy_set_header X-Real-IP $remote_addr;
" l F3 I5 U! n& T proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ R- Z2 a. K& w& u& G2 U6 l, e) K
}5 W7 F- q6 F! l" u) D% J+ q
}
X% c+ K& p; ~6 K, A8 g% e8 {2 {六.测试+ T' N1 \* F( p p- {. p" X9 Y
, Y2 @. Q8 ^* _7 i3 n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 Y& R& ]. S+ ~4 T' e% C1 \1 u1 v( n1 Y. c F- ]( z: C
<?php) s5 I0 W0 d2 ?$ Y" n9 b( ~
phpinfo(); 0 [* ~: X2 X* E! D5 Y
?>
+ @/ \7 A% I6 g7 I y在浏览器中访问:; Q$ U! P9 V" S, T0 g3 @$ Y9 T
0 T' s8 `0 Z1 q0 Ohttp://www.52os.net/phpinfo.php?id=1 正常显示。, a* N X% {; S% g; G2 l
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
9 n0 L" E) S3 s/ _5 \5 k) |& `& shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! U9 J: u! j7 e. |- [* ]" D0 t
说明sql注入和xss已经被过滤了7 {1 R5 F* j4 e
8 k- f! v0 n- H4 u: k1 i七、安装过程中排错2 l. \3 b9 Z4 T& n" {
% P; ~9 l: i( m
1.缺少APXS会报错' q& o0 a8 b2 M) v" c; c- \' ]
+ ~. m* g z: j! a! t7 }' p( t
configure: looking for Apache module support via DSO through APXS. c) G: O/ {% b- l: }1 a
configure: error: couldn't find APXS& T4 K9 O$ S3 \# Z/ B6 e. }, ?
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" [/ V4 L6 b4 q) c7 x+ U解决方法:" j) d- r) E# N5 G4 x8 r
% K( d6 ~# g6 o# i: vyum install httpd-devel! D9 ?9 [: H! b3 c
2.没有pcre; `2 B" O) R0 T) X& ?, `
+ u* P2 L$ f" ~8 x. x' B7 |
configure: *** pcre library not found.
( @2 N! ?0 q# ]$ V8 X4 rconfigure: error: pcre library is required
# b3 H* d* f( g% q解决方法:
+ i! n( E. `/ K! _! T5 n: u3 w, L, Q+ c6 }1 C
yum install pcre pcre-devel8 C* Z" b1 k" }
3.没有libxml2/ r& ^1 L1 v+ ^0 l }; _9 o
& [. V! F9 ]1 T2 v# b- M. i
0 _. q0 f* x5 C, Cconfigure: *** xml library not found.
0 I1 d: r* e' P! r& P. b2 \configure: error: libxml2 is required
3 E* A! `6 j- t" c' |解决方法:5 B0 a% P) a `! u u3 s
8 _ P2 Y3 B# J: s- k. r' Fyum install libxml2 libxml2-devel& B& l* a0 {4 ^/ N9 E
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ s) c; O& y& ]7 s ~
9 X; m7 P$ T. `) c- Y* M3 o5 J& hTengine version: Tengine/2.1.0 (nginx/1.6.2)- d# p! \# `( I. L: e, R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 `4 x5 [. s( E- b# |
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- \ L& r) ` {
3 |& C9 X$ m* R$ X" ]0 r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# i7 r% L# V$ L* a7 G- F1 h8 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 v$ k, o, N* J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; g. f& r, Z- X6 o! D! X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 i- ?' R4 p' h% a7 p; t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 j8 M" `, D# S' ^3 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, o! s0 H" U) G) F解决方法,移除低版本的APR (1.3.9)
3 z; Y% x* K6 c& _% {8 f& L6 t
8 u. ^' o1 g. q% H+ oyum remove apr
+ a* K$ M1 t* c9 q5.Error.log中有: Audit log: Failed to lock global mutex
) ?5 `+ U: t( O0 p* H, r8 T9 P: @( W6 j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock # K( U% I3 [5 P; D% k' t9 c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 }* W2 _7 z& V1 l6 v
解决方法:) {' w5 {% y* y2 e5 h$ y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ c# j9 b$ m' E7 h! G9 S" |4 J" C# n; \
SecAuditLogDirMode 0777) d* a# F& K: r5 |" L. S) L. y" S& N, h
SecAuditLogFileMode 0550
8 x* u4 W; z$ o2 A0 L5 oSecAuditLogStorageDir /var/log/modsecurity
2 e9 r2 i3 Z! D. {" rSecAuditLogType Concurrent& F* m H+ T% n
参考文章:- F, Q% d& o$ E4 Y% Z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: a# V: R: ]: ~0 W% m' Q5 U4 y3 ehttp://drops.wooyun.org/tips/2614 |
|