|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: R4 k1 ]8 P$ v5 f7 f9 l& s
% K# `/ b0 r; g+ H' v# d
一.准备工作% q3 x1 g% _5 O1 g4 d0 `, E; e
9 d# Z1 ]. @" P8 D+ o3 K& Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 M9 Y' X" `. n) V: z9 r; }1 |
4 ]0 a2 }- S6 S0 Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 |8 l" e* |4 ~6 T2 h7 V
0 R4 c: ]& H/ f% p& [$ e; t Ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 i5 _0 q9 j; h% w8 D' k) V) c+ I" f
. r$ o+ c+ J7 [7 I1 }
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. L6 s- }$ ~- @% [+ E
$ U( _/ I `9 q$ x# j( B依赖关系:& `' }. w5 D( a; C
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, ]: s: A/ x3 Z2 K: s
# N3 z* u1 _+ j. N: p) d! w
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 T! J$ ?5 _8 Q: R5 omodsecurty依赖的包:pcre httpd-devel libxml2 apr, u; j0 ?0 D) ?- e; V- a5 P8 ^
& O; x4 Q: n1 o5 z, A
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# ]- B% U- c: I9 o( h% P
二.启用standalone模块并编译1 f+ j( K* {4 Y5 C8 {! `
* L- e0 a/ d/ B# B下载modsecurity for nginx 解压,进入解压后目录执行:, V. s4 ]+ E7 [$ p; q
, e* [4 r, F/ E x5 V./autogen.sh
1 g' {3 s7 \* k$ l: K4 Z( x5 l$ x./configure --enable-standalone-module --disable-mlogc' `8 q. f2 D R# g: m' @
make
- x# X2 s2 c* N三.nginx添加modsecurity模块
9 E- O3 T: _. K' v- p, V6 A# b6 w3 h; l( s; q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ G3 L9 f7 V# X& C6 d0 O/ s6 i h) m$ W
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& ]7 P7 x8 K2 |5 K3 [1 n4 r6 O4 lmake && make install
' F0 N5 _5 r2 D7 L7 n四.添加规则
$ ]* j; O+ m) T6 t+ D( \( u0 u8 }" D/ E( ^7 u
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 ^" |3 Z+ y3 M- N. D6 y
0 @1 {) j! b4 \
1.下载OWASP规则:
2 i* {5 t! K5 ~3 l
0 M0 {/ {2 X+ S+ T: {6 o% \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" R9 M. G( e" F; V2 J# K4 e) Z
* R# H$ f& o" G' w/ N0 ~mv owasp-modsecurity-crs /opt/tengine/conf// w6 T) v6 v- D5 l F
- z n7 N/ I1 u9 Pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 O0 @# {" K1 v
2.启用OWASP规则:
( U4 b( r ]* u! Y& s
1 W# {2 y4 [& l* g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) m( n% f% T2 H( g
* Z1 l: Y, c& c7 `3 U编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" v, k! j2 i7 I+ }0 [# J0 w9 u" ^& q( l6 a+ \" M$ |0 v
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 _9 ^6 y- h8 f2 v: J
2 @! a; Q) Z& s& v. }" bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
$ C& a/ a( c7 c" f4 P9 bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 X3 `1 Z4 `5 q0 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" _( h' I5 Z! \2 m) ?! E) v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 t: K# @0 Y9 R( o) u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ [7 m( e/ I. \& v! l4 j: }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 j3 D- `: x. W) e& H% ~1 CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ X9 G5 i; \! R7 i+ S9 N* y五.配置nginx$ x8 r& G- f# P5 {+ C* {" z
% N1 v* c3 e8 }- F; I在需要启用modsecurity的主机的location下面加入下面两行即可:' ]1 t$ Q0 W% M" f. L8 W
) T$ G6 K* j# S) }% I& T$ P
ModSecurityEnabled on; 2 X- v t: N& a* F( [
ModSecurityConfig modsecurity.conf;. F5 z% @: K3 l+ f7 _' D
下面是两个示例配置,php虚拟主机:% R( N. \! _, w6 f9 }9 ~+ L# a
k( [2 R1 c' u* }5 r$ A( _; i6 N) Mserver {4 A% u0 O9 V( T E k. |" D3 G
listen 80;
' e& e4 a! R! j& k, |) t; ] a server_name 52os.net www.52os.net;% _. ^, y4 ]+ l. H7 ^+ o
9 l. F- r) Q! P
location ~ \.php$ {6 ?3 `9 F, }( c( R: N0 d3 g2 B. ?
ModSecurityEnabled on;
* f! O4 y3 c1 s. }8 v" ]9 v ModSecurityConfig modsecurity.conf;/ Z% M) M1 x$ C7 R2 y; x7 W
; w1 Q2 {9 Z, ~! J( L/ g2 a/ k
root /web/wordpress;
& Z8 R; N5 D& Y index index.php index.html index.htm;7 K G- P1 ?( J& L( F/ p
) I/ m5 B. p; [5 T4 H8 \* k
fastcgi_pass 127.0.0.1:9000;1 d" v8 m( S( U
fastcgi_index index.php;. R1 r" c3 r. s! g
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' R9 E6 |4 ~8 b, c6 }
include fastcgi_params;
) ~+ S1 V2 O/ _; z/ g }/ S5 J% @8 e) v% @" R. C; l
}
0 n% c! A* Z( k. H0 P7 }upstream负载均衡:# L' h3 q5 R1 ~% C1 O2 b
7 \0 N# E1 p' o% C* B5 g
upstream 52os.net {
, t5 F: f6 c9 O1 z server 192.168.1.100:8080;
& d; e0 r& b& [9 _2 O3 R; e7 o server 192.168.1.101:8080 backup;
: [5 ?/ m" o$ {}
& a& _8 w. E6 \! Y8 @& C/ X, j( U' f! c8 T! f2 U" L
server {
+ Q7 a0 ~% f0 Q) b3 ~/ r. Flisten 80;* O6 S: P* r9 P) O5 }" w
server_name 52os.net www.52os.net;( b& b. x/ `( ~. N/ x) c8 [+ c
8 m2 J2 z+ z W2 |/ Z: \* X: Rlocation / {. F% J4 [) ]# _! h
ModSecurityEnabled on;
( Y5 n3 O: p2 W7 d2 ]$ | ModSecurityConfig modsecurity.conf; . q4 x5 N( G0 B7 Q3 x8 a
5 N/ {6 _# }3 g" ^ proxy_pass http://online;
7 W; y+ H6 ]- f' d( G/ a proxy_redirect off;
5 V0 F* R6 d; A% G/ p proxy_set_header Host $host;
, P5 e7 ], m0 D+ |/ L proxy_set_header X-Real-IP $remote_addr;. I$ i* @/ K; H: k
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 J7 F z: R" O R* Q1 x4 _
}
4 }9 S" f7 i! [2 i0 X9 G: r}
% x6 ~2 m% @5 I* N8 ]' r$ I1 a0 Y( O六.测试0 F% E, a1 _; @1 w
W" l- W& y8 n s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
! Z( T. t: x" ~+ q& l. Y8 F8 f& [
% l5 F( j8 Z. n- C/ i<?php
! W6 o) D! A: u. h4 q phpinfo(); + T+ Y# G: V; g/ D) z! c+ M J
?>
. b7 U6 R% K6 U$ v* T' E+ {8 j在浏览器中访问:
/ M5 M2 J7 }- y& x' W! t; X
$ z% K* p" [% E: C9 w# x8 f) l t' B mhttp://www.52os.net/phpinfo.php?id=1 正常显示。4 t: e7 g, D" V. |/ N5 H; g
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- q3 s' r# @, Shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 L1 l5 M& G' j
说明sql注入和xss已经被过滤了+ m$ t$ X8 i4 h( Y; @
' Y( D% W! a- g
七、安装过程中排错% [* J8 @$ a8 x. b& F
" k$ M8 F& C* P1.缺少APXS会报错; m% s5 I6 @; o r4 a
/ G: G/ S( d, Iconfigure: looking for Apache module support via DSO through APXS
& y1 i9 h& a8 `& q6 X0 Uconfigure: error: couldn't find APXS: ^1 M+ x; \2 d5 J
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* c g& B% I$ _$ ]* @5 E$ x
解决方法:
/ Q3 B) W+ X. Z
3 L1 i( M' O8 U+ Oyum install httpd-devel
) `) H( K/ L$ m6 @+ s/ v" \( t2.没有pcre
3 L, C( Z3 h$ T2 H# w" Q" c" D3 |1 W
configure: *** pcre library not found.+ w8 t- ~& F8 a2 m8 Q/ B
configure: error: pcre library is required
; F2 j, M, ] W6 c! Y1 I0 m解决方法:; [, N% r( J9 \4 i' d* ^# W$ t# ^
# I; E# R2 A. u- k% G9 q$ x
yum install pcre pcre-devel, F- I% }' ?& `3 z$ ?8 P
3.没有libxml2
' t* {6 V% y: Y9 w+ b* [4 d6 @0 g; v* v
. Z# `( L, \( u2 P; s- m9 r3 cconfigure: *** xml library not found. X3 n. `- n" W2 y7 O$ } t7 M+ V" @' }
configure: error: libxml2 is required
5 W- C0 o1 R0 R5 i6 Y, v) A1 U6 [解决方法:8 B& b; i" b+ Q: p+ I
, i, w* q0 U8 F& k/ |yum install libxml2 libxml2-devel
( c1 a- ^ |/ l7 U- L1 ~4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 t6 I2 S: K( @
6 i/ h! K$ A! `5 _) q! eTengine version: Tengine/2.1.0 (nginx/1.6.2)% u% h2 I' {1 Y# ~ z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# y4 p$ D! \1 k5 d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( P5 F. V% p D+ _( C- R5 n& E- A9 z
" y1 I3 \, Q6 J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' Q& S5 f* n1 u N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' o# f, R! K6 a- O1 {1 b2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) G1 W1 `; U! Y2 L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- k/ o& D e5 u v) ~. B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ ^% O; o" H5 r) G. s+ f2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 x$ A; n# v' w1 E解决方法,移除低版本的APR (1.3.9)
6 Z0 Z5 ]4 {6 D' R6 w: v
s0 m% `6 e5 v3 M1 l- Gyum remove apr
6 n6 E" P/ U) U' i" k- l5 X( B5.Error.log中有: Audit log: Failed to lock global mutex2 e1 d. H" ]" b% r& G# I
5 ~7 y' l2 n6 S) T3 A
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; U' ]9 _2 w! \. N" N1 Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 @' [6 p& P+ ^" m8 p) |. t
解决方法:
. o' ]' |2 \8 U# e" \; z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, z% T; E! M- `/ Q0 V s: H9 h/ d4 f; {. I) A7 n
SecAuditLogDirMode 0777! `0 D) Y2 P" B+ p# H7 ~% x' ^5 ~
SecAuditLogFileMode 0550
* F* N/ b5 F) N. G6 d2 b# NSecAuditLogStorageDir /var/log/modsecurity0 K `. F/ Z$ r$ R% A; j
SecAuditLogType Concurrent
) m% D. f. w+ B3 S/ k参考文章:% \" Y$ G/ o' d
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 |3 ^- G( p0 p$ P; F* D. ]+ V' h3 e' Ahttp://drops.wooyun.org/tips/2614 |
|