|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 G2 d c. `5 R- u
|4 m' ]3 d5 ?+ C% {$ z( _0 i一.准备工作, m2 ?/ j' x, C6 o4 l$ p
" ?! U4 D) k2 |系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; s7 E" t3 [6 P) M. ~
w* g7 H: h4 l2 U/ I# M6 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. t! ?; k) m, _0 l) [- H4 n" S! ~
7 d8 |" R4 L. Tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz D: a/ I7 Z1 |$ L
# D* V6 t6 L" m2 }# S$ a* u" u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' q4 X. ^5 S' q8 ? u+ q1 v6 W6 Q F C
, m+ Y% z: t" n c- S
依赖关系:
3 W- l! i. p; c1 @1 Wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* l2 S a6 m* f+ ?1 p
% o$ x. [* J) G& n5 q
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; B Q; X2 l; G k
modsecurty依赖的包:pcre httpd-devel libxml2 apr
, L8 _. O; x# o; [! W; k& T7 k1 N. T4 ~9 D5 x
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
& R! u8 f* S. Z二.启用standalone模块并编译
9 i% u" u3 F6 T5 ^& n1 ?& k# M6 ^1 r7 N3 Y# O: C1 Q9 _
下载modsecurity for nginx 解压,进入解压后目录执行:
) N {5 |; P& u' U7 ]8 y6 b J1 M/ b# }
./autogen.sh
) r6 R4 r$ s8 L2 S5 s0 M3 D* J./configure --enable-standalone-module --disable-mlogc2 K8 `! }& \' n" R! V6 k. j
make
$ {1 d/ T5 V" E" ]4 c# J3 ], a7 n三.nginx添加modsecurity模块
; T! n- \& e% F; N0 o3 m+ s1 `9 q" x% }* j* i6 V; K9 u4 q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, t6 y9 a/ e- n! S7 }: x# J6 r# i$ W0 a! [7 t
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! t1 g2 X- f% _" @, y# M
make && make install2 g3 X. Z, m( C& ]) a' [ q
四.添加规则& C3 R$ N0 E- v) {% @ X
9 j$ v% O$ r1 I, m* B* `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。3 _4 f. L. @% J- @
0 e' {' h' L$ u2 V! o1.下载OWASP规则:
- y8 b, ?/ a$ e6 [; ?
' p8 @- r: v. egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ N/ @" Q+ O: [. I2 s5 `) L: u# q
7 _; f) r6 V7 i0 o$ Q6 C* ]) kmv owasp-modsecurity-crs /opt/tengine/conf/! Q& u9 ]6 J: v5 r
& \- ^" I: c! ^8 p- |% wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 ?1 E$ P1 C. `
2.启用OWASP规则:
4 R3 C$ O/ G5 W' u) t/ J8 N; d* ^
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: Y$ N. }6 T4 N, r: W
g# U) |0 e, _4 A8 k编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) W* f7 |( w) e( V w
/ R* L4 p0 h- |9 l0 v3 Jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! M6 _8 Q/ |: {" E. d$ C
# ^0 v8 d, s# a. ^) S' MInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' G8 G. z, ^ r4 n& q( i& jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
4 X* V, G7 ? `+ `9 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& B t S: ~& ?& K: D9 BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 A% h8 g0 u/ xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( X. l$ `) p2 \8 Q# n# p" f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) M4 a1 B: v _+ yInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. |/ A' q/ N# @ X$ t* a五.配置nginx
4 _6 X$ L6 u, R7 d" o
, B7 R0 L% f8 ]6 G ?在需要启用modsecurity的主机的location下面加入下面两行即可:
3 v- K5 k2 l1 H6 G3 b3 C
! M* Q( o' c" b0 ~0 }ModSecurityEnabled on;
, }6 X! l6 t9 ]$ h: jModSecurityConfig modsecurity.conf;
( K9 s% E- J Q" J8 ]' ?& S' i& X下面是两个示例配置,php虚拟主机:7 r: @$ ?- l4 a1 f
5 A" ] |6 B2 w5 F0 G! k1 S n" W
server {
@6 V8 f$ F. y# N& B6 } listen 80;
) r: ]( ^ ?' F- @) b1 ^/ s$ K server_name 52os.net www.52os.net;
; a2 l/ a. J" ~$ o. j+ u3 w! K / e5 ]' J! J K" e3 M/ Q& @
location ~ \.php$ {
9 ?" B/ F# k6 b& {& ^ ModSecurityEnabled on;
! c5 i* l0 u* q; o7 c8 i ModSecurityConfig modsecurity.conf;- o3 t% W; K; V: }6 m3 h; }
+ P% }' v M1 f6 X" B* E
root /web/wordpress;
, x- t6 j i" z index index.php index.html index.htm;+ {, ^0 f. R3 h: r/ P5 a6 l
+ W! e/ M4 m% J2 H6 P+ e3 @
fastcgi_pass 127.0.0.1:9000;* t8 [6 F! j7 c) ~
fastcgi_index index.php;: E4 h: n3 Q$ O2 s# |" H% K
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; ^, _6 a1 ^- d7 B% o1 W
include fastcgi_params;, Q5 `( E& D z8 X$ p8 z( h
}- E! x; A' s2 P& O- }
}! ? Q" x1 a ^
upstream负载均衡:
) X4 H! V: y0 m# x
# L( N' h* k: fupstream 52os.net { a$ N5 f8 G# f' l% ~# m& ~
server 192.168.1.100:8080;
0 e+ }' [& v' } h server 192.168.1.101:8080 backup;( |6 U- T. S2 d
}
5 b- g4 q8 ^0 a$ A, _* P
; A H3 B; M+ \$ n8 V) }server {% ^% u9 H' p; z
listen 80;# a" M. i+ k, E
server_name 52os.net www.52os.net;
# y$ ?! u$ V4 O* h+ \! r
, C5 X1 l8 R4 g2 g; U: Z* Vlocation / {7 P; u$ F/ y. }. }
ModSecurityEnabled on;
. v$ a( e) S* F ModSecurityConfig modsecurity.conf; , \" Z2 B. n/ I' T3 w. n/ X) U
4 O8 N* ]* z% g- e, D/ g3 a1 y$ P5 A proxy_pass http://online;
% }5 k; ~. T% H. \; v9 l0 Z; e& o proxy_redirect off;- ]( L: A( i6 x; s1 a, d
proxy_set_header Host $host;* D6 S# ^9 r Y9 ?4 k2 R. l, g8 G
proxy_set_header X-Real-IP $remote_addr;
5 R. N9 S3 ~' p0 m4 {1 ` proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ M, S3 l+ ~) B
}
M9 J2 v6 y0 ~2 @7 _$ _}
' l1 }( a) @+ d S4 |5 p/ Q六.测试, \1 ?+ [. f0 D/ I$ _# B
2 T3 J( T' A+ e- y* d: ?; V
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
C( K2 @* f! G0 e7 N
& o0 \; }5 \3 r# u. f+ T1 W# y% A<?php; J) @9 e4 o9 S
phpinfo();
* Y3 B* P; F) ^# ~0 G. x+ E& v8 s4 h?>
4 o$ W, @- o! w2 _4 |在浏览器中访问:7 k: q" B" L7 @( D
# r# w! b+ U' S$ ~3 D# C2 K
http://www.52os.net/phpinfo.php?id=1 正常显示。: O- }3 ~' c U" Z3 R I
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。7 O9 P- t* c# z2 E+ ]
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% ]0 f, ?0 p) h
说明sql注入和xss已经被过滤了
8 I) l' A( @8 u. i# p! @$ l$ I
9 X M% Y8 V' R0 u% {七、安装过程中排错/ P' W r4 j. s2 w. }7 `
3 @# }% y7 |& O* H/ o2 X4 ?
1.缺少APXS会报错$ `# L$ y% Y" h+ [
/ w! s9 [ ?' Q
configure: looking for Apache module support via DSO through APXS
* e2 p& C6 t* E+ k2 V' tconfigure: error: couldn't find APXS
6 A: M- L+ z$ T0 V) T0 [+ t" papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
w2 v7 _) x8 g& M. \解决方法:5 Q. o& t6 i* d; i
7 W0 ]: _! `: I9 p! myum install httpd-devel9 @. S+ m* L V) f
2.没有pcre8 S* H% e; a# ?: X) d- d
7 Q% Q, a: O- O1 cconfigure: *** pcre library not found.
! G. `) D0 ?5 F1 X! P* Oconfigure: error: pcre library is required
! H1 v2 S/ T& L% K. B! n解决方法:
+ _# {6 ~; v# {+ P
5 |# ~ ]0 D, F& d" O0 L" d- }yum install pcre pcre-devel
% ^6 ? T- z y0 ~# k) \3.没有libxml2 }7 z0 \; n" n( V! Y' _
3 i# [- J. A% ]+ T+ b% }+ T4 o. V& S1 u8 X+ C: n8 w$ p8 }! J! W9 t& p
configure: *** xml library not found.
8 D4 A( @% R5 e% bconfigure: error: libxml2 is required/ y$ e# Z6 ]/ h7 ^& D4 r; |1 T
解决方法:
% h3 |' R: P, e% J3 ^* p2 i) q5 w9 n/ r/ o @5 q
yum install libxml2 libxml2-devel
/ ~0 C( @; c7 ?! _" S$ T0 t4.执行 /opt/tengine/sbin/nginx -m 时有警告9 ~, w' f* S7 U, E6 Z5 X7 e" A
# h [8 }( C2 x8 [/ {$ L9 X; ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 F; j' w# _' e* ?6 _nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, }. V/ I3 Y% F8 \, D* V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' U7 ^5 A( X. v; r
\1 a. Q4 B4 A) m1 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ ~5 `+ n6 O9 F) b. P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# q6 f+ z( A7 R; v, @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# _# F/ ^- x1 ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, O* Q; Z4 _' F a: J: O# r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ _6 q1 c+ v# T1 M3 V; u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- |. k+ [1 b1 o1 `1 m解决方法,移除低版本的APR (1.3.9)
( h0 O2 O: h0 }5 o6 x S# i
7 Y% t5 t9 b) ~. Qyum remove apr- c8 O! z( `1 c8 c/ ?
5.Error.log中有: Audit log: Failed to lock global mutex
+ h3 d* A, O7 b- w- m m* ~$ _( M- q& K4 D7 `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! S9 Z# Z! Z' A$ e, S" Mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* h- V" B% |3 |解决方法: B6 |; H# ?) ? Q3 U
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. {+ ^4 h( W& x& h) P6 I4 p
7 |" y& H4 N: C. WSecAuditLogDirMode 0777. @/ `* Z0 _, r3 {- G1 ~
SecAuditLogFileMode 0550+ U- L9 j# Y* F' V: q" ?
SecAuditLogStorageDir /var/log/modsecurity
" Q3 d+ i- B( V2 QSecAuditLogType Concurrent
3 z" F0 A, C# S参考文章:+ `( q0 V+ [$ o" s" M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% L1 w+ y4 l' S$ h$ N
http://drops.wooyun.org/tips/2614 |
|