|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! I; {+ `" G/ k. W# T0 [
$ S- W( H; ~1 m3 J
一.准备工作! H- n$ U, T1 |
) N% L3 k$ K/ W5 e5 u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# x6 ?* h5 c L7 ~
5 q: p$ n4 R; H
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* W" L# ~" m6 h5 P, w
9 s, l* C1 j: ?0 X& m1 K3 o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( C5 N8 s5 H* ?" [) W" y
$ b4 H" ?) s0 c6 e$ f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. m; S* S- U1 R! _: W
" ~3 t# b' z7 e9 W, {依赖关系:
8 a2 G: v& U& q# |$ Ttengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 K+ M/ M I, W& _4 i5 u$ d6 N8 ?9 k
# K" @) j9 @; N4 M' h( Wyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel2 l. N* t& m# |7 {4 r' E9 e
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 j W" S8 B7 D3 `6 D d' \
% s2 u7 c) Y) k. uyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 L. B7 X# N; `7 e二.启用standalone模块并编译
* A6 b. ~! C1 I
& K! o8 J; U# y1 b下载modsecurity for nginx 解压,进入解压后目录执行:
( b3 ]* t4 b8 L! c2 t; j/ e; l5 Q4 I8 Q
. i1 K$ x5 w( F4 W y( d3 V./autogen.sh* f. K9 r7 T' y3 z. J
./configure --enable-standalone-module --disable-mlogc# u# X: R. v- N6 L% V5 J
make : A1 T/ y- R9 q* w5 l, x$ E
三.nginx添加modsecurity模块0 L' F5 R3 [8 q+ d, t, q- o& H
6 W( V2 r# c' [! _8 W) K( P7 \3 G% a- K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 c ^0 m; I, x7 _: i" x: D2 ?, c6 c5 d% d5 ]; ]8 k/ V. ^$ a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ v' c9 P" |7 |$ g( Bmake && make install
% E( K& ]) r+ {8 w四.添加规则0 J. S3 `, Z8 I5 v: n. \
! d5 n4 J- g/ H# s' y) |/ Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 b% i8 o/ y5 n+ g6 P. ] }
/ I, y5 w5 W9 Y- E$ X V6 P, i1.下载OWASP规则:
4 ~* M- z% P0 G1 \2 \0 f9 D# v( z! K7 V4 L c6 V7 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( ^. I$ w+ b5 | G9 r
3 I+ R: C# m) Y, A7 `" Ymv owasp-modsecurity-crs /opt/tengine/conf/" F# f+ y! k5 l" X0 }6 ]2 j7 ? k% f& k
9 p' B, S( V( k
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ {7 Y' G! y/ c" E% q! `
2.启用OWASP规则:
; c" K0 P6 D8 Y ~( S, Q9 |+ f3 z% w# \2 _; \9 D2 B
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: ` b7 p' T! J% R
4 N" ^$ ?( s* h" O: d G* Z
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 K" l8 q# J$ t9 v
, ~8 f+ X: I6 o# c' Howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 w7 Q" y3 L9 x- b6 x9 g+ Q0 y* G. c2 z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# L6 s9 z7 c2 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 m' Q& P! D: p& K0 B1 Z5 N' UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 R) E1 E' s$ ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' @: A7 L# K. o! |+ w- Z% k& N* Y) Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! Z# L' S0 H/ S9 NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
o0 C4 U$ ]' w* F9 @Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 [( D& x7 U. X2 @五.配置nginx
6 }! M3 p1 n1 W" B( I$ @
1 {! M& j4 ~6 z0 X* W# r d2 O- n在需要启用modsecurity的主机的location下面加入下面两行即可:2 A, y6 F- T3 ?- s
, u2 Z% S: `/ R4 a) }3 ^% W
ModSecurityEnabled on;
b- e5 S4 |) R" W6 XModSecurityConfig modsecurity.conf;
0 |1 p, q/ `2 h4 ~8 g下面是两个示例配置,php虚拟主机:" w `! s) j8 X. q- w8 I
) M1 O! K# a% n, e2 s- c$ |5 j2 W
server {- c9 e& P6 g q$ }& b
listen 80;
) ]( [8 D, h% z! G7 `5 i v server_name 52os.net www.52os.net;, S _& b/ S' i0 F
5 J) b/ A3 n3 v4 g9 C/ S location ~ \.php$ {
1 j+ z5 @, r# i/ G% f4 H+ O+ \ ModSecurityEnabled on;
" h! o9 ]! ^) I4 [ ModSecurityConfig modsecurity.conf;
% p/ i1 P7 j, v( T( g7 N! f s0 T. ~9 f
root /web/wordpress;
' U: f% N. T: w; { index index.php index.html index.htm;
* ~$ t- ` a6 V1 @4 x* |' {' |4 ]
) y) Z! I* E- }" j fastcgi_pass 127.0.0.1:9000;, l( k6 S/ v. D3 I
fastcgi_index index.php;4 E6 q& f9 h: O" [
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% H3 b) {$ e+ S7 ]$ ]" e; O5 @
include fastcgi_params;
& K, V$ J! A5 V3 q5 Z }$ l& ^7 c9 i6 V3 J- t2 I+ {
}
% m& l1 B9 C! W! B+ b, z! _( F# Fupstream负载均衡:0 M8 B) |5 _3 q9 p! R
1 M2 b) [4 k: E. y- W# n8 R, C5 nupstream 52os.net {
6 E8 R& ^/ b% Q( _ server 192.168.1.100:8080;
0 b2 P% b. r, A& {$ j0 k" @ server 192.168.1.101:8080 backup;6 D! L3 a3 m p, q1 d* |; X0 V6 C
}
7 h) V1 v( r. H0 r' D# x& }( J( P
5 \" W) Z4 `" c0 g% _server {
1 b. Z& r1 w. ^7 Plisten 80;
$ N. [1 ^) T$ m& B. f9 oserver_name 52os.net www.52os.net;
! e$ V% |4 s6 `% n' r" J
( s+ o1 [5 x1 Z/ Q2 l. o$ J+ B2 ?" Dlocation / {, Z) D0 E* o4 y+ H
ModSecurityEnabled on; * d6 p7 T4 r$ z2 t; R
ModSecurityConfig modsecurity.conf;
. o: n9 _4 z$ H# l7 r2 l( G c7 O4 T" U7 | ^. D+ @! |( H. l# \
proxy_pass http://online;/ w. o* \+ l2 c2 b# ]
proxy_redirect off;
0 L k' Z/ Z2 x# ] proxy_set_header Host $host;
$ I' r3 g9 u: q4 _: P( K proxy_set_header X-Real-IP $remote_addr; o | a# V! \$ q+ Y
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
7 m( w; g+ ~) l }
2 V4 e, @% T' r6 Y8 P3 b}( c+ V2 |: U) B
六.测试
( B% O( l7 l% p; Q0 A o& y+ N9 x6 s2 b3 C
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ g5 S- l- L) m( X- a# d
: k+ V% J' a# w, P7 N( ^) a0 R" t<?php* J% Y& n4 l" `" j9 ^4 s2 Q
phpinfo();
5 X" P1 v- q2 ^; n+ E?>
' B, u* _' h N1 p$ t: Y! i在浏览器中访问:
/ z* y% U ?; m) S: }- d
7 l# V& b3 C7 N/ |$ E! R3 z( a" B2 w9 [http://www.52os.net/phpinfo.php?id=1 正常显示。
& V% x# T# N' _( |% qhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ R+ C* n6 \3 E! a" C
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
1 ]9 }' v! U: F说明sql注入和xss已经被过滤了
$ F, r% J/ ^/ K9 K/ b
- o; F. x [( _七、安装过程中排错- x' c6 ~; s+ E& y0 i9 a6 Z
: y5 K9 K. Z# k1.缺少APXS会报错
4 F+ b; \8 p) D' w* f3 a/ Q, j# \/ R7 Y6 f7 V
configure: looking for Apache module support via DSO through APXS! b& C0 G% b, |4 D% @6 }% T
configure: error: couldn't find APXS, a, B9 K9 T# S, a( G% {' d7 [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( V2 B8 K1 H8 j7 e0 J解决方法:& W/ f) o9 q; h/ I/ D
4 R- R" Y0 P( }: f; T: O
yum install httpd-devel- X$ K k4 G" L
2.没有pcre8 n6 y) d' e$ w" ~! j/ {
q" O' Z: y9 T8 a9 e8 V$ ~
configure: *** pcre library not found.
2 F0 B; r4 Z& k0 x* I+ Rconfigure: error: pcre library is required) j t$ F9 F2 o# z5 w: l
解决方法:# i* ] L$ W) k& c
z3 g0 b& _" C. ayum install pcre pcre-devel
( M+ _- E6 W ~5 R, N. O3.没有libxml2
# o8 [ o& H; N7 m4 }
! [9 ] R- H- D3 C! I: L
) E* K& t) O3 e' ^* d1 f: Yconfigure: *** xml library not found.
) s& t/ {4 n1 X8 v0 P3 K- ~configure: error: libxml2 is required3 h$ |9 M5 y5 p, w$ K+ G6 B
解决方法:7 Q; U; b; `& |' H# b
0 X& o9 _( z; ]% [ g6 Q! dyum install libxml2 libxml2-devel
/ b9 ^ d0 J! }4.执行 /opt/tengine/sbin/nginx -m 时有警告7 _, _5 R# F; j' a2 W
; ~1 U) o/ G! F, J7 |1 j/ STengine version: Tengine/2.1.0 (nginx/1.6.2)
/ q* m- S$ {+ ^9 Y% @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ v( K2 l$ ?- r原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 f4 d8 A4 T$ }2 @5 F* n
: p: F7 a& ~" N1 |& N2 ~+ D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 m' v0 Y7 j/ K ?/ W' T' b, r( s( v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
& U9 T6 u3 k7 \6 c' X9 w& z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 J: B+ _/ @; ?2 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 s7 @$ b u2 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" F$ \4 O1 B$ L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% c* _, G( Q6 R6 p( c9 c解决方法,移除低版本的APR (1.3.9)
$ p; }. }! q, a$ c: p q. a
' }) A4 @) E2 ^% N z) Ryum remove apr4 _8 A& Q0 J9 E; m" \
5.Error.log中有: Audit log: Failed to lock global mutex- _- N4 @5 R1 \5 l0 s
1 ~2 x/ p! X, H2 k
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 6 L# g3 }- m+ v+ A2 Y" V4 w6 l
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. i% g* d; i: D! Y' `6 G
解决方法:) q" x+ d$ C! r" H+ S3 J
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 E+ n m' A$ x" p, [' @) i l2 Z- ]/ A" T0 r
SecAuditLogDirMode 0777
" U* w1 e' D: R+ GSecAuditLogFileMode 0550
7 i8 A9 Q! M# p) ISecAuditLogStorageDir /var/log/modsecurity5 J. ~: u, N, h+ E; F
SecAuditLogType Concurrent
- @7 {2 R% p: l Y |参考文章:! ?* a$ t/ x, {! J: _+ A
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" j: @ p; |( l, Z9 x- K
http://drops.wooyun.org/tips/2614 |
|