|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 w, B: }' N- y* W9 X- j n
% T3 _6 H( @0 |- g
一.准备工作 c1 v# Y& b8 j* z1 I3 o, x
8 ?2 R7 S, U: q' G! P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! a! H" X! [/ X2 p. W; a: t! x9 x& N5 }6 g& a' Y0 R, W. y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ {0 t% i. P- O1 }" K# I8 e
) @4 @' d( x+ A' \7 t2 omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 L! g" n, x4 J/ U5 d
# |8 U0 W+ N! @6 ]3 E7 H9 dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ m9 N# N3 w1 U
& m: b1 y$ i& M+ L; q& h% ?4 n
依赖关系:
9 M" Y! G5 ]; M9 Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ V' @# V$ Y, h5 y
# y) D8 J6 c9 g# h
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel0 K" }5 J; d; H* Y% y
modsecurty依赖的包:pcre httpd-devel libxml2 apr! v+ h7 x- ~' C* l$ U9 }
8 x+ B9 r' G' N9 Vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
6 i, d. p( m8 M1 i z二.启用standalone模块并编译 _+ T0 W' H5 V1 R9 l
7 B' v7 [0 {: W- o( v' j
下载modsecurity for nginx 解压,进入解压后目录执行:" y: ^& Q9 B5 i& l6 M
2 b: ?7 L1 W+ x* `6 G9 d" I2 g5 i1 d g7 ]./autogen.sh# ?& c+ o! V& a/ e/ i+ H/ e
./configure --enable-standalone-module --disable-mlogc
. T; y+ [4 {3 _) g6 I! Rmake
" G5 A( v* H9 A' |2 I4 S! m2 ?三.nginx添加modsecurity模块4 S G, L4 q% G+ O! G6 X
2 S& Y/ O- P4 I
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, x9 J- P& {5 w7 G
2 P7 l; }3 P- R4 i./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 N* y* j) U! @3 g1 s: V2 @
make && make install! s" J& x; l8 l- h D+ ^' N. a
四.添加规则
& Y& c Z/ w" {2 ~# k$ m. H( P. f! n% Y; a0 @
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。) m3 j# R: ^& z, d. u- a% j' `: Y
% d: }' ]6 l8 W8 P d1.下载OWASP规则:
; T' g! y, }. e3 l# [3 t4 l4 d/ C3 s1 B q, R2 T$ V$ U1 t# B1 V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ M. @3 A1 W% M
( i4 O$ R, n( R+ _
mv owasp-modsecurity-crs /opt/tengine/conf/3 w9 O; y: R2 J0 l/ O: C8 M
3 I2 V/ x$ |7 K6 Z4 A
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 M0 [* O3 r3 S; J8 `- T
2.启用OWASP规则:
' @1 F6 A+ J, u% h% f& c$ m
4 A- ^! j3 t+ X# X. Q2 |7 z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* j0 f! [5 z( O- k2 {* {# R7 n7 d" F: g5 }
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 Z$ [4 E( o# K2 W P3 ?
( ]" W4 F6 V: ]- x* |4 r2 B6 x5 u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: Z3 U/ y! K' _2 f) F T
$ ~7 A% h8 M! L3 _8 ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# H5 S$ }4 M) C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% e) C" d% S3 f- p P( p% I8 t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" ?/ P4 v" g; F; i+ \+ ~4 D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 n4 S2 R! U5 ?4 U5 CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 r7 v/ @; l! G1 U0 a1 x7 x3 b- r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ w% G6 {- I3 N! y3 }) |Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. I% h- w3 V6 h. Z
五.配置nginx3 [4 O& K3 a, x) J0 v& S
, E( y. K' f# h4 B在需要启用modsecurity的主机的location下面加入下面两行即可:8 x9 }0 S3 u( R5 Q1 J' A
* `& L* [0 t/ x( S7 |( S' B
ModSecurityEnabled on; ! U5 E3 g! p0 ?. f* w/ Y' ^
ModSecurityConfig modsecurity.conf;
! Z$ W% X# a" s3 }" h- @下面是两个示例配置,php虚拟主机:2 P! m/ m. h7 ~" r' V# s/ t
Q! ^+ `. T3 N/ v- R4 Userver {" A& v- _( }5 h5 Z; f3 p' |
listen 80;* P) O& v/ }. f) g+ b
server_name 52os.net www.52os.net;
6 l. K8 I9 V9 Y
( ]' v5 z& O# x4 ] location ~ \.php$ {1 G% L$ R& y& M$ F- `5 O
ModSecurityEnabled on;
2 B8 z7 k& V- q. P2 U7 A3 l ModSecurityConfig modsecurity.conf;
9 J/ z6 f+ s8 q2 |" A% T
# F8 x% z4 b6 ^3 a# {' C5 |$ q root /web/wordpress;. h2 \0 S! I; D4 l! b4 U
index index.php index.html index.htm; r! f* i& Y, @0 X1 {' j1 [) ^
; e2 B/ J W: }7 [4 o) u# |, F' q
fastcgi_pass 127.0.0.1:9000;" ] l, g7 F( X/ G( e
fastcgi_index index.php;
5 L" A% O# d5 |% T. {& b9 P! X fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: T# K( H7 E7 p! t$ K, K* L. R! _ include fastcgi_params;
5 Y" W4 P' i: F3 |8 \ y, U }4 k, U* S4 ^6 u
}( N, \0 j- M0 @, S! h! ?
upstream负载均衡:8 ?9 Z' j( W4 T# P& Z! H) R
. e/ `; ^0 m- X2 ^) C, W0 }. r) O
upstream 52os.net {
$ ~: J f+ G0 U) v0 _ server 192.168.1.100:8080;* j9 X0 E7 O0 U9 U
server 192.168.1.101:8080 backup;6 v8 }8 L. W! Z5 \0 b. C. C l
}6 h& `) [' g6 ~% V4 Q
/ G! Q M0 S, Q) I, d* B
server {; f8 P* G# {3 t+ |5 ~: \
listen 80;! q% M2 i* d6 _# d/ |4 Y
server_name 52os.net www.52os.net;
' W8 |9 b4 v: n9 l# C% S2 L
5 R! Z" t% `, H1 |/ Tlocation / {# x9 v/ C r1 e. {7 I1 o
ModSecurityEnabled on;
, B5 y1 M/ X4 W7 @" m& c ModSecurityConfig modsecurity.conf;
4 x: m( ^. b" f, H0 d) C$ `6 r4 [0 n# K) ~3 Q# U% z
proxy_pass http://online;& G+ @1 G, F0 G0 i: k7 j
proxy_redirect off;* H8 |; y* a) Q# Y9 A
proxy_set_header Host $host;
4 ~$ D/ h! F3 w proxy_set_header X-Real-IP $remote_addr;
# ]3 t& i5 B( s( F proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% A6 Q4 `3 o/ F9 d }
7 i2 W$ _1 J: x; a% `7 Y}
( F% T: g4 I: F8 X六.测试
* s2 p: D. f9 J# y( U2 [4 A, U$ q; b4 C, C) h% m
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
V! c1 z! x/ c: D1 b
" m7 [6 k5 y; S& N+ w/ d<?php
" G0 A# G, n; l0 W. H phpinfo(); , U6 x/ x2 U- c9 t# O$ v8 e; t
?>) |' h5 X/ {. X* C* y/ c0 \
在浏览器中访问:
. i# ?, J+ A# V: q# y- x
5 L! ?0 G) l# y z/ u" Thttp://www.52os.net/phpinfo.php?id=1 正常显示。3 H k7 C5 O$ E6 j: l; @2 o( q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。" c- D) P6 t( y1 S& S0 k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。9 l9 h& ]/ g: d: U0 u% i9 ~" d
说明sql注入和xss已经被过滤了
8 r" M3 r1 y: \+ u! p
& @- Z) G) y* Z8 i( y& B3 t七、安装过程中排错" u- {2 M( g' `- G2 q
2 K. u$ v1 o- l8 M& T1.缺少APXS会报错. {5 G, U3 p$ B) \5 x: O
/ S' U; O! P- J1 M) s% H# yconfigure: looking for Apache module support via DSO through APXS
6 V" w' g2 x1 oconfigure: error: couldn't find APXS# F2 U' N9 P1 U; T4 Q E6 x0 Z. `& n
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' C: g6 L( I# L4 H% B/ t8 H8 g解决方法:
$ |3 Q" t* H5 b5 m! a/ B
- O `) }) ~& M. B( \) Qyum install httpd-devel) h- N; u9 u4 h2 u. K" V% s
2.没有pcre
' r- H, |9 q" |, ]" Q W) I& p, K
8 g% N% D0 z V3 v" O. ~! \7 wconfigure: *** pcre library not found.% A8 F/ h% F8 D0 w4 M, t
configure: error: pcre library is required c z: E: J7 S" W3 O0 F
解决方法:
1 I4 |; P! A5 w% b) R6 e% [/ Y9 C' S% h' s* j# ^& a: v2 E& \/ g
yum install pcre pcre-devel
7 F9 I( u1 ?: }/ t& u- I3 ]3.没有libxml2/ N2 e9 Q* |; D- |
- H. H1 G( t$ n( v6 w
2 |3 \$ e3 I b5 d' ]2 xconfigure: *** xml library not found.# u" F( F- n/ f9 R( G
configure: error: libxml2 is required
5 b8 }+ u, F6 [1 K解决方法:+ W0 n7 v: A/ B. y! b* b8 m
- ^% \8 a+ ?. ]; T, nyum install libxml2 libxml2-devel/ w: M: v, Z9 a% Q" \$ B# Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" ]; ?: v2 |8 [8 ~: {* C, ~" L- T' O% y' j! c& e
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- T7 X4 T$ q. D$ znginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 _% \% ?3 @' Z; s J) L
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* C! W2 ~! Y/ C/ h! G$ A- a; `, o& F& l7 Z7 U8 l6 D3 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% \0 A, ?$ H2 Y$ P" U% d6 \- l, i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# v. H6 J/ ^2 U1 p6 B# k1 p+ O9 H% F
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
o) S8 z0 {3 j n+ L* p# l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. q; l" A0 c2 h2 z% x* I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 W2 l# ~. q; D8 W( a2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 D" O+ F& G5 r: y
解决方法,移除低版本的APR (1.3.9)- U# `; Y. [0 E, [ s: G( u
4 D; [' j' ?' _, M6 z2 Y
yum remove apr0 _; P, H7 q0 ?4 z0 p
5.Error.log中有: Audit log: Failed to lock global mutex
: K! }& Z; L, U! Y& J# w) S8 w- P0 v: Q0 s m( \0 e* g$ ~
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( C0 m5 L$ L0 X& c% S
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. H7 j* C" G: I3 A9 i9 m p+ l, Q, e
解决方法:3 g; y% u3 ]2 R5 o. Q4 c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# M, l8 `/ Y2 d. i8 g+ q" b
! a m2 J4 w- y u1 U
SecAuditLogDirMode 07776 ?, r" |# f1 S% F2 L+ h
SecAuditLogFileMode 0550/ k8 _0 P+ O8 h
SecAuditLogStorageDir /var/log/modsecurity
4 r3 m- k. f% t! c" K! wSecAuditLogType Concurrent
' R3 m+ @, g( P$ z7 m7 @参考文章:
9 W( {' p% g, a. H$ X p2 m. v: r2 ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 K6 t( e7 z$ N# _9 l, w! j" X2 p
http://drops.wooyun.org/tips/2614 |
|