|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* M. a5 i& A$ A% ^- L$ a. _8 Z( Y) h' _" h" m
一.准备工作, C* w# H4 ^5 m& @
* H1 ?" [: W& W8 p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 b; F* k- L6 s" c; K$ B. i5 n# O. ^( ^6 S- U# H* m
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ `% \; v' ?- w) q
, W0 ^8 ~; K$ o/ f2 a l
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 C0 j2 Q8 N. y4 Q7 G0 E, k5 ~ e+ Y2 g4 p
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% K* w1 x, Y1 C+ g0 P
- h+ C0 y/ u$ E Z; I
依赖关系: d, C) e" d$ g4 v# F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 h) \; B7 H9 u" b; [4 e6 S
4 H6 F) S T" Q2 D; l0 uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
, w; w, X/ c4 G2 kmodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 q7 p. _+ p. g9 _2 } b' A2 U! b& `% D* |6 w9 b! H
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 t2 ]" \2 g$ n, M; D
二.启用standalone模块并编译
9 F8 W1 i* `. P# v4 Y' j p: V" V/ f/ z; {
下载modsecurity for nginx 解压,进入解压后目录执行:
7 b: [5 c# y; P4 |* {& P# [: _! P! r0 m" ^! y8 J d7 I' |; ?* s
./autogen.sh% T8 u* I( ^9 w! ], e, A- p
./configure --enable-standalone-module --disable-mlogc
1 q) `. Y4 |# I3 a) Z' zmake
" i$ I) Z* C( o: h+ u2 A! V7 m三.nginx添加modsecurity模块
( y5 t, R# B$ K5 p. d( E" v2 Z) u: V' g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' Z: Z3 ]$ }' a2 ^6 Q
' Y" B. _. p$ z6 x% ^9 c./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ f- b0 ~, W) @4 gmake && make install
! {+ L- q7 [+ s; v四.添加规则" S1 X+ J- V2 K$ ]' g; S8 Y$ s
" P+ p1 v! V) S/ [6 V9 D' S/ S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 z0 A% e f2 E6 I* q# T# ~$ C5 [$ p8 X. ^# a) L
1.下载OWASP规则:8 l. Z+ j% H- {6 B* k& Q! r
1 ]% z/ Z- ~) Z( U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 H, ~% a9 L4 V7 U0 C7 t. `
% R+ A: `1 @, p9 Y& |% a, gmv owasp-modsecurity-crs /opt/tengine/conf/
( i' \% Y0 p5 P. ~6 j R* @: x0 e6 f% g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 m& M5 f& y4 p; x3 `+ q/ c
2.启用OWASP规则:
: E! j& N. D, C' P2 l7 |! c( x
7 }+ p) S: W1 q! G1 z7 ~复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ e* ^# R3 H9 ^0 @2 S
: {9 a5 m1 N* ?6 |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( c& k1 y7 O* D$ l' G) ]
" l- d5 X1 O6 c. eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ J4 g$ V% l: t: M6 P" U7 P7 `! G4 d
0 b4 o' ^: n- K2 H- ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 H3 a2 R5 G J& s/ H; qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 r) W- f# `( C/ I, ~' ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% Q9 t2 o8 j, h# q9 wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- Q: J" q4 P5 p1 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# ~5 t2 D; p% X" _( bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 L. w4 a j% j: r0 Y4 K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; W4 a+ s8 M6 n, ?2 K; y4 F五.配置nginx
3 e7 A4 O0 D- R) p' @
+ [' H' F0 U, w! K; x$ y5 O在需要启用modsecurity的主机的location下面加入下面两行即可:
- a2 [2 x# b3 G8 J0 j# u1 O
( A2 I. T3 ]5 W5 S/ J; zModSecurityEnabled on; 1 ?( A7 j: `9 f& v1 v- C
ModSecurityConfig modsecurity.conf;, z s' |# X; T/ `8 z3 ]8 T
下面是两个示例配置,php虚拟主机:
$ D1 X+ S2 U) R4 Q
* v1 ^& O/ c Nserver {
; _ i6 l5 i1 w2 b( E A0 A' t listen 80;1 ~8 n! ?$ C( T u$ F# s. H
server_name 52os.net www.52os.net;. \4 Z: P5 i' i! A
' q* H: {$ }9 e9 I, V( Q7 G
location ~ \.php$ {% R" n! w. c2 N7 V2 o z" A Z
ModSecurityEnabled on; 4 P+ y+ }, r A, B5 c
ModSecurityConfig modsecurity.conf;4 d# {( i6 S6 g; q, C3 G
4 ]9 ]; R3 K+ k7 u g) e root /web/wordpress;
' c) U0 X) j W7 Y' F: l+ J index index.php index.html index.htm;
; A+ s2 a+ C. r: ^) }- ~
, h& m; c2 `2 Z; O! w( c0 j2 N fastcgi_pass 127.0.0.1:9000;# f+ |" h" z S
fastcgi_index index.php;
5 D$ _7 a7 o( O1 u! S+ K- Y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;5 w2 |/ l, `' l/ a) K6 h
include fastcgi_params;
5 [1 S3 M' s# {3 t$ b }
+ @/ D7 T/ c! S. [! E9 N5 D }
8 F7 T) L7 x% X- |' j* y w8 Aupstream负载均衡:
1 l0 @ d6 r- P9 f- z( \+ f | z! h. X5 {: \; {2 B, H
upstream 52os.net {1 B4 T: f. L* ~1 H5 A/ K) y2 g5 J: w
server 192.168.1.100:8080;
5 l6 T3 I0 [% h7 g1 B* t1 @ server 192.168.1.101:8080 backup;
9 r: R) J4 E7 Z4 _3 ?$ J# f* |- U, B}
, R% P7 I$ U4 R: [# N* G% {* J& l1 x. B% f
server {3 T+ d6 H& h% ? v
listen 80;
! k T" z: j# ^, K# H |( Q7 gserver_name 52os.net www.52os.net;( D, D Y6 U1 x/ K6 d% B
& Z$ A, T1 s- \5 f' E2 zlocation / {% O6 e8 M/ D' I; M6 ?9 X, x
ModSecurityEnabled on;
3 ]0 U, g* ?' r) M6 K/ j2 ? ModSecurityConfig modsecurity.conf;
* T" s8 c) B2 o+ n9 h
2 ]8 Z$ y h. q* Y0 p; K proxy_pass http://online;7 ~ D$ J- k0 E2 f8 Z
proxy_redirect off;
) U6 c: Q2 F: p' f$ W% T$ A proxy_set_header Host $host;) E, g5 ?. O" W/ V( N
proxy_set_header X-Real-IP $remote_addr;( P: U4 P, L w
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 s. n" \5 c2 U5 p }. v; }
}; `/ e2 H2 T& [/ y4 z& Y& J6 ?
}
+ I: h9 U5 F7 _; ]4 B六.测试
6 z: u4 u, \: T. r, i1 [) \5 J. B0 v9 C2 X" v" K$ @$ g+ e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. t) o. W! k3 f* u/ L7 g8 Q4 D, j! v; H: i8 S; M
<?php/ J4 T) E$ S& k/ O
phpinfo(); 6 d9 z9 y' | G6 Q
?>) {$ g! M# L3 I# z( O @
在浏览器中访问:6 t* ~* ^* T& }- E7 o, K
* C. Y% `2 \9 c* L& l% w+ T
http://www.52os.net/phpinfo.php?id=1 正常显示。
4 a. I) R. g, [, H4 L; |# ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。( ^$ {0 q& c2 `/ p/ G2 x% n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 X2 D4 A7 u* N
说明sql注入和xss已经被过滤了0 A* x' }9 M4 A
9 H z, R" y& p
七、安装过程中排错9 b2 R% A' A4 w! t4 Q
: L" W' X9 Y- D# Q: o5 o2 I3 n# `9 S
1.缺少APXS会报错
' l1 D' T$ R" P6 W# a- ?4 U/ O J3 Y) T3 [' A. r
configure: looking for Apache module support via DSO through APXS
* D/ `* n$ [9 ^9 }' |% Kconfigure: error: couldn't find APXS
, Y1 l8 q/ ^3 q' v9 b+ n/ Kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, V8 h: U4 Z1 I' ]3 _' C# k
解决方法:
* e/ n; B- E7 j% S/ k0 E
. i1 S5 ]0 P4 _) d- s Lyum install httpd-devel1 {4 ~. p" `- j+ b' g' S
2.没有pcre0 r t6 t- v* s$ ]: a2 J
. G) O& j! l' g m: W' d0 P
configure: *** pcre library not found.
( G' j$ l: h7 q5 i! Oconfigure: error: pcre library is required
4 [$ Q) ^+ f( W, B2 I# I解决方法:
) ^! J2 z6 I" E3 N3 t
/ j# t, A h3 L1 Cyum install pcre pcre-devel
3 c2 A5 S& v5 Q3.没有libxml2' ?1 J; H! Y8 I' h
8 Y9 [$ v, d6 K( ^3 @# a# e( u0 n6 Q
configure: *** xml library not found.
~" W2 [% I( {( ^+ d! J$ y5 Gconfigure: error: libxml2 is required
" P. c, D, `8 l0 C& Y解决方法:
1 r" R% y" l# m8 }7 f% i6 |4 D3 G2 i' E4 t. ]
yum install libxml2 libxml2-devel
/ I9 A4 k/ x# W6 V9 n4.执行 /opt/tengine/sbin/nginx -m 时有警告" N ]8 W* R2 _2 f+ G( S! ~
2 s5 L0 Q i9 k( q8 {4 K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)& P1 z# T, s% Y7 G
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 K' R) k1 k3 C0 o5 g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" \3 ~7 u" K/ c5 e4 ~; Z G3 }2 Y: R# ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ `# ~! a- j0 S4 r; o$ g( l' G& g8 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
+ r0 w1 E# u% b( I. I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* ~7 U- S( H# T2 i; F) C: S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! n. N, r! e: ?3 A* c8 A9 }- k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 T5 r# s3 P1 n8 E' B3 j! I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 g% l/ K0 F, T5 y: ^解决方法,移除低版本的APR (1.3.9)0 k2 I+ ~1 O, q( j' t% ?
5 P7 N4 q. d; V* O
yum remove apr- z1 T2 B+ y- H& u6 S
5.Error.log中有: Audit log: Failed to lock global mutex/ r* x ]* k5 J( N6 C& C
) z4 x' ^2 t/ D. u' ]/ {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , J+ O4 u W$ b5 z9 r: ^- u
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* ?/ C" }& c$ R. k( c/ y8 ?
解决方法:
5 m! s# U+ e0 h; B编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' f; i- ^4 `# T& m9 G5 h
+ U7 x! m% L3 `SecAuditLogDirMode 0777
4 }: G# I7 l( q0 D- Q I. lSecAuditLogFileMode 0550% J# H# h$ }; y Y t/ t& W
SecAuditLogStorageDir /var/log/modsecurity& k& j) i4 J- O) ?6 g2 f
SecAuditLogType Concurrent+ t3 f. ?" I/ R' F: x9 \
参考文章:. O+ L& R3 @. z M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 w! q, k4 D9 J4 e; ?- @http://drops.wooyun.org/tips/2614 |
|