|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; r( F; t0 |; q+ F% H$ K" S
| {& H: W3 J3 N1 Z% u一.准备工作: N$ k4 q/ M9 @% n, X" i( x
. h/ }( k! v/ ?* @% a" |
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- U% I' D* L2 [! [9 W9 }$ o: q! A% V3 P# K- M& P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( ]' z5 c8 A. K$ y) m" T5 t, J C+ @ o" ]+ e
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 I+ ~' ~+ I3 L4 i4 V" p0 W8 m& U9 m6 }7 m) O- ]& v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( ~+ i* J% Y, _0 k# ?' [5 k& @3 a7 @( C) y
依赖关系:
' _' m4 x1 D: I( etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( ]8 Y0 _; H4 B7 i8 {( L$ j
; b' u/ G2 }; [; R- o4 ?) U8 W3 Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel3 y' c% K+ I5 q0 L$ E
modsecurty依赖的包:pcre httpd-devel libxml2 apr' t+ n8 T9 R$ M2 a$ H3 S
2 W% F1 I5 i! fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, d1 g; N5 z& }) g1 _, i- W2 Y
二.启用standalone模块并编译) ]& P a% h! ^( d1 P5 A) I1 I6 x
1 l [ J1 |. x
下载modsecurity for nginx 解压,进入解压后目录执行:
, ?- h" |% | b
, d; w6 D& D! i& E./autogen.sh
9 Z! R& C, f- H./configure --enable-standalone-module --disable-mlogc
/ p% O+ @: g3 [% ^make
5 ?% z$ q" K% @) i三.nginx添加modsecurity模块& y o! h# O6 l9 E% U/ L
2 A; B5 p* ]3 W, a: h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) _, D1 k9 W$ n% A H6 S* G& W
& |' j6 Q2 ^/ ?. S6 l. D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
( @/ Y3 m# G: s* D7 Imake && make install; l! ~, i" h3 ]. j( {! G6 {
四.添加规则
" b# O+ ?( ?* ?
3 ^1 q3 j4 D0 j, |! B5 Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ j2 R" e5 j1 h8 l7 Y5 z- V8 c
7 M4 V7 t* d% k3 p* k1.下载OWASP规则:
/ M B. J1 K; M4 I0 P$ p4 @( t7 V
4 t7 F" z( t" C7 O2 Vgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ `# o7 R0 B# }# H: a. w
2 G4 k' _* `& p s) Umv owasp-modsecurity-crs /opt/tengine/conf/
; d4 X5 H' O. P8 T1 ^, `1 T6 n5 e0 j% r4 i/ l: L2 @
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; l. A5 V% X+ p: g) s2.启用OWASP规则:7 [/ s4 @$ P- t2 V2 ~
; V9 Z: z( q( B" B
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( p+ K( m. r) _: `9 P) [
; e6 a8 o) V2 [: ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 M# }1 t' Y. W$ U. |. i% M+ P
- F, Z9 t$ D0 N: S w0 F' W% Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' G% e+ R3 L! _2 B/ R
. F7 K. b* O, [- FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 m0 O$ C) ?5 m( i1 W7 FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ U! O8 @: s& G. oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) f$ W, m% S1 k" X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 P4 r- v# S8 `- X6 p4 j9 cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! @4 @/ p7 T4 J2 x3 \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' y: o1 s! l5 f/ q/ Y' _+ W
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( x6 b( a) Y% o0 k# C0 v
五.配置nginx/ f* M1 s l/ U. h4 J1 I: J
% K+ w v5 h0 j# D2 p( o在需要启用modsecurity的主机的location下面加入下面两行即可:
& }0 ?4 T; B% ], W: D
! w+ V: P/ D1 e4 Y8 o2 L+ s+ _ModSecurityEnabled on;
1 z1 _& x8 t2 @- @1 @0 GModSecurityConfig modsecurity.conf;
/ m" }9 U) J. ~/ N0 P5 n& U- e1 H下面是两个示例配置,php虚拟主机:
$ i, }* G6 V4 a7 T& a% A3 R" A# I9 `: i4 ?% B: ?0 @/ G# o
server {
/ E5 o& b h& g7 p' C listen 80;9 D/ D' ^, K _( I8 V6 A- c e
server_name 52os.net www.52os.net;. m/ |( }* g) ]5 T# f2 e j9 U
# I- {. M9 y6 N" s7 n0 K( e location ~ \.php$ {6 n5 t$ R0 g u" }( f+ M- c7 L7 @8 |
ModSecurityEnabled on;
4 Y' p* |# }2 U! P ModSecurityConfig modsecurity.conf;
7 P' b: r* n. Z( K
! F% Z: I; n2 t8 W d! c1 f root /web/wordpress;
# c8 n. G: n u index index.php index.html index.htm;6 w0 B; ~5 e9 [+ i' f
& \- d3 z8 n7 H, b; Z* }9 | fastcgi_pass 127.0.0.1:9000;
3 e6 Y- a. C7 m$ o4 B fastcgi_index index.php;& p( K$ A' @! l
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 O& d' `! |- g Z8 A B
include fastcgi_params;
, w" S- \$ V3 L0 d* J- n1 F }
3 Z8 z: `2 d- l9 o6 q) u$ d1 V }, J+ T" n2 F+ ]* |. _
upstream负载均衡:
/ F" Y; g8 g+ ]; j8 D; C" W6 {+ }
upstream 52os.net {" m/ R2 X& a, Y
server 192.168.1.100:8080;3 a& D# ^( D, I) V+ z- V
server 192.168.1.101:8080 backup;
9 }& B" q- F, O, Q% e}4 x) x* x4 R3 \) v
3 b1 [6 z" ~3 A& Pserver {* |4 z; v# r* y) S2 K9 A
listen 80;: `. [2 ? ?7 S
server_name 52os.net www.52os.net;
. q4 v( l( v& ~0 X! |
. T' D- K9 o% V+ clocation / {- K% N" N5 q8 D; k0 D
ModSecurityEnabled on;
4 g" \3 C+ W+ Y ModSecurityConfig modsecurity.conf;
& X* A6 \, l( q+ Y6 J& v6 f0 O: i6 A+ \: w% H
proxy_pass http://online;$ J) \+ w" g6 g: l+ W
proxy_redirect off;
- |1 J/ a0 B% I$ }/ M; { proxy_set_header Host $host;9 u1 v9 ^. B2 x
proxy_set_header X-Real-IP $remote_addr;6 d8 P% ^! z- `6 X Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: u u5 l3 d# J x# {+ J
}
+ l5 l2 e' i( p, c9 E}
* M# `7 v, F6 Y y六.测试
. H% b; @6 r. ^# y& o E# O3 s7 V# j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" `* f& d! ]! M
( F2 ~* S$ V; V<?php! v; N" p5 e$ N; W( ?$ c
phpinfo();
( h* y9 n+ J; h$ _9 j- b( \7 J?> h, U+ y8 q6 r A' K
在浏览器中访问:
# u8 [* u8 d* Z( v2 ^
1 E% E7 y# m9 @ Y% D! Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" d9 D# o2 Z1 Z! C7 u/ p/ K. o. t# x7 ghttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
1 M9 w" G$ o* v: Ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。& R& s. w% H! Z+ Z5 J( V7 U+ L
说明sql注入和xss已经被过滤了
5 e4 i: @; G; Z; n- s1 @( c W1 z7 I! x: b
七、安装过程中排错
8 w, D" v/ s. ]8 _( b- X8 r* }' v
1.缺少APXS会报错
& w; r1 P S3 B4 o
* o0 T" ~* j, Y; Yconfigure: looking for Apache module support via DSO through APXS
& x" h6 h+ \$ z! D" mconfigure: error: couldn't find APXS- i9 N- {% |* v6 w: l
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& i- a2 z! n/ c1 v" ?
解决方法:- E2 Y" ^9 p$ G! s4 i
/ P. ~: J1 V% F
yum install httpd-devel' W% @* o3 P, _9 s2 O! Y7 p; \2 r" N
2.没有pcre6 K3 a2 t0 I* L9 Y$ [( R$ N
; ? ?% [5 |& j7 X7 k: I5 r2 I
configure: *** pcre library not found.4 G# s7 U2 _ E' n5 u
configure: error: pcre library is required$ c' _7 v1 U, N3 F3 f7 H" k- N
解决方法:: _3 C M; s( m# T9 {/ z
* v! G$ L3 L+ `3 D' s" Tyum install pcre pcre-devel
5 ?# b/ o6 I& P' X. Z9 E# ^) P3.没有libxml2
1 o2 l1 x. O% h4 o# \5 a; C& F% g, p7 h( O
7 C" K: g1 I1 w" q9 N2 s; f J
configure: *** xml library not found.( l8 J- H9 m5 i) r
configure: error: libxml2 is required
/ x4 j+ B8 D3 F/ v- v! @解决方法:1 s; x# |; _+ J0 C
( Y# C& g+ t9 ]
yum install libxml2 libxml2-devel
( f6 K4 h! _8 \; | f/ A# g' J) E; l4.执行 /opt/tengine/sbin/nginx -m 时有警告
. S& W. V* r, w l; O
* f ^" [0 C& f8 O: d6 `Tengine version: Tengine/2.1.0 (nginx/1.6.2)& ~ c z2 ]' i9 v9 ]
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" T& \" t y3 Q9 U8 k. v8 X$ V原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! O7 O" u* C, ]8 M2 h+ w
( M7 {: a' B0 B3 ]0 K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" ~5 T8 {8 z' c; X7 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
- ~+ q: f0 M: d* h! R. x) l2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) P' U T/ V% J; o# C$ q3 {7 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# O# q$ m3 |- [& z8 {# c4 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, u& m5 t5 M3 P' ~3 A' K1 Q; @2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." t* u4 d! F# L$ u `
解决方法,移除低版本的APR (1.3.9)& }. U- g# V# F* e
, W- h# p5 q0 k$ l$ T, N
yum remove apr
& I5 j0 h" j- _1 w& N* ?5.Error.log中有: Audit log: Failed to lock global mutex1 ]( S6 F: V/ b ^+ q9 E
X* ]: P W) _% t% V* ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 E- l' |4 J, iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- S/ L$ W! g8 J$ f/ Q解决方法:- ~* e+ I8 @0 X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 y( X3 X/ k) G3 _
5 N- T2 p, N: c- S. o( ^+ [7 hSecAuditLogDirMode 0777
' J( k9 d* A+ ~/ YSecAuditLogFileMode 0550& [8 K8 m/ G/ c$ ?2 r" \
SecAuditLogStorageDir /var/log/modsecurity: y D; ]% Z* g1 R: Y
SecAuditLogType Concurrent
! ~- h; ^0 D* d$ C$ z参考文章:! I2 S1 l% B0 v3 r, m/ Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ J) `+ [" W& c) `/ l, q! Y a8 ehttp://drops.wooyun.org/tips/2614 |
|