|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' _6 _0 P8 m* Z4 H5 J* W
4 k, o) u1 X8 l- U1 h" H* I. }一.准备工作
; u2 Z" L" h5 D6 d" n
3 I, P( x: `+ Q V3 I3 G# q- A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( b0 _9 _8 V" d1 f+ y& [4 n( h! `2 M& l7 A: H8 {6 B3 H: @3 _
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 M9 a/ C3 m+ y0 l5 [1 P7 c3 b
# _2 n+ r6 H: [6 \# v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 {! r& P/ S6 h- z; C/ `" D0 T5 k
; H+ g7 ^- G8 X# B- A7 R' vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 X4 G% l* @3 C. l3 V
A& K& f' `# C2 L t0 M; B# d% t依赖关系:
" B" v& |* I* ^4 M; T6 ?3 Q" V7 }tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 Y' E/ r ], i3 h) r; c
2 @5 x% d6 E K$ T1 J! b; a: s
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 m2 P2 {. a- S$ p0 m! l7 Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
# u. l$ C$ J w2 g, A8 O
: `4 h7 ^# b! R4 `yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel- }- H$ s3 I: i& `
二.启用standalone模块并编译5 K3 {5 S( {( d8 O9 J" y6 L, z
* i9 u! g O* T+ n下载modsecurity for nginx 解压,进入解压后目录执行:) S( O0 E8 {$ [& @( c2 T
' P h7 y e" S./autogen.sh4 I: k, Y2 T! I. D6 ~5 @
./configure --enable-standalone-module --disable-mlogc
9 \, ^6 W. | Smake
+ a4 l3 P( n$ W x* {三.nginx添加modsecurity模块6 |' T$ Y# U5 p
' S2 m6 \+ c9 @7 V, x6 b0 ?
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; }* o3 K) H# p. N! q
' L! Y @. R; r- F8 y V% |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" r2 e- ~$ c* t, m
make && make install" | L' v2 J# ^- V5 O5 n0 \! ^5 I
四.添加规则
. J5 l+ ?4 [0 y, T. [/ v! e4 N& D$ G5 ^) K) Y& I) _* p, Q* d* B. o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 U3 `( \' f' v w! s P/ ?
8 j- W' B' M- L; f' n# ` a1.下载OWASP规则:9 T5 `* n+ Z5 o- m4 X* @( Q
9 |, j3 A! s6 }9 {& n( Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ `% `* Q% P) i) R$ P- K
# P9 a! w' b; }mv owasp-modsecurity-crs /opt/tengine/conf/
1 a# t- R/ U$ T6 c# F
+ u+ v6 l1 Y. ]8 xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# l, h1 I; t, f- W$ M" o8 P- O; m2.启用OWASP规则:, ~) x& g( _6 H2 r( z
7 d( g. V) a; N# y2 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- Y& h8 d6 V$ T z3 |0 P5 `5 g
+ f1 g+ C$ n) S9 d( k' L1 K编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ _/ y0 Y" }( C D' p
: \- }1 }6 ^; l) g( P9 G
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. ^/ n1 ^# g" O2 n/ n) x! }
- J r! k3 e& @$ l" P& ~) bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& ^4 x0 J9 W' @2 `# ?8 n9 gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 n b0 V( I: _% v& Z4 s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
o7 b+ H. l Y) f9 D- W7 pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( B$ p4 t# X" m' N F' K; }' X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# m: V; K; f8 y% ~0 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 q1 Q: z; f j
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 ^: }. D; a7 s' H% T8 d) [ o3 j- u
五.配置nginx' p- j& {3 C$ ~' f8 h
' b; e$ v- [& F% I在需要启用modsecurity的主机的location下面加入下面两行即可:
+ }2 T* N. {2 q: \0 s! `$ |" \
1 G1 f- y T' ], ?5 rModSecurityEnabled on; : R+ b' W9 o/ l+ j& w9 P
ModSecurityConfig modsecurity.conf;
@& k& V# S" t* K/ M0 q下面是两个示例配置,php虚拟主机:5 S! a7 J- p! P
! E3 ]) ~; j% X. r% Wserver {5 ]+ I0 }' r9 V! t+ B/ S- S
listen 80;% {" k6 \* q1 \
server_name 52os.net www.52os.net;
8 i6 v1 w, C {0 e5 s" h# o 0 y! l# a. L) G' k' P3 H1 l3 V
location ~ \.php$ {# r5 Z( i/ b& }
ModSecurityEnabled on; ( [7 B' ]% d! k' E( s7 q) j
ModSecurityConfig modsecurity.conf;
6 E9 p K8 z; \4 I. R
3 p2 [5 E* N/ _: X: ?7 t {4 V3 J root /web/wordpress;* v3 h. A: A# L0 J5 }& s
index index.php index.html index.htm;
% Y: L2 R3 L* v4 l, o7 u
$ l# L5 Z' f: Q- H fastcgi_pass 127.0.0.1:9000;' B$ E7 X" V# }3 J- t+ p7 W
fastcgi_index index.php;
- C1 V" Q$ g" L. B m7 s* |7 Q: X fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. s# C" w4 {/ g include fastcgi_params;, o. Z P. n+ o; }. x0 M- S& M
}
( d# E8 L2 K* {& Z }
+ X5 s8 N/ V# z5 O/ c5 y* bupstream负载均衡:& ]6 }9 L! z" ^, t4 m5 V
& ]- _, n( F* Cupstream 52os.net {% u% `4 f0 F3 a+ [, F' H
server 192.168.1.100:8080;' m5 o k! P) O/ G* X( K
server 192.168.1.101:8080 backup;
$ w: ]# O3 S7 x2 |% w4 Z4 A5 Q}
4 d4 Z, O3 r, w& E
" L9 R1 Q0 }+ |, Z+ r1 B( B6 ?server {3 S3 ^* R' Z; ?8 D; g; ]
listen 80;/ \2 o# z6 U9 a- n
server_name 52os.net www.52os.net;
5 D# H5 L2 b$ g# _
# d' p. A2 y n; e2 _location / {
1 L0 G( d/ p( ` z$ P5 f5 O1 ^0 e ModSecurityEnabled on;
* w; J( x, o. c% K" t ModSecurityConfig modsecurity.conf;
! o3 h2 W* u3 x; X4 [$ r( u
& s: e( G) d8 z0 p) h1 S; _ proxy_pass http://online;
) ]0 W0 W3 j0 K! S1 E6 @1 N0 ]8 p proxy_redirect off;
, E( s0 n4 w% c proxy_set_header Host $host;% d) a+ `! e0 {" r* V& W2 y
proxy_set_header X-Real-IP $remote_addr; H6 E3 u6 k/ P
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. m: E; l9 T$ C5 c# y1 b }2 e) I: q# a. S
}: N0 G# R. V( V! g
六.测试
* M0 Z4 d9 X# X+ [, r7 M* ?) u0 y N' z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 m/ h* R* u o6 J" F# Z) L9 {) O& n' \0 |4 t6 T
<?php
, V) Z& \) s6 M( {3 A phpinfo(); 4 s. ^+ l# z1 B
?>6 a% Y [2 V% |2 J
在浏览器中访问:
4 _/ g7 z' [2 S( {7 p: x8 ^# L' |# K$ f7 K0 A
http://www.52os.net/phpinfo.php?id=1 正常显示。/ y! D6 i9 [( V) s! u
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: W3 {1 B% H" c8 J0 Ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, V- E9 k, T2 ]1 } n
说明sql注入和xss已经被过滤了/ Q! E- \7 O$ i# P( P' o3 Y4 w
: L& J( d' z1 ]" P) z* w4 q' {
七、安装过程中排错
/ [4 w( ?7 J, L( ^; d% q& P* ?9 l4 L( l. K
1.缺少APXS会报错# m/ S2 N, [! p: M" s
! x9 [+ o) e1 Y
configure: looking for Apache module support via DSO through APXS n6 T$ f. t+ s. k, r
configure: error: couldn't find APXS# ~0 y5 r$ I, c( ?4 l4 j/ a
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ V+ m7 K+ I+ t8 T2 t3 g& A6 I解决方法:4 }: f& A3 N+ F+ K4 {9 | W( R
+ v& [0 O0 H, e6 ^5 G
yum install httpd-devel
) G& U0 V/ n; j2 } D: z& E2.没有pcre8 O0 j& T5 ~- ~3 O
2 p8 m9 T7 _8 ~' W+ V
configure: *** pcre library not found.$ t q* L3 F5 W! c( _& z: a& s
configure: error: pcre library is required
$ Z7 P/ i# z$ f$ b+ o! k6 S解决方法:
# U# P( g C( `$ b$ `8 x, Y: I8 B- p4 S1 X3 m/ D8 S
yum install pcre pcre-devel* @5 e6 y0 k S& `& d* p4 M) ^1 M. k
3.没有libxml2* J! C* P2 E0 X# E% T+ t0 \
% ]1 o- Z! ?# w K
F! @& @8 O# j4 [
configure: *** xml library not found.4 e* z# k+ v: Y- d$ D( F
configure: error: libxml2 is required
Y% @0 [2 l1 a4 ]3 f解决方法:
) S4 m4 }; _0 B+ [# w
3 V4 x( W6 W( G/ z. N( |3 b4 E, Myum install libxml2 libxml2-devel
2 A6 O3 `# X5 T2 E9 l4 J* j' C7 i4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 ?: D! N* {" C& [/ v
# ^$ g% O( d! v7 bTengine version: Tengine/2.1.0 (nginx/1.6.2)
0 F$ `/ \! d( ^nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; J5 }) `4 f- P5 l' h0 M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 t# n! o" h) P0 j
) o* R. s# \# ?. j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ b! R2 f' o3 i G( u6 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
3 N/ ^$ }. D5 Q$ r& k" M, ^2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% X5 p( C! B8 ^/ L- j" M S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! m M6 k, e4 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& _! d% ?8 o' m4 x: V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
Y) p8 K; ], z; G9 a& G解决方法,移除低版本的APR (1.3.9)
6 o% f7 J; p2 h- f& I- x& }* ?' w& G4 u8 m
yum remove apr
1 E. a5 M) h" I9 a7 Z8 s3 `8 @& {5.Error.log中有: Audit log: Failed to lock global mutex
) _6 F8 o& Z8 q: Q: O$ ]" S* m0 y( O3 \% ~$ H+ D8 b+ @& s
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) P2 r# S* Y' `) G9 |+ eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# ^2 c) t0 R5 v, H; B4 o$ S- ]" x
解决方法:, b+ b0 ]9 ~% W6 Q1 @ u. R" L! q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 h1 U. g! l6 l2 B% G& H' k: y0 X' L4 J3 R+ H. @- ]2 w8 m3 j
SecAuditLogDirMode 0777
" ]& j* |8 p1 A) n' GSecAuditLogFileMode 0550
" C$ {: D/ _/ ^" c4 p/ m( V" cSecAuditLogStorageDir /var/log/modsecurity
% b0 Q Z# g7 B# `SecAuditLogType Concurrent
* r5 L% i1 B, e0 |0 R; V参考文章:) m! H# U+ }' L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! \+ S9 D# V2 k3 c+ @# D) m' C
http://drops.wooyun.org/tips/2614 |
|