|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 c* _, h- _" J6 W
& p3 u5 V" Y& y, L- y0 Z9 h) Y1 A一.准备工作' n% N% ?) [/ C2 W a# l; C
, l& ^7 ]: w% z. u% A$ O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& G! l0 g6 H4 |$ S/ J
; C' U1 r# F' H* ^' h+ L$ Mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; |# U7 o$ k% m, @% t! a8 M: D
6 h7 P9 s' p/ mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 Q( }6 y! v- p5 _3 c( e
+ A9 H! H2 v X* w. [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 ^9 d2 s# W7 Z+ e. z5 Z4 N
( e) b& @. q. S* ^3 ~5 L依赖关系:$ S9 \5 n. s, Z8 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 k6 y$ d$ L+ T; S. ^/ ]4 ]( g, x2 R
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel g( ?& i1 Q; I, e" ]$ i
modsecurty依赖的包:pcre httpd-devel libxml2 apr/ z2 P) W) L& y
" R/ S- @+ _) |# @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% W, {1 t. I6 x: i' l- N$ X$ Z
二.启用standalone模块并编译
! I z x: y& h; C2 x/ q( F, v* I. m% J6 Z$ z
下载modsecurity for nginx 解压,进入解压后目录执行:+ O3 u3 ^( s! o' G0 a9 k9 S$ C
7 }( I* e! ]1 e2 f- \% |./autogen.sh9 f; D& B/ |+ d' e
./configure --enable-standalone-module --disable-mlogc0 ]+ s ~% T5 F3 y
make . i4 v- j0 O8 K0 u0 R. d3 Q
三.nginx添加modsecurity模块# i6 A+ y! Q! n Y/ ^7 a5 b; w% |4 z
3 A5 u! ~1 K: }7 b在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, }; r7 g2 Y) _
7 C1 s: I! u" N" _2 }6 M& J, V3 s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
* f% r9 W) q7 w: Q( K) O/ cmake && make install
1 Q. |# A0 q1 X( A( \2 |9 i, u' c四.添加规则, o4 l) Y* k `0 W/ I
. {9 n/ N# g% U0 J& Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* D _) O: a' ]- }$ O
' W$ R% J S& b$ ]/ l- B Y+ j
1.下载OWASP规则:
: v: ~$ M( e( t$ O8 F2 O4 p# d4 O1 `3 L* P: c' p
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs" @, M6 W9 D) \. w- c; }
# f! i$ I* A/ J( ~mv owasp-modsecurity-crs /opt/tengine/conf/
; ?# N+ j7 b3 v& I9 u# w+ o* D% O/ X6 V1 a
6 i( |( M$ ]) o; Xcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 r; K0 w5 G2 F3 H, ^
2.启用OWASP规则:0 {% L2 n4 J: P% s" b
& Y, \1 G) i) ]: j9 ^! m5 v
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- L. |6 w3 g( U& |- P
2 [# H1 I6 [0 ?/ ~) Y8 _* G
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on: B" M3 T# m7 j6 ]" y
" a) V: O2 p: ?# }5 A
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 R: _' T! A0 q+ ^) T+ S' E
; ^. n! Y- _! d2 y. W% ^Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: {5 m( R- a: c- v+ Q' S" R4 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: G4 b G9 h. mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( k/ }: f% S+ Y1 C' e ^# iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& d4 t& b9 f; X) ?- v. l- [1 G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ T+ R* `( G9 UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
6 k8 m4 N p4 T, Q8 c Z6 UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. j4 W( _0 l6 g8 m" j五.配置nginx
8 m! l( L! _) J$ N+ Y& b
. a$ U: [. y, }; C8 K1 T在需要启用modsecurity的主机的location下面加入下面两行即可:
! _( W1 a) F$ H! Q' k' m
3 ]' w% t1 V5 c* Y3 UModSecurityEnabled on; 7 t* j. Z# N# U2 S, F* j* f
ModSecurityConfig modsecurity.conf;
/ M# H3 H9 t: r- F4 E下面是两个示例配置,php虚拟主机:
. A6 m( h3 W8 {$ W
* I* ^ {9 E7 nserver {/ W) k5 V% c# t- F9 y! C. g; A6 W
listen 80;
- p; g' E. W; L4 ~7 ]! i server_name 52os.net www.52os.net;
) u) ~0 Z- a" b- W+ q; F+ M ; k$ G9 K6 F$ |' x7 N) ?# C
location ~ \.php$ {
% | |3 T7 i' v) B ModSecurityEnabled on; * }4 L& C! p5 Y3 g% Q
ModSecurityConfig modsecurity.conf;
" y# t% U1 U& O$ p, \# a# K6 `; ^, O7 W# p
root /web/wordpress;
\& p Q5 A; W$ V9 W N4 y index index.php index.html index.htm;/ p( T9 b5 V" s7 N" d/ l
1 j0 e: n' r' t fastcgi_pass 127.0.0.1:9000;
* D. t; K4 }9 c* [' D) R# y& x, g fastcgi_index index.php;
4 X1 F9 N7 K% l3 [) N4 V0 t; \ fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
$ f3 ? c6 w2 l- Q+ r. [ include fastcgi_params;
7 o8 d0 F: B" A9 J+ f }% w% r+ w( N& E* Y$ [8 b. X- }
}
% N1 V) P! h# ]+ `8 vupstream负载均衡:3 o' t& v, ]4 T" r; W: q5 P
: z% C3 l% ~# L8 O; t& q: m1 E
upstream 52os.net {
9 L- o" o& M7 D0 i. t+ O4 G5 ] server 192.168.1.100:8080;% R2 ~ R$ y5 y
server 192.168.1.101:8080 backup;
; y6 G$ }+ f3 a}7 ^5 Y1 m% V. T0 k
2 S) [" Y( q {5 sserver {
4 G" ]' A! N3 V" Nlisten 80;$ |+ t9 i! t" o" l4 r
server_name 52os.net www.52os.net;
4 N/ Y n- k6 ^6 c; W: Y" o5 Y9 {' I8 n7 }
location / {7 l$ q! }" {+ c4 {+ I+ N
ModSecurityEnabled on;
4 L9 |; l2 f: j' j( D# v ModSecurityConfig modsecurity.conf; % a8 D4 r& Z" Z) L/ A
6 h6 M% m* w; [1 O9 `
proxy_pass http://online;
& Y' k% K! x; l proxy_redirect off;3 _+ n( z: I' M) P2 h
proxy_set_header Host $host;
4 D9 C# \ P; k& k9 t% b proxy_set_header X-Real-IP $remote_addr;
7 v) o/ c# D- h' d proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ u# v8 x. z( u/ w# N7 Z5 M5 w2 w }% h, H" ~2 k* ^7 S
}
o7 ]; a# I( G六.测试
n. H( v3 z5 c& q* c: f" S
' J* b( ~5 |, }# a" j& x( U% p* _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& c' Z0 I8 i- {/ V4 U
1 ?( y. ^+ T$ g' N) {# \<?php
2 Y7 W8 {0 q( [/ a6 N phpinfo(); 7 {% a& t8 j, D7 K, ~7 X+ `
?>5 O( Y! r2 b( L+ r7 U: s* y
在浏览器中访问:
% Y5 D* q% E+ z, A; [2 h; {) l7 Y0 F# q5 M% f% H$ Z; G7 ~* F
http://www.52os.net/phpinfo.php?id=1 正常显示。. @- {, }4 Z' S3 L
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' c: {; [" C$ @5 N; R5 @
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' J# Y: b' N# @
说明sql注入和xss已经被过滤了* _: L+ J& ^4 b# M* e5 b( O
% @; z0 c8 m, b+ z6 v- [
七、安装过程中排错
5 ~/ `/ ]8 v. ~4 D0 h- i- E
! \& [% V7 v. E+ k1.缺少APXS会报错. j7 e# K5 z( i z
( y5 K" T. ]4 a
configure: looking for Apache module support via DSO through APXS% C2 @: x) n/ a% F* i( z
configure: error: couldn't find APXS
" U3 I* Y6 x# d& _apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 B! G, Z$ @1 \9 \解决方法:
3 E$ L- t8 u0 `, w) M$ m" X
, H2 o8 o9 c, A/ b6 fyum install httpd-devel
% Q- ?. ~/ F4 `2.没有pcre
2 v$ K/ i A: C0 s' E' }. f5 `' f2 m! W" q) A* t4 S* q
configure: *** pcre library not found.5 \7 ?- l8 q7 r
configure: error: pcre library is required6 a Y. N9 d' l6 q
解决方法:2 Q$ P9 r2 `- K G8 P6 s; p
: X( K7 J! Z* s2 b' Y2 L) M6 l3 S
yum install pcre pcre-devel7 @5 G( i* M8 k, X" r
3.没有libxml29 u( i0 Y% J' [- [3 D
+ H9 o- j1 W) V" E! i
f9 r4 ^+ E. F# m+ K8 P. [+ Jconfigure: *** xml library not found. ~: f; k9 Y1 X+ W
configure: error: libxml2 is required
) Q) w" |9 B0 r& i' p解决方法:
: P# c& n; l; b: z' u6 e
2 H/ ~4 Y. _2 g4 u- X9 V( yyum install libxml2 libxml2-devel- [- Q! M0 _4 D: q( c% o/ v# y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
0 t, `9 H! ]6 A/ N: S) Y; z! @
3 C f& z- _1 H" Z* nTengine version: Tengine/2.1.0 (nginx/1.6.2). ~; Z' ]4 ^% s: i- I) ^
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!* z+ n6 {( f3 T5 @% M! ~, [) g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' I1 G" n% b" i! q& ?( g: `+ p) J
+ w9 R) D! r0 L9 }5 x7 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) v& u& J( P6 k! Z7 T2 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. G) K. U! r- |: A9 |9 N8 s2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( A9 i L8 u0 J2 R" p+ r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' @% g; A; V, r$ [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6". p$ E( ^ z1 P0 [
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 C/ ^% f1 E( h& {5 b* T/ d解决方法,移除低版本的APR (1.3.9)
. V9 S) ?; b( B: z* V) k6 p0 s1 k) D* a P1 Q1 p
yum remove apr
2 N9 o& Y3 l& q7 i2 l0 v& d3 c5.Error.log中有: Audit log: Failed to lock global mutex
3 _0 t4 G6 ~, M" [: U
1 h, y: Y- Z: n9 Q. u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 I0 r! ^; R+ w9 c5 ~9 R: ?global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 W$ A: J" `- ^+ J
解决方法:
- e* A7 E! N( F' g0 x) ^编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 \& x1 z% l* a; S# Q) R0 s
. K# d9 l0 F; r( sSecAuditLogDirMode 07774 u. f4 }6 o2 K( W; E1 X5 `
SecAuditLogFileMode 0550
% i3 G: a# S9 h& [3 hSecAuditLogStorageDir /var/log/modsecurity: V7 j) K% [" Y- Y# d( s
SecAuditLogType Concurrent) q }, d! U8 U ~+ u3 X# `3 @3 q
参考文章:# Q& c/ r$ G( i8 ?3 F9 e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% y8 r2 y6 X% o' d+ R4 jhttp://drops.wooyun.org/tips/2614 |
|