|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# t9 u, j9 f, `( f
7 f# x- |/ ]7 s5 C+ g. E一.准备工作
& ^6 I! m2 `4 D5 P* u& q: X6 X- e
4 U3 W8 h% G# B: `) P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. A' m* S; P& z7 J# W
, c5 y- t( k3 Z) h8 v; s4 g) itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# K G2 F! b1 C7 A: X! _
5 W& A' q4 Q( ]) C) P9 M
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 ^5 K9 Z/ e/ o% V' {
$ h! U' m& e) m- h* s8 z/ ]$ |! @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 Q7 `. X! t# g+ g. [* z3 M/ p
, M: s( N) h4 {4 [6 {依赖关系:) a$ l0 O2 H" e; S- c. D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' v% y" x% R+ k% ], a0 ~7 f+ E
4 u" w; v2 h% ^# E6 I' a" ~$ [; uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# S; c4 q% X0 Y% Q9 H- n
modsecurty依赖的包:pcre httpd-devel libxml2 apr( u) r; E! E9 w3 L3 f1 H3 n
3 m5 B& d% Q& G* G7 O4 Y4 Eyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 K4 l& w+ u; y. R5 x6 r$ E二.启用standalone模块并编译/ M6 K( L2 W4 k+ T5 p
! A; ~9 E) c% @% \; F下载modsecurity for nginx 解压,进入解压后目录执行: H) q5 ~- n( ^& v: c. J
6 d/ J% L& |8 {8 r
./autogen.sh
) g5 i6 J1 v, Y4 z./configure --enable-standalone-module --disable-mlogc
, I4 Z. C3 ^% N2 [" z4 cmake $ @8 w2 x/ M! q1 ~ y8 c$ q" F
三.nginx添加modsecurity模块8 q# j& I9 \! W" \; w2 g1 {
6 M" ^) N/ s1 C1 M4 d/ S& c1 R1 F% `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 R# I' u7 _3 l6 `6 |- E8 I3 O
& I1 c' q2 Q8 l, {6 F/ b8 Q./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# [7 ] l( w6 _! n
make && make install
: }7 ]- r" c& z4 ?四.添加规则
" a$ v$ @3 T$ k, h0 u# m" d- _ v' |1 Z- Y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 ^ y2 `' c9 n G ?
! M5 t- d4 O% \3 U7 ^1.下载OWASP规则:4 D% }& x$ A4 X ~- B; @5 m
% O" Q6 q7 }4 _) Hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, N1 `! b% M% Z; P7 M2 x
: D& R p- n- V) tmv owasp-modsecurity-crs /opt/tengine/conf/# C8 O3 [" Z5 F) p. p" P
8 f6 g% x. X( Z- h4 |$ V, ^. m! \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ c7 n9 G0 ~: z( [) }7 t# I# t2.启用OWASP规则:: V( ?5 W/ s( e2 d# d+ H
/ s l+ M ^5 P) w3 {3 }9 [复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* R9 F# t* w. p1 r* h3 b! Q4 \; z. @& x
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( _; K+ O' u% z% a1 X
3 I2 [- o+ _ I
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% h9 U% F( S4 _# m% R1 k
4 U' Z* x2 I! O9 pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& @* t3 P8 U O% N+ e. iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( f9 I0 R- N0 k& `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 ^4 P) T# t' b/ CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 \# Y5 T$ D( o! t; \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) e/ G) @; @2 q& ^% x$ eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- ?8 m/ Z) Q8 s8 O+ A( p% zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 l1 P5 |6 ^ g& A3 W4 i五.配置nginx( Z% S8 D% I$ \1 B4 W& M9 L* v
5 B" N, E& B/ ~& O4 @! p5 B在需要启用modsecurity的主机的location下面加入下面两行即可:
8 A% R% _: [4 Y( T: x* V$ i0 [4 P' I1 h( K) O2 z2 N* n9 K
ModSecurityEnabled on;
# M% s3 ?6 _1 H8 [* PModSecurityConfig modsecurity.conf;% h$ _/ }+ t! y0 d) a1 Z% m# l: c) a
下面是两个示例配置,php虚拟主机:
5 K" W: f, Y* `) M) ^
4 P7 q, I1 Z6 J$ T3 U. K; ]server {- d5 Z9 M+ s: X# x9 d b
listen 80;" b/ v! F: J/ f) d$ n! K
server_name 52os.net www.52os.net;
# V% T9 S6 p5 l% k
* ~% H1 P& n" U" [. k0 {( Q4 \* L: O location ~ \.php$ {
" R4 q$ C, b9 s" \* D$ }- J+ o( O K ModSecurityEnabled on; 6 [7 I! l/ v8 V* N! s0 f( e* m: B
ModSecurityConfig modsecurity.conf;
* I/ H- Q, T7 Y: k8 h# `2 B; h" X" j$ n6 |$ P; E R2 X J' ^
root /web/wordpress;
7 H0 i- M; {) A$ U index index.php index.html index.htm;
. D/ A( r: [8 J6 f' @
# h) L) _8 A1 z; k0 A' q) ` b' ?+ _ fastcgi_pass 127.0.0.1:9000;
- f$ |8 l+ K! F# s# ? fastcgi_index index.php;, j$ b& }/ N# g
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
; O2 t) C0 n6 _7 K9 U H V8 o include fastcgi_params;
" D) k3 [( z* o }
& s& ^9 |2 o- k6 Y( ] }: I9 A: b/ @+ Q% v% X) T7 y' w
upstream负载均衡:
" ~* L0 u! J% `4 @
5 @' c+ l+ ^$ |upstream 52os.net {, p( v) w% r0 P& J, r' y& e
server 192.168.1.100:8080;
/ X5 |& D: t5 V$ F0 i server 192.168.1.101:8080 backup;
2 K! U1 |8 T+ t2 P, A: g}0 i. u4 A1 n3 A! s( K7 I3 ~
" e9 F* U9 X# `1 `* f7 i$ h
server {) T9 q4 G, d3 k
listen 80;
r5 V2 ]7 f9 ]server_name 52os.net www.52os.net;
' L$ [9 l4 C) S) s# t& [; n3 Z/ C" w; {- b3 ^0 G3 L4 X2 d
location / {# E9 s. H0 I* E5 A1 _( m
ModSecurityEnabled on; " i6 E: o- d: Y- P
ModSecurityConfig modsecurity.conf;
/ [" D- o; u3 g; Y+ r$ K: c3 N1 n- F* `8 I3 }; [
proxy_pass http://online;
0 {, h. V3 d+ a% x proxy_redirect off;9 }/ z) ?! m& i- _3 D
proxy_set_header Host $host;7 ~. x4 N+ F% ]: H7 s3 x
proxy_set_header X-Real-IP $remote_addr;
* @* S& z% x5 [6 L7 P* @( ` proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 k0 w' [( ~* a' W/ l& z2 G+ o+ m
}
' L/ J2 N: _7 r' L}" P& f* e5 S2 E0 I$ q
六.测试
: ~6 I% k1 k, u% `
, \; P3 Z' V0 H+ ?% F7 a0 E% z5 K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% b* S3 ?/ ~9 j+ ^# r+ W3 x
/ f! W6 h" P- R: P! h4 P4 k+ f
<?php c6 X# P H7 ^5 f ?
phpinfo();
/ K" P& E7 V7 P6 w. S?>
# \& V V; o+ r0 ]) R在浏览器中访问:* e7 h: }2 U) U
. }/ ~/ ~4 {- D# s! r! [. d! t
http://www.52os.net/phpinfo.php?id=1 正常显示。
M3 U I; d9 Y; f C) S `http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 X& d6 C) ], \( \; j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
. y+ _4 c p( }# {5 m说明sql注入和xss已经被过滤了
5 X# J0 l8 m( A, t
2 C# {$ d: ]3 W0 g5 x t七、安装过程中排错
0 W8 P/ P: `7 w2 k# A) f' u% X5 e% i a S6 ~. k8 V
1.缺少APXS会报错% n8 O j+ K! d; [, @! T
% v8 C, c* `; oconfigure: looking for Apache module support via DSO through APXS
- X7 {% k! w7 ]3 S2 ^! Jconfigure: error: couldn't find APXS
+ a) e, v, c2 D( T* _6 ]; iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ j! b% C. W8 h- i' m
解决方法:
) g7 @6 u6 d" v7 Y9 a l, g G+ `4 I# f! R# m. Y
yum install httpd-devel" p& N/ [- m X( K7 L
2.没有pcre
3 K. ?+ K* h# m# N2 U4 P7 K
& H( R" p! G, M# [8 ~configure: *** pcre library not found.
# e( h; A: t# }* H' ?5 C/ Bconfigure: error: pcre library is required, j( A* j8 j3 ~! F! [3 t3 b8 ^- @8 o* l
解决方法:
9 @+ K7 O' m. j+ O7 z$ ^/ L
4 \7 }9 {5 E* g/ hyum install pcre pcre-devel4 A" A# x: v: ]- _" B1 J
3.没有libxml2
+ o' j, d% y0 C7 |0 U, P
' A8 L# _- ^, G# n$ G/ @5 b4 w; i# `, ^6 |0 o4 f7 s2 p
configure: *** xml library not found.
9 H B& p6 j( A1 \' B* Mconfigure: error: libxml2 is required, C7 ~8 d1 K$ I7 ~
解决方法:. V; _. E: q: s: Z, t9 C
/ S7 x+ F8 G1 u) i0 _+ Uyum install libxml2 libxml2-devel7 ~7 _: G! w8 S
4.执行 /opt/tengine/sbin/nginx -m 时有警告- {2 m" r& U* y, {+ E# ~
+ B- f3 t5 f! c" N8 X) OTengine version: Tengine/2.1.0 (nginx/1.6.2)
4 \% Q) d7 Q8 p4 j, P* F& [& w; o$ H/ pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!" @, v. s! I; N1 B0 y3 V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( K; B5 `- E7 D9 S
+ z }& N4 q& `7 D9 Y$ I7 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 I4 [! R# y- p. q3 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 M K& |' S' B2 S& P, z( B( @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 A3 @8 V+ I( s- M4 X& \: O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 f! l% `) G7 ^0 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- I7 k0 P" u4 M% P$ O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 `- K7 A7 H; Q. _2 J2 J) d
解决方法,移除低版本的APR (1.3.9)
$ V/ |- Z: @1 l5 ~
3 z% D( b* h1 u( u8 qyum remove apr
& r4 _# g4 a2 n4 X9 K4 X- i- p, n5.Error.log中有: Audit log: Failed to lock global mutex
4 e5 T% s+ w- I. V; Y* O
! k/ n0 a2 Y5 K" J( f8 C2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 T9 z( Q2 u9 h
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; `' _6 ]+ W. |. v9 U8 H解决方法:1 `* T4 I+ d' [, \9 C- R
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" @& ^/ N$ e1 m9 I. z: P& j+ `8 O% @( F
! D: L0 H: r, V1 M$ H$ OSecAuditLogDirMode 0777 w$ Y: q9 f! I2 [9 ^. t6 R
SecAuditLogFileMode 0550
; Y6 L2 K' v6 R( G( ~% Z9 QSecAuditLogStorageDir /var/log/modsecurity
& i- `5 ~) |; p7 s7 S8 eSecAuditLogType Concurrent
# U6 w6 s/ u2 I参考文章:0 x- Z( q% V+ i6 b/ G7 {9 B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# G8 q: p( w, H$ Ohttp://drops.wooyun.org/tips/2614 |
|