|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, `( a# ]- ~! Q8 ^
( B: X4 S- ?8 a+ @: N# ~一.准备工作 b6 c! b1 M8 X' o3 N
# B% Z# M" F9 W( z' g6 x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' P. W! H) B& K' ?" P- a
( r# Z6 N4 [5 R% c, Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& @9 j" c& T& s) n' e2 F5 v" N- P r7 {. R. ^0 c& l s% m0 n
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) ]" Y# O& {4 n! T; `, l+ s7 y4 R1 |7 I& o- |1 @" y$ h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, c* i6 ^0 b# @
$ B7 Q1 h' @1 ?依赖关系:
) J) h, e- Z5 i' |3 s0 B, B; Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 j, W2 A7 K2 Q, l" a. H4 S
" I+ M3 ?1 S o9 c# {7 Z( ?yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. w. h5 z2 e9 nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
" Y" T/ U' L+ p
* Y. t8 w* k, r! Uyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! e2 i" |2 {( P; W: f5 t0 d+ y7 l
二.启用standalone模块并编译
$ l, e3 W$ \; z: }$ S) q* `1 R0 J4 V$ o
下载modsecurity for nginx 解压,进入解压后目录执行:0 l. T- n# E a0 M5 g- u$ f7 H) k
" f% _- K; f5 m+ G. U
./autogen.sh5 @4 Y3 u' Z. h
./configure --enable-standalone-module --disable-mlogc
& `) M2 t- i( a3 B# _make
+ V: F* b$ r! d$ a三.nginx添加modsecurity模块
: |! ~* q3 _/ R8 I! Z+ I2 q& C* W0 c) _
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" C4 r9 {1 j1 L! M3 e- M* y; ^
) {7 x% p; a6 ]& T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
1 X3 L# S" v% D0 Vmake && make install) K) b9 q5 f5 I2 `! z
四.添加规则
. b3 g, x W7 h! }" U% |( l0 ~1 `$ }, b% x" j6 M9 H3 x
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* M6 _( x4 Z7 X' I6 A* P/ i1 m+ N: G
, @2 E+ j! @2 \1 J; H1 Y$ f. R
1.下载OWASP规则:3 T9 k2 _4 }( Z& ?/ v) d2 X
^( k3 s, k* Q7 A N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
C, r: H8 U1 k5 f
/ K: s3 y5 B/ F) M- `' Lmv owasp-modsecurity-crs /opt/tengine/conf/ j3 K- a- s9 o* }7 S
1 ^3 ?1 Y- I3 z6 ]1 U( T4 \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf K( `/ d D- _0 [6 Z0 |
2.启用OWASP规则:
8 P% `; H/ t t+ B( i5 f
( U! x# u g% l( N( _; h复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" G( i# _6 H- ^2 D- N
8 j# [- d D% ^" K; }编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& r# n# v6 a, `4 ^# Y8 }, h/ o+ o& v `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- S( T) x! x7 a) B
, ~( I G: y* L- a2 m* sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' H9 a9 f2 T; o5 A5 s* z+ OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- i" n3 V. s/ {- MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ Q, ]6 o5 ~% M; y2 LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! a0 k. ?5 k. `" ~7 t& n0 t) LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 \/ M0 ?: b1 t! n/ l& a2 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: [3 V6 h4 c$ ^2 A* pInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# S# T/ S5 |/ Q7 @" V
五.配置nginx
, r5 \% ]# {: k1 a' D& {5 v: Z* t# h& {" Z4 P
在需要启用modsecurity的主机的location下面加入下面两行即可:8 y* y* c: A% R1 G, k; v) m8 d
- e" I9 L. u+ Y# D8 t
ModSecurityEnabled on;
: m" ~" M3 ]$ h! p' R, \ModSecurityConfig modsecurity.conf;3 o: x$ L( R+ P- \% l- p
下面是两个示例配置,php虚拟主机:
. X) \) y, |0 U. g9 G" `! f G1 u& `9 B. \/ |- e& b( k3 H& f
server {4 {( Y3 N9 S5 r# @! C( x" E- `
listen 80;
0 b2 \2 R0 v! v' X; U# M8 g3 O. ?- R server_name 52os.net www.52os.net;
y N7 E! B' m& F# l , Y/ N+ B6 T) b' ?. h3 S
location ~ \.php$ {
% ]$ h% s# s) R- Z9 y ModSecurityEnabled on;
5 W* P7 ^% ^/ Y4 V Z% P' E. Z ModSecurityConfig modsecurity.conf;
# l$ a: C0 C- P4 w0 G
3 r8 S( ]0 X/ w9 K: ]/ k% c* B root /web/wordpress;
; m" g8 v- |% |& b index index.php index.html index.htm;/ H2 i& f$ i8 B3 }9 H, l( E
2 S" v( @" V- Y7 Y4 n- X
fastcgi_pass 127.0.0.1:9000;
+ g! Q# j4 l: ~ fastcgi_index index.php;% n6 H& _2 k: a/ f* Q: t- G
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
4 _+ G8 I% T" C" w( B$ E7 V( C- d4 Z include fastcgi_params;7 H3 n3 S- b3 M5 L1 {. X
}5 ]; p2 T O* l; t E6 V
}( z6 H/ r6 z8 C/ Q9 h- ~# ~4 c5 }
upstream负载均衡:% Y2 m. ` r9 S- I4 I1 x8 I1 q
$ N2 @4 n2 l7 F7 eupstream 52os.net {1 {3 _' K. \4 a) }# w' L
server 192.168.1.100:8080;
' T3 c/ I( x4 p* p server 192.168.1.101:8080 backup;4 o' u; a' `1 M" T) I+ s5 r6 m
}
; z+ z( B: ~+ _) g
- G: O9 q3 m I- ~1 a: f) Y) Kserver {, \9 s9 q/ d, v2 h9 \% _- [
listen 80;+ t- o/ L5 \ e: G
server_name 52os.net www.52os.net;
& n' t2 O- I; n
, p2 C% Z0 ^% l, E" d) s H; elocation / {* o2 M2 U6 O1 s5 H
ModSecurityEnabled on;
# v1 \! S' C2 y1 S ModSecurityConfig modsecurity.conf; & `, X. D0 z( `! |
: z" }$ S# `( ?$ s proxy_pass http://online;
0 {# z! a$ @$ J: Z( E1 s proxy_redirect off;
, O% J0 Y$ j# h proxy_set_header Host $host;/ h! d& T# g; p# F2 S' ^9 C
proxy_set_header X-Real-IP $remote_addr;& ?& S4 j6 r7 u2 D7 R5 D1 J4 C; N
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 V' X2 L6 @. V9 B }
) Z; I" A1 z# H" d& u1 L4 w1 g* X}# U/ h: P% `( u' _
六.测试7 c/ T0 w" O6 |! F# ?/ q' ~
) F) E7 `! h- K( p我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. U* t( t7 P2 K; D) W1 k: T# Y. T G' W5 X
<?php
) T( ~0 K( ?! y! f phpinfo(); 0 g- a0 }) d/ E
?>) r( b" J" U l
在浏览器中访问:
% l+ q5 w$ u9 W+ w% s) h9 @
& }, k, z+ I9 [$ Thttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ b. c! J. N8 h9 e% q1 l" N1 Whttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。2 G9 s$ t$ W) |: s
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 X: G5 w+ U6 _ f
说明sql注入和xss已经被过滤了+ _7 m5 T+ h2 `
' Z& t [5 Q7 K6 I, e
七、安装过程中排错( L; t$ E/ D& d3 J9 b) [- d
) E2 e+ r$ o; a1 H
1.缺少APXS会报错
0 ~+ a& \. J4 D+ I' a9 D
7 e) l: p$ @6 S+ h0 q+ [7 Nconfigure: looking for Apache module support via DSO through APXS
0 k5 t. k R+ D$ I4 Bconfigure: error: couldn't find APXS
" E0 W* u) s1 c% G4 c0 d& Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. |# _& o2 ^+ M- t+ y
解决方法:( c% r9 i9 ~) y8 D0 {% w$ g5 F3 G+ ^: M
& ^/ @9 K: W8 [+ T
yum install httpd-devel- x' J) i3 M1 Q0 R8 n
2.没有pcre
* U4 g. u" R- m# S/ C. x- V& f
( h3 R" `; {9 a {, Uconfigure: *** pcre library not found.
% d+ r' S% o0 E, T2 `6 M5 hconfigure: error: pcre library is required. K% m( N& V+ _8 I: K& E/ }
解决方法:0 P1 F& B' B+ A$ m) s
) V6 @5 y5 s5 I" k0 s
yum install pcre pcre-devel( G9 M+ |+ `& S
3.没有libxml2$ F9 \2 f+ |# a& K, \
/ W. C3 n; Z1 E9 v& A# a
. P4 z. c5 Y. Q% Y" L7 Z
configure: *** xml library not found.. b0 J( X9 @0 K A
configure: error: libxml2 is required/ |3 A, D9 P9 |- e# `9 g
解决方法:+ ~& X% q- y2 @& G/ g' b
& J5 `! r+ M& o W# @
yum install libxml2 libxml2-devel
* |$ P4 L0 K0 U* E6 p4.执行 /opt/tengine/sbin/nginx -m 时有警告% w2 o7 C# {$ r
3 B+ m3 k- H. ?( q& FTengine version: Tengine/2.1.0 (nginx/1.6.2)
) F7 W/ G$ J3 Y- P* znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" `8 A9 c1 W9 |: k* Z7 p7 d原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 H! {7 \' }8 e% y9 C
% h c0 Q L5 i+ d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
I3 }' Z7 Z% L! \+ D% E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ T* T7 B6 Y1 o8 i1 T' R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ |$ e) K" n/ E- F t! w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 y$ C0 y3 ^5 a4 e% d6 z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" V# M! k8 }) [2 g2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ Z8 h7 B4 [) F3 u$ M) S解决方法,移除低版本的APR (1.3.9)
/ a% [3 d" H9 G: ^ A: Z! N' C; a% i: L
yum remove apr" j5 u: U; M' C2 M2 U) ?
5.Error.log中有: Audit log: Failed to lock global mutex' u- {* a0 R" l" A( F
& H* h; I: c* q1 n% ]. d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , @ U: [$ U/ I8 h
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" ~2 Y, H+ m; c
解决方法:
: e- E! w, X! i编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 A3 l6 _7 v- n, j1 d% X
5 I% s, i' z: B( M/ c6 YSecAuditLogDirMode 0777
; {$ S; G4 U0 E; q1 B. r6 nSecAuditLogFileMode 0550
4 G5 \8 H- F/ E5 ^1 T7 b) ESecAuditLogStorageDir /var/log/modsecurity
: W. i' F- T/ m) ySecAuditLogType Concurrent8 D5 {3 g* [7 ?( w. a$ _0 @
参考文章:
9 ?' K$ {0 ?& G: thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; B5 f" O: w8 W9 H0 T0 Zhttp://drops.wooyun.org/tips/2614 |
|