|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# r+ {& f8 `( @; A* @+ }3 @3 I# i- G7 T3 i
一.准备工作8 t3 u i; G5 B
' I5 O& s, k% U' T. W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* t3 g/ ~* F/ y5 v% \) F$ p# C. f
. G" [8 |% [: n7 J3 K% ?2 v) C' d
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ L T" \. z& W: [3 T1 b. ] m( v
N$ e P4 Z1 K, K Q8 |& _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 o+ e% p: d% Z2 i& x0 e# N( k8 R) H& _
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* M! r2 y& l5 s! m- `. F) v! a4 U5 y9 _
依赖关系:
9 t/ S' i2 F# y4 H2 w1 ?% ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 J1 W, j) G; y; o
* | ~5 {$ k- {& Yyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' O4 C3 a4 {. z" ~+ d* O( `modsecurty依赖的包:pcre httpd-devel libxml2 apr
. L& V0 E8 C. E
! _& ] ^6 c/ l/ eyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ o) B( ^ v* \! \二.启用standalone模块并编译
0 g& X8 |0 @( b4 R5 X
2 f# A+ n1 z6 x. n下载modsecurity for nginx 解压,进入解压后目录执行:
! y6 f% ^! N* L' i$ T5 \0 x7 W: m
./autogen.sh/ s( b$ {8 A" k: {4 D7 i
./configure --enable-standalone-module --disable-mlogc
6 w* A+ z+ E C! Umake
) _ k. A, y9 r& b; @" S7 f2 k. f2 V三.nginx添加modsecurity模块
: ?" y7 }& u! N+ G D
: z4 J1 g; p# O) b( | [/ V8 L在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. ~4 B, a! Z7 J) G4 K r) G# B
7 q, K" n& G& N
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% m' ^0 N' E3 k2 i$ rmake && make install% ^# [4 G. n( h* } F
四.添加规则$ M. H/ G) ]' l1 y' q4 C! x
& D, V/ r8 O3 M7 L% dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, N. B8 U' W9 _1 ?# _9 \7 `) T2 d$ Q6 q" D9 w* a! N7 W: B
1.下载OWASP规则:" L7 C; W: w! r! ~/ p
& R2 G; X9 t/ \ P4 ]
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, W9 `) @' p. v) E4 o0 u( B$ Z+ F1 @
mv owasp-modsecurity-crs /opt/tengine/conf/
7 D8 ?: w# ^2 F0 P& {; x6 q9 u# F, @$ u8 u+ }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 V( `/ v' n) g+ j+ z
2.启用OWASP规则:
- ]4 H, h5 u" c! u1 q1 f" w9 e0 ?7 L+ c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* a$ |- P% o4 _( u7 a
& C# W& C# q5 H9 b; V0 k: s编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' s. T9 F/ ^9 K$ p# N
8 d9 Y* P+ H6 lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 k( e( w1 H* O: t8 J9 C: @/ i5 f' e# J' T" N% j( N- Q
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# Q9 M& j' K6 F; tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 u4 ~2 G$ u; O2 O: [% IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 R! z$ C; i' V, k( ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- U9 r" Y2 @% q9 H2 FInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 s- q1 h& w J; m" r7 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 z' i" N* b' {$ z: k1 Q3 R9 E: T2 v
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! ?# T! K- ]( |1 D& q五.配置nginx, K* u+ t( o! Y, b) d* ^0 c }" V6 E
1 o/ T) A! G! `
在需要启用modsecurity的主机的location下面加入下面两行即可:
: S! O# L+ ?# R/ N8 Z/ T! L: @: q0 |- a- u* }
ModSecurityEnabled on;
* |! a4 C6 e) vModSecurityConfig modsecurity.conf;
1 Q! h2 G$ T, c8 i2 [2 \ E下面是两个示例配置,php虚拟主机:! q2 [" ?' t0 Z( M' B
7 s% p: L _/ Q+ O$ \( _# [server {9 q0 M1 ]; P2 p9 \3 B3 W- m9 F
listen 80;0 o5 n8 }. A+ @) q
server_name 52os.net www.52os.net;8 N! h' E" c; D
I( c; c8 {" ?6 Q: R
location ~ \.php$ {8 j C, ]9 N+ T
ModSecurityEnabled on;
3 w% N3 Q' W) m& s# a ModSecurityConfig modsecurity.conf;
6 d2 i+ p/ W' B8 k! C, n v$ A" L' n* Q# j
root /web/wordpress;' c3 R+ t# i) O* W7 X: |% ^
index index.php index.html index.htm;- D- \& X2 D; g' _
0 j1 L k1 r# }; P; q0 n
fastcgi_pass 127.0.0.1:9000;) V1 U; H) c7 J6 h$ C
fastcgi_index index.php;. @) G+ F3 A4 D5 S A( _) n% j
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- a& U# F# D! l! c; l7 a include fastcgi_params; S0 X- n( X& f
}
5 G" c+ @% P# R, w( V; J2 R }# H3 T3 V- x- I
upstream负载均衡:
% H" r( {& t7 N: |6 e. v- U8 Q' w
upstream 52os.net {1 Q( ]7 ]3 V8 N1 Q+ U
server 192.168.1.100:8080;
8 H3 R G5 U' l8 R; { server 192.168.1.101:8080 backup;9 g g9 {# D }; v0 N* G5 R0 a' g
}" U7 X$ b k7 q4 Q# f- n
5 S( w. K& f |" ~server {- R4 S: u8 G; o! I5 t6 e
listen 80;
6 x0 W. H7 J0 i- F* [server_name 52os.net www.52os.net;
! ]: V% N6 Z; N& W1 V1 V7 h+ ~1 e. Y; Y' n4 E4 ^1 m; S5 ~7 C
location / { {" K7 }/ [' C& o/ V* j
ModSecurityEnabled on;
. I' @. H- z) E* _) g. I' w2 o ~ ModSecurityConfig modsecurity.conf; 6 @) Y3 ]* b3 R0 T; W& I w8 \4 U
y! |9 z+ i7 u( V3 d( b; ` proxy_pass http://online;% b4 o8 S5 M& O& @# E
proxy_redirect off;
$ a( ^- J# v, X7 M" ^0 G proxy_set_header Host $host;
9 Q5 c6 ?7 q9 k! S$ T: G" ?- R3 D( [1 p proxy_set_header X-Real-IP $remote_addr;4 C+ x0 Q3 W$ {' r! T
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;( h* f, t) K/ @2 N8 J: W; f
}
& f- p; W/ P5 M* s/ L! D}% Z8 ~. d! z ?/ `, w8 ?; Z
六.测试. D# {' F9 P! J# C/ z
3 P( A$ \. g' h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" u% S9 D; t( a- b0 P8 A( K3 h/ l7 h, V. Z& ?3 O
<?php
* u$ O P6 @ k2 M phpinfo(); ( b# Q1 O g3 y; ~& X: y
?>0 O& Y6 t4 v- `4 _' Q, |% M
在浏览器中访问:& M B _. U! R7 s
9 T1 Z- M: R( K2 ahttp://www.52os.net/phpinfo.php?id=1 正常显示。+ q( d+ h p5 I. r0 S
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, G a( x( @: o8 |( ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) G/ h/ Y* ?& T3 }
说明sql注入和xss已经被过滤了
( W( l( C& @' y! n( H" A0 @3 O# O3 h" }( p( k: X+ ~
七、安装过程中排错, h. q3 u- `. K1 E# q* L+ p
; v3 v; Q$ }- U7 r& \* }) Z" V6 d" t1.缺少APXS会报错, c, G: q; }8 T0 S1 G
; H5 w# X- L' v, e3 X+ w
configure: looking for Apache module support via DSO through APXS6 m$ R1 O! ~ k. X3 f! j
configure: error: couldn't find APXS/ U3 F7 v6 m, ^& V4 d, l
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 L+ I- Y6 J, g解决方法:7 }1 z! o3 Y8 T. y& h Q/ T* Y$ |
5 F2 j5 w; V8 X2 U$ F% X1 Lyum install httpd-devel9 Q5 S8 f( o! e* x' n+ H
2.没有pcre5 k0 t# q) I' S+ y0 l) n4 D+ ?& H
# w7 D. K' Z& a( @& \. K
configure: *** pcre library not found." e) N( S/ h( u B4 l6 Y9 e
configure: error: pcre library is required( L6 S# X+ t6 ^, |8 L1 I8 H
解决方法:
+ n& [* d4 n; Z/ P0 }% a0 y( _( p9 W) G8 C7 w
yum install pcre pcre-devel/ m/ l3 z5 Q9 @5 B2 U
3.没有libxml2
6 n6 _$ Y: d& P: |: f! a o$ c' _4 `% \2 q- ?
0 b' O" i5 G) ~& ?6 d7 Iconfigure: *** xml library not found.
$ [$ o* ], U3 x+ `" F) _$ ?configure: error: libxml2 is required
$ N6 T6 {" |' t9 m解决方法: k9 q6 U0 i" o2 r8 |* v
% F V. k4 c' Dyum install libxml2 libxml2-devel
& A6 B/ z- s$ O# u& w4.执行 /opt/tengine/sbin/nginx -m 时有警告6 Q- R {* W. i
! K. y; ~- C4 w5 S5 D5 Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( H5 d( ~, c" C6 N2 j- Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!" N; E- j ^ m/ t | x
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" j# v; `6 x* ~" K9 Q) q/ W0 Q6 a. E' L% V5 S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 f) ^0 n' E! ^! g+ {3 u) T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
% z- I# F3 N. J5 J' w3 T; {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 W, M) u: W5 e, q1 |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 U6 z, g* Q. S% o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! F! U! p+ }3 |, d# u5 X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ |4 {% I) t" c. e9 h
解决方法,移除低版本的APR (1.3.9)
) v# p/ g& h/ g1 Z+ ?2 P9 \- Q+ e
4 }. f, j' g$ O: ?3 p0 r5 A0 syum remove apr# |* y H- {' k X
5.Error.log中有: Audit log: Failed to lock global mutex2 I) K$ y! A4 o) o7 p) \6 z5 B ]
7 k/ y- Z) I# E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 A: ~' C) @9 ?' c6 A* z, m0 l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 K4 M5 H8 _1 q: j7 I解决方法:" b% _' q) V3 T1 W5 B$ L/ H7 [+ K
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: |* l0 J9 k( V# v
2 e% m6 n! x8 c# l; s DSecAuditLogDirMode 0777
" s3 G q" A; v# C3 i( ], @. hSecAuditLogFileMode 0550 \! d( C& z, s4 Y3 e
SecAuditLogStorageDir /var/log/modsecurity
4 e7 i/ ~7 I- ? j4 P8 ?SecAuditLogType Concurrent
% c, b7 f" P+ w参考文章:
0 Y6 Z' r& Z& x& N9 hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; m4 K) h" }# [. x/ r! B& |http://drops.wooyun.org/tips/2614 |
|