|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ H- T" f* t' I% _* z. ]$ e
% |, S3 Q) _5 ^) f一.准备工作
7 K6 f8 U: Z# M) P) V) m& }) M; ^) B5 M% \5 ^; _
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 G+ y* t9 P0 {1 ]& K
* A' m5 d( {: {tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 ~ v+ |8 M) [- n7 j6 @! o, Y
& c, P( P# K" D+ H6 ^# Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
{: Z; a8 ~; o* t% Y+ Z( f& X6 t" v: R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
j; Z- @1 l3 S: f) W! i6 a: @+ y. ^/ G
依赖关系:
* _, s! j, {- v' D# }; Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 N; f8 h; a' d. M7 d3 K2 M7 ]2 W% L- T+ z7 M
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) }* a8 s1 d+ J4 h9 ~
modsecurty依赖的包:pcre httpd-devel libxml2 apr
1 l0 }! h- ]) e" i2 {( {+ V6 ?; o: f4 Y9 P% H7 E% O: l2 R
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel' M l' @9 ^( {; r
二.启用standalone模块并编译
7 h" ^6 ?& o" S N) f: X% o) V, c* q
9 h$ N' ^9 m0 c6 T3 b5 h下载modsecurity for nginx 解压,进入解压后目录执行:
. E0 U v( I8 [5 [. x( _9 c( x9 h' ?) A+ x5 N. A9 E
./autogen.sh9 L; S* `4 S0 A t
./configure --enable-standalone-module --disable-mlogc( o9 ` m: J; E7 R$ K7 U9 }. m
make ; ?7 b$ j5 w2 c% y8 _$ D
三.nginx添加modsecurity模块& W4 t, W! x( n/ Y1 \
; C0 r& ]0 z/ y% S* @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ v- ] W9 {% l
- Y6 Y; E' s5 D
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 a& V! Q* r# ^2 w7 `1 n4 w
make && make install( W1 b. k% I9 z
四.添加规则
% b6 O" \$ B( w2 A8 f, w5 B: z7 n- }- j! X, l0 f; G( H
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 W- l: M7 O( f7 ~3 Q; w' `
' ^; X/ P8 o1 U" f( ]1.下载OWASP规则:3 ^7 T+ m9 p' [. P
0 O4 X) S' {( R; N' G0 jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 f# r- R* I: G1 @* k
3 X' a) C/ Z, A
mv owasp-modsecurity-crs /opt/tengine/conf/9 k; o6 ~# p% ~% B3 s* @
, j7 }7 O/ w# a" t- E/ ]' c* e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: H& v5 S1 A% [4 _0 s2.启用OWASP规则:
% ~5 p& L9 {- ?2 B4 R
. V, s5 F; q3 y/ m3 A% P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 t* `4 i2 r7 j) x* J
1 \, w f0 S6 z1 o
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 x/ I0 b' A; p& J' e: I f
5 C) F2 |7 p! I2 Eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; @+ k j1 h0 R8 m0 y, u" D
6 b8 c4 \9 R% R! |Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 S. J2 B5 v3 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- x& X: O$ K3 r5 |+ H( H$ T/ @' ]6 aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 T+ P5 v2 f. d1 {1 N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 t& C$ Q9 {1 i+ q& R1 L" l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% C3 n8 e3 i' X- @! c! }2 B1 M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: G m- I8 J4 l% O2 FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. P7 ]) t4 ^9 J; p# \五.配置nginx
& f( D( E, q0 B- Y/ O$ }5 L7 P/ [2 v- K' h
在需要启用modsecurity的主机的location下面加入下面两行即可:* L* A8 t! i6 u$ V6 _; D f M0 R
5 A4 X# {5 | U( C. e/ u
ModSecurityEnabled on;
7 Q9 _5 b) O' |2 y/ _& ^ModSecurityConfig modsecurity.conf;! F# e) a0 w: a( C# R0 R7 G
下面是两个示例配置,php虚拟主机:
/ L' O0 k" O3 e$ H8 P" ^6 K3 l& M. w4 l# i+ H) b; x7 A
server { z/ N% Q' s( l1 I! m* k
listen 80;
3 Y% T5 W2 c' ]0 l$ r server_name 52os.net www.52os.net;1 J$ b; F% t+ u7 n
6 b9 t5 ?* h; p7 a1 Q
location ~ \.php$ {4 r* z3 d+ S3 n( }! t
ModSecurityEnabled on;
: e+ j6 I ?2 E ModSecurityConfig modsecurity.conf;* k% J0 G. F- ?% J9 e* `' O* {% c
0 |" D6 z% }. S; u
root /web/wordpress;
9 U; \, ]( V$ `" B9 @/ U index index.php index.html index.htm;
/ X1 o1 }. ^) c" u' P; e 9 H; W' A( y( p3 J, W% }
fastcgi_pass 127.0.0.1:9000;! n2 v1 J9 C% [* b* m
fastcgi_index index.php;1 S; K( G2 P8 N, `" j1 r6 g6 e/ `
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, P5 s0 P. u' k; m0 f
include fastcgi_params;
7 Q/ h2 d4 n4 t; `, @ }5 O% ~" S8 P9 x% L1 @% q, {4 Z9 }
}
; Q+ V$ f4 @( a" G8 t) \upstream负载均衡:
8 j: o4 J9 T* j, Q1 f: r
8 A. a7 f+ T0 L6 ]upstream 52os.net {
* [& M# }( G5 x: F4 F+ b. Z0 ]! ^+ z server 192.168.1.100:8080;2 @+ x2 [0 M9 U6 V/ \) {
server 192.168.1.101:8080 backup;& Y$ q. o x, |* m& O
}
V# r4 D0 Y% k8 n' _
4 Z+ {7 z, x7 F, n2 @server {
8 q- h( Y* O) a3 l$ D" plisten 80;
9 s+ z0 X+ L9 k2 p1 p3 q- Gserver_name 52os.net www.52os.net;5 q- E3 J l% F
) O8 |0 _( g! U7 @7 f- T/ Wlocation / {
9 |; N; G- U; W) ^4 @) { ModSecurityEnabled on;
8 j/ C$ C. |% x5 O) Y ModSecurityConfig modsecurity.conf;
1 j4 [" B4 e7 D9 ]' f( Y1 P/ g4 l. ~, M; s4 S/ w( k
proxy_pass http://online;
& E7 I$ w5 Y6 V proxy_redirect off;- v0 S7 }4 d" U: ~9 v5 p5 X0 R
proxy_set_header Host $host;
0 j" ?" A* e7 z/ V, Z+ d+ y proxy_set_header X-Real-IP $remote_addr;
% I6 |) x$ f8 i( |8 z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 m/ u7 U- w& ^6 C) x9 ~8 G }* @: g, C. Y/ H& _ _0 O4 h/ J
}
0 k$ h$ L' t" Y" `1 O六.测试
; G% s2 D* B$ }! |7 J4 p1 @$ \! v v: E/ ]
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 ?8 R9 R) c% v. b
9 i/ _, U4 |" c9 c4 k0 j0 z
<?php
* a2 {* G# Q( u phpinfo(); 3 d9 B- P8 L: B. ]" @2 }
?>' }1 K5 P# Y% X' {% X
在浏览器中访问:
) M% z& E& k ]* S! c- K: p8 P1 I# y, Q4 R
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ N( a; N7 y& F4 K( W" R* Q: `% ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' C6 u! @' j0 H l: E- e
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, L* I8 S* C! B6 [
说明sql注入和xss已经被过滤了4 Q9 I% ]+ d3 a$ _+ D! @7 G }
p @6 [+ L2 _- i* Z# l
七、安装过程中排错
5 W$ c3 {0 e& W$ p% M8 V4 }9 ~4 I) v
1.缺少APXS会报错
" P9 ^/ a9 H+ Z1 a9 Z- P I9 f
, ^" O; N' o5 o" cconfigure: looking for Apache module support via DSO through APXS
H# z/ T9 j( m4 lconfigure: error: couldn't find APXS
( N3 w( d( I: [apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' t" v2 M7 _9 f( Q2 c& E! {解决方法:- D& q6 G4 G+ d( @( J
+ N, i2 C* B/ h! h
yum install httpd-devel: V* v5 C2 m- L0 F: |- A
2.没有pcre0 g3 ]& C, Y# p& R4 \
( s5 q5 _/ m8 }& f, ` ~ R, T( s
configure: *** pcre library not found.
; B) }4 X$ e, `4 |configure: error: pcre library is required
) i: O4 s9 p" K+ \4 H& `+ r4 \解决方法:
- T3 ?6 K* p% q% V; l! \% j, ]( d" W; _% c
yum install pcre pcre-devel/ [8 |; E% u9 N. p0 g
3.没有libxml2* h' l' x1 P" B- h5 Y% q
+ |7 T* k( W2 H/ g2 p. o7 V
% h, b% P8 w7 G- m5 Y) ? qconfigure: *** xml library not found.
$ p% s! F+ @3 [* `* dconfigure: error: libxml2 is required
* L) o' y( F6 t- w解决方法:
3 J8 [$ d1 n$ e) M9 T% [: b! P, q$ T+ h l
yum install libxml2 libxml2-devel
* L! E y$ {2 O: V, c- h, j4.执行 /opt/tengine/sbin/nginx -m 时有警告: n! R/ V L3 @& ~/ U7 L3 N
6 Q+ n: w4 N! `5 l. o) H
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 }" V- b. M# B2 s* q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* X* v# M6 ~. S" j& f' l2 l% u原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log M. F' s3 {+ J# U
# Y8 I& ~+ \. b( u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: @% {! @1 ]" I5 [# e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" ]$ H$ c/ B+ H Q% g2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ q8 y: g9 I3 I8 Q4 _# E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 s U' {/ n }6 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' F# [ v9 K# @1 D; f
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On. U* E- [# S8 n6 ]3 i
解决方法,移除低版本的APR (1.3.9)
9 J+ o# n) o1 P! e; E2 `* Q7 ^0 y: S" A6 I0 {
yum remove apr
7 B1 V; O! ]4 u- S; R' Y5.Error.log中有: Audit log: Failed to lock global mutex
, Y- y# _* x" E( c7 ~) X! P- B' P3 G9 A7 T$ n" C' K9 ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
5 k" o" i- Y) F' Pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 B* _/ r* T. T8 h; @* Q
解决方法:* \7 f) `- v& a; v' O8 e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 \4 d$ I; [; q4 v5 y3 t+ d
% B, }+ g0 C0 b: }1 `. `! y7 E( fSecAuditLogDirMode 0777$ o* H0 m2 T* X/ a3 z# {% N: i9 J2 i
SecAuditLogFileMode 0550
) z8 ^# c) e! ^: ^SecAuditLogStorageDir /var/log/modsecurity
9 C8 N& k% K$ G. B. RSecAuditLogType Concurrent
1 b0 z( y. Y; H1 s4 |参考文章:/ e5 E3 {' a& m; B6 b( q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) w& R7 H5 M4 g
http://drops.wooyun.org/tips/2614 |
|