|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" ?" D' M- m* i5 }4 i2 L. ?
5 W( y6 c4 o8 D) O9 E7 F4 t一.准备工作3 k5 H0 M9 R+ \" N. o5 u! t
. {* |' N% X) @) f
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# ~8 m8 V5 K6 b
3 i. r5 }4 S/ `2 M7 _ H4 g
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 u N- y2 O" Y+ I+ O4 u7 w/ i
; b5 ]( N7 Z% v8 F2 I! D4 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 V8 b E9 ~; k2 ^! q9 E, W
; ^/ g2 N9 N& }5 D* a* h) z) \OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ O' d1 B6 q& x( Q( e, ^
4 _% d7 z* @, j$ K依赖关系:# J) `) y! i6 _- r9 H$ R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ {7 P3 x9 f# n8 p& M' q* s* o& G
6 M( A0 U0 s% G, g2 }yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel1 m7 g: R4 Q& x& Y4 l+ Z0 u! F
modsecurty依赖的包:pcre httpd-devel libxml2 apr( b1 ~# K9 ~! }& ]! C9 B9 x
# a9 Q3 h0 W! T3 D& lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel: T' ]* z) X0 S2 c
二.启用standalone模块并编译+ D7 o; q5 [8 g( o' F k
8 Q1 H1 e( `/ A0 R: ^# p, |. F7 t# F
下载modsecurity for nginx 解压,进入解压后目录执行:
) n' T- \% O6 U$ H5 z' y" `' a
# [2 \( U- M) q+ W5 v./autogen.sh2 H4 ?3 q# b, o
./configure --enable-standalone-module --disable-mlogc" p- P) C" u+ y) f6 H4 j+ t- A
make % _, _: I1 y6 s2 K
三.nginx添加modsecurity模块
( A* }* N: s, [/ F/ X" ^* ] l3 R% y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% |5 J0 N- {# u0 I. P- S2 f, k1 `: q1 O
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 R' x/ Q) C: p* I$ r. h I4 ~
make && make install; z) i9 b- n# [9 Z- X' V0 R( z
四.添加规则
: Z, [! ?* B+ V5 d8 g4 \/ W+ ^4 E+ G8 ` I
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 ]4 z# a$ T, p: }$ W e" E% Y' Q
: }5 t" j8 `6 m+ H1.下载OWASP规则:9 H: l( D3 p' ]* ^
2 |. _ y& j5 M9 z: ?2 s
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 r1 {- c9 a/ d3 ~$ j: x. P
3 F3 J! v; Z) g! N- W
mv owasp-modsecurity-crs /opt/tengine/conf/
2 |; \" }; o! P( m8 }% g3 M2 Y; r! h/ k% W7 ]/ b# {
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 ?2 a0 a2 c4 `
2.启用OWASP规则:
5 a! q6 ]7 X8 H, F, ~
7 V* ~6 `/ b7 Y: n复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ f9 C/ m1 c9 Z' y& W H0 ^: x |# O3 g1 C, W8 W
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- q7 g* x) z, W6 E
5 a& F. A8 Y4 Y" F K8 T2 Y: Towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) D0 K" c$ p( y- _
( E: ]8 `& ?& d, Y$ n
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ r! P: l* e+ m- b! M% S+ |& v0 x4 `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. f+ ?5 \ X" m8 }' Q# x0 n5 eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 l! E" F* O+ r7 G; G% W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 z! `1 Y4 Y7 R9 p" {/ ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 A! J ]- t2 n9 N* p. `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" n. k- r+ S$ {( k5 n: O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- [* u/ e" P; |
五.配置nginx
; `" h! U- p" B& o$ ]' q9 K( x7 I# L: g: j3 E( y
在需要启用modsecurity的主机的location下面加入下面两行即可:1 k& g7 U% ]1 r: Q- D; u- g) E4 c* }
4 I& a5 G/ ~- `3 m" x% yModSecurityEnabled on;
& J6 _, P& \0 p2 G1 }' DModSecurityConfig modsecurity.conf;
: [! }) f/ N& T) F下面是两个示例配置,php虚拟主机:- h' n7 C: w8 [# H4 }
, F0 E' |1 z$ {- Y+ P
server {8 o; F0 ]/ n3 S9 _/ g
listen 80;
2 X, o; q) t+ B5 x' k* q server_name 52os.net www.52os.net;# b5 b& g5 s! H/ f: K
4 F+ J$ ]" ^2 A3 S" [/ \( _ location ~ \.php$ {( n- N' R# X' w, S1 o0 ^0 ]2 ?
ModSecurityEnabled on;
" i3 s4 _. n; r; U, ] ModSecurityConfig modsecurity.conf;" U- R' i* c, Y/ M4 d/ o" K6 M
$ X5 `, B/ @7 l9 g0 _ root /web/wordpress;( E4 N3 @( |9 f8 d* t
index index.php index.html index.htm;
; O! a$ d& G8 P5 T" _4 U0 A2 q
* @7 F5 f1 Q! m8 p fastcgi_pass 127.0.0.1:9000;
7 f. I3 C* g( C% _, t: G* F fastcgi_index index.php;
5 b9 k& \3 v6 ] fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( l: o g( c( D( u; A: D, s
include fastcgi_params; i* m' u2 y1 Z; E* o* x. z
}
, A2 i$ |' Y% H' A g! {1 F }7 {1 f# {. r3 w. w
upstream负载均衡:
3 r9 |" ?, I& _9 w* l) t( V7 m1 P& q3 ^& Z( `
upstream 52os.net {) ]1 b: A# \" u" t+ N
server 192.168.1.100:8080;
) D* S# G3 w+ K' s server 192.168.1.101:8080 backup;; s$ P6 g8 b, R+ r( _3 @7 y$ O
}
7 a2 M. [4 T( H; x
( ]+ O0 E: \# O% t! B4 fserver {# o& A$ ?8 O8 l1 c& a
listen 80;4 I/ ~8 D1 p$ z4 }! Y# i2 H: g. u7 c
server_name 52os.net www.52os.net;+ @1 w, h( P3 a+ P. A
1 n- x" `8 r+ {( p& c2 g4 K S+ G: b5 c
location / {& P5 D) Z; s3 p1 V) C; x& p% X& |
ModSecurityEnabled on; 0 q1 W' A% n: n' |( d4 m. U
ModSecurityConfig modsecurity.conf; 6 P; Z) R8 T2 o, D7 n' Z
/ A6 @7 y- i. u" ~
proxy_pass http://online;
& m) T- C9 }! ~3 n' U proxy_redirect off;
' P! c* V# q# |3 m& D; ` proxy_set_header Host $host;
& [% X. A: I1 V" F; I proxy_set_header X-Real-IP $remote_addr;0 X N( B. ]/ o0 o4 J% }
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 M5 |( |$ q9 @5 C1 c1 h, h h
}9 W% R( ^! g6 n3 P2 u4 |1 B3 V' F
}0 p* H% ?1 Z! ^, D+ k4 Z+ k2 {" B
六.测试
& M. Q! E8 r% P. _9 x8 ]+ h* |% {2 E c4 N- t
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; {$ t, \0 E8 a
1 u/ J" x6 N$ n$ j* v/ _<?php
* b" Z3 G* y, A/ k phpinfo();
5 b' u/ V3 |1 s. I( h6 {- \?>, d! X; F" o/ G; Z3 H
在浏览器中访问:
- b; N9 O) I9 u! t$ Z- V
; x4 _; V' i% t) ^- ]1 E( l: j# Phttp://www.52os.net/phpinfo.php?id=1 正常显示。& _7 S- }) _" ]0 P- g, w# t" {
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。0 d7 c( D/ n: @# F1 a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( v2 O+ v9 W# r1 Q+ M1 J
说明sql注入和xss已经被过滤了
( i! q$ t- r9 `! o
& n+ |: I% }5 a- }" [( g七、安装过程中排错, a/ s6 Z0 t& B7 g
5 X% C) O, M1 ^4 w; }1.缺少APXS会报错/ v1 d5 m3 X9 U6 L$ s% x0 ^4 B
5 h# k% {9 ?* c8 ^2 g! H' d9 {# A/ v
configure: looking for Apache module support via DSO through APXS+ Q3 @* N3 ]$ j: ?% M
configure: error: couldn't find APXS
% c2 x- E4 a/ r5 m7 [* wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 h5 y5 `; h2 T! ]0 h8 R) H
解决方法:9 }4 G* A/ P; ?# f7 f# R
; e9 E( k L# T' l
yum install httpd-devel
+ s+ p6 c9 V1 H" X* N0 N& u2.没有pcre
4 i8 d8 g7 i( {8 f2 M( X( {3 H- B4 \! M9 E4 m
configure: *** pcre library not found.
& Z" I7 f8 _4 G( a3 |$ wconfigure: error: pcre library is required1 G$ ?3 u8 a" |9 \6 q0 C
解决方法:2 k8 X B$ d/ P2 m$ A
2 n* w& s; G, F" ^4 H- R! L/ q$ c
yum install pcre pcre-devel
% c$ F1 h) A# d( R2 e2 d5 o+ m% k3.没有libxml2
! Q* Y9 u" X: `1 m" D7 h3 H# x. ]* r3 v h: q% x( s8 Q( Q) S: ?
! ]( L0 ~; x" z' L. ]$ @6 A
configure: *** xml library not found.6 g7 `' A! t; @% h9 J) M2 \
configure: error: libxml2 is required+ h+ j6 Z1 T! b- b @, F
解决方法:4 h+ ~ @1 }) z! O3 f1 Y
, u9 P# ~0 D+ P5 s4 I) l# u, m* j
yum install libxml2 libxml2-devel
# d% }5 B# u) d p4.执行 /opt/tengine/sbin/nginx -m 时有警告* G" u$ w2 P+ ^" z# m2 J
2 O* @. c$ |1 H R( [& J0 k. Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' r' F, l! O; {" w x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 w- v5 {$ W: Z# a1 g2 c% I( m% a! v原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- \/ \ e# e. a4 P3 y) S; F0 I/ a. M& C# B( Z! z: p: p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ \* W; o& b) Z& @ S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". Q* d9 n8 ?. o# s8 C& ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) G; U& E/ L$ [+ w) F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 S( t+ s& q. |& O9 W7 L0 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' e, G! v( x z& t9 @( m3 n1 J, b
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' ^' O9 O3 W' \* f x) k. X
解决方法,移除低版本的APR (1.3.9)# j, y; O7 d% S# [" F' h4 _
0 v& w5 |1 o; G2 ]
yum remove apr
) v, m* |1 ^( D, A# y, i/ z5.Error.log中有: Audit log: Failed to lock global mutex
% {2 z$ B" N# @5 q9 q$ C# j: Q! ]% y* m" A* t/ f0 F
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, \. p6 G8 e6 N+ S& _7 K- Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 Q }& Z5 q" M- G. D: b, m解决方法:
7 f7 m/ `5 l9 h6 J9 R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" @+ R0 d P: Y$ x9 D) y5 R$ H5 J A
! O/ G0 ]$ T$ q K! P& l* TSecAuditLogDirMode 0777
0 x0 F8 ` ^3 f) pSecAuditLogFileMode 0550" `5 B% W c2 G9 d B6 M
SecAuditLogStorageDir /var/log/modsecurity9 F; q# B; r* Q9 o, }" V, @
SecAuditLogType Concurrent% _4 o3 Q1 ?! d; s
参考文章:4 |7 C2 y9 Y3 n j" X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. I3 G1 v; r7 m+ Uhttp://drops.wooyun.org/tips/2614 |
|