|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 e5 h' {) w/ c& \4 e& H9 }% o
8 d$ j( Z& R1 ^一.准备工作
% z6 \9 Q* S; D9 C L1 q9 i8 ^0 F% _
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 z# W+ a) G6 k! V1 p
8 z0 k/ R+ l7 ~% Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. U4 t' e/ L O* x" k. f
# v" U! }: V) m* F/ x! p9 G0 S" ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 n8 Q9 V9 b" {' f+ ~; `$ ?# J; ]# s% x n `! j6 W4 R0 C+ V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# l, K8 k! I: l- v, y
$ P7 \: K$ c/ g( H- N! W. Q依赖关系:
8 Z" B' [$ B; Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* T9 k4 @" x6 `7 @# N! ]! [" R' \; t; N7 b8 _0 a5 c% L" |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel7 |& @% A* Y/ v) K7 }7 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 n! m! z6 F% u# j% t% t4 ^6 Q& R( T2 D+ E- a, G s
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ b) V! b7 c1 F二.启用standalone模块并编译7 V3 w) _" s, {* U
: [' W2 s* ?, T2 S1 d# n5 j下载modsecurity for nginx 解压,进入解压后目录执行:
# K) G: h; w8 l' X/ k: r* B9 S5 }$ z' w
./autogen.sh$ j' W# A/ E o
./configure --enable-standalone-module --disable-mlogc/ r$ v; {/ E" a* ]1 O$ @$ M
make - J. J- x2 Q) s, ^0 r6 m
三.nginx添加modsecurity模块' D$ f4 P; [( ]9 J# z* g
/ P' S- o) h2 D# g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: V( X7 l' p+ M H" W M
# K* P3 y! a0 [ o; R: @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
5 n! f- ?# p4 k. N1 N* D5 Imake && make install" H& E" X; h1 x( j0 V3 i
四.添加规则
8 E4 [% i0 H" Z# T3 `" B1 H7 N A7 w' W& S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 ~8 g7 T: w7 O! @: _1 _3 o4 z) u' |. d3 b. _
1.下载OWASP规则:! b3 P$ c# p. [+ M {4 I
3 P( ~) i+ M9 q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' W* n- j# g0 }- Y& g) F: e6 Z- T1 n& a
mv owasp-modsecurity-crs /opt/tengine/conf/, o- e s* J) R/ z L3 m# t
" Q+ P3 N1 d A" x- R+ lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" O. k* |' \# R2.启用OWASP规则:! `7 m- D$ X. I1 I3 Z2 {3 ^6 a& t# m
( k8 W/ x. y4 k% z3 a) h: P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; ^+ m+ s$ o. ~6 p( L+ p1 w3 w% |0 P6 m) i0 Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% z- Q4 {6 B2 ^) H+ d" o
6 J4 m1 v. v1 l" J. ^/ F n+ kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* n! K+ P9 J" [$ U) l( F
# O, R! z' [$ Z9 B6 J6 bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 d& J9 B& [0 P0 b& a0 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) u1 D, y: j* q+ I6 {- G3 ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' a& ^0 r8 ?7 N7 W6 M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 K, q9 a/ N' k# ^+ N' |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 {0 N) P' I, o! M7 }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* \6 R% G5 @: k5 YInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) k$ x3 Z$ t. F% D. x+ M/ _
五.配置nginx# `! |; ~ O: U @/ t4 h
( z9 f* }0 V5 I. \ a% `4 e在需要启用modsecurity的主机的location下面加入下面两行即可:
+ @' q$ b) ^9 L
# y# ~* O# \6 z7 x' l7 Q }ModSecurityEnabled on; 3 c. k2 V2 g% ~
ModSecurityConfig modsecurity.conf;- @/ f7 } Z* A `1 Y
下面是两个示例配置,php虚拟主机:
, }* ^- Q" x/ w6 f+ K
' R3 Q8 W1 T4 ?0 V2 rserver {
: M) _) m8 T: h listen 80;
9 e* V+ G& s8 M server_name 52os.net www.52os.net;
, ]/ m/ J( O6 Z# z% |$ h1 S
+ A. Z9 A( e0 c; t location ~ \.php$ {" u3 [) |' ~0 n* e, X0 y
ModSecurityEnabled on;
* p! M* a* B- n# M: _4 L ModSecurityConfig modsecurity.conf;1 `4 d* j3 t# M# S6 P' N* C0 v( L
. H( y4 T2 k' \
root /web/wordpress;
_5 m' l3 |( g* K7 v index index.php index.html index.htm;1 i# R' K& a! P; y# [! g4 Y
7 \: I1 z0 H! X+ I/ j fastcgi_pass 127.0.0.1:9000;
: N+ c/ B9 m( h- c+ d fastcgi_index index.php;( ?* I& K" O; Q5 H
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, q0 O8 ]/ r) _ F/ R include fastcgi_params;
. H& c3 g2 g$ o }
, Y6 |+ p: [: o }
1 k$ G! Z f! @4 L. D. xupstream负载均衡:
4 t0 A, j$ t8 Z7 ~( O' ~/ G
1 S$ l+ a, K2 [# U( c! {7 g$ Oupstream 52os.net {
$ P4 S; Y# n% I- g6 I: B3 i server 192.168.1.100:8080;1 {# Z4 f; Y& @8 B: E6 A2 `
server 192.168.1.101:8080 backup;. ]6 m3 Q5 b$ H: g
}/ q: L6 o1 J2 K$ P. k" c
- |! K0 R2 {5 b9 V7 J3 |) nserver {8 h* ` c& q) }! x& v# l) E
listen 80;
# C$ `; ~3 S+ q: u! Iserver_name 52os.net www.52os.net;
I1 A/ Y* l& `$ n
6 e5 C$ {- |0 {2 rlocation / {5 h9 o7 p8 t4 O
ModSecurityEnabled on;
1 d$ {; i3 G9 X1 [" U, ?% `0 o% R( w ModSecurityConfig modsecurity.conf;
: w, C; s/ N: P
, N0 _- B0 d7 s( V4 Z) j n proxy_pass http://online;3 `/ e n1 w" s' R" s( z# S0 {
proxy_redirect off;$ I- K9 t6 r9 q: T' g8 l# L' h' z% n
proxy_set_header Host $host;
' z6 ?3 V' b- H: j/ l! A: _ proxy_set_header X-Real-IP $remote_addr;0 Z& F* T8 `/ f4 z! U
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* R c% w. ^' H
}
3 b1 g% R" Q6 N* k}
: s: q. g, a4 u! Y# }# c六.测试& t3 [6 d4 N+ W+ ]3 V& q L
' `; a! g/ e- t( P' g5 f我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ t4 h8 _* a) `1 q( X* `& {+ x1 N5 K
<?php9 U! t6 j' [9 f) Z
phpinfo(); $ T& v& X6 V+ B$ f) ^. e/ k
?>$ H3 a' J# }* z6 }, ]( Z" U
在浏览器中访问:& L! Y5 c9 S: W
! |- V) F1 x8 ^
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ J) N9 }- x/ @3 s; lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。0 }" Z$ D, G$ M- ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& w" q, C! d% i' L说明sql注入和xss已经被过滤了
" }' x, J' [0 b" w+ a
- a$ [; b- @) v七、安装过程中排错; i8 m% ^' i/ C2 ]
3 [- ?$ f t" d& b) T* } |
1.缺少APXS会报错
+ r- W; Y) ^3 g3 `; O
, c6 N( C# G) A, n k. Xconfigure: looking for Apache module support via DSO through APXS
4 z, ^5 J. E2 m* E5 l F0 q# }configure: error: couldn't find APXS3 B ^5 z% P6 N" _3 M0 e: j* f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 ~3 @) Z$ Y# U( `- e5 D! L) P( ?解决方法:" U& B5 n- f* ?' @
/ p/ C- c% @& \2 d$ b# }
yum install httpd-devel
; i$ A, Z; i- p7 i* \1 j# _0 O2.没有pcre: Y1 g9 ~. B& }3 |& I& b# w1 l1 \* V
9 B5 r$ h+ i$ k8 Y
configure: *** pcre library not found.: A' m f7 y, G
configure: error: pcre library is required! m3 N2 U# b/ U
解决方法:
: N* ]$ _; A3 V
0 m% s- c- r6 Z, \/ f. x) T, \+ u$ ayum install pcre pcre-devel. b$ c6 |2 E9 F/ u
3.没有libxml2
0 O1 R/ e- {% d: }5 n; l. k1 `/ H+ i. c) Q. w
e5 v4 b; j$ V2 q8 y
configure: *** xml library not found.2 t+ @; r; E) n" b1 ~# \7 E' V7 _
configure: error: libxml2 is required+ s# U2 V9 V$ V1 m$ J, l
解决方法:
8 `0 d. z: C4 |! Y A0 u" v& I" J6 p& P1 a: C C8 k! J1 B
yum install libxml2 libxml2-devel9 ^% x( t4 c1 R; m9 _9 V$ X
4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 s1 W0 p& Q8 G, s; Y j& N
2 y4 |3 G3 t( D' |8 w+ s2 _2 XTengine version: Tengine/2.1.0 (nginx/1.6.2)
& N2 k6 k0 C( a$ y3 b1 knginx: [warn] ModSecurity: Loaded APR do not match with compiled!: |, Z# p5 `7 f2 `! W9 S% E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 B \ @ `9 F7 B0 Z3 F
% ?* z6 W1 h$ |2 K2 F' A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- {* J" k6 c8 q) [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
# ~7 \ y5 S; n( ]" L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ }0 e% }" K0 ~) {+ z( \* d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% e; R$ z0 h$ w+ `8 e7 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 K, K# N7 I, }' f6 C3 W9 D2 p
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: R7 B' t! w/ q, c. v& a' [解决方法,移除低版本的APR (1.3.9)
. ]+ Q1 P+ ]. ]3 q, ?# W- m! M' A' S* ~: i/ B" c
yum remove apr; q" k3 e. s7 ]
5.Error.log中有: Audit log: Failed to lock global mutex1 t7 m3 u$ |# e0 Z, N0 [, h9 _
: b( ~3 L- y) T- `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
( V2 y2 J9 n6 U1 w xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
a) T% M r3 i: R d解决方法:. P: i- S5 o1 \
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 z' L: U; o- @- d$ ^2 p5 K o1 {7 c
SecAuditLogDirMode 0777
$ J' K5 ^% f! VSecAuditLogFileMode 0550+ C# e$ K! K8 ^1 Z
SecAuditLogStorageDir /var/log/modsecurity
: R- p5 S. `0 Q7 BSecAuditLogType Concurrent* J) x* I7 {' K) H4 h: m5 H j
参考文章:7 d5 ~# z% I3 I% h) O9 K! {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% f6 }4 {, u3 C6 t) i1 y5 `
http://drops.wooyun.org/tips/2614 |
|