|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。0 x& c4 w& [5 y
1 d7 q; D# w6 r1 |% S$ @! [
一.准备工作
; ]) T# G4 ^+ h4 Q( b. R3 s' Y' I/ B1 M V/ B- j, K
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 l1 T& r- C) _6 p. k; m
( ?9 g, Y8 c& u" T% A2 L/ v4 ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz6 v9 e9 P( l* B# k; {0 D
4 F/ ?3 d2 J# x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 K8 ~" h( U. G3 L6 A1 d
) G+ I" L p. V/ dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# W, `* a+ @: b; ?4 u" E7 _6 X$ l8 r
依赖关系:
- W. t! c! O6 {, x2 P* D6 x7 Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. D Y) u s) c! a5 o
( D8 Z/ Z6 [: ~2 O8 L
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" X+ G* P$ w+ i+ D' C2 h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 s& y& @: v' Y; R) ]" F8 P
; C7 q7 H7 Z6 zyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) ~$ ~2 P+ q" G5 }2 s
二.启用standalone模块并编译
4 E. b7 N, l2 O3 u5 \+ }/ ^
( ~3 [) `) v1 A, x' ]( ^" B下载modsecurity for nginx 解压,进入解压后目录执行:, Z& Y" H; c$ r7 r2 B. C8 }
g, x; P1 w/ o1 f3 v/ C* u./autogen.sh# B+ k/ Y2 i) A0 ^
./configure --enable-standalone-module --disable-mlogc" t5 w$ c& M- ~( |1 N( v) E2 }
make 5 I1 ]3 U. v; o+ r# V1 ], F1 j
三.nginx添加modsecurity模块
6 n, L, q2 F1 ]- x! @4 a' g
+ o4 i, s5 |& |; N3 N5 L$ s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& G& I+ S. t9 d1 D, s6 o
) U. e, S, q; j/ o# C% c/ u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- c+ ]! ~5 t y5 D! L. b4 U' Omake && make install) Y3 S- c% A- f* ~: D$ _
四.添加规则
, q# u3 z8 t! {6 f/ `. p! ?0 V5 V( ~+ D7 O& \" I4 j4 D3 d/ t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ f! O( P" y8 q3 D, i/ R/ I f
4 c& k1 }" n N2 E! Q# X! f% ^
1.下载OWASP规则:5 Q; }8 h: a( M6 O1 q5 r/ ~0 K& ^
6 K O: z- b9 f/ l. p8 W1 g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 p1 {6 O- a7 P4 B
( H" s0 J$ m$ L& Bmv owasp-modsecurity-crs /opt/tengine/conf/
+ c# e. C% q4 N7 E" c" G9 L- e2 g; J) ]
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 A) q4 Q$ D3 b8 I( c; m4 X6 w" n2.启用OWASP规则:7 X9 l D" P" t" ^5 o
# y6 z% j) P% @; g2 i0 E复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, ~- o1 l9 q, u+ p* P9 L) W5 A) |( |. S0 ^( b! `/ b6 g6 A' k/ j
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) d: g* R4 d# _5 H% d$ @' @$ u0 _# P
$ T# v3 M1 N2 m) Y2 P+ U1 _( q. Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" t3 a* p& w* T- p r/ ?5 @
: O5 J L( m' b1 H! L; }& bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ R, x- _1 j: {2 w8 Q) }3 V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 t9 }5 k' M2 C1 K' M5 D" W3 ?% v. nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
S8 z& m2 d2 ~+ ~! LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 E1 }* I; b! B9 N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 t& @, n0 p0 Z) n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 `4 y! E" W9 X; X1 [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 z% j5 S; x6 a2 e3 O& q五.配置nginx5 m1 F$ Z9 E" ^- Z: K, o5 m2 v4 r
4 B& u* y* ? O/ g! T* u5 J8 n, O
在需要启用modsecurity的主机的location下面加入下面两行即可:
$ _, F% W+ G' J' D
& [. Z3 M# T* }0 b+ Y1 v: XModSecurityEnabled on;
) s) `1 j$ z- L2 t5 O4 FModSecurityConfig modsecurity.conf;+ s% f! Z/ P7 u5 [
下面是两个示例配置,php虚拟主机:6 |1 b, p. x& Q( ? I: S3 L
7 l: M3 f* C! E& y9 Zserver {
" ^, Y" w2 ~0 K% ^/ @9 @; u listen 80;
8 J: V5 b' Z3 ]% s/ \" m server_name 52os.net www.52os.net;
" v4 V( R9 n' z: @ / H- ]1 o+ d+ P& m" p4 |, \% ^- \
location ~ \.php$ {
* S3 [% s1 v# f4 G# j3 T. y ModSecurityEnabled on;
2 {' r9 y0 _3 I b- Z4 l$ X; } ModSecurityConfig modsecurity.conf;
9 E" I+ c; K, j: r C( Q1 X( |) C9 ]
root /web/wordpress;
9 x# J7 M+ v, p7 L index index.php index.html index.htm;
( q7 D: O$ K' p% Z5 [" ~, Y4 a
: I' n+ e8 y2 z; B' | fastcgi_pass 127.0.0.1:9000;9 x; {; A2 U4 [0 `- z3 g/ N
fastcgi_index index.php;. ]+ W' R6 e1 F/ | t+ Z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& Y) Z. A3 p) e include fastcgi_params;, U: l' V0 y$ e& u+ t( D1 f" q
}' f; w$ [: \+ I$ d) y
}- q6 E/ s a* v
upstream负载均衡:
7 d2 u' \5 [7 f
* g8 c( ~# c0 N$ v0 N( _upstream 52os.net {
5 \8 p! `" m6 P/ f9 ]0 g- @ server 192.168.1.100:8080;9 l7 @$ ?7 k! y+ F% b5 y
server 192.168.1.101:8080 backup;! Y: j+ o, m' S, v3 f+ ]
}# G9 v# V, m/ o) [9 r$ D+ A
' V+ Y5 s- [! |1 J$ Nserver {. R3 g1 M0 f. ^7 w6 q+ T( U- O. c
listen 80;
6 v. y) }) u& o" Z6 Y7 ?. X3 l7 Jserver_name 52os.net www.52os.net;& u) h% `$ w# Q" v9 h
- h: B/ Z% @5 i) T, n" elocation / {0 R# h: N' ?$ r; K, p1 a V8 D
ModSecurityEnabled on; . s/ d) X. w2 @2 Z# ?1 Y' q8 {. D
ModSecurityConfig modsecurity.conf;
- {+ |$ \7 Q X6 ^' Z2 m7 a- k
$ C: X; j& f- k& ^ proxy_pass http://online;9 q$ j) ]( d5 l* o @7 M
proxy_redirect off;
9 w" t9 b, C) U$ A proxy_set_header Host $host;
& O. V, |2 |+ J8 t0 a# V" [ proxy_set_header X-Real-IP $remote_addr;
" j/ J( \' I; ^5 ` ^" b. t proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;& X1 w ^ u& R, I( \
}( \' ~+ m" i: S
}* E! E9 P. ^4 B7 @, p
六.测试3 C% L+ z+ t. t3 \8 l" {- S
3 Z" F+ N* H8 e8 {
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& z: t& ^' M3 y! \& L
6 `: F* e. @5 C: A# q
<?php- A( m k, m& i1 l# q0 I/ L( N6 e( @
phpinfo(); % C; E4 E/ { t, Z6 k
?>
6 _' o E6 x- ]# E在浏览器中访问:
; C& x" k* ~3 |& H( W, X
% } Z) r }1 P1 c% mhttp://www.52os.net/phpinfo.php?id=1 正常显示。' e4 X: X: A$ m- Y$ V
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) N- y* i- E+ r8 W& i+ G. b; P5 Chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。/ P+ n. f* K2 D/ Y& K' O9 X% l4 V t: Z
说明sql注入和xss已经被过滤了
1 u: d, x) @3 i( j" u* t! p) d" J4 v+ \, @- q; h
七、安装过程中排错
& T" O, m* m9 ]/ u. o) x L, ~( K0 i+ ]# P: `
1.缺少APXS会报错0 r- P! ]7 Y; p0 s" W
3 g. d: H8 ^ n1 {3 l7 D
configure: looking for Apache module support via DSO through APXS
5 S- {6 M+ ~9 U1 tconfigure: error: couldn't find APXS
! V; ]/ N& `9 D5 japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, p: ?' s* @+ F8 g" g0 L
解决方法:; M, v }7 c5 g% B" t
8 w' t$ @8 `' A7 syum install httpd-devel
. p; \, u7 k0 u1 k2 [# w, A# n9 {2.没有pcre: k" C0 p: C# ^8 g" V1 a! c
& d& q! _' g8 F, f0 sconfigure: *** pcre library not found.
$ i" G$ D R( Q6 l& n' } Dconfigure: error: pcre library is required( I+ V8 s# h4 Z+ N* Y" A/ N
解决方法:
^2 E9 n d4 K6 W8 s7 y0 X$ H6 h% j% u1 L9 g# n
yum install pcre pcre-devel1 a* w1 o( q" I- C: u- Q. T
3.没有libxml2
* [3 f( W% d/ j" ^$ X/ U9 m2 z2 `9 w% i- {( {. i2 i- n
* m7 k+ g/ C& o& k1 ?& b% Vconfigure: *** xml library not found.9 f/ K8 ?7 |! g' }7 @# z
configure: error: libxml2 is required
, b% B3 J5 ^6 P6 e解决方法:0 c# m7 i( }9 l7 [6 N- Y
) n4 K* X! v5 H1 Z% Xyum install libxml2 libxml2-devel
, i3 @; Z- J4 G& l4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ w+ T8 L0 g5 }) \# d/ n/ o) F
5 p$ W" b: L8 [3 z7 y. RTengine version: Tengine/2.1.0 (nginx/1.6.2)
' U: t- l1 E5 e6 y' [( b) h2 fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 r- F* A+ \' z9 ~. x3 c0 n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
H. q* m: e7 x! ]' w) e$ V6 \7 X. s& Q8 j# ^. k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 H+ a; F' l, Q7 {& `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 m4 o4 u% A* g& n% l2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( L/ d1 H/ B3 p! ] f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 k2 S2 [% u4 W: M) X: {6 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# B8 |' V# @( g3 I8 s; d7 S6 F1 e# M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 Y% W. H* C6 M. m0 X
解决方法,移除低版本的APR (1.3.9)
% S' {) `' d( p) b, P+ k' g0 S
7 _" d! d; s( ]yum remove apr
, ]/ w+ N% W4 I" H" X+ V5.Error.log中有: Audit log: Failed to lock global mutex- t0 f, X, |0 O6 c6 X, O
3 L, t U3 n2 L0 ^3 \( Z' y; v+ A
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock : y* o- l! u& Z0 s- x7 F. B
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! B: c A" c; r# ^ j4 K# Z7 E解决方法:
' s8 \5 j2 ~5 z' v. U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
x' t Y8 L& Y, t' F
- s) S6 J* R) ~8 J6 P" jSecAuditLogDirMode 0777
3 v$ M! [3 F0 ?" u9 z- e, uSecAuditLogFileMode 05501 w& i) I% k* _- O4 F7 n: \
SecAuditLogStorageDir /var/log/modsecurity
1 @6 @5 \. S/ V$ w5 t, XSecAuditLogType Concurrent
6 W1 B: e8 z3 b参考文章:: G. F; W+ ^& h$ f1 r1 D p
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 Y% K' a' u& E
http://drops.wooyun.org/tips/2614 |
|