|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# ?* m2 K- X! y8 q. e: }5 `/ Q' `' s9 D! h) m' h/ x7 }* }$ S
一.准备工作7 b& c- f" U: [7 C5 t
1 j* ^1 G0 v. M系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! `. l4 Y$ O& W" d& z1 {/ ^
* N7 H7 t1 O! C; b+ v( {tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# T% L) x) J2 t' x
3 G# B6 l9 M# Y. ~/ l, o2 fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; y5 u& J" ~# V3 ]* Y
8 Q/ Y5 i6 y* WOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 k) K0 z& [9 n0 }# j* }* l. J1 E8 R! \4 a. r- C0 t
依赖关系:
( ?7 Z! A3 B) K4 }) @( K! ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( n- V: k, ]3 ]4 z2 { D3 g6 O7 ]# d) o- b b: ~
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 @" n- u7 w+ Y& I( s9 N$ m# d3 I' L
modsecurty依赖的包:pcre httpd-devel libxml2 apr% b [0 D/ K3 Q5 v
, R5 m# B' v6 Myum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
5 D6 h4 D- f. Q9 f9 ?6 [二.启用standalone模块并编译! j$ p+ t; P' A$ A
3 p5 d+ y+ X8 Q& Q/ V/ e
下载modsecurity for nginx 解压,进入解压后目录执行:
& p+ F" n) W) }1 q5 Y+ j3 p
) Z! a$ p6 S7 W5 u& e./autogen.sh
0 ]$ g/ w$ ]( s8 T5 V./configure --enable-standalone-module --disable-mlogc) X# b9 N. `* N0 Q) Y; V# U
make . F5 [: Q) l6 a$ h3 t) K
三.nginx添加modsecurity模块& B9 }; P9 M/ ]' e0 U' B7 Q
' U& d' B4 j! i; p7 O( G+ H: c. W
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% O, i( T. U! e4 C- n1 R" G( h" h1 p; V _
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine T1 b0 J+ {& k% {7 }
make && make install
8 y. d$ ]# U$ l7 @四.添加规则 e7 U# Y0 d) W
H& T: {! _/ u; U7 a' \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ E5 F, I% `4 d' H
/ t; x5 p. @& Q r a3 N, ~2 @
1.下载OWASP规则:# W8 A7 g& _9 t G3 q3 y5 ?
% | k2 D5 Z6 T9 E9 @4 Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs) b+ h4 ?% ?9 b3 {" \2 U
# l5 c# M; i& V+ ?8 |2 Bmv owasp-modsecurity-crs /opt/tengine/conf/2 |2 K$ ?! j( ~
+ p# P/ N( _& M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% y! h N4 Y0 ^/ D2.启用OWASP规则:2 Y8 u+ x6 A% S. }
: L j4 a+ x( t8 k2 E3 H) S" J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 S/ A. D3 k5 Y
5 I5 J8 `6 j6 i4 a0 m I" e/ u/ G编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 i# G7 o8 }4 \5 n! }6 ~0 U, P, x" g7 X8 x& Y2 |8 f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 W j! J" r! Z- v* Y- O
9 {1 ?2 X/ ?4 Y1 u9 l+ z$ L% vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, n9 @% B4 o/ B4 ~5 U, wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 N0 v8 w, j1 D; z2 h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& {! n1 K: n( ~4 a1 n: Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 T" a" @2 H d& lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 y& t: b9 Y( U9 HInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 }3 g1 v# O, S4 a' \Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: `) h0 U! i% B/ G% o- a1 T- P8 H e五.配置nginx5 e$ v" u' k" C0 ?
( ^# p$ A& n: ?! x在需要启用modsecurity的主机的location下面加入下面两行即可:
0 J. j: f; v1 {4 ]* V* _. P8 `( u' |
ModSecurityEnabled on;
8 U0 n' U ?2 D8 i, LModSecurityConfig modsecurity.conf;
2 _$ a& Z. N/ a3 S下面是两个示例配置,php虚拟主机:
% S, L" ?& h! k5 C0 ]8 n+ q# O ]8 X+ X6 V2 B
server {
3 U0 `( \$ P- |9 M e3 W9 ]" Z listen 80;
- F9 v# g1 L9 m. g, ?1 N; p server_name 52os.net www.52os.net;% p* `. [# h0 w, z% ~) Z
6 R( F2 M5 C% U) L% k location ~ \.php$ {
' N* b* q: l1 X8 C! ?2 K; x7 U ModSecurityEnabled on; ' G/ v3 }, y, C' S- X
ModSecurityConfig modsecurity.conf;
- L9 c% N- k0 P, g2 r6 t5 s y' a' o
3 z/ V+ [$ Y4 D0 |, b) \% a* A root /web/wordpress;/ E: Y: ?* k5 I6 i* l1 f9 i
index index.php index.html index.htm;
% \* q& d+ p8 l; ~- F
8 b3 S# j3 Q" [ fastcgi_pass 127.0.0.1:9000;
. ]% x) J7 _" {$ @' ~% R: ] fastcgi_index index.php;
: t' C7 J* I; k; `6 s/ V; Y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# \5 ?. J# p7 y/ W& c& d
include fastcgi_params;
' n6 W% e7 `- A! }3 b2 U4 u, y }% x7 h0 |5 i6 ~& X
}
1 v3 \ T B% l0 qupstream负载均衡:
7 A/ I( H5 b3 g
]/ ~4 J/ t/ P! A+ Nupstream 52os.net {
3 j1 x* g* f3 a! P- m6 O server 192.168.1.100:8080; I* z% `6 b k/ s* N/ Q
server 192.168.1.101:8080 backup;
5 a" a, n1 ~: x6 J8 z}
) u( w( Z4 Q0 a4 a/ F& c1 [+ Z, ?, x6 |
server {4 R# }+ }' n ~, ]
listen 80;" f% z6 W4 a8 B; ~' [" G
server_name 52os.net www.52os.net;
3 ~0 h3 |2 w3 O4 d2 Y; m
3 ^! ?* \5 Z/ C# H, \location / {, e& z: @ Y2 @
ModSecurityEnabled on;
H- g9 `% B7 I! C. u. U" S ModSecurityConfig modsecurity.conf; . M, [( g8 h, D8 M
7 ^9 A2 d* ?' ?9 ?
proxy_pass http://online;
2 H( m" h' X" t! r proxy_redirect off;( `5 O1 C- G c
proxy_set_header Host $host;5 [; E; [( W! j x) w
proxy_set_header X-Real-IP $remote_addr;
4 g/ D: [. v( ]6 \: i8 ~& r proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ k' g/ X8 U' t+ g8 p, E }
$ }6 h! K) p( N. l* v) L/ I}" ~* K9 `4 C# |4 [1 I
六.测试
0 q! [( J* L+ A1 c6 ~3 d. s! g6 q9 S0 c% R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 Z: I! l* r* T, }
: D+ F; B7 H7 R5 A! V, N<?php
) {% f7 x8 @( q+ t2 B0 | \8 j phpinfo();
0 V, X9 J7 L2 k5 o O& A?>$ j% d |; g* T. g7 o- G
在浏览器中访问:) u# A) ~" W9 ? Z4 a/ b( b$ ?2 r! u
9 b# T+ ?8 n4 J) L. Bhttp://www.52os.net/phpinfo.php?id=1 正常显示。
5 I7 A [/ \( m: |$ l& q! Rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 f* {! ~- }$ J L0 B2 Hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
* a3 m+ A, T. i' V9 J说明sql注入和xss已经被过滤了! r& S7 J, q. q3 I6 f3 L1 C$ a" H( R
" T" a. f9 W7 a) n. A
七、安装过程中排错
8 ]8 Y6 G* r! O# |, {1 H) L+ t; E. f. r
1.缺少APXS会报错
+ {3 H" W* z' U2 C6 }
+ u, P" l: N6 { Hconfigure: looking for Apache module support via DSO through APXS
9 {( b- T8 s( {, |: [$ m4 U$ x! sconfigure: error: couldn't find APXS% K* _5 n( W' {. r, A; w5 n
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, g" n7 t+ G! b* B7 j1 d
解决方法:8 p$ H( f2 n5 I- u( L
* I/ X! `9 Q/ n3 w: C2 o+ Tyum install httpd-devel
" c7 w, n% G: d# j" x6 ]3 c1 Q2.没有pcre
R: i9 Q( [1 P: G) ]+ L2 [9 h2 N; b& w
configure: *** pcre library not found.
: Q+ _0 f8 m8 T1 y. {: [configure: error: pcre library is required+ r! S4 e4 o3 m. a
解决方法:, q q9 Y" i* h. {
8 g; y$ U' v" |* @, ^$ r q) p
yum install pcre pcre-devel
- \, e& [9 F- [' ^* X" Z3.没有libxml2
6 |! F! h Z4 z* @: J" A9 z# x4 e. Q; x8 [7 B
8 H Q7 e' ? j ?& Z
configure: *** xml library not found. `$ n8 p4 i% z- G; K
configure: error: libxml2 is required0 ?/ G7 I# O+ L
解决方法:
/ W8 }0 x/ L* H: u+ N
' U1 s9 G5 W( _9 v* w1 {yum install libxml2 libxml2-devel
( k( z' `2 R1 Y5 [$ m1 j0 w4.执行 /opt/tengine/sbin/nginx -m 时有警告2 @& d r" E4 ^- i' q7 ~2 @
) f3 B+ S4 ?- t
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 v# M* \. n0 g+ D' Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, [" \& ] N4 f0 k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) X7 x% w4 \+ a5 J2 |2 g- ~ @# ]% H- y* Q& ?; m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ [- b5 V! Y7 }2 E/ @& q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
T- L3 o% X5 y5 q& e0 i) \6 Y- e/ R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! R. H" A W8 B9 O9 l1 e0 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": G8 a- Z" E) @. d! [3 ?& k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; ^% w! O9 R/ e& M- Q6 P1 j2 z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* a6 ]0 U i+ L0 k9 w9 O8 n解决方法,移除低版本的APR (1.3.9)" N9 y9 Q8 M& M! @" s2 `
4 y1 e- d6 w8 L! Eyum remove apr/ a0 k3 B% j- a) X8 K+ N
5.Error.log中有: Audit log: Failed to lock global mutex
' V8 E6 _+ B9 X, u5 h0 U4 t) j% E2 Z$ g8 ]
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 O: w1 o9 Y8 }
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 }, z% q' y% h- e" b! i
解决方法:3 v( w, ?* Q1 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 J- ]3 D4 \; E: @
@1 F }+ A5 V. r# n, g
SecAuditLogDirMode 0777
1 U/ j6 o$ K$ X- r! BSecAuditLogFileMode 0550+ b1 W- `# P* o( w: I6 ^) h
SecAuditLogStorageDir /var/log/modsecurity
" @9 x1 _! `1 Z4 P8 ]! `SecAuditLogType Concurrent
) U7 Q; n- {3 H6 `参考文章:
* [+ ~7 ^, L! R0 I$ W% I( ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: h! |8 H5 y- L( X
http://drops.wooyun.org/tips/2614 |
|