|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: C& Y! o% F" }% f& H( W* X% m- d( {" h9 M! N$ L& t
一.准备工作, c7 c2 {. Q0 G8 ^+ T5 _
0 C, l G& x) S3 L
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) a0 H& k0 d' L0 E! N( g
: X2 e) j: P& k2 s8 Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 P" A7 `! v, V2 ]7 N
) l# z4 j+ r; h/ x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 h9 R6 ~8 t7 F$ ]8 y
# f: H7 \9 N, \( b6 E
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 R/ w' z% {1 i; c- B
* W: m$ t5 C& w! o依赖关系:" ?1 _ a& t0 _ l- D: F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, Y0 u+ m- f$ d
$ k" T/ A+ L2 ^5 X# C, O
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
( q: I! K( q# Y" q+ i- bmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ o- E. u' g6 z* y/ ~' O1 O9 a
. X- \2 I1 k, b- K7 Tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 Q, N4 H& U* I
二.启用standalone模块并编译8 X. \& Z/ X6 G$ N" Q3 k. ~. o
' H* E) ~* S8 u( S) u5 P2 t' g" a* i下载modsecurity for nginx 解压,进入解压后目录执行:$ J. h0 V7 g- r
) X G' X# {, _1 Z' g# w4 R./autogen.sh
$ ~) q6 @0 L/ E./configure --enable-standalone-module --disable-mlogc
/ `: k, V* d( ]2 J; q t. M6 h jmake
' t; L, f: a; n" |9 S3 A* h7 D# i三.nginx添加modsecurity模块
0 ~' A, o& c: V$ d% C/ f
6 V+ R& k j. G' m1 g, J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 o; [* A) p2 o# b, v
l7 j% E9 @, L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% z0 W5 c( G9 }make && make install
. X: s1 U I+ L/ z: t' w" w四.添加规则
) v5 V, B$ c" h# n& \# A5 R8 K! y/ Y2 B0 @/ T/ q% U5 Z8 e: D" J9 R
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# E% k# Y- q4 Z, D- o3 g9 N4 @3 ~$ C" _( y" `2 u. A& e
1.下载OWASP规则:8 X2 I4 I/ \! }! f- T
1 ?* a9 M) F/ m/ A5 E f
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. \: Z* p; U6 L, i- D1 C( v! i0 s2 p; t* S3 L4 t4 r5 S' V
mv owasp-modsecurity-crs /opt/tengine/conf/
& l3 h9 [# i4 d: J/ N5 D. P, R& @. o
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! J! x, ~1 E; W2.启用OWASP规则:- Y% o/ y* k5 X B* z
; k1 o, }9 l4 w% b" k+ O8 V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ Y! O6 { N# G. _8 C
- _, W: F! R0 c6 Y& l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. ~3 {2 ~+ e* M0 x# h3 i: ~) S
" ?8 I1 s) O$ R, D1 _8 Z- `+ cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* a5 c2 c: z6 ~4 w' c8 o+ s3 s3 |# I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' p2 ^# ~4 g/ M: u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ \( c5 V" I/ [) { n! F, H- O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. P( s3 T- Q7 r2 |4 y# M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* b& ~. W5 B$ g0 e( D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- N5 x+ K: S. c, K0 P4 ~% `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 M9 [( p* Q2 {# d
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 S7 N% e- U1 _1 |$ w; ]" e+ G2 s
五.配置nginx
6 U1 M/ s" {: f1 Y |! m f+ y2 m3 T# [2 y
在需要启用modsecurity的主机的location下面加入下面两行即可:
# ?; z' K# |: {7 a$ F/ `3 e2 U2 f$ V
ModSecurityEnabled on; # g( ~" A t/ i" P
ModSecurityConfig modsecurity.conf;2 @: j+ b1 \6 |4 G
下面是两个示例配置,php虚拟主机:
5 x Y) R6 M' M. [1 M8 d, h
1 l! a: \, n, b9 g/ v" m {server {7 |, S8 s0 Y; F
listen 80;
; q: q& \: q& ?6 h+ K3 H server_name 52os.net www.52os.net;, @4 `2 o0 X- [3 F. x4 ~$ d( g
4 h" _( L5 [+ }9 h4 D+ i location ~ \.php$ {3 p; X, o( V7 |0 t: ~& P
ModSecurityEnabled on;
! s) U9 k" |6 t9 `' }) z ModSecurityConfig modsecurity.conf;
8 a) ]/ p( C' b% Y0 ~4 R }5 O1 a) o* @
root /web/wordpress;! `5 D, Q& L4 }, R% ?. T
index index.php index.html index.htm;6 W* D8 U' S* x3 ~2 C+ Y" @, E: c
' g- s j& o. Q3 B# a% n# A fastcgi_pass 127.0.0.1:9000;
# U+ C2 z, ?6 @ fastcgi_index index.php;
6 W4 ]! }! Q- g. M fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 M( |( }- s4 o% H1 ~. j include fastcgi_params;, J# \5 X) G7 x: H" D
}
" U% V- v. t- H' b }
( \! N9 p' r( m1 Rupstream负载均衡:
) z$ D" ? W8 ^5 @
" c6 F5 U+ v% O {8 J" w) h6 |# X% mupstream 52os.net {/ o/ T+ e1 ]# |$ O/ q5 ^% b' W& T
server 192.168.1.100:8080;
8 ?/ e" L" ~$ ?/ o server 192.168.1.101:8080 backup;5 E9 C) Z$ ~/ n1 J" k5 e
}1 L) G3 s! x, k$ \) p
( O9 ~3 h0 m8 e2 t. p* W
server {7 [, D$ m$ ~0 b
listen 80;
3 H4 g7 p4 L7 d/ }4 b/ W$ }+ P/ m- ~server_name 52os.net www.52os.net;/ |+ I) N) D7 _
+ }5 F; C( N& m6 P3 W
location / {- x5 b5 `& n0 f+ a% G. @7 B
ModSecurityEnabled on; # j: [9 A% |( Q: \
ModSecurityConfig modsecurity.conf; 2 c ~1 x6 M2 u$ _
+ `9 c# l1 \% }+ z/ W7 f proxy_pass http://online;
- E+ p) t4 `1 q proxy_redirect off;
: V/ {6 Y, r+ O6 I! D proxy_set_header Host $host;
/ s' f; T. z6 }6 w3 r2 |2 r- W1 ]) r proxy_set_header X-Real-IP $remote_addr;/ C' J! U5 J3 ?% [/ i! J6 n. d
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 q; F) u$ X* f0 X9 @1 T: K. d
}
# @/ d+ j( M2 B o, i- m) [& a}
8 Y7 \) p* g1 c( i0 R. [六.测试1 K! E" M5 o# p1 {0 ]
) z' x* v, i; k7 c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ F' F, Y; O! w! [) ^& F+ o3 g s# C3 u) B$ a6 [8 ]
<?php
: N4 y* f8 M6 h/ P/ n9 K phpinfo(); 2 A9 Z& k5 t t4 A9 o. B4 e
?>' _" H `3 X- y" X
在浏览器中访问:
+ j# N) I$ I7 M
# @# F0 X6 N7 P! i' u8 y0 _. thttp://www.52os.net/phpinfo.php?id=1 正常显示。
4 B$ B( T" c3 T$ K) r4 s6 \http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
" `0 @) l3 l5 V; zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) K8 U: q& `5 I1 D
说明sql注入和xss已经被过滤了
% v- C6 m# r, |- r6 g& v! k0 H" @2 ^ J6 p( \
七、安装过程中排错
" F' P. ]1 V* p- O: f! W( I1 M5 }$ B t$ P, Y
1.缺少APXS会报错
5 f& ^% k& G+ z7 y2 F
7 E6 ^, ^9 Y4 S: O. U1 lconfigure: looking for Apache module support via DSO through APXS2 m z! h" H9 ?8 @9 t
configure: error: couldn't find APXS3 Q- R) {, ^/ [- y: {# k7 A6 I5 n; Q
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 {) h' I; V/ G3 J; w4 A
解决方法:
. P+ z( Z+ u' }* ~- @
$ ?1 }/ \" [/ @$ Q5 Oyum install httpd-devel& N* a1 |, A6 O6 V; f: K
2.没有pcre
, S" X1 X Z' s: j9 Q! I* |4 P$ W& h$ d7 F8 k
configure: *** pcre library not found.+ \$ E5 f4 P K' K* r
configure: error: pcre library is required
9 T1 L2 U; J7 S/ |; B+ W9 a8 C解决方法:
3 G/ D5 A- U9 h* ~% h# P* n, g+ X; H W& D# r# S
yum install pcre pcre-devel! d/ ^$ ^0 o0 b4 z* J
3.没有libxml2
7 | i+ p$ J- p0 z; x, n9 W7 G: P/ x" ~& D- O2 \( V2 c
2 c+ O4 n: A2 |# R2 y
configure: *** xml library not found.
; a2 l/ H! ^- _8 r- {* ^configure: error: libxml2 is required4 Y* V- b9 ^' |: i% N* l
解决方法:
: L2 Q* s8 x' ^- Q0 v; i
! S3 O5 V, S% T0 U3 ~yum install libxml2 libxml2-devel8 B2 F. \8 [- l# L* \
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" l/ K2 `9 ^ x0 z
' x4 K2 o9 R& p2 Y- e0 k$ `1 L( TTengine version: Tengine/2.1.0 (nginx/1.6.2)
- l3 t K4 T- q* W+ dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!( q& l& x ?) ?. j
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 k5 b8 E& C$ @) b- R* U8 f& E: c5 e0 p' d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( f6 T8 b% u# l1 |( v6 O) A. E y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" L- e0 i% Q4 L& L+ h3 t' \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: ^6 t/ ^) b `9 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) F3 c5 b( U7 l. O& J6 p: m2 Q) A& S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 T6 E8 Y9 v. N b+ |* z& w
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% a) l" v/ r+ F1 m$ X' [$ t9 U7 W/ j
解决方法,移除低版本的APR (1.3.9)8 f3 d; n4 b- R) u
/ l0 n9 ~9 Z- q3 u
yum remove apr. V0 j. k" c/ P5 D1 z, i6 y& g0 _
5.Error.log中有: Audit log: Failed to lock global mutex- v% X0 X+ v& u$ @- q |" {3 H
7 y1 p' g6 M) l. x. ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
& L; U1 p" D3 N1 B7 gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
X, T2 O5 c n- e/ }$ K, ?1 j解决方法:
, r) @. u; ]- r: v- p! Q/ D& g编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& H7 S1 E& g$ L
9 C- L3 `0 T( K, N$ f5 f/ ?
SecAuditLogDirMode 07775 n1 v/ y8 O& H- s8 i' F* i
SecAuditLogFileMode 05509 ?+ s. W9 W' N# _: y \
SecAuditLogStorageDir /var/log/modsecurity
7 s+ }/ }4 N+ z5 ]; H: sSecAuditLogType Concurrent9 Q! m3 i9 v# L# k" a. ?
参考文章:
% T* a) T& O% [" A" }' c3 s% |https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 a! t$ a& r) Q/ Z+ A9 w
http://drops.wooyun.org/tips/2614 |
|