|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 v: u7 `- Z0 R9 E
v2 E3 ~; \' t% C
一.准备工作
1 N2 D; T/ U" }+ u( O8 k; ]( V% ?: g7 N: o1 B
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% }+ J% u' \! P0 r v- c+ U3 y; W( |8 V( o- P) ^8 t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 d9 n! l: A0 Z2 m
9 r! u& }+ e( L6 } mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) ^3 A c2 {3 v( V1 Q' |* _
1 L5 [$ J+ U7 m R% S
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# d) D5 l# Y h$ `" T' b& S+ y
$ b1 p: G+ ~5 }' ^9 V依赖关系:
, E0 J* [! v5 j# jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ t' E" f+ ~) L" ~
7 \+ X! I' p, H9 I/ I; Pyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
0 m9 O$ l; K3 p& {9 U* G! _; Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 Z$ q( O4 l8 I- D/ ^# D/ u$ c9 d" q2 b
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% N* l) ^% U) M二.启用standalone模块并编译
( R( F( f( m) J4 C2 K; c- k8 r9 r4 ]# h7 N1 m% J" C+ L4 m3 @$ h
下载modsecurity for nginx 解压,进入解压后目录执行:' e4 S/ C5 O. y& b) h/ X& t
/ c; V: |9 O" H: W- [+ J8 U3 ]
./autogen.sh
- j6 H$ H, x* @2 o/ X1 \7 {$ K./configure --enable-standalone-module --disable-mlogc
. Q$ y" g' m, h5 e: [% i. {make
4 \( o( k3 V$ i& W8 `# k三.nginx添加modsecurity模块
$ i5 b- q6 |$ I1 c# v f5 I) @4 U" o% q1 ]% H* B. g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ W& E( R4 x1 g* A. y! l
7 z+ l1 N, q# V1 \# W$ m% X$ S6 F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
* Z6 W( ?$ l$ y% X5 Y" Fmake && make install
: |3 S+ y6 w+ h' i, S3 h四.添加规则, o/ ? s& k+ f9 \* [" Z
2 Z. ]+ m* b) ~9 @' h u; b- w$ Y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 L5 p4 K& `; U! O& a: y5 F" A0 {/ r# f
1.下载OWASP规则:
! b0 `5 Y2 }# r4 h3 a6 q* C) P! {+ V$ z; c9 b3 R
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' T* n9 k2 w/ V; S( f- M# I
/ T% e- D/ [* A2 J u3 |mv owasp-modsecurity-crs /opt/tengine/conf/
% {5 c0 P7 r$ Z5 J
- Q3 P: h- }3 n* bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. H! B: x3 Y8 [3 ~: [. d
2.启用OWASP规则:" E2 H+ `6 {) O. C& {! z
4 Z! r1 F; w4 E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, g% W& F9 m' A, ^
7 j4 i' }" ~1 ^$ ?: N" C7 m编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& A3 J3 F6 g! P) W) k! n( z% e; m; {7 x. a9 i+ h
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 U5 E7 ^) z7 u3 E0 A2 Q" e) S. z) @& Z* W
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& g/ Q! x$ l% J/ }2 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 ~( w& s, W; s* v- WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 m V. f" X$ k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ q- B3 |- l) o; k2 J2 w+ l$ Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 H% [0 F' N' Y+ e# gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 D5 p. d$ t8 _: e5 U4 H% ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 y4 ^- o( C. T4 R5 C
五.配置nginx
, n+ V; N+ c: ?3 U1 d+ Y% a* M6 s) l
在需要启用modsecurity的主机的location下面加入下面两行即可:& N) c) u/ p' M0 F6 k5 T* M
8 s" Z6 X6 |) ?
ModSecurityEnabled on;
8 ?. F4 o& o- OModSecurityConfig modsecurity.conf;! V/ T7 d, l- z% M
下面是两个示例配置,php虚拟主机:9 b, T, r& A- x4 a) S
+ V) ?/ C' k% e# n; u9 z& x
server {% j! k9 @, X. ]: f: G" }
listen 80;9 f2 {6 u( M% _$ N
server_name 52os.net www.52os.net;: R3 _, B, p/ K7 M5 V3 e7 B
% H" ~0 M( c( C location ~ \.php$ {
8 @# [0 ^7 ?# H ModSecurityEnabled on; - y; M# |9 W6 y% o5 i) `9 q' A+ x
ModSecurityConfig modsecurity.conf;, `/ H6 q6 O7 I" y; O
* }: }: C& I7 H6 ^: w* |$ N root /web/wordpress;
7 l. ?7 z( ~# f index index.php index.html index.htm;2 [# i) j7 m% O, L$ Q& \
" U9 [% V' n7 g/ h. a; t
fastcgi_pass 127.0.0.1:9000;
3 u4 E4 Q R& s; d ]+ i$ M8 a fastcgi_index index.php;
% b" y4 b1 ~5 k2 a9 W3 u/ H6 A fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, h! d+ L, h8 h9 U/ b3 q# K. K
include fastcgi_params;! s5 t; H1 F5 J' I- X! w& h ?
}5 D# E# j/ M) f: u. s
}/ n1 N1 |, E& C6 ]
upstream负载均衡:
9 Z( _1 Y5 E( z5 u' w, z2 W7 X2 y% ?: v; K! R8 Y h
upstream 52os.net {# P% ?" \ }; K) s9 g5 I) d
server 192.168.1.100:8080;
: _6 q: y2 @/ w2 M8 D" u server 192.168.1.101:8080 backup;/ \' M6 S7 L G1 l
}
; ?; K R( X6 g7 F0 N
0 H/ R! l1 o* B0 ~server {
4 a5 W; ?4 }% g, vlisten 80;
5 G; c {2 f+ j7 cserver_name 52os.net www.52os.net;, ` P% [# n# a1 l) H
/ U2 }% a- k& C- M; olocation / {5 H+ E! p7 a5 z
ModSecurityEnabled on;
" n$ z3 F/ P' v# f ModSecurityConfig modsecurity.conf;
3 _9 ?% t" {+ \ v
& T7 J: L7 d5 P( w6 l8 D( n; h. d$ R proxy_pass http://online;, O" _9 q5 z5 `/ J! Q7 E
proxy_redirect off;
5 g8 [/ F1 e" ^) { proxy_set_header Host $host;
3 G) c' z2 X3 U0 q r proxy_set_header X-Real-IP $remote_addr;
/ r: F) A' R# O$ \ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: Y% i9 n% |/ ^6 |8 S& K0 c }5 T9 Q5 O& p2 u" G! f3 _ J$ o8 @6 j
}
5 C, @- p% v* V六.测试8 F# }0 D5 a+ j j
( u" L1 L8 Z- [* L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" N( L1 p' n+ k. C
! l0 |6 S. J% j( E( ]<?php. A/ V6 J8 V/ E- M8 [' g: o2 w
phpinfo(); 3 Z) P" Z$ C0 L' t& U
?>4 i& E6 z9 m- S4 P. Q; R- ]3 u2 w+ N0 Q
在浏览器中访问:
2 j' O; J0 g0 v* n& A& O! _' c
4 T* k; w3 _* ohttp://www.52os.net/phpinfo.php?id=1 正常显示。4 n6 {. M( M- \) k2 |' o4 j6 r
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! e+ C! D/ _: i5 C' Z' ]http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
% v. A+ I8 I, v9 J/ X说明sql注入和xss已经被过滤了
; o: \& ]" P) ~3 W' Q8 s7 b _& P( F. h0 c- `- h" P4 o
七、安装过程中排错
) ?2 z+ I8 X" w: ?3 t* L+ G4 {
- Y+ e- x# x- ^4 v' [; M; l+ ]1.缺少APXS会报错
( C$ I/ A- D6 o% ?
& D) }6 ^% p/ I; K3 G5 n: ^1 pconfigure: looking for Apache module support via DSO through APXS
/ A6 U& x8 K; Y' e. j% r, econfigure: error: couldn't find APXS& K1 A0 p8 I/ M1 R H) x0 v
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- s9 Q& p L- q' M- E7 h
解决方法:
2 w4 o) M" \7 {# q$ V+ f1 R9 h: C. C; X" S" e4 ~+ y
yum install httpd-devel
. Y1 J$ z1 ^- w2.没有pcre
- ~2 P2 o- c( e k9 y; j1 ~0 t! h9 z8 Q7 o% J7 {; H& X
configure: *** pcre library not found.
1 d8 N$ F, i2 Q( y7 S) {configure: error: pcre library is required
, a. ^4 I/ U' }2 T解决方法:/ H$ E$ D. m- H- r
! x$ d, S9 i( x/ v2 _5 e$ Tyum install pcre pcre-devel
3 L; x% B1 g2 P3.没有libxml21 r ~& K% b$ M& J" {
- {- y8 A1 D8 M4 ?
7 s( n% U7 z" H) ~% Cconfigure: *** xml library not found.
7 T6 o9 W) s: u7 I) f; U6 K! n# yconfigure: error: libxml2 is required
3 A9 h$ h0 @9 V* ?, P: b% F解决方法: ]0 Z! U- }6 I5 |- E$ k
: g7 ?1 u# @2 H p5 |" j
yum install libxml2 libxml2-devel
* T% l- }7 U ]1 c7 _ T7 ~4.执行 /opt/tengine/sbin/nginx -m 时有警告
% U( t( ?! o( ~: D
: M- b9 ~$ N5 g) n- ]& p: oTengine version: Tengine/2.1.0 (nginx/1.6.2)) J6 b5 R0 g' M7 |- ]+ n K, N [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! Z! u; G4 \7 z" i0 ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 m% h( j" |0 I' {& D3 w* D
3 k5 T. _& {7 C( q/ A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- f/ }$ y: r! u% @9 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( _* _4 e! g, d9 g, {
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% O2 u) R y3 P! r B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ k/ r( z3 f' K* Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 [' p6 y( R8 u; o# G( _
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 R5 [; |0 a B. l4 h
解决方法,移除低版本的APR (1.3.9) ^, b5 f A( ^) l! p' \$ g
" O6 ? U# {! [" |$ o
yum remove apr
# X" t, G. q) R1 i _+ m5.Error.log中有: Audit log: Failed to lock global mutex
) y! Y0 F6 G% i, ^0 y5 a3 |) Y, J) [4 B1 V) t
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
v9 \3 P5 C5 e1 U i- v8 Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% L$ ]( z1 V6 C# v; P* C6 o7 ?
解决方法:) i+ i9 z( w, g; W. g% @6 v4 f4 e+ C, |+ Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 e& [5 m5 p; G4 [
1 @: {' _* W% mSecAuditLogDirMode 0777
4 e+ a; r; _8 W3 N, HSecAuditLogFileMode 05503 A+ n' w2 D( D/ F+ ?
SecAuditLogStorageDir /var/log/modsecurity3 [# B' J$ y; J8 c6 w7 x
SecAuditLogType Concurrent
3 N \. Z! R" ?: g. n6 O参考文章:
9 P( {6 h- ^5 E3 X8 H* Whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" l0 E- G E1 U) N& j5 I5 Ohttp://drops.wooyun.org/tips/2614 |
|