|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。) I$ V' W( ^9 Z/ t3 B5 j# W: H1 Q0 e
0 b+ u2 Z2 ]5 @( d% V) j) X
一.准备工作
7 C9 {4 ~. o* o' q5 h! j( E* A' C. x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( b& A& {( d/ V- F- P
4 p( C) b' E) ^. D2 P+ Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 u9 N8 P- V0 a- O2 l6 P! H
/ I v" L2 |' B* p5 k5 {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; Q' M6 T$ x2 ]4 i. V" J a; x
* B0 g) W6 O9 ^ _4 n( v6 K, BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 g* F+ |$ z0 A( j b- v
( M2 y# b; e+ S$ l2 T- Q& ], Z5 C依赖关系:
8 e, l! y0 H: J' i, N1 g1 [: V* _tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
Z* Z5 F* g6 y% R' I# x$ S2 u+ P0 e* o
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ n- Q: A! [" n; [0 G: T6 z8 ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 k+ Z* `4 a, @6 f) r) C9 S
7 J3 H$ f* x0 m+ T) ?5 Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel4 H2 ^) {! M! W* ?% T) I
二.启用standalone模块并编译
$ S: N# U6 L9 K. a, `- E: ^
2 H- a) h$ |- ]2 J/ O, Q) E: ]" |下载modsecurity for nginx 解压,进入解压后目录执行:
4 F K3 [. a l- a2 |* F( l! n- p% z2 o: U( l
./autogen.sh
0 F3 C( m7 K, T! f9 D./configure --enable-standalone-module --disable-mlogc
7 m2 e# t$ S' j# Jmake & {* r% G2 v% r5 ?% |8 l1 \5 J5 x, \; [% W( ]
三.nginx添加modsecurity模块, r% I- Q4 L, S& v# G' q, m; y
8 o$ G) J! }" i$ v. I
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ c! L! L# W! I4 Z4 K- ]% o
: w" t8 o& Z' ]$ @; I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ ~: ~$ U: O/ S" |
make && make install3 g! x G! w/ v3 R3 t
四.添加规则. ?' P) `8 B0 A, G+ y
9 @2 K8 P H9 ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 ?8 G$ l, `: Q, f1 d& U
- z6 ~. l7 J6 A9 H6 B
1.下载OWASP规则: V3 g6 m2 k0 y
$ \" ]2 x, S6 w) F# Kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, _, t6 d: j. V7 a
2 q: y5 }! s1 b; c5 z
mv owasp-modsecurity-crs /opt/tengine/conf/
, r2 [' s7 \; W' u+ Q* r' [
. O5 v' J& o) Acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: y9 _* m! c9 K6 X
2.启用OWASP规则:* j8 s: {+ c/ y5 q- N
2 C" t1 e, i" P. L" B/ m+ N& ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* x3 n) s7 u- Z; g- \
' g$ s6 f2 m& J- @9 l$ @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 i: Q9 r& y2 o: x$ b
4 Z$ {( H4 f' R6 Z# {% O c/ {! Aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ K! M: b' F4 v+ t7 q* w
) ^: p* k. h1 w' \3 F
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) e9 S( C3 H' CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. N. V3 f! ~' `5 z/ y6 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* w2 K- h- [: o3 o/ v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf V. l8 U1 A5 X+ X) x4 K1 _! B5 \+ }0 _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 x4 R8 w4 p" Y" S' W# `8 KInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ _2 X2 \, L6 @. y U* {
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ c1 l8 B! d* t7 \. Q/ a五.配置nginx
T9 I/ \0 ` l9 k G) a6 n
, J" r% P- D; C# q6 e/ k, a5 C& B. Z在需要启用modsecurity的主机的location下面加入下面两行即可:
/ Z- M( n, _% ^8 T8 Y8 P* s
# Y/ h& L( M8 x; @1 ~' m4 `% ^+ _8 w' _ModSecurityEnabled on;
# F9 ~1 g. d% l, D0 @# y( vModSecurityConfig modsecurity.conf;) r+ ]" q9 `- X/ n" u v
下面是两个示例配置,php虚拟主机:8 d+ @4 d0 O8 B, W
8 K0 ^! {' T+ B) w% Z# }5 k
server {* u& A; P7 V; s" V+ A
listen 80;: ^) L$ D, P$ O5 T
server_name 52os.net www.52os.net;) X Y& i' G/ [
1 W# ~& Z* E1 E1 g* N2 j( c
location ~ \.php$ {: B4 X) G1 d; C, C1 I# f: @
ModSecurityEnabled on;
+ @ J0 H, R" ]( [) V ModSecurityConfig modsecurity.conf;) C3 |+ k+ P/ j2 ^
) S- q! B7 F9 A. q2 d$ @
root /web/wordpress;0 n7 E6 k/ v. j" E- a3 {2 `
index index.php index.html index.htm;% q4 b5 @: N' ~! I2 @( s
( \5 M/ d' `" U% z, X
fastcgi_pass 127.0.0.1:9000;1 B5 t! x3 h e* K0 ]
fastcgi_index index.php;
" w$ e: P I( I1 M, M+ K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;) F+ S: A) A9 y, t
include fastcgi_params;( p) D+ W% ?3 Z+ u2 N
}
, v. O) B; V4 |$ D }5 v1 r, G u! I1 i6 B; I
upstream负载均衡:- J6 y. d, j) m' {5 |: m
# ?2 x9 Z4 T: }, q supstream 52os.net {
' R1 @# h: f$ v4 t( F+ U server 192.168.1.100:8080;
" x0 p$ h6 v9 A server 192.168.1.101:8080 backup;( e5 d7 k$ ?# I1 }; F( ]. |* A0 @! g
}8 j9 m0 J; l# _4 N. e P% ` {
* t5 z4 n ` P" N9 Sserver {
% M3 V3 w8 q" b1 E, f% Blisten 80;
8 `, V$ I% S; a& f1 tserver_name 52os.net www.52os.net;7 q: A: y% L6 W1 v
8 z. e0 t+ U) e3 h( r0 o! w. j4 G& |; R
location / {- S9 m( a8 X" W7 K5 Q5 e* A$ B+ P
ModSecurityEnabled on; ' p5 X# g" I) ~8 h- @8 h
ModSecurityConfig modsecurity.conf; & j% S; @: I/ e5 x) q+ R
! @& N6 G* j6 W! ]
proxy_pass http://online;
# O( }2 [9 D5 t# m& i& p proxy_redirect off;4 z$ j6 m3 e% y7 E! K% p
proxy_set_header Host $host;
1 x3 s9 q4 X; D3 C proxy_set_header X-Real-IP $remote_addr;
4 ?9 l; d4 d! D* a2 F proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ Z+ z/ W& U5 W" b+ Z' [. l
}& e- p$ y9 Y+ o4 i3 Z0 u
}
$ u* a1 o5 s; d) `3 x六.测试
6 [6 r! u; G1 W- Y, J3 G8 \) c5 K4 ]0 X9 B2 m7 a g* P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ h7 ~ @3 r/ s% }8 C1 W- m0 E+ W% c& N2 j' }
<?php1 V8 X! i( Z3 Z" n3 c" \$ V
phpinfo(); * ?" K# u6 c) q. C: V8 X- v4 C0 I$ P7 B9 G
?>
& c6 U% S3 j7 q4 h; n# B9 N- F, ?在浏览器中访问:
, T6 h9 c+ p5 m5 v- q: ~7 y( }. b5 c& y1 o) W) N+ _
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 k7 v- l" ^# ]http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。; H. O* w; v/ O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 J' b6 p: t6 z' R5 w说明sql注入和xss已经被过滤了
1 }, c. N: r x2 e+ M }) L- ]
3 ^& p, G- r$ \6 r; W# v8 d- h七、安装过程中排错
, a, C' h$ R9 B+ u9 l6 k( g) b* I2 K: D" z- O: _; G, j% e. r
1.缺少APXS会报错2 I, T( ] b9 ]
. U: z4 I2 M! I& X2 w+ \configure: looking for Apache module support via DSO through APXS
5 b) S6 s" w; q( Mconfigure: error: couldn't find APXS5 j- K3 b, k+ E$ R8 N
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 A. \* q/ C& p7 @
解决方法:
, O) R4 ^$ |* Z: X. R! L' r% ~, {/ k4 ~2 ?* d. ^
yum install httpd-devel
! J5 w2 p9 @5 L& p. }$ W6 x. a2.没有pcre& Q: u- [+ T, h4 v# }1 d& d
" u* l; Z6 A4 L1 t' k B+ y
configure: *** pcre library not found.' H3 ^$ V* l0 @. h* [( k# B
configure: error: pcre library is required
) _# z4 U. B+ y$ O1 u, S: J解决方法:2 C: m, T6 z, m6 T& T
2 c/ h4 Y3 d" ? hyum install pcre pcre-devel0 C6 d# p+ x; H: ?3 U. g
3.没有libxml2
7 k ~4 F' s# s4 j, h! E5 s
! O8 k- D$ F( A: O3 _$ p/ i' I1 d- _0 B
configure: *** xml library not found.
$ k0 ?8 x. \- S& x3 G! pconfigure: error: libxml2 is required4 r) z. s5 F; X9 F. ^6 W7 s- j
解决方法:( y% w/ ~0 [4 G- Z
/ s3 ~, p$ n; S# m
yum install libxml2 libxml2-devel
. e' F* j+ G/ M1 v4.执行 /opt/tengine/sbin/nginx -m 时有警告
- O% j/ z. m* D0 N6 I
* }' Q t- g Z) H7 x& oTengine version: Tengine/2.1.0 (nginx/1.6.2)% [7 f9 l; M r- [, W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!; S) ^- @3 w3 X4 u
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ R$ S! k' [+ k" x( V+ z
( R, S Q, M1 E6 z; `+ g2 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 B' A s3 |. {) G* z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! y- w& G* u; H, w" E" [+ Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: [, h* I1 o" }0 H& D; b O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 G, a1 w. ?% P f: r3 X6 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# a# o5 Y$ p3 ^: ^0 o9 w2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- \, Q# J% D+ T4 _% ~$ O
解决方法,移除低版本的APR (1.3.9)# u+ P1 C% S! q, w( @- k
y" w7 m4 w4 j7 B, D
yum remove apr
! p8 V' J* q* k. k# a% p" @/ ]9 v& n5.Error.log中有: Audit log: Failed to lock global mutex
3 H# E! _/ y2 Q6 V* ^' X0 o: M$ B9 U* {3 j2 i/ Z7 V' d Y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
" p P* M$ w7 m" A! sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 C; `: l# j8 b+ v+ K" j解决方法:
H9 y6 Y& ?5 c- c. r编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 A9 L5 f5 S1 K: W
, O& ~% M& Z1 _/ A# _0 M3 \3 @SecAuditLogDirMode 0777
_- b! J, r$ d! TSecAuditLogFileMode 05502 q% e0 ^1 J+ m/ ~' Z. |) D/ |
SecAuditLogStorageDir /var/log/modsecurity% n) W: o9 B) t0 Y" i
SecAuditLogType Concurrent
$ |0 d" o8 t9 p* w3 X参考文章:1 E5 Q* }" Q$ n0 F( J! `& w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' d+ f7 A# T9 w. D1 y
http://drops.wooyun.org/tips/2614 |
|