|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 k% C( e% q0 k- _$ \4 Z
, f: ]) W% I1 |6 L) G/ D: ]( d一.准备工作% F- h9 i6 v" r% t
* a& `4 B& P. Z6 r0 ?; \
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, ^5 H: K2 x8 ~5 H% S4 o: L* J# v$ ]
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) ]5 y8 v4 y) n0 z' P$ w7 H, s+ f1 i; Z3 _/ m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" x+ w& Q) a; q$ _) |. R; _, @+ d" g0 ]6 L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) Q3 X1 a! S! I0 L3 v
, ^9 M Y3 m4 w% ^4 f, I3 u依赖关系:
1 `. P' A% ] w7 ~. q, `7 h+ Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. y$ ?" b7 q4 p z9 U
. u* ~8 k( B- d" ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 G6 J6 R' W2 c6 g; \$ N: xmodsecurty依赖的包:pcre httpd-devel libxml2 apr8 ?6 \9 F: ? M
: h) n! L' E2 a" Z4 V- @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel9 C; W E3 u/ f) S
二.启用standalone模块并编译
% |+ x4 P1 C* [/ P, `( B9 J
3 i5 |& \1 v! t0 d; c& i下载modsecurity for nginx 解压,进入解压后目录执行:& ^& I* X3 u7 a7 y) Z9 @$ O$ h
0 o0 Q3 e* V" a
./autogen.sh8 y) E: L/ A& M; Y! ^$ }
./configure --enable-standalone-module --disable-mlogc$ M: c, b: D3 s/ X
make
1 @) B6 x* Q) W# Y三.nginx添加modsecurity模块8 _% B1 d- B/ }& a
/ M {) X) c, g' G* H* e: h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 L" V1 K" J7 |* p
, p+ i1 ^% }3 M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ N' |/ }" I% R
make && make install
9 {( V$ {3 x, m6 N& z四.添加规则
, Z: c* x. o% q# k$ c3 z3 w9 M/ S, M6 \3 ]* a/ q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- t/ Z1 m! `+ v! ~' a) j }" f
5 a9 E$ P# U, h% |8 ], L1.下载OWASP规则:7 D! t) ?/ N5 m, C5 d
% _7 k6 W: U9 c; J2 E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ x: E q2 u& E( C3 O2 {8 F' g
. ?# f4 E5 j3 Imv owasp-modsecurity-crs /opt/tengine/conf/' K9 O8 [ r* L/ W
9 B; V F M# G) f+ u; qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 t; G# V7 F' ]' G: o( n6 q: p
2.启用OWASP规则:
2 F& z0 _9 [5 A0 r3 A& E" A- z. R1 @3 b
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ H$ h: J2 }2 [5 b4 Z$ V/ e% t3 h/ K2 A; J8 j9 a& V; E
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% _# f; p( C9 B2 }$ h) t: n
' U) \6 P$ C' C+ m: aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% b3 T) V# `6 L! {: @4 H8 u: _) N
1 U/ Y- D0 j6 s6 s$ p0 T3 YInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 u! k/ T; ^) \) A" @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, H; D. ?% t! {9 rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 q+ ^5 g7 [( s8 r( O# k3 @# H) |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 W- i8 K6 s% ^; T/ r2 i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 G. I5 B' h! O% D$ z# j" w* yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* p0 c' r2 `4 A6 z( g7 e4 u6 jInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
Z* N" i a" z d# l五.配置nginx' t. o$ P. T/ k5 ^
# M: `2 j9 o K! ]# k( Q在需要启用modsecurity的主机的location下面加入下面两行即可:$ r* }- _! b+ ~. ~! Q3 A- p" }
- D3 u, f: ]7 j, b
ModSecurityEnabled on;
5 I* W# V9 K _0 Y9 ^1 v& T2 _ModSecurityConfig modsecurity.conf;
& E3 ?4 \, M) z下面是两个示例配置,php虚拟主机:
5 `% J% k7 N( C/ }; i+ q, X Y. y3 z: J* {* z3 I
server {! i/ O* O2 A% ]; u7 n J
listen 80;
" {$ U+ c" |1 V. h( A6 J server_name 52os.net www.52os.net;" _. n( U, z/ A, n/ t
' M I7 t; y/ }7 H/ |# w% H5 H9 R location ~ \.php$ {7 b+ n3 O& P0 U/ L5 u& }
ModSecurityEnabled on;
. @% ~# u5 n" ^' U$ E ModSecurityConfig modsecurity.conf;3 K j4 n! Q( h- O# s
& G* d( W+ _' P8 n2 P+ o root /web/wordpress;7 \0 o4 j0 C+ B
index index.php index.html index.htm;
" f, h3 s) I% ?% u$ o' J: E 2 N6 k5 q, y3 L% |; d. @
fastcgi_pass 127.0.0.1:9000;
5 \% A2 o) V+ k% a6 L3 n, Q; P fastcgi_index index.php;
3 N6 `: S3 e, x7 B8 z fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 y$ L4 ?' J% B7 C8 ?. a include fastcgi_params;
0 V& U7 ~( o) k }7 m4 `- C' o! T+ }6 n0 M$ \
}# A+ P8 `; n, U7 y
upstream负载均衡:* T$ e2 \7 Y' O. |/ b) ?3 J% a
% J0 w5 K5 I+ }3 D. `: }4 Mupstream 52os.net {# Z, O' W- g; t
server 192.168.1.100:8080;: d" k) C0 {; R4 F( m' R0 {
server 192.168.1.101:8080 backup;$ A s" k- R9 s# Z
}
* s. X! @3 d r) a! R) H& Q, W
u h1 U- n3 ~/ Y% J& {+ ]/ ]: sserver {* }8 E: D$ I1 u+ K$ S; D& Z7 l, N! @
listen 80;5 ]. t. ^( r: h# M3 T" O6 Y$ p
server_name 52os.net www.52os.net;; v0 @7 x4 }% b
1 E3 y8 l5 ?# Y. ?location / {
" X8 ?) R s F9 M3 V! k: ` ModSecurityEnabled on;
% y+ O+ ^4 O! Y" B9 P& Q ModSecurityConfig modsecurity.conf; 4 e$ e4 F; x6 S
0 L. v" e# K6 }, L0 P# W& _0 B proxy_pass http://online;
3 S" `( j# R0 V2 o h proxy_redirect off;5 v9 d! n$ ?8 m3 c3 G" g
proxy_set_header Host $host;
. U, S2 k# w0 p- F1 [5 C) l6 U proxy_set_header X-Real-IP $remote_addr;
6 V; y2 [( [+ a# @$ ^! q proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 N& J% d( i' F8 D& i' z7 C
}9 T U6 I9 X; ]+ J4 r
}8 S D& _3 X8 K, I
六.测试4 e$ e. }( v8 e4 C$ J
0 C. k* L% q' N2 a0 c( d/ ^0 @
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' z6 N" I% g" i% v: q: j
9 ~/ H8 l: I: R<?php
$ `* t9 u) T- K/ A: s8 @2 ^2 C phpinfo(); `' u. A% |$ ^+ d
?>2 b. D' y/ \# p9 n# o$ I
在浏览器中访问:
0 s2 q- q, m; T# U: P+ e! j
; y3 }2 `; g* m4 T7 M, ~3 _1 Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
, h# U. ?5 q9 H D3 o. g4 b! f6 Yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ z# r6 v" |, d* k. @
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# d& W5 R; s6 z5 z- q3 E说明sql注入和xss已经被过滤了, [# W. U$ T* ]* N5 B$ f
; Q2 y/ h( D1 Y" _/ t8 M* A
七、安装过程中排错
) r3 }( Y, [+ q7 {8 o: C
9 }; g# e1 Y c3 M& Q% o/ u$ N" R3 N1.缺少APXS会报错
* _/ \6 @9 C5 W9 u/ _2 P+ s9 S/ V/ H2 x* t) P! I0 d( o" z
configure: looking for Apache module support via DSO through APXS! s' h# \, N. R- C+ @, H
configure: error: couldn't find APXS- ^# M* z* H- ~( n3 D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- x. {1 P M: `
解决方法:
- ~$ V1 }3 q! l$ s1 h0 B5 d1 e& M8 s' G9 m
yum install httpd-devel
2 }1 y$ u* k5 M4 ]8 n1 O2.没有pcre& i1 B6 {) J1 ~9 |3 |
( y8 n. B6 l3 Iconfigure: *** pcre library not found.
, o' d; g9 U! k; Vconfigure: error: pcre library is required4 y7 C0 X! q. v% F8 G% {3 r
解决方法:0 n7 C; b+ U2 J
- o# |% l# } E' ^/ k. C, d
yum install pcre pcre-devel
% x6 p& h. O% y9 G( _1 b: w; b% X _3.没有libxml2
5 B* U1 z# e7 G2 ^& r. u$ o7 v
( w5 Z8 E4 H' t' b% F$ _7 f, @
' H2 z2 q' T$ u. D3 h; Uconfigure: *** xml library not found.
5 M; H T+ g6 \2 s* Aconfigure: error: libxml2 is required
9 l9 q. R; x/ D1 k/ c解决方法:7 {0 h5 z" F# J" Q# N
+ V2 d: n9 h3 r2 o1 |+ X5 Xyum install libxml2 libxml2-devel) r0 i6 Y- r, t1 Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 W/ Y! ~9 _4 {4 P9 Y1 z+ k, s8 s- [, d; U' ?3 |2 J
Tengine version: Tengine/2.1.0 (nginx/1.6.2), J( y1 x3 U; N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% j5 P1 I2 M4 @* W7 {8 l2 m' m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. x: o& W2 S% h6 N& W, L+ k4 Z4 l* v0 p5 l/ h; H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, z' k- h; G7 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"0 X- K& d0 ^2 i% u: m
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
w$ j* Z( P8 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 L% y+ o* t4 K5 q/ m4 \- o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 t! r7 t9 E; A0 J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; r" a- V. V. A* i; k9 \0 h6 @解决方法,移除低版本的APR (1.3.9)7 B O/ t8 T/ d( z
$ a& x5 S( T0 h: Z7 ?5 ?, J" G& K
yum remove apr5 q7 }' I, p3 W' T- M5 ^4 o1 E: {
5.Error.log中有: Audit log: Failed to lock global mutex& g6 r( [/ i( E; W1 ]6 a
/ l* s7 C* u+ S+ K
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
6 a2 i3 A1 W/ pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& S; y6 f% k6 i/ m7 M解决方法:8 O6 @# L. E4 B2 ?* y4 w% N
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 F6 T @# Z% H. `+ }* p7 ~
* o0 p4 ~6 N) N, Y
SecAuditLogDirMode 0777
% g7 w8 b1 }% k9 ~7 ^$ FSecAuditLogFileMode 0550& R8 D% a% j2 h& S5 c
SecAuditLogStorageDir /var/log/modsecurity
2 G3 Q% g- a, ?$ o5 ~4 c5 pSecAuditLogType Concurrent! ? c! U4 b& j0 ^3 c; h/ L
参考文章:
7 q9 }, H0 T" Dhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& Z3 @9 [' s- t9 q# q" J4 ~
http://drops.wooyun.org/tips/2614 |
|