|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 ~! r4 P" Q# M/ }4 B5 }
9 l& R8 `8 t, ]) k# J( s一.准备工作
$ l0 k, o' W9 @
& ~$ T3 w8 }2 b; H# F$ K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' W1 {) {1 I& G3 ?
& g/ n8 W. B& ^* s% I! g& G, Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) q% ?5 [$ c; {1 J: ^$ E
2 [+ l8 a/ h; e: X5 zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( e* p( q* a! C
( F0 N7 N1 B- l0 ~, a
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: K" L1 r1 J9 P. X# S; ^
, ^# ]0 S" M1 E% z
依赖关系: }1 x' t$ b2 _6 c; H" i$ A9 _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ Z. y" ?. I, ?0 V7 ^/ P$ P" o! I$ b5 Z z& f6 y+ ~& O
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel/ T$ h% j( i6 _
modsecurty依赖的包:pcre httpd-devel libxml2 apr
& z* m3 m' J& h @% K+ \% A; F) |& E, ?- x. F& [
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ K% `2 j1 T4 S9 P0 R5 v$ b( ~. x+ N
二.启用standalone模块并编译5 {- U& h$ r( B
2 k4 D9 y/ h$ r0 I下载modsecurity for nginx 解压,进入解压后目录执行:+ Q/ {4 X& }& R3 C+ `7 c- \! h
+ P b( O$ X4 z# M
./autogen.sh
& e% q* x, ^( Y9 I; M/ {( k) }./configure --enable-standalone-module --disable-mlogc
9 v8 r- B" v: l$ I& ~( ymake d' g, w9 y: S
三.nginx添加modsecurity模块- L, T1 s* K) @' K* V& v6 p
6 P# [ V* k# R" a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# {2 @4 c4 [* ^* r) i
2 R! Q+ O! q. ]+ b./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 t ^* y: _1 {9 v
make && make install
* f% N8 t# b' L" J四.添加规则 \( `' e2 p* w) r. B2 A
; v! z4 _5 m% c! i- |1 P [$ E# N1 [
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( P8 E% X8 g8 H: t
! l2 o; }9 O* h$ ~4 v
1.下载OWASP规则:% G* L v4 k) y g: G: J( A# }
( a8 P/ ^8 M/ E* H- q, V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# D- X5 J- U) r2 J
; p4 M6 o. t" E9 vmv owasp-modsecurity-crs /opt/tengine/conf/7 g5 { {/ Z1 i! g) t& h" A, a: Z
3 Y4 V7 u2 e6 U1 E/ lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 D/ y% L4 _. E! m* G
2.启用OWASP规则:
3 `+ {8 l& w- m1 a" m- d" b, ?7 M. p7 r [9 X5 W) C' U6 Q/ T y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 ~; s+ l1 r' {- [% J! d9 ]; J
9 M/ ^& v, j P8 u2 p* }编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ q8 e; Q' @# m/ u
" h5 S, I6 D- f0 m
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 A+ r( _ q8 J U2 |
; {8 |+ S- @! i% uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! |# ?) l, D }) ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 [$ t) u) i4 g! w4 N2 P9 iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ _& n! b8 Z, ^6 Q6 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 g3 K+ B; }% e% a8 \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: @7 t8 m: |1 L" p8 P+ T) Q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 _& K: q$ ^0 n9 ]+ l# K
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
y B+ {( t( ^7 k五.配置nginx
- ~/ o" N1 V, ], k [
% \) t$ E, I/ V$ s$ Y在需要启用modsecurity的主机的location下面加入下面两行即可:
5 I+ l# [ ^! T
9 C# q9 |% `0 }' ~& k' _% d, b6 ZModSecurityEnabled on;
& i/ A8 ^$ V' Y& e( qModSecurityConfig modsecurity.conf;
2 v$ R; N2 h$ a/ P下面是两个示例配置,php虚拟主机:
( f: E! }* c [' E8 C% p
( l ^% W: S/ V& R8 o) q! z. |server {
1 U4 v+ F2 L8 d8 M$ w/ S/ e listen 80;* Z% T) e [9 D- l* o& H
server_name 52os.net www.52os.net;
/ M' W4 N9 q0 O( p1 U2 u ( w! k; K' G+ o0 [9 B
location ~ \.php$ {. [6 o, O9 R0 S$ l' \6 c2 W# w
ModSecurityEnabled on; % I; l, Q" n/ J
ModSecurityConfig modsecurity.conf;
. A1 L3 n+ X0 S1 ?9 M
! s9 [* X1 l/ j1 P D( o: w3 i4 ~& I* K root /web/wordpress;, i; l! S# L9 R7 Q( v2 `8 [2 Q
index index.php index.html index.htm;" u" w- {5 L5 p) y
4 h. g# M1 v* ?/ K5 p, T% L fastcgi_pass 127.0.0.1:9000;
+ b G! H% ?" s7 ~ V' ^7 w fastcgi_index index.php;8 k( t4 ?; F1 w% O5 e* Q4 w
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
" F& k4 \+ \9 [3 c$ H include fastcgi_params;
9 t0 k2 o" u8 T$ _$ y5 A1 D* p }
( B2 w1 ~3 L# {/ U; k; o( g }9 ^2 a( s8 x8 i
upstream负载均衡:1 L- F, ]+ ^4 y# G
* o; A5 e. Y0 s2 v& a! Y- j Qupstream 52os.net {
8 E2 t$ L) X9 Y, F+ c; \# { server 192.168.1.100:8080;
* m+ G& r! A: z' i& F. w server 192.168.1.101:8080 backup;
1 `! X4 J1 t$ F# Z& I$ C}
# S n4 {- i0 N5 {* U9 E
$ g9 `" W; W( o% H' Q5 Q# H, _! Sserver {/ ~# s$ g4 T: [! E0 h, a+ k/ T
listen 80;
, G! ?2 X& [+ i. b# vserver_name 52os.net www.52os.net;
* E" r9 Y( E9 A0 Q1 _* ]7 C {* k0 S; A$ e( _9 \
location / {
# N, Z' D8 l G& v/ j" ~' N5 W, l, K ModSecurityEnabled on;
+ B) J+ t6 G7 t5 d/ d# ?3 q, K/ ] ModSecurityConfig modsecurity.conf;
9 A0 [/ Y# S' R! z' t9 G
! ?* M; C% t- u: m proxy_pass http://online;
6 E& X) Q2 A; L% a proxy_redirect off;5 ]8 v0 [# K+ d! b: R
proxy_set_header Host $host;
" G4 t, i( }* t2 ] proxy_set_header X-Real-IP $remote_addr;4 f- l. y2 }' m3 \
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* J, O w, q: J5 j/ g+ c* j
}* f* ~0 Q; G1 U7 G2 Z
}
4 O" S* f# z) F0 N3 K1 k六.测试
, o, {* P3 X ^
; D" _" V+ D J: J& E我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 j0 w1 `, e* }3 O
5 t# r4 B7 p, S<?php
6 D4 E4 E! Q* [- u* D% C6 q phpinfo(); 6 ], E1 n6 @+ s" r1 @) I9 |0 U5 p
?>/ q' }8 c* `7 |8 J" a$ z
在浏览器中访问:* P3 W4 `+ n# N% A7 u
5 q, d$ g/ _6 t$ m- Q: Ahttp://www.52os.net/phpinfo.php?id=1 正常显示。
8 q3 J" ?" q, d" N+ \2 {http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& @, d$ D0 f4 f% Bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
' e# N# |" l* x* t9 Q说明sql注入和xss已经被过滤了2 G1 |# G. f s0 k" S
" A( U* X* |1 I: G! R七、安装过程中排错' j6 s; E, h4 Y8 m% J& Z
: g! q X/ ~7 n; @" V3 p
1.缺少APXS会报错5 K2 X8 L5 A' D
1 i. x' R3 i C+ Z8 @0 _# @( M& {
configure: looking for Apache module support via DSO through APXS8 S' d4 b% o7 ? O- V' ^
configure: error: couldn't find APXS4 q# L; d3 L& Z' t; _
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' h* r+ Q. C' z1 `1 O; E! t' x解决方法:
1 K0 t; z+ R a0 z5 K+ L& N2 s
* v y: M1 Z$ Y" T9 zyum install httpd-devel
* W8 F3 B" m5 c* [; E, L1 M$ ]2.没有pcre
! H5 a$ G- g: R
, f: S U2 g, n o0 x4 {: Econfigure: *** pcre library not found.: u* J, f5 L6 E5 j/ g
configure: error: pcre library is required: p+ e& i: Y9 ^9 q, z. W
解决方法:2 v0 z l7 q% |2 ]- `6 K; g
( o8 @, E2 C) ~5 X( }* n
yum install pcre pcre-devel% @" Y Y: O8 F
3.没有libxml2( q' i6 p! G: Q a' V6 C! d8 r. `
" B- J0 ~3 f6 Z% t1 a+ I. f+ S+ Q8 i. i, A
, d- j# [) B* B% \: s2 b1 [
configure: *** xml library not found.% ?. V8 |, o( A) c
configure: error: libxml2 is required. S d# c2 h( ]; q
解决方法:
: J/ ^. T g& }; z& U
0 _4 e7 t" z8 w/ e) ayum install libxml2 libxml2-devel2 |! ~4 g$ a. l5 V w- t
4.执行 /opt/tengine/sbin/nginx -m 时有警告
' M7 e4 l- W; \* n7 n2 z. X2 i9 A5 q
& o: m7 J$ C: Z2 ?+ t! ITengine version: Tengine/2.1.0 (nginx/1.6.2)
. m k7 u! a: ?: m% }$ y3 Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; v+ H1 @$ w4 e原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) @+ C1 n+ q2 M
% w4 V$ A( ]% x. U, H) Z1 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( p. f) ]8 G6 L5 h# z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"2 v# i+ Y( H7 k2 M: s
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% R/ K9 G5 b% D! k, z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ `. G- `: q' }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 T2 }: X2 b: n, P5 H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 B0 ^: I' }9 e/ l- M解决方法,移除低版本的APR (1.3.9)
$ ` S7 r# a3 v+ b5 N4 o) d
. D6 T2 ]4 j% ~) X) zyum remove apr' ]/ j1 j' H G( h
5.Error.log中有: Audit log: Failed to lock global mutex6 k; `5 ^9 z8 `0 f, Y- o
+ m; ^& A2 w* R
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* {6 j& U9 I0 J" m- `5 Cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. \2 [1 H* y5 X! ?
解决方法:
2 A$ i9 n/ L! c& U- ?1 G+ M9 u7 h) g编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容: t# w- ~3 K$ T. [ X
$ F, O, ^+ P4 u* i
SecAuditLogDirMode 0777& i" n" [0 g# J6 R
SecAuditLogFileMode 05501 f# t. [! R/ P# F
SecAuditLogStorageDir /var/log/modsecurity4 d& x8 A5 z# _- |
SecAuditLogType Concurrent
3 l7 a& H, y6 ]参考文章:
1 F# {4 M7 O: R+ R, }. \& g0 |& uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, b; {0 r E2 K3 S& g+ Q* D
http://drops.wooyun.org/tips/2614 |
|