|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。$ s' v) M. T8 x+ q) C$ A/ S& W
9 J! B, A5 p0 S6 b2 m3 x: e7 A% O一.准备工作
+ F5 w- @/ {( [$ x3 w6 Z8 o9 r8 x8 C* ^' q. j
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 k' y6 f: c" F/ G
) C; X3 A! w7 m# [. e, E0 d
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" }1 F* E# S9 n% [2 ?0 a' K" A: V
c0 |/ ^9 U. smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- Y8 A0 Y: L0 I/ E- u
1 @3 ^$ ^& T$ d1 K( O
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) x3 P: n% y3 m3 H+ A8 ^8 V1 T- D: L( ?
依赖关系:) J" Q7 j/ z% V( i5 N6 Y/ ]/ D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ }/ W% L& | _ d
5 R9 p2 k8 s+ Z; Iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
]" V7 A0 [* }$ g* H6 amodsecurty依赖的包:pcre httpd-devel libxml2 apr) q( g0 @; i2 a3 x
7 W/ T6 s2 x9 z0 i: ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ ^) j H: Y+ f0 v1 T8 M二.启用standalone模块并编译1 n( |7 O* x9 U) n; c. J/ @
) O0 L f5 |+ P( X
下载modsecurity for nginx 解压,进入解压后目录执行:
5 m2 r6 l4 }% W2 w0 N4 @
/ k5 X+ A; T! }0 R./autogen.sh
5 C: J, q4 S5 o; j/ |4 y ?4 b./configure --enable-standalone-module --disable-mlogc' w- u" ?* f. O4 a8 Y
make
5 }; F4 p* k. e+ o% c三.nginx添加modsecurity模块! V$ S, T; z5 W0 o) a/ P
) F- v9 a$ q- |. ?$ ^% Q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# E" R( @2 q& J& r& r+ D3 g5 N$ u5 K2 C
9 }/ J J+ f" f/ p./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- W+ i7 b" m- }3 pmake && make install9 w, m/ r, \$ O5 P
四.添加规则
% I3 ^9 U, r e9 p5 N" K, ~$ S7 t' t5 _: `- L0 i. M) B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! G B8 J0 y2 z8 ~4 ~ W( C- B' G' A9 c% u: D7 a& K
1.下载OWASP规则:, b6 \- f0 Q A
( c& S+ N) W# @9 G* E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, _! O# t- w) X Z/ {8 h/ a1 Z# k/ x4 A( b) k, R5 `7 I# I" q
mv owasp-modsecurity-crs /opt/tengine/conf/
8 e7 ?' E; t3 T) A6 U& i
: o/ k9 w! V" i$ m" o) wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 }7 E, Z/ l4 |. W
2.启用OWASP规则:* E& }1 B$ e# P5 Y3 V
4 m. L1 [9 [- D/ l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. y7 b4 R8 R* I+ o- N: [3 d
2 c6 c8 ~$ E- d% P$ L1 ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on X' K5 X% f) p } m/ B( E# ~' f% P
. i9 Q: r' K t8 s% Q- U( aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ I$ X1 n _! u( U/ x6 L+ ^
& z+ R5 w- l Z4 m; u2 cInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 o4 ?/ ?4 d9 s. |% J3 UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% j8 b* C- s; s5 p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% N" B, B' w1 P! p- TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# U$ u$ ~( z8 q2 xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 A: y. F; ~& b- Y0 B9 s+ ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: K6 P+ ? `; A4 K. R5 l9 CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, M: A; c5 J% o9 I- h五.配置nginx: Q. L0 b$ L6 Z+ e
# F% f( C! p5 K" [& G
在需要启用modsecurity的主机的location下面加入下面两行即可:5 u% J0 o4 F6 U
4 a7 e) S; V9 D0 B. n4 Z6 \" s3 T
ModSecurityEnabled on; ) A' k! ~! j( K3 d: X% r5 b
ModSecurityConfig modsecurity.conf;
# M5 U7 \% C+ t* D# a下面是两个示例配置,php虚拟主机: `7 D6 e. J+ M1 [3 {! u
6 `4 l# z( Q, H& F0 k( r
server {3 ^8 Z& _$ W1 e3 Y9 g' \1 B1 E; E
listen 80;' i3 A+ t7 T% n( ?" c7 ]: w r( ]
server_name 52os.net www.52os.net;
% _9 F, C, m6 I" ?6 C% Y. ~/ ~
( m: A4 ], ]! P$ t0 V location ~ \.php$ {; e* O- W/ p5 v, U
ModSecurityEnabled on; " Y" t9 a+ H1 ?
ModSecurityConfig modsecurity.conf;. m( F/ K' E/ g
: z# ~4 S% y. }2 h
root /web/wordpress;* l; `* k0 N' _
index index.php index.html index.htm;3 G5 S" [1 | }4 v
- r) v# @+ A' D4 P' j s2 f/ {
fastcgi_pass 127.0.0.1:9000;4 L7 d: G) ]( o( u5 ]7 D
fastcgi_index index.php;
3 E; C7 i# J: G) t; q) u7 j& H fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
! S! L* G8 i0 x7 m7 V5 v9 Y include fastcgi_params;
- M; a2 n" J5 t1 x i }& ?1 v* ~) u# u/ b5 M6 L: F
}7 j; `0 A# t& @& O6 I7 |
upstream负载均衡:
5 c+ b& P3 g1 X+ B9 O
2 {7 |& j5 w: T, ^ t" Nupstream 52os.net {/ V& }- T) U! ?0 P
server 192.168.1.100:8080;; ?& c7 S' \& a
server 192.168.1.101:8080 backup;" V4 C8 q5 W, u+ u; H/ M& d: J
}
! d0 @4 d7 u* Q1 m% o) ?" j5 |' R& f
server {9 ^( t( Z" c. d; h) m7 N
listen 80;
: ] b, w/ ^! k9 tserver_name 52os.net www.52os.net;3 V. Z& y) G4 W5 M# H! |
7 Y3 E; b; c- k8 y9 llocation / {
- X" [/ F% V. l) { ModSecurityEnabled on; ' L. A \9 p0 r9 i0 q+ Y
ModSecurityConfig modsecurity.conf; m1 n0 Y2 m: \+ a- @! R
" C! M0 v: B0 L5 p
proxy_pass http://online;" |6 o' _) A4 `5 A8 U0 f
proxy_redirect off;0 Z+ X0 Y% W6 U" e2 ^
proxy_set_header Host $host;( _$ j9 n/ q# {4 W6 u! X
proxy_set_header X-Real-IP $remote_addr;
1 _2 X7 }) H+ ]4 { l proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- o! I1 B* e) J2 T6 X }
4 I5 j4 ?0 L, k) h/ K& y} R; W7 d. c' s
六.测试0 z. j7 l. z2 Z3 ^6 I: E6 `
3 A: o. `% @8 C* y: B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 W v& K) k& ^' c0 ^0 _8 g. R* b Z" S& e' p
<?php
& ~5 c, B, p9 L+ p. P: I phpinfo(); , h# \# D9 o% h9 K* P2 H) k+ I0 ~
?>! R' k( y* g9 C
在浏览器中访问:/ w4 H6 A9 E' @% D4 y7 f
# h3 ? }7 E: w; o$ r6 O
http://www.52os.net/phpinfo.php?id=1 正常显示。
! o/ h% v/ y, y' ]# r% v% Fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。0 h& a/ P% |+ z& F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 I' U. w: w' }5 e6 S! }
说明sql注入和xss已经被过滤了
6 A6 e( J7 H: V% w5 z! N }) E: c6 E4 t" V+ ?4 u# E# q
七、安装过程中排错
/ B: J. @$ Q% V) i+ @& n6 m0 p% y
1.缺少APXS会报错
; L9 z& V6 P- {7 Y2 a8 K( A M6 u2 I1 X2 b' \" S
configure: looking for Apache module support via DSO through APXS3 e8 e, K9 x2 x
configure: error: couldn't find APXS
% y) @: F5 t9 c' l: c* ]apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 o# D' D; S+ O5 X
解决方法:! t- P5 A, v: [4 i: c
/ \) B7 M% N. D
yum install httpd-devel
& V4 F! u6 D+ Q* O# g# O2.没有pcre
" J1 f1 ?$ M2 g; j( [. K9 K
: h/ m5 T0 w) Y8 I& ?& O q( zconfigure: *** pcre library not found.; M& [4 I: p9 L) L3 _ @3 t
configure: error: pcre library is required1 u2 T1 z. a8 G: _
解决方法:
) W' {7 q5 Z# V/ c- h0 ], X4 X/ {7 j
yum install pcre pcre-devel% c! L5 {. Z* w/ }
3.没有libxml27 i6 S7 T" v1 y" L) x
" R% |, T T$ t% r5 A, ^/ x) z$ X1 w# s# j9 ~8 d' _
configure: *** xml library not found.
7 ^+ M" q. w4 o$ T, `configure: error: libxml2 is required: e7 W+ b. j- @7 K# E: d7 m8 W* x
解决方法:
2 E3 ^7 [+ F' i( ]* h3 D4 D" |, ^2 h/ J, w1 g9 h! y A% S
yum install libxml2 libxml2-devel
: c; y8 Q4 v$ i) j$ l4.执行 /opt/tengine/sbin/nginx -m 时有警告
) Y( X" B4 J! x6 s+ p1 m" S9 \; Z8 W5 h" W, u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)+ E/ a2 |3 d! k9 _$ }
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 Y. ^- x8 c( R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 k5 o0 q7 N) Y ~2 r8 i2 H
2 A7 h9 e% G' u# j7 S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 }& L2 j K% F: r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
$ t& [5 H/ c2 M2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 @2 K! `; C& V3 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
! W/ p2 p1 k+ E# c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% G% A% N- w& t+ {# N* k' [
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# [, o9 y1 c _0 f) q& b# S
解决方法,移除低版本的APR (1.3.9)
/ ^- a# L& m6 u8 J, r6 v
, O0 D% ~) y5 d. N- f/ T! Dyum remove apr
$ R% \ z5 Z2 q5 n% O% U+ D5.Error.log中有: Audit log: Failed to lock global mutex
- c# q9 r6 U2 y, l
/ T; W5 p. W: {% \. C0 O2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% D' G# {# ~$ h. h3 z& ^0 y: vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' Z4 C7 U* h3 _4 _9 b
解决方法:
' x' [3 F" g0 A* G7 m+ W* T编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 ^2 a( V2 S) f& f
6 W, n) Q% R, u3 Q, t% |( ~SecAuditLogDirMode 0777% d+ E- W( A3 i: D0 k: C8 L
SecAuditLogFileMode 0550( W6 l2 D/ T9 o x" N- e
SecAuditLogStorageDir /var/log/modsecurity
. p0 O% O% ^" m5 m- [1 ASecAuditLogType Concurrent9 i* ^& W4 c& S. L- y
参考文章:- G5 t- u( D2 R. f& \# I& C
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! Z3 |* c5 c& P1 H8 P; M
http://drops.wooyun.org/tips/2614 |
|