|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ O( g1 Y' h' e2 z, \
6 u6 x, {- A) T" g$ y9 [& r1 Q2 K
一.准备工作# I/ R @- f. G
, V0 J" I1 v5 ]* c4 X* E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 J0 Y! E8 R- P) J+ ?- L. J* P) {/ m+ a0 U) d* G- G5 S
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- ^1 d1 k0 B0 I) r' `& u; f% o
* q2 g9 u, M, d& N5 ]8 ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
R! \0 _- `1 f: J' V% m( N4 }+ ^
4 A: n% _* Q- E3 l3 oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- U' n: Q9 u& a! ]+ p
" t! K8 v; G! p6 y依赖关系:
, U* a- r& K/ N- E7 k, k; ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( X1 t2 a9 m/ P/ ?- \* Q! V: T2 L
$ Y) U2 w" ~2 }( iyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel4 H# T! R: B" C/ [2 n6 [
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ Z" A- z% a$ i$ i
' ]4 M$ B: b/ @7 Qyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- f- l1 {5 {8 \, ?7 L `. e6 E$ L二.启用standalone模块并编译, C. G+ V7 V4 R$ k$ c! y5 `+ ?3 D
' v/ M7 X0 [2 W下载modsecurity for nginx 解压,进入解压后目录执行:
3 S7 j6 |' |7 \$ F6 z3 ?! o" \2 i8 V7 G: V5 B- [6 q
./autogen.sh
! c Z7 f _0 b1 \/ J' `./configure --enable-standalone-module --disable-mlogc
% a7 d* w% M" [* O. Vmake 1 c" T* {# d+ D9 y- a: \5 o7 L- O
三.nginx添加modsecurity模块
5 o* c9 { T& w" ~
- H: ~0 W( q# l' ~6 }0 l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, a& l, V0 A [0 S+ p( }* M
/ @' k- ^+ ^- k- c* T3 ?./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 w3 O8 M6 F2 U# k
make && make install
: p/ L3 w6 a- X; ^+ ]3 D: q四.添加规则& X; ~; t' x7 y' u4 o& t( d
; N1 c' ]* D4 m2 Q5 c! Z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 f; _3 Q* T( `" f9 g# i
/ Z3 N' s4 b% ^/ f% }. b% C; P3 h
1.下载OWASP规则:
8 X. T! G* X; h0 B; y. e# m x* }9 w# D- [$ Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( c7 f4 G& A Z2 z1 U) m
6 _, f x @5 Y7 ~4 J+ R3 }mv owasp-modsecurity-crs /opt/tengine/conf/
' W6 s7 X" w+ D" F8 V* s7 k/ t! [7 A+ d2 q t& W: b
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" n/ r) p# `+ }: K
2.启用OWASP规则:
6 Z# n" K7 v% u( h; L1 ^' K" W2 e/ B5 g# Y9 z+ V$ e, _# V! S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% b: ?6 C H6 v4 d4 m
. @( i' B+ T; z% \ d P编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 ?3 e% h" T, R
, O/ X! R% O- T* [; Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! X$ Z# d3 A* X5 y
6 w. g9 E A- y% V' Q: i: U0 IInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
\1 D' x* p5 g$ H& q6 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 Q" R9 s: d6 D9 `: W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) S+ U$ W, y- U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 j. l! h4 T: R# w# @' lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 n( V. S( C, n* r% [+ t0 k8 a' c) x0 xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. j3 S# j& ]' \. Q! `+ h/ Y
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; v( o* C1 R+ h
五.配置nginx
5 b0 Q1 r0 \5 c) r0 }4 J7 P
) ]( E" M& \! W: _" Q9 R在需要启用modsecurity的主机的location下面加入下面两行即可:; {- z0 g7 V0 p# ^' E: T1 X
* {) [2 d* W- ^& e
ModSecurityEnabled on; % z+ Z# k4 [, ]5 O
ModSecurityConfig modsecurity.conf;
3 l- j6 F+ W4 X* n3 S- X下面是两个示例配置,php虚拟主机:( @7 ?% P; {* o( _; A1 C; s6 f, |
3 I( u: D( ]9 {" V V2 u: o8 v5 Lserver {- {: q- W5 d& V
listen 80;4 Q; X3 Z. {4 m% c. U5 M1 k
server_name 52os.net www.52os.net;
+ B% t% Y6 ? D& y
, a) y6 {5 @- R) p6 R ^4 F* }) P location ~ \.php$ {
+ s& d6 p2 ^+ [# J) S3 Z3 P( Q" s ModSecurityEnabled on;
2 U) ]# U$ b& | ModSecurityConfig modsecurity.conf;
# m% H& s! S( @0 |/ |; f
) i* E9 P" p v root /web/wordpress;) J; f6 |3 I) X" ~, f
index index.php index.html index.htm;
7 N O0 y7 S6 R. d0 y3 \7 J6 ^5 K
9 m# i4 U4 g$ m) i fastcgi_pass 127.0.0.1:9000;2 i t/ X: O7 X( t- E7 D3 Y* _
fastcgi_index index.php;
+ q, y: T5 u8 O fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; R* d0 Q4 M3 r6 W
include fastcgi_params;* U5 d1 N- @; q. V+ M3 h
}- O; Q, [, A F8 T
}
& M6 J( L( t- v: O8 Dupstream负载均衡:
$ t' j8 Q! J# ~; C% W' w0 K5 e. ^) V$ a: F
upstream 52os.net {
# Z" s7 ~. A1 S, e) _- M server 192.168.1.100:8080;( E9 h9 S1 ?) c% K3 V
server 192.168.1.101:8080 backup;' P0 R' Y, G+ w/ d
}* \" G0 z' P- K" p+ d
2 s: ?, e' F/ S: g) \. tserver {7 U6 {5 H5 r9 O& u9 S6 p; o$ @
listen 80;
2 |% V; |2 ] X' k% a5 H2 I* wserver_name 52os.net www.52os.net;
5 m2 p1 h) N, T$ T
$ T- P% {( p8 K. |) qlocation / {# G7 t4 q0 h+ U m+ M" e- ?* y5 j
ModSecurityEnabled on;
! j+ Q" K3 R" j7 M ModSecurityConfig modsecurity.conf;
2 j9 _7 E8 D7 M( P) W6 h0 ~7 `4 b& e1 P# N
proxy_pass http://online;
. m8 P' H% S; i5 b7 b; b: q5 z/ d proxy_redirect off;
/ l* V5 _7 m/ X# C0 ~3 p proxy_set_header Host $host;
$ L8 W. r8 G. N& W# r; _ proxy_set_header X-Real-IP $remote_addr;" |& k0 k- `8 ~7 d& ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. q, }( d& n! w: n: ?* E( x }
0 J0 t+ B/ J% a1 B. r- X. f$ \8 G}
( L! ]+ V) S* Y六.测试6 x/ S! {: b& v) s! v
( @2 E8 B' N9 n. s7 G5 ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# I/ f4 T/ U+ S L; s0 j( L
[5 J7 K8 N! t* H+ j<?php
4 ^7 H/ ?% \( A1 Y) x phpinfo();
/ d: a* ~. U( |" w& p. r3 _: y, ??>
; m5 b& x& T4 m' B5 d' ~5 {在浏览器中访问:
e" z1 R2 x! j0 N+ |* `; C& q: \( I Y% d
http://www.52os.net/phpinfo.php?id=1 正常显示。% L" h! D6 V! |8 n5 ^' c
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。, d3 G( M& D5 r1 k6 t: V7 [
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, M/ ]) l1 l' S& \% E
说明sql注入和xss已经被过滤了
' J* O. n6 g. E& }5 `8 ~) _! g9 g5 m0 B! Z# s
七、安装过程中排错
; p# Z' R3 r' g" s8 f+ g# n: X5 ]/ O' O
1.缺少APXS会报错1 G a! o2 j9 B/ Z* p* h% i1 ~8 E
% A( o* d8 r% \! u1 J6 N/ ^
configure: looking for Apache module support via DSO through APXS; |. P0 k/ W2 }3 R# a
configure: error: couldn't find APXS
0 j9 j/ I* F# g# C, Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 |$ K7 P0 R3 s) t6 X$ \解决方法:
3 j' s: _0 x* O& ^* O3 ^, L- S) T- n" j3 I8 o$ Q
yum install httpd-devel
. B1 b! ?- m# k" j8 [2.没有pcre4 x. ]1 R4 g: W8 H9 [
' \# _& N& v% o4 G
configure: *** pcre library not found.
( S. k* |4 L8 Iconfigure: error: pcre library is required5 C; i/ P+ I9 o+ y8 C% d
解决方法:# M- J# R- M7 s' w8 B, |# ]- w
( V: ` K l2 T$ G* ]8 I. i, `yum install pcre pcre-devel6 o! D, y5 _2 C- A3 n% O8 W3 b
3.没有libxml27 ^- v4 _+ Y% `7 ]) o; U% S/ k
7 V: _2 h. m4 }; M/ S. [5 [; |
) M* M' X5 s' G7 X' E7 ^
configure: *** xml library not found.0 Q+ u5 K8 v9 V% g; G. @3 l* H
configure: error: libxml2 is required
+ C8 u2 E& L1 l7 d) H- ]解决方法:8 Y( ?- J c8 v6 S, ^0 Y7 T
9 V5 y1 U# _0 G& D- b: B8 k
yum install libxml2 libxml2-devel! Y. R$ W" i& Q8 T: H0 P
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 {' | ]% q) |
! `: o. o+ c; Z0 N3 D: M
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' i8 `* V3 L1 I9 J9 `! @( a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" ?1 q0 D# N, t* O/ w0 H& j( E原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: @( l; V# c# d/ a9 ]5 c5 l7 W: m
* P% L# x1 p* W' w1 J6 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! b3 f' s& j: @1 ~9 [: N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 F, j, K$ ?& ^6 N' e$ w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ g9 |% @8 i4 r- }& ~+ ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ B. f7 t- \1 f% b. \9 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: W* o& e/ \" \$ c' `( m2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! F6 K7 o; H! ?3 P" i解决方法,移除低版本的APR (1.3.9)
F) l3 |4 o; a7 Y9 z6 n9 U* C9 X* h! ]
yum remove apr1 r! S" b @4 F% L4 O
5.Error.log中有: Audit log: Failed to lock global mutex5 \: y, Q4 ^* H
# L5 d/ @" s% R$ {9 C% ?& v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
( ], ]+ w$ N% n Y% V: Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 o. F; A2 Q- G" M% g. K解决方法:. O; w5 O! M) ?& e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 ~, w5 t+ b/ ?+ b% P6 x2 _7 A B m( F
SecAuditLogDirMode 0777
! i0 L7 f: M7 m0 USecAuditLogFileMode 0550
, x R1 X# m3 f; q4 S7 x' G/ NSecAuditLogStorageDir /var/log/modsecurity0 m$ Y; B* M- G: L
SecAuditLogType Concurrent
# z. L* s$ o# p$ S参考文章:2 ?( s9 }9 C& O# e# o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 ~: l" g0 U. b% K/ w$ k: N
http://drops.wooyun.org/tips/2614 |
|