|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ M4 U& I, ^5 B% M( W1 b; o, X7 k! a- x4 w
一.准备工作
( \" j' a# W$ D7 s) b! b2 ~4 z) d, x3 D- w0 b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 I7 }! g( w' c# J4 M9 c
' S( o4 P4 e+ r0 E' H! ?2 V7 Etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 P/ Y$ ^" s- H: k/ _, D4 b% L* a7 p% ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" `' T- f! c0 ^; T
8 M1 `: e: a3 V6 ]& O5 ~1 `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 E6 Y, Z/ d7 G7 u+ e
5 T. B& U/ m3 @' W! O/ A
依赖关系:3 n4 z) _, `& s" B1 w( f( z& L( S) A
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' w1 K( g$ z: `6 ?1 B$ c6 m! r9 n7 C3 X% \9 R
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel8 y; i8 o( n M9 r c( [1 z8 b3 o
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: t7 v0 g4 d/ H) \0 }# D H6 s
6 m7 o2 \0 C+ q0 `6 H, `# Syum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! y5 Q7 Z1 ]4 {# a二.启用standalone模块并编译
( W; s2 K ]* Q. H: A# E- J1 S- C; w4 h
下载modsecurity for nginx 解压,进入解压后目录执行:9 H1 B" e7 j2 S4 q; X
5 Y M$ `! K; @/ c
./autogen.sh6 i" V$ i6 n) V. ?) |
./configure --enable-standalone-module --disable-mlogc
) f Q& N! ]! t3 [make ' M$ K) J5 x& b$ Q# D( S
三.nginx添加modsecurity模块
3 q/ D6 U' s; O5 D
# ?4 l: l H7 H, F0 M4 D7 i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( S% w, e% \- e7 z; P, p
* _$ N; {7 _& c( t8 N4 ~./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 i! [7 v; Y* {5 r3 o6 Q
make && make install
- @! t( S3 W8 ~. Z% v0 L! m四.添加规则& s3 p6 {7 V' Z$ p5 o9 P
8 O) q' T8 Q3 t9 P+ s
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 _3 B) F: n; K" H% M8 z( Q3 a: e. [+ |9 k5 q+ ~/ @8 i; W
1.下载OWASP规则:
# i, n9 I, g$ y# M% J' E3 b5 @1 X' Q) a; W- u
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; B' Y; D# f4 y7 o: ?
3 u" o9 m- O: M
mv owasp-modsecurity-crs /opt/tengine/conf/: _# X# Q7 J3 R! G, x
) J' o3 d$ \6 v! A) R; Bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) B, J+ |" S# z. f- d: x
2.启用OWASP规则:( i' h. t8 l: v1 O
; U1 h0 f2 |, U5 V+ S* |( d1 F/ s2 V
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" V( X% Z# ^& ~! [1 W3 ^
3 _0 e% v: w$ X: F( M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* n; i8 M7 O5 W2 R
: e8 w0 h: s5 c0 dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 e! U: v# F. N) ~$ @8 l7 l& S1 i1 t& l& H, Y$ [. D
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ F5 l9 ]) ]* }, |- y& W7 E8 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" w/ f7 e4 m# X# e) @* b$ f! r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ k$ H$ g" A6 O, BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, ]- e ^. @* E7 R/ c; P- L# Q/ yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! b# z! ^8 T8 L' E# Q1 H. z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 }' S/ Q$ _/ m. k8 ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ R8 v* O3 L ~5 ^ [6 ]
五.配置nginx
, f& C9 \7 I8 n o/ n
+ J3 D8 Z3 c M在需要启用modsecurity的主机的location下面加入下面两行即可:; K- m# \" `7 `! s/ i+ k
% A4 T Z6 g1 r) S1 a6 I1 \ModSecurityEnabled on; ) X, j: J* i/ P7 Q3 \3 v( \2 J
ModSecurityConfig modsecurity.conf;
]; X3 }( o4 M1 Q% S下面是两个示例配置,php虚拟主机:
4 B" a7 o9 v* x/ Z) O. g5 R$ v5 x9 c5 z1 u2 u, Y# m3 u
server {2 c' I& r8 ~& D4 R4 y5 Z1 |
listen 80;
5 m' e& |9 g! F8 G9 n7 j server_name 52os.net www.52os.net;+ u2 E' J3 n* x6 p' z- D. t9 e
8 R; b2 n- C5 ^/ `2 @7 U location ~ \.php$ {/ |: C$ M" L- I+ B O8 j$ q
ModSecurityEnabled on;
( ]* t2 P+ e. n5 j, h ModSecurityConfig modsecurity.conf;
+ J4 s! b6 N+ S2 x/ j* ?6 U# C5 }. K9 ~! ?
root /web/wordpress;5 u" m x; h! ^1 y9 F6 f) y
index index.php index.html index.htm;
0 V3 u; W2 J# M/ f: B & J* u! h+ ^& P( B p
fastcgi_pass 127.0.0.1:9000;
/ S; G, d( a8 e$ F" b fastcgi_index index.php;
) ?: b' H' `) U( I fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 M! o4 L' K5 X# ` include fastcgi_params;. R4 |& D. N1 }
}; I5 J/ h6 d; n
}' g0 Y+ a# Q9 D
upstream负载均衡:
7 M% z5 O, |2 ~* N6 Y, F( f8 p4 [- @# p5 E4 j& Q! d0 M% ~
upstream 52os.net {
8 n' F5 x* P2 j2 d8 ~- d/ B% ? server 192.168.1.100:8080;8 _4 F1 o( j; \# [ b! r7 N9 Q
server 192.168.1.101:8080 backup;
# _; e% @& [" @( ^. h}4 H, S! A) M, z/ `4 W
7 p! `+ z2 P, E# ~# K: o( a
server {
# R* v; z; f( @% C2 N$ a& p8 zlisten 80;
0 D" N. T' Q9 N$ [$ yserver_name 52os.net www.52os.net;
4 n# v0 g7 S2 C2 I U V* D/ M3 p
. _# \1 p j: z/ l4 m" d$ R9 c+ vlocation / {7 \+ L: n5 U9 i% _" ~7 M7 r. p+ ^
ModSecurityEnabled on;
( m0 @3 A6 W3 W6 [% Y7 M ModSecurityConfig modsecurity.conf; ! P5 `4 c: H: P5 r: ~: [7 _/ x/ [
) w: ~% r9 S+ L, }) ^ proxy_pass http://online;4 N8 M9 b4 F1 N: ~& S" ^
proxy_redirect off;- {& B9 y, J: s3 j$ J+ v
proxy_set_header Host $host;+ ?$ b9 [1 q/ m+ @
proxy_set_header X-Real-IP $remote_addr;
0 i+ A- X% L/ X9 W+ ^0 K proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 l% W' ]( }1 U7 W1 y1 A5 C6 S' Q
}
, h% J+ k5 e- r1 Y+ a6 b}
4 k+ L7 v/ F- r& c- [六.测试
& z& t' z# A2 Y& w' x
! S5 B! D- W- y& c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 W" O7 |8 D3 H
1 N5 q2 k* O9 |/ I2 d7 ^- B I2 W<?php
+ |# C4 m9 _! A) }4 Q" c$ j phpinfo(); G3 T4 Y" J1 u& I- p
?>
8 N' o% Z! H+ ^$ b! {: h在浏览器中访问:) M# r- \0 s% _$ T! z
& E& m2 ^4 B1 S
http://www.52os.net/phpinfo.php?id=1 正常显示。$ {5 q' z3 N* k8 F; E0 C9 E
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 @* O' A3 X* M x' T! S
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- r+ \1 R0 C) N5 k' q
说明sql注入和xss已经被过滤了$ j# _0 R- s. |% y
5 ]/ d: T2 H+ f" F, b% y七、安装过程中排错
! J. F+ ^0 W! B- W& g% q7 h [/ I0 S& o9 w/ k
1.缺少APXS会报错
) [. L- e d. A3 B
- C3 a/ U o! w4 @configure: looking for Apache module support via DSO through APXS+ R2 b' n" @! z) T. O" r* u. j; s
configure: error: couldn't find APXS2 K/ c7 m9 F/ W: \( o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 y4 E4 W# p! Y: x; ~解决方法:/ S' r' [& n2 @ D5 c
5 V* x% y6 g* r: C$ b0 K7 E9 i
yum install httpd-devel+ V5 i" E% N5 w N: }6 T
2.没有pcre
0 S& d0 D! }# r3 ?3 c# e0 y8 i9 q# u8 q% I( x, h
configure: *** pcre library not found.
* S2 \7 X4 _; U7 [$ a1 Nconfigure: error: pcre library is required
/ ~- q( v' G# W解决方法:+ D- D) M/ k& x1 [
- h% ] V3 T: p1 }& M( o* |, i
yum install pcre pcre-devel
# s* w1 V$ f2 D1 U% ^3.没有libxml2/ Y) W5 z: g$ S* \4 }! h3 ]0 Q
' d, m1 x0 J7 N
3 J" t4 H/ G Y( \configure: *** xml library not found. @3 k9 W+ D2 Z v
configure: error: libxml2 is required! q; z6 X8 N0 d
解决方法:
. v/ I" l2 f* O" G. ?4 ^; `
+ v1 `5 k' }! Gyum install libxml2 libxml2-devel% h6 S8 o3 u0 ?& P1 ?) t2 M5 c
4.执行 /opt/tengine/sbin/nginx -m 时有警告* I7 {( A- u+ h/ T. c2 k
2 v. y$ s% c2 e3 F8 ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2)8 R, X3 v. `4 t1 f; z+ G$ Z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# ?8 W( \: ~" G: d9 `0 i( ~2 K' H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 ]2 T( q3 @; |: Q" m/ F' E
9 N* O! ~& d6 r9 R1 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
9 f2 B5 L& I8 z, ~: U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"' S, R& X d5 f: j; u) o2 |& O" @: K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* I, W+ C- C/ I r6 Z$ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ K$ B4 e9 O Q/ q ~$ n# f3 V6 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 l) u: w+ ?' |& t: k% M% A2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 Y. { l' {$ F# r" Y
解决方法,移除低版本的APR (1.3.9)
$ [' J" A1 z, }! ?% E" O0 \( B- R# D, U
yum remove apr
) k+ \9 M9 p! r8 `/ \0 t5.Error.log中有: Audit log: Failed to lock global mutex2 n' w9 t- q4 V" [- v6 Y
& E6 p2 P! f" E t t6 X( e1 G7 i2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 6 U% i! [9 v4 b, K s: E5 n
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 H: ~! v _* ~* @- ?4 o解决方法:& E E9 F# r: X" A9 m1 L5 }/ z
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' p+ @0 S0 l- `1 J, j* x. k( w* E7 I' u* p- {3 ]
SecAuditLogDirMode 0777( X2 a" z1 C; Q- o0 @ `! k
SecAuditLogFileMode 0550( O; H% z6 b+ F# F* n8 {* f
SecAuditLogStorageDir /var/log/modsecurity' }4 V3 i& u Q3 n" `- ]* g/ b
SecAuditLogType Concurrent
a j& o8 F; [9 b, w参考文章:
4 X6 H, [4 x. K7 jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, j& c0 y( d" F" @6 K+ I( `$ dhttp://drops.wooyun.org/tips/2614 |
|