|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 e( \. U! @3 y: C4 C% t9 N
$ {% d# a- y& r2 e( m8 {1 r5 ]一.准备工作
: q, s- c! M6 R6 r2 S
- p3 Y0 O# _" `3 T; o* j c. ~( t" b系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 Q5 r1 M& M6 D' s6 y; ]* \; C" o, v) ^. U! ?* F6 W
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ B6 F$ @$ _( c
& J# t3 L2 ^ O2 qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& M, V* b( }4 T4 r# _& ?
! z2 I8 V/ t* ]8 q0 j; }+ H
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 ^8 z% ~* V8 w3 A9 B
" O, `, e' N% W6 k$ ^# }& S( p" B6 t* f
依赖关系:
( H1 d! n3 _( }$ x( stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" ]- m n: ?* _4 j
- s( D2 L- i% |! I7 T' |8 eyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
; ], h0 Y& S$ ]2 u$ R% ]- n8 @modsecurty依赖的包:pcre httpd-devel libxml2 apr d8 B: n; o; m H0 c; W
% Y' U s! D& d. D7 e5 m/ f
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
( u4 j8 h- v1 T+ F" |! y9 N9 R二.启用standalone模块并编译' ` Y- Q" m+ ?! J2 H+ G3 k
% |3 R) w: Z4 Y* q( J& k下载modsecurity for nginx 解压,进入解压后目录执行:' K& w+ h' W; r1 S. u |. ]( v& l
! z" D) ?+ L+ a! ?' E) M
./autogen.sh# [3 f+ l W, W( {
./configure --enable-standalone-module --disable-mlogc2 o; e" `9 s, a# |# r# S; h
make
: Z* P7 d) `0 c% {- i/ { A' e三.nginx添加modsecurity模块
0 Q: L7 F7 N: F* t7 E f/ r k. j5 i
) Y, C' r4 o9 u在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! j% s$ P# D9 A
: A8 z$ w( N1 C. V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 v, L2 i4 ~6 X- z# Z. kmake && make install, y0 O' o, y* h
四.添加规则
8 `0 u1 X5 W4 o7 d) v
$ {/ ~8 Q: ]; ?9 L1 w+ {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 h8 ?0 F5 Q( n5 r" s5 Q" L
6 k4 Q5 g& N8 n) i4 O: P5 y% g1.下载OWASP规则:7 P3 [3 ]3 X$ S D, y d
8 ~! ?$ d, Q {& u8 ^git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( B; @+ J0 q! H- b3 k0 H5 D5 f
0 U4 N# N% w# E% _3 }2 {mv owasp-modsecurity-crs /opt/tengine/conf/& p" Y: d( T' u# Q
( |6 w$ k) \- T( ocd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 K5 H4 K2 u$ U9 }" m' U5 |
2.启用OWASP规则:
" Q+ b5 G6 Y+ u3 u$ f+ I4 H: C" H/ z5 ^9 R5 f- a# e M
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 L, q3 x/ {9 Y0 ` H
* p9 a1 O( j4 m编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 l1 g' H$ s# E" r
0 d2 t1 \& w2 ^$ \0 y" b$ gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 O$ `$ U7 m2 n0 m' b O" Y
0 k% `9 K X4 p/ _. i, XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, _' y7 S0 y9 T6 Y& x2 n) GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 \7 k! y7 f1 l8 g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% s% h9 R2 `% ?' \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" b5 C. D3 b, A h# RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 a0 I% ]9 w3 m) ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 h {) z; _2 T7 J0 v
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 ]' a7 N& V0 d/ t" X7 o
五.配置nginx
_9 ]6 D$ H4 L# _$ Z$ q7 \) U, }0 |( N! d) N- {6 Q
在需要启用modsecurity的主机的location下面加入下面两行即可:
% s( ]- E# K# V, u; b* [
) i0 ]* z0 K* v$ x5 cModSecurityEnabled on;
: Q- Y, v1 m& ~0 j. E5 NModSecurityConfig modsecurity.conf;
e, q% f8 }8 z0 s, n+ l8 j下面是两个示例配置,php虚拟主机:
9 Z9 B" `0 z) b" h4 q6 K# O$ W ?" ^/ q/ N& @& n: y5 v
server {
8 E! ^) \, G) f2 Q listen 80;, t4 x+ G" f, j0 R. G
server_name 52os.net www.52os.net;
, `% y/ d0 x9 Z" j! a7 ~ % m- V# ~. y m; G _5 \% j
location ~ \.php$ { J: \! v4 O' i* V" `
ModSecurityEnabled on;
y! ]* R! ]' Y g* a ModSecurityConfig modsecurity.conf;! ]( Y e3 I7 m* M$ d2 u/ g) x
# c: ~$ i% X5 O( }5 c root /web/wordpress;
$ I$ w! J) i& y; S index index.php index.html index.htm;
" e t% X$ {+ [
$ F& ?1 _8 W7 J4 f! S: z% s fastcgi_pass 127.0.0.1:9000;3 }6 D5 ^4 d6 x! `
fastcgi_index index.php;
9 K9 | a4 A( N6 V' T+ t fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
! y* ?0 l$ `: E' h d0 R include fastcgi_params;
I' ]5 b8 r' w }
$ o( D! _$ V" c$ z* p* l }% X# z4 \! [/ }; @( ~: y1 m
upstream负载均衡:3 ^& _( }' y! y/ i
8 [/ t. k @9 }0 z. t2 y8 Yupstream 52os.net {+ k& |6 _5 w' R0 Y
server 192.168.1.100:8080;9 l4 c; M# _! r
server 192.168.1.101:8080 backup;
- \& D+ G i: a6 M* U}2 R4 o7 M* f0 ?8 }' x) u' Q
3 Y Y+ ~7 B7 }4 s5 G
server {" W/ s5 j! x u
listen 80;
$ F8 N, i: q" |& ?5 x8 K( z+ ^ zserver_name 52os.net www.52os.net;
" f# `/ w& {! s# p3 Q) [
6 D' d: L6 A9 Y. P M8 V& Flocation / {. `2 P, u# C4 G) B
ModSecurityEnabled on; ' k" z% i! y" L' v2 g1 I" o
ModSecurityConfig modsecurity.conf; : d+ w2 b' }, L! n' O- Z0 a
6 ?4 J8 C. k# a! E* G1 Y( A
proxy_pass http://online;% B1 |, ]8 T; g: {% Z9 ^- n
proxy_redirect off;: ~+ c0 d9 x7 N& d
proxy_set_header Host $host;1 _( V: G1 W4 V. b: j. \
proxy_set_header X-Real-IP $remote_addr;
5 S' M+ x; P& a5 L! F3 c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 O8 N d) F) x& ~; P }; c& }( W: A8 b+ h2 y) C( b
}7 J$ M0 U, p$ ^: t$ U+ ~: w, N U9 V4 g6 e
六.测试
( i2 Q& Z+ `; {
9 d, w' h1 U8 p; A4 S2 N" l9 d9 ?" k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:" \1 m0 d% _0 F5 {, T
% ]7 E3 _, i6 U+ Q
<?php2 q( b ~. C1 f
phpinfo(); ) g& |' t% ]5 K3 }0 W$ y
?>
, d F8 \4 \- b8 n8 x: F在浏览器中访问:
) b5 X# e' L$ G- H
4 F* _# y" W0 p; [3 [http://www.52os.net/phpinfo.php?id=1 正常显示。$ q" A% {' W" A; C
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 y* R5 ]) K# W/ J+ O9 d& g3 Yhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。- N# F. p5 U* w
说明sql注入和xss已经被过滤了% f. [, F9 r v* `* V1 L5 g
$ r1 Z7 J. y6 X+ f* ~2 A七、安装过程中排错- y X3 n8 t1 [0 c) x) \
' s& O B& t8 ~; ^0 K
1.缺少APXS会报错- y: z6 c+ Y' d4 ]3 V+ [# h
% c7 ?2 N/ v( T! T5 _ nconfigure: looking for Apache module support via DSO through APXS+ a& V) ~, o$ _$ ^
configure: error: couldn't find APXS( K4 y Y! p; W& {
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 [5 }$ N/ A/ O5 W, b( r2 Z! g解决方法:
+ F. h7 ~3 {& u' {9 v% `
N# H o$ z% }yum install httpd-devel
1 L( x+ u* J+ a/ m9 m A2.没有pcre
' k5 C2 C) ^4 Q6 g
$ ]+ Y3 g0 y% Z K* N- p; ~7 `configure: *** pcre library not found.! Q2 S4 V! \/ o8 y
configure: error: pcre library is required
+ L d/ a5 T/ F7 {: U, x V! Q4 a解决方法:( o% ?: S. b9 z: S7 ^3 h) C5 [
3 {* s; G1 Z5 ~. M# [9 ryum install pcre pcre-devel
! j6 T" d; k! i* V3.没有libxml2$ q! `1 M/ U, o$ h9 W1 n
' T$ Q0 S% Y9 Q& U( o2 T8 c
" A: v: E% c. K1 @9 Q- o& }* J2 Mconfigure: *** xml library not found.
% m0 Q# q# l3 e/ x7 |configure: error: libxml2 is required
; Q3 T! O7 a# Z$ ?0 o解决方法:* d: t$ k' n8 R3 b* J
% h: n) x2 V" N) J! L/ l8 L& {( Myum install libxml2 libxml2-devel
- @6 L% u ]+ W3 _4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 T/ @; R9 ?$ B" X7 t% C7 G: z. N( H
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, t+ K# x. j4 E5 dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!* v8 ~: K1 S7 [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( J$ W" O8 [8 w
( a. x( |! J8 v9 L$ \2 L! x. z9 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' P8 }% D9 b7 g! b! L6 t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( \& ^! y7 V; u+ @9 ^7 b2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% y8 B1 |& j) t U6 S* r' Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 K. m5 Z2 S- `: ]9 v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ Y0 e' U. F9 l! l8 g; V6 D% C+ C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ S3 V: d# I$ j0 Z5 f
解决方法,移除低版本的APR (1.3.9)
, P% ]' } t7 X' }
/ V( }8 v$ }) }0 R( f1 ]3 s! \yum remove apr# ?( S6 Z! `) U6 l' B" I; k
5.Error.log中有: Audit log: Failed to lock global mutex$ K, f% v6 E" l! M' a
1 k. Z8 T" L4 Q0 R7 c# K, ]: H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* J! l4 c# A7 {. kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
l. Y( R) h/ T( I1 O# X9 v解决方法:
! W' F, l8 a4 P( W! P! c编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( E: q5 s9 i3 i, l! C2 F1 d2 ?+ P5 _; s$ ]- L
SecAuditLogDirMode 0777
4 X! t+ A3 ?' j- } cSecAuditLogFileMode 0550
: h" K7 _2 P& e% h8 F' OSecAuditLogStorageDir /var/log/modsecurity$ f% F% z* {5 B' P& ?. j: x3 ]5 a
SecAuditLogType Concurrent L2 u8 ~- h/ J: ^! k; T
参考文章:
- t: n" \& o2 r- H1 |; Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 R8 |& h! y/ z9 R3 o }
http://drops.wooyun.org/tips/2614 |
|