|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 W" f) [$ F r7 L/ k+ A
4 e+ h4 g& o% L5 G _! u5 K
一.准备工作
" x6 R1 N D9 f& Y: d
, Z$ b8 E$ v6 k系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 q. V0 {2 R$ v
3 p( k6 N. }! A; r+ E
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) }5 d* Y" s* o0 a p
( n8 j; o+ c+ s6 U8 xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 i8 i( y. Y" q$ b4 L
9 M0 b9 I, ]. O$ {6 mOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 | O5 _ G' ~( {8 [, M, q: M6 w* Y5 T1 |9 V
依赖关系:3 e: `) O5 N9 u+ \
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 C) C9 O/ m. G' X' _* R
1 G0 x7 }# @& }, ]! _yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* G) {# Z- ^0 l* f" Kmodsecurty依赖的包:pcre httpd-devel libxml2 apr
% o0 h) @7 Y$ `7 ]5 m/ L( ^6 o% S3 f0 Y3 x7 G: z/ _" E
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) H" N" z# h! u
二.启用standalone模块并编译; }& a' F1 w' l# W6 _0 O
- r( ?' t# R/ a* r* p
下载modsecurity for nginx 解压,进入解压后目录执行:
- P4 ~+ r: r. h+ z* g
( [* b9 r/ u2 G: E: m# y( M- v2 r5 C./autogen.sh% }# j8 ~8 P' X0 G5 v
./configure --enable-standalone-module --disable-mlogc
$ L8 w6 q" w2 W) I) }/ l2 Qmake 6 w8 E% I' |; ]5 d
三.nginx添加modsecurity模块
, B# \8 ^/ A0 [" f& |& Q, h
+ o; ~) r( g m0 f/ j在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% Z+ A. |4 I; a: P7 D) q; D: E9 ] l
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 j6 ]1 v) ] }2 Q0 Fmake && make install' s1 a0 W$ u- |1 @% s6 H% V
四.添加规则- v5 r/ T5 S6 |8 L
|) k: d" O3 V& f
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( ?- G' u2 T0 k) p7 l, f7 R7 ?
5 a5 X7 b9 I; O& u. F1.下载OWASP规则:
9 v* p0 @/ n. U5 J2 f6 N" h G7 u2 G( @; H( }# Z. x
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. V, `/ a! m6 y& f( ]* n7 S# b3 h3 j1 z. T) e0 l& u) N4 v
mv owasp-modsecurity-crs /opt/tengine/conf/
3 g8 m( m0 t6 | s8 O/ N! c9 ^% j( B }, H, {
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: h9 ]1 \% Y. f9 [: E, v
2.启用OWASP规则:
/ y- G$ `3 c& N& D$ ^* B; [! C" W4 L9 J9 W. b9 z
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 Z# G4 V1 D: X- X; A7 R
]/ v! |) c/ l0 R& i) }- i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; t8 O$ ^6 g: H" K0 l& o" t
' j# c6 f& p7 J$ c2 i( \7 ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
/ V- e$ i, Q3 t! m& @( e' j& G2 x; y; Z7 U$ V3 i: H8 W
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 { U! \& ^. TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& b. ^4 Z' {7 Z O M1 \3 h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
: C% r& H: l& ]* C% d& ^" U' c& EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: _* u! k, v3 v& a) S. tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 ^! R0 ~: c* P3 Y4 b2 f/ fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. Q* `! O/ l5 q/ v VInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: A- |: d# v: ^" X- h9 Q7 P' n2 e
五.配置nginx' o; ?) J, c1 N
" ^: \( v/ j; n+ Z2 c% h
在需要启用modsecurity的主机的location下面加入下面两行即可:# O+ B8 c, g: ^9 [( f
( [- l3 I: J, Q7 x( PModSecurityEnabled on;
& ]% i/ p% Z0 {3 T! I4 nModSecurityConfig modsecurity.conf;
/ \1 l* ~8 @& t. G+ p下面是两个示例配置,php虚拟主机:! \& _" t4 P; P( r, }7 p- m
/ ^0 Q- y6 j. N6 m, U P3 p/ @server {3 K% ^& B7 H; i
listen 80;. }4 ~" y% q7 C' k) T& S5 {
server_name 52os.net www.52os.net;
' u9 P7 I9 A# Z( X x! m$ ^% y/ M ; g" x7 C4 L3 D6 l
location ~ \.php$ {. o- Z! V, ^/ i( ~ }
ModSecurityEnabled on;
* H. g4 M, ~$ n6 }# D% T ModSecurityConfig modsecurity.conf; X; ~9 f, s- m4 J
0 E& W7 g$ B# ?: C$ ~1 M root /web/wordpress;
3 J) d; n$ v3 b5 j; c index index.php index.html index.htm;/ T8 ]" o( C- Q1 F8 I: a1 C+ Z
7 E+ y* f( i4 |) x' ?+ C% _
fastcgi_pass 127.0.0.1:9000;
/ C! |1 Y9 @6 b1 { fastcgi_index index.php;
9 J% h8 J5 T( {7 ]/ | fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' A) b- l# P6 r: r$ X" o: e! \ include fastcgi_params;
" m, f# S0 x# a) V" T }
+ O d% k9 H. M }
% t8 c: h' O% M# r H$ V% K Iupstream负载均衡:
! }3 n3 u) t* i g1 r
! X; D" m& x( [% Gupstream 52os.net {
9 m; S$ Z5 B6 U1 R% w! Q server 192.168.1.100:8080;! d! k$ }5 c* B
server 192.168.1.101:8080 backup;
# S7 } y% i6 R5 X}
0 d2 U m! N* S* k$ Y3 \) w& T6 K3 C8 Y, r
server {% {9 ^* N1 h* q& ?# `: t
listen 80;
$ e- y. O: `! H1 sserver_name 52os.net www.52os.net;% O5 k6 V! B8 D7 T
; U' G2 G& d) r0 olocation / {1 [) x5 H- t! h P1 R. J0 d1 A
ModSecurityEnabled on;
8 j6 J* } X! q8 r9 `+ \- l ModSecurityConfig modsecurity.conf;
& f4 W, q" g& ]- U$ P8 }- C+ X; A7 \/ r; c, q" g
proxy_pass http://online;' Y# [# z* Q* ~; S
proxy_redirect off;# a4 \% a2 W/ T! H4 R
proxy_set_header Host $host;
+ T; G( q6 C1 n, } proxy_set_header X-Real-IP $remote_addr;# B4 n- l( e+ e8 F3 G# @
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ t% z/ }/ N6 p ^/ w: x# U- g }7 c0 D9 E! [- \$ N& n/ }, q" s
}: j+ x: c2 V, a0 s% C3 a, j! s
六.测试
- R6 B) _6 }- _. S$ v
" v- O4 R$ J8 U我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' H" e$ z2 b% t/ f6 z5 I- Y& _& D K/ x1 z _ v
<?php
, R! a" M- E9 q0 W) k phpinfo();
; i4 }$ {5 U6 S?>4 u, T2 \5 C3 D. {0 n+ k7 O
在浏览器中访问:9 @6 R% Q( V; h) L5 \1 X
2 h$ ^9 N0 v) q4 mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
7 }( S) P4 `) e+ b3 w4 g( Ehttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: F( D Y& L9 H K5 ]: U9 q& Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
, Y$ B* b$ X8 f* I! _说明sql注入和xss已经被过滤了
; \) ^* @1 ]& V Z% ?
, d0 Z1 Y4 L# L0 W6 [7 M, c七、安装过程中排错
5 l0 F( v5 e1 a; V* n5 L
% {7 h0 n2 L6 r1 E1.缺少APXS会报错/ J% [% _( X) ]# ^& n- Q- l
- u+ h3 |! }% Z
configure: looking for Apache module support via DSO through APXS
. d& K I; H; [; Oconfigure: error: couldn't find APXS5 J! E% a) j: Z! @" B$ t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 H) ^; R6 Q+ @/ ^解决方法:
6 c0 @4 }2 G+ _9 D- f/ F. g& X6 A1 s% z% b% q, L* j
yum install httpd-devel
) z" y2 d, S8 x8 ?5 x8 ~) i2.没有pcre
# g. p( f& V t+ F# }; ]: Z- L3 v
configure: *** pcre library not found.
/ M9 _( W0 O6 P' p" N/ Lconfigure: error: pcre library is required
# ~& r8 a' G3 i: Q ^! Z# P6 H. q8 D解决方法:5 V9 Y0 S7 h$ C6 b$ |/ |7 `
6 L* L& ]& R7 y+ w/ j4 e; O
yum install pcre pcre-devel
0 ?$ [6 G7 D: q; B0 m3.没有libxml2( N6 c/ X' { }
N+ X: {9 n4 N% T
$ z3 u: a, L8 O' J# w4 c( Lconfigure: *** xml library not found.8 J! p! A1 m- u
configure: error: libxml2 is required+ k _4 f- z3 D, d( U" {/ ]% b- y) J
解决方法:: V9 u- [/ V1 S1 l5 m2 V2 V
& r9 |! J- }. o$ S' Q
yum install libxml2 libxml2-devel
/ C/ s0 C0 O( y6 x1 b7 d8 V4.执行 /opt/tengine/sbin/nginx -m 时有警告
2 a ?) t' a- t2 B& ]5 q }" O! h( ^* \' @" M9 \: E
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 ?$ D4 V% e& w; k
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 F* Z# V- ?8 |原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 k1 `, M0 Y, w$ @, t( l; |
) ^3 t9 ^& u# D5 n" @. J: H8 ~- n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 g- L" [6 `. ?- R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
& h. v. |/ Q/ E3 R; t# V4 c( e2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 }* D9 G3 P; y9 r# |0 F2 P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 A! p1 ^: u+ p2 e$ \: l5 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- {$ ?8 i0 P# C2 C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. D+ U K9 i6 D4 \$ A' [( P7 ]1 H( F# z
解决方法,移除低版本的APR (1.3.9)
. r( _2 }1 ]" \7 O, ?4 ^7 Q5 T* J
yum remove apr; ?/ ?9 K% G! F) u; D, T' t+ p3 U9 {
5.Error.log中有: Audit log: Failed to lock global mutex
8 C8 Q6 L+ O1 c- ]) i1 n/ I6 D2 B, O( b7 \6 P6 y0 {& o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' g$ N7 p% J1 _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) H7 z2 j) ?$ {9 \ B' s
解决方法:' w% U4 m- Y' M J3 Y# L# Q6 P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ o7 [: v4 c. V5 U% C, ]+ t: ?
7 A2 k6 {& {4 ~8 @: M- sSecAuditLogDirMode 07776 y2 c8 U$ F8 y- W" Z5 L7 M
SecAuditLogFileMode 05509 u/ C0 ?4 C6 W9 R# D
SecAuditLogStorageDir /var/log/modsecurity
2 P z' {! b' z+ o- ySecAuditLogType Concurrent
# z2 A; w: r4 Q, _ i参考文章:! Y2 E4 V9 h% O
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* Z2 {- W# c# S2 Y3 M
http://drops.wooyun.org/tips/2614 |
|