|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 K8 K3 G) |: J8 s6 _6 x: R0 [6 M3 Y6 B( I$ y
一.准备工作; A" i3 s |3 [8 u$ @$ s$ H
# U; y* s5 S% P3 ^ @1 S9 {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! u9 a J" `! i; C. p; z* G5 y+ b" a: C |$ h) c; A5 T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. |5 B6 l, D, Q: Q( w+ Y+ _ o" f/ E6 A2 n8 E: l* B3 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; l1 N- \- [! y: a; x& \/ _ e6 k3 h9 k9 w: Q9 J, K/ V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 d$ A3 h' S$ c% \0 @9 k& H
4 r, p$ M7 n) T5 g S( c" \/ b& e
依赖关系:: h8 ~# I% k: e q5 N4 P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; K) a/ P4 o u2 r! p
, f- D+ O; S1 E# [, Q/ a. uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel0 ^# M5 {& {3 J0 i C/ ~* e
modsecurty依赖的包:pcre httpd-devel libxml2 apr! A/ B# Q2 I# C- s: _- q) j
) F6 E9 H$ n& N# l+ b# L
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- N- J0 Z" x$ t! X二.启用standalone模块并编译) L5 p1 p; X) t* a, [# u$ U
' h( `7 I/ D- r' S8 `& r: ~
下载modsecurity for nginx 解压,进入解压后目录执行:
* b( E/ T7 ^; s: p/ U1 f: a1 |) r( ]- w/ G# V
./autogen.sh% n0 E1 j/ V8 c# d1 }( }0 y2 l
./configure --enable-standalone-module --disable-mlogc# T6 y3 f: O Z0 {; @
make
3 \3 j: e) |4 c0 R. F2 w1 D三.nginx添加modsecurity模块
9 c" m$ T9 Z0 X+ ~& S$ S/ r" D. V, l# ~; A4 s4 ~2 E8 i7 Y g2 M
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 F8 m: R/ D7 Q* H9 B9 }6 f! F4 { Y+ {# J) B1 O! Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine- x0 |9 B4 R T! P
make && make install
* |, f3 ?( L" n3 Y四.添加规则
! E0 N+ D& z2 W0 L
3 p6 H7 \( j0 `% Imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ J" Y0 a, g3 ~9 @+ G
3 _* R# |3 _* a5 {- f
1.下载OWASP规则:. a8 }# ~( C/ U C
* _' ^: y( ]1 { y" L! e& H7 y/ t6 Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& k/ s& v7 Z$ @( V
/ L' I, l/ j% j0 Lmv owasp-modsecurity-crs /opt/tengine/conf/1 @3 [' Y/ n5 w
+ p4 [# c8 l; c' g5 a
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. J: `: P' | _2.启用OWASP规则:
" A2 r& i0 K7 o" W' S1 B
4 L# }+ d# i: _7 y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 [: [' l6 u2 ^2 J% b
+ }# W i6 b, b% @3 V. _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ `: o5 X( y4 J7 i9 z1 c
Q7 }; m- P+ k0 v$ r5 a. {0 ^owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; x# A" C; X' f. P! w% i
& N' e9 d y% w( `9 P/ I) U! [+ @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% e$ a. a7 N- S! G7 J8 Y3 [4 OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& X$ Z) I& c: A1 B1 TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 m2 t6 t) ^+ _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; Z4 W" q, u0 W, \7 }' U3 j. \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf% e: l: S& D0 O) ^, o& j Z& Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ t' |8 d8 o! j9 ~, w! A; ?0 AInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& l9 C9 r4 T& z2 `: R1 v5 m! v
五.配置nginx
4 o* w/ m3 c/ m# j
}; P( J) k" e, x在需要启用modsecurity的主机的location下面加入下面两行即可:
8 U# R7 z) g, u7 [- p7 M* v; b7 p9 }8 `: R7 f" E
ModSecurityEnabled on; & I/ A( U& d* }! b s
ModSecurityConfig modsecurity.conf;
: |3 O; ~ t: }: F下面是两个示例配置,php虚拟主机:
9 o8 C* T G2 H* g) T
. |( g" g# a6 gserver {
# A# N% b0 Y; {5 l$ r. y) ? listen 80;. t. I, t m" Z- w# z
server_name 52os.net www.52os.net;8 B3 l% Y: k1 I( l
( V$ }: H4 n7 v" H/ p; m location ~ \.php$ {
3 Q* t$ O: y u( A, G ModSecurityEnabled on;
$ k* X9 ]) j$ q6 K) ~ ModSecurityConfig modsecurity.conf;
- k4 y$ Q t, ?: H* J* M0 m# l
1 k/ r" @/ B4 ~% a; m9 S root /web/wordpress;
) s+ b& r! `$ b& D, D* b) V7 t# `5 \ index index.php index.html index.htm;
/ k- o* l1 d. f) h: o! U( \ 7 N% C. S F9 r5 D
fastcgi_pass 127.0.0.1:9000;# _& _% R$ H8 F) y( T0 b( \
fastcgi_index index.php;' l' B3 l2 S' j" U A3 l, p0 M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# J. F& W6 t# F1 ?
include fastcgi_params;. c. |+ V7 W) Z( j' ^
}
# `6 v, g. d8 ]7 F! [9 a! k }
: V/ e+ P4 p3 L+ w" R2 e6 vupstream负载均衡:
4 z& q6 Y+ ~* z7 _6 ?& F3 H% Y+ z2 z4 K3 Q4 C% h: T
upstream 52os.net {; k# f! q5 Y0 v2 v6 F
server 192.168.1.100:8080;# \; K0 W9 j# K
server 192.168.1.101:8080 backup; Q9 x# j W0 X
}
4 d4 f* P* g) d: l8 N% D& C0 |4 _9 m
server {
) r1 I' f+ Q k% l! i6 nlisten 80;3 U z6 s/ }, t2 S1 T
server_name 52os.net www.52os.net;1 t% r1 ^8 { N" N& n
( K1 u c1 v1 ]+ _& g5 R& |5 r* {
location / {* s" |4 w) O( |9 R% N3 c) @5 ^/ B- h
ModSecurityEnabled on;
* y4 j3 o* h3 o" O, v+ D ModSecurityConfig modsecurity.conf;
/ W+ b! d# w; h/ N8 ~1 R$ }) ~% n* F, ?, i8 ?5 P! `/ x
proxy_pass http://online;" H {/ h2 T8 B0 J0 }
proxy_redirect off;+ H1 u) ^" @. R6 h& R8 m# y* f
proxy_set_header Host $host;3 b0 n' r1 M& z0 V) e
proxy_set_header X-Real-IP $remote_addr;
" F* g: v U! S4 M2 Z* q3 k proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 r# l2 J! S5 K3 n; j; O3 p }
7 _$ z) \3 y3 N& r& U1 t}
" l* `7 R6 ?3 w% ~六.测试4 ], S. f7 D, k' b
2 V2 Y! n! O2 O# k# l2 v4 e) _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: Q/ B U6 ~0 f+ F( z
- c, z6 X5 |0 a- _<?php
" A5 M1 w2 O; Q1 V phpinfo(); 0 \9 R m' y0 K. }
?>
9 K, O- P* ]2 B6 w( L4 y在浏览器中访问:& c( Y7 m. p8 a5 i; D: l
4 \2 F7 C1 D: p7 x! P
http://www.52os.net/phpinfo.php?id=1 正常显示。4 R4 o6 `* z* g8 I9 w, i4 q; f2 p
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! d, r6 e0 t' g1 K% C$ U9 q7 v xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, b. F2 l% w" o! t, T说明sql注入和xss已经被过滤了
) A" G: A C7 |, g
8 E- k- w' D- l) G8 z7 ?4 i七、安装过程中排错
( H: A$ M- }! F0 p( R' Y: ^
, Q P6 J) E: w/ Y9 r" U1.缺少APXS会报错! y: z9 `7 r4 F! R+ R" }+ [' \
: o/ W7 Z3 z: h. c
configure: looking for Apache module support via DSO through APXS- }: m' J$ u* k$ J- v. Z
configure: error: couldn't find APXS
$ q! q1 l0 f, C( P. Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 l/ J6 `8 G& i6 Y; B+ Z解决方法:
% i+ F2 {5 e& T6 G+ ]4 X( G# t# e {! L* \. c! Z
yum install httpd-devel) c9 O( `! B' l0 j
2.没有pcre8 f3 c! I$ c$ [4 s& ]# e
4 ]+ Y$ }5 d: V+ G: h: u3 ~3 yconfigure: *** pcre library not found.1 X3 i" H) ^# ]* _' [+ J- n! i
configure: error: pcre library is required
' |" n$ r) Y7 o解决方法:
, I; H! t7 p! C- q f, H8 R$ \ z3 i: f% F
yum install pcre pcre-devel% S/ P4 h) W* w/ @
3.没有libxml2
+ D: c' e' [5 n6 U8 M& E3 Y2 U0 n( h3 n% z
1 J q# t8 ]' ]: z# H
configure: *** xml library not found./ `* D0 Y/ _ x
configure: error: libxml2 is required
- _5 a% ^! ]6 ?! p解决方法:# i; t+ Y. q# ]' ?
, ]0 F [3 }2 W$ D; k$ e
yum install libxml2 libxml2-devel: X5 [% Y, i* @6 y# G, [ R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 S( r" U1 A0 Q" [% e6 C) x: c. }0 H, w1 }5 f
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- e B' ^! C! s) U' `7 q: Snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 \- D4 u" k6 J# l5 V/ m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ v' v- w3 W2 T2 u1 E
D# P$ K0 c9 z% [9 A+ l, Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! }2 V0 K2 i7 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
: R3 p2 I4 O/ C, S# f2 }! l2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 [8 R# D3 f, F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ @5 R4 F R$ [) ]3 ^7 n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 g# g: b8 ~" q# A0 V9 h4 f2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' ~5 [* W+ g( P/ U; l" e解决方法,移除低版本的APR (1.3.9)
0 b5 ~0 b. Z3 c
; C7 d2 m$ w: h# ?yum remove apr& V) z; T. v3 ^ S6 X c
5.Error.log中有: Audit log: Failed to lock global mutex0 r; Y" J* d3 o* \8 S$ z2 v$ e
/ x8 f. a, }; q. \3 s9 N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! @% h2 I, U" i8 K% S: R# P+ gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 b/ e3 W9 G+ E解决方法:# L5 v8 n; s$ ~7 T5 r) A. o; I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) c! P4 J' H/ ~0 R* r) w" g
4 i H6 f2 h4 ^' `, E
SecAuditLogDirMode 0777: c" p9 S& Z; o; r% }1 Y2 n' J
SecAuditLogFileMode 0550
% }7 H; a2 g+ A2 e0 F) ^6 CSecAuditLogStorageDir /var/log/modsecurity; E' E" s# j7 @3 A7 j# X
SecAuditLogType Concurrent
1 ]& P7 o& G5 T7 w参考文章:3 M+ s5 M- F0 z0 l% {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 j! i+ } s9 a# w( Ghttp://drops.wooyun.org/tips/2614 |
|