|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。2 ?6 e( k9 A! a' ^
' q( p/ u% s! U/ h7 L1 e一.准备工作
# u3 P1 C8 @9 ]9 B S
1 ?% A7 [4 e6 D. r& Z, [8 A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 Z7 Y! |# I# y- ^6 }8 b) p! O
# e' }8 \- p( a3 y8 a) `% Q9 H% {tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz }; r0 ^! x) z6 {
- [8 H9 J, G8 E o6 H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 B$ f3 e$ ]# z+ a9 X+ W% V
' a- O. H) k( ]3 K* [4 AOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 D2 O& z. _) d, ^: |8 T
! F4 h i) P$ Z3 G依赖关系:
2 S( b/ R) H, P' Ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 g% e1 k `" o$ g- k0 `
! D% w6 `6 j5 v! X) n! \yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% @8 Y+ t# a$ J& qmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( |# r9 O6 l/ S& v( U4 Z# x& D2 S/ W$ }& i4 L! g$ c6 z" t
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ c1 A3 Y6 Q; c s- F& O二.启用standalone模块并编译
* s0 \" X: h0 v7 T a! W% Y" G. X G O7 t& H
下载modsecurity for nginx 解压,进入解压后目录执行:
6 x% v; J* E( I* z, p
3 z1 c+ t% S* M./autogen.sh J# O" s1 E# C. Z. J
./configure --enable-standalone-module --disable-mlogc2 r/ ^# m4 N+ r+ \& V% O
make F: }5 y. s: d# A; P1 a
三.nginx添加modsecurity模块2 O7 O9 u8 p8 l' u
$ f' e' [( V3 D7 P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, g% v& ]/ h7 u) o+ |" X
3 L% P G3 g) q, x4 F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 x) M! w/ n( @% q8 r% gmake && make install. I4 T2 k; h6 {7 H5 r
四.添加规则
5 j4 S v) K" Z) P' R; I
+ J" ~3 a; `$ f' M6 R5 Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# s* I& z9 ] z6 N
/ I7 e- f, |& ]7 K6 L. |
1.下载OWASP规则:
3 D9 o% k- Y7 U' h m! \
; L2 K! M( P$ L# b+ ygit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 U/ u+ G0 G: r# i/ ~) A' P
7 E! K) o. P/ A6 k( i( x- D
mv owasp-modsecurity-crs /opt/tengine/conf/
* U0 O2 w" f* Q0 T: W, U3 o: D; B8 S( \- y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 O% @: _# V2 U* t% w% }+ j, W- E7 }2.启用OWASP规则:; x0 B3 _/ p8 f, [% A! d% a
; n9 `" Y1 c$ @6 r; m9 H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; X& ?4 ^4 q3 Y; E! v
9 b* e) ^( w% K0 U& [/ L
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on f- T* i2 j& A: T- w
/ I3 F$ s/ x& P! [( Z6 D1 Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 S; v7 s7 j; w
- Y/ E/ M+ @; @- v2 n, s9 u CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: @. V5 d0 B: D0 n( ~& Y5 }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ G6 L% \7 S, H& n' k. q' |- eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. }! F( N: w* H: i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, q. r1 w8 f, [- y9 n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 ?( z- i8 w, L% q" G DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) j! Z3 ~+ s1 R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 }3 y8 ?) k- r8 X9 O五.配置nginx3 |, _0 _7 \8 m# L5 [& C3 B7 k/ I6 e% W
4 C/ J3 e0 W( ^$ U0 h, E在需要启用modsecurity的主机的location下面加入下面两行即可:" Q$ V- q- P5 q8 \3 M/ q( G# M
8 O. t) Q" ^# b$ _- X; R( a
ModSecurityEnabled on; 8 f: m( C; `: t! R8 S" F
ModSecurityConfig modsecurity.conf;' F/ u. v% ^; [/ w
下面是两个示例配置,php虚拟主机:
' w; {9 ]; V& A/ ?( R+ x: L0 q2 S# M5 I
server {9 h1 f* K/ [8 n
listen 80;
8 |* s% n) a: G3 \/ G server_name 52os.net www.52os.net;
! Z7 P& V* D; r- k* E3 C U G2 |. Q, X* o, \
location ~ \.php$ {
6 F1 y4 x& b2 c8 |. G/ S ModSecurityEnabled on; . `/ W( s$ B- C& n$ ~# ]3 E
ModSecurityConfig modsecurity.conf;2 o2 Z/ Z2 J: @
" u( J* k* O, Z2 H9 j# x8 d
root /web/wordpress;7 H" e. T5 s! D7 Y$ ]" c0 V
index index.php index.html index.htm;. ]% i+ S" L9 ~) G
8 U4 q4 N2 Z3 a( A7 A3 @
fastcgi_pass 127.0.0.1:9000;
_& e# e% n% d2 g: A! C fastcgi_index index.php;/ R2 u/ y+ i( O8 ~; g. L8 x Q$ k
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
4 ^# X9 H5 O8 Y) V1 ~ include fastcgi_params;
3 o3 \3 r/ W( T8 L, {8 J! ? }
& h+ I9 O/ X/ d! l3 C$ r }
* r. Q0 A8 F2 u6 f' bupstream负载均衡:
% } a8 R+ o h3 r; ]9 l; |
9 {. f" I& s! w, n- tupstream 52os.net {- c- T# u. r% M- O |6 F/ q% I U
server 192.168.1.100:8080;* T2 d8 i( x9 K+ F
server 192.168.1.101:8080 backup;! m# O M. j# u5 |- g8 |
}
' h4 |; l0 u! u! b; }6 w
. G3 w9 N9 Z' ~4 Y$ Aserver {- |/ R+ P/ Y7 W
listen 80;
) T3 u7 E/ d# ?7 E% [% |server_name 52os.net www.52os.net;6 d7 C$ }7 u( R( J; T4 w/ i
2 @7 A Y. O) q0 x% }8 v& tlocation / {
& K$ B6 J. v* F# | ModSecurityEnabled on; 8 _# T! ]% |. F' ]3 {
ModSecurityConfig modsecurity.conf; K6 Y0 U# C5 ^ r; a
+ B1 ]" Y% z5 B/ m3 \ proxy_pass http://online;- T0 L8 T+ I% D& ~: m9 D+ R3 Z% P
proxy_redirect off;
6 {8 h. S1 Y4 L% y- P% w; ~0 o6 S" ` proxy_set_header Host $host;* I) Q) K4 i0 k+ ]! s# ~: W+ V
proxy_set_header X-Real-IP $remote_addr;9 ^# ~2 _; K t D, f
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. c. v: t: ^& G }& `8 }- ]( A9 Z, O1 [% x1 y
}4 _3 z$ ~# I0 w4 Q! R
六.测试
* q& f' V8 p& w9 z! l2 j( m
' A9 [6 R9 _$ O3 P! G) v) F# e" F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:4 Q! ^$ u- Y) V0 q
6 Q3 C7 \: j. d. V. y" e0 M<?php
/ n1 f i. E" l- `4 |4 O4 n phpinfo(); ( o- `3 K2 g( M9 k S, {: b
?>( N/ y3 I1 E- {6 e& z2 ^2 W5 _
在浏览器中访问:" F) V, Q6 W2 Z2 X2 s; L
# I3 u' ~# K! x N7 P6 f5 U
http://www.52os.net/phpinfo.php?id=1 正常显示。) {" j. I- d& D) a ~9 s' X
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# `5 s- V$ P+ a* D5 G/ ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。/ c6 n+ {+ s9 C7 Z" z3 \
说明sql注入和xss已经被过滤了9 B" \# E9 A( O
, j' c+ J7 t; I9 x! e7 l/ x/ w七、安装过程中排错
z+ o, T/ d# R* o4 O
% @ F4 Y/ r8 E9 ~! h- u8 ]1.缺少APXS会报错: S% |9 }/ v; v3 I& O, G" A
1 ? D/ |4 ^0 P6 |: ?, \1 t. O+ ^0 yconfigure: looking for Apache module support via DSO through APXS
4 l( x b6 V( J8 Y1 _$ d( g/ _configure: error: couldn't find APXS8 D/ q# [7 J1 ?) b! I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 ? `( z% u, X解决方法:
8 J5 Z) K' ]# d2 |' p: \# V
. n3 ^% f( c$ u0 T3 K7 b, Nyum install httpd-devel
: K6 S8 L( e; o! Q+ I0 e2.没有pcre, K- e, d% F t j/ X( l' ~
! I9 i) J" Z$ L1 B- }7 P( i% s$ }9 Vconfigure: *** pcre library not found.
w* x' l/ }4 S, |# }+ j5 Dconfigure: error: pcre library is required" z5 }/ _* @- ^& h# H( S+ v
解决方法:; ]6 q6 ]! C" ^; a4 A
+ y5 i; q X4 C5 eyum install pcre pcre-devel: h/ K2 V* ^6 m; n
3.没有libxml2# e5 R5 L k# ^
2 t* q) P1 f) ~" }. q% d2 i1 l2 _8 [, V
configure: *** xml library not found.
6 e5 v$ E5 V# R; [/ J2 B$ {/ |! Uconfigure: error: libxml2 is required8 p N8 a" k) c& ~# @$ H$ I
解决方法:
9 t7 S; p. r- N' Y7 t- K- Y, c( y( }, D% ~* m
yum install libxml2 libxml2-devel
, e' {* E& F/ z; v8 P" u% b4.执行 /opt/tengine/sbin/nginx -m 时有警告, q C F; {+ @
, V* |# b$ Q; N3 a* m7 a/ P# B6 R( X
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, Z7 l4 }' P6 D2 A* T+ v' `nginx: [warn] ModSecurity: Loaded APR do not match with compiled! D) q7 q3 Z# C! M8 Y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ d/ |( t' o8 M8 ]: V! f& y9 ~) V5 y! }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: D0 B3 V1 ~# p/ e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) J: m" q2 a% b8 R, f9 C: A2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 K! M0 ~+ c; z( R8 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 P7 M5 @: o9 i0 z" Q: h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 R" a: c% Q- t# z- L) [6 P1 D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. W g7 s7 ~8 y5 b/ a( f$ f解决方法,移除低版本的APR (1.3.9)
/ R( [$ L0 C- V9 ?2 `: t* G: E$ a( N3 l5 f
yum remove apr. |( i5 r4 X, F6 [: S+ h
5.Error.log中有: Audit log: Failed to lock global mutex. D `2 w I2 ^
+ D2 `) @; D7 r5 k
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
( U& R' p% u4 p: v7 x" J5 O- |. \4 lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) M$ H* w" T1 M& [解决方法:9 j5 t9 C5 H4 L! ]# b. U/ w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( j# c9 S1 \% K# O; x" f* b6 I/ R2 L2 L* \
SecAuditLogDirMode 07774 h' S0 J" @2 }. ~2 T, Y, c2 r4 F$ d
SecAuditLogFileMode 05509 L9 Q: c6 c* L1 m! X6 M5 m
SecAuditLogStorageDir /var/log/modsecurity
5 n! ?+ C, g" H1 t8 lSecAuditLogType Concurrent9 n& c _/ F; H# }& t
参考文章:
?9 n! z" I" \5 Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& I, K1 A' y; Y, B3 H5 @" W/ ?: r+ Mhttp://drops.wooyun.org/tips/2614 |
|