|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ x a, ]3 y6 {! v9 }4 ?1 g- n; h- R3 H8 ]
一.准备工作3 s6 H" S5 Y# l; |0 T0 e- q0 E0 F
# _; D- f8 t( @; a- u( G \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ P, j, R, [: [. O) A8 Q% A- I# L6 O8 {
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 o. p% p; P" x! C/ e) m8 X
4 u$ I) G6 `8 a( ~7 X2 q6 V3 t' |9 ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# Q/ Q- a3 s5 x4 x& b! Y1 Z
3 g. l, {' H$ m$ ?% h3 f! pOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- P6 A+ s( F' d6 j: C+ Q
4 p j; a8 h; Y/ O% A$ m7 }
依赖关系:
1 p& B7 E8 n6 Q) G0 ` otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% j/ ~5 m) j) i" | o
- y9 s; P+ B. T6 u( N i/ E8 b9 ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 V: L3 y5 _0 z
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# b) j- `$ ]; v: b. b8 @/ Y; S8 N( C; b; ]
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 w- `' ^% I& p+ P二.启用standalone模块并编译5 t% B' N# }# M0 K5 @
. G- q0 G6 ~* C下载modsecurity for nginx 解压,进入解压后目录执行:
! `" j2 D V( `% Z6 L3 Q
" _: A5 y0 s( U, z0 I./autogen.sh4 Y: D% P2 L/ m( O0 H1 G
./configure --enable-standalone-module --disable-mlogc
+ |# J- H5 q# Xmake
6 p% z% n. w3 G3 T: m% e& V' g三.nginx添加modsecurity模块# L7 I# y$ a3 R' F9 q' R3 K
& O, Z. D3 G1 [3 R在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ ^. j4 l7 Y, S, j- N& v0 I9 u
' h Z( y' S. w$ I9 l: ?. C* T5 @) D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 L9 E, R+ L/ s
make && make install
% [9 j3 d. }( s6 q- A7 X" Z四.添加规则
( S; W# ^: M1 z+ }
$ K8 a) M* E' Y5 _: V/ s3 w2 E. rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 u, I% x( {5 l) k% }( T& K
0 s/ R, o: S5 M& y/ x1 V* ]3 L' E4 H' W
1.下载OWASP规则:* r- J' x, E/ A& x- g
& g; U' O% R- R( H7 f/ n/ C5 J
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 U! l! u6 E- m; S
( A5 M$ [2 ?5 b7 X
mv owasp-modsecurity-crs /opt/tengine/conf/" O O9 q4 ~( o
( O+ Q6 E; a1 v5 L0 o
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" t7 O0 U% H+ @& ~4 Z8 r
2.启用OWASP规则:
, r1 d; z9 d; l. R2 x& k+ b
. j- E2 G7 G# j- ?+ N& A* @复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 h$ c9 J) R2 K1 }3 Q- O' D9 j) d5 }# z: {( X1 r9 j) @( ]
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 V6 h0 f0 X1 f; O; g% [- K, d" }% ?, s( k8 C. ?+ C& O( D
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! p7 O' ~9 p9 n/ ^- G0 m
; U: X: S( k% w0 w8 w' hInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! ^$ j3 i- m/ N8 y$ FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 J Q8 `$ n! j/ ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- ~' p: ?- c, x& ?, |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& i; A# x4 g0 r! |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ |& w! V$ a5 x- R l' ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ o5 S- v8 f( o+ fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: Y& K! k, N; Y
五.配置nginx
% C) C; b' k5 R. t2 y/ [6 q) D
# `+ ~4 m$ N8 n) \. h! a& L在需要启用modsecurity的主机的location下面加入下面两行即可:
* J3 e _( T! o l7 P K2 }1 H4 x8 L' g& Q
ModSecurityEnabled on; ~% i: k% |" ~# m$ Z
ModSecurityConfig modsecurity.conf;5 c) Q: W. j' n+ K
下面是两个示例配置,php虚拟主机:
" ~+ T9 _6 n7 ^, ~9 i, z0 b* Q4 l3 m
server {7 n% o( m% n# k3 u
listen 80;& z# q, o4 ?; [( v% B7 b. a. [ ?
server_name 52os.net www.52os.net;
0 r+ s! L4 i9 P- e0 P - y4 O$ \: e* k- `
location ~ \.php$ {5 z) l, j- S/ ]; R) c
ModSecurityEnabled on;
/ e1 U) q [% k; J: S& R ModSecurityConfig modsecurity.conf;$ p+ {* ^9 _* k
8 @2 }0 l# ^# ~& Y) w2 [ root /web/wordpress;
/ J' v) I# t9 z4 ?3 y; u' b index index.php index.html index.htm;+ X' J5 n; L" U# ?5 g: Y' F
2 M5 T! K: E+ v0 V6 e
fastcgi_pass 127.0.0.1:9000;
: {9 K. H% ?. {4 v2 k fastcgi_index index.php;- p9 v& n3 A4 k0 b8 x H0 Y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 f7 a V, `) v8 _ include fastcgi_params;9 h2 P, L+ R/ L! N' e
}
v& ]0 y: T0 }& v: h& i. d# ^$ q. K }
) x* l7 |1 r, X5 ]& @upstream负载均衡:
" n' a3 _) V& K6 Z- Q
7 I( Y' g$ b5 N8 Iupstream 52os.net {! ~( @0 f0 t- _7 P% j! g- }
server 192.168.1.100:8080;3 p J% I+ Z# n% E* U' ?
server 192.168.1.101:8080 backup;. w( `7 N4 Y+ W, T- \7 j6 T
}
/ h/ R8 r; B3 C! O& K" {$ G0 R" s( y+ @# N
server {/ M; b+ C |- H9 q6 o6 ?
listen 80;$ S! S5 x) a* a$ H
server_name 52os.net www.52os.net;. I. a6 o) [/ f; j; r
& E; [! K2 _4 }4 b5 u7 tlocation / {- K& @% P$ D! v9 ^" d
ModSecurityEnabled on; 8 h5 u; t' y: M& e+ W0 X
ModSecurityConfig modsecurity.conf; ( R, f" L' v2 g- u4 F
( X! O' t- J1 d/ E! ~ proxy_pass http://online;" _( U x$ q" V) Z3 @. f
proxy_redirect off;
! `/ b3 o. i7 d# A' G5 r proxy_set_header Host $host;
9 Y$ X+ {5 T; p; i2 U% P proxy_set_header X-Real-IP $remote_addr;
* n" m& c; |) Y: M' _ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
* _! l- A# A5 [0 _+ O3 G! y' Z }
* [" ^+ \% ?" ]& d6 t! e: Q}# S" a9 d% W2 H; k1 L4 u0 S
六.测试$ z$ A" r5 O1 j1 w9 Y. v% r; H
& X" Q! C+ {' @) X) s' M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 F) n7 I/ o- g0 [& `; P* A9 C5 i( S. q* Z) f/ F) c3 M
<?php; ]7 a6 y P" a# f: e& s5 w! E4 V
phpinfo();
2 Z8 l. G: \4 {/ E?>. H5 [, `" I: h9 V* T
在浏览器中访问:
& ]4 ~# S8 D2 @
+ |. L9 ] z1 c! {http://www.52os.net/phpinfo.php?id=1 正常显示。
6 G' i/ ]4 f# o2 U! d4 Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
9 N/ g/ d3 v' G1 L$ v2 Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
. u7 O& T8 U5 N5 J/ }说明sql注入和xss已经被过滤了. [; r3 h! F- `7 e: Q/ B, V( K$ a) t) S
) F7 V: z; S$ r2 v! L; ~9 ~9 H( f七、安装过程中排错2 z, \( A. f& B8 L0 g" a- W+ _, x
; \! e# f3 L+ j' r1.缺少APXS会报错0 x: w- n3 ~% Z, T4 H
/ E2 U( ~# I D9 C5 B; S
configure: looking for Apache module support via DSO through APXS0 i' `- J8 r+ ~2 v: ?
configure: error: couldn't find APXS3 E% `8 R: Q4 I6 @9 n, L
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* |; M1 N8 S% E$ h. T
解决方法:
# C: _! l) G# d) }2 F( A3 E: ?5 `5 y+ U( T% L/ O( U- n
yum install httpd-devel* b/ }. D; `: Q2 @. E0 ~% U
2.没有pcre
1 Q* F+ D2 ^' T( x4 Y; C0 P1 T
5 x. m8 B% q l9 t2 Yconfigure: *** pcre library not found.' ?7 C8 t' l5 O. K$ \2 H
configure: error: pcre library is required' G3 @% ?# v- H5 l! K5 [
解决方法:8 X. ?- ^ J- Q3 q. v
& z) M1 c9 i; L5 R8 |yum install pcre pcre-devel
: ?+ L* d6 o6 i$ ]% ^3.没有libxml2
9 V! s* ~" X w8 \ y! k: _8 \9 `; V; `7 r2 o* \
+ x4 o; m+ E/ k1 i( W) J
configure: *** xml library not found.
) F8 j/ g( z8 S: g- ^9 pconfigure: error: libxml2 is required
5 X' J; {) X9 f解决方法:( N" n5 w7 c! `% D2 t- J" E
- I) k: a+ ~; k/ k. s! D
yum install libxml2 libxml2-devel
3 [, q5 q/ t3 U' Z& Y3 W# Z4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 B2 z, v- L% h- c( f
9 a& z! s8 M. U. V* VTengine version: Tengine/2.1.0 (nginx/1.6.2)4 i/ N/ R. N( M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. V! ~ g' Z0 H, F5 r/ x原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 \( ~- k$ f! w# F3 |3 J, D n* @. [# x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 Q" S2 I% X7 \' `3 B+ R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
2 N3 x7 W7 C, c) ?$ f2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 k8 k. f5 H d9 C' ~& n+ I) X c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! @7 x7 F/ J, H8 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 P+ T% [! P. ~* o, J% G4 P* z# y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, U6 ?4 e" ?1 S解决方法,移除低版本的APR (1.3.9)/ }+ h& P) u! y2 l9 Q/ r! I
; f7 a5 p5 h" x/ Z0 r; R
yum remove apr
- k$ E( C/ F. ^4 I. W! {5.Error.log中有: Audit log: Failed to lock global mutex
7 k* R4 c* ~. }0 b# K
- d) k/ G H& c8 s1 ?- e. h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
3 Q/ h3 {) e2 j+ C2 Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: t" n, d2 y; q `2 e" E6 q
解决方法:* ]# t5 [, ]) n {+ I; `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:% H. V$ A+ g" v' W5 ?( {
6 {+ `6 ?' [; I! o0 A" `% fSecAuditLogDirMode 0777
* h( H; S6 t7 h8 vSecAuditLogFileMode 0550
8 s" |1 s* K( U/ T) bSecAuditLogStorageDir /var/log/modsecurity6 @2 t2 }$ t: t& A) ]' _
SecAuditLogType Concurrent
; s# X$ L8 b: Z" S/ v3 d- l参考文章:* v* c! |! [5 q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' P+ t# X- A0 a- I, O
http://drops.wooyun.org/tips/2614 |
|