|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 B! z( G9 n" S3 T8 V( E
; u- F5 } L. {0 Q3 D1 g: u8 j
一.准备工作
5 l* b, h* h7 p, y3 h! D
1 ?' |; a0 |# y4 g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- D/ Q- z& F# H0 W
1 B! Z' C! C/ x8 k# ctengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" c* Z8 R& Y% m6 b/ Z
) R# ]" _1 G Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz" ]4 b7 M% \0 r
8 X* _+ |; B2 {2 C: S2 c: lOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" ]. p7 S1 u8 P# U2 t' N
?$ o" Q2 h, t依赖关系:
$ n: P$ S- V' l0 k$ Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ Q9 M6 J# K% ~. e$ V2 t
6 }* I# r3 {2 z& I0 k" Q' xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel. ]/ [: r5 ]$ l6 g0 h# e2 B
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 S" u3 D& {4 f' Z# C2 `) R
( {" M3 c3 E! k; W1 P1 ^2 Ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ q- p6 i; V$ D二.启用standalone模块并编译
: X" I. e- l: R) w2 v& Z. j0 L, ~/ v( X/ M2 h2 X
下载modsecurity for nginx 解压,进入解压后目录执行:
6 G0 p* V V- I5 J
2 N, a- }4 [/ ?! {- H. ^8 u4 F./autogen.sh
9 q& Q& W N5 ^! }9 o./configure --enable-standalone-module --disable-mlogc
9 }8 w+ d; h1 k7 F3 _5 }( O* hmake 7 n% P8 E! o6 l- }1 Y
三.nginx添加modsecurity模块
% x; k$ h J& i; e2 m4 F5 }$ s3 X D
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 m) D1 d# A- A" W0 C! V8 u+ D+ p# O$ z5 |6 ]; U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
" R/ j! Y2 l: I/ W+ Wmake && make install/ |: C4 ?% m+ n( M4 c
四.添加规则# Y+ F1 ~9 P7 \; m
) T) m! G t$ c' p
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. ]7 Z+ O- C# a0 g# G$ o
7 r$ R6 X! D+ m/ ?, t5 r; r% j1.下载OWASP规则:4 c( f2 {9 e& S5 v! Y
& q g4 V+ Q3 W5 e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 H6 _6 m- \- X9 ~6 U/ n4 U+ q; g" ~
mv owasp-modsecurity-crs /opt/tengine/conf/
4 p5 L4 u3 F/ `, d& i
2 C. b# H6 p- }, wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- [' x8 r( |# H4 j- T' ?- x2.启用OWASP规则:
8 }# P: A2 F3 a! v# Q! N& i3 T- v. k& `7 c! }/ `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; x- |7 J1 p4 b
' U1 [0 U! e z! K6 R
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. {' c' l9 ]& K
+ W6 J& B8 b1 x% z) b9 O. Uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。 ], I3 V3 z" ~% p% J1 G
+ [; J7 h+ S: z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" w: H( _/ o/ R8 N' DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 l$ z" j$ Y% fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; v, H& @$ ?9 D# C3 S2 ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf4 m" \" Q. Z6 R; t- x4 A. d+ t3 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' G S) c. p q9 nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 K3 k0 _ b+ c# K$ S& I6 BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- C: ^# U/ P& [' |) o6 U. ]. c
五.配置nginx
$ o d+ ]( }5 Q5 o. @. m; @- Z/ O) q9 x: u% c) I: w* \" ]
在需要启用modsecurity的主机的location下面加入下面两行即可:% v; Z- M z4 o: Y# C# D6 A
& z# ^/ S3 p) R) oModSecurityEnabled on; ' O. W ]' W" [. U4 Z8 P
ModSecurityConfig modsecurity.conf;
7 J7 x P7 e8 x6 R3 V下面是两个示例配置,php虚拟主机:6 `8 Y- }) M( C, c8 ], ~
8 N( G9 n) S- N
server {4 A' z8 I; V# K5 y3 \( t: M7 m) u
listen 80;
5 `# F) u+ D2 h4 w server_name 52os.net www.52os.net;
! {2 u( b3 i6 `2 s& C/ a9 a8 b
8 ?- M& j) K- D0 J# C$ ?9 R# G location ~ \.php$ {
$ i, P! x' c5 [, ` ModSecurityEnabled on; % ~8 h/ |8 q" _& R$ _* L% y4 [8 g* d3 e
ModSecurityConfig modsecurity.conf;
, V7 m0 i/ K, ^# P9 ?
0 E0 u2 Y4 Z7 |4 l root /web/wordpress;4 i6 ]! a; {8 d$ c8 ]4 C
index index.php index.html index.htm;
& M& x0 z- k' f2 I
+ Z. J& G+ s; X3 C% P: o; { fastcgi_pass 127.0.0.1:9000;$ O; i5 u9 w- m( A
fastcgi_index index.php;
% g' C* v. F1 s) M- H0 i- m fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 {! ?4 k S! M9 }9 L8 R/ F include fastcgi_params;; L! L# t" i- h- D/ O5 }. c
}" F! ?: @ s3 U6 E" L/ s6 j+ n% ^
}
3 v9 L; N3 u/ ^" J+ X8 r5 aupstream负载均衡:$ g( X2 M, A+ {' r! U: X: G! S5 A
7 u3 s* D! C2 E, pupstream 52os.net {2 w; B! r3 [1 x5 V9 ?
server 192.168.1.100:8080;1 ^# s/ M- |4 t" ]8 x! l1 O! ~
server 192.168.1.101:8080 backup;$ s: P# w. w2 y( o0 V! W9 S
}1 M( A ~+ d% n
& {! d; {8 L% a. T, vserver {
: K; R; q7 Y" w) d2 D llisten 80;
' c* I% ]. L7 |5 d0 Z4 F: nserver_name 52os.net www.52os.net;; U* F$ Z! d( A, ~1 N
) B, l. W5 p C6 ^- ^+ q, H# z% o
location / {* Y- K. U7 o- J) j1 }
ModSecurityEnabled on;
) b4 t* @1 u- H0 G- b- |6 f2 C ModSecurityConfig modsecurity.conf;
/ e7 V0 [+ o+ R; k4 R" |& {
" N9 j# c& u: t7 {- F! Q( _ proxy_pass http://online;
7 A4 d5 R1 W! q; B; e) z9 O proxy_redirect off;
( Y! g/ |: d' ^, `+ w- M proxy_set_header Host $host;
' ~; i, |5 L6 A, U9 f# x+ w% V+ H, X proxy_set_header X-Real-IP $remote_addr;, n) E- [3 q& Q( S
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; h Y% C* x6 C; o Z3 [+ V
}
0 K! Z0 E5 A( [}2 E) _( E; }8 S( `: t" L: F; l
六.测试1 b2 M/ E1 T/ T: K1 N
- s+ Q. R* @" O9 W2 [
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ N; |4 i( U$ n! |$ q8 J
3 H; E$ X* v, i5 l0 Z' V* a# Y<?php# n$ C) _. u: [: L
phpinfo(); 3 ?. B$ Q5 ~+ N {) d& l
?>
$ y/ F$ {. L/ ]4 v+ P在浏览器中访问:
* e2 L7 z& A0 U) Q4 W
( e$ ~8 }' N: z3 zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
# ^9 X! j( J9 ~# }http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 X1 j4 J& A9 l5 [+ Xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 r2 s. Z- P% C# B说明sql注入和xss已经被过滤了% B* D! J$ G: z5 w
. ^1 a4 @% T, S& j七、安装过程中排错
, j6 i V: L6 c7 f+ K3 Q4 l' d# X+ t K0 P/ j3 z2 r9 A/ R" I
1.缺少APXS会报错4 Z7 B1 _- {( O
* d1 |. F. ^4 X- H; o) `# Nconfigure: looking for Apache module support via DSO through APXS- p/ v6 W2 e0 L! o7 R
configure: error: couldn't find APXS& {( k6 O" E ~: c4 A1 p- z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 Y4 h5 g( O3 w2 n解决方法:
2 z0 D0 x9 v6 {) H! G2 u" |' e# ^5 o* e5 h
yum install httpd-devel! `, c: S; N( P0 Z
2.没有pcre+ \. d! x3 j. q2 u
: Z) ~) m6 H9 n6 @, l6 Zconfigure: *** pcre library not found. f* } I q1 W( [7 I1 e1 ~4 T! X
configure: error: pcre library is required+ z6 v, c, y% y9 A/ f4 g6 z0 w. L
解决方法:1 S9 p. ]2 w& o1 l$ S
1 Z3 z" p) l4 K
yum install pcre pcre-devel3 ~$ u% r- Q/ X
3.没有libxml22 m, L4 S7 I4 j
& G4 i0 G" L0 {6 l: y3 w
* _/ p6 X" W u+ g& r+ T" [configure: *** xml library not found.# r+ C- I4 {5 W- U
configure: error: libxml2 is required
+ m6 M1 O- i/ V- f解决方法:5 }0 e, y$ R* K' x' i
7 E$ v5 y. m' z1 `% p8 W
yum install libxml2 libxml2-devel
! m; v% P" ?8 b( i7 k4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 M9 M, J4 j9 M! {& l, S
# z& ?1 M/ l% u1 X( s4 ]9 eTengine version: Tengine/2.1.0 (nginx/1.6.2)
% l0 j5 C4 a/ N0 @& ~" G& \2 nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- ~6 t8 [" T7 V6 _0 Z, f原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 p3 Q7 r/ M" y" G, @( d# Y7 R
. o5 e, p P9 \: x- m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. @9 O1 J* l C' `1 ^0 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
- q# m1 F6 E& w2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; B1 N# ^$ }; `% E# u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" ~1 z F$ D6 m4 G6 V3 z/ I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; u2 z' ]( U( n, R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 r* n V1 Q; M& e解决方法,移除低版本的APR (1.3.9)
5 X7 W2 F; K4 A" ~% N- ~* C; k6 l" |5 F' h9 _4 W4 l' L# Y4 {
yum remove apr
. i, ]; ?' l/ y# ]5.Error.log中有: Audit log: Failed to lock global mutex0 z# n ~8 {8 T h& n6 f, K
$ K" W8 ? @3 ?2 D6 I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% z# O) @+ I- V: ]* v) p! x9 |; xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 |- N1 ~. c( z, J# q: g% o解决方法: |# q3 {* A& T! h! P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 V. y. h% u, @+ k( {
/ o! ^' i2 }) S/ U& K# J' w5 hSecAuditLogDirMode 0777+ l& }+ ~* t7 n! [- H7 k4 R, j$ {
SecAuditLogFileMode 05501 Q3 \5 [! p# A* G# Q3 A
SecAuditLogStorageDir /var/log/modsecurity, M4 D$ _$ R) f8 u+ s$ h4 N
SecAuditLogType Concurrent$ t5 N# x7 Y" d3 G6 @9 {& S: [; `
参考文章:( i9 Q4 E7 A/ q0 b9 c/ O4 {. w/ R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; `2 F$ _- s, p0 I) k: L# P% }- lhttp://drops.wooyun.org/tips/2614 |
|