|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; L+ K P' o2 u* q8 V
* N/ y4 v) Z6 h- p" }" |一.准备工作& I+ n, w% h% \0 O8 A
! C- j+ f0 Y& O/ [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 q) l( H/ m3 h) o3 D9 j3 S' r
* f% O2 C: M6 a5 Z! P9 Q4 m& @3 Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 J: s$ I8 o- |' V
) a! P+ P: Q* y% i, Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; r `9 z! q- ^9 F
6 U! G3 B K- M9 m1 E7 w# |7 BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 c$ Q9 I* @4 `% b4 e9 K1 t' l
# c6 ?( X0 e0 }2 N/ d% ^6 H) U依赖关系:+ f" U$ v3 y [$ m, i; k* y( L6 d0 q( f* j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 Q7 s9 j e7 K. E8 M1 j& j! l4 i6 p
5 X' i7 W( c% ?: U! xyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- s' R+ K3 s% P4 g0 K
modsecurty依赖的包:pcre httpd-devel libxml2 apr
5 }) m0 \" m; r" s5 C( }6 v8 Y8 ~& A p O9 \
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 N1 f' N/ s% y' p+ O, t( O& `
二.启用standalone模块并编译
! V9 p- Z7 w! a- ~2 @ R' w( x; y9 \- E# i% L
下载modsecurity for nginx 解压,进入解压后目录执行:+ h# r, }! k$ o. d' w
' z3 h; i& o. ^- J./autogen.sh4 }3 V' F- }6 {9 K% v3 y5 [
./configure --enable-standalone-module --disable-mlogc, b# }0 L2 ^! k1 Q9 [
make
' h+ B: M3 d& \# I8 f三.nginx添加modsecurity模块
/ h4 R) O& a1 m3 @2 G& _/ b/ O* [8 q9 x) p% H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 j2 c, {' A: ~6 s( W- m U
* U% v; Q' k% @3 e./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
( E5 ~$ y( H7 D# h/ xmake && make install
! H( R, Q0 k: A9 J9 ^ g+ [四.添加规则
( N/ S. `* e. B# ?+ ], Y" T) S: R6 h$ d8 z: R
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: Q+ n- Q+ Z( H) c% Q9 X j i
) A! V; z0 D5 P5 M0 d1.下载OWASP规则:
+ A3 i0 d( d+ |3 D% t! E5 y; e( Q9 B; T# L5 S
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* _8 {" R! b. \) _* F8 Y4 Q% W$ e7 m
+ i! E( _) p0 t5 w3 Vmv owasp-modsecurity-crs /opt/tengine/conf// ^2 |* v; e1 X, l) O# B% l
! c4 O+ K% `) u% E) C& {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 w8 h \, F* W" y. e8 G
2.启用OWASP规则:4 `# B w7 V$ r F9 s5 o" f
$ f- b2 i: m( {5 [/ p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; v( T( j8 i# h% k/ J' X
$ ^* G- G |! E* s0 A) i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% F! k# F% H6 l }+ }, _9 R5 \$ u W9 G) o
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; `* z3 @% W9 a# w. w3 k N
5 h7 t+ X. c: e0 F; K# kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 H6 M7 r2 d+ J/ a5 V! ]+ `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& G( N5 A/ l) u/ _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) {1 L2 ~& y& ?) K! T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) h9 \8 X( _- I& P' A' _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ s. a X j$ N& _7 [6 X4 r8 X6 R) n7 ^
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 x# N9 A2 d& i F* B+ j
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) `6 d, g! R, g- o; o, \
五.配置nginx
8 y, b ]0 z- [$ C$ H) Z0 y! b5 K* Z6 P2 v# q5 m4 G2 d1 L
在需要启用modsecurity的主机的location下面加入下面两行即可:& w9 c5 z+ C$ t+ p2 q9 w" X
7 `) ], n+ n* ]) k4 J
ModSecurityEnabled on; 5 m# D7 l/ p1 c8 n9 @% Z
ModSecurityConfig modsecurity.conf;
4 D& W8 K' r# Y0 C5 Q, {下面是两个示例配置,php虚拟主机:
* e7 y& ^: F T( ?5 q( d* H
; c& O' E1 z0 l7 {- _server {$ \' x) ~# Z1 W5 A2 m0 y) p1 x
listen 80;
' x% l( E8 Q& B3 V/ W9 W0 u5 Y* C server_name 52os.net www.52os.net;9 ~( `. K0 C2 @4 R9 s
5 M% v2 h, B& M" w
location ~ \.php$ {6 F) }/ H: ]0 e( E7 G/ n6 D1 h
ModSecurityEnabled on; 5 J& A2 o. n7 |3 C
ModSecurityConfig modsecurity.conf;
, e2 q2 l; S3 T; i# p- C% U! y/ q6 {- E; n* q3 l. {+ x
root /web/wordpress;5 e6 I. {0 {/ M- H4 j* H# n
index index.php index.html index.htm;
I4 ^: W' Z& L$ z& Z
. J+ R/ `" H% r M0 v fastcgi_pass 127.0.0.1:9000;0 N; q5 Q5 E$ I! O1 R. V
fastcgi_index index.php;+ b" L# w+ Y2 O0 e- J& R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& R' X( [% ^# b; B$ b
include fastcgi_params;
. _# v1 i6 N5 f: K! L }
& d; `5 H: B4 L5 M }. S9 [. R5 H; X9 J5 W4 X6 N
upstream负载均衡:# V& d) s/ U1 A+ [2 N' I' N
- ^+ }% N- k- |' Z c [8 qupstream 52os.net {
, b3 _: ?) n* o5 v server 192.168.1.100:8080;/ F, u/ z, _. S6 O/ ^: ]
server 192.168.1.101:8080 backup;/ d, z& z4 \, V+ V2 V
}
: m( H4 ~! f" u/ J% b0 l
+ e b* o/ |$ X0 R, tserver {
! x- K$ B2 |* T8 ~3 Xlisten 80;& J) }9 ^1 V/ t0 C" c9 J1 c
server_name 52os.net www.52os.net;
; C9 j( q0 I% Y% P8 d' a$ T% g/ q/ ~- j7 e
location / {
" v# _: e: r. E4 D ModSecurityEnabled on; 7 F& R2 Q2 a( |
ModSecurityConfig modsecurity.conf;
' o* Y5 z( u3 j' P/ @3 I
- F4 @/ O* D; y& c) P* C proxy_pass http://online;5 F0 n. _* H/ w9 Y+ x
proxy_redirect off;: x5 o/ N9 K: \1 T
proxy_set_header Host $host;
% I! _$ b+ T1 Z( B+ Q proxy_set_header X-Real-IP $remote_addr;
6 k8 _/ I' ], L proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* g. E( k/ k5 _1 S/ Q8 Q5 c D. T6 v
}8 i. w( i% p+ l6 y
}' c+ J; p! P' h+ ?0 P$ B R
六.测试
" h2 r6 }0 M+ V% O. F5 Y" Q
# {, o, e* i3 A' ?我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 B( q& Z8 ^8 N+ f S G" j" }
, x [" r1 `3 l) P! i<?php2 e3 g7 t4 E- H c$ q: V
phpinfo(); " c# W% A0 X" f7 }
?>7 w; M: K, v6 \% \
在浏览器中访问:1 m7 [; ]" |6 `8 K7 x' a2 s2 }
2 C% p. J; `; h, x, b' F7 Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。
" F4 Y/ t# M- j8 |http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- D; K. V' A4 m6 s, c% {http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
+ g$ I e4 x4 ~6 v: g说明sql注入和xss已经被过滤了! }. R' C, H9 a. v
# o8 p3 Q( \6 U$ |; z% i七、安装过程中排错2 L; A6 Y2 ?3 ^% N1 T) j
( W- ~$ u( V( h1.缺少APXS会报错0 }! a& c, A. ?: L" f
: X; R# g$ e: wconfigure: looking for Apache module support via DSO through APXS/ h% {# @& e$ ^
configure: error: couldn't find APXS+ I' j' {1 t7 p' F4 S
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
N W8 I) p8 _8 h _解决方法:
$ S# |7 J0 Z4 s @. E" ` ?4 U' i1 t5 B8 e* W7 o. ?6 n
yum install httpd-devel
' @- f" K6 L* Z0 \2.没有pcre
7 T! |1 B* z: e8 A% [9 Z
4 P, P1 u; v- T! b; Y. t0 `configure: *** pcre library not found.7 P0 }. y9 k, F) }$ y5 D1 t
configure: error: pcre library is required1 v4 u) j7 ]; Y1 M# J6 r
解决方法:9 O& Q, W% l1 a& c* {
8 o2 Q! C* J9 i6 qyum install pcre pcre-devel
! f" y( U4 i% a0 x' k, D1 t4 n3.没有libxml2
; K: X; v' k& ~9 k, b# o/ l$ \, ^& o$ ?# ?0 }% L# C
) t! O( @' J' a {configure: *** xml library not found.
9 K" K- H+ [% Z3 h) O9 tconfigure: error: libxml2 is required
' M- D( d( p$ U; x" F解决方法:
2 p3 M1 [; |, _+ [, e2 r0 A( J ]9 _6 Q" R- m! I
yum install libxml2 libxml2-devel% F) T9 @- s9 I1 o- r6 C
4.执行 /opt/tengine/sbin/nginx -m 时有警告6 G: i+ b% E, o6 j( }% r
c0 c& ?6 @$ O
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) z" o4 T: Y# H4 w: N8 n& }$ Onginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 O. B9 k0 H: @" h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 h0 F+ {, K( f+ S, Y+ P+ d* E
$ \: h: c8 E, V2 A* L. h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." l2 ^- v+ ]$ d( t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"% v2 o. K7 w1 n/ J5 v& C
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! m0 M" L. o% Y- S! A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( ~. W8 I; H9 ?" V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 r% w) F) _1 {! j o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 o4 `1 _* B0 K ?解决方法,移除低版本的APR (1.3.9)
0 ^7 a; L' ^' a- _5 }5 h! e* Z: W% k; C' b. Y. o* Y
yum remove apr
# [! w' y, l+ q4 v# i. i I- y5.Error.log中有: Audit log: Failed to lock global mutex
* j( z* c: `7 \* X" p( Q( D D7 {* X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
" h$ a8 W5 l8 B: U: e: o nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, D8 I; U" O5 f6 j8 e解决方法:
" ~4 a6 c: H# Y9 S( |# d3 I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ `6 l$ G9 l! Q% w3 f/ ?8 _" c
8 t' t w/ {$ P% s+ z/ TSecAuditLogDirMode 0777
* W/ z$ q+ Y. }: e7 ^$ q0 `* A sSecAuditLogFileMode 05503 p, _ w4 i7 e- l, M7 D- P
SecAuditLogStorageDir /var/log/modsecurity
( h5 P2 n4 ^" z6 G: ~SecAuditLogType Concurrent: j' d, C4 u5 E) l7 ]; s7 u
参考文章:- H1 ]# `, B$ Z/ T# ?2 s- s+ X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# w; n/ f; S% t/ G
http://drops.wooyun.org/tips/2614 |
|