|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 \$ d# {& J& a; Z
P3 X" {; `9 ]: P% s% M一.准备工作2 Y9 h, y' ?! t
: h2 H* c8 s' [% k5 x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) U/ Z& G4 C+ O5 W, r0 K* I+ ~" c
) F7 Y* ]! ^. Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% P0 h% d& @: D& {" m2 H- S( d# J
1 Z! P5 A z6 e% Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 l( A! X1 Z1 c: q8 c+ D
2 @% j) {; {/ G+ m* ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ Q2 }* P* x/ q0 `# n
6 d8 J) p: Q. h+ `8 s
依赖关系:0 D$ Y, C4 F( w+ V r& E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 r; B6 T, E* }. ]+ M5 N
; x, P4 H ?- ?* w7 l/ K# p# yyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 A1 k0 v* o0 n) P& A& {7 C* R
modsecurty依赖的包:pcre httpd-devel libxml2 apr" v2 h! r* ?+ V; n$ ^
5 w" C, ?) y) L. B) Y- iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) j( Q" v' g# Z3 |' f- O$ i6 y二.启用standalone模块并编译
* f7 q& K2 p0 n: j* k# L& ~) l* n, y; J( ?
下载modsecurity for nginx 解压,进入解压后目录执行:
5 j4 g6 i6 L2 x7 {3 c6 _5 O* ~9 j. A7 i9 P, K
./autogen.sh, x1 J% F. s2 v3 Y9 P3 a/ P; U
./configure --enable-standalone-module --disable-mlogc
5 h$ N2 L8 \& ^# Z0 i3 V7 hmake ' ?, c0 c* ~) n, p# V. s
三.nginx添加modsecurity模块% T4 ]3 s' E/ E
8 }: g; m, E T
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 x3 R W1 X+ h3 l
& M3 P3 C) L0 ]* Q" v. u* ?4 R/ ]
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& N% c% [7 _3 b1 ^; Umake && make install
* d, y. e& |( f% o# M; Q四.添加规则
% x3 j: F# u# \3 j4 E( y2 K! Y4 j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 G V. g; U+ T: P8 O
2 S, J# p* H( o2 t5 x X1.下载OWASP规则:4 J) k+ E" i% X" m+ D4 V4 D
- E4 o" o9 X9 y: K$ M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 P9 Y( w! m! p# [
: Y9 t& M, s) M
mv owasp-modsecurity-crs /opt/tengine/conf/8 t; v( O' G0 w! F) g+ S% E8 W+ l
0 P3 Z, h4 i# @+ Ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 s* ~- _3 M7 v- _
2.启用OWASP规则:
! c6 B# \0 U. P( [8 {
5 l. i% A3 V ^0 H+ t9 A' B2 m复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. W6 T- ]$ I% ^# F8 t% n7 y
T" Y( N( @: L+ R9 ?编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( p, q [/ s9 J' C9 H8 [- z" L# u3 J
U/ ]3 V1 b$ cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
I8 `4 y6 W# K$ I1 C
& N% Y# ^8 ]; M) }3 Z# rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* n3 y/ G; E. `) ~+ {6 ~$ f) b! n5 c6 ?( ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" D1 D" ^. }/ v# e* ~ L$ `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ n4 ^3 e5 U) h8 U' _( WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 P6 t' `: |9 g. k" D2 {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 g5 H9 ], ^# `% O9 A$ d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# M3 \0 |: H9 ^# t) u0 C6 B: }Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 }) l3 a) U5 Q/ }
五.配置nginx! H- N9 U6 s4 N" i$ _
0 B% u5 b" a1 E+ T) K! A# a/ z' @在需要启用modsecurity的主机的location下面加入下面两行即可:
, f& f" {% s+ E" k6 ~% t" n" U% ]( N c W% ^' \
ModSecurityEnabled on;
; L4 ?+ f2 J( f D! ?ModSecurityConfig modsecurity.conf;
0 h# |( \& T/ u4 C+ ~$ g! P下面是两个示例配置,php虚拟主机:4 }& `- T" w, c ^6 o- o: Z# s
" P8 O, p% n! K3 O) vserver {
9 u5 |, V4 w2 ]1 {3 {7 D listen 80;
0 |7 L' P5 V' U, w server_name 52os.net www.52os.net;
0 e6 @% @; k/ y5 l1 L4 j6 D
% p! ]9 l: u# E! e0 m location ~ \.php$ {, s; c. G) j% e: E0 a4 b
ModSecurityEnabled on;
8 h: M1 F# ]: r1 e/ t- [ ModSecurityConfig modsecurity.conf;/ p+ A9 w7 F3 X3 I$ c
X- S) p; M& n2 h9 h
root /web/wordpress;
1 q- |: D& m& O/ ?1 e* r1 U' K5 j0 p) Q index index.php index.html index.htm;
9 @2 X9 S) `" T% i 6 j. V( n* v" ^& l. R
fastcgi_pass 127.0.0.1:9000;
: m% m7 F L$ u5 i$ Z5 Z fastcgi_index index.php;
! C; T/ s' t# G; \+ x fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( \' K+ ~, O8 N" O% D
include fastcgi_params;. o. n2 c" _6 _7 W7 j5 J: E& s
}1 ^4 h) R7 z- d; j
}
, n, B5 ^5 m+ c/ F' D( k4 F/ q7 Xupstream负载均衡:2 r T6 l% o; Z! j
: K1 V6 m1 ]( lupstream 52os.net {$ n' @+ e- \" f
server 192.168.1.100:8080;* ^" n' q5 M$ m* W
server 192.168.1.101:8080 backup;
( C3 G7 H" I) r}; d- `; [) P7 S, k
2 ?% h B3 X4 G% s2 `/ o! jserver {+ p- U& p- b+ w3 P: a
listen 80;
8 L0 e* Q6 I* G) z+ dserver_name 52os.net www.52os.net;- a2 G* ]4 A- n! J. g
$ ~/ H. |. `% Z. m! h
location / {
. O; {. n2 A, O6 j9 L( \1 d n ModSecurityEnabled on; $ M: j P6 f, @9 Z9 E
ModSecurityConfig modsecurity.conf;
* j) f2 d' w1 r' d, h2 r. [ |8 H- C9 i7 z" C0 H
proxy_pass http://online; s' j/ s& ?6 B" W1 b
proxy_redirect off;
( Q2 I2 x0 P; l! ]% ? proxy_set_header Host $host;
1 I1 s/ F2 ]6 K$ L; p, X$ ]1 { proxy_set_header X-Real-IP $remote_addr;
) ]% K c. L- M5 b. A proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 p2 C9 b- G% ^0 Q4 t
}
5 b2 |3 h; p: I- t, |) I" r}) F$ O) ^2 a9 p, Z
六.测试
% T! T. g6 `; B9 o- t" A9 Q
4 L: j' @& |. }2 U4 P: x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& Z9 Y u4 S7 A1 L6 d0 W1 v% H5 k2 l, C$ v- Q* }% U+ _3 | z
<?php1 S. ~* T1 d/ l# |$ ^4 z
phpinfo();
' i" a6 l4 A$ E?>- _' ^, v1 l0 `+ S3 A m1 d5 Q
在浏览器中访问:
8 }* g8 ^, G6 W( \( K
1 W! C7 X9 v+ H9 m) j+ rhttp://www.52os.net/phpinfo.php?id=1 正常显示。1 @. K8 z% Y/ q6 H7 q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. W9 h9 w9 H* J" }http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, P% u, R- B: S$ f# d说明sql注入和xss已经被过滤了' G9 p7 s. X$ A7 m
% m9 J% h! |7 \. o8 p
七、安装过程中排错
" u# N; ~4 w; q, a9 p; F* { n8 s( X0 q5 S; _
1.缺少APXS会报错
. i/ Q- `* y8 K7 W( m+ d
' f9 N0 b5 q2 T" d8 T, Lconfigure: looking for Apache module support via DSO through APXS. ^. D4 n3 [7 Q4 M
configure: error: couldn't find APXS
/ z, `& g9 n+ I) a: N/ gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 } |) n7 C1 t1 X+ i- ^* E/ p
解决方法:
$ W P0 ^: z7 k9 |2 H/ ?( L# m5 j6 E* L) `; ~) U! F/ {" J. }
yum install httpd-devel
. M Y( y: t) J5 N2.没有pcre' A, B% M* }/ Q; E% k$ q) w
5 a- Z- O7 t5 S# S% z2 Q0 |
configure: *** pcre library not found.& _* ?. t8 v' C/ n
configure: error: pcre library is required6 d$ L8 g* B- Q* A$ Y( [
解决方法:
& R- Q, N* A* _1 K& v3 t; f [+ U3 {+ G; v k1 V4 I4 t
yum install pcre pcre-devel3 ^7 c0 D/ N S. `8 ~7 B
3.没有libxml2) x1 W" B+ O T: J1 a; V$ h! p
$ G+ a2 @+ E+ u1 \/ n' N7 X0 m6 i
2 A' @# z! p2 k# v6 _ ^
configure: *** xml library not found.6 v: z3 |" I" T! w- x2 R" r* t
configure: error: libxml2 is required- w8 b, l( |! V5 |$ o) E0 u0 s
解决方法:" N/ e' X6 G6 Y$ E- ]3 t
/ o9 m- o5 d' z( [6 B+ Ayum install libxml2 libxml2-devel
6 v, u0 u" ~: {; ~% P4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 ]" g0 [, t6 v
) `3 G; p- l5 r; D- I$ ~Tengine version: Tengine/2.1.0 (nginx/1.6.2)) c6 w; V% |' J7 M X$ I; W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" i6 F2 [. L. t$ A; a1 v) L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 G& g# b) u: ~& O! B H
7 S; R) P! P( x+ t! Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 x# N( o; u: [* X; P" U% J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 v4 X# I8 j' p, I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
7 O* a+ {7 x- M8 m5 t: [3 `/ E2 U: b G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 S: Q; \2 \# F! Q, A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ P3 |! S) v1 J+ } q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) {6 }- D. j3 U: G% I8 y解决方法,移除低版本的APR (1.3.9)$ [( N3 w/ F. C2 A/ q+ P( P
" z+ W3 w$ c! ~7 Z+ z/ i2 P# L. C
yum remove apr3 c' {( f9 C; R5 _7 l- |
5.Error.log中有: Audit log: Failed to lock global mutex7 H* C" s9 u6 n: q6 D% g
+ O! U4 u( ]( b5 R3 p( F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ) A+ L0 k6 Q: b" x: l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& Z8 I0 v! n, ^& y6 r: _) N解决方法:
9 u6 y' ?$ y7 G编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 Y; W. z- L6 z$ z! _/ k: c) @+ j- ?. }% [( T- L
SecAuditLogDirMode 0777/ ~6 n% z# M! }
SecAuditLogFileMode 0550$ y' o/ E$ w0 l5 z C
SecAuditLogStorageDir /var/log/modsecurity6 _. l) U, V' {/ t! }; \
SecAuditLogType Concurrent
- [5 U' B8 ]: z* u0 `% E3 o- t9 j参考文章:7 u6 k! T4 S# S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! H M% k( `5 j; f$ v5 |! \. K' Ihttp://drops.wooyun.org/tips/2614 |
|