|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% `% n' Y5 y9 F' S
# \5 k J" s# M6 H5 m4 s! B4 C一.准备工作
6 ^% ?1 {" p, |4 Q) R5 {9 Q; V2 u0 h+ {( Q6 B9 {# Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# N3 y0 V6 Q' i) _. V6 d1 A
, p6 J. p; [" l- Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' p* U _9 a; s8 \0 d3 f M( y- B0 Q, A* J$ e2 L" N* F0 f
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 @; N' {7 S* s
1 W, W& G% e4 D( p# POWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
H' t2 Y4 l# _ E4 l; L- n) ~/ P1 o4 Z- _. X3 d
依赖关系:
1 r$ r3 k: Y8 t$ [tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# i, ^0 a0 V+ N) u9 T) _/ d* m$ d J* T# y/ S+ N
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel7 {! W" J% m0 R- F
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 Q0 }9 x$ k. @: A
% }; w* x/ n' p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel2 K# c0 X) i6 ?6 s% O6 m M
二.启用standalone模块并编译4 t1 }4 a" Y. M' u7 P
4 W- _. v( p& v7 T- t2 }# l下载modsecurity for nginx 解压,进入解压后目录执行:% x3 s" V& s5 w$ T
7 r" |& A( K+ p9 U' i3 E! H
./autogen.sh
; a% \* d1 o# h1 X./configure --enable-standalone-module --disable-mlogc
$ z4 m* w# E; \. E; k7 V+ x% gmake % l/ s! C3 H7 V# u7 V% ^
三.nginx添加modsecurity模块
7 ]# ~& X! m' H* |& A9 X4 ?8 O
( h& C- g# M! m4 u x* T4 U在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 f1 Y0 d$ F" |! c2 c* c
2 s4 ^' [2 a4 G. N3 E./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
) l0 [) W$ t7 I" L3 E( R0 Umake && make install
* E! k; v8 p! N; d8 [, l- d四.添加规则; m4 {% U0 Y8 W! |% l! V
; F, x7 P+ L1 Q* ^2 i( V
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 c5 m) V* X8 F6 }% V1 i0 X! C
% `; ^! N L; n3 z3 R1.下载OWASP规则:
1 F3 a7 W; D1 I; B' r8 b! C! m1 U* J# c% M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: }/ V# V# f% g2 V" y6 V; N" x
8 M% u# _' Z, M6 x( A6 smv owasp-modsecurity-crs /opt/tengine/conf/
# e) E" s$ |* p+ h( j
/ k+ p- F) `5 C/ acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 i6 \3 e H, C$ @3 `3 h3 g
2.启用OWASP规则:. U: a- h m) o8 v1 g
2 m h. Q+ `: G0 }5 x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) p1 k# W2 n& t4 k# ^$ x+ b2 J
1 B8 P0 {' T! N9 q0 U n编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, T. f3 T Q9 O3 ^/ N& M
& _, z! h- d; J; \; f/ U
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" L7 t8 v( }, \. |- Z& |6 A
/ t, Z+ d# ]7 Q. j FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 x4 s# [( o9 d( f0 w6 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf0 M- T0 X7 J- L% G7 y+ L# T8 j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ u7 W. B0 o0 b. H- j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% C- j2 U* n: W* pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ s0 g" s3 u0 R' I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" z) p. z# D2 `. CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 G7 T8 v' k' F( U4 k- L6 B五.配置nginx
" D' Q6 e- \2 E0 Q6 f( m
( D' C. E! d4 W" Q5 {9 i在需要启用modsecurity的主机的location下面加入下面两行即可:3 U3 z4 G- }( Y- ~
2 J/ {% f# }2 `) z3 Z* N
ModSecurityEnabled on;
( M* G" V# f5 W, dModSecurityConfig modsecurity.conf;
- j2 d, O0 Z% X; c. {下面是两个示例配置,php虚拟主机:- y; K0 v% \: ?5 D
b8 P, e4 L" k& O0 `; K
server {
; U: b: c. N& U* X4 l4 u# w8 c listen 80;0 Y' P* |; ?; O/ U M% z
server_name 52os.net www.52os.net;! R- J% F7 k; ?3 C# V4 x
, D( s- y7 }. Y# B f location ~ \.php$ {! P$ a' g$ r8 _6 [/ ^% v$ x
ModSecurityEnabled on; : E6 y/ H. |' ?& V! e0 ?
ModSecurityConfig modsecurity.conf;* \: Z3 t0 [! @2 [1 v, v8 r
% G+ s! i |* s root /web/wordpress;
+ K' V* N9 F$ \' y% @ index index.php index.html index.htm;
, ]& E: {% B, V! { b6 Q: O( @8 f+ {; E) D* G4 Y. C
fastcgi_pass 127.0.0.1:9000;7 u( m! x) O$ I8 q& z
fastcgi_index index.php;
, S F* B0 b- p fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 [' L4 V$ L9 |# f
include fastcgi_params;& d+ F- E& ]; r" }. l, y, s8 V! X
}# K# a' |; ?% y0 H9 m
}
4 j1 j/ J* c$ ~( b# D0 t$ @) v& Uupstream负载均衡:
3 K& O4 ?; k7 t" F W4 v: f8 |5 S( E- k# ^7 @) @
upstream 52os.net {& m! a( ?( U7 l$ e
server 192.168.1.100:8080;
( G4 Q$ I/ k/ a: S4 I! F server 192.168.1.101:8080 backup;
7 |" ~6 f7 K" e r/ v# e}
% p" b4 I; O w+ I. c8 O1 W4 f3 U: l' T/ [
server {+ ~3 }2 a, P8 Z& H
listen 80;1 L8 X% E$ H0 [! A+ q
server_name 52os.net www.52os.net;
6 l: {4 |+ @: u* [$ q6 i5 q7 r4 u9 |& T" z
location / {, H3 f& q% R- P8 [& c
ModSecurityEnabled on;
a8 s% @6 g/ b, [% r ModSecurityConfig modsecurity.conf; : ?6 F* y/ B! M7 y: W
& r4 t1 I: {% }, u- h8 d1 n proxy_pass http://online; k- X# I. }, o4 b, E$ p7 a3 [
proxy_redirect off;5 L$ o: ^/ V1 c5 Q* C
proxy_set_header Host $host;/ |4 h: g+ v# c! x1 U/ c) l
proxy_set_header X-Real-IP $remote_addr;
3 u; I/ y) p# ^. s proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 p$ U, x! \$ T J( ^
}
' K. @7 `# X x |6 i5 d+ E}1 ? P' j6 C# ?) {5 N4 Y
六.测试8 w/ N1 U+ K2 `0 Q
9 G+ X# ~+ E4 a- v% c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 U6 k+ ^, B- C& L2 J0 P- x2 @# j6 d+ i
<?php
: a6 L" a5 I; }2 p. g phpinfo();
. j+ k3 {9 p/ y* |?>. `, l$ k9 ^+ h \
在浏览器中访问:6 l4 _* B! q. R; C1 h' t
+ \( q( [' W0 [. S
http://www.52os.net/phpinfo.php?id=1 正常显示。
" j; g/ N& E8 }# X7 Khttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。8 Q& Q( c5 }2 m: u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 l* M* J3 @% c& A! }& b' q0 c( I {
说明sql注入和xss已经被过滤了
. G9 b. O2 ^2 p7 j q3 y& Q5 d* q! z0 r; C) y+ T3 O/ @& G- e
七、安装过程中排错
- J! z" p8 v) I V( H
* G2 N- v- ^- A, r! q8 X6 `1.缺少APXS会报错
! ~* J) e" h8 N1 Y5 Z1 Y! L7 A9 ]* O- C* q8 t
configure: looking for Apache module support via DSO through APXS, L6 y) G! h) J4 o# @: [, N& X
configure: error: couldn't find APXS
0 T. N1 U& m, Q5 a$ F5 @% c! Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) N N k" N T; q( Y解决方法:) [( d- H; M3 ~8 ^ ~+ m. m! d2 Y
J, W+ l. [3 M; {1 T+ Q6 v8 f
yum install httpd-devel; ?. Z8 U s0 z* m" I, S3 }. M2 f; [' i
2.没有pcre9 | ^6 H" j% F& k" M" i
1 h7 L: N- v4 v
configure: *** pcre library not found./ N! k, L8 O% e' K! K) p0 Z; |- ^
configure: error: pcre library is required
0 B( y% A/ }* D3 Z3 ~: r解决方法:
( Y3 F$ V. j2 l7 }
# ]+ ~' c5 o# t( Y' B- ]yum install pcre pcre-devel: z4 O) j" k* M$ r* e! F ?
3.没有libxml2
4 o; {2 {9 w2 C: p, o7 w7 c( l; t" j& L( b# @' O* P
% Q4 ^3 F# z1 Q* G* h P! Wconfigure: *** xml library not found.6 ?9 M) }* q' s0 L0 H9 u
configure: error: libxml2 is required
7 r) `& o! M0 E2 t解决方法:
, N7 G+ V6 b$ C" F/ ^" R o; B6 r" `8 A C1 h; n
yum install libxml2 libxml2-devel) G$ _# R2 ?7 z/ H! Y8 x
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( W7 T0 M' z) l, Q7 F9 d1 @0 A5 {$ [1 t6 s0 ]; e5 ~
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; W) \( p% I6 m; {nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# `: v; Z1 k& ?" \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 c2 z T& [8 k% ?7 U7 O- ^* I. X/ H. B& V- b |( S7 I4 T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 c! l7 Q' F& N& Z9 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
9 W3 D* q7 w& F' ~/ v+ U2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 P# [. n$ o2 s' I. g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 P7 k0 ?1 `* O$ n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 b6 T' d' i: @ R( @/ M6 W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 |# S8 I9 `! J8 p) a. a9 V% W
解决方法,移除低版本的APR (1.3.9)! G0 \0 C! T: F
0 C/ T4 j5 H' v1 uyum remove apr
. W! F$ y& x0 e- ?: ^5.Error.log中有: Audit log: Failed to lock global mutex
. ^, R# S$ v; h7 A% ~2 m
) Y, N6 v$ |( D) ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . l: \& P" ? x$ N1 r: ? ?
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ w7 u( f8 `* r# K" C解决方法:
8 j m3 D( M! J/ M; K编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( o2 v1 ~ P! S! q
9 D* o7 d# C/ S, F; l' \( G$ e" d% cSecAuditLogDirMode 07771 V+ [& e) F4 k% M0 N
SecAuditLogFileMode 0550) g q( C' \ B6 ]4 c
SecAuditLogStorageDir /var/log/modsecurity1 }: b! J6 K5 }# L" c
SecAuditLogType Concurrent
; F5 n1 ? Y& k: i; y" C: L! U. n参考文章:/ [7 z" k# T. X" e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& I8 B* R2 ^8 ~& P, H
http://drops.wooyun.org/tips/2614 |
|