|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) q# Q& I: N$ }
& @1 S$ p1 q. h8 T& }6 r$ |# b一.准备工作
5 V4 F# W' a# ^8 r8 c) i3 t# X5 @5 O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ U; r0 m7 z6 p! r. c9 f @. {! R( ~% T7 Q9 C& M4 F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 H7 H% _9 q: B
4 Z/ w7 T* O. w i6 c1 b
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ K/ A/ e+ U4 e
$ l9 P1 E, z. T; kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ K1 x+ t9 g0 c5 U
: [5 Z4 D, k4 U6 ^# n! ^& \8 \3 K依赖关系:6 b6 a x4 W% a* E$ C; w- {/ q Q e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( y, l% U9 \* w, p
' m7 _: u1 }; w2 P" Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 X* i3 k; z2 Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
2 r9 y! I3 i$ i! V' S+ @8 i) R, }1 v6 M" B. z" Y; p2 [
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
8 m: @' X, M6 A) n二.启用standalone模块并编译
2 }2 M$ w- w* J3 K% }6 ?3 S# x
' o) g5 y9 x+ g7 _下载modsecurity for nginx 解压,进入解压后目录执行:
$ \4 j1 V/ U" i* _2 U. O l3 ^" S
3 D+ ~1 f& n G. P. }' {./autogen.sh4 J# @) S/ a( V: u3 N2 a: D N$ S
./configure --enable-standalone-module --disable-mlogc I, f+ |; ^9 m, j
make
7 U1 o! k0 `$ h' D8 d8 Z三.nginx添加modsecurity模块
4 Z8 k( l; L g( Q H
- [2 u! K. H5 L' R" J, _6 W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! _/ Y P/ j! @/ I* e7 E$ M* a- @' L$ \) v- e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ _9 j( m: n" p7 B# T) S9 Smake && make install
+ Z1 G5 Q- F5 c' R7 ^四.添加规则$ A1 X! X/ h$ m
) q% e+ f# \' Lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 n" h5 R) ?+ f& F# n( \6 e; c: M; f, e/ a6 N6 I
1.下载OWASP规则:: b+ c W* c# a/ t [
4 g- E+ j( c S. y, J
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 x3 F f- B8 `9 o0 |
6 l# x1 ~7 i- ?( Cmv owasp-modsecurity-crs /opt/tengine/conf/
4 ?: k4 U, [5 R' I$ i9 z: d6 H( x) r4 O& t* B f$ `8 N
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: v5 t0 Z; m& X5 Q
2.启用OWASP规则:
0 Y" V1 V; b5 x4 A O- |* R" V& m" h" _& e( w7 s; ~' a4 c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: ? e4 f/ h1 d |9 R5 @6 \0 f/ g, z! f" |5 P, [3 ]- K& l( r! V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 y) c; A4 U8 d3 g" o
# \7 D: v0 n1 \' T0 d
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: `! O Z4 o9 Y4 [. K: P6 ^& P/ G
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" o' w/ D3 h1 v7 eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ e/ p4 E, d% w a6 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) Y' z. {! D5 O) V* G: @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. T6 l f) k* ?5 U, DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 G3 [6 Z# _ i! p5 C% jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 f* }) |, ~$ _- B: A) m" l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 q; ^ h. z* i+ z) _* g4 S; a五.配置nginx
+ Y: a8 Q6 B+ h8 \6 y
( l" ^. u) v" G在需要启用modsecurity的主机的location下面加入下面两行即可:" `3 n7 V( y- S% \
- C. m9 V8 T6 ]: _0 v3 M( @
ModSecurityEnabled on;
! W" s5 r3 ], \! l) u9 |ModSecurityConfig modsecurity.conf;1 C z+ B' t: ~
下面是两个示例配置,php虚拟主机:7 ?1 ?. u# h( H( N
. ~- O q5 n3 Yserver {
- H% a' _, P( x! b listen 80;
, f- b1 N/ g m1 X* ?3 t8 L server_name 52os.net www.52os.net;
8 q" g( I4 T; d: r5 X D* O$ A; D: j * C* j$ I& ^- S1 A7 \+ u" g3 d3 I
location ~ \.php$ {
/ M1 E: k- p; [! ?# l6 T' ^ ModSecurityEnabled on;
& u4 i8 n0 {0 g& \$ j ModSecurityConfig modsecurity.conf;
, G( [* Q8 Q! q a+ N* I0 D& y, R# m" n
root /web/wordpress;) H- @% V4 ]4 n- y% f- e# F
index index.php index.html index.htm;
~2 f; I) O4 d # ]3 }9 ~" t8 }3 X( O
fastcgi_pass 127.0.0.1:9000; @* J9 L' O$ s; @( s
fastcgi_index index.php;
& A$ a3 U! B# [- _1 g5 o$ y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 @7 N/ k) M7 {1 M( Z7 z include fastcgi_params;
* G: C3 V3 n/ {* U1 c }
7 d0 g- D5 c7 ^ }6 g: g9 b& Q. L. B" y, H# l4 o2 a
upstream负载均衡:7 T1 s; [4 j+ w+ D* J& ]- }! g3 ?
+ |. Z& n4 P' @8 D9 ^" Fupstream 52os.net {
0 K& o5 q, h8 U server 192.168.1.100:8080;
2 s: V* h+ C2 B8 v& P. A$ M server 192.168.1.101:8080 backup;
$ _/ ]) Y7 c a( k" O5 u} Y5 n% f: X9 V
1 \0 ~. R) P7 m! v% P+ r
server {0 F& i$ Y4 C: i& i% D' D" _8 T
listen 80;* \$ j! d9 h% P: X" [' r% Q
server_name 52os.net www.52os.net;
( y2 a# L/ W( F4 I; x8 |, K/ \! v% x. E9 R: k
location / {
" H) l F* v- o, i$ V" m# W( o R ModSecurityEnabled on; 6 P$ y% t% H* @# b% q
ModSecurityConfig modsecurity.conf; 0 Y$ F5 o' N4 n8 | \
$ ?9 c" C7 @+ [
proxy_pass http://online;
+ L p: n: y5 z; u: k& B proxy_redirect off;: _ K+ o' k' @7 X
proxy_set_header Host $host;* s* w, ~+ E% X$ L" n
proxy_set_header X-Real-IP $remote_addr;' _4 }& g! y* y. N2 f- }% ]2 W" G# N
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; h; t0 P/ I n
}2 b/ v/ c+ o% O! Z8 e8 t% k
}$ M0 _( s2 ]) A/ t5 V! z
六.测试9 e) q: w- T9 {2 A" ^) P# R
& T# q1 C2 R# v7 P; A
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# Y5 t W( B# n! x) f& M9 W, K+ J: r
; q+ D% }% O- c( `% ?- ]" Q<?php
9 { j- ]. u$ O# d# ~* d, f" X phpinfo();
4 }% `; O. o) H; |( C, R?>- x1 B$ G$ ` T$ O
在浏览器中访问:3 n- N0 k1 a8 I
# V$ g: W ~9 d1 i1 ~: M9 Q) Uhttp://www.52os.net/phpinfo.php?id=1 正常显示。0 \% M& T" S$ |5 I5 J+ B
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% k4 J2 R* s8 m. X$ _http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 h% X) _! J- w# X1 W4 Y! l
说明sql注入和xss已经被过滤了
$ \' p" T% ^9 R1 H4 B5 W/ p" K# G2 {" z1 z: Q
七、安装过程中排错* e' |& H; z7 @# e' s, g) n5 w, r
# y/ U C( T7 z1.缺少APXS会报错
5 j) b0 s0 z) t$ q6 v- G1 }- @& W% o# b3 [* T( k
configure: looking for Apache module support via DSO through APXS
3 y# K: n" C# R8 ]8 Zconfigure: error: couldn't find APXS
% a% T+ g3 O" Sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 }4 b" ~& p9 B$ @5 c& @
解决方法:
7 p1 O5 k1 d5 y
( A0 P4 n$ v5 c8 ayum install httpd-devel
" P# ?+ f9 E, i( L) b2.没有pcre
7 e" Y2 F8 |1 H) v
* B) W4 @8 ^4 h8 i- m0 l( Q7 }configure: *** pcre library not found.# `$ C0 [/ Z$ A7 j$ [, V8 B3 s
configure: error: pcre library is required
4 H, l0 [" A, K: ^ L; V解决方法:# k- V4 G& a \" W
4 }8 i8 i6 l& o" G9 O3 Gyum install pcre pcre-devel
1 e9 |1 Y* a3 x3.没有libxml2
+ k4 ` i; G( u$ `4 q1 X3 h) ^
% m" K0 J e8 H$ B3 Z- O4 Z
8 z, l. i* h; }! xconfigure: *** xml library not found.
$ s$ L1 T- g* g% h0 {configure: error: libxml2 is required/ L- t- b3 Z$ O( z$ o7 Y
解决方法:0 y. r: q; _' S- ^! b0 X% U7 h
; P; `5 O& Y3 _ E2 Z: C
yum install libxml2 libxml2-devel
. l, I# {/ `% j4 I) [2 _4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 |) B! y: J9 B0 ?3 F5 `. I
5 @* G' E1 M9 d% d7 H* X ]% A3 q, TTengine version: Tengine/2.1.0 (nginx/1.6.2)! ~7 P0 j2 d$ Y2 l/ `" h$ {' w* z2 U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, d* `' i, ]! P6 t+ z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( ~. ^2 C/ M. E3 U' Z3 y. c
& e# M6 P( B+ S1 d, T" X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( n1 o# C. N4 E3 `2 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
3 g' r+ W0 `* O$ `7 R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ W% [2 F" T2 F" x3 G' D% A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") R# W# C9 m! |, A6 } l6 |( s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 S# A" P& S! g0 v0 k* N! E1 g( C/ H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 k3 R7 I/ G& x, G) [% `& m# m1 K解决方法,移除低版本的APR (1.3.9)
% l% p. U* r4 w' R1 M* i0 X. d3 D8 c1 i8 s% R
yum remove apr# v& d6 G6 x# R# O0 k& ? m5 ]" z( {
5.Error.log中有: Audit log: Failed to lock global mutex
3 S% M1 _5 N* z9 G* r# t5 u, W9 }( h# `2 X5 z* t6 r
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( ^% j! ^& I; ?, i4 Q- i
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( `( r- Q3 A* d, m7 y; S
解决方法:' ]; Z" \; Z, m' j$ @ N9 C
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 C3 _# H* F: C5 d# w" y { J8 b
/ U2 v9 H2 K- g( i- G2 HSecAuditLogDirMode 0777$ C" \8 m4 [8 G3 l* q
SecAuditLogFileMode 0550
- T% a" s1 Z+ D3 hSecAuditLogStorageDir /var/log/modsecurity
6 ~2 g8 Q- m1 Y' p) t5 U/ O. lSecAuditLogType Concurrent
0 y# I8 Y' |( d2 h3 V; y( r0 f参考文章:1 g8 x! ~/ b. `9 L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, ]( u* @" a6 E
http://drops.wooyun.org/tips/2614 |
|