|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' p3 F2 E0 p, y7 U4 E7 r. Y
, O$ U( A0 k* y
一.准备工作6 Q* r/ I/ X6 m7 n6 }1 F
! o# X0 u6 i- F- X) L+ P" |系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 t# A# |+ s; a( g* v% r
; Y9 N: w9 l4 \/ _tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% {9 {$ K, L+ \* u; O
' q$ z3 R( [8 n2 _6 {7 tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
H, ?& P8 Z- j( k
7 h/ {3 P G# r/ TOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 m/ U% ~( U; J. i+ K$ L- k, J' N
$ L/ ~( [( y- h
依赖关系:- g4 O3 I' V, v7 V# p; y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 d9 s' m- g0 d" H
" e2 ^" I. t# T
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) ~. ]8 k8 c: W" x9 Z, E9 e, F
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ [* t5 i- C( G5 Y. O- p
: c8 Q7 |% O# J: C: [1 byum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 A$ `: E8 M8 v8 c ?: ^二.启用standalone模块并编译+ ~! X6 K- Z" Y4 ?% v+ r0 ^5 b
( ^5 Y5 s5 f0 e/ ^3 Q
下载modsecurity for nginx 解压,进入解压后目录执行:
8 R* C! Y K/ X8 y- e) l( A2 [6 h, H( c7 g6 A4 F0 `4 l+ v% f
./autogen.sh
. t3 q+ i5 b- I& Q./configure --enable-standalone-module --disable-mlogc
' X" c" ]' Y% S( xmake
$ Y- [0 M' j- p. S7 b三.nginx添加modsecurity模块8 C3 G! b" W% ^9 i: ?% w n2 X
% F9 H1 F( q( s5 y5 M7 @& c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 P: o: s9 ?* y6 E5 T: @! i: q3 P. K2 `9 s. q* v P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' V7 j l' ?2 F$ |2 b
make && make install
# T- ]7 Q$ G. D四.添加规则
% }% t% y9 Q, g
+ C7 ~* \$ R" J: L C& ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
* `5 d4 F5 O' ^8 {3 a2 _% I1 }# Y5 Q9 n( i% Q! O$ n
1.下载OWASP规则:
+ Y! ?$ O& w7 |' V5 W' M: h6 r. U3 V/ H- g) k# a+ ^2 G
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; M- A _8 y: P$ E5 Q+ x8 S
+ B, z' A. Z% g8 Xmv owasp-modsecurity-crs /opt/tengine/conf/
& s6 G# L K; l2 C; x7 j0 `! {& x) {$ v8 z+ K. q; T) T' t
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: S0 s5 f7 D# L6 J8 n- U
2.启用OWASP规则:0 j$ ]1 g% x2 t; z# @9 l
* {4 t* t% T9 ]' T复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 \. ]8 {, z- W" ~! }8 H7 Z* l$ p, n) r4 u K5 k! o, |/ w" H
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) C; b% i6 p* C- E- l$ I3 [1 W. N* _5 }6 @& D/ F% ^" p% ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
2 V% a( }) n6 K3 H' B! O/ X5 a! X+ k7 m$ i. S/ t+ n! k3 F
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! M) ^8 \. w" @0 ^+ K) T" ~8 @; QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! j Y- J0 h8 i1 g( z% oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. K/ Q+ }, m$ F- p/ |1 Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 T* S. F5 ?: j' V8 JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 J* k. }8 l1 `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: P, _, Y* H+ jInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" j& a( f( r9 O, z* K2 n: I五.配置nginx
. |* j5 x. o7 A5 B; c, e' \9 f' [& L' T( z0 v1 M" b( L- I) Z$ ]1 N
在需要启用modsecurity的主机的location下面加入下面两行即可:
; E9 A. C& C! J6 C& X$ A, V) C0 s6 U- o5 k; ]2 u9 Y
ModSecurityEnabled on;
% ?. G7 `, _& J- Z: rModSecurityConfig modsecurity.conf;2 g; u, L% L' \. E; I5 ?
下面是两个示例配置,php虚拟主机:4 ~, ^# W3 {- G
8 n) C6 P. s8 G I& W. x3 O
server {
2 l4 s6 C) Q$ w2 y, H+ x listen 80;
4 }( H7 i% o: |+ X j! T server_name 52os.net www.52os.net;
* F X" u# K$ e9 P0 {6 B, k
6 X' m- h6 ^% x, G$ ^ } location ~ \.php$ {
: [ G( |# _+ a4 P1 R0 [) n7 ? ModSecurityEnabled on; 0 c' |4 s) |1 J1 m a& p" {6 G
ModSecurityConfig modsecurity.conf;' f3 {7 g' h% F5 F5 B1 ~* j. c" D7 [
4 }! c& Y. y3 o# X( h; q% t1 p
root /web/wordpress;
; |* ~/ J; f- x7 h; K index index.php index.html index.htm;
) s& k6 [: A. q) r" k0 C
$ W g {1 ?8 T* V, ^: L fastcgi_pass 127.0.0.1:9000;5 p/ n7 E9 K3 P! O# n0 ?% a
fastcgi_index index.php;
1 i+ S" [& o' |* w& i fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' ]7 ?' U; I" O0 i- q include fastcgi_params;# B- l- A+ j3 M; b
}
- j0 u$ ^$ J. T$ e! c& [ }
& b# d$ m, U( A" y2 O) eupstream负载均衡:' a/ Q/ t2 }$ @) Q. Z
$ q& M% e4 w5 N% \3 Y
upstream 52os.net {
" r* p& c5 a7 v- T1 [# _5 D+ z% c server 192.168.1.100:8080;/ a+ Y( t# k8 a" A/ \( Q
server 192.168.1.101:8080 backup;
% [ b6 e+ ~, ~: s2 f; h}
9 c$ Y7 ^, b; c6 |5 ^- |* o
- H& G& o% Q* `server {. s$ L+ z: v& ]5 U
listen 80;
2 u3 E; R) U& g- Q% Dserver_name 52os.net www.52os.net;
2 q3 F; Z* L* ~/ [# l
5 z( ^2 Y5 @+ D* R4 a- c4 A- i6 rlocation / {
, _$ `# W/ _& _3 [( _0 ~ ModSecurityEnabled on;
4 ~9 R x8 X# W1 k2 ~! t ModSecurityConfig modsecurity.conf; " I7 I7 f, @$ q8 x1 p
, T- g# r; d# N% v2 {3 f) A
proxy_pass http://online;
7 l' u4 ^; X# s/ Z) S: Z( o( ` proxy_redirect off;
9 U6 Z; h7 ?2 c6 C" B' B, W proxy_set_header Host $host;9 o& `# X8 X7 u& p
proxy_set_header X-Real-IP $remote_addr;
4 ], g" p8 a; W proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;! }% I. W" X$ Y B$ g
}) z1 s# k: ^, ~) E
}4 f9 C% Q: ~+ C" `$ @0 h
六.测试: f; m" G# m# T X
2 Z/ R( K+ b9 s ~1 n: Y& n* _" Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ G2 V6 n1 q1 G# Y5 z: f X: J; e3 _6 V3 ^, Q4 n7 b
<?php
. o, J9 ~, P# ^ t phpinfo();
# H: V- x. ^$ j3 e$ F?>
1 A, j& ~# z# P% |8 @在浏览器中访问:- R9 H0 V% a8 S% l! N, I
( C2 T' k% ~. U. C* W& Chttp://www.52os.net/phpinfo.php?id=1 正常显示。& }/ n/ e' W0 \/ S* m/ [$ c
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ d/ V2 s. A, {% O, L4 f
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 I1 K3 V5 W% j+ {! y9 a
说明sql注入和xss已经被过滤了 L( |# i+ F9 t2 G# A/ ?" Q- l
+ j' R3 @: k$ z$ B
七、安装过程中排错
3 u9 |5 V' H+ G( Z `3 @
( c' F8 F9 {0 \) [% [* w5 F. e) J: i1.缺少APXS会报错
" h K. n: R( p, L: H6 D0 |5 P4 k9 N2 g- r
configure: looking for Apache module support via DSO through APXS
8 `) J8 X9 M+ E' [( _. f- cconfigure: error: couldn't find APXS
9 a) I' c! d+ papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; S% l% U" O3 U; v4 b) L解决方法:2 f; \" }2 q6 g) w1 p0 u6 f
1 g, d3 \$ L/ r8 u) f
yum install httpd-devel& |. M# b# u1 D1 ~6 y7 l& _
2.没有pcre
; r( ^, `3 U7 J$ f0 \1 |& }7 O9 Q' N& `* G) I) g
configure: *** pcre library not found.
2 _6 E8 }9 O+ rconfigure: error: pcre library is required
. l5 O0 ]( R' H1 H8 t( |& q/ M a9 E解决方法:
9 S6 h- g* w/ S; Y, y1 y3 J; i: |; L, z
yum install pcre pcre-devel
+ b$ R0 w, I c0 z7 d4 k3.没有libxml2
% \8 \' ?, Q! X: ]2 f1 M9 L7 F% Z
7 }0 z' v4 L( z ^5 o0 F: j C. H m% M Z9 Z8 M+ r
configure: *** xml library not found.
3 ^0 k! T I) c* ^configure: error: libxml2 is required$ h4 R" ^: y: F& i8 {+ V# W) o
解决方法:8 j# |' d8 w; r" H4 \
# d8 ?3 \0 [$ w# m/ I3 x
yum install libxml2 libxml2-devel5 q* l$ ]5 ^8 `1 H( [
4.执行 /opt/tengine/sbin/nginx -m 时有警告
d% f2 Y( s% E8 s; G: m+ ?# H" P0 \: d5 a3 z
Tengine version: Tengine/2.1.0 (nginx/1.6.2): ?& [4 g* T1 L( q( K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 A5 ^" q6 u4 g; `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# w- L; ~; O* e6 n
) ^8 K" g( o9 q; K& M3 w8 ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% t5 \- v: O. R9 p$ [ f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
+ N) R3 T% N, m/ a3 J8 V* z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ y, K. v, I2 z- j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' I2 V, n9 q9 ], `4 c( p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 E& R, X/ T% ^; r9 f" \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. K, n6 V7 Q: _3 z% a
解决方法,移除低版本的APR (1.3.9)
9 G, l1 N. T9 v a6 B# Z7 p% n9 s) K- G$ ~+ t
yum remove apr$ {! O0 T+ y% g) e2 I/ H. s3 G
5.Error.log中有: Audit log: Failed to lock global mutex
- S1 [% X. x. ^+ T
9 [& |9 A: F u, N% c i7 h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ W4 C" F9 ?) V$ x$ Y5 E* yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: u8 M6 X' b% B! l0 p2 K
解决方法:
! @" ]8 _$ d4 U; _5 k7 {编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, X+ c* l9 M/ D* k
# w3 z3 v9 _2 @3 I2 jSecAuditLogDirMode 07771 f& `+ Y E' ^2 g" @
SecAuditLogFileMode 0550
@& Y8 t4 C) uSecAuditLogStorageDir /var/log/modsecurity
$ Q- M1 z4 ~/ z S: n* a& K# LSecAuditLogType Concurrent2 ?# }* P& ^4 C+ L9 p
参考文章:! Z* m. _ P. f
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX u8 ]9 b$ h2 @4 ?& b9 Q; A8 F
http://drops.wooyun.org/tips/2614 |
|