|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 }3 }$ a% H& M& ^! n
7 r5 H# |0 H8 x4 d0 P' |, z
一.准备工作- z9 }6 {* ^9 o3 @1 Y6 q, [
7 y0 ]) P# i6 \! a! K1 m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. _3 O6 T2 V: R
' L. G/ @7 R1 @) {3 H& I+ ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ S- R2 y) N$ X2 z$ o1 O7 {
D. o7 \! |3 e. t
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. y1 Q1 v% U6 @7 d/ p! G
8 \9 w7 r5 d7 _5 w j yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 r1 a3 {* J% V9 a: H
6 y5 @4 ]1 b7 c依赖关系:' V' u* z3 y; z0 K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( t; C$ ~4 |% d7 V/ K1 d7 {
2 @( {' H/ _% E: A# L" ^7 {$ |yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
6 } K) N6 k, Y! Q. d0 h: T: Q) S0 a; Emodsecurty依赖的包:pcre httpd-devel libxml2 apr
, e+ H% ~$ q+ _( s; }' Q* l& f8 D" p) H3 {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! @; Q7 ~3 W( y7 e: i# e; x5 ]$ J二.启用standalone模块并编译
3 m* E w) m9 f! i% [: j. g/ s% S. [: ~5 y0 m% h
下载modsecurity for nginx 解压,进入解压后目录执行:- L8 Z% ? E3 }. _9 `
) I# ?! q$ h+ [; y6 n' t
./autogen.sh
* T! }) j0 y5 ]" U# M./configure --enable-standalone-module --disable-mlogc
* ?% L6 E: P$ Hmake
' J9 |6 Y1 C$ N6 G3 J! q三.nginx添加modsecurity模块7 ~9 T; w C4 f- \9 Z$ O0 v) ^
7 d) r: c4 e, h% r" P2 t$ I+ E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 k0 N! O: X H) Q: g
5 u" w9 A) x& G! G* @' T% R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% F5 \" E! u9 Y
make && make install
) s: N7 R0 B( y! |( F' N四.添加规则/ d2 o% z* H* f/ z3 A
" N8 e7 ^* \$ c, |! |/ smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ y: G& ^3 N1 h0 _$ Z/ ]0 l- R( O9 B$ H( D) o9 Y7 R
1.下载OWASP规则:1 R6 r. g/ I" J0 N* O( S" |
3 ?+ U; V8 H1 ?5 C. Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- b8 @2 W9 g' z' p
! b/ x1 z& Y" ?8 d V" omv owasp-modsecurity-crs /opt/tengine/conf/
. G8 s- t% S6 [. M# M/ [2 t4 g, B# }! n9 `
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 R9 V4 v. [, Q( s4 Y8 a }2.启用OWASP规则:
0 d: n- F& t$ H# O
! R: _% ~5 K9 O4 ?+ b复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 y7 M6 f% q/ n2 O0 k! `. [$ t
+ \8 L8 g' Q; L* l2 @编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 f; |0 }& \: c) n# I8 Q/ j; q
+ p: k: }7 ~4 e% n2 f. T; U5 c( m
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 n$ [, E: ?3 v8 l: ]
) s7 L. |. Q3 h$ z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 M" a. `; Y+ @* E$ v& z3 n; TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' ]+ D3 u, d$ F# v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 R8 @% g0 X% i$ Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: F4 z& P1 M1 H/ T9 g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& C% c) M: k: k+ ], l; W/ ^$ f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 t. {4 i# O# H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 [! G9 o0 f: S- c" l" m7 e P# Y- h
五.配置nginx' X& @7 p1 p9 N+ O. X. y
' K& Z) i7 t8 `
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ B/ D" {+ g/ t8 y# ~" w
& p7 U! p( |. W- }6 x7 bModSecurityEnabled on; , n J, r" s( \$ o3 x
ModSecurityConfig modsecurity.conf;) n7 l$ y3 a o/ u, F8 Q9 I; v* D
下面是两个示例配置,php虚拟主机:0 J* k: {6 u) P' K' v
4 R! l" j5 i1 v( w% f1 q2 u* U7 ~
server {
\* V, o9 c. U listen 80;! R9 o, l' Q( B) ~0 K
server_name 52os.net www.52os.net;2 l" T, I. o& z I2 e9 B! |# V
$ |; F( M9 |4 Y, V+ V
location ~ \.php$ {
# l+ O1 t0 i f( M$ W2 D ModSecurityEnabled on;
4 H Q' |! _1 n0 p f' A0 u ModSecurityConfig modsecurity.conf;( P- u m2 b$ |4 P6 @2 Y7 M, d
4 R0 o8 p9 B- `9 F root /web/wordpress;
( J# n- i4 V# {8 q5 i _0 x index index.php index.html index.htm;6 ^/ ?; `) e. i Z
2 u* J2 f1 |7 y5 T3 G
fastcgi_pass 127.0.0.1:9000;( S+ m+ Q4 H/ {
fastcgi_index index.php;+ T/ ]$ J( B6 J+ Y+ ^, |* H/ x, `: d
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;. I* h" D" u7 m3 l) F& \7 ^
include fastcgi_params;
: l! i" f! P4 `+ }# @ }8 R+ C1 f C! f: Q' X
}
* z$ f, I) Q! B4 \7 \upstream负载均衡:1 V) U3 K6 T3 s3 \+ G- Z: T
3 w: t* _/ a j; w/ O7 l8 wupstream 52os.net {2 Y" T7 ?( I9 ^; @/ g
server 192.168.1.100:8080;
/ w# ^5 y8 s! n8 |! t( O6 U server 192.168.1.101:8080 backup;( B+ Q& u p0 Y4 P
}- K4 n: R: t9 ?7 q, ?$ `
# @ Z, |6 ^, B3 C2 M8 Q6 F
server {
6 h7 `- v; K8 Clisten 80;0 v, ]9 m( ~2 R& t% p# e+ ?# a
server_name 52os.net www.52os.net;
+ | ]. P' [; E( k4 h8 V
( I% u5 @. i' c0 b* s, `3 c- H6 elocation / {
) t {' j. t* K9 t" {8 b ModSecurityEnabled on;
) @9 j! g1 e+ e. [& _1 s; |4 C0 V ModSecurityConfig modsecurity.conf;
3 w# R6 M+ g: a$ H4 d# f0 ]# v1 O5 V5 K3 ^
proxy_pass http://online;
3 e: ]1 a$ ]5 _8 O V3 `3 }( t proxy_redirect off;9 u5 ^5 k+ J! w1 V& S! }3 ]/ r7 {
proxy_set_header Host $host;' Z/ W: w' d$ J& X
proxy_set_header X-Real-IP $remote_addr;
! o9 D* p$ w- }' v proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;% L; U2 }( h0 q t! x5 q9 M' r" T
}) q# s3 n f' s& T) l% u% W
}
% S$ F) [) w0 t+ V六.测试$ e3 u( o, t' V' B9 D! e
1 y, g0 d' c$ d+ O! n5 |' L; ]0 |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 h% G3 p/ f% P
' Z" i9 r9 ?5 A<?php
6 G5 ]+ c: J3 f5 V phpinfo();
! L# X; F8 U8 o6 i) n?>8 I& [6 d& O/ v/ I" G( Z4 \; r! F
在浏览器中访问:
: J1 Q- A1 W5 @+ S5 t
* i; J7 w: V8 [http://www.52os.net/phpinfo.php?id=1 正常显示。2 _% B: b5 }9 \" Q1 _+ y# m
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。% O7 ~+ o) n( g( I
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 q, I0 I4 F0 V, t8 p- K+ x
说明sql注入和xss已经被过滤了4 ?. H7 B& a( w
2 k9 e* t# f' W5 D3 v
七、安装过程中排错
" [' s; y7 N7 n
8 r8 L% w& K8 b i u2 b1 S/ c1.缺少APXS会报错$ z$ d) v1 J: t z7 i I, q
+ H3 A6 D6 D- C: tconfigure: looking for Apache module support via DSO through APXS% B! P# L c! ^& r( Y
configure: error: couldn't find APXS
7 z J- V/ c6 K3 w2 q: ?2 Y" w. v* Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( q. u3 I) ]! o* k8 j& {
解决方法:* Z! ?( z! J* v( \& M9 h
- Y2 B4 P% D5 ?5 v" l
yum install httpd-devel+ q$ u' }- L- f9 ]6 g4 b
2.没有pcre
7 e: ]) L2 V. k" h- T! H: M N8 h
configure: *** pcre library not found.1 k; ~8 u! N0 _( L! c
configure: error: pcre library is required
0 N- s* G" A5 `3 Y解决方法:3 `/ m% F6 C) s9 K4 }
1 q8 d4 P4 T. ^( hyum install pcre pcre-devel; h! ]& w$ l# v) H7 D
3.没有libxml2
5 V" F# `1 \3 m. G2 o% J: S9 Q
% \* Z7 \: H6 d+ F+ I; d( j7 w& f5 a1 _
configure: *** xml library not found.
* I5 G! O2 `! o+ |configure: error: libxml2 is required
$ o( u) r7 s) I/ S( S# |解决方法:1 u$ c6 t. R$ y8 W
( ]8 U1 H& y2 p* D
yum install libxml2 libxml2-devel
}0 f2 K/ p, _$ D5 L7 v1 Y0 i$ n4.执行 /opt/tengine/sbin/nginx -m 时有警告! R# e7 Y0 z- i3 r+ z' M1 c/ k$ Z7 D
" ^& U4 p) G% V0 J" n8 E. u8 NTengine version: Tengine/2.1.0 (nginx/1.6.2)
' l( Q( V% O: O3 B' }* q8 O9 L0 jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 [) k; g/ c W5 u- h原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- Q! b( z3 k/ k! h6 P* X$ d3 d3 s8 W+ @- @5 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; s. N4 P9 W! Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". w0 a3 {+ G& j3 P9 Y5 ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% U0 }. e8 p( G0 k; [5 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, R, q. s1 L0 m$ z ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! s) c+ @( ~8 \ m1 L- h$ |
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ S, _0 m: y. c5 {; ^解决方法,移除低版本的APR (1.3.9)
" E' ^" | T6 W; R/ u3 e* f
$ N% \, G+ ]- _9 R* h. Q) ?) v. ?yum remove apr$ W. F0 D6 E% G! q0 W, Q- \& \1 c
5.Error.log中有: Audit log: Failed to lock global mutex( \7 ~1 k, h/ c1 d* d" ~
9 m( C; L; D2 j0 K1 H0 P- |2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 S. J- l1 h5 U+ |5 jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 a' p; M' Z! z+ W
解决方法:
5 s7 P( j0 \; u' E) G3 Q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 f i9 C0 m, s
! m/ h& R- n( b, b! |! R4 R* D
SecAuditLogDirMode 0777
0 e& x1 h& Y& n0 fSecAuditLogFileMode 0550
* U( L3 v8 E6 a0 N) H' `2 C0 {SecAuditLogStorageDir /var/log/modsecurity4 T- s3 R0 u3 p7 f! u+ h2 q( f! |
SecAuditLogType Concurrent
6 Q4 d% w0 W: y& Z+ c参考文章:
* w; U) l# Q- F" [4 ]! w" m5 khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX E% J8 m2 n8 ^ X
http://drops.wooyun.org/tips/2614 |
|