|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 J* K1 r$ W3 |9 R0 {/ V
* e/ c7 i4 o0 g' n% k6 l0 u" o! C9 k+ @一.准备工作, M2 K( Y: F* E y
1 S0 Q, h( v9 y& r- G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 [+ y9 ~8 u4 K0 x8 q
" _3 D/ d2 C' H- f6 |
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' [" H+ P# s4 K: I% c% O! `
G! B& X# ^( N, j$ imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ @, E: `0 K$ Y
1 v% a3 }* s% v( {. OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* P' a: E/ j* W/ e; }8 n j$ ^0 o9 T" [$ v, h' [
依赖关系:
7 p: y% ]$ `: y' t# T+ \: Z5 X+ Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# B8 N# S8 c6 n. F& }+ x: Q8 V( f7 y' s2 K. s2 e1 }2 K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel8 N# W0 D8 t3 E" [- }/ p8 C
modsecurty依赖的包:pcre httpd-devel libxml2 apr& X3 @( K0 | e9 t a
' ]* }1 O: m3 T: q$ [& M+ [
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ a! r0 t) [% h; M; `
二.启用standalone模块并编译
3 E; D- s7 p F$ _) c$ N! K( L- A$ Q4 t$ c. {' g/ u+ b
下载modsecurity for nginx 解压,进入解压后目录执行:
! m# M- ]/ P: T }
; Q/ [( m3 k$ n' R- @0 K1 z./autogen.sh% H7 n" d' U. g; P. B, z1 |- r' D
./configure --enable-standalone-module --disable-mlogc) q" Q- H3 L. W& ~! {$ O
make # O" y8 E5 m- f5 ~* b* w$ W' s
三.nginx添加modsecurity模块
; _' _, o t4 X: Z" w
7 ~+ Y% }, \% ]8 Q y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: G# W8 l+ e/ G) F' A
$ H T+ J- Z9 \4 \
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- {% k6 W( B h3 B* a% b. {6 pmake && make install
8 s( ^# A8 d3 n( t# I四.添加规则
2 f( O1 p. |: I. I- J8 Q1 K% o" Q o3 [/ v. z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- s! P6 U# i' r, ^4 S% _
$ @9 M- O: n/ x5 o+ n1.下载OWASP规则:4 X4 F; u! A1 q
- L- P' l/ ~/ \ Q% u% [6 l2 Z! f
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ G# a8 r) E8 o0 s3 e2 s
L/ T4 S9 \6 Bmv owasp-modsecurity-crs /opt/tengine/conf/
* ?# R9 b* G( v6 x* z1 b
k. S+ ]8 s, C0 \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 C# [. ]+ `5 c& g4 Z G! r4 m2.启用OWASP规则:* d7 f% l i5 r; j* M# ^" M7 | G
( E Y& X! ]4 j/ B3 E/ V* y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 [0 \/ S) s+ O$ {4 R" B" a n% M
" [# i* Q- U( B编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 D! P( J {+ f5 V J! t( Q
; C- h' S, w2 w0 Q
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( y, T, X4 y. g" L0 c1 ~( ^; J B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: F( H7 G( F. a) z# J' b$ i' v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 x: l$ W, Q( O- H4 Q8 yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ o2 K. ~( z4 O7 C% I5 D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& |: R! n5 ?/ i) }. P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ P! Q7 q$ a1 u/ M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 H# w1 S" v/ xInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 \& j( F" q, [; G- F
五.配置nginx
( s" `3 ~4 M- z( U3 Y! ^) x% `; F& }% ^# r* i" D" [9 l# V% ^; s
在需要启用modsecurity的主机的location下面加入下面两行即可:
9 {2 K8 {* k V x) G5 k7 }
( g7 W+ \# K* X }ModSecurityEnabled on; , c4 ?. ?$ W* O. I% p/ b
ModSecurityConfig modsecurity.conf;
- O1 \3 q3 Q# g6 o' y下面是两个示例配置,php虚拟主机:
- ]3 f7 a) T2 i3 `3 Q. d5 ]9 d4 w
9 E& R3 x& N$ ~! v" o& Lserver {
/ `' k& N; _1 Z* Y listen 80;/ {* R' L1 Y- w6 v. i: r* L
server_name 52os.net www.52os.net;
4 @- t3 `, g0 R) y0 D! u " @- [+ W m! @! u) L2 p
location ~ \.php$ {
3 S) \8 d0 H5 |5 T/ i: l ModSecurityEnabled on;
- S# G; ^- V, a8 y, i5 {! s+ M0 N ModSecurityConfig modsecurity.conf;6 q% ]) a' @' k) N/ ~! I* l4 f
2 @; N" ]' X6 _+ \( |# J$ v
root /web/wordpress;
0 ]$ t2 e+ {0 D' A7 ` index index.php index.html index.htm;
0 I' X* Z2 b% A t I
5 m7 o7 i6 k. o4 o fastcgi_pass 127.0.0.1:9000;
( F4 F8 K; v' J ~- q, ^, H fastcgi_index index.php;. S8 x5 M6 z% ?/ z- ~1 B$ E
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;2 V* Z* H: S' z0 O
include fastcgi_params;
9 n( _% N+ [7 J# w# {* | }0 w, f Y8 ?- ^4 V" l; f+ M$ w
}& j/ L, x: _9 j3 ]- h' r3 M
upstream负载均衡:
' K9 V+ j! b4 Y1 [7 o: |% P4 A' g2 Q! U7 ^; h
upstream 52os.net {8 _( Q9 f$ {2 G/ H5 }& x: z7 s
server 192.168.1.100:8080;$ `+ w# V- u2 O" s9 C3 f
server 192.168.1.101:8080 backup;
( C# J5 d8 U% I: w% \7 d$ y}
+ [# ~* z: o& H# w
% R7 u# |/ w6 t2 s" u1 U2 w4 aserver {$ X) O; w2 n8 g, \4 f, [* T
listen 80;/ R$ G2 @$ Z1 N& R6 \
server_name 52os.net www.52os.net;. g4 `7 V9 c9 T- O
! B3 b8 O2 x4 _1 E2 C- Ulocation / {
( G' M- U/ }, d( @$ Y ModSecurityEnabled on;
$ `0 O5 U- w. }( B! N5 I ModSecurityConfig modsecurity.conf; 6 b) c6 a+ P+ z7 K4 B9 T; L* N B
8 P" }) ^5 r x/ p. K
proxy_pass http://online;
( Q3 V; L" U% S6 b: C proxy_redirect off;6 c4 Z- A% _# \2 Z, @+ \% B% |* z
proxy_set_header Host $host;$ g/ |- j! H }2 G$ G: a" ~4 s1 X
proxy_set_header X-Real-IP $remote_addr;
) u$ T0 Q+ Y8 b: ^! {4 F proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 L( o8 U" F# A
}
3 y; Z( D( p6 t: i R}4 {5 T3 H: x4 `' @9 d! U: c
六.测试% g! z2 K8 e! D( E$ Q
$ [8 X J6 J& h. v+ f- G P我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" C! g, M& X3 f
+ n8 m& M& e5 _9 B<?php* V4 M6 K9 Q' [( p
phpinfo(); 3 e; U+ O0 t5 t2 p. ?2 N
?>- l) h3 p" z' @! [
在浏览器中访问:0 @5 P9 B7 x2 o9 F0 {/ [% P
- e4 l7 R; f5 n, ~) a6 D- O
http://www.52os.net/phpinfo.php?id=1 正常显示。% Q! K6 N/ H& l, Q* J
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。% W6 D9 f- J: Y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! Q7 T c+ [1 j9 Z# ?8 W
说明sql注入和xss已经被过滤了" @8 K( Q8 A9 g9 Q% ?! P8 j5 A
; R0 r8 q q [! E) G七、安装过程中排错
$ z R6 i {! K3 W
5 u V# S( H2 b1.缺少APXS会报错
* y) M+ s d3 X* u" f/ E
# h4 ~$ f2 m6 m" fconfigure: looking for Apache module support via DSO through APXS/ m% k3 S7 h n6 ~9 k3 l. g+ O
configure: error: couldn't find APXS
1 i" j: n$ m! v1 p9 S1 ]2 G; D: oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# {" X; i, n' k$ Q3 p解决方法:
0 w! I1 ^- i' p/ Z+ _9 g. D; M' y! A3 u: G3 \5 ~
yum install httpd-devel" m2 o; F7 q- r- p4 c( r" q
2.没有pcre! o) Y! [1 H& |& R
: N9 D& ^/ ]. ?
configure: *** pcre library not found.
; r) T0 d. K. w- W3 S' sconfigure: error: pcre library is required
9 {6 {3 W6 K& `7 Z' R解决方法:
' Y J6 Z0 ~5 C: H8 O2 R9 [# z0 m1 z$ j; x0 Y
yum install pcre pcre-devel
# {( j1 t. x0 X0 N3.没有libxml2
) `. |2 ~ i/ T; S0 W
8 y1 s# I* f/ B7 D4 v
5 g/ H9 i! D2 N- e8 Hconfigure: *** xml library not found.( ?# O4 O0 J# P( z0 C; y7 A3 h8 o$ @
configure: error: libxml2 is required- N" E/ v9 H4 P3 ]; C
解决方法:8 J4 H6 o# M* n% T/ k
% Z% k" K8 c$ ~9 H" W) q6 hyum install libxml2 libxml2-devel
' B' f$ s9 J, D$ J' G+ W6 k( j4.执行 /opt/tengine/sbin/nginx -m 时有警告
) [1 J: u9 f- x4 N% j8 H/ _* {6 o1 c$ O/ l
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 o0 o. }7 E$ R7 \$ Z6 T* o$ x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 q8 x0 C$ f7 V% v
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 E" e" f6 m" c; h0 p
" S7 o( R7 `# W- t# S4 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, K" a0 k7 w( j3 j- g5 `! n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! j1 G/ ?& h3 o8 f- ? F$ j# S
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 |, R# {* j' J1 `5 e' O# C4 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- D; c2 ]. r4 r$ x; W, r5 ~, N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 ]8 a2 j. K) H/ |8 k8 z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# u" \" w5 X e% e$ U6 O解决方法,移除低版本的APR (1.3.9)
( K2 V. b' k9 d% l( h- b# k" _! E* w) A1 t" A
yum remove apr
5 T/ W V( R% V/ Z: H5.Error.log中有: Audit log: Failed to lock global mutex
+ _: \/ F4 |6 X8 K- j2 X6 r/ d0 l) z" N% U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 l' r. [' N) P, O7 T/ y. i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 p% R n- l( G0 n- c解决方法:4 R, R3 s7 h: D* _' a* U: J; e5 r
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& I: J" |6 x# M) V! g- Y* }
c! d2 I7 B) n$ ^" U+ ~SecAuditLogDirMode 07773 g+ r* f" _% W: G1 K) M7 M( T" p% a7 x
SecAuditLogFileMode 0550. O# [& _- g9 D, P! P% E3 P, c5 Z9 W
SecAuditLogStorageDir /var/log/modsecurity
6 M# K( }4 _- H% H" k$ ASecAuditLogType Concurrent
, z6 ?$ j/ h3 p1 z# r i) s, o参考文章:
" P6 g& A1 }7 Chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' R6 q9 v( T) U( ]% T; qhttp://drops.wooyun.org/tips/2614 |
|