|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 C) b# m2 F" ]: q3 D* Q2 [
2 k; T6 p% g/ f7 X* V L一.准备工作2 D# O% c% S/ m" _( |5 }7 D
# w. g! M9 T% J& W- w1 {
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- m% q. M& b$ c G, R. X
7 R* W- R, a( f/ ^! @
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: e$ _8 x0 M3 u2 @9 `8 d. j: h! [
4 r( @0 |9 S. B) \. u( p8 X& ^
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 S! m. Q c: d* |/ A; N( Z7 t" i$ d9 n5 O2 ]
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& k, G. B7 \1 r8 z; S
& g( u1 |, b: ?0 i: v; D依赖关系:
0 t# ^# X, B% B) [/ F, t) \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, @# [6 p: k2 S' Q# }
Q; N$ q7 ~, J0 I- h6 p. Q2 Zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. |) |4 k% l4 c2 K) [! t- x' o1 mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
* N# z* ^+ k- q( Q7 P: ^
+ o' U. K2 ]9 K7 n8 g) q4 cyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
, ?7 g3 u* v g/ U二.启用standalone模块并编译
4 w. m0 ^- @& }8 ]6 @7 m/ Y6 F$ ]. G- f; [% U, F
下载modsecurity for nginx 解压,进入解压后目录执行:
; D$ V, b0 M6 Z; d4 G3 S# q8 O0 W
9 C: Q4 q# D ]% R6 o./autogen.sh4 q" v$ k! X# n8 A5 C7 E4 I k
./configure --enable-standalone-module --disable-mlogc
2 {! }" I3 M0 J2 v9 I3 x* nmake
- q0 i! X$ b' g% V! i, o0 u. d三.nginx添加modsecurity模块
9 [. k# s5 i. E$ \' K7 g4 N6 F: T1 p/ B
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: T" k) V1 c: _: X, v! Y: m& a6 s: f/ @$ @! y0 u+ `
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! N$ S; ?# f. f' s) J
make && make install
- f7 w- a+ ^# e) C# e4 `$ l4 l四.添加规则
1 j4 y$ M9 l7 w" o/ i# |2 a
0 l; i5 c" Q& [( W- N: vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 e3 Q k# c% O. @, a& Q8 d3 G; t1 O( r3 @: T
1.下载OWASP规则:
2 B0 H8 x0 |8 @. t8 ^" W* F1 u& K4 N% M' U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 M7 Q F. J1 y5 o' }& F! }* x. d. ~7 q7 q6 k' y
mv owasp-modsecurity-crs /opt/tengine/conf/& \$ l+ [1 o% D) }) ?" U
, W# w+ l% E6 Qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( ]5 u+ |. }0 f: }9 H
2.启用OWASP规则:
3 R! Q5 m9 u: i6 C" H# N- d. B$ j) F9 k. ^8 c5 A1 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& P) _: q c3 ^8 J& r
' S5 W: Y" l& E$ l# _8 G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; ?8 {8 J8 g! I* L8 B) I- d5 `0 r: I7 v& e! y: v. r
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# c* ^ l* _- B6 p' _
# k# o) R& @- p: Z zInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! M2 t- S1 w/ n2 m- a, L$ Q$ s* t7 R' P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 j1 g" o- Y* V6 k, w; M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, @, ]- V; N& W. |7 a1 T2 }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 j6 J$ g t4 B) u5 w# I( \6 bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 O1 U* s5 _7 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 A; M; u. _' zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" W4 |$ l# O2 }) e. ?$ u五.配置nginx
2 I) O$ n6 _* |% U& d0 M$ S/ Z1 F! p+ F
在需要启用modsecurity的主机的location下面加入下面两行即可:- ~" r" y+ I0 `- D7 Z. t9 ^$ C3 z
9 ~$ F" a, b+ J: a) e% ~
ModSecurityEnabled on; . R" Z1 r9 ~ C
ModSecurityConfig modsecurity.conf;
# n: d! e% T8 w! Y) S7 ^$ I下面是两个示例配置,php虚拟主机:
& M( v5 O" L2 m/ l1 u/ v( q) u2 j+ m( R9 ]8 Q0 F
server {
7 y- F& I f' v$ h6 O% Y listen 80;
% C4 v& F5 C$ ]2 o# A server_name 52os.net www.52os.net;" Z: v4 N9 X; i9 O
: i- H: c2 B6 S: M
location ~ \.php$ {( y) d2 Y: G) D! {$ l+ q
ModSecurityEnabled on; " f" N2 s4 c# m0 {
ModSecurityConfig modsecurity.conf;% P# |2 }! u; a% w
7 M! S% ^% A H* A4 @ root /web/wordpress;% ^8 A3 D, v" a- E" H9 u
index index.php index.html index.htm;$ C( n) S. u* ~9 q& r
! F% A1 Z) _( d$ A Z
fastcgi_pass 127.0.0.1:9000;2 C+ I+ y$ K3 V. B/ h: `3 d( \
fastcgi_index index.php;/ S( }9 ` `0 E* p8 o$ S
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& n1 x+ S a7 ]: N6 X V
include fastcgi_params;) d: N% f* o! p- U9 l% @3 Z
}
9 D1 S" f( X Y }$ b6 O8 \# l; y: \& G( n1 z* |! \/ Y
upstream负载均衡:
4 x. a) ?; O5 e* w R0 }$ }
$ n+ S8 C9 l2 j* Dupstream 52os.net {$ O0 [9 o: u; ~0 V: [ `- \# g
server 192.168.1.100:8080;1 L3 S# q& J; u' b- e, S1 J; f
server 192.168.1.101:8080 backup;6 T2 v/ Q1 b, H1 r9 p3 ?$ `
}
- L$ L1 g* J9 M" q1 ~
" P3 I$ o4 W* b. ]3 mserver {
7 d& Y! F1 b6 ~6 ulisten 80;& ]) H k9 \+ P/ ~& c* |8 b
server_name 52os.net www.52os.net;
) c) Y7 E$ v" B, T- X! s" U" m3 n% m: y8 w3 N. P
location / {' f p3 D4 @1 Y2 N8 D$ {
ModSecurityEnabled on;
) x$ _. q9 H. v. U ModSecurityConfig modsecurity.conf; ) Z7 ^0 F) ^& i# y( ]0 p# h
5 Y5 `$ T7 u' Z! R6 T5 S
proxy_pass http://online;
) c t/ ~& @* V) A$ _ I proxy_redirect off;
6 e8 V+ W, G. Z/ p9 \7 v proxy_set_header Host $host;2 u" ~" d; C: I5 [# b
proxy_set_header X-Real-IP $remote_addr;+ V0 M2 K8 S ^6 [% _
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 Y' { q; i% Z/ v0 p( q1 q+ U' m1 v
}
) r' p1 c* e: ?}- s5 [8 q G& N
六.测试7 ]8 H3 A; ?9 s* x7 ? F" P7 d% y+ j7 f
$ c$ x2 D& ]4 `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# K4 i: Z: s1 T3 D+ \
# M: O9 u! n5 y<?php
# I. V( M+ O# p; f; c* @ phpinfo(); 2 j) o" n1 E% W: c$ J4 c0 f
?>! D" ~5 j! N' S/ E6 e7 h
在浏览器中访问:# b; ^$ |$ H3 l) W7 M. s
+ k6 x0 v9 f8 H. e+ g
http://www.52os.net/phpinfo.php?id=1 正常显示。
9 h* z. b. m" s: D' x yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。) p" ]0 y+ E3 T6 g8 R) k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ _$ Z* Y" P, b6 v
说明sql注入和xss已经被过滤了
7 p6 |# r* f; X' P( }1 f" y4 E2 L& F( [
七、安装过程中排错
2 A0 d) b- U* Y d* q7 I' }/ ^& k
5 ]( c; R9 s4 q: g1 p2 l1.缺少APXS会报错
( N2 _7 e4 n! n/ H9 l8 q
1 i+ }0 x' a1 g4 z* D/ m! @configure: looking for Apache module support via DSO through APXS- I2 J+ M" ^% v) A9 \$ _2 H/ i0 T
configure: error: couldn't find APXS
( ^' b; ~3 X$ ~0 ?# S- _9 H! d! `apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 O& V, [. J$ {
解决方法:2 _( z2 o5 j3 \
1 ~! a( u8 ~' o ~! @$ Cyum install httpd-devel
2 n# ]7 [3 |% r+ M. r# m+ M7 F+ U2.没有pcre
) C% k# y1 ~) W! O; E, T. n8 B
- o( ^5 O: c8 E" S3 H- mconfigure: *** pcre library not found.( O5 o9 B0 W4 V9 R" Y9 j' w; i* [
configure: error: pcre library is required$ y/ z3 K' M$ U2 ^/ l' o+ u
解决方法:
+ K' I; }' ]8 T G6 S0 k
2 N# G/ n+ \( B3 `( \6 _ _1 V; zyum install pcre pcre-devel
( y6 |6 W* i. a6 J5 ~8 e" o3.没有libxml21 F) c( J$ y8 c1 C- ^
8 _( A! [8 X- _% `- k0 B$ q( b
1 [$ {1 Z- Y- L2 kconfigure: *** xml library not found.5 {) M! l9 ?% E
configure: error: libxml2 is required
" R4 Q& }3 T! M$ H! Q$ t解决方法:
# M: V, ~0 B2 {6 d/ z+ ?/ I
. H9 e% p/ v. z$ m- d% D% u: eyum install libxml2 libxml2-devel' X0 ]& T) [3 b3 h
4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 Z$ C% t0 K: ?( r( V% [' t( c, e9 Z- b- [% H0 e
Tengine version: Tengine/2.1.0 (nginx/1.6.2)! M5 A9 \7 k1 y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 R* V/ i! g& m8 L7 L! |$ I, G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 ^/ k0 G0 ~# l. u+ k: x9 R+ a
) U. _' e [" P/ z( V5 m% m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ f* t+ l& M( ]- k A( K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9", p9 n! D6 c/ Y# K" b3 x9 X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ ~3 I! {' O. r# I! s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") H. y* U1 J1 u5 L" x$ `* `6 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; _* n! i$ k; A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' @2 e; R$ h" }) f8 e" L- c$ x: j
解决方法,移除低版本的APR (1.3.9)) {' a( g, I0 n @- \2 M+ I; P! w
+ o, \: T) L1 u0 N
yum remove apr |; i3 k# \8 }* t# `
5.Error.log中有: Audit log: Failed to lock global mutex: M5 O) {; @2 D3 P3 [& A4 }. n
# n& M* K8 A) @+ q3 @7 }1 v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 K9 N% G- E9 o* y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
J# q6 T" B1 c! h解决方法:
* f1 |- A( ^& l( Z. @编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 c4 D0 N& S9 \) G' S \
A- S) ?5 r. I) ^
SecAuditLogDirMode 0777
% A; b6 Y* w9 f4 |) P* h+ n7 bSecAuditLogFileMode 0550! Y( T" B& J- J. D/ a0 W" v$ E
SecAuditLogStorageDir /var/log/modsecurity
# a2 Y5 m6 [: k9 q* ]. X; K2 ~SecAuditLogType Concurrent Z. A( I, S# k1 u/ l0 o- f
参考文章:2 F" N0 \9 ^9 _4 [6 f" m
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX @5 ]+ a1 @4 o1 P! c" A: t
http://drops.wooyun.org/tips/2614 |
|