|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ O1 [% }& O# E9 w$ Q; R
" P0 |+ l! v! {1 |- D# `% V" g一.准备工作
: @& j1 E# E5 `/ D6 o8 F. O
4 }7 r% ]7 W* B5 `2 H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! C2 q: m3 T8 }; M% J: |% E5 _" E! w2 O
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: ]) l; m& x) M1 z* b$ Y" x8 ~5 ^ H& E3 ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' i$ b3 i- m6 I! ]) \# q0 {
' D, b( O' r* X! ]( ? g l+ O: ]% [3 `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 {+ t3 C) S @# z2 t! E% G: }9 \7 ^, [
依赖关系:$ ~3 ^/ z/ M4 S, `! H: t
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; _) X9 j. [0 K! D2 \: F! U9 T6 _% \7 @( V) i- g% K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% p! t" u8 F3 e$ |7 ^: }modsecurty依赖的包:pcre httpd-devel libxml2 apr) n3 k) k2 G( k" x
. G7 g# V+ W! u
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel$ x/ J+ {9 _" U# w9 g# L6 k: ?
二.启用standalone模块并编译! r7 D# m T& U( t2 W
$ S5 ]/ D* W' k/ x1 l# f v
下载modsecurity for nginx 解压,进入解压后目录执行:
5 H8 ^& r% B* A: J9 B) |% z" e# d" c+ n; G
./autogen.sh, K* e2 ]% A) Y5 @1 h
./configure --enable-standalone-module --disable-mlogc
( y7 y; G* w) L; W# ^ s: Pmake
4 q& A1 N' v4 `1 u0 }. O0 o# F三.nginx添加modsecurity模块& F, t* ]+ U. w1 J* i5 s
; _! R2 n5 F$ e! B& |) I& a7 ?0 a9 Y" v
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; Q3 E/ R& F) y# Q5 ?- Q' q/ z5 V* N
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# O. O/ \) V" o7 B* z3 h; o; G
make && make install
6 v" R. x) S" Q四.添加规则/ E3 C, h6 n! n9 Z9 \; }
# r9 }2 K# c2 r/ S; B' A& Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. A, @6 Q% {3 M Z
* R+ j4 s6 [0 F1.下载OWASP规则:/ I8 ]+ p5 @. [8 n3 V8 `4 F
5 Q' U# ^2 t, _) ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& b" u: S( `, l' [2 s* k: e* {
" L& _" ~% n) E/ [- K
mv owasp-modsecurity-crs /opt/tengine/conf/
+ H8 T$ ^$ C7 w8 g" v, W5 e( y
9 e+ v; I% L/ A. X, W2 ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 C8 m8 ?9 U1 l( U: k( d2.启用OWASP规则:7 o+ t2 p6 Z; L$ v! B8 D: e
e/ Q- X% R9 Q8 F' p* t% ^复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' U1 g. _2 l* s- p" P; D7 \ e! e' P# V: F; `
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& S8 X: O) }" A) B7 E: g) D6 z. `1 l! Z7 I2 v' }! }
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# G0 Y# Z8 _% c$ \6 s6 y0 ~: n* i q1 e2 j# y8 c J
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 C- d# c: t" z' n2 i; x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# @; j% o* B. S) s! KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, N p) f. }+ ]. E2 h3 E5 @, X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf u& @% h: ^1 o/ p% g/ T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ o" ?$ X J E+ R: V
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; X4 s3 C R: Y9 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ F# P9 v. t- s8 B3 {6 G- s
五.配置nginx; m- M6 b! h: C7 H7 _+ Q# {6 l
' \5 Z$ f" b$ P% u5 e" S$ F在需要启用modsecurity的主机的location下面加入下面两行即可:
% q+ x$ U9 {/ y2 V. S/ }9 r7 ?4 s: C0 L
ModSecurityEnabled on;
: Q9 v& [/ [8 D8 B" p7 GModSecurityConfig modsecurity.conf;
9 F/ @( n( z. V( n) _下面是两个示例配置,php虚拟主机:
6 u* w/ z6 v( ]' A- I& w
( ^! A- u+ F) V* Q T! V; fserver {
3 A/ _2 i, ^9 C+ ? listen 80;
1 C, Z v: R+ r& Q j server_name 52os.net www.52os.net;5 r" d5 @% l2 O8 U( j& q
% n: t0 V- d$ r: A
location ~ \.php$ {% G/ v; V0 {. u9 F# n+ E4 ~7 y
ModSecurityEnabled on;
* N$ `" C- T; y4 d2 D f( Y ModSecurityConfig modsecurity.conf; x3 [7 A+ C, v2 _$ ?% `* A
( o8 o$ R5 K- d' b) C, M3 b
root /web/wordpress;
, X, N! @" H: V: U \) e& }0 \* V index index.php index.html index.htm;* l4 ^. q' J* P) y* i t
/ ]4 i# p& T& T* @) ^ fastcgi_pass 127.0.0.1:9000;
9 N1 l/ L( j. J- J7 m9 n fastcgi_index index.php;
' t; Y8 r+ `2 k" a fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, p$ U, H2 s4 V4 a7 h% N- n include fastcgi_params;
" D9 }, O. r. R) t1 Z }9 e9 a" }- A; `: |
}
, |: N& ` \( Q9 D* ]% p+ iupstream负载均衡:8 o% ~. o0 Z4 O8 r
: g( x3 R& Z4 j2 w: _0 Q
upstream 52os.net {3 q ^* O( O% x
server 192.168.1.100:8080;
' M6 M- h% n% {+ J$ v1 b, x1 } server 192.168.1.101:8080 backup;
' \8 J: Z0 A1 z t: H- s$ T2 l' E}4 n9 e& v7 H- e) @: Q
3 k& T: ?5 \: U& r6 l) u6 iserver {
/ s! R0 y5 |+ p' z# I5 W! Alisten 80;+ z% t& F1 u" v2 p
server_name 52os.net www.52os.net;
8 i5 ?$ }+ d# n" ]" _* m
7 w$ _. i3 P0 T) J9 ulocation / {
, ]+ ?7 o& i6 a6 d2 G7 t0 v6 o ModSecurityEnabled on; 2 m' w. q+ K$ {8 s- T( E
ModSecurityConfig modsecurity.conf; " Z6 B. E Y5 _/ d4 M: t1 i. V
' U: D# ~9 N+ \% H5 a& c) F, Q5 F proxy_pass http://online;; G0 g/ s5 S+ Y1 u/ K! D3 S
proxy_redirect off;
# V" K: d( D+ A proxy_set_header Host $host;* d5 i$ a+ \# W6 ~
proxy_set_header X-Real-IP $remote_addr;, ]% Y) D6 X9 `
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" c3 |* X( L. n }
1 J3 N+ A$ Z: u! S5 z5 e}
. C% y2 v0 g2 H) ]六.测试" j- |- T! l! W; S5 v/ R
& _8 q! z$ h, M; D! Y. [
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 `* l0 X% f: @7 B) U
: t) U; Y+ `. }6 M<?php
% N; J2 o# Q8 b9 P: y# q phpinfo();
1 ^$ u3 Z7 R4 E5 @?>
9 U+ H. V- t' Q5 H$ R在浏览器中访问: Z7 U5 q3 @* Y
( ^+ O3 g8 Y. f6 I# u5 {http://www.52os.net/phpinfo.php?id=1 正常显示。) ]. V e" |' z. I3 i" H
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' I$ L3 Y# u* n9 Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
' O4 H4 y0 R5 d6 H1 u' X+ y说明sql注入和xss已经被过滤了! I7 ?5 G$ P; y
3 y& Y, S' {1 u4 ~7 [6 G七、安装过程中排错
0 ^; r2 l6 C8 J# G [% ^( J
- g0 G. ~/ ?+ h' h1.缺少APXS会报错
8 u; b1 \$ F% ?! ^) k5 j3 V
; C1 q: `- ]4 H, R: bconfigure: looking for Apache module support via DSO through APXS( y( R4 j. o% _1 `% [
configure: error: couldn't find APXS
$ {# N6 F0 {8 W) a) d3 h! W* }, _7 mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" g8 n: m4 Z2 ~0 I- d解决方法:
! H8 l: `1 n+ C4 v- A* s2 ^
: }7 H5 R# X. a: ^1 |$ J: D! I: h0 Lyum install httpd-devel
( ^3 \2 R# E/ i( b9 x/ I4 l2.没有pcre
9 a, i4 G3 z3 F) Q3 K" O) r0 j. C& J0 H
configure: *** pcre library not found.- c6 J K# ]# t# c
configure: error: pcre library is required
' I7 d$ ~7 R" u( Y解决方法:+ K0 i, `3 I/ X. L4 }( M4 ? e
# h; L9 f, C! l) }1 V! G7 W- L
yum install pcre pcre-devel
" v8 k9 p1 L7 i! L2 A3.没有libxml26 ^1 Q4 |& H. p$ T6 w
& L. r6 U0 \' R4 i2 \5 i1 `/ a2 @$ M9 `9 L' o
configure: *** xml library not found.5 ~6 S4 Q9 ?; U5 c3 w: }
configure: error: libxml2 is required% h8 b# X+ b0 J5 f! p
解决方法:
# x5 U7 ?( d' ^8 J2 D7 Z) A" t! W* O2 W6 m& _4 A0 d
yum install libxml2 libxml2-devel
9 p# a3 D$ R7 n( ~' Q/ w* {' M! ?* n4.执行 /opt/tengine/sbin/nginx -m 时有警告
& E% p: j3 k0 K" S: Y
( x, t+ i7 b5 l1 o- MTengine version: Tengine/2.1.0 (nginx/1.6.2)/ g: ^3 o9 d7 _7 n8 M: U& S+ z# K5 R& ^
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 {7 G& _6 J1 a$ K6 \原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 ?3 _1 n3 }, O$ l- ^2 t5 _/ B( h+ A+ m3 S) r3 F$ O. l$ I5 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& y J( S8 V, f( X' B5 {/ T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 U! K2 {) p: t! O" U3 T2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 _: J5 ]& s$ h3 k1 W1 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& H) ?# P) r, `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% U& l. @7 M0 W1 U ~
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, X/ S& A& h& }6 n k解决方法,移除低版本的APR (1.3.9)
: G3 E2 C& N8 G0 {/ v t
% Y5 f# w# I4 G# A1 f- z |yum remove apr" B/ F1 s+ q1 J' _' c! E) l
5.Error.log中有: Audit log: Failed to lock global mutex% s" k0 E2 r0 x( D( y
5 q1 b: k. `( p4 Y4 {+ A y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
( S, y, W- f1 L( ?8 K- z+ @4 Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- L1 c* u' k" z
解决方法:7 G0 A+ w% F X0 J* j$ h
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" W) Y) U, B( _9 |+ K$ d- m' `+ L% K
SecAuditLogDirMode 0777
' U7 X' i/ T4 JSecAuditLogFileMode 0550
' D6 z6 z5 m( [5 X2 P# [SecAuditLogStorageDir /var/log/modsecurity3 E7 r" N$ ~5 s" K0 a: |
SecAuditLogType Concurrent" d3 g% j, |9 T9 F' h* Q
参考文章:0 N: I) M( C+ s {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ b: v# @( e1 F& I& d+ E. {http://drops.wooyun.org/tips/2614 |
|