|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, \" y c% [& e# M, h
: [$ n% q' B2 R' J
一.准备工作7 p( k$ d' _+ i& k* o8 I
8 A. ]7 h, w7 p! v' A6 F3 x) }) p3 _系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ _ y0 E7 F0 P" J a; M X* H4 g% u. L) ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 C5 O3 Z* {' x6 B3 p N! G
$ A# J+ b) U2 _, f2 p Z# u9 fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 K4 N! ^" Z0 j9 F2 h6 q) J
, l" N+ b4 b; {* v6 A8 dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" i5 J! M2 y Q, W2 X( }' z( ]) O
2 }3 j3 {, O/ {) u
依赖关系:
* \4 `& }0 U. U6 B5 V1 l9 ~9 s: Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* [. T8 M0 d& k' u0 ?
% Y: G8 i. J: J9 ~3 Eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 f) z# Q/ X6 B6 u. v- a/ ]" [modsecurty依赖的包:pcre httpd-devel libxml2 apr( V; c- C g5 i+ p$ J
8 O: q$ i' |4 z; ^, ]1 jyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" r4 g3 z# a7 t
二.启用standalone模块并编译0 G8 Z0 F' j% ]3 Q; ?
) a: Q( B& H) ?( U下载modsecurity for nginx 解压,进入解压后目录执行:% t. i+ H( a, n' j2 F
. a- J/ H) W* r8 {$ n: d; B
./autogen.sh6 C) K" a2 p4 y& A3 [
./configure --enable-standalone-module --disable-mlogc9 S4 ^5 N$ B6 P: G' p% p
make
; r* @9 \; ^# S# d8 C7 U三.nginx添加modsecurity模块4 a7 b" c6 o L- w! n( B+ P
6 `8 I" t7 N) G$ b) f' N在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& l: G: ~2 R, q ]) h, Q6 l7 y: C) \& g# b9 Q0 j' Z: K3 d! e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% I5 q3 Y/ @0 p E2 q( f$ N
make && make install+ s. W+ \' G6 N4 @- y
四.添加规则
; j7 `7 v# ]# y; G3 |
) N& ^# \! ~# b+ s; Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& n o0 L6 `' o$ ~" l0 F( J
# `, w4 d1 L" J: [3 n$ |3 `! t7 u( H
1.下载OWASP规则:1 e I: C# C Y! b
8 L' O9 P" F4 K$ J1 O) J, s" ngit clone https://github.com/SpiderLabs/owasp-modsecurity-crs( K) I A1 p. [1 O
/ ~8 A6 g: H9 B. |4 h9 \( k1 E! L. @mv owasp-modsecurity-crs /opt/tengine/conf/
5 c; e9 G- l* ?/ x7 R+ i5 G7 @7 q3 c/ P, A* h6 B4 a2 b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* A2 y) u) v# K
2.启用OWASP规则:
$ E6 f' {* ^- }" K6 ^$ T
- T" [& {1 D' z% H! |复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ a$ d+ g3 R% P5 G. _1 E$ B) f8 @6 U' p; H, v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
f J2 n) `& p5 ?. t& k5 Z; y9 @% j. f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- i" n% v: [0 x% V) h( o
1 R' W% C6 h4 rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* D6 \ r2 ~8 }- E% i j/ ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! }8 ~, H5 j' U) S. ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; A4 H" O3 K0 R d0 ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ \% P3 n4 T0 ~2 S' N( B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ [, D0 G/ s* k$ V9 p- k5 ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ V, R7 p( T; ~- E# F* F
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ h5 ~/ }9 _1 W" _& d4 ]; g五.配置nginx3 F; ~/ ?2 I9 k9 C U8 y' W( h% b
1 d5 E( i& d( k
在需要启用modsecurity的主机的location下面加入下面两行即可:
1 @- b) G1 A, I
% U n' O( G3 Z( m: x" f9 ~ModSecurityEnabled on; , l; a" K6 a0 A7 A" e' W
ModSecurityConfig modsecurity.conf;1 E8 d9 _1 Z3 I6 N# e
下面是两个示例配置,php虚拟主机:- s. Z& ^& h$ v" X0 \4 s$ D
! g7 U' Q) B$ u* ~! r: K! W5 h5 w
server {' u, u% I* b1 p0 h+ b; p
listen 80;; O4 f" I3 E/ t9 O
server_name 52os.net www.52os.net;
- t! s$ D3 h6 }2 z- ]# m
$ r2 Q7 s% L9 c2 C0 a" J9 ? R7 c location ~ \.php$ {# f; w4 `" h- ^- ]: M3 c
ModSecurityEnabled on;
; x, v7 C7 u9 X3 b) ` ModSecurityConfig modsecurity.conf;
+ M a! k6 q1 ]1 o1 a( m
) ?2 g# m" C8 r( \' o7 z root /web/wordpress;. k, v; F$ O! i1 o! v8 g
index index.php index.html index.htm;
. _9 Z. W9 d5 T9 l, h M X . T O$ v4 y2 a1 L# h
fastcgi_pass 127.0.0.1:9000;- W, p' @4 g$ `; a2 I2 O
fastcgi_index index.php;4 j* X% H" P$ M, ~
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;* V; P" h. r, Q
include fastcgi_params;1 S6 F3 {) `2 l* L$ a1 z
}
. {! g% H* Z# [# ]! Q s X }
$ K. K5 a# W6 R) |# A+ R8 yupstream负载均衡:" s' E+ r* b/ L' o
6 n) R5 c. ]+ W- A. m" V! R
upstream 52os.net {- c1 c. @$ V& w
server 192.168.1.100:8080;
% K B. D. |9 v: d server 192.168.1.101:8080 backup;
- J. S8 K I8 a/ K1 N}
9 d2 s3 \) I' M y! {
0 z* r# h% ?# y) g9 i- Gserver {
6 b8 R( f8 z' @listen 80;
7 f, X1 z% ?9 _* g# ^5 f% Kserver_name 52os.net www.52os.net;: g4 w1 V& U* z+ K
/ O1 ~1 M; h. |2 I4 Llocation / {+ n# l9 S6 E" i0 v( z% ]
ModSecurityEnabled on; / y% f- C7 I- y2 _7 B
ModSecurityConfig modsecurity.conf; ( B' v }. e' T/ K7 O, U3 k1 g
8 z' c: ^9 M6 s% l$ m
proxy_pass http://online;
! ]7 `+ m$ \6 K; v6 ^: o1 W proxy_redirect off;
9 E0 c% Y, @$ B) C proxy_set_header Host $host;& k( h7 x, c. C- E" [4 M4 P
proxy_set_header X-Real-IP $remote_addr;% j7 s2 l5 m w8 G% Y0 q
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
0 D$ m/ {. F: [ }
1 `8 X: O# O5 u/ B}5 d# ?8 h0 n4 K, L
六.测试' B) a; V- f$ ?/ h. Q! A7 f& l
. k; p2 }4 i8 V( V+ K6 L' I2 Q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ x7 v8 Z+ s* r/ I! q0 W( J# e
# C* S: l {" A f7 w
<?php
! q) |- l7 h' o phpinfo(); , {% J1 a' K: m& ^
?>
) m- C( x [# Z$ w7 N) |在浏览器中访问:
) w h0 f" ?" C0 k. X- ?, ^# v0 E9 }& I* f2 c
http://www.52os.net/phpinfo.php?id=1 正常显示。
! U u; J% O, b! Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
/ M- u8 }% N- H8 `7 [9 v6 |. phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 ? E; c3 O( ]1 [+ I3 z说明sql注入和xss已经被过滤了
+ r$ y, V1 Q3 s4 i3 f& k3 M3 S4 n) @7 w- n7 }* Y' y. a2 @4 S
七、安装过程中排错+ Q0 w+ }3 H- Q7 w" i+ n' G, r. Y
( d/ o$ T1 ~. i- [" o9 i
1.缺少APXS会报错, W+ |% W$ s4 b7 w/ ^9 J0 Y0 c
* j) a5 y5 X" A& `4 N7 Oconfigure: looking for Apache module support via DSO through APXS
1 R: V) ~! ^: g8 {, nconfigure: error: couldn't find APXS8 g j3 C ~' |; q# v
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 b5 e+ i; H0 j/ J
解决方法:& c- q& n% E0 `6 L1 b* _
& W5 i" `+ B8 ?" R0 g0 byum install httpd-devel
- f6 L+ Y5 z- `5 o( S( u7 {2.没有pcre
) i& P5 A3 R G4 ^+ e7 m7 w& @9 X
% R7 v0 w8 Q; e' q! t0 J2 `configure: *** pcre library not found. k* h1 I, W }; R
configure: error: pcre library is required
! v6 z; T- I+ w( D8 K' r解决方法:
- Y0 f' |4 {" }0 \- i
d$ l, M/ W: o1 @/ i1 W! Eyum install pcre pcre-devel. x" w3 n% H2 p+ t5 G& d! [
3.没有libxml2
, D& r' U# Z% N' R
8 P* [% X: c& \ h- o& ]( x% J( u
% k9 L8 ^! \+ \8 J7 D- V, U/ @9 p, wconfigure: *** xml library not found.
- R7 R/ X1 m9 d; W& [5 i+ qconfigure: error: libxml2 is required
- C/ \# v; T" ^* X6 O1 M, L8 R解决方法:
9 G' B: V8 h/ x$ K' k# A/ Q/ D/ F- M! ]8 g# [; P% {
yum install libxml2 libxml2-devel$ y& Y5 s" k/ g! r! z! S
4.执行 /opt/tengine/sbin/nginx -m 时有警告- V0 i- u( \& k* Q( {8 n2 Y
+ U; l& F0 o" C3 L! `Tengine version: Tengine/2.1.0 (nginx/1.6.2)- z. S, `: X! J% ]" B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 N3 p5 M& c& D& R1 p" @* X8 S# g( G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 d- R; t% L$ C" b( P2 Q% }6 T, S# j1 R }" c) H9 w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ n8 p! a3 e+ q% g G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": I9 t$ t* R. y6 f
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% R( e5 ^( F9 L& L& _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 M% n+ T' X6 ?! Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' b1 Q4 B S. c3 q& ~$ \! I1 f% D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ X% b v4 h/ B& _( q. ?1 }! K6 B解决方法,移除低版本的APR (1.3.9): L0 C6 j" m. h$ ~! o1 R4 m
. ~. S0 M$ K' r2 A' k# r
yum remove apr
* [( X' H8 F; }% Q; O( r5.Error.log中有: Audit log: Failed to lock global mutex
6 Z7 c% @, p; z
# J7 ?8 @9 K8 l& M# Z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) n% N/ ?9 C; o4 ~8 ]) Yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 i4 L2 ^+ y" d0 v# K
解决方法:" s& u6 U' Z: I# O' j1 t8 H
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 j9 k( T8 U" K$ ~* @8 V% L
# H. ?6 G, s9 h# } ~3 R2 r) |% \SecAuditLogDirMode 0777
/ y; G9 o" x# k1 D- [, [: h6 q0 HSecAuditLogFileMode 0550
! z4 d: z* H6 c9 A! ?SecAuditLogStorageDir /var/log/modsecurity8 q; z9 G( o; H& v5 }
SecAuditLogType Concurrent
1 ]: V3 Y! h: p0 h1 y! Z参考文章:
) M+ f. N& H8 [5 @https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, p3 Z" O) _* Y& n' rhttp://drops.wooyun.org/tips/2614 |
|