|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* E- Z7 X- b' A1 \- G
2 c* u8 `/ K9 g# q1 `6 _, o
一.准备工作
3 y) _* Q9 g; H- A/ Q8 ~& W
7 {3 E& W, J! f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
: u4 R6 \% s# L+ v
/ }* s$ e, D5 L% R' itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 @; |' ?$ N3 N% L' E5 h* R
3 F" J& w- X# b1 ?/ Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, v! ?4 ^' ^1 s4 R w0 I: c! `5 {- h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* o0 R) i* ^0 G7 n( X) g
& e1 }( ^0 T6 u3 B
依赖关系:! b4 D5 e6 W+ C
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:8 z! G# G3 l R2 Y& V4 e7 U# H7 ?
7 O5 A/ v( [+ a. z: n( d* |
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel4 ] I2 h$ N6 T# ~4 F
modsecurty依赖的包:pcre httpd-devel libxml2 apr, s* A3 z4 c; h
% Q' O! e1 ?# L( V6 ~1 G0 i4 syum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! E' @! j) D5 W/ \% q5 c+ a3 N二.启用standalone模块并编译
2 I8 w& q+ Y5 Z5 D& y1 p- q, p- _0 v$ D/ l
下载modsecurity for nginx 解压,进入解压后目录执行:1 B2 u' J! u7 R! x8 U
/ T2 R0 S" B9 `/ t$ a Z
./autogen.sh8 \" S9 B& \$ `9 J
./configure --enable-standalone-module --disable-mlogc& n4 P- D& f4 t2 s8 ~7 P5 h2 F
make
' k4 Z7 z- H3 D- L9 i2 \三.nginx添加modsecurity模块
" L( S$ h9 c- l& ]) S+ T$ c2 H* \, @7 @3 n3 }. P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 j* S4 P; W" V0 B, S6 `. D, M: q# i
" G4 M# C8 p% z9 [& B./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
: N/ v" ]' j+ a) I' |" y: Y1 Rmake && make install
' h, y; a; w0 L5 X" v四.添加规则0 [! h) c; h$ q6 ?7 [' [2 w, t
0 V1 N+ N( U' t( g) |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% b+ e" R9 t' d U% u9 C; y6 E4 e: @. j3 z. ~/ c( _
1.下载OWASP规则:
( }; }; ~/ {- |# o1 }, T" j1 o& ]' q/ S
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs [" H; f E7 N0 D- q, j
, N. W/ P- ]" G" X3 z) e4 h8 ]% G9 _mv owasp-modsecurity-crs /opt/tengine/conf/
8 k; I3 V6 |9 C( Z
; D7 x- \+ @6 {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 r$ C, b) r2 [/ |9 S) Z) G4 j% i& x
2.启用OWASP规则:6 N5 ?5 B4 {, e1 e( O+ _6 j$ l
/ c1 q) p" `; E/ J复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( `! H6 u7 G7 E
9 F/ o' c1 [# `0 ~% P5 N- G5 ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
6 u8 s4 y! v4 g0 P: K% D9 }( C. ?' E; h
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
8 J" J4 X0 T3 G# n7 N
& I: S; y6 r3 Q9 r9 ]" Z0 S1 {Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 X2 C6 p+ i& |. F1 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 L; a, v; D8 F/ n& B; m- F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% @% |( V- b! w5 T0 s$ v8 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 ?; O% d# }0 |4 u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ z( u! s$ |0 Q- K& K n4 n& QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% Y- C! y' n3 I) Z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 n3 c6 P+ ^2 S2 Q" y五.配置nginx
6 l$ R; @* _( _) K$ |$ K/ w& _. R# `& n8 C: m
在需要启用modsecurity的主机的location下面加入下面两行即可:5 y! _0 n" }6 J
8 ]( v ^5 r" @. P. A2 M0 q
ModSecurityEnabled on;
3 d3 N8 L: s: O+ c" iModSecurityConfig modsecurity.conf;/ F( a' ]" m1 s
下面是两个示例配置,php虚拟主机:
$ L* d5 x; z2 ]% g
) X, B+ X5 o; h- u0 Wserver { ^) B7 D$ o( V, @. \, C, A
listen 80;. ~, W; h% ~, F1 o2 @2 N7 _
server_name 52os.net www.52os.net;
7 x0 w- `& ^; Z2 v - y; }4 k' w- s' N# {. ^
location ~ \.php$ {, b' B4 ^, x( W3 ]
ModSecurityEnabled on;
9 c, L/ \$ f5 |5 |% X- x' Z ModSecurityConfig modsecurity.conf;" Z$ g8 L. L+ p3 s3 s9 r
$ f* Z+ A, G W2 U, l9 G* E: I" \ root /web/wordpress;3 y2 T" _. v' _( M6 G& \' W
index index.php index.html index.htm;
# x; K7 p! ~# w0 p" P- Q
" `: x# s8 U$ G2 q fastcgi_pass 127.0.0.1:9000;
1 l$ e) c7 B3 `' d1 H1 Z; w fastcgi_index index.php;9 z$ W: m2 x5 l! u3 T2 f. N8 w
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;' e1 o i& F7 |( d% D" E; f
include fastcgi_params;
: K. a' ]) F/ h Y4 D; E }+ H* V; ^5 K' G" y8 W
}3 ?; O( T+ u X9 `
upstream负载均衡:
. ?3 y: s4 w# Q4 y$ `+ ^! V- n! S8 k3 ?# E
upstream 52os.net {
3 e- M' k3 ~+ Z, Y5 ? ` server 192.168.1.100:8080;
! v: O i. z1 s4 l4 x3 o server 192.168.1.101:8080 backup;
! O6 W) y' @0 _. p4 M E& S}4 r" {4 j9 {/ g& Z3 x2 n
0 P& g) m* v* Z; O2 ?6 ?6 }+ t
server {/ p! C! s0 X$ W' q. v3 y) v; G5 e
listen 80;$ A' @# i5 S4 }, M* \3 `. [
server_name 52os.net www.52os.net;) o4 u+ B) F$ [" n% V
, k! ?% g7 d' u3 }+ S! Alocation / {
! w, D- s: N. z: g' f3 B ModSecurityEnabled on; & ^9 H) G, @) t* j( F- k
ModSecurityConfig modsecurity.conf; # V2 F1 g7 g7 L: I, T
% p. r3 o( Z0 |! G" Y. T) I
proxy_pass http://online;
! P; \: C3 A5 l proxy_redirect off;+ ], c+ Z2 Q+ r1 |
proxy_set_header Host $host;& j" B* L3 ^ B8 U) u, ^2 M' h+ U
proxy_set_header X-Real-IP $remote_addr;7 W0 k! z9 ^5 \* O" b1 z
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ o/ L" C! X6 d, u$ n0 ?/ f% O }7 z; z$ O% j6 X8 V9 Q
}
0 b9 `: @$ M3 ^6 L六.测试
/ K6 U) M. Y- i# E8 p6 A/ t' l2 g0 h( D+ [$ A+ c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 t: c( A* |* }3 S' K2 }( t' b3 @4 x
4 n; H* j+ S7 P<?php; ]9 s. P1 Y" B; }
phpinfo();
- U5 `& ?5 \+ `2 ~0 [* m?>5 v* K5 a) J* m5 G
在浏览器中访问:
3 X) E, e$ | ]# l' q, D' f# A. H% y. o! ?6 B {
http://www.52os.net/phpinfo.php?id=1 正常显示。
: P9 h6 |" N* R7 ]1 S# T Z: Whttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ ^' {1 T. I4 ^
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。; \, Y+ C! X* b$ b+ M0 [9 Q/ B
说明sql注入和xss已经被过滤了
% V! Z1 V3 o/ m, V
+ v# { t! G5 q七、安装过程中排错
* Q' ~4 q' O2 @/ }1 j' p" N }1 h4 V6 E( n* N' p* j! m* D7 _
1.缺少APXS会报错
/ v' `. A% A2 A" A* |& k& e5 x( Z m$ @* y. E
configure: looking for Apache module support via DSO through APXS
- n/ I* w" M$ U' rconfigure: error: couldn't find APXS
" k4 V! n5 W# l) Q; v1 w% Sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: Q2 R9 \: Q$ ?2 E4 C3 S; {. g
解决方法:
+ G# b8 U" k* R5 d' V
) E) r2 q: P( k; D; K0 d5 Qyum install httpd-devel" Y9 O+ e: N6 \' m6 H
2.没有pcre5 [3 e/ s8 q( U+ g
) l! ]7 U5 W- S1 |1 e( w" S2 g
configure: *** pcre library not found.+ a5 x: ^ L5 C0 |
configure: error: pcre library is required
* g$ Z4 L i# k8 H' e' X解决方法:" Y5 T6 A, a6 U. I( r! {
- \: U2 a4 Y G7 }+ kyum install pcre pcre-devel% b# d* n" g* `' w( p3 @
3.没有libxml2$ G& D$ l0 Z) w6 Q: N: R/ f0 r
9 ~+ Q! D9 L' u9 \+ X3 v) h+ l+ D, ]# j. C# ~
configure: *** xml library not found.$ ]: K* S/ e% _: i- {
configure: error: libxml2 is required
1 X$ N% R& e% i2 J% j! F i解决方法:6 Q) b* c, W1 R( A/ H* @6 z* E
5 L6 F2 l% N4 G$ J" eyum install libxml2 libxml2-devel, i; a8 l' s* i
4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 n: A8 I0 @& J1 y* g& {3 Q) G4 {) `( C+ E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
: d4 w8 v' H; D/ q/ \* Z7 Knginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! c3 N6 c+ X: k/ O7 M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( O6 ^# y' }) i8 L" S$ p# ^
8 Z2 }5 U2 ^% u0 x! i, _8 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 G7 s5 F# y4 ^7 W5 E0 k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
* X/ t! Q- @% B/ E8 z4 n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( B& z( q- C0 I3 K( ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". |/ v- a% X5 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# X) n3 Q: B- g: Q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 }, r6 C# j4 P4 a, @解决方法,移除低版本的APR (1.3.9)
0 o6 r3 u# E: c! ]9 v9 _$ x1 G7 t, I1 t, C" z- p# Z
yum remove apr1 I, O. v7 x `( _
5.Error.log中有: Audit log: Failed to lock global mutex
" k7 Y( J5 J: V4 E# ]' a+ b* D$ J4 z; z+ S {* }6 r
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 K- T1 h/ b! q, c/ M: ~' _ n3 |
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" X9 `1 @ T7 q+ o5 t( W1 g
解决方法:
9 g t3 Y, a5 Y- ~. M编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 P: m9 B7 w2 N' Z8 N
; k4 t! y1 ?' N' J; R% v' L* ^
SecAuditLogDirMode 0777, M; R* f V6 H: m: @" o" ]9 Q
SecAuditLogFileMode 0550, ^' i4 [5 E4 @
SecAuditLogStorageDir /var/log/modsecurity3 z9 ^4 G6 a; o! y
SecAuditLogType Concurrent5 Q, W% [8 i# \/ V$ w9 e) g: ?
参考文章:% i- M; ]3 D7 ?* P4 e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% X: ^" b9 x% C
http://drops.wooyun.org/tips/2614 |
|