|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ q9 A, K2 n4 c- ^1 e/ D4 S. Q' p5 |9 e' q8 Q! d: Q* i
一.准备工作
. X, s$ [6 H1 N
$ x, R% O/ ^3 ~/ s, y- g* ~系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! ^8 Z/ h$ L' E' [! ]( }
7 I4 a7 B U( @* G3 ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- x$ K1 a8 X+ [
6 C/ }/ y8 Y* \' u. I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ |- d5 ~7 o3 X w
1 b( ]" t' L( V7 X. f7 GOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 d9 t3 j0 f7 C# f
9 H B& r/ ~! X: L: X Y
依赖关系:
, T1 \9 T9 l# Z |' V/ G* I0 Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! R8 E. }8 J+ ]1 t3 L
2 l+ f4 c1 J: F( yyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 v$ e6 V' F4 U; ^! L! q0 Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( p+ ?- i* r3 T; |, Q- s9 L# ^0 \1 J+ d! P) I z) B1 `1 c
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! T4 E$ `5 d# e! Z. k6 {) {9 w' q6 L
二.启用standalone模块并编译4 t7 N0 u/ }% d0 t: R
: l1 s8 K( X2 N' D! D$ ?2 g' ^/ r下载modsecurity for nginx 解压,进入解压后目录执行:+ q7 `5 r( p- ^- \! E
$ X U+ o/ L/ Y' ]! h
./autogen.sh
( \ m7 m( s# S& o./configure --enable-standalone-module --disable-mlogc
/ T; m6 u$ p+ e* V" }0 y7 \7 Cmake 8 R* V) l$ ^. J# `, w& t3 @& q( I
三.nginx添加modsecurity模块# k' y L5 J" S4 V7 ]
* I0 r. u! @; `6 q- [: ^9 R: c
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* B9 x0 j A8 ~
6 j- u" G& q* J4 _./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine8 {7 M. z1 C, R: D' k+ l' d
make && make install$ ^- ^6 P5 _! n; S! {' R
四.添加规则
) Y2 |1 A* V/ H0 p! q }. R3 `
* y% ~9 ?# K* X0 O8 ?2 Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: }, Y/ @& w/ K1 }
& ], I: P h) M0 j' _1 F i0 H1.下载OWASP规则:* m' d: H. t- D: A; \
$ Z+ O- }% [# q3 Y! B. z/ Z( i* y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' r. K+ j# J; r7 {
$ t2 h1 p; `! X7 Y& h- umv owasp-modsecurity-crs /opt/tengine/conf/
: q9 Q1 x9 D, q+ B5 }5 H) {) k6 j2 u) y8 M5 w# f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) R3 J: B" @4 _; X( \) X; ]2.启用OWASP规则:
A- _- v1 E3 ?+ \) m. k
& Q% r* f8 T$ M# B/ @3 h复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; C1 D' q5 P/ S+ S/ N1 h4 M! C+ j1 h2 V4 a0 x/ c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 O1 X( d5 w6 E4 U) g
4 g( ^( S% m0 Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& E2 I7 ]! o% p5 m& M
2 r2 Q) d4 |# ?3 T! y; kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, \7 l+ j/ _* f& CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. z3 p, o7 v. u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' [4 r2 v+ f# c x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ h X# x O! ?, P$ t; sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 ~* r- D8 k8 ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ U5 o# j2 n* E/ x3 o6 F1 i% W3 E
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; ^: F2 s- e% g五.配置nginx! {+ K( @1 X+ I# C
# T/ F" s' K1 g) a5 Z, x8 ]. `
在需要启用modsecurity的主机的location下面加入下面两行即可:0 Y9 q1 I. k/ V, X( H; P' y
9 N+ l1 Z2 I: v3 K, D9 l5 V
ModSecurityEnabled on;
+ f, p) b" f, @ModSecurityConfig modsecurity.conf;
* E" @, Z) l% I N3 E: l下面是两个示例配置,php虚拟主机:& {) }! y8 S; M0 n* }
4 U! \6 m) o( n6 W
server {4 r$ U, G* B' ~8 n+ q# _
listen 80;
& a. M8 t N4 ]) D0 O server_name 52os.net www.52os.net;
; H) X9 Y" p% l# u" `& O3 \ 5 r9 r, }4 K: E% O4 Z2 \" S
location ~ \.php$ {% Q! w+ |" a) m2 F8 j2 Q* b% K
ModSecurityEnabled on;
+ }6 |2 C u( v ModSecurityConfig modsecurity.conf;
5 W+ O c/ ^5 V/ I, Y& S$ m9 W
. Y% H F# x3 B% s root /web/wordpress;4 T' z1 ?$ z+ p' o5 i8 a: W0 g( X: ]
index index.php index.html index.htm;. M. q$ f1 W$ A2 Q' ~4 r1 Q( j, _
) V A' F( ^. p+ A9 I
fastcgi_pass 127.0.0.1:9000;/ w: ?) Q* e* G! j' N
fastcgi_index index.php;
1 E( H& y$ ?; x! A7 @" } J3 q; h; J fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' a1 S( u. m: W- |% p N, Z3 b
include fastcgi_params;1 E3 K5 h" V# Z& F# j, O0 ]
}
- E, T: z) b J, x( t6 x }
( v) Q- m2 W& v" `; U1 ^$ a6 v" n; Dupstream负载均衡:
4 ^& s( e5 n- Z/ Z- \% R1 o9 v& ^/ F
7 K, x+ R# O$ c5 xupstream 52os.net {" K1 H: N$ P5 U" F
server 192.168.1.100:8080;8 M- n- j9 ~* @0 J/ Q
server 192.168.1.101:8080 backup;
7 b6 Z0 w9 b% `}, o: w7 {; L# P; A( u
- a: a8 H6 x. G% t* R, vserver { O' U- m- D! ^
listen 80;
, M1 c/ z3 Z$ c& B! S' `& Y* p" `server_name 52os.net www.52os.net;- n0 z. S& N& @: B
$ O, A6 ?" M6 e% u9 K2 j& Tlocation / {7 l, _ u) ]; _) K% G. R [% U
ModSecurityEnabled on;
. H, q& ?# } ]+ ~% x ModSecurityConfig modsecurity.conf; ; f; [; B5 [& [8 U+ Q
8 y4 p$ e& n! H: _ proxy_pass http://online;6 I( j6 C' e4 ]4 T( h% B# \
proxy_redirect off;1 K; u. |! N5 \! g' B/ y9 f2 ^
proxy_set_header Host $host;: {5 \: H E p! k$ @0 b$ d
proxy_set_header X-Real-IP $remote_addr;! R6 Q% T7 n2 U. f* A
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 ]4 k% t# |; c+ g2 s9 U, i
}2 ~1 _# ?: q5 i7 ~9 Q( D
}
?7 G' [/ f. {6 k六.测试
% g! e$ R! P8 y& k7 c
+ L G: w2 d5 [* t; a( D; ?: u我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 g9 E6 @& F0 M4 |
9 e" q" A4 N9 X: z' |<?php; Y! \" Z9 _* ~) g; H& O
phpinfo();
5 r q3 Y' B9 S6 A' h?>% b$ w' y8 n- x
在浏览器中访问:* |; t$ D" i2 w1 B- C6 a
7 ^( b; O8 H3 v2 t: d- L* p0 B
http://www.52os.net/phpinfo.php?id=1 正常显示。2 ^7 i4 @3 h) K: [
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ O. Z3 r: _7 ]
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) @3 j# }7 J' s6 o) ^% J; w4 u
说明sql注入和xss已经被过滤了
$ q) h2 D& W8 U: {2 |
- O! H+ g# {: }' L9 ]七、安装过程中排错
) ]/ Z% ]4 J4 }1 L
0 H% d! ?9 X$ J3 O+ @1.缺少APXS会报错2 g; A) b3 u ] I1 }
) J% e3 [5 R5 v: `4 ~1 A) M
configure: looking for Apache module support via DSO through APXS* D; a5 ~; \8 h; P2 V
configure: error: couldn't find APXS
% ~, w7 c: Q2 y8 k8 R! @) Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 E! |5 U4 m* Z, O解决方法:
4 k9 y4 z! G; n7 \* O: Q. c# H& N# D$ g4 C5 F! \) v
yum install httpd-devel
" J" B" ~2 {& k8 y* s( o+ s5 p( w2.没有pcre
' I* X) O2 {8 ?$ q. z1 j. G9 j
9 h% a) a2 v( w$ i# `configure: *** pcre library not found.3 U: M$ l+ ~2 c; g: f4 ~6 G
configure: error: pcre library is required- W" g5 [* g5 D8 H; x. M4 T4 d
解决方法:
# ~2 u" h% ?# G. [, @# P4 _- B! T" U. b5 H
yum install pcre pcre-devel# b4 U: l: f6 y) l4 ^* Z F: C4 m) _
3.没有libxml2
$ s4 e. s5 U8 W- _8 M1 \6 k& P5 ~
$ a4 c5 O1 Q, `+ S3 ]5 j
4 Q1 w3 C, r' Z. w* |configure: *** xml library not found.4 P( [. z) E3 j; Z+ G4 b
configure: error: libxml2 is required% J2 \ J9 \# g t! p. P6 I* l0 |; Y
解决方法:
# O: S v" m; _* d/ r& u( N" l* ]+ ~, v; g3 S. b) G
yum install libxml2 libxml2-devel
1 H+ ~1 K* K A- C8 ~) O q+ E% M4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ g H8 o5 P% m% b7 d4 j
/ o$ L0 v5 F5 P+ i4 C2 [Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" R! @& J+ | i# W3 b" \- B' W' a) P& @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 W% v( w4 N9 w C( \! w
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ n- Q- w: B) n6 B" F# q( r" c% ^! n( z$ s) f6 Z# Y& m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 v+ _- U0 n/ T% n3 @ E Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 Q% X2 v5 D4 ?, r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ v" Z! j/ K4 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 e$ d7 }. [% |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# d0 K# t8 |: j( B* R' P
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; A; @% o( `" M8 _
解决方法,移除低版本的APR (1.3.9)! S; k& U* y0 { B8 U3 T! q/ M
, L. E. I9 k4 [$ f0 S5 @
yum remove apr
# J. C6 X k R" x$ k5.Error.log中有: Audit log: Failed to lock global mutex x2 n' j3 n: A3 s! `6 _% E
+ R) C* a) O9 W, u2 t+ f2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 E+ s; N: O E, j/ @, Aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 Y( u$ v( m3 V0 A
解决方法:: k a, c- i( |. v: ?5 d
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. }) x/ r0 }$ f' j4 a- |" X
, W- r! ?1 [, a
SecAuditLogDirMode 07770 I! ]( g& Z7 ?1 T2 g
SecAuditLogFileMode 05504 I1 r; ?2 Z# @7 N1 }2 k
SecAuditLogStorageDir /var/log/modsecurity
- `! e2 Q( j' b3 ?9 M) X& iSecAuditLogType Concurrent8 L, J, @: T5 E2 D7 u
参考文章:9 }- J4 b3 q! i: f1 W) n( d2 a7 }
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; G6 }5 e( E8 W6 K/ d7 M! `http://drops.wooyun.org/tips/2614 |
|