|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 x7 x2 H% _8 y8 U* S+ u
* n$ n4 A; K# `1 j
一.准备工作
6 Z7 \& J9 E6 f5 l+ i3 B& i. n; I) S2 a4 Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! c$ E9 c: _4 i$ ] b
6 _. X. l- v) v3 jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# f5 X6 H. Z* c, G
" E# U$ F' f O5 Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ U4 S- \+ Q* G! g2 ?
9 ^9 B9 B: [3 n6 @& U& U2 \ y/ Q1 VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! x* W2 W3 Z3 k( T" z c6 f) g' I; k, X3 q1 F; o# Q
依赖关系:/ |2 \' a& ^3 O6 K2 g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* [! _- P' N, z6 i4 q
: e& O! @% e7 L
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, l+ v. \+ Y" `7 s" P" Q2 u1 ]
modsecurty依赖的包:pcre httpd-devel libxml2 apr
, X& u$ E! h e; K- f+ d, m
$ p/ E! D5 g; Y% V+ Wyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ Z) B# J4 w$ k" L6 u; A& n二.启用standalone模块并编译
( ] B3 G+ [8 p2 K' i/ [* a( m" n, W) h) d9 @% Q
下载modsecurity for nginx 解压,进入解压后目录执行:
. h, A( @5 i7 T% D
4 Z5 C9 ?1 P) W0 D" C' [, y# |& w7 Q9 ?./autogen.sh* X p8 c6 O3 Q- f# b
./configure --enable-standalone-module --disable-mlogc
) x2 z. c/ M' s! X9 C" smake
/ d) x5 x8 Q$ I) Y三.nginx添加modsecurity模块
/ T$ B+ g; a5 Q
# n4 T. h+ y& G N! i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 I% G8 G9 T& R5 k6 d+ ?
( f6 r# w! Y. R0 G% v1 S* [
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine4 u; C' a3 i$ b( q& K
make && make install1 J2 E2 \/ T" C2 z3 k
四.添加规则
/ s) T- S( S/ ^3 _; y7 M- E- }+ v5 R7 t! T O' T
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- L4 U' B) z0 r$ @& i1 p
$ A& z; h* H0 e4 i2 B6 K1.下载OWASP规则:. D; h# s8 K. d( u8 A m
' O2 D; Q4 F2 T$ {5 A" G6 pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ Q6 f, S ^- [% [& x$ k
; }( V1 N. c6 [3 N& emv owasp-modsecurity-crs /opt/tengine/conf/ }) S n+ G9 q
. O' N; j. O ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% J9 m4 f( A! G$ w& y
2.启用OWASP规则:
6 g- [, y q/ o+ ^ D* R
: C5 [# W. X7 Y- z4 v复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( n- G. p) c% A0 M$ g+ ^
- m [% H- \3 y; T8 [
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on z& M9 O: D4 k& k
& z& g1 B: b# C' t+ I) r) h
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 m) y b" Q5 ~( _. r
* j, X% r! }- Z, @0 {
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ o, i5 g4 a; j7 n: {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 m1 T3 [- y& l% m9 A9 _' nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& s2 C% q S, i+ Z0 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 _# j! ~( b3 q5 h. uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: F# W* Q- \4 P0 J6 a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 {, k7 |6 e. P2 aInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% ?! r; ~6 g7 i+ S, J3 q5 _五.配置nginx: q0 c1 Q' m9 ~" r6 @4 S* d
0 P _: y& y9 @. m: [+ w1 E% s在需要启用modsecurity的主机的location下面加入下面两行即可:6 n$ ^* C3 L7 ~* g( K8 Z
9 A9 t& d: r F3 }9 YModSecurityEnabled on;
6 K) d, ?( ]& m, u7 _/ Q% |' IModSecurityConfig modsecurity.conf;0 U2 L3 R% _: X2 L
下面是两个示例配置,php虚拟主机:( w, I4 h- h3 c$ M1 { Y
. ^7 y# e4 t/ F; K B% V0 L
server {* {: K4 U/ }3 v: k# @7 p
listen 80;- Q( p/ C- S5 L+ X
server_name 52os.net www.52os.net;; B: O- d2 l8 I% w% d
6 p0 D) m$ a+ [6 s location ~ \.php$ {" V. j+ T4 {$ U) B F* G
ModSecurityEnabled on; 9 o! x$ s; K- @- ?2 c
ModSecurityConfig modsecurity.conf;
# }/ _7 z9 c3 A, V# ~4 b4 t0 \% I8 ~* a0 x! C. J
root /web/wordpress;
7 |7 T) E0 W2 B index index.php index.html index.htm;
8 U/ [4 ~% E7 X- z- Y 4 L. m, d$ T* D% N: P
fastcgi_pass 127.0.0.1:9000;
1 U0 L% p$ m) ^8 P fastcgi_index index.php;. B1 |; C/ z8 f$ }: R' r5 D% w( B
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- ~- ?9 e$ Q" ^) H5 i* z include fastcgi_params;( C: r) I% `3 f7 o
}
! `; W# B) w3 i }
: R" v. G7 E5 }- S5 Kupstream负载均衡:0 x* I+ U7 W* b- { [
$ z( ^1 f; P4 N5 s* [6 j/ |
upstream 52os.net {- H# @% _) i- E2 y3 P
server 192.168.1.100:8080;1 Z- I) [) X: t( Q2 ]6 B
server 192.168.1.101:8080 backup;3 n7 l0 B5 @3 ~8 K' O7 q
}% T, Y2 _ e7 @4 v
, A& ^4 P Z3 X1 [" I0 |# J/ Iserver {
. C* k# R* Y/ b! S4 `listen 80;
! v$ ]# i- F+ k: K( \9 |server_name 52os.net www.52os.net;
& \) R; b1 \+ T. ~9 \/ k, z! |$ |% u4 u: z5 J8 p& F( \
location / {: w5 {" f9 ]4 B; l! B5 w- a9 A4 _
ModSecurityEnabled on; 2 Q/ g: C" ?2 `8 [& z# n
ModSecurityConfig modsecurity.conf; ! t# q- Z: D3 d, u4 h
0 Q9 G% ?$ Z. v6 s9 @. _2 v proxy_pass http://online;3 _" {! ` v: ?% h+ X4 o! @& B
proxy_redirect off;
5 R( b. S; r& `( P4 T1 C9 l3 x# e: v proxy_set_header Host $host;" [6 O3 d1 _2 B$ r, l
proxy_set_header X-Real-IP $remote_addr;% n7 \% Y; l n) u: _
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;( s, o* d" t' ]8 j9 z, D& Z
}. a5 M& |1 i: c9 r
}
2 D! d P5 A: B9 {( b六.测试
* y* r, Y! }+ G- X1 ~: J- V
: K' H6 O0 Z' ]& |2 R- P n/ H我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ x/ x! J3 E) a
7 R7 g1 Y5 ]# B( W- S( q6 P<?php [+ X2 l* H1 M. u/ I; Y, r) n
phpinfo(); . i- t: w" W: H8 m
?>4 M X/ p+ @" Y2 o5 ], N) j
在浏览器中访问: T4 {& c; ]$ c! ~# m
" j: Y- i/ p, ]5 B
http://www.52os.net/phpinfo.php?id=1 正常显示。
% U; ~ M2 K" Y. y9 ehttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' g( ^1 e7 |: t+ g; Z, zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' m5 f' X( K6 ~; m1 y- X
说明sql注入和xss已经被过滤了0 N1 Z1 L8 B, ^0 h
+ }+ Q. [* ^' R& }. v
七、安装过程中排错
2 a* ]$ ` }$ ~: }9 o1 W
4 I4 e, m, r2 x% s* |5 {1.缺少APXS会报错
4 o: K1 g4 \/ M% U6 x- ?9 S, [# c' u1 ?+ \6 N5 `) p
configure: looking for Apache module support via DSO through APXS
4 p% b; I# Z, u$ L' N" Gconfigure: error: couldn't find APXS- |2 m. D: Q7 p; T2 j o# T
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 x2 o9 m( X5 _) q1 o
解决方法:! x) ?0 Y; |* H* }! m; L6 o0 {
1 A i* d# _( Zyum install httpd-devel
( T I( L0 V! {7 E9 I2.没有pcre
- ]7 g% d3 h9 j' Z3 l% j
& c. H. F. s2 D! q- Q; o7 m; Gconfigure: *** pcre library not found.
1 W6 y' u0 B( e! p( w$ ]6 dconfigure: error: pcre library is required5 f( y+ I9 u$ J8 a& F @4 ?, e
解决方法:
6 M5 r \1 |2 g: z3 N* P% t: w+ S( L
yum install pcre pcre-devel, W8 p- ^3 {1 @1 g$ b
3.没有libxml2
2 [7 u3 B! @' k* y _8 J' G l# {) {& ?5 T, {* \: p/ z5 X6 P
7 U/ N- G# t, ~- _
configure: *** xml library not found.
4 M( [* f: D4 v& O# k; l# Pconfigure: error: libxml2 is required
3 ?. w# B) e% {- e V8 _解决方法:
! q2 ~! N, a3 p2 O5 f' S* Q B
, ]) a0 P* s+ {2 Myum install libxml2 libxml2-devel0 `: h; H! S9 [8 a
4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 a1 T" Y3 u1 B; D+ v
1 C0 M/ _2 w6 {Tengine version: Tengine/2.1.0 (nginx/1.6.2): U3 |) Q2 ~' R3 M" d' W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# B. D5 ^: |! i! P
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& A: J" C% _2 l ~
7 F' @, s. g3 `0 h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 Y8 u+ ?7 M2 g2 Z! W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
% m( }9 c2 w* t) P2 q$ B- s2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; _" u* K9 S) D9 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( v. ^2 n7 ? o+ N5 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
0 T3 O- \& k& K4 H7 s2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' W, D6 S7 u. b
解决方法,移除低版本的APR (1.3.9)
6 f( O9 a: h, r* ?# X9 j5 D
6 _$ @* U8 L+ A! y. q" Oyum remove apr
! ^0 y m+ U ^9 M& x- D5.Error.log中有: Audit log: Failed to lock global mutex+ z3 @# L9 c, O- ~( N. C! l
" k! V' e* r4 n2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ' r8 m3 b5 d- }( c, m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 L6 P; y/ ]- C解决方法:
( v( v" g/ i' ^9 y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 V' t9 o% Q: _; d, ` l2 Z6 r0 d9 D& R) f$ J6 p5 e5 f5 T3 C
SecAuditLogDirMode 07775 m! T# q$ z4 i8 U
SecAuditLogFileMode 0550
, c% b& |8 w" l, y- P5 k. dSecAuditLogStorageDir /var/log/modsecurity% V L ~+ \, J* x- C" @
SecAuditLogType Concurrent: V8 a$ Q# e9 _* l$ b
参考文章:
3 b- s8 r. N# Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) R/ ^' l" I1 i% X5 A
http://drops.wooyun.org/tips/2614 |
|