|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ M/ K" T! R5 R% q; t/ D) Y: q
" n# Y+ V N' N! G8 i5 `4 g. _一.准备工作9 }2 U% F+ ]% B
8 p6 U2 v' f7 N4 D/ t系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 @, \' J; L: o5 `# w
( S. s, e- H- g/ r9 s' r* W! atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
) D' ^0 j' s" c2 T3 [& t
2 j. `5 c0 @# Z% t- i3 Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. v( o9 b( x8 K$ u
$ {3 Y$ B3 P9 S$ A# [3 ]OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 |1 K/ ~9 _9 G+ D* O z, [# ^; x* p5 b# x% U( V: U
依赖关系:
d7 e; K4 B1 t1 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' U# _. e' T2 W; a; e. D8 l/ ]6 J, H
, `" j; m3 H2 h- f9 A1 cyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% j& I) S% N ]# G9 l8 ?modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 ^7 c1 `3 K7 H3 W6 e/ N
0 C/ L6 }% S1 L: P6 W0 X5 \2 Ayum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel# ~* @: w& W- C, I7 \- z& U0 a* R
二.启用standalone模块并编译' Q, B( ^4 @5 T/ B, S* J
) h9 o- f4 q, s- H# M0 s; t$ [) \下载modsecurity for nginx 解压,进入解压后目录执行:) X, e; r0 V q1 u
! s2 V8 b4 ]7 ?1 Q+ H1 m- W
./autogen.sh! a8 e% P) t3 ^" m0 K
./configure --enable-standalone-module --disable-mlogc
/ e2 r% K7 C' _0 `% pmake
% M* Z, o/ y5 f% G# X( v三.nginx添加modsecurity模块
% D' g/ w& E6 o2 n" ~8 \. H8 ]2 Z$ S) |7 C0 G5 g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" p n1 L$ n/ i; y& E( j
& G5 }; Y% v; `% G0 L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% j, t1 F$ \+ G/ }; }
make && make install+ U' i2 l8 V3 h* M$ h' f6 p
四.添加规则
5 r T5 w5 p, Q* [$ j' ?! d1 S
1 F7 F( |$ h# d! _& X: rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ |" e0 k d( E8 m; Y' Y+ `
6 v/ }! M0 N9 C4 K9 b+ W1.下载OWASP规则:
) {. l6 { S9 G% [, l, u
; X+ f1 M- F5 X3 \$ M; Q5 M7 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 D! c( G) F3 g8 J0 U! S# @ [, S8 Y/ C
. r# i3 k0 V) B7 L3 Ymv owasp-modsecurity-crs /opt/tengine/conf/- q( ^1 w# n6 F7 h0 I' V2 q# h
. L+ U; D- E8 b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. `$ Z) h' n" N8 _7 j# D' F8 }2.启用OWASP规则:
) T2 v! Y: K# H3 O; v( l
9 [. P9 F6 \' ?* _. E4 W复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* P$ [* a! B6 s0 h; X# a- W9 r0 E7 Q4 {. \
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" s3 F- m% I! P, Y1 y$ s- |; _
2 p* s- e C! h% ]" [: Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ H% Z' a& j m# Z& x/ U
$ }) Q. Y/ g7 Y! \9 y" u# XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" V) O B6 P8 s1 P& tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' v* G/ G8 N; c D6 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# A# L! H. O4 z4 @+ J/ qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; T3 ?% H" F3 R1 J- g7 l& _ F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf' d) W% q4 f. P. K e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 G9 s# J; ?, ?, f) o$ SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 _# l& T; U+ H五.配置nginx& K1 { p& y$ q, D
: ]8 |, \8 w# a" w0 W
在需要启用modsecurity的主机的location下面加入下面两行即可:3 j/ u! j' W+ |+ E& f2 h3 A5 l
; B& M, _ f! H# AModSecurityEnabled on;
# X, ~# u5 h1 V) sModSecurityConfig modsecurity.conf;
5 c1 j4 K& t, P2 t+ q0 r% ?下面是两个示例配置,php虚拟主机:
5 o; |7 D; g' U2 h2 }
5 {# g" g* Z5 l( ^+ b4 n$ l& gserver {4 l. L3 i2 E7 D; r. Z$ ?
listen 80; R0 a+ B& A% s9 k- h, d- F0 n+ `0 n1 ~
server_name 52os.net www.52os.net;
Q* g$ _2 V2 f, W0 W% A7 D, ]
$ Q! V( c l1 U+ }: Z location ~ \.php$ {% P, L7 S5 l' |! Z' {# v( w
ModSecurityEnabled on;
0 a2 G) |* c# K! a: f* _ ModSecurityConfig modsecurity.conf;
2 J9 g7 A4 K" K- ?( ]7 x+ i$ |9 Y5 r9 a. I/ z T
root /web/wordpress;
; D# }7 c) j; S1 l; f( b- p index index.php index.html index.htm;6 K* R1 X, ]4 w; H0 d! V& A+ p( H k5 D
' ^8 a/ {9 B' o6 r, S/ H' D fastcgi_pass 127.0.0.1:9000;& M- l" w9 a( K+ ]+ ]# G1 A: i
fastcgi_index index.php;; ^ O9 Z! K+ s
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 j% |8 m- U% J: J
include fastcgi_params;6 ^6 z7 Q/ d7 \1 [
}
2 _( ~# \0 h" u. Z. p4 l }! e/ ^; h: E+ e
upstream负载均衡:
8 f9 b- `8 [8 v" y! V1 ] A7 a; `' z6 C H. g' s
upstream 52os.net {) g. J d8 A2 N* P+ l$ C
server 192.168.1.100:8080;
* ^0 C: n0 E2 [! l server 192.168.1.101:8080 backup;2 v3 n y; k, w. M( W& ~
}: N% t1 a# V$ t3 \! s% ?
$ f& ^: s) ]# u2 k
server {% ]0 a7 Q9 P+ Y" W' R
listen 80;
0 s! A' ~( h; D$ _$ |server_name 52os.net www.52os.net;
V7 f3 }' W! [$ t0 O! J" d/ f; J3 g/ H7 @
location / {
$ @3 G2 u4 Y" Q, y ModSecurityEnabled on;
2 ^. w* u1 y; x/ ]7 r, D% l ModSecurityConfig modsecurity.conf;
3 F, x" K$ c& q1 ^* A( N) ^8 y U3 g0 v( U% ?. f% Y* W+ T
proxy_pass http://online;
: C9 P+ H5 v% Q6 j, c proxy_redirect off;
, p% J- b @ b. b( f proxy_set_header Host $host;
% @5 d# s. x$ O8 y( A proxy_set_header X-Real-IP $remote_addr;8 U1 K9 K4 d$ n
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: X7 u0 }( B/ m; z' _3 b
}
' t; j+ m- w6 a( ^) z- c4 f% ?% q}) R) i& g3 e8 J& j5 i) o i q# W# P
六.测试
9 c1 V1 K: A$ ~1 D6 A; ~6 g0 ^8 V5 M' o7 |. E% P8 f
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 g- O6 v& g. v6 W$ h4 |( L) e& m
3 I9 b* W: x" Q, t<?php8 n/ f" h- m3 R8 A
phpinfo();
8 i0 ]/ W6 U. }) M0 g?>
( [; P3 P) R8 \3 \在浏览器中访问:' @7 ]/ \% s+ W, e( ~+ g; G
. q# ^4 W) g! T; G* w
http://www.52os.net/phpinfo.php?id=1 正常显示。& P, b# {) X. x8 g$ Q; {
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 y: s/ D% V$ A' E
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 y d1 [2 ^) o* e4 a! r& A说明sql注入和xss已经被过滤了6 {/ C) j; b) P, h+ Q8 q! C3 R
# A1 C* w9 N+ }! ]七、安装过程中排错* g) i. ?/ T+ d
3 D# m, E" B2 h8 }1.缺少APXS会报错. J2 l- ?9 q4 p% g1 o
6 q0 V) p4 y# b% k
configure: looking for Apache module support via DSO through APXS. B" N4 h G+ V8 Y. ~: k
configure: error: couldn't find APXS/ M1 V( ?9 B( M- D9 V) g/ {; k
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" O0 j2 b0 _* Q& Z解决方法:1 f. j( z7 V* e; r; @" v, E; ~
) E8 F1 `' t+ U9 e( z
yum install httpd-devel
. X: C' |9 U+ P6 @6 Y2.没有pcre
' l( h2 o k; V: V0 f4 ?( f9 P
# Q8 Q6 {+ [+ |- p8 [8 i9 `configure: *** pcre library not found.6 L# ]3 R7 n, a$ r9 U' I- x1 H
configure: error: pcre library is required
5 W, c/ L6 Q* }: _) L( }7 \; U" Q. ^解决方法:
9 a" D) m ~* X, H% s
: N+ b% X2 D. G1 Nyum install pcre pcre-devel
) o6 Y" r( [& H2 W9 P3.没有libxml2
6 g6 w$ P, ~* w
6 G. u$ x# C5 o9 s( d" Z0 t" @2 }0 R1 W; ~; G$ \/ ?
configure: *** xml library not found.; w- l4 T% x$ k" f# q! r
configure: error: libxml2 is required
$ ]6 ~1 t0 u \解决方法:
' Z/ E* M6 X8 B3 }+ W
1 c" n( `5 K2 b y- M4 |yum install libxml2 libxml2-devel
7 w' Z3 K8 r; `, n: H- ?4.执行 /opt/tengine/sbin/nginx -m 时有警告1 R) W' U! K" g4 d7 x [
; k: I& y8 n. O9 L& s! fTengine version: Tengine/2.1.0 (nginx/1.6.2)
$ w D0 |5 E" ^) j2 u: E7 X" b- pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 I& a' a: [+ q. k2 `# I
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% J3 o" y" R; {- e
( Y0 h% _; I4 ]4 W5 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. K+ F$ b+ s1 z! C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ J7 T0 c' V) _1 a! }0 g5 Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, V. A9 q/ Q' K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! o; l% O7 I8 j5 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# u/ q2 M& z' r/ @$ G! q5 B2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 u# B+ U( Q/ ?6 A解决方法,移除低版本的APR (1.3.9)
) l: P8 X4 r( h6 ]- N8 I( L9 \2 R/ e" r! `" p0 H6 ?& [2 j
yum remove apr+ e. H/ G( h' I- _' h: v
5.Error.log中有: Audit log: Failed to lock global mutex
7 B, [. d/ z6 G! x; L
5 }( _5 W4 B8 l( V4 p7 W2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 f# |& d$ N2 o8 t9 |7 K0 Iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. @! D) w0 g6 T" g% k) b( A
解决方法:2 t/ ` m. I: d- H6 i( C2 e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! J( ^6 ~9 p% Q% z
+ Y$ K$ \5 A5 V J/ Y
SecAuditLogDirMode 0777
% R5 H2 t4 V3 a& D! O' a) D" ySecAuditLogFileMode 0550' L8 {7 K1 A* }8 M3 w! `% N
SecAuditLogStorageDir /var/log/modsecurity
$ i8 K$ ?; M( ]+ L- S& ?7 `SecAuditLogType Concurrent
) h/ O! V2 c- o参考文章:
% ~6 t C8 n) B$ J0 G* lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 O" `$ e6 D- K
http://drops.wooyun.org/tips/2614 |
|