|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' O I) J) T: v# q }' u8 \
1 L" t ~6 n: E
一.准备工作* D% B' J$ G$ K% A
- C/ O" ]( B/ J! H- U' i系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. v8 b) j6 ~% }" d) I V. C
; l: |, b! T1 _! mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ @. W# R2 Q, M8 r& q& K5 k1 E. t# U% a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
2 W7 J' s' U* Z5 G+ R
3 z( o; s/ N9 f: N9 Z0 t8 T. B3 nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ J$ P" P+ l& x3 C% F" H
3 y6 l4 Q6 }; P% F0 V
依赖关系:) M# J/ d' m5 M) B0 Z/ y% S4 c
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ }* g3 j. M( m) g! h" g9 x
+ {# p7 K* }* Q( K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ w! k; b) k* ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr
, ~; ^' o" G _. |) E- G6 o+ a2 T
% p. c4 E) M* X5 Z% tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% {' N) e9 d# T" @
二.启用standalone模块并编译
1 U" l1 l1 d7 s3 p' g% Y; z4 y' i) x* H4 b {
下载modsecurity for nginx 解压,进入解压后目录执行:4 R/ e6 ]( [7 k) I
. P. p0 f# m/ v! r0 c
./autogen.sh( R! n, e' k. f. V6 O: o
./configure --enable-standalone-module --disable-mlogc: I& q1 ~- P7 f* H/ C% n
make G* J8 G" L) G$ J
三.nginx添加modsecurity模块
' }; K* D6 l+ h. a5 N. [6 S2 I2 Z+ G( Q9 m( f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 ?+ _4 {9 M9 l9 c1 d
! M; O/ G: W( [& u, J$ M% \./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% g0 n' Z+ \6 ] [0 e1 r; M
make && make install
' }; o! E9 H# K' y; U* `四.添加规则
: _$ c( O. n2 s: x5 N) ?& ]* l: F9 a/ ~: @; f. S! @
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
; {4 [9 d2 x/ B
1 n7 U8 G7 j9 \% }1.下载OWASP规则:$ q) A0 d( D/ a& h
$ c- z/ p+ s" E% [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" [5 N9 y* s) w. N
7 ^/ `* q+ `; Y3 n
mv owasp-modsecurity-crs /opt/tengine/conf/
- P% @# D: r. g1 d/ J4 G" V
" R3 [' s h7 N& jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. D' \, K- ~, x8 y( R1 w2.启用OWASP规则:- q# ?# ~% u4 b6 G# n
3 ^) }) t- l$ y' ^, q3 R
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& [: [6 C9 ~1 m- F8 O" o2 j: o" u9 D# O& J
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, ?0 M# n+ F# {2 Z: j) S
, ?. u" B4 a4 U% k" xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ F+ [* [ s7 W& d: Q9 P* H0 ^& }6 q* I% n4 J1 V) c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( h& J: X0 g# p5 f6 G! d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ U: w' y5 ^/ `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 C @3 i& ]! D7 x3 ~; Q* vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 s7 p& c. t" b& E- D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# }' k2 [2 X* s1 T( Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ Q9 H X4 d. A* s7 c" |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* r, j: w7 P; b) o \0 v5 R
五.配置nginx) X8 t" B/ G" e; Q
9 f: f: o; j0 F, ]# K% g在需要启用modsecurity的主机的location下面加入下面两行即可:
- U, |4 L" d; ^2 a7 t$ _" }/ W( ?# `$ ]' f$ j
ModSecurityEnabled on;
2 x* e1 c6 D: X* T! QModSecurityConfig modsecurity.conf;
* j& `1 ?8 F# o下面是两个示例配置,php虚拟主机:
3 s' _% [2 o6 d+ O) s
6 e" h6 H, h4 b2 W: Lserver {
; b* W5 N6 f# ^# g# [% Q& }' p listen 80;
- b8 I7 S- _+ t+ E; w# t0 N% J. C server_name 52os.net www.52os.net;
5 Y: s" a. j, M3 i& R$ X! e
0 P! G" E3 N. N5 r6 I& { location ~ \.php$ {9 _/ n$ G+ {- P; j8 Z, C
ModSecurityEnabled on;
3 x1 k: } h) t, ~/ E3 B6 g ModSecurityConfig modsecurity.conf;
0 q) ~0 X7 C- J6 X( F7 ]
u4 @6 ~$ n/ S* W3 m. V* m# k root /web/wordpress;! [& w& N$ q% D& }
index index.php index.html index.htm;$ s% ?0 v+ t( a/ e/ `; a7 P
0 F3 L+ b' x1 B# {) l fastcgi_pass 127.0.0.1:9000;# U5 N+ G2 ~& J/ n7 _ I: T
fastcgi_index index.php;
' C& B2 G+ d9 g8 k1 y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 z7 o3 v4 W0 [8 f8 R2 h# @
include fastcgi_params;
. I1 W, V/ k6 Z& g" p% o }
8 O' H( B. `9 s! l( \ }; T h, v$ N3 i* y4 a5 z, p2 R) W
upstream负载均衡: H: c2 {! c2 D4 Y4 B
8 `* |% H0 G+ I1 }. F" ?( C7 Bupstream 52os.net {. r# L* \% w' h; X$ _) w
server 192.168.1.100:8080;! g) |& h: U0 @/ R
server 192.168.1.101:8080 backup;( T/ m* \. T) Y6 c
}1 ~1 L$ I z5 Z A
4 x$ |) E( }% n2 M" w
server {
5 ]2 H4 C- T) b g W) Nlisten 80;( Z8 t7 e! \5 h+ i: ~
server_name 52os.net www.52os.net;
4 Q- U# J& D, e9 g' L+ K5 b. |
6 D$ G) l+ T" p. Zlocation / {3 T6 ]2 l/ q* c% o, n N
ModSecurityEnabled on;
0 Z* L! Q5 @2 `9 W( N1 I) X ModSecurityConfig modsecurity.conf;
( `4 E5 j+ C: i1 K
/ d- ?7 x* b/ a! Z; c proxy_pass http://online;; H) Y4 ~( B% s+ C3 L
proxy_redirect off;, @/ t5 [' M# o% R1 e3 c8 C" |( B
proxy_set_header Host $host;
) k" h2 D8 X U0 _! ?( Y proxy_set_header X-Real-IP $remote_addr;7 |/ e$ e# A3 E- U1 M: \0 f) @
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# E& q# u3 b0 V
}
, j: B9 l0 y( x# }}' n# X" O, G& g' C
六.测试
5 }+ ~: |, \6 e6 c9 M$ l: p6 }9 a% f6 R- ]4 w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# ^/ s4 g+ V8 a* h
. Z0 }6 e! ^( D5 r- W0 J
<?php0 v( \8 z8 d$ ]$ ?% l' u
phpinfo(); ! E" b. f7 K4 ~3 q
?>. m+ c( d% J; \% d2 g9 o7 s4 W
在浏览器中访问:
: P3 S. R( v/ F9 g4 @9 ~3 G; c4 i7 |, V L$ U+ d8 w9 d$ @
http://www.52os.net/phpinfo.php?id=1 正常显示。
' y- n% O5 E. C; {) x# ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
" V- G a+ T2 K$ N7 nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- L: O) x* M0 S) ]
说明sql注入和xss已经被过滤了
7 U7 u1 K: j) o, s
+ r: D$ L0 q# p& y6 X$ g5 P; I" j( b七、安装过程中排错
4 ^ A+ c+ _/ D d
" I6 O3 w# x) B9 ~- c# l6 A0 A1.缺少APXS会报错3 K/ ~; \. @; b4 [& Q
5 i4 P7 C. W( {4 ?) E+ ~5 k
configure: looking for Apache module support via DSO through APXS
; O( g3 d! S7 a4 k$ b0 Aconfigure: error: couldn't find APXS
7 ]6 I0 j$ O* W7 o( U6 ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
8 Z: t7 P& G' u d+ `0 y; W9 {/ s解决方法:/ G$ Z, I% @8 S8 h$ |
$ { E) I2 `1 K# l* ~yum install httpd-devel- q. r; I7 n! | m8 n4 P5 d
2.没有pcre4 }& k7 [1 Q1 h B( ^
0 \# }: ]1 R/ _5 n+ C( o5 D( d: Y
configure: *** pcre library not found.: q; k# B7 S8 a
configure: error: pcre library is required
" X. q s/ o$ M" Q* k! X! {# M解决方法:
: A9 T* c; Z$ `) c( J& ` w& Z
yum install pcre pcre-devel8 G9 s& b6 p, z V
3.没有libxml2/ \% D) W# I. V
: N. b9 I% z3 e, ~ u' F) S8 D# @* I
7 ]9 q: O% D$ i+ x: x' Y1 Iconfigure: *** xml library not found.! A. i+ `9 z* x( B% n
configure: error: libxml2 is required
# r2 L* k$ p8 G% J& a/ G$ M" W# q解决方法:7 t$ A/ t# F( S
' h1 g, k1 Y! \# a1 vyum install libxml2 libxml2-devel
* y, V7 Z5 ~: s6 T# ~2 {/ I4.执行 /opt/tengine/sbin/nginx -m 时有警告
) n/ A$ z( T7 v1 h+ h. s9 s) z* N7 R8 o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) t) @: ^8 m/ ]% Z/ [. vnginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 m5 `9 |, }+ o& T) ]: c# p. h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% U5 x; i' G7 j7 w8 _" V0 m h o! h0 w+ m% J, Q) N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) a8 Q4 u! r6 G7 d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 {% R7 B, O5 p2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 [+ X( k3 \* B X# r$ p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 s: |1 W% [2 D! L4 b
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") K' K* o4 e; A9 m- X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) B# b: O6 @( S) _2 D' Y
解决方法,移除低版本的APR (1.3.9)3 q0 X& b) t- W, |" m* r! B$ w# U
+ ^+ b, e; q9 X. b% I- R
yum remove apr; u- ^2 Y, g* X2 ?" `3 L' @
5.Error.log中有: Audit log: Failed to lock global mutex
0 w8 L |& s' W- Y
0 A* u) V/ }( a) ?2 v! }& G$ k/ E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . V7 e" k+ ?* [ U9 u/ I
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 l: v; l4 N7 _( e9 E解决方法:
% ~, U+ b! Q% d2 W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 J6 {6 e3 I6 o; x2 }
9 M1 ^! n, b, F4 H
SecAuditLogDirMode 07777 ]7 M8 v, ]# c' h, E2 W$ ?6 m( t
SecAuditLogFileMode 0550
3 P# Y9 n( ^# l' h: _SecAuditLogStorageDir /var/log/modsecurity, R% Z0 w# O3 e3 c) D
SecAuditLogType Concurrent
' x+ ^1 }4 G) k( w' ?/ Y9 z6 f参考文章:4 H1 T2 W1 ]" A8 `5 A% J) b/ c
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 \0 N( ?9 `( s
http://drops.wooyun.org/tips/2614 |
|