|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& K' C N4 A' c2 `. J" R3 q1 d3 w2 l& m5 l$ T# d! u- v
一.准备工作
" O5 e3 o) N$ }9 y- Z) C, A- A% J6 _) Z! J2 y( w' ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
F `/ c s4 K# V: a4 g9 \2 O: p
0 h3 B. y- K: @1 q- u K# Ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ P* a; r: ?$ z5 X9 V* g% |$ v- e. _" r( o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ A1 `. S! Y; J1 W! y1 ^! @! X& {- q; D& Y+ j) l; n
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! c: M) H c- O. l: `+ I- J+ ^! _) n& X1 s( s) U
依赖关系:" ?9 H& e* j9 d! e1 d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 y8 x3 w; i3 q5 `5 W
0 y6 m" @0 J0 }" u5 d' |: Syum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" ^- {/ B# X6 u3 c% |. u( V
modsecurty依赖的包:pcre httpd-devel libxml2 apr, h1 E( }7 G1 V5 Q
9 R! \% v& ?3 x" _* H. p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
. S% i) u- ~, Q. h! G5 s二.启用standalone模块并编译
$ n& d( D0 y9 M5 w/ X, f( X9 K* N6 R& I' S
下载modsecurity for nginx 解压,进入解压后目录执行:+ w6 t( ?! e& H4 R8 X$ `0 m
+ v& B: m5 R' ]4 T
./autogen.sh
]& `! `& X' ~3 t1 v- T./configure --enable-standalone-module --disable-mlogc
+ e1 \2 j8 Q" Nmake
: T+ ?8 B) y9 n, v% J三.nginx添加modsecurity模块( Z; ~8 ?' r3 g; ~
( F1 J7 I( h' _在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ x1 f% U, O- J( T! b( B# g- @( Y& i! d, X' B( M% x% I# `. b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, I) m$ q2 b: p1 {
make && make install3 _) N3 @$ a- o" J1 r4 P+ {( x
四.添加规则" [& ]3 p7 R+ A/ q
" n: N( x. r# g/ O& u& G ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; _ Z& x) j6 H. L, p) R, V
7 }+ }2 J' I& e' R$ c1.下载OWASP规则:
* d( c6 U4 M+ t! h+ V# m
5 L3 i( z9 h* h" agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ Y9 O: }# E: I9 u; G2 X+ U/ X3 b. _: ]
* V v5 }$ ^9 {0 p6 m
mv owasp-modsecurity-crs /opt/tengine/conf/; V3 B8 y8 O. Y7 c2 Y* o5 a
% \9 _9 `7 z1 @: d0 j; s2 @
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& q* o8 b3 v: @+ v7 a# q/ h2.启用OWASP规则:: ~& g$ ^; t# r% I
" }) \3 b6 {2 u5 [* g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- _" S- h- t# a' F% m; D( x7 K, Z1 c2 f4 Q o" }4 ~
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 }5 T+ d5 ` v
- H3 |. e/ R! U% f+ |; sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' u6 v2 K2 ?) {% P: X3 h r
! Q2 K8 a, a9 S7 \& dInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 O" X. e4 C* w2 U U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 g" M5 c7 F) X j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 f& @; s( ]3 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 i0 F" p" H; t `% [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 ?6 N4 b# h/ v6 YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 O8 J7 _% o% y' l" n" m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 Q! s) G' k O! M5 p+ Q/ S
五.配置nginx
* \/ U/ F& M, |% p' R9 d
( h/ |/ j/ l% a4 A e在需要启用modsecurity的主机的location下面加入下面两行即可:
0 X0 V9 W- Z2 w) R3 b* h/ F: \1 m- S7 F9 t' P% P4 y5 W) n
ModSecurityEnabled on; 8 N2 v* ~. j& Z
ModSecurityConfig modsecurity.conf;
& k I6 ~# v" @) J0 {1 ^3 ^ r, |下面是两个示例配置,php虚拟主机:
4 f" @- R' Z0 Z+ M2 x
5 L6 H7 K4 p9 F4 B5 @7 F* bserver {2 I. D+ i7 P9 c, ?
listen 80;
: V' q" z/ O$ p" T: I0 o8 L server_name 52os.net www.52os.net;
7 m ?2 t5 k( X8 S! s' H4 m6 G : V4 s3 g7 V, B0 e o: L# F
location ~ \.php$ {; Y2 D& b5 ` n
ModSecurityEnabled on; ' @. H5 D+ F+ _. [' S) y& x7 K
ModSecurityConfig modsecurity.conf;9 F- S- M! }+ `1 J7 i1 V2 x
5 ?0 V) q$ T1 k( K5 w, m6 n3 o
root /web/wordpress;* Y4 n% T k' }/ R4 k
index index.php index.html index.htm;- |3 _4 E8 A3 x3 n+ r# t
( J9 ^ Z1 S) W/ ` fastcgi_pass 127.0.0.1:9000;/ n5 U1 z) Q' p8 C
fastcgi_index index.php;
+ ^% E5 y1 x% F* e- v1 E5 \* t! J fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 z, x [: v/ b/ O1 O; c9 e- F3 T6 u) T
include fastcgi_params;6 c1 d1 L! S3 _! C4 }. L* G
}
. \- r3 t. I! o l- V5 a) d }
$ P5 |8 } u# [1 }* A) P9 pupstream负载均衡:* l0 D/ w1 M5 L2 a
! J' d; Y6 t' W2 s8 V" A6 l
upstream 52os.net {
( ?- O4 R. q% ^/ p server 192.168.1.100:8080;
7 a0 }1 w+ p% L! @ server 192.168.1.101:8080 backup;
- m# l: t( D/ b) G0 J5 H}6 m4 E( w: e7 j0 f
8 k% M2 J7 t3 m9 q) v$ ^server {9 Z& h4 d' S0 ^" ~( a( q- g( Q6 [
listen 80;
F4 v; u' b. w4 P0 L! ^server_name 52os.net www.52os.net;
]+ h* p$ S7 w( ?, V8 w' x1 Y
$ c6 e5 @4 p5 T$ h: Q! u7 Y+ mlocation / {- c7 Y: l, f T! ^" r7 i
ModSecurityEnabled on; $ ~6 j, R$ c' p2 i
ModSecurityConfig modsecurity.conf; 8 E5 h- ?2 ?0 P" v4 J
" K% Z' r% n! q. ]9 C3 x
proxy_pass http://online;
, K: x2 Y- }( F5 ] proxy_redirect off;, v! R! x# ]0 m' K; R3 g
proxy_set_header Host $host;
4 i: i! X. Y4 H# J proxy_set_header X-Real-IP $remote_addr;
7 A" A: U* Y2 R& K! W5 [/ d/ s+ M& O3 k proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 h5 Q7 V% U" r1 u8 r) {( Y5 j }$ i8 q- w' D! K& E# ?5 f# S3 X: v' }
}5 ?1 T, k/ u. \3 M G+ [
六.测试
; D) _# a7 ?0 ?2 d. ?' U8 W7 K' ^; V' u( X
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' L8 a8 Y7 Z6 C$ O, b: s3 I5 {, I* W2 k, i1 G
<?php
, k3 g* j( c. } phpinfo();
! b. D6 d! _. }?>( L* z9 Q2 h! W5 m
在浏览器中访问:
2 O! Y$ b9 M, j; ~ s6 x7 X. J( r" B- C& c! Q K
http://www.52os.net/phpinfo.php?id=1 正常显示。
9 |/ R* {* r, |" T% y/ T" Ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. n$ t) n) d/ ~& U; S* {8 p2 Whttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ X$ E0 v; T9 B+ M0 E" |! T) |' W
说明sql注入和xss已经被过滤了
/ b$ X2 c! \+ q9 k. ]/ T2 F8 T
$ ~( t7 Y3 [$ o1 U; W) n7 ]9 a七、安装过程中排错
1 G) O7 d9 }2 J( }6 D( W
3 r J7 a3 L3 |0 ~( ~& Z C- M% m1.缺少APXS会报错2 L+ K1 `" V. f: g5 F
' Y) e9 `7 p+ o: j0 ~5 k Econfigure: looking for Apache module support via DSO through APXS, H# f, ]/ j6 f" Z8 a4 [" V
configure: error: couldn't find APXS
9 g) s4 \: h- F F, n$ ^1 O) fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 f7 N: m# O4 K% m解决方法:( Q& X9 M' a# Z& [* O' y5 N9 w
7 }& e. |+ b0 Q: F) B/ n5 R% E
yum install httpd-devel
0 Z3 I! V3 {# c3 ?' t! m. m1 Q2.没有pcre
7 ~8 `. Q4 L; L6 p) W" k f1 l9 n! y) y" ^; C
configure: *** pcre library not found.
B W2 E2 o; ~/ Mconfigure: error: pcre library is required
, l$ @( \! r; j( \解决方法:
/ R! `' X; G3 r9 d9 U F
3 T0 u) O; D/ c1 {2 ?- G- ]0 ?yum install pcre pcre-devel5 B4 ~4 `) q9 n/ w( c7 u! _8 k
3.没有libxml2, ?9 {# E' e# `4 y/ ?# O$ b/ ]3 O
' @1 p Y9 K! H/ V1 }( ]* S) ^! O6 ^1 i( `& t
configure: *** xml library not found.4 d, Q- _: |) \( J/ Z
configure: error: libxml2 is required
- G- x" V5 W9 C0 D% B6 W# \解决方法:% i( p# h/ W" f% j- G3 m
5 \4 ?+ w, h- u
yum install libxml2 libxml2-devel" Z7 I1 X- v+ u/ M( O
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! \: r7 o9 w5 V9 J6 O/ `
% M9 j* _6 E+ v1 c& WTengine version: Tengine/2.1.0 (nginx/1.6.2)# F) N& R5 Z/ U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 l8 S* i5 V' A. {5 a( h6 _+ q# M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 g( W* j7 W1 C- t
( a- W# `6 o0 E- r/ }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# p9 m8 [# c b5 U6 B9 |3 u3 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"9 }6 K8 e% w- j1 h3 C; O& N& D
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: f( j1 P4 B) v2 \8 T7 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 E! G# \7 }4 g! H& G3 Y1 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 H8 K' ~: u' Y k; D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 ~6 w7 w8 `9 c0 O5 A+ Q; x
解决方法,移除低版本的APR (1.3.9)
* S! c# c, w) c% o( D
* ^* S% a; T" b9 i5 O5 `yum remove apr& ~* W0 q5 Q2 @4 S) U; w1 c
5.Error.log中有: Audit log: Failed to lock global mutex
" V2 m/ g& k3 N
1 r# T! ]0 T, s/ ^# M. z$ M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 s% q# K" U4 Oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 g; @! j C; ]0 E
解决方法:5 e$ h9 r3 w( i; c% P4 J; f) W; z( h. w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( ]- E, |6 \: a% A) e$ I' ^8 D, q, e9 j% E! E4 M% B
SecAuditLogDirMode 0777
- s0 r/ d* C, E; w) E! u% SSecAuditLogFileMode 0550
# T4 t$ o5 x j8 O, r' _3 @- {6 \SecAuditLogStorageDir /var/log/modsecurity
) z& B3 {& k/ N: m* oSecAuditLogType Concurrent! s. s n) u, R' C' `) ?9 D
参考文章:+ u7 C4 d. K7 z( M( R* F7 g3 S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- x- ]; Q2 @* E* {; chttp://drops.wooyun.org/tips/2614 |
|