|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: v: P j" D* K6 d2 G0 f6 U* {3 p3 |
1 D4 o' c7 D M. b7 Y一.准备工作7 |- _1 b" i) Z# v* `5 f& N1 E5 g& q
: N1 Y3 X: X# d1 J系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 \. @" L+ c8 G$ H5 e+ e% K
# M* h4 W7 D2 e9 q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 o" \/ ?; ~/ |% e
9 y4 u1 V& X+ x5 ~modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ j! L7 K$ _2 b# D* J. B D+ |+ a. r1 I
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 B, p9 k; v0 c6 B- d9 Q
$ S @ m9 x$ j0 l依赖关系:0 A8 O$ q6 g; ?/ F9 l8 X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! i3 t0 c/ Z* B, [4 d( o' G) y* R# Y5 L, P
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
3 B: I# z1 E9 U# ^) A0 N0 vmodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 y6 j5 F7 Z* [: ]* A, ~
. ?, t. D0 ^3 dyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 m+ G' \5 F- K$ T二.启用standalone模块并编译' L0 E) ~) [# s: ?
|0 a& ?: j v; W5 ?
下载modsecurity for nginx 解压,进入解压后目录执行:0 P( T9 B5 t1 x3 R p
2 A. L1 G0 R9 ~# O0 H5 }
./autogen.sh+ p. f/ ~! Q5 \) P
./configure --enable-standalone-module --disable-mlogc
" U, [4 U2 B' j0 H! Z7 Q# wmake + [# _& S4 E6 X3 k4 ?
三.nginx添加modsecurity模块
& Z* [- G6 V3 R0 k% _* p
1 [8 x2 |- D% |5 J1 s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. {. E' c P9 e4 c' f# J! @" L$ e* y, Y( O
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% k: } e: s. M( l/ q7 \( }
make && make install
3 g4 q3 T# N! |四.添加规则
% f# Z2 F2 K4 [$ l. _$ J
4 \/ K6 o5 ~) B% S. bmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" b) u9 q( t; O8 B1 ?, v' F( a2 P6 }& X
1.下载OWASP规则:4 p- x; h& L0 \: K: _
4 z) U+ Y; V7 i3 E/ u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" ~. m5 s2 g. x. P4 }' k
2 z7 [2 K, v3 [0 z; S6 h. Emv owasp-modsecurity-crs /opt/tengine/conf/
* O: [# y% o. g0 y1 }# e4 |: F# b. [: D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf d6 d+ Q+ g; F, Q( {4 x
2.启用OWASP规则:
0 [- X7 v% w' p/ t' o8 u# ]. f1 J& X7 F" R: X$ N8 `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 K8 L) n3 V7 h% A4 a
+ f* A a3 L/ M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# P7 Z6 x" R# D# v9 x. @
e! o3 q; w. c! v6 t+ o* O; lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 Z% j, T) n4 L
% _7 E7 G$ u4 r/ ~+ OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 c% \* t8 b2 S. u3 P- b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! P- C2 e4 w+ b4 K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
A1 d5 z7 e3 P3 TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; o* n2 f, X* d L; M/ h) B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf U* L$ H9 J) I1 W" _! g7 g( l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- l/ |+ m p! ?3 U- g; F/ K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf9 J$ B ^) a8 n* I8 b8 D5 n
五.配置nginx
3 e- @; b% H( ]" @9 U3 z8 z6 t+ N! T; Q
在需要启用modsecurity的主机的location下面加入下面两行即可:
: a& k. p+ g+ h. s
# }- z1 t* u* W uModSecurityEnabled on; ( ]+ E5 e+ t( S. F0 j% f
ModSecurityConfig modsecurity.conf;
0 K; u- K( m/ c& K5 p. o' Q下面是两个示例配置,php虚拟主机:
2 _" V6 y {: a; I& V5 M3 d
^6 |* W6 `" X Wserver {7 m H, `! o) J7 m W) B
listen 80;' V( a. e) G* H1 c ~) I1 ^# z
server_name 52os.net www.52os.net;# {! n( Q# P. ]6 A4 X' T# L) r- \
% j# M- f4 a" P3 R4 H) R( T+ S$ [* H location ~ \.php$ {& o! H# [9 l: t1 F6 D) Z
ModSecurityEnabled on;
- t1 d( G' O- H& S" Y ModSecurityConfig modsecurity.conf;' K, {1 J, H1 Q$ k8 n
; l* v& E: m4 l9 y root /web/wordpress;, G$ Q% e, v! W
index index.php index.html index.htm;8 S, h6 \/ J9 p! D% f+ W! K: K
c/ Q* C7 H( |# K. ~1 r6 k: D fastcgi_pass 127.0.0.1:9000;6 {0 i- G3 R7 E6 _5 X, t! z2 c
fastcgi_index index.php;/ _# G! @) p: T2 C' f( r! h" N
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, d8 l/ _/ ^( ^/ Z8 A include fastcgi_params;
. l8 O/ o6 t1 a N0 P V. c7 P }5 L2 j* u5 x- D( \
}9 K l8 m3 }: j
upstream负载均衡:
+ s) X: y) t& ]5 s% O
0 I( c% C) J* \( t- J* \' R. o8 ^upstream 52os.net {
4 y& h2 y4 q4 `, A2 Z server 192.168.1.100:8080;9 N- |( W$ j$ d& {
server 192.168.1.101:8080 backup;5 f2 U% c4 c, a
}
9 x, q( n" g$ \0 F( i9 g2 @
5 n; r! p5 g. n- s% z& Rserver {
9 I }7 l x9 e$ i. Q3 a1 Elisten 80;2 T% ~% U/ E7 b+ \. Z! g Y; z1 E
server_name 52os.net www.52os.net;+ Z& b" Z. E0 s. K
2 L4 \1 ?7 r( u" Z l5 `location / {
! w' Y) \9 P# K1 M. r: b k) w1 L* F3 p ModSecurityEnabled on;
1 L3 P- S2 |' ^ ModSecurityConfig modsecurity.conf;
]8 J* l5 ~' L$ c% M: ^4 ?8 T4 W; {
- [/ s% r2 A9 I# b proxy_pass http://online;
# T u2 P& f( m; b: @. E: a+ D5 Q proxy_redirect off;$ \: S' d" i2 i! f; s
proxy_set_header Host $host;
. }9 W: D" B. H% l& x0 D1 A8 U+ w- ? proxy_set_header X-Real-IP $remote_addr;, G# m/ K. i2 X" W
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% q' u( ^6 M# c K4 e$ R }
3 @5 L& v6 Q( B, C3 `8 a}4 f- Z" v0 n- e4 a5 X
六.测试
' Y0 M) b3 I- G( P; R {4 L& Y
5 c8 c; D: l% e/ B+ C; e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ `; e( w) r# B, ~8 @4 B4 F' ?, G; ^& h2 [% S; h- w) L U
<?php
# u1 F) b! Q: ]/ k phpinfo();
. w: r& R9 f: t7 e. B: k% F?>) ~, T$ W! a6 a$ e W$ W
在浏览器中访问:) S- @5 M2 m% r' P4 t% W. e
# { a' P. F, e3 }* Z# w1 fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
W& u' k3 ?+ thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。, o9 T* l( {, `. Z- ^! F, ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ _/ G( z Y* a; A说明sql注入和xss已经被过滤了
+ n( B. z* _/ S/ U6 v7 a/ G" b. R6 x
七、安装过程中排错: u( Y3 y0 }$ I4 S
+ y$ r% P4 _: q( r0 o7 A1 \1.缺少APXS会报错$ q. B3 x! Z7 w& T' k8 d4 Z
9 o. Q; c8 V) _9 ~. | N
configure: looking for Apache module support via DSO through APXS
3 C, Y7 M' _ Mconfigure: error: couldn't find APXS8 U3 k# P6 D- a% g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) E0 w$ Z/ f0 W" b- q. D
解决方法:# E" R0 N, d" Q: F
6 s0 ?7 [5 w2 d4 n( S: n) K6 o
yum install httpd-devel
! h# f7 ^9 e* z( i6 H2.没有pcre
5 r, B! ]! q( c) F7 T3 U( o( x$ p- }& r& _, b1 u( u: A
configure: *** pcre library not found.
$ z2 P; O* z; L X9 h! U5 Pconfigure: error: pcre library is required0 |. K1 P: S. L, B
解决方法:6 f3 H. m* Q6 L" A0 \9 T# D
2 Z4 k0 X, y/ R& P2 `+ }" _8 Lyum install pcre pcre-devel: ]5 D. D' t7 p J
3.没有libxml25 b( o8 n- D% g2 ?$ v
* \) z! M6 _8 d7 ~2 ^( r6 {- Z: A) ~/ G$ r
configure: *** xml library not found.
& _3 i0 p/ n$ Kconfigure: error: libxml2 is required
4 Z; ~( d" y g; r% ?& }解决方法:
! Q+ ^8 o4 H1 ]6 l
7 d" i( K1 s. ^1 _yum install libxml2 libxml2-devel
2 I. m$ O" e% |7 \3 I6 Z4.执行 /opt/tengine/sbin/nginx -m 时有警告- t1 F5 u5 P& e2 x3 R
& O! H) ~) X6 F5 ^5 |Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 R: p8 Z9 L) `' i3 v# r; z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 ^* p/ c5 N7 J" E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 R/ G- X0 j# e$ W: J, c: q
$ y9 x% J: _1 c$ [4 J% T( [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! J E% _# Y5 B+ W6 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 \' v6 q* f8 }' ~9 a! l
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% ~; S$ S( i8 p! K; T! m) D, b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* P5 r" ^. r4 _. k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 \" X* |/ x9 } W5 E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
D3 q0 g9 S2 T( d# b+ q) ?解决方法,移除低版本的APR (1.3.9)0 ]8 ~2 O/ R: p9 J
; ]0 V; J$ p; p! Y; e
yum remove apr
) |7 D2 y# h% {+ S5.Error.log中有: Audit log: Failed to lock global mutex
, J3 w+ O' s" N/ G) H( v0 G8 \7 R) Z2 J7 L- ^- L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
6 F8 g1 `7 L% ]global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 t1 \2 R0 V* ?6 U0 s6 V5 Z$ r
解决方法:
0 n' O4 Z9 u0 S* P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 k" S/ D& o/ d# s
3 \$ H2 o6 @1 d$ Z
SecAuditLogDirMode 0777 k0 C8 j6 j+ i, g
SecAuditLogFileMode 0550, x7 ?# S% n' B! I8 U; K
SecAuditLogStorageDir /var/log/modsecurity
~1 b- h. F. R; O7 W$ o$ gSecAuditLogType Concurrent' E+ ]5 e9 E9 |4 ]; `
参考文章:0 i2 }$ ^7 Y: i4 A/ q5 W, W
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 f4 E+ ?, x# |& |" X2 e3 ~
http://drops.wooyun.org/tips/2614 |
|