|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" m1 A* \8 R" Q: c
# T% E. q" \2 m一.准备工作- l* [3 ^9 v' U' h2 x& [6 F
$ r$ Z; {9 j" a# ^) z+ {/ X
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- h7 H% L! S5 k! O# f, j6 ~
: L, x' x8 ] h% `tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 V3 d e4 W6 D5 o( K6 K
+ v5 I( _ d" u0 ^2 T3 w$ e
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 v4 g. c) z7 R5 ~
! p a) x4 w/ r6 _8 `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; L* D/ h/ M2 }9 Y5 s* B! d0 a/ }* |3 S( I0 B4 F7 {/ ~, t
依赖关系:
0 S& N! i0 J3 E: Q, `" B% U7 qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 W2 R* v; j1 o9 X8 t9 d
$ R7 d: W5 y4 ]5 d9 @yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel0 k- ]1 u6 ]0 g
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 N5 T6 L1 @3 j. r- ]6 L
9 E2 U4 v2 E) |* F6 g! ]; v' \2 h
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) Y: ?5 p+ K' [# r- y% c% S
二.启用standalone模块并编译4 N3 s$ g9 _, _/ t
8 L* S5 {* X4 @' h2 ~
下载modsecurity for nginx 解压,进入解压后目录执行:! [/ q% ~1 q7 \7 }/ i, p7 Q( E( a% @
0 T' I; Y9 k' S3 d8 r. m
./autogen.sh
; q( [3 `7 O/ `& Q! B./configure --enable-standalone-module --disable-mlogc9 Z7 c$ _5 c, n8 I6 Z' O7 d3 I
make
; U. _* p" @, ~( T+ Z+ |- ]三.nginx添加modsecurity模块% H7 K* F- c$ M5 Q" N6 v
6 R- d' h5 C* y' { h K在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( }" k$ K6 Z, M2 ]' Z
. B p" e1 X; x. [' w./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ T; Q6 k; E5 K! m
make && make install
) ?6 J' W6 U% G8 `% ~- Z1 \四.添加规则& d/ w) m A" H) V) _2 l
+ k# O5 o# z& X$ M
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 ^+ f' z- o6 x
: T# j# [# U+ o8 W/ P" r
1.下载OWASP规则:
% G" y3 ?; R1 C4 y
1 P4 A- |( H) }* G( c) A7 Mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ D: o: S w# k7 s& I6 u5 H* B6 U! ?( q
mv owasp-modsecurity-crs /opt/tengine/conf/' X. u1 U& o0 b4 A, \- B
! y1 f" K% o: c- ^; o5 K( t- Tcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf+ N- E9 A3 G( T# c, }2 c: i8 P
2.启用OWASP规则:8 i2 D6 k: _/ _; d, u% z
. ]) G% W% g( m! \2 r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 q3 C: n- m/ Y3 U4 }
' B$ D% s/ {5 j3 h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' n. V. c) _+ ~' L) F! J
* n8 d' P9 x# |! zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# C- {9 f; T9 T
1 G6 B# J( R( p* yInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: c* `* k& a% m8 u3 A# U n: ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: Y4 q$ z- H( {. Q7 ]0 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 A0 c" k6 x1 A3 n% YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) h; {2 w3 v) L3 aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& j i% r6 V+ |% u: f) X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- l! ~0 T- u4 t% T1 p' {Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. a% z- d# w. ?# h
五.配置nginx" D0 Q( T, o1 L6 X& J/ J
4 d' x. }1 A5 ]% `9 k1 v在需要启用modsecurity的主机的location下面加入下面两行即可:
6 a0 I2 }, V+ k/ h5 g: c& t
# b1 T3 ]$ L6 K6 D7 OModSecurityEnabled on;
$ A/ T" f# m% U6 `+ l9 QModSecurityConfig modsecurity.conf;+ r" R- m% d1 j5 p9 t
下面是两个示例配置,php虚拟主机:# s$ U5 N8 M2 K# S6 Q
* H) U i9 z( F: ?8 z) k% {, Oserver {# P& i/ l4 d! i0 C! B( W
listen 80;7 M" u. R# x' D, a# p! ^# W j
server_name 52os.net www.52os.net;' J$ J+ m B3 M( c2 O4 s, G
' ? ^' P2 B# w. P6 @# S4 s; X
location ~ \.php$ {* ^1 o8 Z3 v- ]3 p P3 [4 W
ModSecurityEnabled on;
" B' ^) L; S/ N ModSecurityConfig modsecurity.conf;6 N+ j5 q4 G; V( R9 d# T
1 u/ D! s$ L& C. Q0 [1 B
root /web/wordpress;, P8 O" K& n c5 p5 f
index index.php index.html index.htm;
1 Z7 V" M, s8 R- e+ T ! D) Z9 n9 O0 g
fastcgi_pass 127.0.0.1:9000;
8 g4 \: @. g/ d" H$ W fastcgi_index index.php;4 ^+ n) U' L+ [9 k/ ^/ ?
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; C9 f" f# D$ O: v4 I1 p
include fastcgi_params;
) [% ^4 h' ?9 z* q. g6 l3 P }- i D p4 _! n0 [) d# J) b
}8 z* v$ a/ q. ]6 L: y9 k: g$ r
upstream负载均衡:
7 B( M# Z! r# b# A6 d7 j9 {* z5 q! G4 s7 z" i
upstream 52os.net {
! T( c! k' N8 [ server 192.168.1.100:8080;
; y& c. I, X% k, R: a* o3 {, f6 i$ o4 [6 a server 192.168.1.101:8080 backup;: s. t9 ^* I5 o0 C
}
: M$ X; o; N# k2 }, Z' Z
6 }5 N5 f3 S, n8 U- qserver {' q1 ^; V6 l2 o t; A/ }& L% {
listen 80;
- f: j4 M( L s' R7 T7 tserver_name 52os.net www.52os.net;
' g4 r" k* E& k7 u
. @) n. X! F1 K, M) y8 Rlocation / {
7 `8 B* f) I, U+ B F ModSecurityEnabled on;
1 a' t: h- F: a. G1 H7 q9 V0 U ` ModSecurityConfig modsecurity.conf;
5 U+ Q1 }& X& B0 a# @- A; \' a* q, N& u1 v' Z/ H: [5 y
proxy_pass http://online;+ Q% ^' Y- E6 E# ]9 _8 F, K
proxy_redirect off;
* F( c- E$ I- G7 z4 {( w proxy_set_header Host $host;6 `; p5 J5 O4 ~0 d' E
proxy_set_header X-Real-IP $remote_addr;
2 Q7 t. M) |: l proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 C5 D( p: t% F' y0 t5 U) b* C
}
6 e( C9 D7 N" [9 ]( \; c3 `}
2 U, p4 y" Z' ?/ h; f4 \六.测试
$ l; X3 U! M a+ v& H: Q7 Z0 I+ V$ B
' I) o" h0 r% i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 d( q$ J Z' j2 E, c. v
; d$ H8 _) I2 n/ X<?php
) |* U7 F- B+ M: S' ~4 i7 D1 p& N phpinfo();
' I# R" R* r4 c/ v* {% x?>
, n5 o' }$ H0 Z1 E: o/ l9 v* O在浏览器中访问:
1 a% I4 J0 n( x5 `; I
) M9 s7 A! B* f3 M9 c K0 Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。- H' @( p S' H
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- |1 u* S) f7 B* B" e* thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 m. D" v7 {0 h说明sql注入和xss已经被过滤了
* Q0 i" U* Q7 _" `3 M7 D
( c4 k0 L3 o; L8 K3 {七、安装过程中排错1 S4 u$ J3 w+ ^/ f9 `) j1 o5 h8 K
- A* i& s$ i$ f' m# a1.缺少APXS会报错- _6 A7 {, g! {; K6 v# t
, T0 S. O4 w; C) N/ lconfigure: looking for Apache module support via DSO through APXS3 b2 z5 F* m% a* l* |* U
configure: error: couldn't find APXS
4 X0 N' d( p# T' D; x( u! ]: vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& F1 v) s' z0 n
解决方法:+ G! n( y% w; Y0 E
9 N! W. k1 E% v2 F( H' [, d$ J
yum install httpd-devel
/ h( a) d* o$ F9 @* k2.没有pcre- [& Z6 i; r9 I5 W- [3 }
$ z4 B8 A) F2 ?" W1 C
configure: *** pcre library not found.
3 H0 B6 f( @2 a+ gconfigure: error: pcre library is required
3 V9 i+ ` E6 U& s解决方法:9 n4 q3 }7 D/ x9 F5 R: I0 n
, I' z# e' P; `* syum install pcre pcre-devel! I$ N' q8 T2 [! L
3.没有libxml2
0 o0 q. B- V1 J2 {5 B/ i+ c
" t7 ~7 q" B( D* ]. O& {" ?/ v$ c5 _* {" k/ k
configure: *** xml library not found.
% [1 w% H' W; \8 _1 ]4 i) o1 vconfigure: error: libxml2 is required3 k4 N, A3 d, X
解决方法:
: Y% u( z, M% _0 L- V& c2 }. g) _( T; {! M: M& O8 ]+ d: i
yum install libxml2 libxml2-devel1 |+ |9 V& ]9 R: s+ g/ K
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- O& ^: F: m3 H# _
! v5 h1 v ]8 V3 y9 o) }Tengine version: Tengine/2.1.0 (nginx/1.6.2)! ?5 {8 z! h4 B- c1 ^
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 k3 ~6 V e. D6 L4 z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 W& q1 T+ ~% E) X! O% O
* i% `5 |' V ]7 a' J3 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured. M, d/ h$ K5 s1 w5 G" Z& B& d( [5 X# ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
* |$ e, ^: u9 Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. J" {4 S, u' E% H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 H8 c( ]2 Q) [- X1 j4 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 v! J s+ j# b+ K% |5 y) _
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, S. L" C7 S) j解决方法,移除低版本的APR (1.3.9)
# I& k( e1 }$ H, Y4 n6 W' v9 x* P* q N2 X$ P6 V
yum remove apr9 M$ U J! m) Z6 F8 d
5.Error.log中有: Audit log: Failed to lock global mutex
# s8 k- X, y; a7 S p* l% W
( p# B8 Q6 @9 Q1 j2 p2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
5 o4 _' p0 k) K. ?- x+ F$ vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% G+ W$ U% B \5 J5 f! J
解决方法:
5 ~$ m( C6 S% o5 j/ k) Q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: t: z0 u- o. l5 W# p, j7 K) q
( Y, |: T" D; k: [1 Y; P0 D4 R
SecAuditLogDirMode 0777 X0 x0 ?+ b. l7 I: V8 d, c; w6 i
SecAuditLogFileMode 0550- Y; K7 `% Y7 J- t7 }! c( M
SecAuditLogStorageDir /var/log/modsecurity
- n) M7 }# f) }3 f% rSecAuditLogType Concurrent
+ m L7 P, _$ e/ ]) C4 N参考文章:
" G6 d/ o. X! z, Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ c1 v1 t4 D" _ a2 \: a. A: bhttp://drops.wooyun.org/tips/2614 |
|