|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) R' p/ j* a- z! k1 h
& }$ s: R% x2 P* R6 L一.准备工作
7 \4 F3 U) c# `" R1 ^4 V6 [
, F- K ]8 o" f4 D" n- L系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: J, f" E& d( U. q* D
8 m% r$ l: h+ n/ d
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# z- O1 B# J* j, I' z1 E
2 p8 \1 Y! [2 R) R. Q8 V
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ O) ?% F4 N: O( N. I8 O7 L& j8 D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# } h" C+ N2 r
( \7 A3 G/ U& d& _依赖关系:
* e7 ]! s x2 [# F2 `$ Ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( R F8 b1 z; h- I7 @1 I0 x) V
& ]: S5 q ` V' F9 x) n% v1 wyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ ?: m8 l- _( m' A* L$ w0 t: {modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 n/ j7 x5 h) _5 ^
. y& L( [- d# C3 L+ u: ayum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* Q3 K# @' M2 c; U
二.启用standalone模块并编译
; ] E' T* x: ^. d, B9 R/ u$ E. }$ w& C% P6 ?
下载modsecurity for nginx 解压,进入解压后目录执行:
; j1 v% P. I. `. }9 b4 O$ E2 s, r4 @& W; Y
./autogen.sh! ^) @9 A/ A( v2 r
./configure --enable-standalone-module --disable-mlogc
# d$ L2 Z! u% I& k" d) E5 nmake
' w) M6 _/ j1 K6 v+ l# {& Q三.nginx添加modsecurity模块' j' [0 E. T1 Z. K' z
& o- l. U2 `) V, T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ h4 ?, N' A& D( C E+ ], p1 i. z# D, S
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
5 n- V& O& q* z3 F; Omake && make install
' a, w) N$ h# F& D6 }四.添加规则7 g, h: p' ?( E5 k5 K
- J% k* V! a, y& ]0 ]7 Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ D1 K9 w% r. b& J0 G
" z( J+ f2 T7 ? U- w) c% Q1.下载OWASP规则:/ p6 S5 ^- S, d O! a. \) O4 _: G! V
% V% [, o0 k7 ^/ ~2 e% e# ~ g9 ?) X2 Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs# C; A) P5 q- Z4 H) B3 W p
* X& c! g9 \/ }, p' wmv owasp-modsecurity-crs /opt/tengine/conf/2 E6 n, J% ^# h7 w. X7 L
+ h% F+ E; k3 Q4 a; T) L. wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 D0 R; E. Z, [" @# n
2.启用OWASP规则:0 ]& V, _: g7 p/ f
( h" {3 X4 a# @1 i" L- i% \2 D复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ I" l! P4 i5 T' D; h6 L1 M9 B9 w* p" N) D; b, B. A( y% v2 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, ~( Z Q, p) r
8 f, f& F4 \' F7 G2 q% p6 k
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 g9 S5 @6 ~& s
7 N5 @5 \" L& N9 ]2 Z, W* }2 ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& \7 {" D+ y7 ~: P# s: }( U; D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
5 l1 O# }; u2 l/ x0 E; A8 c0 y# b9 yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 f8 Q d4 L2 K, m- j" uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; [7 U0 Q5 P' { Z) c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ Z1 x( \) I8 o# a" N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 \$ ?. Q! y/ }! W% J SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 F( M' ^; z# Q; z) x五.配置nginx
$ m% E) F$ r- I7 n( e0 V* B' U& T$ X& h
在需要启用modsecurity的主机的location下面加入下面两行即可:
; w+ n5 f2 U! H' [0 Z7 {. C( v3 p$ q8 t" G( @ H/ T
ModSecurityEnabled on;
3 |5 M: h; h2 U! s1 wModSecurityConfig modsecurity.conf;. p* `6 K1 \1 G$ z4 b
下面是两个示例配置,php虚拟主机:
0 h' {4 I( v6 h- ^! [: v- z3 }4 _
server {
: B4 P( t+ v6 L, M( w7 [ listen 80;
, j6 f0 Q* G7 U* P7 g2 {" e- _6 o F server_name 52os.net www.52os.net;
5 B" o. A9 L+ b
: b1 B" D- Y. r5 o: i8 s# L* c location ~ \.php$ {* b4 O" h! E' j0 o) {! Q; @# K1 b
ModSecurityEnabled on; $ N3 h+ q3 d2 f U' M1 [
ModSecurityConfig modsecurity.conf;
! _! }' C4 u$ D3 I' C7 [) a9 [* \4 c& A1 [; i7 i
root /web/wordpress;
$ V1 D+ G- f. c; Q' u1 ]$ D( j# {3 v index index.php index.html index.htm;/ q0 u6 m" h& Z a1 `
$ R+ ]( Z4 H9 x* C: Z
fastcgi_pass 127.0.0.1:9000;) |; k" k2 O- f( _9 ?0 S4 I
fastcgi_index index.php;
3 [, [5 ] T! u fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 @5 w3 m6 I/ p% m. ] include fastcgi_params;! X; N0 D. G( e \" v
}: M9 O1 S# q0 I2 T" q& j( F
}
5 g0 l0 b. g6 U8 Fupstream负载均衡:6 f5 e7 Z! c( E: H: s- b
/ K: a8 A V5 ^- R7 I Zupstream 52os.net {
4 T/ i/ b; P o3 c2 y server 192.168.1.100:8080;
1 Z/ z& B' u* ]/ l3 q; f0 o: K server 192.168.1.101:8080 backup;
; O# N) @- `. ]7 q9 H8 |6 n}5 W5 `7 U& K4 m; d# b8 t
) e8 H) g4 O1 c0 l
server {
0 x9 D& ]# h, w# @# A! Dlisten 80;& d: v9 F4 v! S* u$ D, [' G
server_name 52os.net www.52os.net;
4 o! |6 o0 q: o$ P* o# ?8 q1 N0 j' k' r
location / {* A, `6 o6 ]0 {& Z
ModSecurityEnabled on; & q0 a1 {* ]9 h3 {
ModSecurityConfig modsecurity.conf; - N- J E/ s7 Y6 K
" n0 w9 R- T% V) j" U& x: A( i/ c/ L
proxy_pass http://online;& ]: z2 }1 f P X& d* N
proxy_redirect off;
' m( @9 n8 R' h proxy_set_header Host $host;: F& G! l; g8 }
proxy_set_header X-Real-IP $remote_addr;
8 P% @) G; n, [, H& p0 K k proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. D# P( E& ] c" e& `( K }& c; I _' S7 N. [( R: m1 b, k1 F5 c
}
2 P$ R8 { ~1 O/ v( \六.测试/ G; A Q( H: f j
# L9 d6 E3 I8 y2 V# G4 f5 R) ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 V9 l" K0 U% ]+ F" \" l; U) D
8 o/ T6 R0 P# ~) w+ [' Q5 G
<?php
8 Y, U! x8 h# }' w3 ? phpinfo(); 4 \, r. g6 N7 ?8 ^4 Z. f2 x; n
?>; A7 E; v3 Q4 @6 p! i
在浏览器中访问:
+ y9 T/ R1 t! s
4 e$ k- L% ~0 [http://www.52os.net/phpinfo.php?id=1 正常显示。
$ _8 j2 t+ ~+ p* o! _http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 i# y( S6 B5 ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。: V2 |2 L) |4 L& `& I
说明sql注入和xss已经被过滤了
1 L% E! D4 R6 G7 l6 |$ ?; F F; E6 S+ Q n2 h W
七、安装过程中排错
6 x5 a. A. c5 v: h# i# ~4 w: |3 ]
! d. Q6 n' ]) Z% c# k- `1.缺少APXS会报错
6 v" j% o9 m6 _7 ~4 N% q$ X+ k" u- x0 [1 P* |
configure: looking for Apache module support via DSO through APXS" W1 X# f* r: P5 ~/ n
configure: error: couldn't find APXS; H' S) L4 a0 ]4 z C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, d" A- }, _' W3 [
解决方法:$ k' ?0 w, g3 k" M
8 E7 u# @0 U( v0 w2 Byum install httpd-devel0 |+ d' r- e' R, v) ?9 c
2.没有pcre1 l" a; m7 i8 K9 V7 x
4 a1 Q$ `" b% H5 iconfigure: *** pcre library not found.
" p4 x( y% e: X% }5 \configure: error: pcre library is required
$ k6 Q7 v- P i% g J6 W解决方法:
# H% W# f+ v/ J" Z/ I- C
$ U) \2 s( Q& P5 o9 \" Pyum install pcre pcre-devel
1 q, N- H' |0 ~- A3.没有libxml2
2 X1 s3 X" m; P, L& J; u
6 o1 [) u' A5 B7 a: I* t6 B W x+ a
# S, V3 Z' l; l7 Z! econfigure: *** xml library not found.
0 E- d' {& Z# ?# V( p ?configure: error: libxml2 is required
% C" }2 ?# Y% @/ \/ _* S' E4 C- @9 c解决方法:; ?$ y$ {- c, D6 Y- Q
+ w) H; i7 P- U! }yum install libxml2 libxml2-devel" f* P0 }1 Y2 p4 Y- M
4.执行 /opt/tengine/sbin/nginx -m 时有警告: h9 s6 M/ O( [0 X4 V) m
( [ g7 R, [/ v: zTengine version: Tengine/2.1.0 (nginx/1.6.2)
+ h" ?8 u) O" F# d. G% X. fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% r% q9 W1 @9 R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 ~; F' a7 ~5 W- W, D, p/ @& c4 P
/ G# ^: q" L$ G. @0 m+ i1 E, t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.* v6 S' ^5 J. `( H/ e) g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 |) O) j, D1 X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ o% S/ L& ^" \ K, h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"7 P* f. E2 z8 b% r; T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 h8 w* f. Z2 o" s9 ~1 T8 z) G
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: ~. }7 |9 h! J2 Q
解决方法,移除低版本的APR (1.3.9)1 M+ Y7 ^9 @( d* r6 m/ j. M5 [
/ x/ `2 |& P; D0 b2 q
yum remove apr( ~" o6 ^) u5 W7 a4 s, ~
5.Error.log中有: Audit log: Failed to lock global mutex$ |) ^$ |) D9 E8 v' g2 M
+ B, B, J4 t& ]$ ]( |
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock # Q n; o2 a# `- L3 ~6 f y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 B2 p; [6 v- h+ @$ r
解决方法:
! z' Y, z5 {0 X Q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# i; X) s* o( S% l5 `
8 b x X0 ~9 G( {SecAuditLogDirMode 0777
% Z7 A$ Z: B" [SecAuditLogFileMode 0550% D% t! q' r7 f6 r' u: G& Z
SecAuditLogStorageDir /var/log/modsecurity# U$ @9 S: l+ H1 W: x* o
SecAuditLogType Concurrent7 w0 B7 v" i; K
参考文章:
* a5 i# g- ^' G( U- |* fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# u2 b2 m! X. F& L3 _8 v0 o& A9 W" A, fhttp://drops.wooyun.org/tips/2614 |
|