|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( U1 M' O# T$ C) {
- a+ }0 V3 I9 w' a3 \一.准备工作
: Q7 }7 z' e7 n$ O2 ~' Q
- B+ x, x0 |" y% o/ P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 }8 D C, o" P+ X8 q3 a
O4 {* n) J* M9 ]. y3 Ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: X1 I, E' e- I7 ?
8 Y0 T3 i9 b3 Smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; a2 m$ x p6 S
9 g5 y( x7 e. X& D! p5 [OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( x, H' I# x: @4 `# K. k# t
+ S& B$ `7 L7 ]- [% b J$ R依赖关系:
9 o0 R, y+ s$ mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; c4 f; y+ |: A* z6 W% r0 z- p/ q( t, {* a2 G3 U! p! n4 ?# s
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
" D) m$ [" { o+ vmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; R# \' ?: n# a
5 t" m9 L3 U# T. Z! Zyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! i$ P2 A, C6 k7 M二.启用standalone模块并编译
7 Q( t+ c+ x! d0 ^+ ]( ^! P1 Z6 ^" m. ~7 s* [: D% F7 g9 P5 ^0 d
下载modsecurity for nginx 解压,进入解压后目录执行:
. \9 K4 ]5 i/ s9 p' _) f- {$ U- k9 Z* R: ~
./autogen.sh: r9 G% h. P% K8 `7 v
./configure --enable-standalone-module --disable-mlogc
3 R' i+ k/ Z% B- Z/ J" A0 N hmake - U7 h, L9 S" S
三.nginx添加modsecurity模块
& M, Y+ @, |1 A$ n" E& \" d& i! Z9 L* d! i W/ \3 S
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 [9 G2 J# `0 j$ ~& F% \2 F
' f) N$ d. b W: H# E./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% i; q/ G& i! ?% hmake && make install4 l1 }- k: n8 t2 A
四.添加规则
; ]8 \4 C" Z$ m2 v8 Z0 l4 n- U. X7 n2 h( t; A/ c( B- g* Z7 I1 z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ J4 Z4 L; y: r% K/ ]4 X5 x
9 V1 t1 s; E8 z7 |7 O
1.下载OWASP规则:
0 ~6 g8 l0 G; u9 z7 N
( C. s' P% r+ Z4 O: Y" \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs- J0 z% {! {( y
# {: [8 }8 p8 k/ q. X6 `mv owasp-modsecurity-crs /opt/tengine/conf/6 k5 @* p+ a9 Q; V0 [" R
* m$ f+ `/ D3 A) n- O; }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 O) y4 v$ ]8 x7 g8 y: H# Q
2.启用OWASP规则:0 O; `4 v7 y% R: U; F
& h' h4 } `% c% K: |复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: V" B U& g0 ~
/ h8 V! H6 O4 G& e# `1 U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- n, `' ?4 B/ t! m; {0 \- o5 d! X) a5 m+ S% d$ `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# o' y3 e+ @- q7 |9 e0 y+ L
7 r- @% H7 R- X3 B$ C) l( f2 J/ B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 }$ c7 P* ?, h h8 w) f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) {: x9 ]) f& T3 S* g' U* Q4 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 B( S( x) Z7 w# yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% t' b+ A1 a; L/ p/ L; R" mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& d' p1 W5 N0 y2 m% |1 Y, p5 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& S3 f" [' W0 ?4 v
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 H, _' u% k o
五.配置nginx; F3 m2 j& e/ V' e4 x* b: b P
$ V" l3 l* a2 _6 z, F1 a" k
在需要启用modsecurity的主机的location下面加入下面两行即可:
1 {. u& w' d2 T, C; N2 A% y" q1 b* B
ModSecurityEnabled on;
$ L( |: u% @; o* P# b2 gModSecurityConfig modsecurity.conf;, v! M( a+ }; d8 }$ P/ Q
下面是两个示例配置,php虚拟主机:
" F t E2 } J, o L
9 T. P6 k% {) gserver {; f2 }, z( L* _9 c" g" ^
listen 80;
2 a( t( E5 x4 Q' m server_name 52os.net www.52os.net;
9 h+ R" m2 F, c 1 g( z0 H& f0 Y4 j6 ^- a
location ~ \.php$ {
$ B: {9 `& R+ D s: b% w2 S+ ` N" ~ ModSecurityEnabled on;
; o5 |4 x8 y. a" {; l( }* k ModSecurityConfig modsecurity.conf;
( r0 a( v0 ^: H7 ]6 F' |0 D
3 z2 A; |) m6 ~0 b1 H+ w root /web/wordpress;
3 Z% x. X8 Q2 \1 @' C- \ index index.php index.html index.htm;. g4 i" W Y V1 y7 H
0 Q1 W4 S( x) z8 v fastcgi_pass 127.0.0.1:9000;
5 x2 g* \* b% U D9 ] k) v. u fastcgi_index index.php;
7 x! w: S. N$ t* k7 i( ? fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;* }& O8 e/ F+ }( Z$ ]
include fastcgi_params;
6 d0 K; V% c6 \ }
5 E3 |8 ^4 O( T/ g, P7 L }
; P# C1 p4 a" n: r8 z# M# Dupstream负载均衡:, b# J, W9 O0 \8 W
" ?. G7 k- [. X% n3 c. Z
upstream 52os.net {
- [" Q5 {: Q2 L+ S) c: \- m. U server 192.168.1.100:8080;7 a5 G- k3 F8 M: a a# s, A& g
server 192.168.1.101:8080 backup;
. i6 E$ t' s$ L) Q* F/ ^}
. z$ v" f* V( y+ s8 z4 r4 }8 C" [! h6 b! L1 U J
server {
g2 J( _, k7 {8 A0 @0 q4 c qlisten 80;. o( @; R4 Q! T9 G3 L5 O
server_name 52os.net www.52os.net;
! x8 O" e* \4 E; Z) }0 s( q* y# G: N: o+ u
location / {
& p5 o8 P6 e! u% w1 D: F8 Y ModSecurityEnabled on; : g7 S3 y) }2 r& p0 s% ?
ModSecurityConfig modsecurity.conf; * d* z& g: b) N5 V
( _. X: _7 Y5 _; p" V proxy_pass http://online;; Y; K( ?( G E7 E
proxy_redirect off;
: p8 S3 f, P) n9 B. D K& A, W proxy_set_header Host $host;# b C3 ^9 l* s2 P
proxy_set_header X-Real-IP $remote_addr;
9 b% l5 u8 Z4 n! B proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# V1 T# K9 F, u0 [8 U }
K% e+ e/ Z8 v}
: Z% A6 c( y ?" h7 E/ q/ E( U六.测试
[, Y Q, L( k; U) k, j9 ?; G. A0 v8 @$ v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( I, T) l( u! h6 L" @; L! ^; ~ Z @& S( c; R9 k& j
<?php0 J ~7 I8 ?: h6 l
phpinfo();
$ `0 B: F$ M; n, c/ c( O- Z?>3 n& B' `. D0 ?6 | ^/ _
在浏览器中访问:9 w, j F6 O: y
8 F! t1 D" W2 l X8 |
http://www.52os.net/phpinfo.php?id=1 正常显示。
2 j/ p! S# |# mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。, }$ Y. ^3 f3 t1 z* v8 c3 v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。4 `! y0 l) J+ j( x6 q! E/ x$ k% Q
说明sql注入和xss已经被过滤了- |: C8 D* d5 @9 x
% r: C! v% T/ [七、安装过程中排错/ L' q% G) S/ q7 {
. j4 g4 V1 V% U+ u. T- I5 o0 P1.缺少APXS会报错$ ]$ [# w9 F1 s2 T0 o$ _" A
, \7 o6 b& x$ Z" h: _' ?configure: looking for Apache module support via DSO through APXS3 B: Z& s& I' O+ e
configure: error: couldn't find APXS
; f$ t) A( R* j, Y: Napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ R; M; q- y; R- |3 J
解决方法:
- ], p1 k; Z0 J. P0 l
6 T6 m" q. p) Zyum install httpd-devel
8 n# v7 e# R0 g: B- _* B8 G r2.没有pcre
, B" ^# B0 e3 ~6 E" G& K& o) v' S3 \& {) T! E1 w8 S2 h
configure: *** pcre library not found.- e H, f0 J* K% T4 j" w
configure: error: pcre library is required
6 X6 y& L- p; v/ ~" c解决方法:
4 @* l/ f2 E" C) C g
& n( `& p; ^& K$ C/ F' O0 Q& Hyum install pcre pcre-devel7 T" w! S+ i8 Q" k$ H
3.没有libxml2
( u m$ z. }( D, N8 T
! B4 c# h/ S! y4 B: p, ^5 {, N; E* m( \, [+ M& P
configure: *** xml library not found.
, N+ K6 v/ C8 ]configure: error: libxml2 is required
& u3 Q4 D# j2 J9 R解决方法:
2 L% _0 k Z p4 ]0 }& N" s0 J2 O4 Q& F; H( K' C1 S& D
yum install libxml2 libxml2-devel
$ v) I6 v7 G I3 Q! e* G4.执行 /opt/tengine/sbin/nginx -m 时有警告! H2 G* |2 J1 U, k, v; W' |
7 t4 N9 a( `+ a, NTengine version: Tengine/2.1.0 (nginx/1.6.2)% l1 [4 T( Y) d, G
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! r. A. i; j: s/ E* T8 `( Y& X" c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. \& O6 X7 j! N1 J/ ]* ~* S
* T, H2 t+ X. U8 q+ S- u: v, L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 F' W( a* S. }% W5 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 y2 Z! w7 ~2 a' e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. f9 j6 O+ |4 w; M7 m2 I" Y$ ?" m: c( ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ h& l$ q7 V' V# A! o2 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; E, \8 e: R1 N1 ]/ M/ B; {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 X C8 W% Q( @# F; @解决方法,移除低版本的APR (1.3.9)
. t* ^' {2 t4 {0 \* b
! v- w6 ^# {$ z0 k. iyum remove apr5 }7 ]: i: u) E' K) _1 p$ F
5.Error.log中有: Audit log: Failed to lock global mutex+ C5 [3 ?2 x' z9 q; ~1 Y" L* Q
8 ]& G w: T, ]' ?: |$ B2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 6 B# ?# b6 l, `8 o
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 d, p& L6 J* N1 r7 O$ X解决方法:; x& y" o4 ]% T$ b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* P* O: J3 z9 `- Q7 F+ p) e: }4 f, _4 V( d" l( Z! a2 r. f& p m
SecAuditLogDirMode 0777
7 |4 J" U- b% ~/ C& `SecAuditLogFileMode 0550
6 P" o$ | t& bSecAuditLogStorageDir /var/log/modsecurity
. [5 g q: u) `$ N i sSecAuditLogType Concurrent
( z2 T" U z. p参考文章:6 N3 J# P. X, Z9 f: ?% {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 O3 Y9 v! P6 zhttp://drops.wooyun.org/tips/2614 |
|