|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 @: L; d: e- S7 t# o0 e. l8 c) J4 h9 r$ R, h/ A! K0 N& ~- }
一.准备工作
* q! h' c1 |4 Y' S0 k0 E$ [8 c, `1 T9 x2 G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' a, B8 L3 `1 C: [4 D8 ^" p+ r3 _
+ u3 r' y( V) I$ ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
8 ~0 } F E: o# o* D
" T+ U4 Q. v h1 W- bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 R3 b) J4 g9 H: Z
& R3 R' Y! T4 b' r
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ y# L) }4 Z* J2 _4 r" ^- I
- ?2 I M1 E' I3 z
依赖关系:4 k* g: f% y0 D7 h! ]/ e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ k/ c( b& C. Y2 Z
# c; g1 |7 M$ y2 v4 q9 N! x W. V; Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel! ]/ j5 L2 n2 j( ]6 ]1 R
modsecurty依赖的包:pcre httpd-devel libxml2 apr
2 s" {% m+ p: p5 ?2 P9 ?8 {
, s& c% b3 v2 @% }8 R) F3 A( Kyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
* k, h; G2 `6 x# P, s# q4 V5 \' T. e; f二.启用standalone模块并编译1 I( F7 l/ f0 }; X' `
; s" _0 W: p" {; w7 X下载modsecurity for nginx 解压,进入解压后目录执行:% _: _; K+ C9 G
/ j$ K! s" \# j9 P9 X9 C./autogen.sh
% X$ v3 L" [5 {$ a./configure --enable-standalone-module --disable-mlogc0 F; [' B6 V& @" i; t1 t
make 4 D0 t1 V0 }- C0 [: r
三.nginx添加modsecurity模块
6 ?2 M1 ?( @3 d9 h/ Z1 o z0 s7 ^# f, Z, r( J+ d/ y; j9 u& U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 N, W; n8 |2 N1 s+ P: z' `& w+ s( Y* y8 v' I# c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 I6 l/ l) R8 @make && make install# u t. }+ m+ K& f
四.添加规则
- u* J# x$ z; d6 p, R& k3 {( ~% G8 K. |8 Y% K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 S% Z6 G+ ~$ \% u' Y
2 ~6 V$ Z0 s( s( h9 s+ N. W1.下载OWASP规则:
' h7 Y+ G1 x, Q; \1 ^
8 y+ y2 E9 h( J9 Q$ f9 egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, m! w0 X \/ G3 I k
* j4 ~' p! Q& t5 W4 j
mv owasp-modsecurity-crs /opt/tengine/conf/
3 v, @; S: s5 {- \0 A' P8 }, T
. I% U. A* h- s5 r pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 e# c9 ^* R' G5 P9 }; L2 Z
2.启用OWASP规则:
8 X# W8 S9 V- Y0 T1 K2 h; v( o+ q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 v0 g5 P. ]" r9 |6 g4 T# _; L. B" J& O' C3 X$ y% v- i( k
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
H9 v: x2 Z C! c+ c: v
: r. c8 e0 I; E+ \1 U1 ?owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# \: u! ^4 ]* x5 u# |/ |6 d2 V% o3 {- n6 O6 W' O. O% T5 U
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& j' T2 }; e/ q5 S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; {1 b6 }) b$ X; N/ C# PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf2 b+ u3 d6 x! T) f( W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# E5 _9 i0 C! B4 p; qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 N6 Y% ^3 J) G. [5 ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" K- l1 Z; v$ u3 g' RInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 }5 q' }7 e" m) V0 F
五.配置nginx
; L, F% \. Z. @
) @6 e9 }. R* e) K# P6 @1 b( h在需要启用modsecurity的主机的location下面加入下面两行即可:
4 ?5 z3 c5 v* s1 x: R1 g$ J+ _
4 K( |! k5 o' {/ tModSecurityEnabled on;
6 T, [1 G6 \7 L/ |! K9 H( rModSecurityConfig modsecurity.conf;
9 Z. O% o8 Q0 E/ B1 ]/ r/ v下面是两个示例配置,php虚拟主机:1 v, G+ L- _, Q$ D
! d% _6 \2 M8 Q9 \. E# b* [& p# Q9 [server {
# w/ X* i+ w( r* U% M6 ^8 _ listen 80;
- }1 R% a+ C0 I) C; m server_name 52os.net www.52os.net;% F3 u; G! d! G/ U( Z
8 ^6 E, C1 k3 }, _% J
location ~ \.php$ {
6 {1 A3 Y8 j6 \5 r ModSecurityEnabled on;
; ]7 s. z! E0 q9 ]- ?( H ModSecurityConfig modsecurity.conf;
]4 ]9 K! ~- h: E( x
! k) d- p, L2 @: H: b* v root /web/wordpress;9 M4 ^. i2 ^8 I* ~% q* s' J* }8 C
index index.php index.html index.htm;
) w7 P h& R7 {+ i0 ^+ ?; f p& m % ^* P1 H$ f6 O
fastcgi_pass 127.0.0.1:9000;
4 `7 X8 A6 p% x( \' o, g: k fastcgi_index index.php;$ S _: K. U: X2 @0 O
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; e' o) f, E) X; U0 u+ H$ L) P
include fastcgi_params;# M) a" e9 U& f L1 k
}
5 B3 e I' S* w( x3 n }
& f+ L7 o9 ~9 X4 ?upstream负载均衡:
; Z0 S1 ]9 t! I- |3 ?
$ B @; k, s$ z4 x4 Z/ S; Gupstream 52os.net {2 c0 h+ V$ i5 B
server 192.168.1.100:8080;6 v+ }9 B2 O9 e9 O3 v0 l
server 192.168.1.101:8080 backup;7 g+ A) Y/ c! x4 ~! G3 R# B: h _
}
# P* U, C/ b( K% ] r0 b& E6 A: X$ g/ F# @2 d
server {
6 D% _2 d0 B& P6 {listen 80;* ]/ N8 v4 O1 [% O. t6 ?! I
server_name 52os.net www.52os.net;9 v ~' X$ J, u5 o- P
" V2 Y8 o4 H2 t0 L' ^6 vlocation / {
% B1 S* U0 k. G% ] ModSecurityEnabled on; ! t N- o" F4 v+ i8 \
ModSecurityConfig modsecurity.conf;
}9 @8 a: o, A7 Q5 _
# `7 j6 R$ R: w proxy_pass http://online;0 P3 x; @0 L$ f
proxy_redirect off;9 F4 ^" e/ g# f( n, y8 ~
proxy_set_header Host $host;
9 f6 Y" F% p! k2 I proxy_set_header X-Real-IP $remote_addr;$ i( f; P0 ^4 f3 _/ e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- v, q7 O0 C5 C# ?( T; T; h( k }
1 {6 c2 [! i/ t) F% ~}
! A& G/ K2 H9 n' J. T( `六.测试! W- s2 x2 L6 p/ ^
9 K' r' l( D6 o; p
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 x y: q8 S v" o, y
- t' D: c0 L7 L: R2 D5 @
<?php
1 @, F, X8 l% z phpinfo();
( o, l( } [& i2 E3 {* q4 B3 }?>% |: r5 p( y5 v9 G3 Y5 b) h
在浏览器中访问:$ B" p. H$ ^6 T3 l7 x3 J
5 `" ?0 c k6 d" o3 G' V: {http://www.52os.net/phpinfo.php?id=1 正常显示。
6 r. j7 _9 A, C8 m2 y fhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; Q5 L9 |: t' M* I- W# qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
T8 ?! W8 J0 f; @# ^- w# o说明sql注入和xss已经被过滤了. ]; r+ X G, M# p) _' S* J. I9 l
& ^# P, \4 {6 F
七、安装过程中排错
9 g9 C' J+ R, {+ D0 ?: n9 V! T% k* I3 ^" f; H( H
1.缺少APXS会报错3 M- F6 Z0 P F0 v& j
( }9 o% [& ?6 G1 Pconfigure: looking for Apache module support via DSO through APXS
1 |9 a6 P! e& d% n/ S8 ~6 vconfigure: error: couldn't find APXS) K! d7 R3 B" k# Z2 y$ h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 H+ F0 _2 v3 B I2 M* D解决方法:# d3 L1 S1 N! \1 k/ J$ _
4 K1 P7 w# p5 { E( `9 U; Ryum install httpd-devel }' \' F! ~5 Z5 e0 E1 M( W! c
2.没有pcre% Y0 @. ?" S8 N( @5 L
+ ]' r* |+ r$ F5 k) X/ T& l
configure: *** pcre library not found.! J% o& H9 L8 {$ K* P# C
configure: error: pcre library is required1 ?% ~: P; z/ H
解决方法:0 T; S4 b7 J7 d8 b- o- ^
# [9 R/ e: S" l. @; i" Nyum install pcre pcre-devel' |4 I0 J' C2 L% I q2 O
3.没有libxml2 U- e) `+ e, b( n! e
/ [% Q! U, {+ N, B
- i# @4 r$ B2 A, Q& S; |5 q
configure: *** xml library not found.! E: e; M* x7 U/ i- g
configure: error: libxml2 is required
3 d% w3 z5 S' F$ F6 G; C解决方法:5 U6 J9 x/ h2 T2 ~- P
: k Z9 N/ i; k% F, ]yum install libxml2 libxml2-devel
6 ?6 y0 T. s) ? B+ V4.执行 /opt/tengine/sbin/nginx -m 时有警告
, `9 Q# A! N! r4 p- e
8 |# k1 W6 }6 G3 U5 WTengine version: Tengine/2.1.0 (nginx/1.6.2)% O0 C: ^% O% v1 g8 K: `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" _& O' o6 H1 `8 z: W: U" @1 P: y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! D0 K/ A: E/ p# v% T3 i, K+ u
- s" G" L6 l/ J- t- g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# `6 ], f, t+ P# u! i, k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- l9 R6 S" T# |! k1 g! N
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! t' `2 W0 a" Q/ s% }( k, u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 |3 s6 H! ~9 f: Q) b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" h& R a+ i6 m" J8 E8 Y& N2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# S$ _, Y, u s4 S- w1 @3 V解决方法,移除低版本的APR (1.3.9)
) c4 W9 T/ c5 W& a
& w; [7 P. b, B+ R9 Dyum remove apr( [5 T6 L6 r& P( r$ {1 q7 B
5.Error.log中有: Audit log: Failed to lock global mutex
) w* B& Y! n+ m; j! r' t
9 @2 z. S' b5 M! M2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock % k* v9 |" d3 J
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ Q+ z1 B$ @! u5 L2 T; `( R解决方法:% t% M; F6 l# B' z$ p
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& X1 R" g/ q- t3 a6 A: _" ^7 ?- @- ^% d$ l: x7 N
SecAuditLogDirMode 0777
* d/ c' `- [! c0 U3 @& H+ PSecAuditLogFileMode 05506 I& r0 J% c T+ o* W/ t$ X6 E
SecAuditLogStorageDir /var/log/modsecurity2 o( y* l( H% C/ N0 X! }( U
SecAuditLogType Concurrent
- N/ ]* M$ A2 V: }- r参考文章:
0 v5 Y/ @+ T2 {2 W. g" c6 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& L& O* u. W! t1 X
http://drops.wooyun.org/tips/2614 |
|