|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) C9 ?% l" S- M! x: Z
1 f1 P% Q/ a4 f4 D/ [一.准备工作
3 r' F E! e' d7 n' I& k+ B+ ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ R( B8 o. o6 R
" ?. M+ G+ u0 Ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' M0 M4 B2 E/ A' V+ u8 ~3 M: S/ ` d" W, I4 ~7 c0 B
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; s( B9 a/ [1 [" f6 N- W. V: ~* J+ k5 v' R0 b8 s4 X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 F% z* H" P. j# k1 r
3 `; ? q. N* \5 a/ a0 A# T依赖关系:
* J- E3 R: p. I. {& Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 I. _$ r5 v9 G" l e" p9 R9 ~, M* f' q+ h8 V+ T
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 e3 \ y0 x: [3 omodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 A6 C6 l: S6 e7 |3 I$ p: {( q% f! n( z) c
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ G3 K. C+ M% u+ V) P二.启用standalone模块并编译
7 }. j8 ^! M$ }& W2 y$ b' u6 O
4 p' z ~ P! a& k: M; ^6 Q下载modsecurity for nginx 解压,进入解压后目录执行:) L0 ]7 f& _1 ?+ r2 t9 [
4 c ]# i* n0 a. F8 M, ~8 c
./autogen.sh
: ?! D3 K" F. A8 I; H/ x) a/ |' ?% [./configure --enable-standalone-module --disable-mlogc& _5 `2 k2 W* \( k C+ y' p# Z# I
make 4 _& e5 P: }' o: x+ H4 I4 P
三.nginx添加modsecurity模块
6 n( X. K6 L3 `( d3 j
" }8 M, I2 I3 t+ @% D6 a* q( e6 U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) i0 h g" f) Z" t+ x+ u$ e
$ M2 C7 a, n* O
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine: k& q0 J! i, \) a4 |: B7 T$ j; V* _
make && make install
7 ^) b# j3 b! R3 G: a四.添加规则4 T3 n1 [/ E* f6 k! [
. l( ]$ \; j D3 Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! I* c" `, B- \. ~* Z# x- Q* e2 ^0 K
1.下载OWASP规则:
1 l9 m7 o1 ]- C! s/ ?3 O+ |; C- y) l' q2 d
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 \, @0 r' Q& O) _
" r3 |( Z- Q4 l. G @; ?" jmv owasp-modsecurity-crs /opt/tengine/conf/
1 ?' i+ e' U/ W/ \# h. ^0 j9 J% j' A: B/ ^ F; m
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; S7 I0 t8 R' K% C
2.启用OWASP规则:
" @5 q J9 D5 l8 X' i6 V6 ?
4 Q) B9 S5 C" I5 m4 Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: G. b4 c! w5 W/ j- v, q
: [" G! p5 q* |7 M" u* i( a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ L& w) D0 j8 T0 @; }( X1 V" d7 \2 Q2 {2 S% l; R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
1 K& R P: a, l* I2 b
0 x [( k$ Z1 e H+ [, SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
j6 J0 A$ r' n, `. e) uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ i5 O: ?1 q! F& B: M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' V* `3 E! Z+ i9 T; h6 Q! B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ j. F; P1 d6 r5 {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# g7 g$ x0 K" ], k! a9 O7 H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 ]% B- y: Z5 `, e- {! t, [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 ~$ s1 n d/ Q5 S, @4 f五.配置nginx! ?& O$ ~0 u8 x; O0 K9 _
0 g/ t) R: ~6 P7 v4 J' f
在需要启用modsecurity的主机的location下面加入下面两行即可:7 |) W) S% }* u/ Q' F1 n
) x9 I' [/ p: t8 O9 _5 f
ModSecurityEnabled on; $ |9 j3 L Y z$ g( e6 E' ^% Q8 A
ModSecurityConfig modsecurity.conf;6 [/ ?6 m* a9 F. K! a1 V" n5 g1 d
下面是两个示例配置,php虚拟主机:
% B8 ?% e( V- J, T
7 T+ a$ |% B, |8 p9 Lserver {
9 D$ |0 M3 e, x% f2 f1 n listen 80;
: o3 @ F" @7 n+ q5 ] server_name 52os.net www.52os.net;
8 ^8 j) c( K. q* H; o
* X4 k1 h: T( T& I4 } location ~ \.php$ {9 t* f n! z- @& F! i, y7 B, [
ModSecurityEnabled on; 0 y" z J0 Z* d! ~; @
ModSecurityConfig modsecurity.conf; }$ }2 n2 m5 R
) `* A2 V3 I5 ~& D; ^1 V+ ?9 u% S
root /web/wordpress;- X0 x: A+ v, v
index index.php index.html index.htm;4 d3 m9 e6 i8 B3 h4 p) B1 v
& l. S( _; t9 x. t- C
fastcgi_pass 127.0.0.1:9000;
3 r: l& T4 Z0 J/ N) e @ fastcgi_index index.php;+ |$ V! a4 u/ r: d1 n' {) A1 X
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, C- |1 b8 ]) W; l: {
include fastcgi_params;
3 w" L5 P8 Z. C5 }9 z }9 c! u% I# o* C- r- k: p
}/ c4 \/ S8 x: z4 v, K, C
upstream负载均衡:
$ b9 r5 @: b6 ? [ H* \, ~, O6 e( m+ z1 g+ {9 z1 G
upstream 52os.net {
! x9 z, s8 H6 p, Q7 X- ^* T S server 192.168.1.100:8080;5 \' C( D; b: P+ L2 J. y5 e, ]! y
server 192.168.1.101:8080 backup;
+ w+ L* K$ s, s$ F4 s}
: a$ ^# a+ b. X# G! J
1 f& P; P' y4 e+ e' oserver {
0 O+ L8 o! l8 s, ` z. b* Jlisten 80;9 S" I, t( `+ k- W) @" P5 T9 f
server_name 52os.net www.52os.net;
7 [+ h9 k& {5 [9 b' L" S. D
0 s1 \, [. ~6 _% g/ Tlocation / {: \4 O) G J$ t; c/ P/ C. n
ModSecurityEnabled on;
7 M/ j; ~+ G0 W ModSecurityConfig modsecurity.conf;
9 q: Q5 K1 M* d5 B* |# {$ x0 O1 |2 X6 S$ D. k: N7 a- I9 W
proxy_pass http://online;
0 W2 B# O! E1 o g# X$ v. P" g proxy_redirect off;2 t5 G+ N8 N+ O% _5 ]5 A1 L
proxy_set_header Host $host;% l9 \2 n! H2 Z k( f; b/ e
proxy_set_header X-Real-IP $remote_addr;/ N' D, `; D' `/ ?! |* I5 k) ^
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;) x" r$ r! y3 ~1 B; N
}
s) t) R6 k+ Q4 a% A' C}. A. }7 z# t4 \, M$ s/ x' e
六.测试
/ u6 I1 U4 }8 d. _
; w4 `, F+ q# W% h& c1 A# h0 C3 F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 Q ~" C( J6 d8 w3 L& Q1 V
* r# h# W. {- [
<?php$ J. k. b, b }+ F& q/ s
phpinfo();
3 s. N A9 Y' M y- S8 y?>: X k% x p1 J4 j1 m+ t: x
在浏览器中访问:
" V2 T3 H8 {6 a; n6 f& x# M6 P
8 P5 N- n* @6 k( ^: k% x' Lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
; H1 A6 U+ U) I( `% o2 N/ z4 u7 D9 thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. s8 F; D C" b9 @7 W- u; ~http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
6 A! ~; t- z7 o0 V- e6 |说明sql注入和xss已经被过滤了
% W5 \3 v' o) n! l4 T2 o; j4 g* H P
: |4 F4 y( _& u: E七、安装过程中排错( h8 l/ p: a$ Z: R, _
, ^! p2 ?& t @! e w* x9 I% a
1.缺少APXS会报错" d2 x$ ~! |# E8 R
! N! Q7 t4 Q! {1 A3 Lconfigure: looking for Apache module support via DSO through APXS
( J z$ D( r( t, ]configure: error: couldn't find APXS
3 p6 R/ ?1 |. t& T8 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- A/ F+ H* B! s& @
解决方法: I* a/ r* u: f$ |2 S/ o4 B
. ?$ G8 K9 @0 N) yyum install httpd-devel6 `& j7 p3 l6 Z3 V5 Z; w
2.没有pcre# y o8 r6 J/ ^% Z
: J( D+ P- b4 F' \% {0 g8 T4 f
configure: *** pcre library not found." i2 ~9 u- q E$ b: x8 B3 Q
configure: error: pcre library is required
5 E/ `8 `) g6 v6 S. j解决方法:6 X( k* O1 |- g b2 h9 ?
% M l3 s# x* F6 ]
yum install pcre pcre-devel; B7 s* P9 ^" J
3.没有libxml21 A8 I: C8 ]0 p3 r" ?+ A. j& c
) s8 m5 Q, x% b- Y ^/ F* [1 s. O+ P: x: k: ]0 s% ?: H+ q) B5 q, ^
configure: *** xml library not found.6 ?+ d* y) p7 m% Q3 z3 W3 E+ S
configure: error: libxml2 is required+ j+ | F/ R" p$ k2 f/ b3 ?3 E
解决方法:
: i; ?4 d0 a3 x) H$ k3 p- v4 W% {7 A
yum install libxml2 libxml2-devel
r" P4 J! [6 \& n' @0 I4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 e, j3 D/ I0 ?( b0 E: T
9 I; X; B5 q4 K8 _! gTengine version: Tengine/2.1.0 (nginx/1.6.2)! V6 N3 }( @: ] X* {; a% W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 ~$ k: i2 Z9 R4 x; ?
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 {: \- R* r# ~; T, i
$ W. u. H# o6 S/ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.9 `4 Q) b, S2 o3 x, x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"7 r; f4 w( e8 i+ O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& a8 b6 v( ~; o" [/ J% Q% z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 R; J8 P; @9 E- a4 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& p# Z) V3 l* u* c3 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., c( I6 W% e' m0 y8 K
解决方法,移除低版本的APR (1.3.9), ~+ T% D5 f- y4 K
, x0 J7 J( ?. W7 y+ R/ A P
yum remove apr: C# D- }4 e( v! V& q
5.Error.log中有: Audit log: Failed to lock global mutex" Z! b- Z( I( J1 ^# }
( u8 j6 w, s {$ a# ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock + j6 i/ ?# g( b ^9 a/ |0 M* H/ F: O
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 r! {- \3 h v8 ^, v6 m( @8 P1 ]" N
解决方法:% f5 l# Z" f" b. S9 e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 w7 ^9 K! }1 _9 W8 M2 B( l
$ B9 a' B$ q. q9 ^
SecAuditLogDirMode 0777
5 `, o1 V6 i( j$ b4 C/ F) U& DSecAuditLogFileMode 0550
3 @, F% n7 t* {. a. t; fSecAuditLogStorageDir /var/log/modsecurity
& t5 s# Y. ?7 U/ v% ~( [% I3 j) aSecAuditLogType Concurrent1 |4 I5 t2 [. _/ l6 w& t
参考文章:
" [4 _3 Y9 s. `! Jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" \% W, V4 L7 L4 yhttp://drops.wooyun.org/tips/2614 |
|