|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
$ @5 F8 [7 {7 K6 H4 D
+ n; [& v/ J% u6 t一.准备工作
: p4 Z6 c; S+ f/ H7 x
( C: d* Q+ m/ ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; N% @: n& z' B& v4 @$ g
0 o4 f- j+ X0 p& P/ Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! d% U& U+ D$ X0 a) A
4 Y# A+ ^; m9 Y& mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; u- @9 b; P* E4 I' X5 I4 m6 n2 z# B8 P, q0 ^5 x& R5 C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ Y0 f; U! Z9 w5 u" x: |
3 o$ l$ D$ ~" d# T依赖关系:: z) w ?. Y% J |" j4 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" r; w3 d& h% |; r* B7 g8 @4 h k2 s/ V! x% j) Q- O, i; Q# N
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ D P! S: L. X+ \" Vmodsecurty依赖的包:pcre httpd-devel libxml2 apr: J: @& Y6 R$ h
7 |* ?9 k8 Y% b4 h8 W$ x; k. Gyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
2 S3 c- c7 I0 ?' i二.启用standalone模块并编译
! k( {) A) O- J, V! J# P; [; G' B3 N- {4 j- _, z
下载modsecurity for nginx 解压,进入解压后目录执行:4 h0 ~# F/ g$ H. W6 G. g! g
0 q2 K2 O* W6 {% m./autogen.sh5 n6 A: h3 Y2 J7 @2 d
./configure --enable-standalone-module --disable-mlogc
0 g/ \3 z& ^& p1 O# ?& smake 4 \# e* d+ B- F9 s0 o/ s1 k
三.nginx添加modsecurity模块$ X6 z) ]8 C9 G$ F
. @* r7 p* H; L% @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 ?5 ]8 N. W9 p8 N7 B
0 z3 ^! {* s+ O$ I7 z./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% R5 l3 G+ f8 v; c2 L* R9 Y
make && make install$ p7 ^/ Z1 X# I7 Z
四.添加规则
+ z# m& E- f* } l$ T+ X: ~1 c! o" L% X) Y S% a
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. z2 I% u+ j. P) w/ F
$ Q% R0 s4 c- L8 U
1.下载OWASP规则:
, x; V( V+ A% J5 q
; M, E# P# Y% Y6 o' }/ Tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: a0 |( A. b' Q( Z0 t
. z, A! @; D# ?! S {+ e7 M
mv owasp-modsecurity-crs /opt/tengine/conf/
( i+ p- z& I2 I/ M& {
" z- p' F5 X" k4 kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: j& r7 B4 Y4 w* N# a1 c4 j2.启用OWASP规则:
* U# n# M* m; y6 m! G: l8 n/ ?$ \$ K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. n/ B# }: j9 z0 K0 Z9 r4 C
# f8 w( A2 B$ e% [, M% ]$ v8 k4 u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 a' I* s4 y# D0 n
* \/ |+ e: D1 t: T' sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 k, C6 A* K4 g
) L. z2 L& L1 x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 I/ X& h9 p5 s0 S2 |; rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 N7 R- ^+ p5 U( k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 v+ \- I5 g- w4 c4 RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% |) D: M) |. |. j' _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 @4 G k7 l. i# c" L" xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 c# v2 }% M6 }- D& G5 b0 O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 x: {3 A8 d3 H( {4 }& n! T, f1 y五.配置nginx$ n: {( b! b3 Q$ j p
" ?% I, r+ Q. z) L. L在需要启用modsecurity的主机的location下面加入下面两行即可:
8 y/ C1 y- Z8 ]6 K! U
3 L& S5 b- W# v kModSecurityEnabled on; & j: w1 S! R# K/ K0 O3 \6 A0 c
ModSecurityConfig modsecurity.conf;# g5 w2 H0 e5 H+ d/ o. v) p2 M
下面是两个示例配置,php虚拟主机:' x' o+ @2 A# |* A
7 F+ Q$ `; m3 Y* d/ ~7 |" B; n4 I
server {0 G/ `: r6 a, c; k* Z" h
listen 80;3 i! V2 d2 _) a% T5 J6 K* w9 I$ g
server_name 52os.net www.52os.net;. w" U3 b: i' W% |2 {# Q- y L
+ P( Z2 W+ a' N/ B location ~ \.php$ {& e7 \) n! ^, Y, c* o( s) e
ModSecurityEnabled on;
$ p% ^" k1 i% W. P3 T0 A ModSecurityConfig modsecurity.conf;
; X) Z( ~4 g% e+ p( V7 L4 I0 R1 B* a/ v8 I
root /web/wordpress;" Z' H! k& k) {; V( }
index index.php index.html index.htm;
; C2 t, Q$ B* e2 Y0 c
; ]* Q/ P( c5 u) B) d, W3 r fastcgi_pass 127.0.0.1:9000;
z6 y6 L1 t0 }. e fastcgi_index index.php;+ d* K! L' }0 ~/ J. b
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) B7 r8 A: N1 N# C m. a# U include fastcgi_params;) |# Y4 n5 b$ s3 c/ v7 E# R+ H# @
}& }& I- l- E6 c) T* J
}# u. U0 G0 S0 Z3 l: e3 Q/ b! Y
upstream负载均衡:
2 d$ Y$ `$ _" a- B9 u& {5 r. U& e# W0 e
upstream 52os.net {
- O) N0 v+ f5 t7 h. i" Q server 192.168.1.100:8080;. g/ ~ @+ W& L% a, }
server 192.168.1.101:8080 backup;
4 N$ E: r' W3 `7 c2 v4 Z}9 F, r: e* _; i1 o" q* N( q2 X" h
, K5 `7 m- G) f8 V6 r/ |server {
! v( y7 |1 b8 {6 Ylisten 80;
q! z. L# h+ a7 q/ }( H5 wserver_name 52os.net www.52os.net;8 P! j- s9 X! t$ G/ m& a
) T# ^7 n3 r4 Blocation / {7 E' N1 a% T4 T) S) T
ModSecurityEnabled on;
$ y! o! j* E1 c8 r: ?8 u' M ModSecurityConfig modsecurity.conf; / F4 D& ?& u7 D1 k4 a6 @' V. s
# d, g1 j! V" d: M3 {
proxy_pass http://online;5 D* ]( c& y) t( N; A9 P6 |5 G
proxy_redirect off;6 {( A& r1 V/ F& K9 I6 X( f
proxy_set_header Host $host;3 L' U; w O: B' ~1 o8 `
proxy_set_header X-Real-IP $remote_addr;
( V" ~' k/ {( D$ k Y; S- ^5 t proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; S; B: B0 o6 t
}, |/ l/ ?8 U/ e- q9 v% e
}
1 O- j6 E" T7 x3 G9 l六.测试/ K" U& ?' n6 @( c C
$ m8 N, P& }* _, o& v3 q8 G
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 m9 y3 ^- q6 H( I
' `: R/ g( C& l3 U: U" Z6 B<?php
4 }( H8 i5 M& ?8 d+ h phpinfo();
5 y5 U* z: F" N3 \- c$ I?>% e8 W& b, k0 i
在浏览器中访问:
. V/ ~5 G* h( b9 ~
) p) o2 m, U3 t: H1 Z2 e& A! ?8 _- Ohttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ d! x) t' K* H# W- s0 phttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。7 p/ u/ F7 R+ [1 G
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- ^& ?; Y, |8 I5 A2 {
说明sql注入和xss已经被过滤了
# h5 l" y6 c+ s) _5 Z4 ~/ x G- R! T- y) ~
七、安装过程中排错
: ^4 Z0 [0 E, f- `/ J4 p6 J7 z3 o/ w' m+ G/ W p& }1 q- d+ m' B
1.缺少APXS会报错
- t: Y/ I4 l9 }! ]4 j1 _: _
b0 E6 H; x' o2 g- ]6 rconfigure: looking for Apache module support via DSO through APXS
3 L/ ]1 Q- C) y' Y! A! }configure: error: couldn't find APXS* j2 r$ |! Y+ B1 C5 c2 O+ \
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, E$ P1 B5 c& I+ j! ~
解决方法:" d6 X/ i, x( t4 a0 p5 g) \$ @
5 t, G" P3 Z: T1 Vyum install httpd-devel
! _. u' `( w3 I; n K2.没有pcre
; Y% \1 Y7 p2 d9 h0 _% A4 b: t" u5 w" [1 @( ~% D
configure: *** pcre library not found.- c; T* f% M7 \2 W7 R' ^( R
configure: error: pcre library is required
0 i$ f) E/ A8 T I解决方法:
G I4 z" O! Q1 u% A4 X" [& n5 a0 {5 `' E+ c2 j4 u7 W( Y% Y
yum install pcre pcre-devel
- r% K! R/ o: v, @( G' _5 i8 f0 a6 \3.没有libxml20 U% Q+ t! r& P* J# D9 C' R% @9 Z
) u& X3 _- l8 g( b" P
5 h2 c' p& f6 H% Nconfigure: *** xml library not found.
0 W9 R* r" `- s- Mconfigure: error: libxml2 is required4 e7 s% w* P4 Q- M B
解决方法:
# c% I1 m M1 k4 r; M! y+ \; ~
i; d0 Z% ?' C- g) Q( p+ `yum install libxml2 libxml2-devel
- s, L7 Y/ w( V; P4 J4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ U7 C& m# d4 B3 A$ X+ \
3 Y3 z" L: l& ? ?; q9 a, @! |Tengine version: Tengine/2.1.0 (nginx/1.6.2)' T7 Y8 B+ z6 h q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ q) ]2 j/ W. W5 q' z! A! o原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 Y0 `8 Z' P! ~8 U% b# m: @/ x2 P8 g; m4 i6 o% \0 V1 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! ^6 D7 n9 A) _7 F- ]8 V6 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
% N7 w2 D7 y8 n# _; u$ ^2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: q( R. B& c1 D1 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 v2 ^8 q K/ D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ Y, B2 I" i$ _; j2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 z& Z- A* c0 E! x8 _7 U解决方法,移除低版本的APR (1.3.9)
2 q; _! O4 D- S- W" K
' b% e& {0 s( xyum remove apr i+ F" t# o+ ~
5.Error.log中有: Audit log: Failed to lock global mutex# G# D1 G, q' k5 F0 i4 Q+ _/ ?
# q+ {3 T7 C9 F$ v5 @' M0 N3 T2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock D% r3 l8 T9 h, B& x
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"] g6 ?" J5 v1 W: L) [2 L: W
解决方法:
( L. o: ?# I C2 l编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; P; v/ W2 D3 b$ A$ Q
5 i5 W; P+ Z; q2 d6 m% n! ?
SecAuditLogDirMode 0777
4 x1 Z' u6 _( A" b/ l3 QSecAuditLogFileMode 0550; m. `2 G+ M. Z4 d, O& D# [
SecAuditLogStorageDir /var/log/modsecurity3 D9 f: o3 g. S. C% I
SecAuditLogType Concurrent
9 a" o+ n7 ^1 K( A9 o' y: G参考文章: b5 G% u9 ^0 L; b7 C% { X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 ~' b+ k i5 j$ L# T9 |* D. {5 |http://drops.wooyun.org/tips/2614 |
|