|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 X- r% r/ k/ v- q. H6 z- c3 Q' M, w [3 J& n" c" k; G
一.准备工作
/ R( R' n% e) e _9 K7 \- a$ w c; c- p, H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ U$ C1 Q! U" p7 c+ {0 C6 p0 f
- ^$ p( r1 {8 n# o9 h8 j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ S% r) ^+ Q, R! z, X2 m! u4 p# L1 E3 B8 W: R% R$ I1 W
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 A% x* n$ C! I+ N$ v% n# q9 c2 U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 W. \; R- h( X) g# f
4 c1 k3 C$ _1 f8 e5 H' Y; ]/ d依赖关系:- u4 |- s6 F+ x$ m
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( \8 S" [6 W( ~1 e3 a" _! n) Z5 M( Z; L
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) ?- I( I$ F! N/ y4 f# x
modsecurty依赖的包:pcre httpd-devel libxml2 apr+ I9 u( A, w0 I! ^
( Q& A# n* Y" d9 V0 fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel( R3 y9 B' J4 k$ [) V
二.启用standalone模块并编译" z7 G5 `' j% O5 g
`4 Q0 }" J7 }; D# l下载modsecurity for nginx 解压,进入解压后目录执行:$ R7 {* ^$ x( I. p- u5 M: H$ C- @ v
+ E! q7 z! [# k8 u
./autogen.sh
: X) f8 y+ i8 |+ S- j: h+ v./configure --enable-standalone-module --disable-mlogc
8 l1 i7 }/ J' ?6 I; Dmake : A' o( T3 ^, a: T, ^$ ~
三.nginx添加modsecurity模块
# @3 y/ V6 G2 y( O u: J! c U, L( C; Q# E$ d
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* F2 ?( M' I0 P7 `0 r
; L3 s' g9 H* I- O9 f- M( S./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
* c& T6 I3 W8 w+ Q: x; Imake && make install
$ Y9 o$ }2 ^; {! ^. K, {+ ?$ P四.添加规则* V0 _1 D& Y2 x/ n& Q* D
9 J2 W& B. s2 c# Z/ R
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' r" e: }6 e" I+ l G6 _0 L5 j3 V3 }8 J+ ^# ]
1.下载OWASP规则:
3 K9 f" g, Q+ a- z# B4 @6 A* p4 v( a" M, \: T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' T0 k: `4 `% V0 M
' G1 Q, ]5 U% l: Nmv owasp-modsecurity-crs /opt/tengine/conf/
5 q9 z9 b( l7 s- T1 ~0 r) t5 o! u/ j9 M) u1 Y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) k7 |2 T ]9 A# j) W$ T- w4 Z2 V2.启用OWASP规则:
6 E& A) C7 X" \, }
+ k- ^; F. E; F3 C" v1 R/ P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( M0 U8 P6 }7 f3 \5 m0 b- i* _! _" W( E( o4 @0 J! ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) t% s: T3 d; \9 F- Y
9 B# w1 S6 N! @# bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! g. _8 Q2 j$ u* i! y/ \5 R% x9 o- H7 V2 s1 \' M( c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
Z. W+ f z5 j6 X7 sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( B& S2 [# f+ ?3 _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 E. U8 `' T! p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 L* F) O5 K% p8 e/ F2 gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 B/ v6 X" F' R5 W1 ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; \1 `5 q1 O7 Q1 b! O$ }: H3 BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& L/ q. B/ X( d8 `& N+ E2 F
五.配置nginx
: c3 B( p8 K$ Q5 q7 C7 D" {5 Z$ T1 {3 L7 p/ v& }2 k. M9 r: a1 M
在需要启用modsecurity的主机的location下面加入下面两行即可:
, K+ \! F A, `$ e5 p/ g. H: T
; L4 c- ]# Y* D8 X! rModSecurityEnabled on;
% B6 O$ E3 A6 ^+ a* IModSecurityConfig modsecurity.conf;0 y3 p4 I6 A- x
下面是两个示例配置,php虚拟主机:
7 a7 r0 z0 Z; _ r/ {% s: m0 v: {; _4 U1 x2 a% j$ A$ K& V
server {
( }, b4 }# r$ U- | ~+ } listen 80;
0 J: ^7 v; D( F' e. k- l server_name 52os.net www.52os.net;- A" o/ r, ~0 Y$ T! S
1 `. s1 c0 B* h# a: t1 I location ~ \.php$ {$ ? M2 J, J- u' O2 J) v1 B/ |
ModSecurityEnabled on;
6 V. q( z& ? V+ U ModSecurityConfig modsecurity.conf;. A8 p G% V/ H! e2 m9 U1 `
P ^- j6 ?. m! X/ k/ O: }
root /web/wordpress;
}6 a( t( x7 x( @* O$ U index index.php index.html index.htm;
+ b* u$ c) c% q" F & M6 }; Z+ R3 \6 h
fastcgi_pass 127.0.0.1:9000;' p$ O7 s: G1 D& [+ M9 N
fastcgi_index index.php;
g* b" S* E8 W9 F4 J1 H8 W fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 K. m" @. O, K! ~2 X W" \ include fastcgi_params;
) r- j7 E3 _( H& p }
. f/ W7 G) h& Q2 d' @ }
$ e6 u, r/ d; H6 u; r ]: k8 h* qupstream负载均衡:
# Y/ C3 h" J* L, U& i A" s& R7 Q1 H
. S& l+ X) K5 ^' V. [" {2 W! Rupstream 52os.net {/ p9 n; g# b1 [, a' w
server 192.168.1.100:8080;
( W& |9 m3 D. P0 g7 \ server 192.168.1.101:8080 backup;! B, j5 w) {7 p
}* k1 a6 D. p0 ]+ T$ T2 a
7 C% D9 R' ]6 O+ }, A; V# ~) b
server {5 `; ]3 m- n5 j
listen 80; X8 i( N$ R* Z1 K2 i
server_name 52os.net www.52os.net;" | u* K+ o- l; g# k/ }
0 E/ ~! K( b% x( {( Hlocation / {& a) y z$ m; @3 y; n, X
ModSecurityEnabled on; % p* T0 k: V7 W, o' |; }7 _$ B
ModSecurityConfig modsecurity.conf; / @8 v* W* Y- @ a- [4 N+ v
: P: `; Y" C/ U. E% Q' p+ d proxy_pass http://online;
$ ^6 J) |2 t: C* n3 U. {$ D proxy_redirect off;$ F; L1 E) ]) r/ G& d
proxy_set_header Host $host; u( n- \" {* E; u! ?2 R+ U) g
proxy_set_header X-Real-IP $remote_addr;
" _7 J0 u% T5 w proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, N* b+ c6 n; x. e" @/ ~
}5 |( G/ L- w2 P6 _
}
- z f% i1 `4 G4 |六.测试
( N ~% C2 f) t2 k0 _) P% h
, t) W0 Q- \1 C0 L9 B4 y! z5 p我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ ?# s# g) I( o6 s7 U. Y* {" t
- o8 m/ S- Y9 S' m, Q: y6 h S, l2 a
<?php
; D' N2 q8 N1 B$ `& o; r; \. i phpinfo(); 3 P& z# W; X9 F
?>% k% J# H, B& W: F1 a! H
在浏览器中访问:. G9 M" n6 ~4 F4 [6 v5 u
7 F& i; ]" d" p# u0 e# O( [http://www.52os.net/phpinfo.php?id=1 正常显示。
5 z( h I7 U N& w: O% Z3 U! ahttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。7 F2 b" R h1 T$ o1 A
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。7 Q6 c8 P4 B# M, t5 e* h) F
说明sql注入和xss已经被过滤了
) |' c4 F D+ L. _ Z0 g
: q8 X4 d( A6 l2 P4 U0 L* g七、安装过程中排错
7 R7 J, p2 C; ^, g9 H: J x: i& c1 J+ e. ^6 _
1.缺少APXS会报错
1 d9 w' o( h" R$ @( r
3 j/ v k \3 k1 s, Lconfigure: looking for Apache module support via DSO through APXS9 z) P$ x, Z" ]" q; L0 m
configure: error: couldn't find APXS0 r( [$ |% N! c3 K0 ~
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
. y/ N6 R7 |4 v/ M) X解决方法:
% n+ @) `4 X" U+ S
" h8 E9 f1 Q. m' s8 J3 k+ ? qyum install httpd-devel
8 \$ W+ M3 K' y0 I2 U7 l) P6 J( @" U2.没有pcre
. G& c4 [3 j1 s, v% |0 a' f: m/ ?
9 D. @/ ?+ Z/ S* v) Q; Oconfigure: *** pcre library not found.
& w, j" B/ L s5 G9 Vconfigure: error: pcre library is required9 p4 ^/ H: z$ H3 h6 x6 u
解决方法:
6 w0 w) H0 D, P' E
4 y, N4 P4 b& q* X" y! oyum install pcre pcre-devel
5 }. I, P& z R* L; D& T3.没有libxml21 e( X, H' B% z6 ?& U$ A
6 Z% d0 L) p1 d' e; n- }" O$ X1 y5 H0 d/ m
configure: *** xml library not found.
0 V+ B% @+ D# @ [" o! ?configure: error: libxml2 is required
/ ?8 H& ^0 S$ n, u解决方法:
4 r6 i* n4 Z% D+ S6 k
+ o3 \4 L" _5 I5 cyum install libxml2 libxml2-devel
2 _7 T' f! G- Q6 y4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ _: N" c/ L8 t7 N1 l b/ G* l$ f$ y9 C- E+ t) J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) N6 @* }- F9 q' [9 h* w5 N& o$ Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!: I' K* R4 A6 m- m! U B9 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, e# U% T+ Z! f
8 ^1 p0 S: K$ r$ I5 r3 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, {1 f# ], I4 Y$ [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' G0 e% t$ V3 _1 N2 k2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) i5 P \( i7 [& m% l/ I) I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"8 }9 f7 K ?6 A+ B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! g2 H' z% l& r3 G# r4 v" k V
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., q2 }3 k0 K S+ Z' j) T
解决方法,移除低版本的APR (1.3.9)( s' ]( j" C+ f! N* T; b* m
# B; F' \1 e) {
yum remove apr* p9 b2 @3 M9 L& G
5.Error.log中有: Audit log: Failed to lock global mutex9 k1 I& x9 f: X
y1 a! `5 I& r6 q/ Z8 @( W2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' e3 D7 ]/ s( hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% Z2 Q6 C C; g6 r! T" w2 x
解决方法:
. l$ a# ]/ D3 i: [0 V9 l1 K4 B编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ l+ Z( @$ }* j% e# ~! Z8 G' l
V, Z0 Y& _2 p, SSecAuditLogDirMode 0777
' {( o# m$ c" ^- I9 n) p/ SSecAuditLogFileMode 0550
8 D% V( g) o: Z& M1 `SecAuditLogStorageDir /var/log/modsecurity4 [& K6 S! V3 \' ~
SecAuditLogType Concurrent1 g% o7 e6 a% v0 @$ k
参考文章:1 ?: J( [, Z1 b9 _$ B9 [
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 C, c; r; _; F7 }' p8 u7 Jhttp://drops.wooyun.org/tips/2614 |
|