|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 z7 Q- z* v2 T
, I1 \8 I( e. v8 R. ]6 e
一.准备工作
( k, V2 V* a# h$ }' N* }1 v1 f
5 ]) p6 w2 L; K( M: _, `# j系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* }7 g+ U' M0 M r( [5 o5 t2 [. M7 s+ E5 h3 F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, G2 \. r6 p+ b- j) d# \5 W
( @6 K" B' e, ?" O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, l1 r- V, d2 Q% D
6 A) a& |3 W2 a$ G* ^/ @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 e8 D$ e6 c8 U9 {7 W/ ~+ R( G2 h
" r7 x. p. W) L3 b. l依赖关系:3 g' i0 Q. Q. G- c( U3 R7 B7 b
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" s6 B2 o9 D9 c( F
. o. E: T3 V, a8 o
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
+ }# `$ f# U5 zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( d; ?- j7 o6 L
5 I' W8 N/ h9 B( h; ]yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
}, Z/ g3 P! P7 O& }' l二.启用standalone模块并编译" M) u) J% s! r; |0 A2 ~, ]" B
6 j; E6 L9 [# L; I" y, _
下载modsecurity for nginx 解压,进入解压后目录执行:- M# W+ T& @# e
" B9 d+ x- ?" D
./autogen.sh
9 s+ u$ p# V* m+ }. m! ]./configure --enable-standalone-module --disable-mlogc
- s4 q h2 r# N5 d+ Zmake
" z( i2 n9 K9 ^$ ?/ C三.nginx添加modsecurity模块
" m+ U. W8 C4 R' \+ V" r, e- d7 L" V, y" b4 q3 g6 s3 E
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; o7 r" F5 N- o& M" M( S! O( P0 W- S; y* G! q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
; |2 a4 s6 D! Z, j1 p' jmake && make install
# Q' D3 L+ h9 d2 r: |四.添加规则
. q) H* |! Z. p( f# A1 q
+ f: L& z2 T2 m& ?/ d- hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 z. k- ~' _1 i' [0 J( C1 d
0 Q7 o7 c& d% W) v1.下载OWASP规则:) V7 @% R- v& P" k
) Z' O; K R2 Y2 K, ~, ]% K
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
% d1 W' a! A. @9 f" ]$ S+ s8 R8 S% o0 c' O
mv owasp-modsecurity-crs /opt/tengine/conf/
; b, x1 M. E5 F: v& m# t; F) f( W( C) [& ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 t, F2 ^/ R& B- p, U. A
2.启用OWASP规则:
8 _$ x# k2 \9 Q1 X5 m3 k9 V# @5 R' l% h1 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& g. @5 r# U6 D9 M1 Q6 F' ~8 z. R6 c+ J% _* \
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* ^8 ^ F. y6 g( b3 q
& G3 l% |- d4 E% T& I8 m- m5 n. kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% h; c# }* Y6 J9 E2 N7 ]/ ]
; o# ?! T; M0 P0 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' a- ^( P. C- `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' _7 D3 w, I j$ I+ W; Q. L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% k1 W1 B% l+ ~/ ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 q5 N# x: X9 o! ?' nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, z1 R8 \/ @5 D5 C$ ?- d$ `; K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& {/ G7 E1 Z" p: P6 M; n3 RInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ Z3 H* ?) r6 E0 g
五.配置nginx
% F2 X3 H; q" I$ ~! f& _9 O8 w' y/ u
- }+ ]+ D8 V" v0 [# Q3 j7 o在需要启用modsecurity的主机的location下面加入下面两行即可:
& S. q% P- `" a6 @5 H/ m/ q) Z5 C
ModSecurityEnabled on; 8 K* K* R2 {7 @# j; e
ModSecurityConfig modsecurity.conf;
S- A" n1 Q) u2 p下面是两个示例配置,php虚拟主机:' f, Q( u- [ H9 E9 Y0 Y6 F6 J
( d0 E- [1 A8 y# K
server {0 M2 `3 o4 M/ I9 \* v3 _( u
listen 80;8 _0 j/ W2 ]" m" K% i) w8 V+ f( F. _2 q
server_name 52os.net www.52os.net;! c0 W) K7 @3 J2 K& D5 O$ a. t
* J( d9 k" L# {2 w
location ~ \.php$ {
5 G3 F& X2 Z$ @. U' { y ModSecurityEnabled on;
6 B, @: L9 L6 N# m ModSecurityConfig modsecurity.conf;4 L: i+ J9 _1 f$ b; z/ \' y
5 t/ Z4 ~ x+ i root /web/wordpress;
: ^) m1 @3 @/ j0 U W index index.php index.html index.htm;
( h' g, @( s& W. [2 V) `- m ( G: q! e) g" k# Z" p" Q
fastcgi_pass 127.0.0.1:9000;5 s$ Z5 R( C6 P2 t2 E
fastcgi_index index.php;4 H% d" P$ w; K5 o9 t$ ^
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, y( f8 u$ X' k2 k( Z) n9 a5 ~ include fastcgi_params;3 A5 ~: a' }- B w' }" x7 S
}
" x4 G7 A" ^9 Y5 Y }
, H) h" s& u3 j& xupstream负载均衡:
& e+ \. ~' B S" I" g+ T2 M9 H) `8 w* ~. s3 \2 k
upstream 52os.net {1 p- U7 Y9 k% o; L
server 192.168.1.100:8080;
- O z/ ?" g, v/ S" W server 192.168.1.101:8080 backup;
5 X) t) A$ V% J6 V6 f9 p, H8 O}
s5 U# i6 x1 @, C' `8 a
1 \; O% p4 w$ W3 b, d& Fserver {
# Y7 r4 X0 e& Y& j- f4 m% clisten 80;
& w4 k9 n1 q$ f }server_name 52os.net www.52os.net;
; ~/ e( a4 X1 |0 Q5 R) N# c
9 Q! _+ k0 S( P( ^* A2 k7 A3 ^location / {& |0 j I9 s( n; D& }
ModSecurityEnabled on; : i) b5 u! N7 O9 E; E
ModSecurityConfig modsecurity.conf;
$ n9 u7 ^: o1 t/ G( Z6 r
9 Z3 w9 L3 K. r0 e; E proxy_pass http://online;
! F) W! Z- @5 ^ l/ L4 ?$ S$ S proxy_redirect off;
% P( R' Z+ u6 _+ U% ?; A- _! `7 W8 j proxy_set_header Host $host;
6 p4 v$ i4 W# S proxy_set_header X-Real-IP $remote_addr;& V- ]: d$ g1 F' u
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; O. w/ h2 v" ^$ K) G% ]
}
" R. x! h3 @- v. X! {" g! Q}
) `9 ^9 V# I6 x m六.测试& z0 }% i! U, p+ P% j" w& Z6 n U. N, a
2 ~: n7 _$ ^4 T# `9 {8 t
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 {, a# H! c1 Y
q* [1 }0 c4 E<?php) ]4 k, o. o4 ?
phpinfo();
# }5 H2 a- \* J$ L?>6 h/ e9 D! B' w! `3 B
在浏览器中访问:
4 R& w* N! C( D. [& R" ~# Q, Y* K8 T1 Z; {; [
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 Q9 C. L i( J# Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 l! k4 ]4 }! ]. l3 ~# W: \
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( t2 X5 u" ]1 _1 x! e1 @说明sql注入和xss已经被过滤了' ~7 D+ C0 A+ Q# y
+ J; x7 h! \' Z七、安装过程中排错
8 D! O* u; u: |3 m( x$ |) I* \5 k% k X' k& W! V/ O
1.缺少APXS会报错
7 O' k" s6 x( ?
+ m' q$ T* y" m( M& V/ u. @configure: looking for Apache module support via DSO through APXS9 y/ A" ^" D. P% V5 z2 ?
configure: error: couldn't find APXS
3 \) C* `' ^( Z& n/ _% Wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ D! ` X" v9 W) Z6 R& I; m解决方法:
* W9 g$ L; L) c3 m3 O
* ?4 {% t. q$ Y: R% fyum install httpd-devel
0 j9 ~' ?1 B6 K0 Z7 C) J& k2.没有pcre; {: ]. G7 O* \7 y/ T
t; R" _* c4 h" a @configure: *** pcre library not found., J* g' g- Y% w7 ^8 ]" a2 a
configure: error: pcre library is required" Z8 f6 `' G1 X$ ]
解决方法:+ {5 h2 o! M: V$ O7 w
4 r ^% L3 y5 m, W. \yum install pcre pcre-devel
- E) E1 k9 ^ C. o: }. {: W8 W- U3.没有libxml2/ x) N, D( a- a8 I7 k
! S% V/ {/ I" o9 p- [' i- F( |; i" F4 h W+ [0 N
configure: *** xml library not found.3 {" b! q- [; [0 K
configure: error: libxml2 is required7 ?2 M, ^" G* }4 ^4 T m: d" o
解决方法:
$ j [- y* q. ]( I7 O5 Y! Z7 n; r& z- v
yum install libxml2 libxml2-devel
/ F1 i8 ?, }: L l4 w( b3 U0 l4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 t& G: j8 f# w8 S
5 m5 A0 z4 ~, F; X6 RTengine version: Tengine/2.1.0 (nginx/1.6.2)5 i3 C# h( a, O+ e2 }
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" R1 L) j2 y0 |9 H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 \" q' Y9 L; x" i N
* f# {) F3 Q" _+ a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( Z! r C3 g! ?6 w: r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
- M! [6 M7 K) k2 _% E2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ k/ t- e! g/ p& ^( J$ d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 ~, L2 {- y" H4 T+ d. p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ T& q6 e; U! U: m
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: X9 E P0 M7 P8 Y$ @ @# K解决方法,移除低版本的APR (1.3.9)
, _$ [5 h* y7 G- I0 Y+ w7 @0 p' h/ s5 @+ j" f
yum remove apr
E F( \9 f: @6 m5.Error.log中有: Audit log: Failed to lock global mutex
4 n2 T$ r( R$ ~& k: L$ S/ K. v( X1 S; P
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 _1 v+ \$ e& h" {4 A& qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 M; T, V* h! d. P# W7 c9 J H解决方法:4 g. {4 x; [- P8 c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 F# B8 E- Q# @5 \* t" s: V/ @/ X5 A8 I
SecAuditLogDirMode 07778 a) ~# m' m. t) X* l# _4 j6 b) q, e o
SecAuditLogFileMode 0550
2 S8 s; i9 }6 BSecAuditLogStorageDir /var/log/modsecurity
" T! d; \6 {8 Q1 X7 m' q9 G) sSecAuditLogType Concurrent. I9 ^5 z( ^) R( b1 G
参考文章:6 v y9 l x5 ?" C$ p
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( C- B& S% x+ {http://drops.wooyun.org/tips/2614 |
|