|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; q; f& w# ]0 O
' N0 B3 h, ?2 D _: y# k G一.准备工作
# w- U9 H# [: x8 _6 o
. C/ K w5 ^' t/ s& s) d' \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( t/ l% ?& A6 \" C3 P7 H& D( z
7 P" q4 f0 c( _7 |8 i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 s# Z! J( Q/ F! j; \' s m( \4 }
) S( @% ^- `+ C4 w7 P8 rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 J/ y1 u" o' C3 @, t4 ~* T* h* C- ]3 J+ K9 L
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ y' q& g2 }7 i6 \% U
' O0 c0 Y* J7 Z5 ]0 ^0 o% E
依赖关系:: ]5 P. [5 j. j9 h
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: Q# I! `% _" b, r- H
6 q' _3 b- M/ B. t% Y
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
9 w6 k- Q& k$ a0 S% H7 i, Wmodsecurty依赖的包:pcre httpd-devel libxml2 apr) E! U1 [; _. a- D4 C' ]
: R1 O' E+ G( k9 A9 g7 W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel: d% S' a2 F' z5 |2 R
二.启用standalone模块并编译( r* ]* m; M. g9 i" C+ K; V
. x! G7 m4 S; ^, Y; m/ G b9 n' P2 J
下载modsecurity for nginx 解压,进入解压后目录执行:
: Y; G8 [3 B' C6 k5 m. y; p2 G
4 @9 P: Y- d2 d; g8 J% K1 q% I./autogen.sh- t B6 u# w9 s- Z2 h" X0 f
./configure --enable-standalone-module --disable-mlogc) \5 f3 ?3 [2 o) x* K1 v; y
make 1 f* X) f6 \; m- W; K
三.nginx添加modsecurity模块
) o1 c7 C3 L# Z
6 a1 e# f, W. C W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! b0 h$ e5 g/ j
6 l& l5 V5 A: g. U4 r./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, k2 V' Y& t+ d( e
make && make install5 W P' m4 Q P% R5 w
四.添加规则
2 _- R# [- f( c( M4 x& z$ I5 ?/ I1 D; }: D4 Q5 c5 V& Q( @9 s# r
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 i7 z K6 b h! B2 B$ {( b
9 D+ @: H2 I7 j7 G9 d2 R
1.下载OWASP规则:
8 r A; g2 I8 R) u$ U8 _* |
0 z& w7 ]4 s2 Y Q# Sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' t( M4 o" b( S2 y5 ?6 C) ]1 J
; }% D0 M/ M" q% q: y- u1 W; ?mv owasp-modsecurity-crs /opt/tengine/conf/ A- s1 w2 P/ J7 G4 u X
: l d* k0 X/ c \ }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
5 }4 G4 U' s% ~3 x R2.启用OWASP规则:
3 ~3 L# y6 n+ P4 G* K# V
7 `( _- O6 U4 W: H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( n1 o- x" p$ u! t
* _$ ^- i3 s6 d2 V
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 D* \3 U+ v0 D: V( q$ I$ b) K5 T4 I! C) W( `$ `' f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. v! n& B% m7 V* R' g s4 j
E; A. @% q" z- w% c4 xInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 P5 A0 I0 h) T/ ?/ D) T& G8 y5 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ K+ d- Q* q0 ]1 f; `; D$ |# V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 f0 q1 D6 ]1 i* @! [$ @' J1 F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" }; E# L1 R s6 v) @& X ?2 GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. v9 S& l! K# I& V, X* ]$ ^# t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 x' Y* g3 y+ Q2 @+ l1 z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: y& H8 l: O/ @% b. x" x
五.配置nginx
# ~7 v5 }+ X1 @+ `. n" t0 x" }: W& `' E) s& E0 V3 J! D( p) z
在需要启用modsecurity的主机的location下面加入下面两行即可:& {- i2 n+ ]6 I8 a o2 ] n# R
4 r+ q% l/ f. P' _/ m. ^# \
ModSecurityEnabled on;
0 p$ d. D6 k* S; T IModSecurityConfig modsecurity.conf;# G! y3 y, \/ g# B" H) F9 r, p4 u, h. J
下面是两个示例配置,php虚拟主机:& s F/ {8 k' |" c0 U
3 K1 P2 Z: Q+ i2 q; J
server {
6 `) \/ _. p: H7 } listen 80;# G) |5 z, ~, H* H
server_name 52os.net www.52os.net;) m$ C/ b$ C0 J8 H, [- J6 s, S& s
6 V9 m# c$ t9 }7 z location ~ \.php$ {! W- m! p6 Y+ Z7 L6 O( ^4 d1 K
ModSecurityEnabled on; 9 W) M. L. A2 l E/ i9 h$ ?
ModSecurityConfig modsecurity.conf;9 {! Q: @. m5 l# Y3 ~
! y/ b# t9 K, o1 X. b. F$ _
root /web/wordpress;' p7 u, u5 `. z4 G8 Z
index index.php index.html index.htm;
! u; c% g! ^8 I7 O: Y: }" V
) C% B9 x: ~. t" B fastcgi_pass 127.0.0.1:9000;8 h2 o) w, t% r) H; x
fastcgi_index index.php;
+ L: ^% W* p) s9 M( K. R fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. [; h6 o. b9 ^5 N' N+ l include fastcgi_params;- M( v4 r' {0 e1 L: r
}
5 _4 u: B, C' M; [8 i, V. }- l }
0 `$ C& T8 z7 w/ m7 @upstream负载均衡:6 ]8 K" v$ s4 M b, b5 f) t
# E9 Y! D6 E6 X _$ l% K2 Q
upstream 52os.net { }8 e2 b) V. F
server 192.168.1.100:8080;3 c; T; i, h$ p$ r6 K
server 192.168.1.101:8080 backup;
) _3 s [- x) Q. j" e3 a! l) a}
3 E1 O8 p4 C1 y
& S" I% ~6 p# [8 U% {0 F9 qserver {8 |+ O F7 i( Z3 S3 r: C; S n4 B7 d
listen 80;1 E( S7 J+ N5 K# e: e6 D
server_name 52os.net www.52os.net;
$ f. c4 @9 ]2 K
- y# N* k- Z* n; Wlocation / {0 w0 j# E) u0 }
ModSecurityEnabled on; & S& t7 H- G. o! n- i1 q- m7 ]' `
ModSecurityConfig modsecurity.conf; 2 V+ Y3 q+ {& ]4 t5 }" }
4 f9 d4 h2 [$ P8 ]5 ~6 N- z
proxy_pass http://online;! o" U- k, J7 e; D
proxy_redirect off;
, S- K1 n# Y0 e# d7 @3 s proxy_set_header Host $host;; k1 w" |4 X9 l+ N4 N+ o2 Z
proxy_set_header X-Real-IP $remote_addr;
2 P3 b$ r( { O2 N proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# u4 u. y6 p1 q, d; H$ o: s }
$ x) Q. W$ p/ \/ j}3 z& v. k$ ~/ v/ s8 w+ S: w
六.测试3 W( {& R# q: X o" j1 E" J
- ^. E, k$ O# L8 s我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, N" ?" v2 R6 m8 Z; u# m9 D( U% Q4 ^
<?php- w( s+ Q! }) b {- [8 ]9 z
phpinfo();
% A$ h1 C% N$ d3 T+ l0 } G G/ U: D?>
- x. W; \1 \9 q W7 P. Y在浏览器中访问:
# J' ~, ]! J6 d& u2 Z( p0 I; \' _( \; j9 V: A: m
http://www.52os.net/phpinfo.php?id=1 正常显示。; N5 N/ B' w- Y( Y9 l" G0 b2 w" y
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; y. d ]9 }4 n. ~; [; B' |% H% ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. e# J/ q; ?7 ^+ c. \+ O9 n
说明sql注入和xss已经被过滤了
' h6 m) P" j: P5 Q
+ s( A' q9 V& R9 \- A) m( W七、安装过程中排错
: i& V* r( Q& O0 ~9 J, R1 C" f6 M i; Z8 g7 m# t5 g& A, k
1.缺少APXS会报错5 |: D( }5 U9 s) \0 A
/ V' t8 _" } S- Hconfigure: looking for Apache module support via DSO through APXS
) R9 m/ a! ?5 H- bconfigure: error: couldn't find APXS
6 g( C# c, n- h5 Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( y2 h: `6 s' N
解决方法:$ k3 w* c% }, A8 Y+ p
9 _; D! |! S( P& f
yum install httpd-devel
. V& d I" c; k0 @% P2.没有pcre
+ m& r' F1 U! e! g4 K
* D$ x5 ~3 P0 z& d1 Lconfigure: *** pcre library not found.! g* K2 ?4 Z5 E r, c, o& B6 Z
configure: error: pcre library is required+ [( g* y) T3 b0 S* Q! }# x) v
解决方法:
, e6 i/ s; _# M! M9 [3 O
. E; i8 K7 m- Y" F: nyum install pcre pcre-devel# N/ N3 N+ }3 @( ], o) L( B4 j
3.没有libxml2
$ j9 a5 j9 v2 C: k8 B$ L8 i, K8 v# i) {8 G. a5 D1 \8 s
$ Z+ f! A, s* v& Mconfigure: *** xml library not found.
. [# d% p8 D. z, w+ rconfigure: error: libxml2 is required( P2 K! p! x6 t2 z, U0 R
解决方法:
, q6 |( f( M% h6 |- m h9 o. K8 v) V3 c5 M5 T$ i* X; I
yum install libxml2 libxml2-devel% }1 J$ K9 b4 Q; U h0 X
4.执行 /opt/tengine/sbin/nginx -m 时有警告7 n$ n1 p& S/ j+ j1 Z8 N* W
; Q# `3 Z& A: d+ n
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
" w7 {" w. Y9 v% onginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ ^) V6 H# B, u& B0 @) p6 Z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& ^# P3 H* O+ I: h0 u8 I g
+ F- z& x0 R# q4 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- p1 B7 l9 Q& G( @3 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9", N) X7 Z- f5 b2 S2 d- H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ T+ m* Y& p/ t# Z' x' a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% [6 F* b# E" X" T; c) E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ E. A" o0 P$ Q r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- `1 `8 f& U1 ~8 |4 [
解决方法,移除低版本的APR (1.3.9), P4 J: p! K: j+ ]: f0 [# a% Y
+ n4 ?8 D1 K$ A8 q" j% ?yum remove apr
6 v" z8 R+ n0 I1 x, ]8 B5.Error.log中有: Audit log: Failed to lock global mutex
2 v" v Z4 G1 y; `3 u8 K6 }; c( q1 |/ O: m0 h8 M0 i: c
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
$ v/ Q- W2 _2 N& ^global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 A% m0 k l) [ E- @7 x7 ~( A解决方法:
9 }8 [. X# X# n' H编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# a* o# g# e5 p
$ ?9 [9 M+ R8 r
SecAuditLogDirMode 0777
7 V- d3 O# p, H& g- k1 OSecAuditLogFileMode 0550
( C7 e/ n. n h3 l( m8 ?SecAuditLogStorageDir /var/log/modsecurity, |2 C5 V. v, N, M
SecAuditLogType Concurrent+ L" E5 O" l) Y2 g2 J
参考文章:
! y/ j$ g! M3 e _; E5 H, Hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" A8 ]" U. g/ b2 a1 Q, D/ qhttp://drops.wooyun.org/tips/2614 |
|