|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% @+ E7 w' o8 P3 B
! A& M4 a+ C; q f1 a一.准备工作
/ H% i8 j+ n+ M' x2 P" [- Y) c; h. e% d( j7 R' k0 r9 V: w
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# I0 P% I1 L- G4 |& Z8 K$ `9 ]2 }4 r9 C
( U" [$ j% }) k0 P: btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: c/ j" W& s* E( v: G/ `# w3 z& n$ `0 \5 e4 M3 b
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 l. D) ^" K8 K [4 d# j0 w
0 r! j& o s, S* i! o3 bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" h# U, l% c" ^% P
U# t2 ?: m) e8 f2 O: D依赖关系:$ r4 j/ Q/ T, m+ ]3 e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- @' p' G3 |0 @) t; m% s5 }! N6 i' C# m9 P$ V
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& E# |2 ?: ]5 g. umodsecurty依赖的包:pcre httpd-devel libxml2 apr* \$ b) s$ K/ K# D1 b* u& H
; j; q. v2 I' _4 [5 o* r" V
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ H2 h; J1 ?1 d
二.启用standalone模块并编译/ n3 |: e" J( \5 n
+ _! a4 X8 v& n下载modsecurity for nginx 解压,进入解压后目录执行:2 r6 y1 }) a7 ~9 g. J
/ U6 h$ k/ k+ X7 N
./autogen.sh
. s" m3 y4 i/ H" |./configure --enable-standalone-module --disable-mlogc) I" w1 n1 r! z) p3 }% W! _
make
! _( l* U& L. n5 h# \9 Q! M p! {/ B三.nginx添加modsecurity模块' t# {3 n/ ]8 H2 E* s
( E+ M. c6 a& v- C! {: x
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# f( r: s5 e* U
: u* V, c8 m _4 h7 [ r9 e. W* o./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine* L0 w4 l I/ y! N4 ^
make && make install: O8 ?: }; H! l
四.添加规则7 y/ ~$ u& x. i
) }5 h( L# T; P& o1 S( E. smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 s5 y! r$ t) _1 t& t- R0 E% _# s3 f
9 X( n# I% b" H0 H) c. a* Q1.下载OWASP规则:
, x9 H- z, p! h9 t6 I; H E/ V/ W0 d
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs: ?6 @3 F2 W2 G2 r$ ~+ {! u y
/ p1 Y+ m0 \% s8 x+ D+ z7 h- Z
mv owasp-modsecurity-crs /opt/tengine/conf/; ?3 s. B* V2 U+ b
6 T- C3 \/ I8 j; x- h6 Y* h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
7 n" J; \, J# ^2.启用OWASP规则:4 }7 Q' l3 T$ a) ?! `* h1 c$ }
0 f4 Y1 I5 e* G B5 n# b* `, r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 T% g( K' B! f
" _8 V7 T; P3 W* P0 Z. f( j- a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- a; R& u0 w6 R& c" B9 R& {9 ]. F+ F3 f$ h8 K& u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。: S$ @1 O& i7 G, J& i
5 X, h$ p9 ]& G0 Z* T5 J/ HInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" R S& T1 }. j$ l/ G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 P: x1 u) _8 ^9 M3 z& EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* ^2 T( k! a7 V% g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& D/ M. N! T6 \6 z( CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( H7 ^4 ]9 W4 l" r) B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( J2 F0 R" j7 L' l; w" a5 kInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 V& s2 K7 K# Z, C5 C- O8 o五.配置nginx
% @1 }1 V P$ W" Z; h% J4 }( v& Q9 d1 f) ~% t
在需要启用modsecurity的主机的location下面加入下面两行即可:* s/ V, ] a) p' G
& f/ y! @ e- b% M9 cModSecurityEnabled on; _/ s# f( M! F) R
ModSecurityConfig modsecurity.conf;
; |2 G8 m j5 x# b% Q3 f9 Y6 @/ _3 C R下面是两个示例配置,php虚拟主机:- D( U. V* `9 [. l0 {, g' ]
: Z# p C6 A9 s$ `/ O9 V
server {5 p% s9 ` w8 p. g g, A
listen 80;& q7 c0 D; U+ Q1 p' l, @9 g
server_name 52os.net www.52os.net;* l/ L0 u6 m7 ]! {7 a- \
, p* {/ F; F/ \- R8 O location ~ \.php$ {
4 p y1 o3 R+ P" U ModSecurityEnabled on;
F/ F R7 N0 a+ y2 N9 a: T ModSecurityConfig modsecurity.conf;
2 r+ Y; B% ?' b7 x% V- i/ A, D8 f' s4 V* }, f
root /web/wordpress;
6 w% X/ }0 o' b7 m) l: m2 o index index.php index.html index.htm;( \0 F% p; d' U* a$ k) o1 i3 ]
! [! u! N+ o# _: k- Y( ?
fastcgi_pass 127.0.0.1:9000;- K0 z9 C' W3 ]" h
fastcgi_index index.php;
/ H5 J8 X' e" k) K* A fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; S% C+ ~# k& Z
include fastcgi_params;$ I& x$ }5 t) w& ^ b- I! g
}
, o y# S2 Y: B: n }* c; H& }2 E" w( @
upstream负载均衡:$ C& M0 U. M0 K6 N* f" i
( ~ y j& t. H/ f% [upstream 52os.net {
+ b( D# f: k+ z4 {/ d' p# Y! n server 192.168.1.100:8080;
% B4 \ j% Z+ o5 C0 z* Z, T server 192.168.1.101:8080 backup;; P7 }% x% Z8 A0 F Z! l! ?( l
}
* G* ?% x9 \# w+ r& z/ X* S1 S4 t$ V8 a; `/ z( k3 O( q
server {+ D% X! c$ D9 s% `
listen 80;# r$ d% m/ _! ]
server_name 52os.net www.52os.net;+ j5 ^* m' H( e% L# o0 F
6 A. O) q& R" o* Wlocation / {* G/ x* W5 J5 d. I; v
ModSecurityEnabled on; ' _! m7 l1 Q2 g$ k- L R
ModSecurityConfig modsecurity.conf; ) J2 f/ B# S3 p+ _0 L
0 L$ T- x7 W3 ~' W& E
proxy_pass http://online;
$ T5 ~% p; v0 u8 w8 v proxy_redirect off;+ [+ w0 x! a; v' f
proxy_set_header Host $host;
: ^1 T, E5 C8 F0 n) I1 W& O1 O proxy_set_header X-Real-IP $remote_addr;2 A: l( @" Q: O0 I0 y8 P& e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ |7 G! y3 H5 x1 i5 O
}/ L+ w/ b' L# @& w" Q9 R* M
}/ Y: ?' O, _: Z# | M$ v" F
六.测试& h' Q4 S k: M
- w# i( f$ A0 d. l( H; R$ k+ _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; Z9 Y4 r+ V2 r! @+ {9 H
# q5 V2 c9 }# o) m<?php
$ O ~4 I6 \. a0 U- G phpinfo(); @3 X+ r: ~& C [$ |
?>& v( u' B& \& W' P
在浏览器中访问:
* t& J; s" y6 D* K
$ r% ~0 W3 X) W0 nhttp://www.52os.net/phpinfo.php?id=1 正常显示。& t4 `1 T1 i7 Y
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% {$ H! _- e% c4 ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- g6 b) E4 h1 K* ~2 g) X
说明sql注入和xss已经被过滤了# T. f! M4 J P$ o! J( Z' U
( u( ~% N D2 \) G2 c) ] m2 I七、安装过程中排错
* W& `7 e X: B; u" E$ @8 B5 i: P, [+ T4 e- W5 z
1.缺少APXS会报错
+ j) O; ^& L2 G, X
) ?) `% N9 k2 m* z5 ?/ |! g$ p. u& oconfigure: looking for Apache module support via DSO through APXS
& h( O0 l( ~- q5 K+ A% Yconfigure: error: couldn't find APXS% M3 t9 p% k! q# b
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* n5 a, w6 i G; q7 W, x$ N解决方法:
) g8 g7 G/ d7 A, H* _5 P" H& h* J$ V& N
yum install httpd-devel
Z9 N" `5 f6 l4 m2.没有pcre# j# H* m: r ^4 |# W% t( Z
$ L: u4 J" [0 S# ^ m! Wconfigure: *** pcre library not found.1 \( ]* m; V9 o7 F$ v/ R
configure: error: pcre library is required5 r. o1 m; Z6 Q7 Z2 `0 r
解决方法: ^/ L4 j6 l) N0 n
1 t$ R2 J: j4 t% S: h
yum install pcre pcre-devel
* W6 W! S7 x9 U/ n" J4 @, E* q3.没有libxml2
& v/ t7 M& K1 s; C' m- `
/ [ [& \& ]/ X" @, m! |2 g
) ]; B7 |, k. K" f G4 }configure: *** xml library not found.
u+ ~" o7 _7 y1 _* }configure: error: libxml2 is required# z% o! L/ ?* U, w7 ^
解决方法:
: T! ]$ G/ ~) o$ O7 Z5 P' Z( X4 j9 r/ r+ g# A* U G8 Q' P
yum install libxml2 libxml2-devel
' t3 d# Q# q0 C4.执行 /opt/tengine/sbin/nginx -m 时有警告/ b6 H) J8 f2 L7 v C
& M/ v$ W& `7 Y& ?4 i o( ?1 C
Tengine version: Tengine/2.1.0 (nginx/1.6.2), F( |5 W+ F3 X9 u# K5 m
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 F+ N, q7 Y' Y9 x& i原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 w* t2 C. X3 s. Y
, x0 X) @1 a/ `+ `; }; W/ _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 ]+ C2 ?* a1 L& z. p2 A5 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: M3 d+ c A/ l# D. j2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& Y; `" h" d/ ?% u4 u4 `( X) T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" h1 H$ n0 B+ k# R0 \, m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6" [8 h4 @4 i- p" I) q2 m' n! C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 X z0 D, n% l9 }6 H7 o0 t解决方法,移除低版本的APR (1.3.9)
& y% F+ t, x: s$ V9 p* T
+ t: c$ s9 x: \- l+ a- xyum remove apr
7 q# r0 A! C7 j! \ `# v" A- B5.Error.log中有: Audit log: Failed to lock global mutex3 q. t1 b8 {, N1 S2 x: W! ?$ e
3 g }) i4 O2 K6 a. D
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* e& W- l( {' J7 R/ k) U4 r% bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ }& s' h* E/ \, H; u1 C( c解决方法:
5 ]/ l! \' a7 O9 `8 n, [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# V# p/ k) `# A9 a
) Y3 O1 K) ]$ y7 f, g( C
SecAuditLogDirMode 0777
% ~$ V9 f- X8 ~$ a. LSecAuditLogFileMode 0550: x8 [. ?! i1 Z0 y7 [
SecAuditLogStorageDir /var/log/modsecurity
8 ?, n4 r, h9 u5 xSecAuditLogType Concurrent+ W) M; _! j% C1 x& s
参考文章:
% O1 y6 z: D3 N' V' k$ ]/ \/ Ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ o* C; X# \& w+ F8 t0 E: nhttp://drops.wooyun.org/tips/2614 |
|