|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: J" i6 A0 I3 w
: k. _2 i2 _0 s5 S9 l7 K0 L8 [一.准备工作
E$ k; G+ w+ X/ I# V
5 P: H+ V3 v8 u! H6 e7 ?4 P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; c& M4 q( j! ~
9 m5 x- d( u6 _
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 ?6 r( X) _2 O% R* X/ a B( z
+ N! f9 t6 _ ?0 Z: z7 A) r k( omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 g% a8 r8 e. P* R6 j# }
& X, l* @8 v# H" v3 eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- O- l2 y8 p' m" O4 ^2 t- \/ G/ I3 R g/ L5 h# z, F! B5 f
依赖关系:
* D; Q3 o V0 b! H6 s7 E! c- ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 L8 |" R$ ?( P6 S% z
: |: m6 d- G+ K) ^9 |. n, T- Hyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" z- S) `5 }7 z4 |
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 R* X. n+ u8 r3 }5 @' f: F$ X/ C$ Q: j8 @9 n/ A! p ^9 R; S
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 U/ U0 g5 z% E u, \二.启用standalone模块并编译8 ?5 |5 _3 D3 O. ^' C4 B
! ^0 W! f1 l% ^; z7 p2 I6 K
下载modsecurity for nginx 解压,进入解压后目录执行:) Q9 E& p9 q1 @" s/ D' \; J: D
' r- I$ W8 ?- C |* `* k) n6 B./autogen.sh$ j/ F% [0 f. h( ]# _
./configure --enable-standalone-module --disable-mlogc. J6 v1 P s& v
make * w5 P3 n/ L6 M0 e: V- b+ t
三.nginx添加modsecurity模块$ _ `5 y3 A& I3 T' C
! q4 t, v0 T i0 w/ ^; a/ [
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:7 H" M, p# h: V( A) |" z
& e2 m! L" t3 N; [, H* R' ^./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, {: {5 V5 b4 J9 D
make && make install1 d$ f; t& n4 Y6 ~! z( _0 W
四.添加规则9 F1 K1 G) b& Z3 Q
* p9 u& ?8 s$ ]; t+ rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 {* d4 ~$ X6 i* s! g9 a' b6 k8 t( ?- o* C7 e2 ^
1.下载OWASP规则:
2 }( X c9 c p! }- ~# \) T4 @9 y8 o' f8 ~8 V* n5 x
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 O" k; i& d$ B8 [/ ]7 A
0 n* s! D) {& F* K6 ]7 d. pmv owasp-modsecurity-crs /opt/tengine/conf/
7 F) J$ d1 E4 A% G8 T( [
# J# ~: X+ ^; y; Kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 Y7 e7 }& }3 k0 \! Y. R: b% i
2.启用OWASP规则:
* S Z4 Y" G8 J9 n2 ~3 V# {* c& ^) I7 a- ]8 Z2 i
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, W" H0 l- a, v0 A y
% l2 g0 P; N( H# |2 ^: u" W编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ A6 U* s7 V9 h' G# w0 l7 J
6 h" y0 O9 ]& @' v8 e" K2 {9 towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 K7 l" \1 o, z- B* a2 I
9 p) R! ?9 v5 f: }1 O, eInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" z2 i0 H6 p$ z r3 J, d0 }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- o* Y: A; A- Y) P) Y) ^# [9 d1 {, C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, {5 ~2 C3 q* `4 w" T6 x& c, g2 {! r1 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ e( z! s% j% m8 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 p# J$ |, [4 Y' a: o( X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 n, Z z5 w% l2 p3 ~
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 i- Z, k# j) O1 `5 z2 b( w
五.配置nginx
3 T% o: R0 f+ l8 J8 F! y0 z' E, v# z% \( F6 S @" Z% i! ~
在需要启用modsecurity的主机的location下面加入下面两行即可:7 h( v9 R/ \% Z3 p4 [
! _ b8 F+ A$ ?- w' p% l( x, D' E6 g y
ModSecurityEnabled on; : N& v% b' j$ i; |9 C3 L6 g
ModSecurityConfig modsecurity.conf;3 m e7 ]* I( j- S8 P
下面是两个示例配置,php虚拟主机:
9 ], e* d5 y: P0 c- u" Q- s2 `* B( _; Y* p. x( I' B* T
server {. H: R3 n6 [" Y, ~" b
listen 80;
k5 }+ |9 ?! Q0 T# h0 K$ B server_name 52os.net www.52os.net;
2 h0 b; A5 S* e+ } 3 m5 W6 _! X. B' ~
location ~ \.php$ {
$ E; \: {, ]3 D" G; N/ D. { ModSecurityEnabled on;
9 z `# A! i7 C# c8 e' A8 z$ J ModSecurityConfig modsecurity.conf;
p& [2 B6 w* ~, x7 ^0 I3 l0 Y
7 k9 G9 o" o6 m9 @7 u3 A2 f. x root /web/wordpress;
" x& ]7 x+ [7 |5 B) | index index.php index.html index.htm;
( Z! ^- U- y2 y+ o $ m8 D5 Z& A; Z+ D+ g
fastcgi_pass 127.0.0.1:9000;
n: Q8 J: j6 o1 H fastcgi_index index.php;
: x, x3 D$ d2 S' G) i& d" O2 m, K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 ~$ I' I. h; b
include fastcgi_params;
9 v$ s0 z- ~) n5 [ }
n) r- n/ `3 ` }. l$ }- l- D7 ~9 a
upstream负载均衡:4 v/ `8 r1 ]6 [, \1 I! f0 f8 w
7 u6 i# G5 H9 N: @
upstream 52os.net {5 [( X3 R6 u1 u x6 g8 w/ ]
server 192.168.1.100:8080;) h; l4 P/ c2 e0 h0 y$ a
server 192.168.1.101:8080 backup;" G' M s8 V; C; g
}
. M/ h/ s0 j! t {* N$ V( h% S6 s$ @0 E
server {7 a7 m( a# U$ D* }* z
listen 80;$ w8 @& w/ u) y# V( a
server_name 52os.net www.52os.net;4 S* |2 U8 B% L1 h
6 ?" Q J; f. M {& \) ilocation / {
/ B5 R$ u. q. V) } ModSecurityEnabled on;
) o2 J" Q2 L6 d8 o; m+ i2 Y$ e8 [( c ModSecurityConfig modsecurity.conf; & ]4 b0 R+ n% n ~/ @
. F/ {5 o g3 t, }: t8 \' T proxy_pass http://online;$ _5 x- g" j. R
proxy_redirect off;& r1 L. e" \! o) U) H. N
proxy_set_header Host $host;
. ?- C3 `1 O6 S proxy_set_header X-Real-IP $remote_addr;
2 h/ h: A5 h2 _+ d i proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 h6 W+ F; q: i" x8 B1 u2 B. S }
8 q+ _' s! P5 p. }7 f9 e" G$ a, @! V}
* u8 O6 I( O4 g8 T3 v六.测试/ s; o+ R5 M- ^' ^1 U+ R" Q
# l( }- B6 ?8 v* E* s" P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( c8 r4 q: q# y. i+ s& v
, A! G" J5 _# @$ J2 P7 F<?php& Q: J' m5 z1 H9 n4 W$ n3 Y) y
phpinfo();
?. J5 I+ W/ t0 X' F, T?>2 B7 U7 X- P, e7 d
在浏览器中访问:3 c8 z8 f/ H! [) e7 d& F
# G) \% \! ^7 O8 \& n3 R! A. ]& }1 ?
http://www.52os.net/phpinfo.php?id=1 正常显示。( i% j6 g7 h# B
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* x% C! {% b/ E4 s- r7 U
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。; M7 E& E/ @# A/ W. n% I
说明sql注入和xss已经被过滤了+ ]+ x3 G$ A2 L9 a1 t" Y7 S
5 h: U& u. Q5 n; c# }+ c
七、安装过程中排错
% g0 P- E+ p8 e0 V8 V/ t. F( v$ L. R$ ]+ D5 C$ X% H
1.缺少APXS会报错
: a- A1 e! P" x9 z! L& b8 T1 ~8 R( f' h5 N8 `) A
configure: looking for Apache module support via DSO through APXS
4 d: m W% ]1 uconfigure: error: couldn't find APXS
5 |9 {* a( P+ x3 c7 z6 Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 G* R4 P/ J: r. a% U& Y解决方法:( y, |& h) t& C! s. b' f, f
% f2 N9 O: ]/ W/ L$ P" y* h
yum install httpd-devel
* G' r4 |) C* o) I2.没有pcre/ U7 y, a) a a9 t. Z
% Y7 x2 @* }' g& j
configure: *** pcre library not found.
1 |! _% o; r3 Qconfigure: error: pcre library is required
# c- W8 [. S1 T1 A* e解决方法:6 e1 R; e: ?0 w5 r1 F$ d
' R- @0 Y' \# A; k
yum install pcre pcre-devel
8 |2 Q0 C; T4 M% \: J+ U3.没有libxml2: C' k2 i! N M7 N6 b0 E( `+ x
* K8 B. F0 I i& R3 D o
# t& l6 u8 s6 Dconfigure: *** xml library not found.5 X* w5 z4 v l+ Y
configure: error: libxml2 is required( |- N2 n5 w0 k
解决方法:
/ I# B# E; v1 S* @1 y1 s8 I( y& A; {2 ~
yum install libxml2 libxml2-devel
" \" c7 o. g4 V8 ?8 a8 \4.执行 /opt/tengine/sbin/nginx -m 时有警告( R) U8 G) t$ ]+ O$ d
) y- x- C2 L) J x# b5 o2 S& vTengine version: Tengine/2.1.0 (nginx/1.6.2)
: A j$ W; N! i6 ]nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 Q- l t4 t3 J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* s# b1 Z } y3 n% d# q! _6 t- ]
/ X, j, E; Y2 f( `$ h9 ~ b5 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# J( z, s' o2 g2 V9 J* m2 o0 [/ H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( |- l- Y+ d! n9 N4 d5 f) ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ V4 Z- o7 j. N" m# `; E' p" f/ F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& a- I% L( `6 B$ Z3 q( l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 M7 w4 W, L" u7 ~, R9 c: q0 @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" [& D! `! ]2 C* F2 J/ g5 r. c3 b解决方法,移除低版本的APR (1.3.9)9 N) ^; V: [; I! H1 n8 n
9 w2 n8 H; T: D0 a! Q
yum remove apr" o- J# A# I: c) v% O
5.Error.log中有: Audit log: Failed to lock global mutex l: B0 q$ I8 l) t
, W) C' C$ v7 A- v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock * v1 j0 Y6 a& u" X* \! r
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 |/ z6 x' w- C3 q( u! g& v
解决方法:. o# L6 _5 J3 j; E0 v4 _
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! M, \9 _$ @+ I8 V9 ?) i/ X! J- |2 R5 ]
SecAuditLogDirMode 07779 P/ J2 d6 x2 k1 ` K; i9 e
SecAuditLogFileMode 05506 K- { b" U0 P6 `. z; T
SecAuditLogStorageDir /var/log/modsecurity
6 X" z+ T( R1 u9 C9 z. t3 rSecAuditLogType Concurrent, F8 _% Y0 |# d C" _7 \
参考文章:
9 p9 V7 z, A$ ^- m" Rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" e: }" A; E" [, L) C) j
http://drops.wooyun.org/tips/2614 |
|