|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 A5 S3 t/ U) G' l1 p
2 S1 L0 m9 b& m
一.准备工作/ z: i; {; c1 C6 z0 S: Z- k2 _0 |$ n
- [$ C' A1 ^5 L. b" N; v7 ]3 z8 W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 t& b- T2 @7 {7 I0 i! \8 l) ^5 r% K/ i7 z; H1 j' N, I
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& }4 H8 X1 j" x' V+ j9 p+ |+ i* Z- H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: N2 q8 V- \/ x! X* H
`9 P' T! t# x* [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 H" o! R4 F5 @% B# D* x6 Z
4 n5 o# | [9 V2 n依赖关系:
5 m2 M) [- A9 ^7 V: ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( H& y) [9 I* [% J- C3 W7 ]8 W& z" [7 D3 \" N. D: r$ \
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 x$ q8 G+ K Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
r& V1 l5 |' n4 J
; _( Y: m$ F r% `- L' `4 Vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ z) n4 J( p% O# Q二.启用standalone模块并编译
5 A" s: N, m7 A6 Y+ L" l8 h0 k2 [ a5 ]' K9 n; X
下载modsecurity for nginx 解压,进入解压后目录执行:
, d) f i% l1 \& \: B% V
0 b0 {. Q! t" \% }. T. X1 c$ D./autogen.sh
5 S4 k! g6 K, S8 y. _1 N3 m+ q./configure --enable-standalone-module --disable-mlogc
- r9 O- `0 t, r% T9 Z* |- i8 l! Zmake
`% O5 @- `, c' {+ W( @' P! w2 V三.nginx添加modsecurity模块
+ p& ^ H" d' I7 _& L/ B* _! ~# z; N1 f7 Y
1 `$ T; K* ]! v g! ]在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
5 s) E* M1 f- K1 F! N8 J' A; Q0 C% B- w# k3 x! s& ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine8 T( l }; ]# ]3 y. ?+ H0 a
make && make install/ k ~' \* T9 T* E s8 `
四.添加规则
% }4 i3 b, H! t! r* g- \9 V( X
( m5 I2 r% I# g* i& ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 X0 p% ?7 x3 [3 C7 O! v3 W, d \+ c% C! H3 z) B/ a
1.下载OWASP规则:
) ^5 a& {. T& O, j& P2 t, i3 R" o: C& \% T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
+ `' c( Q. \6 H4 ^
. k2 C0 T* b2 G7 A! Fmv owasp-modsecurity-crs /opt/tengine/conf/# r4 g1 ]4 j% Q! d/ w: }
6 P2 o1 A/ ?' [; F1 Fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 ^' F( G/ _6 W7 Z2.启用OWASP规则:9 n0 K8 w7 G3 N1 ]
; z# s8 [. I" H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 r: ~( I# g, W7 q! N, N( t; c) N( X% R+ O* q# Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: f2 @9 \ K4 K& T Q$ g/ S1 |6 \
" B8 s) U. p4 I" c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" \, Y$ ]$ \% n. M) y8 N' x
+ u) \& H! v9 z( ]Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& S6 ~* J' a, Q8 q6 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 y0 }2 C+ L2 U' C. v. C }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! z8 m/ J- n/ y. s' H
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 } Y4 a7 d. rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
: |. o; y; @, u }+ D8 NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) v6 D. {; H/ Z6 W% P
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: C8 j* E: G8 i) y7 h2 V8 _# ?
五.配置nginx
( y0 K! X2 V: g# p% x3 H) G" x% k0 v3 [. [% ]9 B2 @0 o6 w+ Z' N
在需要启用modsecurity的主机的location下面加入下面两行即可:- k7 Y/ ^7 j& d' Y
5 j, G! N5 \% B$ ?7 E) ^
ModSecurityEnabled on;
# X2 _+ {+ `: NModSecurityConfig modsecurity.conf;, o+ S* K6 w& \ K4 ]7 ^2 @
下面是两个示例配置,php虚拟主机:. j' M* J4 x2 j! U. t' v7 r6 Q7 A
* |% c: A% ~( r% h$ M* jserver {7 u: T, T1 M7 Z: T
listen 80;1 `( T$ \, s; [ ]
server_name 52os.net www.52os.net;
0 |5 ` e0 O2 b w 9 j3 i3 T6 B2 W3 w3 { Q
location ~ \.php$ {
9 q$ _* m/ C$ f# | ModSecurityEnabled on; 8 \2 ?) C- p4 b) z! n; q7 u% B" V
ModSecurityConfig modsecurity.conf;
+ |# y$ L4 m+ a8 Q" s* O
( I7 V2 b: X5 @ }1 }, q$ r root /web/wordpress;& c: N. S& J! O2 M& ~
index index.php index.html index.htm;
. z% G. _4 f: J4 X4 \. ^
( c) S6 {2 ? T A' f fastcgi_pass 127.0.0.1:9000;; M, ?1 S5 o2 Z5 M! V
fastcgi_index index.php;
% N h9 _4 R9 s; K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;! r; h. X' f: Z; B; E/ ?% C
include fastcgi_params;
, Z, a% G" ~( f( T" u. H6 ^ }
9 H O: o3 _* }9 N: v3 g' g6 z/ E }
1 d7 Y* j1 s) }4 @upstream负载均衡:
; ?0 E/ i4 V: `2 D/ p# P0 _4 z) c/ M L
upstream 52os.net {
s8 [0 U8 l2 t5 r server 192.168.1.100:8080;
2 o% r+ J$ I8 x* j( `5 D server 192.168.1.101:8080 backup;' F1 n/ c: z& w3 d
}
% d) H" v1 H. i7 f6 R/ o# l2 o% |3 B" }& Q
server {! y. r# q: l+ {& h2 K+ q7 A* v. V
listen 80;8 C) ?+ k) [+ ~( o: e |% p
server_name 52os.net www.52os.net;
' S; |4 q/ P: z* |) ^8 p7 s- d% B M
9 [; v0 |- T' ]1 M6 r# M$ R, slocation / {1 |& `9 I# X, P: l
ModSecurityEnabled on;
! {/ [: S4 x/ W+ p- X ModSecurityConfig modsecurity.conf; 3 m' v" z1 W: @
' H3 `) U& o% ~' O7 l
proxy_pass http://online;* @8 }) r& a: ]) U7 [. X2 d- E8 v
proxy_redirect off;% {' k- S- J- `- ?) \
proxy_set_header Host $host;
4 C) Y8 _& x1 u proxy_set_header X-Real-IP $remote_addr;
, B6 k+ K7 y$ i" p4 v' ~# c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ ~+ v6 b6 k3 X; w9 e5 P$ [ l }& o# [2 _# s8 L1 y' K% M: [
}, C/ @8 f& {6 k9 g( Z- s
六.测试4 e: V( O5 ~' s8 x# J) }; P
( \/ L* Z# W% c ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:* R0 w# h, D X+ e$ D4 k
" V0 b! A' K$ F* b5 T5 z0 Z<?php
2 t3 F9 t2 A; N9 W9 S _ t0 e phpinfo(); 3 z# b! q5 j, R0 w
?>( d8 B5 r8 k, U' D
在浏览器中访问:8 q! s9 P- B5 ]* l8 Q
4 n G+ h% o5 N1 l, a9 G$ l6 shttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ u- v! F9 @1 q9 H( ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
6 a3 l$ |* J( |/ P: ?) w" }5 _/ }http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 Y/ R! k' ^. @说明sql注入和xss已经被过滤了
( A* i# N s! r9 z, V
3 F, F! r) c B七、安装过程中排错+ z) U+ o7 g Z: I/ ]7 {2 O7 ~; r2 M7 D
2 z8 w; U/ J5 o6 F' W# G9 a# ~
1.缺少APXS会报错3 ?2 |1 w1 w4 Q$ o' n1 s
1 {; G# @. D7 x2 A3 vconfigure: looking for Apache module support via DSO through APXS# T1 p* w0 M8 X) _; V$ C' V- @
configure: error: couldn't find APXS
5 j8 G, o3 V% ^4 _% A8 S" Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( _" U$ q% h! F: m! A: t解决方法:9 ~* ^9 O7 q$ t6 @4 Y0 L
6 Y3 r7 w% O+ i6 B
yum install httpd-devel, u, {% f7 W# {1 l( x
2.没有pcre
. ]3 r% O+ [% x) q/ q! Y9 @3 \+ W
; v7 f% u, N' }9 B& [configure: *** pcre library not found.
2 r8 s) L9 `1 c- K$ x+ Mconfigure: error: pcre library is required
; b/ q* r v3 }$ P解决方法:7 D, a. L7 S: F# o
% i" { _0 c2 O( D" L4 Q7 Myum install pcre pcre-devel8 m8 p) |. p% m
3.没有libxml2
2 {3 c5 z O. X) z3 W
2 u7 v: V2 i, y! w/ }) U4 H+ @6 K8 L M5 ~% @6 I8 c
configure: *** xml library not found.+ h+ C# M' s# T( X5 t
configure: error: libxml2 is required* r$ k/ k8 X3 m) [7 t C% ^* I
解决方法:/ A3 E8 X% x; N9 }
% j4 z, X- @8 Z" o# O
yum install libxml2 libxml2-devel
0 t7 `. I" w/ @1 o4.执行 /opt/tengine/sbin/nginx -m 时有警告, `* r6 {' [6 c' Y8 P
) K$ L% K9 ]/ E8 E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( h0 q; Y5 P7 W$ j' [2 Q6 e2 [+ X
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) C1 k) C' q. h4 E- w6 {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- ^8 v( H6 \) f$ _; N7 s! |
1 e3 T+ s& Q! t% |% @( M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" @2 j8 q0 |; Q8 `4 O& w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9") s9 ~) D& ]1 e5 p8 R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 V0 a( v- g" }9 L& Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") o) U, J" `2 W+ n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 }4 P* X8 C# `0 E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 ^/ g' _( i$ _5 t7 L
解决方法,移除低版本的APR (1.3.9)- D. C, j+ X( a
8 b: e3 a" p3 i" q* {0 Z3 H' v( Jyum remove apr; D; ?9 [; x8 U; r a+ _! P) J
5.Error.log中有: Audit log: Failed to lock global mutex
9 l' G* @2 ~! J% k1 _
$ l( c3 }( A9 ^. L7 @. ~" F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
& h" I5 }- O, R& A1 eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ Q) E- d) Y" E P解决方法:7 G7 e' d* ^' T- u. E) [8 V% f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" F6 }4 Z L) E2 m. B& g
+ Z9 A# v3 k9 H( [; iSecAuditLogDirMode 07777 \) m: u, h9 z `
SecAuditLogFileMode 0550
, n2 K I. Q* }8 ^) o; A# c4 T7 m: RSecAuditLogStorageDir /var/log/modsecurity+ a0 j' C5 v3 l, z3 i1 W
SecAuditLogType Concurrent
3 U1 X; a8 z9 S; o0 X! I7 E参考文章:; I! I/ ]. W# x( `' t) {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 B) b* P' d% I
http://drops.wooyun.org/tips/2614 |
|