|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ l3 S: e, u) C& ^1 q' a, M4 G
! e. K3 z7 K+ j$ ]! Z
一.准备工作4 O# i) W: j: d5 b# |7 G$ ]0 v
8 [8 _8 \# H. }+ h8 ?系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 O8 i* x7 |5 j: y. |% A
@0 Y$ ]1 L" q; t* D# wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 o+ B8 D( E+ g! S( s
0 m+ T! P+ S( j$ K! ?" [/ d1 F
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 n7 i7 m2 i7 A" i+ @7 |
& p' J, m; S/ ]: W" N+ T; L! zOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ L# q5 C$ p9 E0 i6 `: S9 `% W
; b9 u0 `! z: d- j; r3 f
依赖关系:
/ y4 d/ |1 t4 C- v+ M: r$ ^' ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 q0 w" g7 M% X# c! F
; X2 A9 T# K [+ c* P: z- wyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel0 a0 Z. e. S: m( q( I
modsecurty依赖的包:pcre httpd-devel libxml2 apr7 g3 _4 J3 J# L4 }
$ n- h, n! w! z' Ryum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ }- S5 z$ [9 ?9 n. L5 |! Z
二.启用standalone模块并编译7 l5 v- t8 _' n: w5 Z/ Z
& `& M, ]7 u% V+ `下载modsecurity for nginx 解压,进入解压后目录执行:2 Y% D5 b) d5 a! d1 u. W s7 n
o, s) p1 J" T6 `) S# A
./autogen.sh
. N4 }% f0 b; Q( }$ D8 G" M./configure --enable-standalone-module --disable-mlogc
y# n1 ?, @4 R: ]% Xmake
2 I# F; ]& ^" y% W6 `三.nginx添加modsecurity模块
" o, [3 Q+ Y4 Q/ M k0 ~$ D6 c
1 \2 }; s+ o' n2 u1 h! [在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% L0 ~* h2 I/ b h& Y" g. A! C8 F- L
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& R5 D1 k4 m/ ]: fmake && make install% E \8 T+ T" b: C1 F M
四.添加规则9 V3 Y, \9 k% {- d8 a' N
; n/ |/ j7 w7 ]2 R" X! h q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 k3 P1 q' @7 l, t5 H3 X) h$ j& `! L4 d% B" B: j: B* ^# u: J
1.下载OWASP规则:
! U! I4 F0 U" a8 t" |0 L
' C! {. }# T1 i: T2 _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( ~, M6 H# Y, i9 I t; d" T6 T
s- W) h S6 `1 a! \
mv owasp-modsecurity-crs /opt/tengine/conf/7 {, O+ G2 b% v( E. C% f
$ u5 D3 y1 Y- h9 }& q6 ]- q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ W" ~4 k; ]' y2 j- w B; g. W" B
2.启用OWASP规则:. K+ T! ~& ~' W3 L
6 ~4 j& }) i2 ~, F/ e复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. X: J# w* Y& G
! @ h( N3 M: z- W) E编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 }' s3 j9 V4 ?: w0 `0 [6 p" @" E
, O: u' m, z2 @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 o2 N* |1 d" R+ W9 s
6 Y% c! B: G; E. @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf _' t$ [) U8 {2 j% ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ f1 P d7 t! D, G9 I4 qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. n4 Z$ C) w/ i& q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 p7 @- A5 ~% |2 M9 M6 @9 j/ Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& r$ P8 }: \ Z- X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
, ~( @9 X4 A, c' C6 q1 G) LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& b9 [: N* a i( g; Z% S五.配置nginx" i3 X( v. a; S: c2 g
. F N6 q9 h3 y! {4 `7 f在需要启用modsecurity的主机的location下面加入下面两行即可:& @" W c+ H6 O! x
( R+ d+ ]- B: O1 V/ Z- eModSecurityEnabled on; / ]/ T% e, C4 i9 a. Y( s
ModSecurityConfig modsecurity.conf;: i. c1 J: Y# o& @+ H
下面是两个示例配置,php虚拟主机:; }1 [" b1 G, F, n" ?/ d
, }5 P/ q; q* W- S1 n0 z
server {; y0 V" k6 n! C/ A% T3 v6 M
listen 80;2 |& Q) t( L( W* }4 |
server_name 52os.net www.52os.net;
6 r8 q$ ^: F/ k& r7 s" Z4 y
5 j2 y" `( m' D location ~ \.php$ {
) D4 ]" u) v. d8 h ModSecurityEnabled on; 5 |; W4 o3 l: P0 y
ModSecurityConfig modsecurity.conf;
7 x2 e2 w! V& i3 C$ [" V
% U( J1 T2 U0 w# k- D root /web/wordpress;
- z! \9 x r; P* l+ C- f* @ index index.php index.html index.htm;
$ [/ `, N6 m* T. H) a: K; i5 s2 I% \ 7 E! M& s7 [, k5 S: c
fastcgi_pass 127.0.0.1:9000;
3 D( t' O, S8 Q1 ^. G8 n fastcgi_index index.php;; J; w1 l* e/ w
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% s& {" \' L2 z! o. n0 P
include fastcgi_params;
& O7 U( | J: T }
4 ]' d9 R1 @/ v1 B }* l: G+ ^, f, V& w2 S' s* T
upstream负载均衡:
$ Y' C- a/ S- X4 b' p# _1 \
1 n1 e/ R! R4 z/ qupstream 52os.net {$ J" J. V4 R1 e& U9 ~
server 192.168.1.100:8080;
3 f8 j4 d) Q$ s server 192.168.1.101:8080 backup;
# B1 V: M7 }0 x( @}
c, U. [- w: P( c2 i9 T1 ? t& O D
server {
' |4 o) ^+ W9 P6 Vlisten 80;
" b7 R1 g$ M4 r, m( nserver_name 52os.net www.52os.net;; |& o0 Q& g: z& m' H7 F
( K( S/ X- K0 ^. r) Y8 Q1 ]location / {
0 d$ y& W' f* h- B6 `8 I ModSecurityEnabled on;
, E' c0 [. ^$ d, f* { ModSecurityConfig modsecurity.conf; ! S+ y+ b; t! \! c2 w8 p# C( B* _
% l7 L2 J+ N7 [4 k1 s- }& G8 G! K
proxy_pass http://online;( i5 b4 `) i7 t( }6 a
proxy_redirect off;9 D- H" l- R! q, G1 v) n
proxy_set_header Host $host;6 B# I9 L9 x5 |4 `; W" G; Q M
proxy_set_header X-Real-IP $remote_addr;* Y' D. u. l6 p6 d# d; c2 [
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' q2 J9 M7 u3 k1 ?. n: ^
}5 m$ X: d3 u, p: b j
}
2 {2 H- n; p. Q/ k& j P' d, T1 Q$ x六.测试% R/ y) ] f0 i- i. {% `
1 q& E5 Y+ q9 W S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
# ?* K* H7 H. `& h9 \! q- A
0 m) A$ @7 \7 y1 H( a8 F- D; C6 k* Z<?php/ G0 A- I/ x3 b: x% `8 q
phpinfo();
9 N9 y/ C* b3 A7 y) k6 @: j?>: Y) p) ~# o: u. Y9 ]3 P& j" o
在浏览器中访问:4 W, j. F1 `1 N, d# l# Z$ r. z
5 q. A& Y- X- a; O8 ]8 `9 L8 t8 M
http://www.52os.net/phpinfo.php?id=1 正常显示。# ?9 p. L+ m; _; A) N
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 P5 P4 g, \3 ^- X/ _http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
! E- @# Q! i% ]- }4 }说明sql注入和xss已经被过滤了
z- i9 o6 S4 y* A0 o' l5 \& b7 O! C5 ~
七、安装过程中排错- H6 B+ X3 S" r7 k3 U5 j6 P
d. k5 P; \: V2 V; L) V1.缺少APXS会报错/ o' ~& x* }/ g- Y& O6 @! [
5 A( D4 t. U& _1 A( G- W; A
configure: looking for Apache module support via DSO through APXS5 |" H+ ]% G( A O, E
configure: error: couldn't find APXS
7 x) V& A& R1 M6 K) B [1 Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 z: _, R& a9 ?0 ?# ?# I4 U解决方法:
. X' s& m5 \) m/ o- r; f( y1 H6 j
5 h5 \+ a2 t( ?2 n7 i6 ryum install httpd-devel
. G6 I0 i, W* I, `/ g- O# F2.没有pcre+ A5 M- c1 B: |/ Q2 v
* d' Z+ q) g, y
configure: *** pcre library not found.& R* m7 L. e r% o" \4 e
configure: error: pcre library is required) y) Y1 ?) f! Y0 Y
解决方法:
/ l) P4 _0 a: `4 ~/ C
/ o. |. W! M- U* I# i0 L" S. iyum install pcre pcre-devel. Y2 V! M' f, L6 v1 w% b
3.没有libxml2
4 m; x6 V/ A0 u7 B! S- G" h- n" B) ~5 F H
& R2 Z0 h: Y8 c1 _! Fconfigure: *** xml library not found.7 K) h3 `5 X% u, A# D
configure: error: libxml2 is required
9 D$ r- K! A. w0 J% ?9 I9 e解决方法:2 l1 a3 B# O& H( K/ J
9 B, G% Z% W& S$ M! y* m( nyum install libxml2 libxml2-devel
/ ~) p- G9 V; s3 i4.执行 /opt/tengine/sbin/nginx -m 时有警告7 d! S; N# I0 F0 @$ {
) C* x* [( O" i& ^, h% r# ^' K! U
Tengine version: Tengine/2.1.0 (nginx/1.6.2)3 ^5 T4 ^8 B8 r/ u& `4 w
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 @+ }; X; V- U! B7 v# g9 Q4 |9 b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 I. F8 X: X" C6 C6 c6 p9 G3 ^9 W: }8 f. Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 ]0 s" p: f/ D" M. l! A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"" C4 L; E, O1 }3 V. n" T! W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" k. E3 l, r) n2 q5 z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
9 c9 E' t; F! \$ F2 {3 H0 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 Z+ z) I$ ?. p# V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 B" d/ O5 i( W, k. O# h4 {) l2 e
解决方法,移除低版本的APR (1.3.9)3 i+ ~+ D- Z& {# |
7 m! R( Q3 i0 i) t
yum remove apr1 D8 J! k) i. ~) ?: \
5.Error.log中有: Audit log: Failed to lock global mutex; |1 q, }. S7 i+ ~! z! z
3 F1 z9 y+ S6 V1 @5 D$ z5 h6 C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 0 C' ?& w* E" m% c8 B% y; L
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
t: g8 `3 {$ L4 ?; f3 Z4 V% a) w# m解决方法:$ O m, b7 z8 Y- b( F
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ M+ }2 \2 x+ P6 j4 [# h% _' n. n7 X: S
SecAuditLogDirMode 0777
& k+ u' `8 l" L* J- b' ]) g# CSecAuditLogFileMode 0550
9 X' r$ j+ S: [5 k1 w6 ?SecAuditLogStorageDir /var/log/modsecurity
; d& m7 W; e1 ?7 c7 zSecAuditLogType Concurrent: t+ c! \0 G) x+ X
参考文章:0 U u/ K# b$ u9 d, {: A5 t4 k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, y( y# t) Y8 l6 X+ `( P% Jhttp://drops.wooyun.org/tips/2614 |
|