|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 D" E. Y3 p, i
6 e G' I" c. |
一.准备工作$ a3 v4 b2 {" N9 C: x0 T
9 i5 V6 f, {8 W U" g3 y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- j+ U" I" k$ V) j5 L, e" g
0 G7 \9 d3 }) B% {4 Z5 v: b6 ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ F* l% m/ ^" J4 C5 g! F; l
# p# I% g. |0 J9 g5 r+ r" j
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& H2 ]8 v8 q1 v3 \8 D7 Q
3 Z/ ]/ n' X: }7 k% D, }5 TOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs g- \4 W! b3 Y: b) U
! I! g) H$ U& N* k( f' H
依赖关系:
, ^; w* K% V8 z. T5 Z' `9 Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ \! T. Q- |* B; g. l5 O) E
4 j9 C3 u, M& f+ s# D
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ Y( d5 x6 C* h1 Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ ^9 ^" f1 D" v+ q% @ i/ Q. q' [, u' F: h! F5 R" X6 y, k
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# l3 o$ a, Y* ]6 @
二.启用standalone模块并编译
! R) U7 i; Y D4 r" J. c1 M) A9 }0 x1 k( n
下载modsecurity for nginx 解压,进入解压后目录执行:
+ ?$ _ S9 e: `( ]
) [7 h2 Q; M7 W9 s2 I. \# e) b./autogen.sh* P" k8 m% f* _& o. \" X
./configure --enable-standalone-module --disable-mlogc3 c# c5 d$ S. E- i
make
" _4 D: Y' m6 D( r5 T. V N4 I三.nginx添加modsecurity模块' V3 ]: c5 I! I3 ?' B% V: k( i0 u
" N/ |4 K3 k9 h" u+ |+ {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; w8 V0 U1 M2 q+ x' i( z9 N r
8 L3 p2 o5 x3 @1 O5 S7 Q./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" Z' G9 r! D/ H# l
make && make install
0 w: h/ a. C7 N3 D6 _四.添加规则0 Q" C1 x5 R4 c6 B: m6 S$ K' p
, Y" T7 U/ K5 b, l& U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 Y. J$ o8 r, d- K/ e
4 P5 L9 `4 t% I
1.下载OWASP规则:
# Q* P$ o0 c8 _1 e
) W* V( B4 [0 f/ G- cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# m' f8 B6 M# |+ K9 Y4 Y% `% d6 w, {1 W$ F; n t
mv owasp-modsecurity-crs /opt/tengine/conf/8 m/ N! \6 y1 s
) B6 E! Y% v$ z g3 Q+ \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! R. V0 o! I* W1 f* u; u2 ?2.启用OWASP规则:% b/ b; S! ^9 n
0 y' e2 j. m, a* B! r. a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' l# X" x$ Y# x9 c7 S( _
: A0 K2 F( h2 p, `* N$ b5 G! `2 _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 h9 e n) l. p" f* |, Q8 R, E! j# G0 B k! f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 N9 W) ^4 a$ t
0 _$ D! M$ t3 Z6 c6 w7 H
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
' [8 q! L* ]6 h+ c( L# iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
Y2 [4 G( X/ J% A* SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
E; D8 ^: }5 B( KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- H8 C7 s# g! H2 K$ q! ^2 SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! c$ X9 z# n+ h9 `4 N# H" X9 k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) M$ L/ z& M. c% F0 c$ R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 G' V" ~. S$ j/ D+ I' k. D6 U
五.配置nginx
9 o- r: R; O; o$ _# q
: X$ q: E2 l5 W$ K7 D8 S% B在需要启用modsecurity的主机的location下面加入下面两行即可:! Y; K# O9 ?3 u; |/ `' P7 C' I
, J2 m* a, `( y) \! E$ Z/ R
ModSecurityEnabled on; - V6 ^5 [) y& M8 \
ModSecurityConfig modsecurity.conf;9 ]8 v" r6 G- ~' w2 {2 S
下面是两个示例配置,php虚拟主机:
5 _7 Q" E, A8 M N" D7 U+ i+ a* ~4 M+ L2 f2 d7 S9 }. |
server {
7 m% D. c& _$ x! T7 t listen 80;
1 M' _0 m8 g. ~7 b server_name 52os.net www.52os.net;9 ~+ Q: _) p! M$ f( x
: O+ m$ M( Y1 }. E location ~ \.php$ {. g& d. G8 o- J% K# i
ModSecurityEnabled on;
$ w; J8 K9 H6 R1 ?5 f ?- c; V6 o8 n: l ModSecurityConfig modsecurity.conf;7 f* K- u' j; N7 p9 ?) q% p( J
$ S2 E' W6 W8 o" w9 _# J1 q
root /web/wordpress;: h7 S% q0 v, R6 }4 J: R
index index.php index.html index.htm;
0 n, q. }( T; I6 D/ T
4 d v% j8 H' f" @+ j fastcgi_pass 127.0.0.1:9000;
& J$ x, Z' o% o6 V- {4 _* M! l fastcgi_index index.php;
- K8 f' Y3 o. Z& w fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% k+ f, k0 u! t9 p; W0 N4 `
include fastcgi_params;
% q* h8 \; o5 K' b }
7 a; K, n- |+ b( d: Y4 K }, a9 d( L1 m5 S" I. \: n/ [ G' \
upstream负载均衡:
4 w# j) S1 g' z% g# J7 r; d& D
5 q9 V3 |0 n. W" q4 m9 C1 W% iupstream 52os.net { L- m5 ?. g) A8 H' Q4 a/ j
server 192.168.1.100:8080;: d) S' _- A/ q+ L5 U) Z6 ~
server 192.168.1.101:8080 backup;
* k* n% o. W5 A- c" z- Y% L; k}
3 B. D9 M& w [) _2 t7 F( x$ r" {% P) r! |6 _" \' g8 `
server {
9 ?. c4 ?0 r$ L! W( p6 _+ Slisten 80;! s2 t1 z) z" D; p7 B
server_name 52os.net www.52os.net;9 }# J" n/ T" J/ D9 }8 `
+ F; _, X7 s. ~, C+ P: K, t1 j
location / {
- N: }- c4 ~& [5 T ModSecurityEnabled on; % v: v- e1 z) m
ModSecurityConfig modsecurity.conf; 6 k; k2 M' _$ ~# P, Y t; @4 E
3 o$ @5 b+ j. ` proxy_pass http://online;( M L9 Y( F% r9 w2 x
proxy_redirect off;7 D9 I7 ^ ^2 N7 |
proxy_set_header Host $host;; E0 {' x7 T, l4 V
proxy_set_header X-Real-IP $remote_addr;( V. w9 b, H% r# T" s: H: T
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 q( A. ?2 I$ a3 X% F! L
}
! J- t: ^8 L$ t9 r( p2 F}" c: x- {3 [% J0 r- P7 J
六.测试8 K: |9 J4 E$ ~. V6 b3 b6 X) p
; G" n7 u0 Y- [我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( D* s9 d% R3 ~6 b
! Q0 p. ?9 Y( @<?php7 r4 A! I) f# _4 n% x
phpinfo();
2 ?; Z" l: ?1 b# b* a?>
$ U- y3 S$ i( F) g在浏览器中访问:
. C3 A) l) K( R: B1 `5 q) g4 T( L- f0 x( _' M6 b6 m0 [
http://www.52os.net/phpinfo.php?id=1 正常显示。
% f6 m" J1 i5 E( o3 } f( Xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' X- S. `; I, a fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。7 F* i* b8 b& [1 l
说明sql注入和xss已经被过滤了
. e" Z* ?1 q7 G# H
$ k. U" [9 o9 X! C/ a七、安装过程中排错
) K3 w2 G8 E; F8 } J) a& Z% m* T8 u+ D; w+ X- B4 z
1.缺少APXS会报错
& e4 n' t) w- [- D0 h; b
" o$ K" _0 Z$ {3 d5 Q$ Y! b( j" yconfigure: looking for Apache module support via DSO through APXS8 e) V* B, C+ D" Q% T
configure: error: couldn't find APXS& L/ b3 P9 _8 k1 w3 I0 D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 e1 V/ M- u' s8 w8 D
解决方法:1 W8 G9 m+ o* S5 Y: |7 [
9 x, k/ ]. M$ `1 H; S' y! k2 [$ }yum install httpd-devel8 e4 E$ N& j7 C* E
2.没有pcre
3 Z o$ e3 ^; C
' Z. E, x4 F. J" t) \! S+ xconfigure: *** pcre library not found.
" t7 S+ C: r5 E5 ]/ g5 D) Fconfigure: error: pcre library is required5 [# u9 ~" D0 k; W" M
解决方法:
4 z! X% T* F) v2 a4 @
4 p0 z0 f! q2 t; |3 X- Z- {- H5 nyum install pcre pcre-devel/ L9 S1 j( Y7 {% p* `
3.没有libxml2
8 ?0 t' Y( i3 C1 b3 L2 |& g
4 i+ z0 E8 j: h+ r' w
( l# F8 D. I3 u: o7 Dconfigure: *** xml library not found.
K; V! b1 {1 [1 W. Gconfigure: error: libxml2 is required7 G7 ]+ M3 S$ Z7 w8 x
解决方法:
$ ~4 d+ N% h- Y! z6 n. ~/ Y8 G# j3 @9 M! G0 h
yum install libxml2 libxml2-devel
5 S, q) G3 d+ w6 i4.执行 /opt/tengine/sbin/nginx -m 时有警告
( |6 K! c( [* B) ]: E
' s+ Q- Z8 ?7 b2 F5 J7 E* n9 s4 C6 nTengine version: Tengine/2.1.0 (nginx/1.6.2)
3 w1 G. H6 h# D; @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. r0 m- E7 s6 U( j* N
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 H9 p9 S6 E" v, a3 @7 [# A! E7 P- }
* E. ~0 T+ h' R8 ] i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 h5 H* M. e5 U0 x' x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"$ A9 n5 x/ X% G' X2 @7 t, `$ ?) W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. C# o" _% `6 ~: ]0 z/ V" t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" a2 F- n( Z' Y) n+ I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") \' p' g+ A% @/ q( O8 c/ }
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- ?4 M4 W5 s* H! k, F
解决方法,移除低版本的APR (1.3.9)
2 l, S) m; B" {* V" ^2 Q, {) }5 B( m) S& ]$ x' ]/ {
yum remove apr1 t0 L8 B' j0 e$ p; c8 b
5.Error.log中有: Audit log: Failed to lock global mutex: U( H" U: g& ~2 x
) J( n% ]4 z! \, D* E0 F# w8 G
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 8 W1 N. O: N; B" g9 t* D
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) S7 C: `. o4 S l
解决方法:* z; d: D. B. m" m
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' {. g7 `9 `. S) G5 z. j0 x9 s2 w. d; R. s: c
SecAuditLogDirMode 07773 L4 {) z4 E+ V! g9 k" K' m/ i
SecAuditLogFileMode 0550$ k2 T; i. ~$ x, q9 F" w$ Q
SecAuditLogStorageDir /var/log/modsecurity4 n" L; {8 u$ T( N
SecAuditLogType Concurrent
' ?* F% f3 G+ v参考文章:0 S+ @2 ?7 M* e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 Y$ p7 E2 Y! s' e4 }0 P* c
http://drops.wooyun.org/tips/2614 |
|