|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" |+ s4 S8 ~$ D4 b) M( L( y7 M( Y
7 s# q I$ {/ b* }" m一.准备工作, Z; L' l/ K' U) `" [
8 ~7 W1 @/ E: `8 Z8 q0 V& e
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) A5 I* D8 U4 F `0 v E; i
e3 X1 W* J7 `: ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz F. P, x1 a6 R
$ W& g! N! G4 D4 P0 U8 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 ?- M; S5 \& f/ ?
y( {4 w" W2 Y: n' ]: m
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- }& |% m1 H2 f3 t
, Y5 d4 v- u8 R+ z# c P" [依赖关系:
- K ]( d5 w0 c. A. |6 Jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% j8 J7 d7 S) U. [( ~3 G
" C" N3 [: D8 F4 t9 Y/ Tyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 Z8 X+ V; w# p- I: k
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ m0 I. a/ R- B8 K1 X+ n2 V# x: M( _" L% Q6 \/ `
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel: z# [ ?- j; n4 @! ~& m! \
二.启用standalone模块并编译
; v7 d; ]0 r) q8 j5 n6 y3 o9 W% r/ E" d- t: ^8 |; N( m
下载modsecurity for nginx 解压,进入解压后目录执行:" E- n" b: Z) o- V
9 e: W/ X- |, N) j./autogen.sh
$ |, t; ^# n9 l./configure --enable-standalone-module --disable-mlogc% @5 q; a) Q' M+ c+ u
make
0 J$ F* x9 N, ]三.nginx添加modsecurity模块9 _1 u, f) Z8 Y9 H6 q" k4 k
6 \1 U; {1 y8 P6 D* f! l- P0 g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:% I! w* v" G; ]* a" R# o; Q2 N
( X1 C) X' J, J* G3 Z b& O./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, k; k3 m' } smake && make install7 e. \; X) P. H8 I. J1 q; M
四.添加规则
! B, e& l) p+ A6 n
! ?1 y7 o' m M3 M) Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" |5 ^) k/ I5 u, U6 ^4 R5 Z2 F& T) B' j! X' {! @! w
1.下载OWASP规则:
8 V( r1 h" \( d2 `& z
4 n' r" i7 D' o* E t/ H/ bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs. a" ]8 C1 Y) m" |2 r
( c' T, W% h+ {7 g* T; I* Y- @/ pmv owasp-modsecurity-crs /opt/tengine/conf/
1 N( q+ R" @2 i# z0 Q" {4 z
+ V5 t2 Q8 U& b& U8 a' w# fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 y- l" }" H- V/ H% Z5 U6 |
2.启用OWASP规则:
( j$ J7 @; z/ r$ f
) O) D: b* ?2 c0 D复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ @5 G. C# _4 X& ?4 X' k, T
8 t, m& H: h' Q \' S( s- W3 u& z. C编辑modsecurity.conf 文件,将SecRuleEngine设置为 on. T/ p/ }/ _0 f4 E; B% _
* l: E5 o9 Q7 y/ Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& `, b. j# ^" @' g
8 ?% J- J/ ^; X+ W( [# b
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: Y2 ]' V0 F( Z: u" h% }& QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ ^/ ^2 f+ m. J5 }2 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& Y; U9 b7 ?5 l* l0 Y% wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 n. w. j6 Z" a" H7 EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 l! c8 `3 R$ v) a( k$ }$ c4 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ N$ F( x2 V3 K$ AInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& Q# `% |) D g, d( T; C! f8 t
五.配置nginx
* _7 b* Y. V- B. g
" N2 z) Z" W6 u" S2 @在需要启用modsecurity的主机的location下面加入下面两行即可: P7 Q, d, t0 S* g
% p9 o+ V4 \5 z! T) `
ModSecurityEnabled on;
# ~# [1 Z0 Y4 s2 r4 Y" jModSecurityConfig modsecurity.conf;- F Z# O- Y9 J* z; b# ]. c
下面是两个示例配置,php虚拟主机:( E' e( R* K' l" [
; e( B( f$ @) ]" W" [. U0 o2 Xserver {
( f, N% S* u: D4 I+ B listen 80;
' G9 u: Z+ [* ?1 \* J4 j server_name 52os.net www.52os.net;
a8 P# V! n2 `% \& E9 B
, T& u" `- T2 O" F5 E location ~ \.php$ {& J7 Y5 U1 |( ?
ModSecurityEnabled on; ) F/ _8 ^* D% F7 |! v, Y
ModSecurityConfig modsecurity.conf;0 ^+ @/ Q% H) M. y
) v2 ~9 ]) o3 P8 l4 \) ]
root /web/wordpress;
. a- a. E" G7 R, p( b# P index index.php index.html index.htm;
0 c1 z7 ?% g! n5 S" r/ T4 Q 2 t( v& n$ v1 C4 a2 l; e( t( F9 w3 A, v
fastcgi_pass 127.0.0.1:9000;
/ Y5 N" M" J' `, u# N fastcgi_index index.php;( @" M3 d9 d1 `: ~2 S3 w7 W* j Y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
/ g U0 D4 G5 V8 g! g% y) C. k" ? include fastcgi_params;( J+ b* x& \! W! L$ j% Y. U6 t$ h9 s
}
% R- f! J7 P9 e: v% n7 b }
: ^0 A7 ~( f& W4 u3 a4 k$ N" i% Cupstream负载均衡:0 z% \; z+ o5 L& u
& t0 t7 i, D6 U2 tupstream 52os.net {
1 f% k+ G! B- I1 U8 } server 192.168.1.100:8080;
7 w& S' ~. a( m& e$ {# N8 J server 192.168.1.101:8080 backup;
, T0 L; V6 t# d( |6 e$ M}6 ]% O# R) p# F o4 l& y
( a% v+ D3 I& p1 O9 ^server {! y+ [% [% J0 _) {( z( a7 |9 E1 q
listen 80;: [' H/ K- q+ w) Y
server_name 52os.net www.52os.net;$ p$ {3 `& c% w) U& @% L
5 r. K9 X$ i Q# z& L) ]location / {& [/ D a) j, M: p
ModSecurityEnabled on;
+ X0 S8 p3 f9 r: ~. c ModSecurityConfig modsecurity.conf; 6 x W, m4 K! I7 p( D: K& m
/ s7 }8 u L& l! R& w) j$ V
proxy_pass http://online;6 v. g- T; y2 e& I
proxy_redirect off;
) y. B# x' T. j/ B: Y. F proxy_set_header Host $host;
/ H" E# ?* s E6 h$ Y6 k proxy_set_header X-Real-IP $remote_addr;4 t- \- P% l6 t
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ g% t2 K/ A+ ~
}
" J; a5 u" @! m4 X}- V. ^. E8 u$ R( s' n8 x
六.测试
' n D' Y0 a- _3 ?
( }; e: O( h1 k) S3 `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 j9 e, {. b* V m
& u# `0 O4 [$ }" ]) ~- H<?php
- g/ k# h( `8 p$ E! L8 r4 u phpinfo();
# |9 Z' X" |) x; `?># Y+ Y: K+ w1 v! f1 @3 N% J
在浏览器中访问:/ o- V2 j& Q& Q+ f
2 L0 }3 C N8 r0 Q* s. }
http://www.52os.net/phpinfo.php?id=1 正常显示。
# U# _) X7 ?* i! U xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: M. @$ V( D' O! C0 i6 ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
8 c- z1 L: b# }2 ]" k& o8 m5 ~& K! D说明sql注入和xss已经被过滤了
- K2 ^( P) V% g& W, @) c0 l) q& ^
$ A0 l' p; ~9 a七、安装过程中排错
0 b# D0 W0 N+ @( t; G0 w7 u# Z3 O4 Z1 `# ]) d9 H6 F; ^6 `
1.缺少APXS会报错* L8 a8 ^4 X$ o
% x( M( h( u8 A; l3 [7 a8 S5 @
configure: looking for Apache module support via DSO through APXS7 Z) Y$ C* {; }8 f2 B% d
configure: error: couldn't find APXS
0 c" b8 P1 N$ T; ~4 b Hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, P l: ~$ z, J0 r$ _解决方法:
5 e1 f% j: Z# S
5 u* C2 _* t, S3 V3 M5 fyum install httpd-devel8 @/ f% E+ r- O
2.没有pcre
$ G- I9 _, @% k. b. ?7 B4 T1 Q& B- j( H1 h" y6 S D2 w X, c8 ]
configure: *** pcre library not found.) B/ \" X( v& R; j& z
configure: error: pcre library is required
* _. g4 Z5 D5 y3 e. k解决方法:
" v) d' a# n- t6 Z* r% \8 f) f7 @) y. U H
yum install pcre pcre-devel
; K4 b0 j5 m; t6 A+ d0 X3.没有libxml2* P* W' U; @$ \
- `8 `3 H! M/ H
. }; I$ R7 W# ]" q3 R0 Rconfigure: *** xml library not found.: t+ K6 _6 r3 }) ]: X
configure: error: libxml2 is required, n7 S0 V* ^0 ?. g: I7 s0 o" Q; S
解决方法:
& F( ~ z R \- @% u$ {) v& R. R" C3 N8 y; l
yum install libxml2 libxml2-devel# `7 ]6 \3 q4 v: f) C$ q9 i1 }
4.执行 /opt/tengine/sbin/nginx -m 时有警告, t5 |& c9 J1 w4 [' r7 R
1 a" Y( R, X% J8 yTengine version: Tengine/2.1.0 (nginx/1.6.2)
+ F- I% ]/ Q3 V7 L; h6 L$ @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!% j& J6 J. W; P7 n9 x+ ]$ A4 K
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, }1 Q* h- E: r3 y
" z; p& A: P [$ k3 t+ Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 h! F9 @' B# d5 k' b5 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ S; \- R' i& h# D: b3 Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 j: }; K1 P3 i9 ^. T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 W. F( K, ?, Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% l4 R' T8 T( X: R1 e# L; L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% Z$ H: y9 `4 G; E6 r解决方法,移除低版本的APR (1.3.9)2 n1 P9 f4 J5 ~/ o. ?
. t. {9 T9 v6 ^1 X+ G2 Tyum remove apr$ t5 R$ n. V4 s9 n9 s
5.Error.log中有: Audit log: Failed to lock global mutex2 r5 a- ]: ]/ I, M
( m. E+ ^2 Q, D" M4 o2 a( N1 I$ `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock . i+ d1 o" a% p8 w, h% X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ E" l& v) M4 o8 y5 y7 b5 p解决方法:. l8 k; {3 p: S) P. u9 e
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 k5 i: H5 r$ X4 d
2 F# H! `' w- m$ ?1 |" U/ C+ x
SecAuditLogDirMode 07773 f7 O8 L* N$ K
SecAuditLogFileMode 0550
4 I1 [# a5 I, M8 T3 oSecAuditLogStorageDir /var/log/modsecurity
- V+ o1 q# U2 U$ P% U1 E9 _4 O1 ^# iSecAuditLogType Concurrent" P9 h7 ^ ~) u* Q4 `. C" t4 D
参考文章:( A- M& V( T" ^6 L3 \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, z w+ a9 q; H1 L! a
http://drops.wooyun.org/tips/2614 |
|