|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 i2 d1 I1 a& e
1 x* ^, P7 ^/ I/ P5 E P一.准备工作6 p4 n, K) G# a' E B, S
, @5 [2 Y* D& H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ _; v6 [7 F8 y$ J6 y
# C4 Q n2 o+ Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz R- Z( }4 ]" n6 H
0 W1 o% F4 |7 I# V- R2 dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 M% l, j6 I; C5 g/ U4 a/ T
. f) n' P; l( JOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* ~: d6 O- D8 W+ S
% X6 }$ W. q3 N% L
依赖关系:% E1 N& r* j/ [5 M, L
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# G% _7 [- l8 Y2 j- I. q2 E' G( F3 a% t# v. _
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. x3 b3 M* s7 {) a/ B- e" D k' Dmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; u4 l2 l) W$ i% }4 W" O- j! f! ^* j
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 R- \+ X; L: J0 d0 g二.启用standalone模块并编译& A8 W9 v/ N$ K* X' }! a N& R1 x& Y& N
( ~( U7 S, {) g: L, ~( n
下载modsecurity for nginx 解压,进入解压后目录执行:- E4 s4 }' g1 d
( A0 N( ]6 O! ^ T
./autogen.sh
8 v$ i6 e7 C7 J+ k) m./configure --enable-standalone-module --disable-mlogc
; J( Q0 L" r- ^+ tmake * O6 o3 P! l" n1 i9 }' v. ]
三.nginx添加modsecurity模块 d1 |" ^& o7 C: W5 p" h
- u: u4 f* v2 _2 p- k在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块: i! H/ j, s. B6 w
. }1 T- F$ i( y. _
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% ~- f9 \3 Y* V2 R# Q% ~make && make install
; D' C) I; B M四.添加规则1 T" v+ O- z; w; B' b* s
( X4 o6 I, J* ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- A+ S' K3 C% p' b0 [& U+ ^2 n7 p5 L R5 _
1.下载OWASP规则:
. `; C" M) s# ?/ e3 X1 y4 i! O7 ?2 Q$ n
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" h g+ f2 Y% I
0 C0 S: c; n, E
mv owasp-modsecurity-crs /opt/tengine/conf/' G% V% q* d3 X9 N; M$ E7 o
- y. G# B( c6 C& |! o1 n! ?1 F2 Hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( q4 _2 W1 F+ U2.启用OWASP规则:
2 n5 ~; q& k( H) k. c" x' H, c4 S/ ~6 e' b
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; f# M2 p5 ~. M( i7 b- |
% w; z- w q# i6 {7 G4 h
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ h0 e4 _$ O+ ]+ h! ]' v1 S# M. ^
w$ u( X0 U6 D U* ^. ]8 eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 [9 I1 T1 ?9 r: k; Z4 V
& E1 p: X- J/ [% G6 L& i5 j. a$ MInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ j* C! B, C# o, @$ z$ U3 S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' q( P6 \4 X9 g. R0 \* V6 OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
Y3 j5 |. ~; ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* ]% x2 ]5 ]8 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; r6 d- }1 Y3 f- Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 l x5 E- {4 \% oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; F- I) D& t8 b5 e! N' E2 ~. d
五.配置nginx" B r1 s" v+ g. V3 i6 j5 F7 Y
0 _3 M- v; Y9 Q2 \ z; h) ?( C8 j+ d
在需要启用modsecurity的主机的location下面加入下面两行即可:
, ~2 B, B7 G+ \: K, n, [# P9 V- q" A3 O; [
ModSecurityEnabled on; & H! [7 {5 b" `5 N0 L( K
ModSecurityConfig modsecurity.conf;
- h8 L6 f' K4 N0 z: _" Z8 n下面是两个示例配置,php虚拟主机:1 Q( k' I: [% Z+ g) Z' C2 H
% H& t6 v- g4 i' q+ C3 f
server {; Z8 E2 b9 h% b0 q4 D* s/ X
listen 80;5 R! H0 E/ U0 D1 u% j% i5 N6 @. D
server_name 52os.net www.52os.net;
5 V, g/ O' B/ j7 i+ r/ m
2 y6 t% [! k7 \& Z; l location ~ \.php$ {8 L- V0 c+ s4 x1 j3 L
ModSecurityEnabled on;
- J5 o6 @9 s4 c) Y* Q/ O/ f ModSecurityConfig modsecurity.conf;
" V( y5 D* e) J! Q# m9 X9 x4 s7 p/ K8 t
root /web/wordpress;! Z1 ~ `( v$ S w) ?- \
index index.php index.html index.htm;
# I: t h: c$ b, |" L
2 ?0 L$ N. h E7 w2 Y4 p% W6 | fastcgi_pass 127.0.0.1:9000;7 ^# n' A% |* e u! X! D3 I
fastcgi_index index.php;
4 c C. _9 l9 U9 l fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 V% ?- D3 V1 r4 [3 \) Z include fastcgi_params;8 I& T& J; S8 |3 W7 H( }
}
- L4 w0 F& |* }0 o }) ?# D& z9 u9 ~6 ^! D) }
upstream负载均衡:
+ m" w; q+ U \' H4 H* w
+ D- V% O& ~# u* w2 H4 ^upstream 52os.net {" a: H7 c) l* \* U
server 192.168.1.100:8080;3 t w) A& x4 d! H, F
server 192.168.1.101:8080 backup;& V! g- H* b0 G
}# I5 G2 }, z( X7 {4 F
2 {. R+ F( v) M4 y" c
server {
7 I: m3 _# w6 m8 Flisten 80;/ R9 I4 U: `( [- X# m3 T
server_name 52os.net www.52os.net;- k. k( U+ H" n4 J1 Q( c
2 f% N# ?1 U: hlocation / {
9 C, Z# D5 S- a; k" e# V ModSecurityEnabled on;
' }4 }0 I7 |! ]5 c- o6 H* K ModSecurityConfig modsecurity.conf; , T2 h ]# [4 {3 D+ J+ z
) p% C$ r& `. a z5 t proxy_pass http://online;# _* F, L% \1 n2 L5 K* B
proxy_redirect off;
. f( Y# U) c0 {- E5 R# {0 [ proxy_set_header Host $host;, X( W3 `3 S" X
proxy_set_header X-Real-IP $remote_addr;) d, s/ v/ p, b2 C/ M* [' j
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" B' A1 O4 \: g0 P8 S' L; f
}$ x- i! O& L2 `/ A
}
3 V% i4 \6 r, p7 b& `六.测试
8 @& i! k8 {0 h% O, ~
0 H9 q7 b# G% ?) }4 ^2 d8 H* ]& w我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& S: [7 H7 t) a: H6 k5 O1 i
- {, {! ? n5 _4 p% K& k5 n1 c; b<?php
( X1 H" n" w9 J% x) p phpinfo(); " ?- s# o& e; e3 u0 Y( h) J
?>
1 E# [* N9 T( v( c+ Q: e7 j. `在浏览器中访问:. ^6 y) P u8 o) }1 Q, o* c; x
2 J U3 E4 p2 I, M
http://www.52os.net/phpinfo.php?id=1 正常显示。
) v6 F2 f( t& h/ i g7 Vhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。, T1 x, M- m- ^! c6 d% n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% m0 M4 x" }6 g
说明sql注入和xss已经被过滤了# z0 m* g, T) Q6 ~1 E
% H& ?$ K+ J$ Z3 n( E
七、安装过程中排错
' X9 @$ G6 S) _& b; E# ]2 _: m4 n3 B' `
1.缺少APXS会报错1 C O7 X/ R( y4 ^/ {- x( ?0 o
: x7 O5 W. o f# U! p3 F% gconfigure: looking for Apache module support via DSO through APXS
' V; ? U' a: G: j" X. r4 dconfigure: error: couldn't find APXS
" R% x! d6 a) H( E6 [6 ?3 ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
C$ ]) ? ^0 \6 j" I解决方法:$ I: v5 B l; D+ k, {9 E5 Q
' B1 j" I8 A$ f% c6 r% j' _9 c
yum install httpd-devel
+ D: V+ H% w# A* q/ K2.没有pcre$ j, z6 L( Z8 e' p! M( L4 z
* u! X; c' U3 v- [9 D* u
configure: *** pcre library not found.# ~+ O- c1 w$ p, N! A }0 L
configure: error: pcre library is required; |5 y" l8 F( u2 x* Z! ^' r- @
解决方法:
1 [7 i, }/ k7 q$ k' @# f' k
' F: F u3 F5 Q8 Cyum install pcre pcre-devel/ E3 Q' z( O" L/ G
3.没有libxml2: Z, z, a: [0 x7 l) Y: j
, ]( D1 L8 S+ C+ V
2 A5 m, D" t/ ^4 g# e0 u0 c1 ^
configure: *** xml library not found.
6 W- f' I$ F R N, U2 P9 O, lconfigure: error: libxml2 is required% `) j' I* C" P$ V
解决方法:. h: T/ g3 U: ~7 z, M7 ^
" x D! A8 B: @# z2 L2 X7 T5 l* qyum install libxml2 libxml2-devel) o) C" y6 j$ ]4 P2 t1 t7 |$ J
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 U( Q1 \/ v1 e* ^, l
2 U2 O8 C0 y+ [4 j* p, nTengine version: Tengine/2.1.0 (nginx/1.6.2)
. x3 ]7 v2 F& d! h+ e0 L$ Anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' h0 o# S' P$ F4 t/ n3 t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, l) Y+ ?; q+ x( I0 l* X8 C/ G# l( A1 u" Q% C, q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 P7 I$ g7 D/ F5 R: D% M1 {- f' b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( m+ x' l6 s7 u9 _$ ?( \% }2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 C- z" m2 e4 ^9 `! T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ k/ F( K. J1 D+ B9 W4 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# m/ y5 g e `* ?: w d2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# q3 p$ t$ {0 [/ U3 w6 A- `
解决方法,移除低版本的APR (1.3.9). v4 Q. c: p0 y* M- B
" X: a8 f2 }0 y( J
yum remove apr% `: m6 i% L& g
5.Error.log中有: Audit log: Failed to lock global mutex
, g0 f7 B' c' D
! N& h* a# `6 C2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock l- J: }6 x: t
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# ?1 b- k8 H. {1 N0 H6 p解决方法:1 }4 L# t S% V& X( T% _
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 k9 L) |: }3 w
- d, F; k8 e8 MSecAuditLogDirMode 0777
, P+ @2 u m/ R4 f! S9 |; H- ZSecAuditLogFileMode 0550
* D# W9 `- e" ~ w' h8 {- vSecAuditLogStorageDir /var/log/modsecurity4 {8 Y( [) h# G$ X
SecAuditLogType Concurrent
/ L; C! `1 M8 p- f/ |' Y! P参考文章:
& d2 B4 G0 j4 r3 B. X- V0 C4 Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ V* A% _" C5 `$ e- b
http://drops.wooyun.org/tips/2614 |
|