|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ Z {1 O% |6 }: T5 {1 ^
% y$ k& {9 o" r+ M2 X9 M7 t一.准备工作
' E1 S* o; L& @
8 z1 B0 L" f8 k系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 F! a3 h1 B* Q9 ]
% d0 F5 l6 P* v1 k: ?* \3 T6 |! c6 w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 Q$ p# f0 x1 [& O1 g
' j k% H; Z9 D& F! z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* D& x0 ^& a" v8 i
& w* Y# N9 ]( |, ]OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
A; b! o8 s. `
2 g' e6 t' I, f9 ~1 r依赖关系:
9 i$ n/ ^$ o. y; Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 N* p3 l: x' V# y% y' C
/ ~! O4 t6 s' U! M# y8 L: H
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 s& y- |: A4 n5 \' emodsecurty依赖的包:pcre httpd-devel libxml2 apr
# Z' E: w) t) ?- h9 W7 U Y6 B' e4 H# _! K) B
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel5 w. S7 v: U5 A$ |* ^9 D) [
二.启用standalone模块并编译
4 r1 x$ j% B; q' Q& X6 h
- y: O k8 k3 p4 o) a F下载modsecurity for nginx 解压,进入解压后目录执行:
$ ^5 |; Z" B3 R
: S% i- @& a. C" Y) [: I) h: Q+ }; S" \./autogen.sh* D: E5 E5 C, c6 t9 p9 n) F
./configure --enable-standalone-module --disable-mlogc
1 {& I* a x. [" z8 pmake 5 ?$ D. C+ k* V& f! w$ Z$ D
三.nginx添加modsecurity模块: _4 E Q3 l5 b3 [7 \
; {! L, n7 c: l! @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ ^; }# d3 Q0 d. P* S1 @ S; c. j/ O1 G# m. ~$ J$ W f/ x
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 _5 t: A' `/ Q7 O/ y: C; A( g# i
make && make install
# }4 w5 J5 z2 m ^& }+ Q0 Y四.添加规则
7 C! X9 h8 q. U% v3 Q, W9 U# E w4 T1 j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ I/ a2 G1 W, @8 l2 j: r- b# u0 l/ T% a2 l! p
1.下载OWASP规则:2 \- f; g& I9 m! c9 \/ }$ G+ i
# I" j; v+ D/ Q, X @: f' K% y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 K( @3 i+ r. v
- J1 P6 E( w( p& S
mv owasp-modsecurity-crs /opt/tengine/conf/
) p8 \; z- U- h9 Y$ c4 |; K
8 `- d$ @0 R* c( ]9 d1 s: y) Zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf+ O2 p# ]( {. [' S: m: ?
2.启用OWASP规则:
. d, K" m( h6 N
; t5 w2 ?. }# m复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 G' o4 c( Y+ Y/ s
- }. R$ M. _# Y6 C编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 O4 X% W3 ?6 f) Q
7 M' m- e! \8 g. _$ y; S9 B0 s
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) t, O3 B+ T4 K% ~, b/ s" ^1 N* F
: U. Q! ? R( l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 x, z) V4 r0 ~' D# tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' R% N' {$ M' v M6 w5 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 ?7 w6 |) v2 r4 m; E3 A; Q1 `4 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) K4 l. I% ?8 ?2 z* j) BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 p9 P& a, g" U$ T1 o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf s7 I% z0 e; k. ^9 C" E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 E, J% ~# c2 }" u, d五.配置nginx
3 ~8 l8 `: O& Q( {: h2 j% X
- \4 z0 d6 Q/ {- {: j# f在需要启用modsecurity的主机的location下面加入下面两行即可:/ k1 C: K0 D" R" n8 n, j6 B
0 W% m4 o: {, C2 W! e# Q
ModSecurityEnabled on; ! E6 u% E1 p! c6 _8 g% G8 w1 {( b+ r
ModSecurityConfig modsecurity.conf;% k2 ]6 G& h p& l4 [4 F: o
下面是两个示例配置,php虚拟主机:
1 G' C$ M Y7 d9 R8 s, w: n v$ c# K1 m
server {0 n X7 w+ w! J$ \/ s
listen 80;
* s/ D) e9 d7 T" D, i9 \% Y server_name 52os.net www.52os.net;: ]$ {3 c/ a; R
8 O) D8 |6 j" ~1 q9 ^% F9 l. h
location ~ \.php$ {
6 f) R: t( X0 M3 m3 j3 n2 _# j9 j ModSecurityEnabled on;
0 ~! a- w7 J- d$ } ModSecurityConfig modsecurity.conf;$ x( S F9 C4 @8 U# h3 o
& x1 W# |0 x- [ root /web/wordpress;, w2 d/ U8 i4 T' c4 b
index index.php index.html index.htm;7 f3 S, H- c% h
. M0 H9 i0 J/ W" U g fastcgi_pass 127.0.0.1:9000;
+ C) A6 K( m" Z# ^' E! M fastcgi_index index.php;( ]- q( H& x) U+ }( o1 c
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 ~# O4 M) f% I4 ] include fastcgi_params;
0 E, H( M1 c, g }6 _/ B% D5 w8 q/ |# E" `
}9 M1 e: a. z% s/ {( X! x" |' j( h
upstream负载均衡:
$ ?5 b1 [$ v- x. ^4 F( l1 N
5 D( e1 g$ c) \1 e: cupstream 52os.net {$ ?' [4 t3 D5 ]
server 192.168.1.100:8080;
) o1 R$ L" t1 F$ {6 f$ l server 192.168.1.101:8080 backup;
3 j' X1 `8 B- x. j0 m, U% S9 H* F}0 S- s. [( f3 ?: Q0 p
1 Z) J# ~5 }3 Mserver {7 m( `4 ]- w4 {6 \5 w& y
listen 80;2 g, i0 z1 N5 W& x
server_name 52os.net www.52os.net;) y1 b8 R9 E! j" o2 |, E3 X1 R
+ s. b8 m' X0 ]
location / {
. ^# t! P3 d+ X( i2 i+ N7 A ModSecurityEnabled on; # g0 m2 a3 F) ]
ModSecurityConfig modsecurity.conf; " Z" N8 `- a' Q; n' u
; r- \. f. m, K4 M# U
proxy_pass http://online;# K2 l ~( V- b0 \
proxy_redirect off;
: c+ d$ c. c. v8 a) U proxy_set_header Host $host;- i ?# U& T5 o' r k: a2 F9 m
proxy_set_header X-Real-IP $remote_addr;
- z) w# ]! V9 `: L/ U proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 u- {4 F' f5 |& t4 F- y$ x- s }
- b0 ^. J& f( h r5 H0 u1 Q, G}
% `! f( T/ e7 Z C+ g' ~" q六.测试
/ U5 M( {4 u4 R! g8 T. K2 `4 d' A: E8 m2 u2 {+ o4 J! A ?
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 y4 Q0 M: d0 ~; C+ z# }1 z' e
6 G; R/ U9 N4 [, |) p& i<?php' @6 l* ^1 F. c4 b v {4 {8 z, H
phpinfo(); # H- X8 g% T0 f' D9 U
?>
! U2 x% o( h C2 ]3 J在浏览器中访问:) u. \5 B; ]% y1 |2 Y1 U
" J$ z3 d1 @3 @5 f+ C! n, n
http://www.52os.net/phpinfo.php?id=1 正常显示。. z" X! B1 K0 A K! d3 c7 v' `
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) |( x* v! A' F1 Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
: p% y6 w% j: y6 D说明sql注入和xss已经被过滤了
+ U- M! X7 k' N: L0 V+ e; l' O
8 q! A, X7 T4 C1 L: ~7 Z& n七、安装过程中排错# c1 Q/ p# Y2 B+ r# Z
( b- c( ~, ]6 r3 M6 ]- m" a
1.缺少APXS会报错" V3 l1 p9 m9 l3 A, Y+ R! M4 W
+ F; @6 s! k3 W3 D" M) R
configure: looking for Apache module support via DSO through APXS; x' j& b( b0 f7 L" b
configure: error: couldn't find APXS. Y! i# E) B& }) D- ?9 f+ F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ [; ~ v: Y, Z& h: o! N# _4 @解决方法:
- \3 W4 }( G' B; O1 c0 z" @% C+ {- K
+ t5 h& y3 g2 ~1 o6 R T& iyum install httpd-devel
5 S1 {$ r, P3 r+ O, V/ m w2.没有pcre2 u& ^- Y. T2 ^# b; l
* f) t& _8 N# e9 r
configure: *** pcre library not found.& L6 L2 g/ d! Q( r# b
configure: error: pcre library is required9 r$ j2 E8 X P( s$ }3 A$ A
解决方法:; K2 [3 k: q9 B: }7 r
% a: z* ?, ~1 ^# a% t
yum install pcre pcre-devel" R: ]& b0 Y3 X5 p
3.没有libxml2
$ u3 x4 s8 }9 T# b$ S: n
' J* Q1 G) c3 C$ s% E% n2 l7 ~9 t: R( L" X# {+ k% W, s
configure: *** xml library not found.6 P0 }4 Y: U$ }9 ~- s
configure: error: libxml2 is required
' n: D) q J$ [8 L f' f解决方法:$ h2 X3 _* g: z2 i% D7 w9 n7 u
3 j' |, L# t) R/ p+ R
yum install libxml2 libxml2-devel
% C4 G n2 k {, U9 o) @& ]4.执行 /opt/tengine/sbin/nginx -m 时有警告0 H' f; i3 v7 [% f. b v
! W) T# W" \ `, ^8 P
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. l! c, s8 d& N+ M8 w Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 z, P$ ~% e8 K g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 ^( U8 t! @, C3 L3 j+ }" d) @; n! }. p; F" O/ N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ d7 M7 }) B$ U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( J# [6 Q. h3 G/ T M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) l6 W2 q( I; I' i% q, X6 q _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ w5 j& ~# h) x6 w x( ~. H$ \. q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( s2 c" A1 T% z! Y% w$ ?) O: b6 }3 h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. t/ A! v6 }# R) w$ ?% U( z+ |解决方法,移除低版本的APR (1.3.9)$ e9 p$ T& w8 T
5 h3 e7 \1 W5 j1 ^: g5 xyum remove apr
0 c, @- t% }5 m& x. l# ?% ]/ y2 b% w5.Error.log中有: Audit log: Failed to lock global mutex
4 i2 ^: h: Q! \
* Q7 N6 G# x' J7 v5 d, m2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock # [/ g/ J4 F( `- I7 s3 r! F, g+ ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" `1 V' L# r6 a
解决方法:
1 Y- e% }% k$ I$ ?, w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: c+ [7 d8 c9 O0 U
7 ~3 y" V. A1 Z
SecAuditLogDirMode 0777/ m2 Q6 ^7 r% Q3 Z
SecAuditLogFileMode 0550
+ J9 y0 f) b4 ~) w- c! T6 R4 f3 ASecAuditLogStorageDir /var/log/modsecurity# [- w# Q0 ~$ p3 |8 P
SecAuditLogType Concurrent
) Y; }7 F: c- o5 H0 W3 R! @" u参考文章:. w+ i4 `0 N4 ~) z' g! Z/ C, l
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 B" C* M8 A1 q T' v' zhttp://drops.wooyun.org/tips/2614 |
|