|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! Q3 |: d6 [* I
* Z# T. b. {+ M) `0 P一.准备工作
% R. W! b- y' U) { B9 |: }
) K8 d8 |* q. ~ Z1 `系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" @+ v8 s% Y$ |4 c2 B2 W
* T( T5 R( A& T& ^4 M* i* ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% r+ L! Y& H1 a5 [! N; f
5 d, ~8 e& x5 m6 y/ O4 f* V# {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& x; @+ k& v0 Q
. ~. d# H2 t3 p, A4 T' pOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' z" C4 d) T3 u3 S" C
* Q* c( u0 z H, b/ `. d: M依赖关系:
$ p0 f" s/ r! ?- n6 ^ N, \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 `$ V' m, Y0 e& ]
& E, M9 Y, i/ o# l; a. I* H/ ~yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. J. f+ H1 \# M0 w; U. U' D0 ymodsecurty依赖的包:pcre httpd-devel libxml2 apr \7 p( h+ C# L# e1 _
2 f) h8 S/ C% G, T: Q. r7 V
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' h! R. t' m( E& e. F$ m二.启用standalone模块并编译
|- X+ c; s7 ]
" z: U' U9 g, T/ o下载modsecurity for nginx 解压,进入解压后目录执行:
/ O0 G0 z; X( l
3 n* E" e5 R% Z./autogen.sh
: I8 H0 H, ~ O- _* Y./configure --enable-standalone-module --disable-mlogc
' K( }) M8 A3 u/ T0 l' \make * ?2 ]' _/ B! V0 _: X t. [: A
三.nginx添加modsecurity模块' ]3 E% C2 Q/ g
9 M1 m0 g& S9 M4 N9 X6 X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. I6 Q/ R5 C, f8 c5 H9 w; o. ^, i& p# y. Z: c, N T8 S
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
) A$ P X, h) `/ k6 `make && make install, X: u( r# @1 U. r z6 h7 r
四.添加规则. G! |& U+ j. J+ f6 C( ?' x9 m
1 J. A( g4 e d" t/ X" Amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" l0 X/ I$ K% K( X% _
: r- |- ?: z' y8 I8 X) S, n k1.下载OWASP规则:& }! b; H. E( K/ W6 a. M
5 J% f6 j& V7 {* ?* l
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 {2 T4 B3 [8 m4 |# l' g
. l6 g) Z K: N' Q. @% v2 Y- {9 Hmv owasp-modsecurity-crs /opt/tengine/conf/1 b& |& b/ `# p& a
6 f7 k+ k/ z r4 ~) f& Z! e& Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 D; {3 v! R$ S U( B: R
2.启用OWASP规则:# A8 Y+ D+ m c5 A$ i0 Q
5 ?- \3 J$ [+ c% X% \ d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; S9 h, ?, o( K
, C: L4 L6 {7 p# m0 D* w7 _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 ?& a, y/ ~7 y: J6 b5 ]4 O( ^, `" _. a2 L* [: E3 ?/ m$ v6 M B5 F% }
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. R( L2 ~ f0 }( f w. |
6 |. J. d! p9 B/ f4 x* XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: w8 \% ]) H7 j( V8 E3 _& P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! b* z, ?. E/ a# v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' P% _+ j4 p% g. S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% N; V. _/ F& g1 d$ K8 J2 ]5 J3 C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 T) s5 g7 E- f$ RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- q# [: S% P- i9 {! ?/ yInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ R$ |' K! Q; p# N* |( R9 h6 [五.配置nginx
4 v8 K4 [* T6 j( o2 b; l: ^" g: W; @/ I. ?# |4 V
在需要启用modsecurity的主机的location下面加入下面两行即可:+ @( J W) }, r
4 N: Z! Z" @. s. j! x
ModSecurityEnabled on; 7 f! \9 ?, p$ D @
ModSecurityConfig modsecurity.conf;1 `5 Q3 z+ x% {9 w' ^
下面是两个示例配置,php虚拟主机:+ j3 s& N% L2 c( U, |% {
! z# s2 T1 N+ p+ v/ K$ Z2 d( mserver {! f, e# t3 a! e# B
listen 80;1 C& I, k8 q [: h& K
server_name 52os.net www.52os.net;
+ k' p; j% s/ K# y
6 v3 r7 O5 L6 P8 q; y9 C! v0 M3 V location ~ \.php$ {4 i+ w J* f L! h- Z- B2 W
ModSecurityEnabled on;
& r% P9 x" b7 _6 W ^6 m ModSecurityConfig modsecurity.conf;" [) @7 b1 H( ^; p) S8 f
# V+ s8 z; h t0 C
root /web/wordpress;
+ V t9 }! }% V! c* j8 ^ index index.php index.html index.htm;( r- x! x& b' r0 s; ~; |
1 K+ p0 u; z: j7 G \
fastcgi_pass 127.0.0.1:9000;
! x2 [# H5 j& C( q% _ fastcgi_index index.php;: P3 i7 {+ M& v! s9 v6 @4 u
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( ]# k; w! q6 X
include fastcgi_params;9 [8 C5 G& U3 j8 L; I
}
/ P, Q A( r: T7 a G5 o }
3 b' X6 {% V ~" K$ Iupstream负载均衡:8 }: V% t" G: [+ }
, c: ?3 k- M+ `6 w1 eupstream 52os.net {
) b! N1 s. B8 P& {" E$ S server 192.168.1.100:8080;
' ?9 ?( L: D h: \1 k7 A- N9 a server 192.168.1.101:8080 backup;
& f& v' T5 ~" v: C}
2 \/ z |" n+ M, a) a
! ?2 F; E p l: c5 T Lserver {1 h$ ~' k+ e% K Q
listen 80;5 \7 |- D& V# x5 }8 R
server_name 52os.net www.52os.net;9 v+ {2 h1 V9 J- j9 [
1 L! C. j9 u. U$ p8 R( S" s, Olocation / {
) t( i( Q& f* m ModSecurityEnabled on; 1 R" C+ P! ~ m* i: B/ A
ModSecurityConfig modsecurity.conf; # {& n3 L/ E- S3 V: x
! U3 i5 ]6 M: r. `1 ? proxy_pass http://online;
9 N- ?) `7 x1 a: G7 f proxy_redirect off;
. K) s( F" }1 Z( y proxy_set_header Host $host; b; N; {- V% V( L
proxy_set_header X-Real-IP $remote_addr;
2 Q! i# M0 v0 W u2 o" o proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% O1 w* v& }( q* ?! B }
- G- d1 P! E6 q$ `0 B- R}
1 ~8 ?0 z# h6 R; c! e( L: c六.测试/ M9 h$ i! b+ m
4 t5 i$ g7 U5 |& L! }+ o& \" K3 I9 o( f1 Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% V: `/ b' Y# I5 T1 g% L3 i' T$ R
2 N/ M t5 I: j H<?php& u4 A1 G5 J4 u9 a& Y, W9 g4 [* s
phpinfo(); & Q# O, I( V' A9 b
?>! V1 H! E( n$ D' T
在浏览器中访问:
+ f& f- v& @ I) m2 J# G6 _* {& o' q' L" c* ~
http://www.52os.net/phpinfo.php?id=1 正常显示。
2 O% i N8 {( q2 @" p5 I; L6 Z" @7 L$ L1 whttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。+ U X- y+ _ [ n9 f7 Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。/ S, Y' ~ X1 t, s8 @
说明sql注入和xss已经被过滤了: T0 D4 ^% n( t3 R# E7 T
6 Y. j7 y* K# n, p/ y
七、安装过程中排错
0 \% o1 Y- J- p! v8 H0 Q- g9 N
* U% U3 m$ D5 K7 r) n1.缺少APXS会报错; W) U r% a! f
m+ v- m, u1 ~) {% W& S$ uconfigure: looking for Apache module support via DSO through APXS
- t+ E: c6 Y+ C2 ?( D& w* Vconfigure: error: couldn't find APXS
" O7 E% S* k( D8 @0 D; a* ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 K+ c1 Y. E; B
解决方法:
5 I) i1 T* u3 W e$ a7 E v' L2 S- `, [+ H, t9 \; L2 y
yum install httpd-devel/ V% s$ T# q$ n* h
2.没有pcre
2 F% S1 Q7 {/ i5 o& \+ f& `7 [8 M) y. r% J
configure: *** pcre library not found.3 k5 a, V' s+ |) T2 R) P# O) h
configure: error: pcre library is required1 l7 }' R$ x; b1 V
解决方法:
% ~7 N7 O' R. F @2 Y
; s$ L# ?+ O! e( eyum install pcre pcre-devel
5 f, n& e- ?7 B" K/ z3.没有libxml20 h3 }5 V7 Y) m* Z& \* N! n
4 s7 e3 \3 ~4 M& `' ]' {6 G
% B' R2 i. M$ U- D% C
configure: *** xml library not found.$ G" r& e0 a1 W Q8 f
configure: error: libxml2 is required
; J* ^# U% Y+ w2 Z9 x* a( ]+ R解决方法:7 b5 J4 q7 c- N( p3 P9 g) G [
6 J# @! |6 q/ y1 Tyum install libxml2 libxml2-devel
z( X2 B9 F$ g9 x, c! M4.执行 /opt/tengine/sbin/nginx -m 时有警告3 |' g+ d0 y' G( O. a2 X2 i
" E5 V6 I2 g4 ~+ z- x8 g; b* R) m
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# O5 z% t8 O4 R# w( }' Anginx: [warn] ModSecurity: Loaded APR do not match with compiled!' L$ b( f: W7 c! c% l# k6 d& Q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& P6 ~- P; O% P2 G! w" X& g3 O
7 B. {, T4 d$ o" C5 C, |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: I+ E5 H2 H" w9 @6 D. b# R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"& Q' c2 c c8 q+ E- r, H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- n7 T5 w# V. `8 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ a) q% @( k" s" ?: ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 m! D( D' q1 S2 [# M( S/ w
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( V9 N) C$ j! c: Z/ l
解决方法,移除低版本的APR (1.3.9): X. `8 ^1 W6 H5 e6 Q' |0 A
# `- Y8 |% b4 O, B
yum remove apr
1 l% e( E" {# d5.Error.log中有: Audit log: Failed to lock global mutex
" ^/ o0 a: |) m: R" P
# r) H$ c6 t, L% w o) K% j( _2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ) Y2 u2 ?' \& P7 X+ d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 U! |: |1 d' k% f; | r. c
解决方法:, ?6 q5 Q5 j: b9 s* u6 q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 [ k4 m; f ~
) c3 q: _9 X. t, i, e
SecAuditLogDirMode 0777+ y8 r, {3 P' T! h( l- @" `" ~7 t
SecAuditLogFileMode 05507 f+ {* ^8 e# w8 e. T
SecAuditLogStorageDir /var/log/modsecurity" W M2 q3 q, h8 h. {
SecAuditLogType Concurrent
; s" k. I% X9 E) D$ t参考文章:
" ?; C! ^# V- w' r2 E- ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! i" d$ y( |; Y" b" S( {: x
http://drops.wooyun.org/tips/2614 |
|