|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, y! d( E2 Q1 G1 k+ W* V
% F- `; r8 u7 Z; }8 u一.准备工作
7 N# }. R- H& l0 [
" |% [% W O7 e$ b' `, ]9 ]6 G( w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: H Y) m* k' E+ c3 L" `/ f
D$ \8 a' p3 |1 r1 }tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 i0 a/ W6 c, c0 ~
1 S5 _' l, |" {/ l/ ]! Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ v4 ^/ p" s' |6 |
9 g/ N# K0 x- {- {( a0 _, k @( nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& J+ u, ^; _ ~6 W$ d+ ~1 b/ o
* Q) W2 D8 g L& K; ]# e依赖关系:
& r9 B) j; I: ^ K# o& \+ x: w. ?tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. I! m8 h% i+ E, b4 h( O
# E; Y7 D A) O4 j3 A8 q! xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
4 `+ s+ D4 K) e4 Bmodsecurty依赖的包:pcre httpd-devel libxml2 apr2 x8 T# H. Y- g3 d
% V% ^1 \% f V. a$ g/ x$ P3 U+ p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* I3 G, s$ D% a
二.启用standalone模块并编译4 Z: l5 Z$ y. ]& s; `, ^% ^: L5 C; J
7 U) H& C- A5 ?+ J! _下载modsecurity for nginx 解压,进入解压后目录执行:
2 S( `7 d" C. b! `; H7 b4 ?! }4 }% Q" Y) M8 w1 s1 E* j k" b$ A; @/ |2 t
./autogen.sh
; n/ J: d" `; C% k$ b/ @/ p# |./configure --enable-standalone-module --disable-mlogc
4 m" u9 P0 K* L& b6 |0 }6 Dmake
4 o* D+ b" y; X三.nginx添加modsecurity模块
/ R0 S! J7 E6 Z$ J+ p
& S4 V; L% p! F4 [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 i& }: F& [3 ?- B A+ [4 w/ X6 L+ O9 U7 F- r; }6 {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 k' t) F' g6 l, u" F% ^2 Wmake && make install1 N1 q5 S0 d- T
四.添加规则# c8 r; W+ Q4 n; V
: d) |$ e6 V6 ~8 S C s( M4 {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' B- [; I8 n# I$ r/ W( k) ^6 d" E3 z, p" f5 x
1.下载OWASP规则:. C. }+ D5 |5 H. n
& E- X4 a- t8 \* c& n) Mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 A9 K/ Z+ T' n
- t/ Y" j4 }" _ Z4 k9 qmv owasp-modsecurity-crs /opt/tengine/conf/. l3 l0 n5 i2 v
5 W! b# v1 g* K3 j Y: t! \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ t m/ O6 [, d; K$ n& Z1 \& A2.启用OWASP规则:% C$ V" o9 e4 A% H k) j
8 c. B6 F* A% {& Z$ J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! a- j" U. k) [5 \( j9 q2 v M# c
* j( p/ ~+ q4 S5 |' ~; ^' e3 w; ]
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on G4 G- F# L7 [% G8 W
1 Z, b0 p" i, o J. k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 `* e& f8 ^. }9 C0 i( D) e& Y
( j( f6 E) D: W% Q; _0 k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) W; S2 h3 ]% z) J% U% U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 s8 H4 E% G! o8 tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: e7 ^( o+ i3 a4 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" x, h" v) B$ q2 ^% F1 x, vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ L; x+ k9 [- J i- o8 Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 y' k# t! m. D6 _; F+ B) KInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf+ P. J2 J, G$ ^# n
五.配置nginx
7 e0 N/ p& @. b" }$ @8 i: K$ T# M# k4 I* h4 h
在需要启用modsecurity的主机的location下面加入下面两行即可:
. E3 G( V1 \# Q! U: m* N
" r) l# b& W! AModSecurityEnabled on; " _% m4 r( o+ }9 R* u
ModSecurityConfig modsecurity.conf;$ P, i l: m3 N" z$ A/ z. T
下面是两个示例配置,php虚拟主机:
/ d3 w7 D2 u! K, h" u% k$ g+ C2 V4 p7 R/ E
server {
* ^/ Z4 X+ }- n2 ? listen 80;
$ H W+ C2 K9 N+ K* T server_name 52os.net www.52os.net;/ L/ l8 x7 A3 Y
: i4 w) L/ T: `: |+ e1 w P' U5 F location ~ \.php$ {2 A" d! ^( K7 k8 w
ModSecurityEnabled on;
7 l; a: J w% H: \) ~: T( t3 s ModSecurityConfig modsecurity.conf;( z. i; R) A& k" d( o- I2 D) x1 d
8 z3 i8 p4 J7 L" v( K. e# b) J
root /web/wordpress;% C9 e+ k/ S; ?% h8 \- x" T
index index.php index.html index.htm;
% q# y; m8 w6 B5 {$ [9 k0 i
% m0 I: D+ l* i! k% g fastcgi_pass 127.0.0.1:9000;
2 O) s0 L9 T* g7 a# V fastcgi_index index.php;
0 C/ A" I) H4 B/ n/ J fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ D- k7 h1 S+ \# }
include fastcgi_params;8 ?5 Y6 F- h& x7 @
}7 M2 I0 B1 I% q( M
}
% o& W F0 g+ s( ^: O- yupstream负载均衡:# K2 d) c# W1 i3 a z! g
) _$ Q* E n$ ]$ R7 u7 x3 y$ R
upstream 52os.net {' i8 H: v* C3 Z( F& H M7 l
server 192.168.1.100:8080;
$ _, c X. Y5 Y2 p8 O server 192.168.1.101:8080 backup;, U9 l& F' i% F; c2 e* t3 e& b+ P
}
0 R( [" s/ q5 ]; g% {5 [1 F0 }; C* s, o! V* Z3 f# O* Q
server {7 [6 T3 h# A& m. @
listen 80;. c6 I' t: I% E k( f% c9 s, Y9 `
server_name 52os.net www.52os.net;
- Q/ e7 h+ j0 ?+ \# O8 f$ {% d# x4 Y( k
location / {
0 y# d# D3 X9 b ModSecurityEnabled on;
- A7 u0 p) u. z8 G2 a; Y# j ModSecurityConfig modsecurity.conf;
h, x$ f9 A+ s. m# d/ n: r6 U
4 e* v' H9 C$ b- W proxy_pass http://online;
7 H* [* y3 Q; z4 l& K proxy_redirect off;
1 ]2 m, k6 D, `( H proxy_set_header Host $host;) \1 v/ E; A: B$ D& _6 ^" m0 ?* x
proxy_set_header X-Real-IP $remote_addr;
( K4 Z% r0 E3 J6 ]# `$ R proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" y6 u5 S, t7 K$ P
}
, {, O1 q c, X4 J$ u}8 O/ r( t& I! d/ o, R1 E
六.测试9 X7 K; F. K2 W z- s
4 ^, ^) d# k( I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! e$ t/ ]: x% [# {! e
( X% J' T& N2 ?: G9 @
<?php9 W. q( y8 o: W5 w
phpinfo(); : Q+ [/ I) L& {$ M2 n" E7 K
?>0 y5 o x4 O/ B ~
在浏览器中访问:
1 e: a- Y0 V; [# d$ I; K. M4 m: [/ g
http://www.52os.net/phpinfo.php?id=1 正常显示。3 @7 @& o$ o# z# R) W7 Z6 b
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 w, O, K/ u7 I& Q2 T
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# D& \6 r, r4 F& o* U( @说明sql注入和xss已经被过滤了5 R5 K' o H9 d% I: G8 c
- l2 W E6 V/ I2 W
七、安装过程中排错% \+ ^- ?( q/ D- @/ T) F% ~
5 u6 B8 y5 y+ l& d' h, U. x0 n
1.缺少APXS会报错
! @8 w2 V, O" h' c, f+ C' e# ~# V/ l1 M0 @; `8 d. {/ s3 ^/ Q
configure: looking for Apache module support via DSO through APXS1 y# C; _4 k% G) {
configure: error: couldn't find APXS9 K6 `) S# j8 |# f9 X) H2 _- L
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, r8 |0 O, d3 ^+ ~6 |- _
解决方法:3 f' d# a, J8 X+ E4 C" L9 _- [
# K2 }+ O7 }- y) Lyum install httpd-devel$ v, ~0 M% T+ X0 W2 I3 Y
2.没有pcre0 u& K7 X0 ?' {$ x" r0 H1 I
. H4 R9 U7 d3 wconfigure: *** pcre library not found." i- `; I; X2 V
configure: error: pcre library is required
' ^$ U' |$ B1 h, N解决方法:
* t- h z1 J; Q6 U( h% e: E) N. ?2 q* f5 Q/ F( ]* j: }
yum install pcre pcre-devel7 m, x" a; h7 i; a5 @/ t6 _
3.没有libxml2
& q/ a/ r4 x; O8 v, F; _, g7 D E1 u5 d; X: A, p
" ~; I. X$ i& M
configure: *** xml library not found.* M' M+ U5 f& J0 h8 ]
configure: error: libxml2 is required
+ s% C4 F8 g* n- O3 w/ s( O解决方法:
2 m1 j7 n$ k1 N/ s3 o# c! |3 z( f1 L! l3 S, P9 b1 N/ J
yum install libxml2 libxml2-devel8 f5 |/ L0 A. j3 J# k; Y7 d9 }
4.执行 /opt/tengine/sbin/nginx -m 时有警告! x3 P( Q" R4 n% c$ B5 D4 b; `4 L
! N5 G. l; a# o! T L1 B0 VTengine version: Tengine/2.1.0 (nginx/1.6.2)
% l9 ~6 y' |- C! I$ z* U% M4 nnginx: [warn] ModSecurity: Loaded APR do not match with compiled! `9 ~, K8 w& [$ Q0 q& _1 w
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 e2 \, n3 \6 h/ Q) K! `1 @0 T' L: ]8 @( G6 u: i2 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 X! o ^2 L2 [0 P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"# _4 a" M2 t6 I! s2 Y' A% A- g
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 x9 l+ G# x5 I4 M8 n0 n" S" Q3 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ ?1 ~$ W m; i+ X$ {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 \6 } c5 S, m- F j0 r$ T2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., C& \4 g- b4 q% T' Q( J
解决方法,移除低版本的APR (1.3.9)0 n, c7 o, r1 p0 u9 a6 H. ~# S, M- r
0 o5 E! j9 D3 p! a7 g; u z) ]/ Nyum remove apr
$ }: T4 J( S+ K, h( T6 ~. B5.Error.log中有: Audit log: Failed to lock global mutex
; U: Y- i1 [% {& C! i% o2 l) N
( n3 D3 w$ a/ y9 ]2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; B- M. F6 j6 U, U% n: ]8 B; [% a, g/ M
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ t2 I# V4 |: [/ z: y. n
解决方法:
# e0 p% F2 C! E/ C编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ E; l% V: m4 }8 q4 E
) f& y6 A3 a) g* s1 YSecAuditLogDirMode 0777
! u, _4 p" @; L- D" d, \% aSecAuditLogFileMode 0550' }" t$ i& e( p6 `9 I+ L. t7 Q
SecAuditLogStorageDir /var/log/modsecurity
2 v1 \ I. L9 K0 |% L' y* Y' dSecAuditLogType Concurrent/ y9 v$ h" m+ n$ z, q
参考文章:, \5 b( [* G& K
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! W1 d1 {, {, i: b# m3 ^http://drops.wooyun.org/tips/2614 |
|