|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% \% p9 c2 s# N7 g/ m
5 T6 C" M. e, W! f
一.准备工作
8 f) w0 U# m% D# b
: W j' t- d+ c4 J6 n2 x* P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& E$ R. [: z& l7 v0 W
: u9 }7 g/ M4 X6 z* s% ^: S
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) A0 f# l+ U* B9 c1 R- s$ E( i1 L! L+ |( o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: ?- D9 }" \- h
' U* @+ m$ W% M4 X$ a& v' h) d f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* T* q" } @! b- g2 X/ Z3 O: [- w
/ }' [( l# h; S依赖关系:
/ d! @5 k/ ?& w9 Jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ C& _. V, D( l g' ]& _5 T: f# M
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. X" y% A+ X8 r; f5 Hmodsecurty依赖的包:pcre httpd-devel libxml2 apr
% x4 {3 q( P7 A
, d \0 k. F2 V# r( syum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 z/ S* f( N5 x- u8 D U# }6 [二.启用standalone模块并编译! F1 \; r: l8 w' ~& T6 T- q
& j4 i: h5 p- T; I& B0 y( o下载modsecurity for nginx 解压,进入解压后目录执行:
/ x, _( k9 s! L# U6 @0 p6 w( r' R, v& ^6 S
./autogen.sh/ E, I2 N& [, E I' f6 ~. y
./configure --enable-standalone-module --disable-mlogc
- F" r; z- ^! O4 x! A2 d" j% rmake 1 @& {/ f% f- A; Z' i9 G2 O: q
三.nginx添加modsecurity模块
l% C& ?' [1 i" `5 M e: N4 G
# M. C4 t1 o& ^- D! p在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: j/ R" m1 E ]% q* \$ a3 n9 B% v& g L+ i( Q/ U- G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. b6 c8 M( [/ a
make && make install7 N+ g1 s5 S9 y2 S7 X
四.添加规则
. x: [7 [- L& _+ y
1 ~5 X9 a% n1 jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 ?7 [8 T( u W% U- {9 g! r: D1 O" @/ P/ e0 d
1.下载OWASP规则:# E3 w8 p1 t9 V k- t
1 {. c& q# y9 ^$ jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 N' z& J. [( {; g! v A$ W
* f% t' Q& q( s+ t! O5 M8 G
mv owasp-modsecurity-crs /opt/tengine/conf/' A D8 k, [( {8 ~6 o v& P8 K9 p; ]
4 R1 Q6 Y) {5 P" x Z4 z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( X. R8 e8 k! d% ]0 a! f+ f
2.启用OWASP规则:0 s K# T# T# |$ `) B& M# y
) ~# R3 q# l1 Z9 T* g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 g* i E% s4 o9 N+ R
4 z- z8 @( K3 r" s6 j8 w& Y& z/ j! Z8 \编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 w8 v, S' U5 Z& s2 x
: L2 A) R+ _4 k$ }: u/ r
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 k* k$ m2 p0 j* ~. b4 U; |7 n- P
* @, }1 |! Z# p0 jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' z! D, s) m) X6 c* t, Q, g- T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" Q6 q" {- x# }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" S+ l+ q8 {7 P+ u1 r" pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 p0 ?& P0 |9 s; bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! N# \+ K6 {3 F& h, q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ |* v; r# P7 h U
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& D: v; y1 X R0 C% G# r7 `五.配置nginx
9 b* `8 R# b2 t$ A5 i# Q
8 B# L) T9 x/ U, {) M: ~在需要启用modsecurity的主机的location下面加入下面两行即可:
. q+ [7 D5 G9 B( r* [ @# P( k0 |" [. p
ModSecurityEnabled on;
3 x; M( W: s8 |- J/ qModSecurityConfig modsecurity.conf;
6 B( r2 k( y" @$ j$ N9 |7 Q下面是两个示例配置,php虚拟主机:8 C K3 W8 p! U# v
* C7 E; v8 S* b7 O; @( ]" {& }server {1 D( F" H; {2 f: v3 y0 p
listen 80;
; p2 q( l' U0 F: z2 s9 ~ server_name 52os.net www.52os.net;0 m& s$ g' H0 A( P& ^
( k8 H& l( H& Q3 V. p8 ]7 [ location ~ \.php$ {
e A- P* u- T, g2 J ModSecurityEnabled on;
1 x& l: Y* _- S+ d9 E ] ModSecurityConfig modsecurity.conf;/ v1 R7 y6 ] c* n# N0 f& X
( E/ e* t4 j# Z# i. t& a5 G# O root /web/wordpress;
- U. q3 Q- F! \9 t: W index index.php index.html index.htm;* U; n7 _* N6 k8 N p) `
- ~3 c+ W h0 L6 z1 B% ~( c
fastcgi_pass 127.0.0.1:9000;
5 Z- y& M r2 n/ g5 r0 u* t4 N6 P fastcgi_index index.php;3 H5 U! W0 e/ @/ P7 V
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ R2 U6 w0 V. S; Y1 s8 ?
include fastcgi_params;
3 J- v h5 z5 }$ Q6 S7 |% z }
& \9 J. C7 m% t2 } }
" C' X3 Q# U ]; `' V5 m; h8 |upstream负载均衡:: x. a7 r4 \, _" ?& M! \6 K
3 C5 }7 k2 b: ^7 `- |upstream 52os.net {% K) T0 ?5 A( J! t8 o: P' D
server 192.168.1.100:8080;! S; d9 W& o$ Z! w7 d! o# P
server 192.168.1.101:8080 backup;
: k& N. u3 ]" S) L& {+ u" S* g}
( p T- h2 F9 [! D3 n3 A+ N& m' y. x V& u
server {. O. D m* u2 g2 L5 e% M
listen 80;8 R. H" O3 L0 R% w
server_name 52os.net www.52os.net;1 q* d" F1 e0 N) y
) y' a& \. W4 b( ]; h
location / {
" I l6 t Q' L! @ ModSecurityEnabled on; 2 B9 u& n! p, p% F# ~
ModSecurityConfig modsecurity.conf;
2 ]* F/ W. D0 x9 @0 ^$ {/ K
1 B& e0 H$ k. O7 Q! d) \' i* z' v$ k proxy_pass http://online;1 r; z7 _- _: [4 b
proxy_redirect off;
7 m. K" M: f! c proxy_set_header Host $host;. ~4 d% M3 ]+ e. c( A
proxy_set_header X-Real-IP $remote_addr;
9 [, t, |% w4 a3 `: Z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! ^2 V, k) s) ^, B6 g/ ]6 _ }9 N& u& i+ p8 B' ]% `1 B, q
}
9 }* H9 \* n8 B( ^0 o六.测试
7 X' v% l( G5 U# o1 q- ^4 Q* @$ P' D& g0 e7 L( ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% t p# `) r7 d! v0 u
+ ^5 E2 k4 M$ Q1 w' Z<?php
/ Y, |2 r% O4 v# q% L phpinfo();
1 P% q$ @+ g% ^$ u5 N5 ~4 o. A?>
( D* @9 q# T# x7 c& k+ n在浏览器中访问: l( }7 z/ t: a# B4 f' {$ @9 d
9 ^/ f6 c0 w l6 a5 _7 {http://www.52os.net/phpinfo.php?id=1 正常显示。
: p1 U7 j" q, J0 Xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 c1 r& p% D7 h; j% D3 L8 yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 E# l$ x- r" D9 z
说明sql注入和xss已经被过滤了, x1 Z# E- h+ B) Y
1 r/ ^$ |& w: T- l# F! |
七、安装过程中排错; f6 O1 v( p; S# N
, b# k6 E$ U/ k# K* U( ~( j' B" ?
1.缺少APXS会报错
% p1 `) g; u: {" I8 R+ q$ k9 M9 l4 G. R4 q, k6 Y. ]
configure: looking for Apache module support via DSO through APXS; x0 Y3 R# Y: ] R- ^, u
configure: error: couldn't find APXS& M1 D5 J0 E; v- j
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 u) P/ q2 F# E9 }
解决方法:0 F! z6 H- H( D9 E2 l
; w9 o. g+ e: X1 j1 ]" k
yum install httpd-devel% \. C6 t5 l* ?- ^1 Q2 s- U: \
2.没有pcre8 ]6 {) L9 H, b; x1 n
. z1 ~6 o: O$ n+ Q
configure: *** pcre library not found.
+ V; r& O$ [( y( q4 V# [8 c7 yconfigure: error: pcre library is required& q3 X5 i- }5 A, ^$ k8 X, `
解决方法:( L7 {+ j+ [! a4 v7 q8 v) V+ x
$ Y8 _" z5 |8 E/ C0 ~/ h
yum install pcre pcre-devel$ ~$ i7 ~) O/ d! p& M
3.没有libxml2. A1 } G) [- d) o* @1 W
/ A; _) H# w4 k' F2 J. H; C+ Q3 f! k9 E5 E9 a% F
configure: *** xml library not found.
5 j& } {. F6 d! r7 X+ Sconfigure: error: libxml2 is required
8 l* c e5 y+ k解决方法: \" K$ }$ Z# w7 a
% o5 V# Z# E, |! H( Y9 @yum install libxml2 libxml2-devel' b3 U5 C( b8 e4 g* y) w/ D
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 t2 N: N0 e8 n! U8 q
8 Q: ^* f0 Q R; @7 y+ z+ o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 O3 z- g0 [! W1 H
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) O# ?1 X& F# U k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- Y/ ]6 O% l0 d$ @# I
, e: ^, ^# `) u/ [, B0 p) r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* H6 J' U, f- Z& ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ w1 V1 j E2 w+ |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; n: P5 p ?( r+ x: \3 M. m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# N/ ?: b8 W1 o# ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 r, J, I; {8 F* l. n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 X1 F' I7 ^* A m# a$ v解决方法,移除低版本的APR (1.3.9)
- g" A7 Y$ N, x" I3 m, ^ y9 i
yum remove apr
: A$ W5 o0 j! A4 T/ p5 q6 @5.Error.log中有: Audit log: Failed to lock global mutex
' F: W: h! Q; a$ i8 p" p6 v5 j
5 r. I' m* ?7 x& S, N: I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 5 m1 G+ w D6 ~- i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"], D F6 h" v I) t& o# d
解决方法:: W) E& X9 F4 P2 @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 o- [ }4 y+ ~2 @, r0 f0 v M* J
4 I: l- w: ?+ ] j3 E! Y
SecAuditLogDirMode 0777
# z# t, O: _8 R! G& iSecAuditLogFileMode 05502 c! f" P* Y) E+ L
SecAuditLogStorageDir /var/log/modsecurity
1 h* `3 I6 ]# s8 k9 xSecAuditLogType Concurrent+ V- s$ A7 y+ e
参考文章:/ \) V" K0 B8 L) X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' \- M: r i7 s; n7 b: T! G3 jhttp://drops.wooyun.org/tips/2614 |
|