|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 {0 h6 b8 ?8 F0 Z; r2 y; U* _) ^: z* i" x0 \( w! K4 b. H9 g/ C; i9 @
一.准备工作
3 z$ i) E6 J Y# ?0 X1 ~
, ?1 \ W( C6 C4 W3 c系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.00 ^5 ], @' g: E) P6 E0 Y) [' d
5 o ]- X5 `% U
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) g- I( h. t: x/ f L# _# n; _8 g
5 H* {8 R% e1 e4 \% s% A) v mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& Y; ^- R6 h# O F% K9 f
( z/ s D+ s2 d9 p! f3 P1 eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* x: ~& z* t* g/ \, H) [# l" B" L. F, C5 D4 O% `
依赖关系:
8 c8 U6 T+ C1 g4 k& E4 h1 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
H1 r {, L. [0 F! N" e8 s5 v# G0 H
5 |, \2 w( O U5 g B+ [0 fyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
( H+ Q5 ?4 i( C* E; zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
6 a: V6 r/ h1 o* ]/ E
- a! b/ s6 P& X6 i. ~* m: Eyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel& d* s( N+ j- Z( U$ I' O
二.启用standalone模块并编译9 B2 b+ @# R+ S( G7 Z9 t/ Q
* n" c/ y$ @ ]1 h) ^! z0 k
下载modsecurity for nginx 解压,进入解压后目录执行:
: @6 O2 `, g9 q$ `2 y. q# N
, R# n+ P0 c9 i9 W" V1 e./autogen.sh
* I; Q: u/ d3 T./configure --enable-standalone-module --disable-mlogc0 L& o6 ^+ z8 @3 F3 R
make + z( c6 j% Y7 x' C
三.nginx添加modsecurity模块
/ y9 J1 Z: F2 A) j9 e$ E/ ?' R' W
3 {1 C: U, ?) {1 V4 _& M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# c$ ^) P( v: F" \
! [6 g: Z9 y) |) `$ A" b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
8 c$ p; e7 A$ W, r7 x3 o- \make && make install! \1 x7 V, }1 k; }: o9 w! t
四.添加规则
* b! @4 M" J8 g" A( z- A3 r8 u% `, |6 r& M
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ M* V; N! n( n5 g- H h/ o- [1 I
* P5 h$ C7 W; ^1.下载OWASP规则:
$ j& ^* i4 _! U6 [2 q; N( _, j$ ^2 N- _# N! d% j
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 _' r* f/ s& t- s! [# {& Z* J
$ A, c) N9 L/ v' B
mv owasp-modsecurity-crs /opt/tengine/conf/" j& h8 V5 i" L3 Z" S
1 R* K: x2 z- {) U b! c# `cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; P+ u9 F- b$ N- f2.启用OWASP规则:
6 r& M, A+ W* H8 B. r# c, ?- E- Q* s- ~: w0 V3 e, j5 P9 e) `; {3 |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& a8 O. }8 J. c) R$ S4 A
7 a, P) \' N! M3 z& b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* o4 i+ r& O* S: }8 C5 U+ M& v. U, V# b; G
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 {+ `$ A0 E. u4 j/ d; K5 E
% [: m4 b2 I6 U6 T. Z& `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; J! \: K& _* L5 I$ l4 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# G9 n. {1 F: H) Q9 U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 c- F h+ w' x" I" K" JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) W: z* _, m) G" M( M3 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 R, z. q! `) S9 U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ j. }2 v* b3 yInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* P# O) [9 J. V- S五.配置nginx( d" W1 W( Q9 c% k" m. \
5 e5 z( S' h) R, L( I在需要启用modsecurity的主机的location下面加入下面两行即可:. d6 k; h- Q6 G. l1 V- _% M; M
/ o, {$ k# C/ S9 p6 y2 ?
ModSecurityEnabled on; 8 P- G# w. F6 m: E7 }3 ~* @( {
ModSecurityConfig modsecurity.conf;. H" F0 n- N& J# l# _ A' c( ~
下面是两个示例配置,php虚拟主机:' U) Y; Y& H& D! P' m
; H, j4 A1 R6 y
server {- |3 `0 U7 P( f6 \$ }
listen 80;* }, Y" |( a" B* z7 r, Z, o
server_name 52os.net www.52os.net;. C+ \ ^/ g4 O; t
5 |7 E$ U5 r6 _5 K# {/ I- ^/ r4 {) z
location ~ \.php$ {5 f0 m. h- l: ~
ModSecurityEnabled on;
) ^1 N: v3 ~9 A) D5 W Z ModSecurityConfig modsecurity.conf;8 z/ j, v. S% S% H+ P2 e
0 L% ?3 F, S( X4 g
root /web/wordpress;
% ?4 G4 j; w0 C" ?# o index index.php index.html index.htm;# o6 P9 Z# O7 U
3 } z& k. T& o/ A. C: u L fastcgi_pass 127.0.0.1:9000;, N; M) T+ }) U
fastcgi_index index.php;4 d9 k/ E$ e7 P# i/ q
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
6 t. O8 P- l& l! J* w include fastcgi_params;
\- ^& {9 P! }* T }; E8 O& H* Z _ \; c5 e. R ]
}
+ F, {) C9 M( b% S1 s; M/ ^0 xupstream负载均衡:& @8 t) q: e& }' C$ K5 w: J9 n& z
6 M/ R: _+ E0 T1 Mupstream 52os.net {1 i, J9 g$ a9 j% y/ L
server 192.168.1.100:8080;
2 s& Q; Y/ M! p3 Z server 192.168.1.101:8080 backup;
* P7 l& N1 V& l+ v/ o}1 r- {% W) w+ |
) f3 H* E: }+ ]1 nserver {' p! U3 V5 o! Q! [+ @
listen 80;* A- ?0 j& a$ ^0 m7 I+ s
server_name 52os.net www.52os.net;5 Y, ]9 o1 `4 I: I' M5 K; S
$ z. N5 z1 V( P2 }$ @, I- F' K! Plocation / {
* ]1 M! z* r3 K ModSecurityEnabled on; $ {! X4 C: W9 w8 {' k
ModSecurityConfig modsecurity.conf;
5 A9 s& \+ U' E3 B( U* Y" a' k. w, a* @9 E- b
proxy_pass http://online;3 p3 T* t/ ~+ n, F1 v$ M N* k( K
proxy_redirect off;' A! ?" ]( y: Q, O. u! {5 R2 } q
proxy_set_header Host $host;) ?3 b& k" Q0 k* L1 K( c
proxy_set_header X-Real-IP $remote_addr;
2 O# z' k% r: @+ B+ U8 t/ W proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 A" Q" Q5 M0 t; {$ l3 u9 o0 c
}
, S3 }% ~) V, C5 I+ [& T}
r. Y* n" k! {$ P- v+ E六.测试
: X% T# b4 b4 Q+ U1 u; m P% w! ?8 o, x2 e7 N7 B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ v4 D( q& o g
, g$ H- Y5 T n. P6 P<?php
- p' U; w9 y' R7 ~& v phpinfo(); : Z( |+ J/ B/ i. q) p. h7 }4 J0 E
?>
/ G: m9 G" i% A' b- D在浏览器中访问:
( R6 Z* S$ t B$ n* @2 Y1 i9 r7 t8 d' U" n& Q: ^: K( b, _
http://www.52os.net/phpinfo.php?id=1 正常显示。
& i" d* N; y+ ~5 U. {http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
( L8 ?# j0 w, I7 f4 c: C5 O. E; Dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。& ~ ~. s# W$ m' m
说明sql注入和xss已经被过滤了 Z$ v9 r! a0 U2 r8 s, r* b
4 h4 t4 X2 L# Y4 q* R+ @1 Y七、安装过程中排错2 i3 G) Y+ E. t
$ X% O0 v$ i4 d+ ]- _3 P. ^2 t6 M! `1.缺少APXS会报错
" y8 k; V9 a2 l' x( T/ G- j# g5 f" W7 x, e
configure: looking for Apache module support via DSO through APXS( i; I* g. Y+ q# F+ W4 Y+ w
configure: error: couldn't find APXS
3 e% V# B# u4 c! papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& K% Z3 M) _- H5 d H- E: m解决方法:( {2 u1 j! P& @! c/ F
3 A% L# [3 M/ A" D/ W% r9 O( ]
yum install httpd-devel( A1 f! F& }/ e
2.没有pcre, x E4 }" n: f1 ^/ r: U
: v% }; q% T& U
configure: *** pcre library not found.
2 p ^" }$ K% Xconfigure: error: pcre library is required1 u; w1 J$ @4 `" c# G/ R
解决方法:
- ?3 G9 V1 Q6 c$ y% n- P7 n2 |' ~1 a! o3 }& f3 f# E1 N P
yum install pcre pcre-devel9 M, n8 \$ N4 T
3.没有libxml2+ t1 q+ L8 G& E$ y# R
% P( S* V, @4 m& ~1 f. `1 c& _7 N/ P P) l7 i6 q5 Z: U
configure: *** xml library not found.
; E6 g7 ^8 }$ H- W# j: W5 qconfigure: error: libxml2 is required, g B% \" k$ C. ?
解决方法:
1 Z1 q9 T! L7 ^1 _
4 Z4 a, U2 _3 m. T3 r4 Lyum install libxml2 libxml2-devel
& V" H( U7 J1 k9 h8 d0 u4 K+ C3 a, r4.执行 /opt/tengine/sbin/nginx -m 时有警告- d+ t' n7 U" p% M2 t
1 B0 C% H; q' B' p8 `8 R
Tengine version: Tengine/2.1.0 (nginx/1.6.2)! Q; h2 t- D# D+ ]; V9 N3 A/ q4 C
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; q1 Y4 P& U Y ~- u! N原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' O4 j% m- H# h: E7 |7 U7 x
, h, p$ ?" h: T8 I& F' Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# D# Q/ d5 a% g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"7 }; O' ?- D' i: b; E5 ?* [
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ s1 e& _2 C, _3 @0 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") `& B7 W8 ^, ]7 {6 L3 G) ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! L! W+ p6 h. C# N* g- X) E8 r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' I3 ?7 y) _1 k& @* v$ F G
解决方法,移除低版本的APR (1.3.9)
; Q' y8 @6 Z& ?) H( ]" K0 W3 \! M( D: Y7 w$ d& i+ t3 K4 ~# u* D F
yum remove apr
+ A s& N$ r' @# Q5.Error.log中有: Audit log: Failed to lock global mutex
- w# }3 F. \& U9 |' a6 D, h' Y( p! m D, x# `0 Y7 j% f6 I. A% |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - k% }( o, n' l5 w# j( ~
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" N4 l* s' x, p1 U, x7 X, d
解决方法:* i+ o! d E0 T' v1 X5 h
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ k, H4 a4 }) y& x# ?$ ]6 x
# k& f" l3 ?) r7 x# c4 L' @- t, cSecAuditLogDirMode 0777
0 f' v; C3 b s& ~0 uSecAuditLogFileMode 05500 P8 O- g0 u$ J. l I. ~
SecAuditLogStorageDir /var/log/modsecurity
4 L8 _2 K3 C: e! K" j% U7 ~- SSecAuditLogType Concurrent
( H& c% f, b5 z. k3 ?8 \参考文章:% `" w6 _+ |& u' s. J6 W8 g0 t8 k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 z9 W( \* v0 D u# R4 Nhttp://drops.wooyun.org/tips/2614 |
|