|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* c/ M7 t$ p0 ~2 [ T+ z" b1 j" K0 `' L: q+ g: V
一.准备工作( C+ g" H2 ~" G: b
, b+ O( G8 L' V: B! I, E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 x6 x: T0 r$ e- o/ R
4 P. B5 _ E7 \% P$ C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( E2 z3 C( B+ o% O+ D; g8 V" n6 ~+ X# `1 K% N; F$ v& v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 w1 E) h2 J) A0 O: N' x
/ A! Y! m1 a5 @3 i/ FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" p0 F7 P9 ~2 U1 K; w' X, U7 L& e% t1 R( q5 c5 u
依赖关系:
' B- y: X7 Q* W9 y6 d: _tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 i4 U p: ~4 b0 V5 t+ e+ u- F/ d1 C- D- f8 A* F
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 ?, g/ m, ] l# N+ gmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; o* g+ d% {; Y( P) m
! I0 _: x! U: a8 s; w" ]% i9 Myum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! `3 j4 ?6 G$ T( v5 u* C' b% k
二.启用standalone模块并编译
1 N; {- W* L! O1 F ~; v$ l
' |* P# |, X" P' T下载modsecurity for nginx 解压,进入解压后目录执行:
) V) ?7 S, H: v, _& z# Q) S9 R
$ b( I* k" a( Y, d" @, O6 b( Z./autogen.sh' x' E6 f& e3 c% w7 t t
./configure --enable-standalone-module --disable-mlogc) @# \9 q3 Y0 U; i1 O
make
" H+ W5 V9 T/ b8 p- d& G三.nginx添加modsecurity模块
6 H3 P! I4 v; T- w$ A$ r9 Z# x$ ^. X7 Y2 H7 G `- r+ _
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' c j$ y; Y' U7 O6 T0 N- q( c3 i, N& w |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine0 l; e% Z. w7 h/ I' Y# k5 t8 L
make && make install
' i- M1 B" P2 X4 a" G; U' v四.添加规则
5 x& x# \& U, Y7 J" H
" J+ N$ j5 K3 U. S; `3 x+ Vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 H6 U; T' \ x6 F
8 O5 d4 ~% }5 z$ U4 P$ j1.下载OWASP规则:
! p6 {& D$ U: X9 b# `" ? _$ o5 q+ S! c. z; v' k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( w" N1 N R \ A6 {0 A
5 o z: b% E7 K& N P/ V8 d. b gmv owasp-modsecurity-crs /opt/tengine/conf/3 Q$ a: K0 Q' \( B' H# x$ L# s# a
$ z& J8 |1 p% E$ v* Z: u0 Rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& a2 T4 [: w! k4 h* L5 o6 K2.启用OWASP规则:
( ^$ C/ S! T; l& ^" s: l* W
/ L; O% G" ~& W% P7 c复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' R$ K" r. [7 M4 r% v8 C K6 L! p$ p# @9 P( j+ i* p7 X
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 I1 q, Q( w6 }) ?4 c4 M6 P7 B# a( g) ~3 M/ l) O9 u& b# d
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 _# b2 b- @( }
" c9 U4 O! T3 X& T! y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ F' n) ^/ E i Z5 @ P6 bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# u) \- `, |. q! l6 u c8 G" VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% b0 s% G' E( c& u% D% ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 _! D9 [7 s+ [& cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 B6 b$ f C) _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf v2 |/ |% h v+ v# F+ L
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf' f$ ^7 o4 @+ p% n( K
五.配置nginx+ n, }6 q {# f9 {, s0 g
. I- t7 m9 m; K1 t在需要启用modsecurity的主机的location下面加入下面两行即可:& ~9 p4 u$ n. |
- X8 |& Q7 _, e; K, E* \
ModSecurityEnabled on; % `) j2 s5 T& Z5 i# ^
ModSecurityConfig modsecurity.conf;
% u" f T- k: i+ ?1 V下面是两个示例配置,php虚拟主机:# V8 e8 y2 v5 {% Z
- [9 t7 E$ ^6 B' j' S
server {
3 i- @; E. E& `" _6 e$ K$ t& R: h: \ listen 80;
6 I. v* C* _! i: y0 U4 X+ [ server_name 52os.net www.52os.net;( |7 a, e, t H% Z
2 k. v: i T K location ~ \.php$ {: D$ J P0 c) u- T: X
ModSecurityEnabled on;
3 h5 d0 ^# ]: W8 \" m ModSecurityConfig modsecurity.conf;9 W' V! V) f5 b {& P) W2 M
5 G# i8 B. B6 R) X$ Q! i8 {; R6 Y root /web/wordpress;: R* @ E$ A0 z1 I! b" s
index index.php index.html index.htm;
: {- e/ l5 A- n0 \$ x. D' d ! r0 {* [( y% L7 p# A; L% ]# y9 X y
fastcgi_pass 127.0.0.1:9000;
& @ a+ X8 O, w, b0 C0 I7 h0 q3 C fastcgi_index index.php;5 L& P/ S8 [1 P, ?, {* u$ m$ m
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
+ b/ w1 ^( i$ P include fastcgi_params;5 }: y) T9 B) |* A9 w( i
}
) j: W7 ~2 w. J }
% ~+ r. q3 @, L+ c% L8 Z4 \2 S0 wupstream负载均衡:! i: m5 d3 L! p: G5 v) u; Q
' b/ _3 w- M) x( ?8 [0 t
upstream 52os.net {
: ?' W3 G9 ]6 u V server 192.168.1.100:8080;
3 N0 B; [( L( Z/ I* J) q server 192.168.1.101:8080 backup;
& k0 Z1 k8 A C( W}
) \2 p$ t8 c/ y& ]. n& o
4 @! s; t1 b) X% ?! Yserver {
( A* n9 h) P9 ^& d" _5 Elisten 80;1 o( p5 g; s6 a) I4 w
server_name 52os.net www.52os.net;9 Z/ _. E6 D1 [1 V% _
" P0 [* q/ x1 h, ?: E {0 j
location / {2 Q" i; {4 T6 ^7 D
ModSecurityEnabled on;
. K6 w; M% I. j% q6 f: j0 @ ModSecurityConfig modsecurity.conf; ! S/ o3 e" }+ s: A: D* u8 W' ?: v
+ W8 \9 n2 x+ n! c* ^ proxy_pass http://online;. E0 Y1 ]* E9 ~
proxy_redirect off;; P1 W, Y; ]/ L. l# c4 J/ `* S
proxy_set_header Host $host;/ `" W' i6 ~5 z2 g3 `6 c7 H) P7 q K
proxy_set_header X-Real-IP $remote_addr;
# P, S% N5 }/ a% a F( J- F proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 v$ a& [2 r/ j" o* Q9 U4 q
}
9 ~% [0 v5 ]' i2 S}) ^$ [- o% ?1 C' {! u7 B+ I% \$ f0 a
六.测试
, G% S+ ^- K C( U8 \, W* ?# e2 q6 Q. `3 z9 C5 R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, [1 L$ ~/ e: v8 t8 L
1 `( M/ n/ I |- g1 |% A<?php
9 t- ^& N' S# T4 T, O phpinfo();
- ^0 f; o# T: s; V?>7 N. |0 y; r4 Y! M/ P
在浏览器中访问:8 b: Q/ ]$ {# {$ _/ g$ w6 G" Q v% p
' W% R5 p# e% G0 L' E2 g
http://www.52os.net/phpinfo.php?id=1 正常显示。; x7 F X0 t7 |( h) k$ W0 i9 L- F* }
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。# z8 x1 A9 c& |/ z9 G4 s4 F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 {9 \5 b I1 |: K4 ?
说明sql注入和xss已经被过滤了. Y/ ^6 R6 j6 a) C% \ R* r5 [6 w6 J
" y) u- L2 M t# m) ^# m1 a! w; _七、安装过程中排错
0 B% d9 ^, e0 c7 _+ v5 k. [% }& R( u
1.缺少APXS会报错
! V4 k2 k* r' X; A9 O7 b$ K" a, A
6 P; l+ |4 E& j! Gconfigure: looking for Apache module support via DSO through APXS' T: a6 b6 [) X. H( y: J8 |! x" ^3 U* J, _
configure: error: couldn't find APXS
# U( e1 v; f) I. {/ Q# Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! ^1 Q$ A/ D K, N$ N3 `" w解决方法:: Y: d" u2 _8 s- |7 y3 r
7 y, F0 I( T: o; N) ?
yum install httpd-devel
, T( d% m! t6 G5 H2.没有pcre
. h$ }% u9 x/ L# w" b% Q& N- c/ K- U! v
configure: *** pcre library not found.
% p: T+ v n" s& c" a! ~configure: error: pcre library is required
1 I; q) ` h0 W: Z2 a2 l) g, a% v6 ~1 d解决方法:
, R4 w+ d3 C; J$ L' r1 V
/ q; y* V' Q. L9 v3 i+ G9 W' T5 Dyum install pcre pcre-devel) v8 I* K1 a* S" {; Y
3.没有libxml2) q5 B8 J% G- }. r2 V
' ~' G) ^: l: v" a
5 x' n4 a, m& r# @' Sconfigure: *** xml library not found.
" a# n% M" V2 V( V: j% o' ^3 jconfigure: error: libxml2 is required9 u* w# F$ H* ~7 J; ]5 L( J
解决方法:
1 ~: \2 p5 d% f2 \4 X# R0 L* A
8 W0 x& p. U( c2 s2 q* A0 N$ Byum install libxml2 libxml2-devel i( t8 \. n( b
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 a7 D: a3 ?4 u9 q( O7 o5 ?% e# [
; K. U! X# Q& F0 i! v, o5 lTengine version: Tengine/2.1.0 (nginx/1.6.2)
: |1 G X1 D4 C" c* dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!$ J# O: |! j" `
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; ~" L, `8 x7 k; ^
3 l2 u; }: o8 T$ t6 C) M. _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 ?. d3 B( a, P2 g# s9 [: E0 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
9 ]: N- k% d1 R2 _4 u2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% n# f4 S7 A7 X) w& W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 H/ o7 P2 f7 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& K% o" X) B! d& t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( n) \# H* z2 K# n: _! w1 W
解决方法,移除低版本的APR (1.3.9)
: C, o& h6 ?/ l5 k& V+ [& E# U# D8 S& F u9 v( I7 [
yum remove apr9 l5 B7 U$ _5 z1 }$ K
5.Error.log中有: Audit log: Failed to lock global mutex
; g, G; b/ P. B! q) f2 _, v( c9 _" Y5 `. p# [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 r/ O, v9 q4 ^2 [5 ^, b8 Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 X, o. H7 M c8 G4 d
解决方法:
* J% H4 y; R! I% O' `2 C编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( F: G& L8 t8 J* g+ i: z' A
5 m& ?* j+ L) V1 xSecAuditLogDirMode 07779 g* [) Q3 U+ o; ~3 y* D- d
SecAuditLogFileMode 0550
/ c* E, h' {8 b0 i5 `5 J2 {# JSecAuditLogStorageDir /var/log/modsecurity
1 b8 K2 c1 W0 j$ w: S2 r# wSecAuditLogType Concurrent S7 d" q; I' ~, {1 q
参考文章:( F1 r4 _, m. t" Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* j5 [( j6 {9 ~+ W6 bhttp://drops.wooyun.org/tips/2614 |
|