|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' p9 c9 K7 J9 r& ~ H1 ?: H, F8 h" I/ E P$ w$ @& ]0 h
一.准备工作/ l( w: f& J& @! t
$ F2 |6 O9 |3 ~6 |- R m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( j$ J/ e+ ]0 T7 W% [! B. V$ E2 Y* V+ w, i/ ?7 K2 L
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 H+ Y: m% T- M7 i, V3 L
- }& ?7 u5 S; [) B& w* t7 Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ A9 j( G8 R$ o- y! a
9 r9 O# [7 h: a9 s* N5 g3 E N% VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& e! h V& Y; K3 t
3 ]7 h4 k; |1 L5 ~) _, [
依赖关系:
7 h6 G. R! W; X0 N0 |: Y& `tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& G" @9 w) h- r0 H
2 D) N) x/ F8 B7 P4 Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ T6 T3 a9 f: D4 \3 |
modsecurty依赖的包:pcre httpd-devel libxml2 apr
) o0 O, W, M0 }5 k0 f. z
0 i# r" s$ p" w* Y, Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ U; ?1 D; T" h0 K J8 P$ b二.启用standalone模块并编译2 H, o& F& ~* y- V' f+ j
3 j, a. z \; {* o4 _; I
下载modsecurity for nginx 解压,进入解压后目录执行:
) B/ |+ F/ T. X/ @# A" M* b' [" X. Q4 m2 m, s
./autogen.sh
3 r& @9 @" b/ @" z( U, U' B& u./configure --enable-standalone-module --disable-mlogc
- e( z3 P0 L- G3 B! L. @2 h( L, tmake 6 q, e; w1 m) L. g9 q8 n
三.nginx添加modsecurity模块( W3 h0 f8 b2 p! [
4 L7 d# T5 z3 A6 O0 r
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' c( t& Y: x {$ s: l o9 i% e- b
8 D- u, C# ^+ p6 K6 ?* m./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
. q% v0 ^# Y' o4 ]' |make && make install
" ^! z; |% {5 A6 r0 e& O% n. S四.添加规则7 B0 ?$ ]$ ~$ _ ^0 _0 a
/ @& w& E2 [& e0 imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 ^; c0 ~' G `! z% ^5 i c c# ]" }; k5 P
1.下载OWASP规则:
) I0 Y0 b6 N9 G( J' r: d0 p+ {; H0 D7 v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs) z O i6 Z. _3 w2 N" K) m
! u, c; G5 X' Z6 W6 @3 V! A! i
mv owasp-modsecurity-crs /opt/tengine/conf/
4 ` z [/ D& l3 q; T5 m
' p7 j% ]- D) K8 mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
7 m2 s/ F4 Y4 G7 X* W2.启用OWASP规则:7 E" ^/ G6 G' U- t4 Q$ Q% z
% k U) B1 P" u H7 J; r0 E4 C复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) n. M1 Z1 g4 |7 z- A0 g' Q6 L1 q i8 H1 P$ G. k; ], s$ h
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ d {5 V5 q1 f6 Y
- v, U' j3 s3 X
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
( j$ g9 s! ?! d. T# c2 G
* ]( }, A. {5 e# cInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 W2 ?' D% Q6 J9 p' ~* c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( Z. i" b5 H6 S. E, K& ~+ [, iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 H+ u! P! p1 N# K A# _. I' e+ c% aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& ^$ |7 p2 h/ m. |) S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; s$ @' a9 [9 L" R9 r( w9 IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 x# S5 ?1 Y& p1 A* |1 h$ fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 _: q7 i- S+ I0 `9 w
五.配置nginx" a2 _# p+ q3 Q
v' U1 L, A& ^+ e! }# v) f) {
在需要启用modsecurity的主机的location下面加入下面两行即可:% k0 T7 z# ^- |0 K
8 H Z5 [8 K( r1 G
ModSecurityEnabled on;
& @6 e7 q6 d4 j, tModSecurityConfig modsecurity.conf;
, s9 |7 e! ]; I* J% v* U/ v下面是两个示例配置,php虚拟主机:
: k0 h) |0 S O
' X6 @1 \% P$ @% E& S6 H$ l( cserver {6 @) K' Z* j/ O$ l' W
listen 80;
( `9 o$ K/ }; Z1 r4 ]# e" v% Z server_name 52os.net www.52os.net;* U+ D' N) }2 u( i, l* h
6 t+ u1 Y! C- C0 v: D7 ~ location ~ \.php$ {$ ~2 m% X# W- i
ModSecurityEnabled on;
8 g- `- F5 ]) ^4 r5 j0 g. V0 d$ l ModSecurityConfig modsecurity.conf;
* D3 k/ S2 h1 B& b. H; I# f8 S
8 }/ Y5 q& [# z+ f, j root /web/wordpress;
) ~! J( s/ R6 i index index.php index.html index.htm;
' f$ z7 u) U& `2 V" Z) S5 o , x+ S8 X0 s, e2 B, s
fastcgi_pass 127.0.0.1:9000;
8 p* E6 W. d7 {4 f" q fastcgi_index index.php;
7 V' ]. {3 `8 N& l7 ^' @ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ ]0 m/ Y; u- a! A; S
include fastcgi_params;
n5 b3 Z4 f% c( h' Q( S( x. v }
! w l/ _ E- z1 p% k }9 v4 D* J" U' E2 Q0 E: u
upstream负载均衡:# I6 h5 K) a0 P+ w* }+ y& @, h8 x
; q( n7 K% W& w" ^4 }: T. \upstream 52os.net {
k( W% l' l r server 192.168.1.100:8080;: i$ B# U! r5 S2 C6 y& a
server 192.168.1.101:8080 backup;% B% v/ |1 y. T2 D( X9 J
}
& a0 P* K* @" W ]! N% T
% Q2 m+ v. v+ N: ~8 R, s) mserver {
7 a# q) {: F3 v# s2 Blisten 80;
0 d# _ h" u" f! n1 o8 gserver_name 52os.net www.52os.net;% N0 F2 S& ?: H# _$ c+ A
- J6 \. Z9 l; q& q# nlocation / {; L* v! V) I$ {) [
ModSecurityEnabled on;
( S7 @0 Q) s2 O/ Q, J ModSecurityConfig modsecurity.conf;
+ e! D9 \8 N8 C* n Z# U
" l9 p( G0 v Z/ N; f8 o* d* i proxy_pass http://online;
* a$ C0 q" M- `* D* p) \! ^2 d proxy_redirect off;! N, P( Q5 ?4 K$ o7 c+ f$ G' j3 l
proxy_set_header Host $host;& ]# e4 N, j9 V' u
proxy_set_header X-Real-IP $remote_addr;
( f) k0 ]! ?3 } proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 `7 W+ u, Y( ^# T: w0 j
}
) U8 ~( q. E" K' H}
, g+ l( e. \: [六.测试
1 j8 G# X5 a3 U" f) ^. a$ Z: B$ [( [: s( q/ \# g! j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ U( }4 K t' J, L- U8 \8 u8 b7 K- W% h9 t0 t
<?php
B. I5 C, a7 I4 _7 B. h; m+ E phpinfo(); . g1 Z7 g1 O% `
?>
8 Y5 T& ~8 q# {& G0 d/ ~* N4 o在浏览器中访问:6 K7 e, _3 y; ~% d7 f4 G
* Z( p2 }7 M" H1 g* E: h% [
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ d* ]5 G+ S, b- ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 W# b* C$ Q4 E8 E' q1 x, Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。7 ~! X- ?+ r3 \& y1 k2 U4 a
说明sql注入和xss已经被过滤了+ q$ l5 Q& V" w3 Y( @$ H3 D2 M& E
$ Z! l9 m/ n: _0 ~4 X
七、安装过程中排错9 [. r2 L3 x& J1 J% ]! w
! C3 l4 d. S) O `. W1 R7 v- d1.缺少APXS会报错
2 q1 }% S% C, X. v7 g7 J" ~! A# ^3 m. e! p- ^* L
configure: looking for Apache module support via DSO through APXS
8 v, J4 A' K7 Qconfigure: error: couldn't find APXS5 j; }* R6 Z4 ]4 H. [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* n1 N' I0 u& ~- ~1 i解决方法:" ^* y3 i6 _! N5 X2 o
+ P" O% p( Q* J/ Y- R; g
yum install httpd-devel
, c5 Y8 Q: ]! ]3 _4 ~$ s; y2.没有pcre
X! |; d4 g5 L7 b( w4 m) K: R& J
configure: *** pcre library not found.! x- m: D% I$ b4 C) `4 z
configure: error: pcre library is required& m& A ~( A+ O
解决方法:) d+ o' r5 q7 `! G
0 Z @& T* r4 ~4 d ^* E# T" S5 w V
yum install pcre pcre-devel
; e4 }: r; ] J( C E4 L: e3.没有libxml26 L- p+ i2 ?0 J. `8 s( n; z
0 J M2 C! o! h: D: D% F7 c
9 f7 {: e% E5 }5 Q9 Tconfigure: *** xml library not found.4 m& {- C& f' U, J
configure: error: libxml2 is required
! S9 f/ s9 r9 L) M L解决方法:
/ [4 W1 W# m: {2 {/ z' B
' }7 _" ]4 u" p& l* Z/ s. ]yum install libxml2 libxml2-devel
1 U- L% W1 Q8 R2 T4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 r* T7 m( S/ d; \5 }: s5 S7 y0 C+ o, i" e. ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2). J# R9 T: r/ q. `, T/ ]- R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: |# z- C3 _& W' \, t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ @2 C k7 q# g$ Y3 f9 R
, `0 s% n l- c3 E0 d* h& [, f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; N5 O$ z: b" Z5 s! x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ X# t/ c. g: ]. W0 M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 u+ e! ]/ [8 L9 h/ G* t) \) ^9 x5 {4 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ D, h" Z, V3 M2 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". i* ?$ @' J5 }; y2 ]5 v G* R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 b3 J& n m; y2 F- X. Q9 z! a
解决方法,移除低版本的APR (1.3.9)8 C) X' A$ T* C4 X7 D ~; n
9 |4 |: A: t# N2 L7 u4 h, S3 _9 q' `
yum remove apr
4 d4 i/ W0 ?* D' H5 w- g5.Error.log中有: Audit log: Failed to lock global mutex
- w7 l8 k: u* \* M1 R
$ O% S& p# c* A) h( ^4 t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 M" n% O+ b1 E4 G" X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! f7 N( L9 }$ f3 E; C
解决方法:
- l `! W1 R9 ~9 r! \+ N0 g编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ l/ |1 b" e* r5 O' Q
2 @- d5 ]1 E( |6 FSecAuditLogDirMode 07777 O4 E7 e3 x% ?! }* C5 Y+ ]5 d* z& G+ U
SecAuditLogFileMode 0550" j4 b9 ?6 J7 T7 K2 V$ s5 v3 F
SecAuditLogStorageDir /var/log/modsecurity4 P" i. C! V# c5 E0 p% p
SecAuditLogType Concurrent; x1 r$ e8 R+ z8 O1 O1 ?# X
参考文章:5 C/ x/ g7 [6 V/ I5 \2 J. A x& y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 B8 C/ g9 b! d. V
http://drops.wooyun.org/tips/2614 |
|