|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" E9 j4 p( y6 ~; M* j- \& L) s! z O% w: Z, j2 g* z
一.准备工作) t$ a1 ~4 n1 s; {, T0 t# |% {
/ w' F. q$ t4 y: W. u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 q) I o! g6 S3 a( E/ [: a& o+ y# G% g `- e8 s u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 Y- ]$ Q. b# o7 X& g
x4 W. w0 |- e$ P4 h& Q ]' x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, Y X2 c, \6 K1 n. W; N( r) F N# Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs+ e* ?0 {. c/ Z4 A w3 ^1 o3 S
S0 L: L. Z- b% W依赖关系:# f! Z5 l% u3 K& b
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) J8 r3 o% b. ]8 g- e6 f3 `! j/ w3 m" ?2 `- `7 p
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel z" i- @! f' h( F- K0 D& S5 |1 E: }5 J
modsecurty依赖的包:pcre httpd-devel libxml2 apr) u! f. h) m; i% ]& t
# R/ ]# E1 V* t0 A3 }9 }- oyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! D4 M$ z! e' h) f5 j+ A% }4 w5 Y$ n二.启用standalone模块并编译
* `) Q/ L8 Z( K0 C4 l. Y q" _1 k' w5 m& c4 @$ M+ m
下载modsecurity for nginx 解压,进入解压后目录执行:& f% t4 E( m, g2 \
' D6 C/ \" ]' @! f" ?
./autogen.sh
0 m8 G7 l! f2 F6 r./configure --enable-standalone-module --disable-mlogc7 E6 V2 k* M, N* j/ d# @4 i
make
! B4 Y7 [( G5 v0 _% Y3 t三.nginx添加modsecurity模块5 f/ R X# Q! e% M
8 }1 i5 G: K. T r+ l) z# l/ o在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ d+ L1 {) i9 z( k
; n% z2 W3 S2 Z! Z; F./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# F; d' o" \ x5 G" L4 qmake && make install
0 K7 y; |3 X6 Y7 e四.添加规则: h# s& g& B; T/ |- ~& r
( H. k( \: d1 J/ Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- f: y7 z& o9 _" H
* X! ?! s4 K1 M
1.下载OWASP规则:: `4 o9 b- Y& L3 z6 c0 ^) m
+ @, U% L0 ?! V4 y* I& o
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% U( a7 j4 C8 C
% }9 K& w& ~4 ]& S0 \$ S
mv owasp-modsecurity-crs /opt/tengine/conf/
! w# a) \' L2 Q R* Q& G! D" w0 S: L! Y! R
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) E$ Q$ R/ r1 \7 S2 a' q: u
2.启用OWASP规则:1 c3 E: F/ w3 I) C
7 C1 J1 X% v: g* z/ G" h& N
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ J& ^) T; k; V. X+ z0 [
/ P6 {$ T5 q# f8 u编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) K7 y8 I2 t$ e5 V0 M, N3 }: R
) J; _* o+ t& v y; dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* X! b3 Q; C+ z$ Z2 e
' x7 ~( G+ c% i( I5 { gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 L( j9 V$ S) S) j7 u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: ]! J X: y5 {+ _) y0 N5 H3 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 Z. p$ @. ~- \; a* k% J2 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. |' D' C3 j! k/ }( |) x, _+ a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! B5 }. m( g) C) X) w$ u' pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 O7 ^$ d# ]' E# o$ XInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 } i! B* \' D6 A
五.配置nginx
- D, y# ?8 Q" Z" s3 m; l% q) \. m
在需要启用modsecurity的主机的location下面加入下面两行即可:) c( X) r% C+ C+ R
5 ]2 c7 @7 E# R: x* Z$ m8 ?ModSecurityEnabled on;
4 y9 [" f$ I6 OModSecurityConfig modsecurity.conf;/ O( [/ d. v7 \& X1 i/ d+ _
下面是两个示例配置,php虚拟主机:
" A4 q( d+ c1 H" P% R" ?8 k- Z! Y! M. E A8 p U9 `/ M# r* _
server {0 c- e/ \0 F6 i
listen 80;
# V3 Z0 G9 m6 N7 f$ \4 x% K server_name 52os.net www.52os.net;: Q: s# i9 f1 \* O5 F' O5 F- x, c# b
8 d. |/ X* a* s/ g% ~8 \ location ~ \.php$ {
, O7 O) a& x# M$ b/ `+ c: Y: O ModSecurityEnabled on;
Q+ K& D+ _* ?+ P ModSecurityConfig modsecurity.conf;
2 H; }; W+ t3 B; E0 h6 n8 n) v5 M! \9 y" X; }
root /web/wordpress;: y3 U3 s3 Y' _5 c* _
index index.php index.html index.htm;$ p4 |3 A0 q9 H3 n+ ]2 j3 G0 S$ W% V3 _
2 s2 `* v- Z) p, }% ]9 d: g
fastcgi_pass 127.0.0.1:9000;1 Z# n8 f4 Z2 {! G
fastcgi_index index.php;
4 C6 S# }7 O0 [% j- G, H fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;4 n% d! s& o0 Z; J4 g; f
include fastcgi_params;
6 a4 X5 E: f) m& Q' y4 x! @ }+ ]8 j! }0 K9 M& M* Q- V
}; U# U7 Q4 f/ U3 D
upstream负载均衡:
$ y2 F1 y2 P& {! p, @. L8 ^( _, E; q
upstream 52os.net {+ W( m2 Y6 T3 e- @' |; p
server 192.168.1.100:8080;
3 i6 V" x Q% _& U" u( C r server 192.168.1.101:8080 backup;# D% L) {" d; ?9 Q( x7 A
}1 W$ [& j7 m5 u J4 l
+ l) X3 N* ?6 t
server {
- k9 U* D- p3 C8 ^! glisten 80;
6 _ `0 {- O( c: l5 @. nserver_name 52os.net www.52os.net;1 \6 i% W4 Z6 t. Z
7 d6 b; i* Z3 S5 ]( b8 S
location / {
) o5 O" L$ d+ z$ }/ K( y: d ModSecurityEnabled on; 7 k( j' \/ T. F1 g
ModSecurityConfig modsecurity.conf;
$ K; m/ F2 a+ I+ }/ A5 ?5 C, O9 ~/ Q! j2 L5 j# Y& B9 k
proxy_pass http://online;
! l1 t/ r% B3 n% J proxy_redirect off;3 I* r" _3 ]' t8 b) n- m+ W
proxy_set_header Host $host;
* q, j1 P' t( S- k proxy_set_header X-Real-IP $remote_addr;
- }2 y+ M9 [' O proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' D# e8 y$ ]" `" L" _6 n: {
}
% {; D2 d# ^* c$ e+ J, o}
/ B D3 o# M3 t1 w( R六.测试6 Z0 R% [! \% |& M
& Z. E$ b9 Y# `. V
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 U. U7 M6 f* Q2 R8 I5 }
l* x, ]% ~" B# a1 g+ @<?php
7 k/ K+ ^% b% R c: d4 ^ phpinfo(); 8 O x9 E) S0 {) ?
?>
4 R Y6 K/ u `( L! R$ H: z% j在浏览器中访问:: y, c$ P+ N% {" K
0 J( w- U7 r9 {7 b+ ohttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ C6 [2 ?+ c# M. g. h7 R- }http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。" g" n; ^9 @0 ^
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
1 j, k7 l% ~, L说明sql注入和xss已经被过滤了/ n2 j: c/ M, n$ B- K" v
, |& ~( v8 E. j七、安装过程中排错4 Z' J8 M9 H3 D) f
; T2 q- V7 E8 Z0 I$ E
1.缺少APXS会报错
9 u" H% V8 Q$ h& `: |2 E, `- U5 Y: _5 T/ s& x% M
configure: looking for Apache module support via DSO through APXS
) Z) s5 R0 i. Kconfigure: error: couldn't find APXS
0 B/ M: K( Y- v E0 bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ g8 k5 x- w6 l; H
解决方法:* Q4 n) b2 d+ v. F: R) D+ p
- w. K8 I" Y! r- o
yum install httpd-devel9 Z2 r7 d/ n3 J5 E: \
2.没有pcre& L5 D W$ W0 C8 F7 e1 q. X
) [( P' x% T0 O, v$ B: R0 K" Z- econfigure: *** pcre library not found.0 T4 g4 n& V) c8 L" E
configure: error: pcre library is required
5 N! s3 g5 d4 p$ i6 D3 ~解决方法:6 [3 z! s1 Y0 F6 y
# X; K0 Q) r* S) K! c5 D& ]! yyum install pcre pcre-devel& S Y5 ?4 `( I7 Q/ ^7 R
3.没有libxml2# }/ l# \: ]% c$ {
; B, f# c# B$ E
D$ @$ k* F$ G, U& a bconfigure: *** xml library not found.
0 [0 |3 y, x2 ]- N* f& dconfigure: error: libxml2 is required
) s; V1 R& @' S解决方法:7 P8 O5 w5 S0 ?6 J
4 c$ w- N+ V, w8 [+ e
yum install libxml2 libxml2-devel
6 @4 L- E8 q" N! d9 k, y# A8 r" G4.执行 /opt/tengine/sbin/nginx -m 时有警告
) A# T. M$ M1 J8 c" ~! G2 n. J* [8 Y8 a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" p7 g6 [7 F8 x. \) Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* ?' P1 r. R- ~" K9 i原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ W; H! p* I. G. F
w/ v' W9 c! H0 A3 D$ w- ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ \ \' o6 Q/ B5 ]) i3 H8 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
, w2 L) U. u. w* X# l+ l2 f0 ]; K: i% |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) z. t, X6 f3 C2 y; s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 ?( k/ i" v( v' }; k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; J" u* X9 u, Q0 L; ?9 {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 E: X3 W" K6 Q2 n9 v, }解决方法,移除低版本的APR (1.3.9)7 _3 {5 v2 Z. c; M" Q% Z
6 K. C' R: p; q. J5 |3 a4 E1 A4 Pyum remove apr
& r! [) \2 I, ^$ v# y5 u" w' ?- b5.Error.log中有: Audit log: Failed to lock global mutex" f; r/ q- _0 o# a9 t6 J7 L6 Z
) t+ D }/ d- J5 Z( ^: C R2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - F# R& k/ x0 I& l% c+ P
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ }) f. C, V% k: ]9 o解决方法:, n( ?0 C+ r9 h1 P0 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 G- t2 M9 M7 ]1 O) E) y* l" E4 D# a9 w
SecAuditLogDirMode 0777
# Q0 i, Q1 G# x3 q) ESecAuditLogFileMode 0550
) ?+ v0 I4 y# [6 ]* K3 BSecAuditLogStorageDir /var/log/modsecurity2 t" | W& n |3 C5 F$ \
SecAuditLogType Concurrent
' S+ {& t* d* H参考文章:3 j P, M$ M0 |5 \. z9 G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: Z* k7 c7 y/ @5 M1 Q% |8 @# C
http://drops.wooyun.org/tips/2614 |
|