|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。5 S) Z) s: y- ?
, h0 `8 \5 P+ k' o) S) v
一.准备工作
4 k. @9 i: w) W3 Y) _
& \2 u: x F9 G7 P& n& O1 V系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! Z8 A- Q+ D L2 A' r# W. ]/ s
0 j9 K$ {9 [+ X" z9 y Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 G0 R& i( P% z0 f
. k& I: ~5 {' h3 amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' ]- F! j" f+ g8 [: W
. ]8 X7 h& R" R3 y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ h. N% f( ] @8 Q1 N
$ V( s4 f3 Z% D: b# r依赖关系:; l2 e( a" b" l8 B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 T2 k0 Q' U8 W1 e# }( t; D
: _9 O- h4 q% x; E$ w1 ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# H4 y# e& I" cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
x, \" W1 P* k0 _. N' D) j0 |7 V. k3 X. H! ? W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" l! L, a! R% l5 \/ G4 c
二.启用standalone模块并编译% |+ x/ b; f$ A0 ~1 q' S
; C; S/ W( y$ c" Q0 V' @
下载modsecurity for nginx 解压,进入解压后目录执行:
7 { p* y) s W0 G3 N7 ^) [
" ]: Q) ?* e8 Z$ m, d7 {: ?./autogen.sh; D% q/ k0 K7 ^7 E9 T3 f1 O
./configure --enable-standalone-module --disable-mlogc$ c3 W. \ l+ M: t6 @: d
make ! V3 ~+ m+ j; Q7 s' [4 h3 i$ ]8 D
三.nginx添加modsecurity模块
0 e* ]& p! A( J3 u
, ?) \, }( I: }在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 O( b! v8 z2 o, X p0 y5 F" z6 S* b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
+ @9 U8 b6 r, g Z) r- ?make && make install
* Y( P* n( I$ U1 Z四.添加规则( _& G4 ~$ `: r; X+ ?/ @) S- S1 ~
6 \- J; [! @5 S/ G+ \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 |0 N$ Q$ \4 r; v: r) N, ], E7 D' L! ~/ j# r& N- j: `
1.下载OWASP规则:
; P5 f; h; ~2 W3 h) l! |) T" S. f2 j, U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" X2 D. z! o' [9 d; m
1 N; q* k2 A# Z* Z, Z% N& e
mv owasp-modsecurity-crs /opt/tengine/conf/$ G. ^" P: k- Y8 m2 e( ]
* \5 C- [' y- t
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 E% t1 K) Z3 N0 G$ o& W; U& I2 t z2.启用OWASP规则:9 ?$ Z- Q; { F& v+ x9 S' ]+ l/ I: ~
0 _' ^) l; J( r( Q! S4 ]( M
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 m! S! h/ k9 u
& b4 P0 g8 K1 x) k7 E$ ^ p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 p: m. Z6 W' M0 @
9 U* j- s* u0 B0 Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 o5 l: O; Q$ q5 a# Q) m: k2 K; Z" Z% J, K" V" F3 f; g# M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( G! f# G3 S% |# L; Y3 `& o" G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 J6 B. c3 A$ l# ]8 r8 ^* l; f. p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 T: u a( {1 A1 G; \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 l' ^1 [7 R( pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 |" F, @# P8 o% P) d2 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( @; m: K' s. WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) D1 q3 t2 I2 S; X6 _6 `五.配置nginx
& U) m4 s; X( n) b* A+ R: K1 m6 R- ^, r
在需要启用modsecurity的主机的location下面加入下面两行即可:
* m! Z' w, O6 S6 I/ A
- a% g, E+ g+ O0 N* oModSecurityEnabled on;
3 B9 j& p, ~5 d5 Y) U6 c, EModSecurityConfig modsecurity.conf;
. \4 N; z; r5 T: l/ m+ P8 }下面是两个示例配置,php虚拟主机:( U, m$ j N# s+ t
/ _0 [0 c$ C: L1 V( D% Tserver {
n/ R& }1 }: S/ X, v; | listen 80;' b* z$ U+ {/ v, l$ T' V5 ^
server_name 52os.net www.52os.net;
8 V& {) ~8 L8 t& n E& l/ F
) R d4 Y" S" H7 q2 _, z location ~ \.php$ {- k) O- X# Y5 z2 ^0 K5 k
ModSecurityEnabled on; 3 |3 M1 `" s& i* \, T" W/ Q% @
ModSecurityConfig modsecurity.conf;
( {; h0 K0 j& b" _
1 Q( o( y- _" N+ Q8 V root /web/wordpress;2 q. t9 u; `: @7 \- e# ~
index index.php index.html index.htm;- R0 Z+ i: J+ j' G& O( Y
' {) S# T" |4 ^- a( @$ l
fastcgi_pass 127.0.0.1:9000;' Q& ?4 ?* V7 ^, R: T. m; a
fastcgi_index index.php;
% g8 @+ K% q5 d) I$ J( ] fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; ]1 \+ R* n( |; l
include fastcgi_params;
5 X# ^0 H; U) H' V9 ?2 T" A }
, Y; q* [% p* {3 H( t ~ }
+ `- Y. h. N, ]2 Z) k* w, Supstream负载均衡:; g2 J8 k2 f5 ?: L& F. y. f
+ L8 J0 A9 x9 f$ {/ m9 [upstream 52os.net {- O. y/ T* {* [9 }6 D# g( v. Q
server 192.168.1.100:8080;( l8 t. o( C4 e4 R- N8 J7 U9 T& z3 T
server 192.168.1.101:8080 backup;$ d" f5 [; E9 I/ q
}
9 ]. @) b9 |, _4 T: p2 x
3 ?8 J+ ], l) V2 S' A, O+ s3 yserver {
& Z( ^' V+ V4 ~. u' n8 T: q# {, c5 ?: _listen 80;
/ E' G& D$ l5 O9 k( t0 u2 C: ~3 Iserver_name 52os.net www.52os.net;
6 U! ~" A9 R* j' G1 {5 e6 d5 d" f/ Y8 H' f: }+ I/ v
location / {
; P( [5 y v; w# k ModSecurityEnabled on;
) R3 e! g; W. o, J& ~# _+ l ModSecurityConfig modsecurity.conf; . l* n0 F6 N& E3 C' I7 m, w2 G3 M
! _5 z5 s9 S$ J6 t. n Z2 |/ r$ h
proxy_pass http://online;
1 o5 b, _% S- j- W% A0 e7 x, P! ? proxy_redirect off;
$ i8 h2 H/ r8 L. o: D3 \% G proxy_set_header Host $host;7 I, i8 e: Y- i& V: n! z
proxy_set_header X-Real-IP $remote_addr;* j7 E. e( b- g; H* \
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; G- G$ Z; H8 v2 e
}
/ g, F, T' `/ d! D) j# i}0 e# ]8 @! g; N
六.测试
; I0 Z3 W0 \+ y% G: X( D& I
! O( k$ m7 e0 ?# R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" j! V+ E) X: `! ?
8 U1 G h. A" c5 J<?php
" v# C& j6 d" Y) X% ]' _* ~4 X phpinfo();
; O2 d I& w- a: i D1 u?>
. I: [) `$ g# H# }* X {, h) v在浏览器中访问:
. M* r+ [$ z+ r* \+ G
6 K# p( h7 p) M# ]" z# Zhttp://www.52os.net/phpinfo.php?id=1 正常显示。5 r+ _: j3 ?- h: y7 S
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) }- P8 L+ _8 ~http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) D" ]4 c9 Q; W1 R; W, p; }" k9 z
说明sql注入和xss已经被过滤了7 p# p8 }; ?, k, @. R/ H; B
9 q# X! P4 Z1 n% [ ~5 \七、安装过程中排错) ?0 m2 `+ O% r+ z+ j
) i, f! L4 \1 b U; e
1.缺少APXS会报错
6 A9 |: G7 C N4 Q" l# s$ X4 _6 ]! a# H. O) e
configure: looking for Apache module support via DSO through APXS6 ^8 W' L6 k) N6 f% k [* w7 N% Q
configure: error: couldn't find APXS
& G$ s7 r; m+ r( d( K0 Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& m0 S; E1 B! J/ z解决方法:1 s& k5 D( e; ?6 {. D
% |! v) u* @) u) X' ^. S* |
yum install httpd-devel+ e, m& {4 J0 m1 f
2.没有pcre
: k8 n9 K/ A; ?6 m; W1 F8 o- q2 N2 b
configure: *** pcre library not found.
2 `/ g% [, p+ f |& z. X5 wconfigure: error: pcre library is required
4 k& B# A$ d" G3 ^5 T解决方法:. B, i# p9 i" C" _
$ T# L) T% {8 u& b. p9 ?# zyum install pcre pcre-devel" C9 Z0 h) P- t' d: J. Y" G
3.没有libxml2' n* q; s; o5 A7 B* ^7 H6 ]8 K* B
( ~) f8 D" A5 M/ P9 C( D
: \+ K1 q- w% P0 t2 Zconfigure: *** xml library not found.
( g. C) N2 ^# L, S9 U2 ?configure: error: libxml2 is required
7 v7 H. M7 d) G: I解决方法:
7 z$ @9 S- M8 L! Q5 }
; q1 m1 q/ D# Tyum install libxml2 libxml2-devel
) L! Z3 A2 Z: @& k X4.执行 /opt/tengine/sbin/nginx -m 时有警告
f7 h& q% x7 h+ w, L) u( r+ W0 G4 x. J) _' Q7 o5 D, F3 ~/ ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2). {- t" y! T" i1 g
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ @. R. O) c/ V/ S5 Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 H% E* y! I- h1 Z, m
) @9 l' ~1 C* |4 K5 A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- c) `( g7 M {/ S6 U4 { }! W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9", x# h( \) U' [- K$ ]: U
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 _+ E* D$ v& t7 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05", d% @* ~+ f% S: T' n3 h9 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ a) G0 b" b- `" o3 R" y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( |6 B) i, c5 g c/ m; v
解决方法,移除低版本的APR (1.3.9)
7 C, V$ p5 Y! k# m5 x1 j: g4 S+ q1 W8 H# @0 _: B/ Y7 R* Q9 B
yum remove apr
/ S' R. K+ @1 _' o; H/ G! D5.Error.log中有: Audit log: Failed to lock global mutex3 ~$ v# T. Q @. S1 J
: h$ K! h' @5 y" \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' V( Y7 ~" y" O% t6 V9 f6 p# _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 v5 ]" |4 V: a5 Y" [& J解决方法:
; _) E+ p7 C0 m" v5 E' M编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; M/ w" Q5 o: t5 C! @/ A; n$ U! j$ W" b/ [$ R% T5 e0 T
SecAuditLogDirMode 0777- X6 M' ^, P3 L# l( c
SecAuditLogFileMode 05500 M# f; h& g% }+ f3 ?
SecAuditLogStorageDir /var/log/modsecurity
9 ]1 V R. O4 w8 U }' M/ ASecAuditLogType Concurrent: N% [3 Q2 a# o X0 A/ k
参考文章:% k$ r& z6 ^- u; q `: a- ?# n
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% c' `! n+ g2 A: t. Qhttp://drops.wooyun.org/tips/2614 |
|