|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 w: x! f3 s6 j6 R; Q
; q. `1 s/ }; e
一.准备工作
7 E+ r/ Y4 [+ r( q0 K' f9 p
9 t7 S- {% ?8 k! `系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ T0 Z ~1 _1 i+ i) l
) M3 P& {8 Z. _. j1 M: Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 O% n) h* P* t8 n3 B7 I0 u& M- Y" s# r
8 ?4 w+ E8 i* d( qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 |$ ]" {5 i( Z
) `5 z6 ^7 z8 j& J/ l. s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! a, w+ U) R- U
% d# N6 y3 ?9 g* i9 A. m( d9 ]1 A+ D* k
依赖关系:5 m6 C0 E2 x0 M G& v2 {5 W/ q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) G; x7 ?7 N2 `. i" P! _8 S) A8 ]% X! w% [0 {, _, y" V
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) F6 C5 p( d8 a
modsecurty依赖的包:pcre httpd-devel libxml2 apr* J# ^8 j. |: j6 V$ ?! m d
3 q L# m/ n3 \; M) y$ V8 cyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% T. Q' h* r7 H1 ]# y
二.启用standalone模块并编译
9 o: l7 n& X* ~1 Z
+ ?8 T/ ~6 _6 m/ u! ]/ V8 O+ W下载modsecurity for nginx 解压,进入解压后目录执行:
9 J. H4 d9 K; q/ i9 B( d6 b% r3 W$ ^$ h1 X1 ^, H5 t
./autogen.sh; d" H# k) }0 {' J; ~7 S+ D
./configure --enable-standalone-module --disable-mlogc- H! J% X& i* F9 T1 q$ |, {, I
make ) L6 h0 L* l6 M7 J. E
三.nginx添加modsecurity模块
% v# W, N7 A9 E' f7 M
! B, L) g% s, L在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( [0 G. L- _: f+ |( j' w
# V3 }* k* T, S6 b! _3 P0 ^% j./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! t a7 h6 R9 g1 D
make && make install
: [% l2 d5 R4 b3 r5 p( j2 F8 w四.添加规则; ?# X; J* M( W0 ?
], _: x4 N" F0 G+ P7 [( d2 Rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
\5 a) q1 M8 _2 k; w [2 P" v9 T
" d* |) A+ X+ [* A1.下载OWASP规则:
" Y2 M8 |. P& z* c
8 [/ k7 C6 H# c5 s% Q6 q* |git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
T! A+ s/ _( M) \% T( r) }5 b: h3 l& I% u; {7 T
mv owasp-modsecurity-crs /opt/tengine/conf/# h) @7 ^2 N# r! @( V$ y& q9 q
* i. {) o% i) \4 k& T! ?cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' N+ A9 d: m- h2 e6 }; W2.启用OWASP规则:
' v/ h; M: k5 h. H+ ~. V% I$ t/ }! i L; W6 ]# M. L8 R0 t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& E- j) Q! j8 d5 G) g. `3 H3 l. L/ a, ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% @7 n: ?' C& ?& {7 x$ E
1 D$ @; W7 d* q3 A
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' }( u3 V( f( ]3 P) `1 K: _, W8 s- K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" a, p4 ?% o: W% Z* TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" }5 T8 R6 V( h/ r; r; \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# T! b. Z \& X" V, e) P/ H, A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' K3 q4 \4 g. A( b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* w. F; f& j6 g3 v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 W; M. M+ }2 }9 P0 z* |+ V5 U8 aInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf O0 i, S+ S1 f1 V* q/ Z$ b3 [6 ~
五.配置nginx; e+ t; j/ B9 {+ M# Y9 _" H% F* g6 Q
! T8 z3 b+ f- ^, f在需要启用modsecurity的主机的location下面加入下面两行即可:- O4 a: u) W& u ?) Q( \0 q
3 S# J3 ]: D- D( dModSecurityEnabled on;
( x5 L3 B; d. zModSecurityConfig modsecurity.conf;6 v! k4 W' h' U2 d3 J
下面是两个示例配置,php虚拟主机:
' }7 r& W0 U1 m3 o! d h
) Z" _; X! `+ x7 Sserver {0 m2 \" t5 N5 f1 v
listen 80; X% V) d# p% h' V H
server_name 52os.net www.52os.net;
2 d z) y: A: }, P2 d$ X
+ S6 W3 x0 ?: S7 d$ {" {* K location ~ \.php$ {
: [4 e/ Q: Q' G9 { ModSecurityEnabled on;
+ W" Q/ k3 S! [ W R& B# f ModSecurityConfig modsecurity.conf;
1 K/ `: K7 z, _' n0 M. B9 H! q. f+ _. Q1 }$ @2 h% R6 V' i
root /web/wordpress;. O1 Y+ p( t4 J; t( s4 }! b- y
index index.php index.html index.htm;
& U y4 J- c! _& y/ i' q) M3 E + q- A9 W+ c0 n7 Z$ U7 ]
fastcgi_pass 127.0.0.1:9000;- N) q6 M4 o. B2 |! j7 ^
fastcgi_index index.php;
& D7 u" e) J: q7 X' {) Y! K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 H/ u. S) A# v9 t
include fastcgi_params;
# B$ G! i i, ^) }# O0 V }
, F2 v4 V: f* \$ h }
& k1 T$ Q" e$ ^6 _/ {( `upstream负载均衡:' J$ H* P% M# e1 o, l' h
/ ^6 ?0 F; C, e: Y, j) X
upstream 52os.net {( a% E$ f& |" a4 e1 Z# z- n. b6 g! _" N
server 192.168.1.100:8080;
) G- c$ r& Z# P9 e% f1 ` server 192.168.1.101:8080 backup;2 ?2 f% o) M1 W& Y
}
% z3 h3 g! [% l- ~5 c8 j- ^, l7 F
& r6 m# L' s- n5 a' s& Zserver {
6 L' {' d3 {: { P! c2 Alisten 80;' k; _: G: U3 O$ o. Q
server_name 52os.net www.52os.net;( E8 ]. c1 z( n) Z6 x
; `5 W( {8 D0 x8 ?+ _% K* |% s; z
location / { o) R& {1 `6 w( s
ModSecurityEnabled on; ; \0 V7 C) C; t- R5 Q
ModSecurityConfig modsecurity.conf;
2 {+ J7 _: E2 r# G: g" e- U6 P% m; n
proxy_pass http://online;; G. M! P, E$ d
proxy_redirect off;
3 [: ]- y' D9 [9 M6 d proxy_set_header Host $host;
" `2 M7 p( Y1 B3 a$ ]8 U& @ proxy_set_header X-Real-IP $remote_addr;: N, n; V, J, I- l+ F
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 x6 T* p N) ^: j6 ?' r
}
, ] V' `: D. i9 h+ P}3 h, S7 h# X z- q7 e6 d
六.测试5 B# _% c5 I0 y4 Z! q a8 `
0 a% z6 `% y) O o8 P5 D- L
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ u1 Z4 H: b V! `& D1 y9 a, U) |6 |
7 N! `$ S4 h; R2 J( D! h: w/ J1 p<?php
9 T8 L9 _2 H$ l& H$ Q6 u0 Y phpinfo();
$ L" l0 N+ p2 I& S?>$ m1 A% I7 f% W( {7 ~2 d/ \1 J* |
在浏览器中访问:5 O, G0 Z- ~$ {4 a$ f
& Z- M6 W$ u9 q4 p2 Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
9 t2 G+ H2 y) l1 D. e9 r! Ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' u E: W9 h; thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。4 l3 W( y5 `; k7 I& a6 x7 X4 A" N
说明sql注入和xss已经被过滤了 h# z% r. P2 }, d! g
+ L$ F! v# R9 E- i, ^: r8 p# f' k# ?
七、安装过程中排错
( S/ A7 X$ ^; k v! u# q: g8 Y
- t) x' S5 s% Y d6 k; B# V, r1.缺少APXS会报错
! o& F E) B ]# N5 ?/ `9 X
# A8 _& g7 L1 V1 r. K' ]; Uconfigure: looking for Apache module support via DSO through APXS
8 g7 }7 S7 e7 e U/ X6 b. [2 Tconfigure: error: couldn't find APXS) a s; ^& H( |( a# {" r
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ `* f: c$ n4 @1 h+ }
解决方法:
8 f9 g( b2 [( `3 [6 m2 t! V# `% n3 P' f8 h+ q
yum install httpd-devel
. v) x+ T1 f- X2 c2.没有pcre
! V1 Y$ B( z6 B1 y) k/ w" C5 |# A2 ~, t. d6 U0 m3 Y4 M
configure: *** pcre library not found.6 E \9 A8 E/ g" j7 }3 h# B
configure: error: pcre library is required
' L' d2 E7 v' z, E$ _解决方法:5 Y2 ?' j5 e9 b, ~4 l+ D
3 W3 p& ]) ~& r$ R
yum install pcre pcre-devel
9 c$ Q; m* P# `. {# }3.没有libxml2
d/ @( y3 N7 y: ?' ^7 m2 c" t7 V& a1 B) l/ ? H! [
% W4 R1 W# T2 L7 Z
configure: *** xml library not found.0 |. H9 J. Q a3 S$ _% ^
configure: error: libxml2 is required
1 ]/ H1 c: w( ~1 P解决方法:
3 r3 Q, J! q. e
2 E, r$ _1 N. @7 V; Jyum install libxml2 libxml2-devel
9 u( a, |0 s7 ^" H" T: q8 J4.执行 /opt/tengine/sbin/nginx -m 时有警告
) D$ }- ?; z5 H) ~. F
) t9 t% R; X; zTengine version: Tengine/2.1.0 (nginx/1.6.2)) L: y9 X; a8 ~9 V! h7 E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
\1 n5 b8 w2 z1 {7 W原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 k# M$ r; s" q+ e' z. V
5 V4 Z7 P) h& z3 J8 z# n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 L1 A! U: [7 `( S) A0 w0 x- H/ E/ r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 U, u; b, j% h2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 C( n2 B8 Z) Y- ?8 b6 t8 e+ Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ _( i7 t, \- |6 V& l6 o0 g3 }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". `5 e: u" `0 W( B8 U* j) ?
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ R8 l/ b K4 y; x( s! v/ K/ \
解决方法,移除低版本的APR (1.3.9)0 O( z" F) p; l. Z o/ A
9 {; Q, C4 l G' O$ a0 {" T# q
yum remove apr
* P& g" z6 _9 r- q$ I0 ^5.Error.log中有: Audit log: Failed to lock global mutex1 K1 n5 ^$ G5 k9 t- N/ F
! A, q" ^# [- E6 f% H7 S6 N' z- E" {
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ D: T% n1 A& L9 kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! @# d4 b. p" d解决方法:; T5 W9 ?1 k( x) V, o! L' K" f) p9 M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- A+ d# |, @/ }% \0 g0 a
; v8 i* q/ k$ E! X& n: xSecAuditLogDirMode 0777
2 D3 A' i5 q) D0 {+ O3 {8 `! }SecAuditLogFileMode 05509 U) i. j. R! H. o8 ?6 h
SecAuditLogStorageDir /var/log/modsecurity
! u3 {3 g8 d7 @* X! o0 f9 lSecAuditLogType Concurrent1 a' \% W6 m; a" z2 R
参考文章:' n/ O* v6 j" m# m& T7 A7 [. z; G6 G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ e2 ~$ i+ _8 {2 D7 _http://drops.wooyun.org/tips/2614 |
|