|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 k |; G; K$ s4 \
; b1 w* c! ]' y: c$ v3 d" ]一.准备工作) O/ z6 U! R% l4 T
% _, U$ m- e* z, i2 f
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 f# a3 J' B5 K: g0 N9 w; Q. R. o! C" q$ R8 E7 B& |1 \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- a7 P( P7 _. d% y8 \" P/ n6 P
: b+ A( H' W2 ^4 Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ t5 {. C! z0 H; }* R* B+ F9 R
& F |8 I- _2 |" f; ]) v( Z- B( O' kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ w0 \6 Q9 d9 m4 G: J; L& X% O
7 D+ D: D3 N! G9 y0 d依赖关系:3 b4 z2 X: z/ \+ u; o q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
, A4 P% d3 X$ L, k$ P, D
% I! w6 ?5 k) Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 ], v. y T+ h; @. nmodsecurty依赖的包:pcre httpd-devel libxml2 apr$ U! D4 r/ z( S' u& I5 T
, Y$ l: z# A0 C2 @' syum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel1 p. s* `7 o I4 `! @+ r8 V i
二.启用standalone模块并编译1 j+ w6 u, v- `# m
% M# q4 c ~9 ^: f5 e- ^
下载modsecurity for nginx 解压,进入解压后目录执行:
. a5 _7 I' v" _( g1 a" a' x$ p1 O9 ~$ A1 P: W: I5 L' ]
./autogen.sh
, o) I8 i' R- W. j) n3 j./configure --enable-standalone-module --disable-mlogc' D/ k9 e1 Q# t- d" O/ Z* T
make E5 m- I' A! H( t+ K) H6 p4 H, d
三.nginx添加modsecurity模块
; H5 h8 a; E* n. V% Y
( p7 y$ p- j q5 ~. Q, A在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' x, a+ |2 ?6 o% w0 r
* o, X! O/ n8 x, N- o
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine- f/ G5 x; u& x9 f' ^
make && make install W k, J: c" |6 Y
四.添加规则
. i5 v! G1 u" s) v% L
0 f8 n4 a+ [! S. f& imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# [( M: W" a+ g5 H3 s$ E# `; @" s' i$ U9 d. s2 b/ O L7 m7 @
1.下载OWASP规则:5 z# k; N& V1 T- i' W2 a, C" d
) j) j) D2 s, o% V, Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs' `; j1 w) t6 k, a# {1 d
; N1 E: @1 B! ?4 `
mv owasp-modsecurity-crs /opt/tengine/conf/
7 j0 ?2 O% M' A$ t
p# z8 {6 G1 S+ h" ?9 Mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% k! q/ @9 C* ^$ f9 Y2.启用OWASP规则:* J. K% c, A" Q2 {& V
; G- d! M# w f8 K9 F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) R$ t/ Q$ c# D/ \
( ^# H% i6 V; b" e编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 M) e1 b/ `; j7 V% z8 ~; t
) x- S2 C/ p+ H$ u; g. e" Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。 m+ X4 l2 @* r
- ^5 \4 p; K, f3 N* i4 B$ Z3 I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 a( s3 n, q7 M4 r' TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. \) a( X b7 L7 M, [7 CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: Q) _3 P" t/ n# o" dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 J- b& p$ w3 `6 h; ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 w1 {8 B2 H' Z- ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& s. }& z; W3 t" k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) h% {' i' ^! y9 C' m! L五.配置nginx
, p7 L; X- p9 F. s! `9 f$ x2 ^% m3 W, X
在需要启用modsecurity的主机的location下面加入下面两行即可:
1 r8 h+ O5 V7 b' C
" A6 `. L/ C( a7 e8 v0 pModSecurityEnabled on; * C% a+ u, F# f( W; C. N! }
ModSecurityConfig modsecurity.conf;% ]5 K# Z! h* G4 u" u
下面是两个示例配置,php虚拟主机:
" k% E3 o& \0 l& h8 \" l4 `- [8 P3 z4 c6 s% l8 \/ g6 J ~) q
server {
' x1 J& m) ^4 p9 z2 w. d0 @( e; L listen 80;
7 E+ f, Q2 M: a: Z. a5 D server_name 52os.net www.52os.net;
, R/ r6 V& w' I/ L' T* k
: P4 X$ x- }+ x; q location ~ \.php$ {
$ l7 G- ^9 u& I2 g( V' m' o ModSecurityEnabled on;
+ ?8 s9 J8 T$ {! }8 v. r ModSecurityConfig modsecurity.conf;7 I" q, o, g& b& `4 n R5 W
0 j" q$ W) A, }2 i7 M root /web/wordpress;- U) f. H% a( E$ L
index index.php index.html index.htm;8 _2 t& @6 S( \3 @
! A; H1 @. K8 ^( _ fastcgi_pass 127.0.0.1:9000;$ N$ e+ k4 ]2 b6 z G
fastcgi_index index.php;. r3 W6 ~$ c; @" x( v) v0 s% R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- D/ h( Y% m# g3 `- Z include fastcgi_params;5 O7 N1 p* H! W# J1 `
}! r6 J- r" U8 s! k
}. ]1 f6 p1 b6 Z6 }
upstream负载均衡:6 A: k# @7 a" G! E) G
! R3 [0 W) M- V# m
upstream 52os.net {
, q. q: J$ {$ a, Z server 192.168.1.100:8080;7 r1 u2 X' |; x3 b) r
server 192.168.1.101:8080 backup;
4 N5 W9 y3 h7 I}
* y9 \0 m9 S0 b3 q, {. S1 G8 K
server {
r2 R' c' R% c/ F* h6 C% Zlisten 80;, v; O, K% @5 Z1 L" f( o& X
server_name 52os.net www.52os.net;
$ ]" g# X9 ]7 ]- ?! x( N# `2 l- `) f' v% ^: M9 q
location / {( A( d f+ B; a" C2 E, r$ p
ModSecurityEnabled on;
' Q$ A7 |# s- a' Q2 e0 J ModSecurityConfig modsecurity.conf; 7 h9 c- o" [$ d | o
. w! G, p! x0 E) h0 _ proxy_pass http://online;6 [2 c& I: k$ x* r* X# W/ E
proxy_redirect off;/ w8 s0 V; B4 e4 v5 [9 c3 Y
proxy_set_header Host $host;
+ q7 V6 V8 r# ^7 m" g8 a proxy_set_header X-Real-IP $remote_addr;
1 {( k! ?% g; O* o# Q5 \6 k( c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; r8 ]; R- k# c' }
}
) H6 [& k, ^ M2 U) }! n; x}
}$ D3 b. M& U M2 g2 p; Z六.测试
% j. }( X4 B8 P$ {0 U
8 e. I% |+ r& x' O, S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# R1 N6 s% g( B5 }- l" |$ _
6 ^+ a a# g* }
<?php* R5 q1 L) b5 [7 z6 s" k
phpinfo(); # U0 [0 s% t# g+ ] {. E3 h8 i* _
?>
* k! d o. i6 u2 `3 ^' i在浏览器中访问:
! S/ U! V6 _2 @9 @! w8 z, }' W, C1 K
http://www.52os.net/phpinfo.php?id=1 正常显示。
: O* g$ Y) g% n$ m2 `http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& d, }2 L q+ j( j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! v( }0 G( F3 b7 ]) y
说明sql注入和xss已经被过滤了
* E0 J( h; y( V0 z2 v
- g$ r) Z a! \七、安装过程中排错
% E5 M. U3 G3 @2 s# U' N6 k
* L% v8 X3 }3 C- Q" x! ?1.缺少APXS会报错; T! {. R9 W1 D. r
- `9 L+ l% @- ^7 |! F/ \
configure: looking for Apache module support via DSO through APXS. t9 K$ i& a# H3 R! N
configure: error: couldn't find APXS
4 o* d6 G# j4 j% uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 c, C9 Z# N/ D8 H* B' S) T* Y
解决方法:5 s v: o' i2 d8 G3 S
" o6 w) u5 r7 ?! G3 T/ s7 x0 n
yum install httpd-devel5 W- u2 @/ i0 a1 Q
2.没有pcre
$ T( c; O; U3 l A
. K( q7 J2 Q" Q* L' v) dconfigure: *** pcre library not found.& B- {2 I$ B3 _* V5 n: s
configure: error: pcre library is required
# u$ P" S" p2 L t2 A解决方法:5 {, f, P! m# s5 }) ?
/ D/ w; h6 Q. x0 r
yum install pcre pcre-devel
+ G8 f! M$ F: |& A- }% V4 F- P3.没有libxml2; j! s; w. N/ c k% Z9 a( d" V
8 O: h. \1 Y# O6 \ f& h
0 ^) r6 N8 J; L6 c4 O
configure: *** xml library not found.8 \& P0 Q( {# r
configure: error: libxml2 is required7 R: E) b5 m' @) ]$ C# T
解决方法:0 b$ p& z9 Z/ N9 I1 i2 p( V6 |% w
9 V: Q4 b/ |9 { x
yum install libxml2 libxml2-devel
& l* k3 U. @$ K2 Y( h$ h4.执行 /opt/tengine/sbin/nginx -m 时有警告
- ~6 I! B: F) }# ?- ^; s* ?' p
' s* d3 Z. h1 Z$ Y& rTengine version: Tengine/2.1.0 (nginx/1.6.2)
* I8 a" [* \. B7 x3 u" Nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: U& i. q, q4 U: L8 T/ `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 S: B- p( v% E0 k6 _6 R
4 N' M. i$ @" q" S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ {8 g0 }# B% _; D9 y7 x3 }' {0 Z' f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; q$ m3 B' ]% A: Q% ]" B: x6 Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; L, W5 N) k% f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"& p$ Y0 X" _0 i- R: G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 M. T3 U$ R% A% T' P# H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ M. R- n& C) }( J, |解决方法,移除低版本的APR (1.3.9)# ~0 H# \9 T4 D% ^3 f
: j6 V+ f( h. H! I. I+ nyum remove apr0 U0 U" d) j" X0 v4 F, q. n
5.Error.log中有: Audit log: Failed to lock global mutex# q" j1 d6 [- \, h
% m! E- M- n& d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. Y6 F4 o- G! m/ N, }- q) M1 Cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* r q: G( ~2 U
解决方法:- i% V. y3 Q+ W7 r( p
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 ]7 B6 x9 H; n* ~0 D P4 F+ d# k# D9 F! @' i% S/ F) T9 |6 W
SecAuditLogDirMode 0777
- z! i) j& a9 z+ p. L- o) Y9 ^/ XSecAuditLogFileMode 0550( R4 D) W% G" A( Q- V: e: l
SecAuditLogStorageDir /var/log/modsecurity8 Z3 u6 g. | @! r9 }5 T! f
SecAuditLogType Concurrent/ ?. r. z# `) E/ v; h
参考文章:
' s6 x! j7 C8 w1 U( v5 Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; ]) A' P+ e9 l0 x, v; mhttp://drops.wooyun.org/tips/2614 |
|