|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 D7 ~$ G3 ]4 |) m/ B
4 K! Z% S2 W9 V1 u8 y' S一.准备工作* ~2 Z- {4 u* x8 c- d
$ v" L/ h9 J8 ]( Q! t
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& ]7 y( z$ F; I4 R! l/ N$ `; u2 m/ V" m# I$ q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* V" N7 `# T4 P
: V: {% y& l4 ~modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 L4 L3 ? U. g( p
! m+ g% p l7 [' N7 z! E5 KOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, z" U: J. U$ x H) _' w) { ~- ~& |
依赖关系:8 E* z5 ~/ h- ^* I: O) D2 [) u
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ ]* X% F9 P6 d" w: {7 L
( B, S" B8 h& K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 P* A& x. |0 G9 ]$ Y0 {, omodsecurty依赖的包:pcre httpd-devel libxml2 apr
( J/ R0 H/ ^( y
2 c' M# e) X' ?/ J, j2 J$ xyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" M/ h; c& V4 Y0 _
二.启用standalone模块并编译! t/ c; D& E3 z2 E- d
. _) S2 e7 g& z4 I" M2 m* d
下载modsecurity for nginx 解压,进入解压后目录执行:
3 E' \. u' c9 N( s0 S5 { a- w9 k# F3 V3 o- R! [
./autogen.sh
$ n! _0 X% ?! |% H8 l2 `8 Q+ W./configure --enable-standalone-module --disable-mlogc( k9 h8 r. e- u" t; Y# Y/ g
make
6 g9 `. ?& ^! b6 u! ]2 h5 E三.nginx添加modsecurity模块* A1 d3 J. H" J4 V* u
3 T4 ~2 }" V) o6 `$ b4 l2 [! Z7 M
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% H& f. ?8 P5 z2 ], s* C
+ p% B" k& e5 U2 j1 V% r1 d+ t# n./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 f6 O( N- n' M( W) ~$ r5 g
make && make install8 f! O6 a3 Z1 `; k
四.添加规则- b6 S+ I$ W8 Q
% y$ V& p4 p& m6 omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. E( S1 M9 P) o8 E+ v" C; y. i1 D) U# H; O/ P5 ^4 d: L
1.下载OWASP规则:. x# E; `' ^ t4 s$ n* g" C2 S0 Q# p
+ F- @$ W) X; K N+ A
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) D- G( L& t; `& C4 t E
T9 _4 l5 c# K! ]: Vmv owasp-modsecurity-crs /opt/tengine/conf/1 M( ?% S, b1 i3 |
& }9 f0 A6 X, ucd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 e+ i$ C, u# ^6 a2.启用OWASP规则:
8 O0 x; S/ Z2 m+ m1 g8 x' Y8 B% s. q; m3 Q( l% Y0 `. N0 P
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 w& }0 @) I4 w2 `7 h! W$ W" z) q
* e+ t( N7 o( N P编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( m V. @" ]% D% a. g
. o; r; h4 T: D: m) A7 E* |owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% K4 ^0 U6 v. J: S& c O+ l
b: P0 }& q* L3 M9 JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ @2 {- @- J2 Y" A1 F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ q! e& ~6 `- iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% j; |# |2 |1 W9 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" R2 N) [9 z, J9 p0 D: s* f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, ~" w' I; E7 o7 g! e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 `: U+ B+ u! VInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 n, q; P2 j# J& v5 l$ E+ i1 ]' l五.配置nginx: ?% R/ M- A1 j# L0 t( N2 Z( x5 m
4 o/ }6 c9 \+ X1 E" ^2 M在需要启用modsecurity的主机的location下面加入下面两行即可:
( e n, H! j; x6 z; J* L5 A5 e! d( n2 _6 E$ Y$ t" k, L
ModSecurityEnabled on; [8 C8 a! o! B8 s7 ]
ModSecurityConfig modsecurity.conf;
. m" }* l7 X9 i& W. T) {' Z下面是两个示例配置,php虚拟主机:/ [/ Y! n# K& m, z
/ K `2 `4 g/ z
server {
3 \# i7 \0 {* n listen 80;2 q# v& ^0 H$ z5 \" {5 E1 X
server_name 52os.net www.52os.net;1 A' f8 S$ e$ q8 |- L4 p: B* p
, R( C+ k1 G6 `3 S! e location ~ \.php$ {
2 \1 ^6 }9 O' C1 b3 k) T. Y; L ModSecurityEnabled on; ! z n( Y3 t6 L) ~# B8 O) L2 ^
ModSecurityConfig modsecurity.conf;
) d: {& i! r+ q+ } a3 I8 v. M* `. _, G) r. H& N
root /web/wordpress;$ `* D) N& F& b f
index index.php index.html index.htm;0 Q1 U5 N& ?* `: d" W
8 K' B' m4 K) q L% f ^* N2 T9 d
fastcgi_pass 127.0.0.1:9000;' Z5 x2 [/ l, Y9 ?: K
fastcgi_index index.php;2 H1 L6 G% r3 T- f% p
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
7 }& `2 D! z; e1 ], I! g; E include fastcgi_params;" N0 s: @* L% O: T2 B
}* R. s3 i2 M3 A2 D Y
}
5 v! C5 F8 N2 f, F" Qupstream负载均衡:! j1 L" n& U* Q
3 q! P6 N1 g) L3 J" u8 D- kupstream 52os.net {! j0 D4 i( m& ?
server 192.168.1.100:8080;$ x2 K5 X2 K* n( M# ?
server 192.168.1.101:8080 backup;% N7 @7 A8 p3 @! X) f. F
}
1 v7 V$ P* {& Q
3 Z: T' _, q0 B* S. Sserver {. ^ u* L, p) l, B* P
listen 80;
. D7 {, a( ^' Q" M* s$ o' cserver_name 52os.net www.52os.net;
- k5 E) a& G' r+ t7 V* m+ P( B4 ?! [
location / {. I& ^) p1 ^3 x1 g9 B4 }" Q
ModSecurityEnabled on; ; L& \ H9 @ s) z6 Z/ l
ModSecurityConfig modsecurity.conf;
3 f$ O' d9 W4 I W( I( |! y
3 Z8 ^$ l' P5 ~2 C5 o proxy_pass http://online;
' X4 v5 B# t6 L proxy_redirect off;
2 I6 |# X+ G4 n( t( T proxy_set_header Host $host;
7 D; N$ h/ H o proxy_set_header X-Real-IP $remote_addr;
* f2 }( I: \# c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! z& |# {- w* P9 r# M1 M }
4 i ?/ @; g( j3 Z}
7 A6 T9 A7 q w% F9 a六.测试% F/ \) i; W& I+ ~
1 C+ P( h* k7 c& F1 B: I% g5 @& @我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 J! Q* V/ L* k" v
" A* Q2 K7 V/ ?3 u7 M<?php
2 ~# A+ _# [$ O2 T$ B phpinfo(); + k& G/ A: u+ G$ }- ?
?>
$ W% j. }' ?2 W7 g! f/ E# m# {5 |在浏览器中访问:" t. T6 [3 x7 _$ x
% i$ Y B; A! ^; y* `: o. v
http://www.52os.net/phpinfo.php?id=1 正常显示。3 j' h) G. Z! ^
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 \8 D* o7 `$ C; \) |6 C# d- G
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。, R4 {9 |* s: [7 s
说明sql注入和xss已经被过滤了
; G; F9 c1 g; h7 l. j w V Z) f e$ M
七、安装过程中排错
6 L |0 U2 G' `- ]4 I2 \: K( Z0 i! y) `% g2 f
1.缺少APXS会报错5 J1 U( B5 g2 N$ n, r
7 O* T. o8 k9 nconfigure: looking for Apache module support via DSO through APXS8 u0 b. E) g% t
configure: error: couldn't find APXS
7 ]: [3 {9 b( }) W+ {2 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: J6 a' S. w' ^% b- i A; h解决方法:
9 P1 {! B" {- g+ P* \
5 t# A* z: r( s8 i$ Qyum install httpd-devel
2 P- h/ A0 i) S' c& g6 E4 q0 B2.没有pcre
8 ~( G, v" O* ]9 [/ K) e% x. n% S, h; _1 i, k( _& z
configure: *** pcre library not found.. F9 k, w- V3 I9 M, y5 H+ L
configure: error: pcre library is required: D) j) g+ N+ |" N" b" g, p
解决方法:
9 U( Z& Q6 K# t, d% I0 }; [% ^1 a0 b p
yum install pcre pcre-devel
# H0 z1 d" U }$ x; s3.没有libxml2
- ]3 V1 ~0 B- L) H" R8 f: x
1 k1 U/ H9 u' b& j: f6 R
8 c, ^ b7 f7 T- p& R1 \1 Y; m$ yconfigure: *** xml library not found.
: \ q" S. ~% s8 Econfigure: error: libxml2 is required; l. b9 M( ~5 O8 F6 g+ B
解决方法:
' z/ ^9 k* h" W3 z5 H8 V8 B0 G0 F8 k5 h& t/ r6 h
yum install libxml2 libxml2-devel
5 J& F+ [0 e# a, S4.执行 /opt/tengine/sbin/nginx -m 时有警告4 j @+ X0 }& T( Q6 f9 U
* a. D7 i0 J) ?5 O
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 v* }9 Z. z0 M$ g G. `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
y& R& e7 ^# F' j& w* y J" w原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' j( K& m1 u! X0 G: s+ J) g
9 A' h% B* f8 T' H3 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ b$ t- P5 ?+ l7 l. |6 ~& g( M2 L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
+ M( d% A- r9 _, @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 ?' b+ M6 p8 x( I/ j& O# r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 y' a4 J" n# M8 v: j5 Q1 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") l4 ~( U. e, V8 Z5 [) x# E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., Z* Q/ c8 d3 v- u
解决方法,移除低版本的APR (1.3.9)0 q' Z6 |9 z0 v; o% w9 [
/ q: D3 \# a2 O5 h) D' s
yum remove apr# c( B! K- N/ Q! `
5.Error.log中有: Audit log: Failed to lock global mutex+ c6 U6 {8 s9 g) T
3 H4 Y8 p) O/ Z8 s5 ^2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ( h4 f% [4 T6 i3 U5 J6 H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. L& a7 y1 R" U. M9 |; D. g
解决方法:9 i/ R4 c6 Y' O' I. W7 R H
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- \: b' ]: m- c# T! X, V5 {" \
4 S5 I( U: [5 `
SecAuditLogDirMode 0777
3 t' \# M7 y2 {- e. \SecAuditLogFileMode 0550
- L$ w- v6 \& i4 OSecAuditLogStorageDir /var/log/modsecurity7 o: w$ Y/ p% u6 f$ o) Z
SecAuditLogType Concurrent( t$ K/ x S4 U& t/ _ R3 A0 q9 C) p
参考文章:
: \$ K0 C. J9 Q/ ]% T2 U5 ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 g" g7 @3 O" s* d, u9 |0 o
http://drops.wooyun.org/tips/2614 |
|