|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% S }, z8 m1 {
$ Z: ~* d- _ d
一.准备工作
. ~! j+ P6 ~! L x
3 A' f T/ Q7 d1 C. d/ |; b系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- `8 v7 z* p4 \& t" \7 }1 ]
8 a3 i: S1 _+ ^0 M# o" }) d1 Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# m) H. V" S) P/ Z& X% z* f. I( P% J( K" \% Z$ }
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* O( j3 P; h; C1 I% _* ~
) P& Q0 i: n2 {1 Z8 eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! G# ^$ |. A# z1 N
5 {, z9 Z$ d4 p3 u- d4 E! k
依赖关系:
, h2 R& k$ }2 B- F/ c5 e2 mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ A+ Z9 J2 l- w
8 h3 h# m. J/ X | i
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; b% R7 b/ o: }; T4 f1 S
modsecurty依赖的包:pcre httpd-devel libxml2 apr
$ c: }' w! w! A
+ u+ W% x" i* c! V) |yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
1 d: C1 h% ]/ k二.启用standalone模块并编译3 Y, D/ `( h+ x, Y
1 t, g$ `' [$ L. j下载modsecurity for nginx 解压,进入解压后目录执行:
/ R6 Q" V$ N; i9 s
* ?0 }) ~5 ?4 |) z, W./autogen.sh
; e4 S: V; I) W: J8 ^./configure --enable-standalone-module --disable-mlogc
/ u$ t7 r& K- Y" o Gmake , k3 R- C7 I. T0 d$ l
三.nginx添加modsecurity模块5 M' O4 w7 X1 }4 N2 P: x' J
9 f& h9 Q c y- \* u7 t, |
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 H% E2 |7 x% |- {
0 r' s6 O# Q4 J0 G) [
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine$ c* u7 X8 Y9 ]5 V+ _6 I
make && make install
, z7 w9 j8 w( ]& ~' n; @四.添加规则
% F2 E0 r4 h6 W$ m/ i! J6 ^. v0 M
- x; R! |& v- P$ D( l& r1 Umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ ~6 B( Y, g! V
! Q: H, ^" x- N1.下载OWASP规则:' p3 c( ?, M5 N+ P* D
# N0 M8 z# q3 q2 R# ^( m F* vgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! j2 F% n ~/ _' n# A3 G# Z
% i1 v5 L5 J- d2 |+ Tmv owasp-modsecurity-crs /opt/tengine/conf/
" g- B% S5 n) H L. D2 F C9 _, d' w" _0 g* S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" h# V- f+ {! a5 G# u7 t2.启用OWASP规则:5 `% b/ U- g: {. @
7 o. T1 U, `( }3 l5 i# Z, w& s! K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 N8 e9 M2 Q8 u/ u5 O$ D+ L% J% y' | r* k9 T9 h" i, N
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( b: A2 Z) u5 {+ B
$ ~8 m, ^. F$ }( I2 X* a" d9 D! n$ S
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" n' b U& `- W- ?: M
* q1 ~* M! I7 b$ a0 LInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ P/ M3 L; V" ]; KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
r; G. Y! ]6 Y( T& {3 ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" l }: ^8 s' H9 x' XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 ?7 d( x3 e2 Q" ~$ BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% V a! {; s3 GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ l0 ^# O- c9 n( A0 G6 [Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- Y! E1 Z) @" b0 h0 w+ G
五.配置nginx# `- @5 D) u8 x& W! ?
; X; v5 L$ i1 t. W% H, L
在需要启用modsecurity的主机的location下面加入下面两行即可:% |# p" \3 |' r6 W: ^( s2 u
5 v( F8 T/ Z' A8 `/ g, VModSecurityEnabled on; ' O' B$ J+ s( I/ P7 M' M1 p
ModSecurityConfig modsecurity.conf;$ Z4 f& G" ^5 q8 C9 ]: S- W' M7 [
下面是两个示例配置,php虚拟主机:6 C+ x' o7 _! }
# U& d5 T4 S& Z- R' _" Lserver {& H; }! l& t1 A* U" C
listen 80;, o% W8 I9 a& k( j6 d- c
server_name 52os.net www.52os.net;: I1 ~( S; g5 ?+ M" M
. C& S) N3 M( s# u; l& V# z
location ~ \.php$ {
* M/ r& g* P a8 z% u ModSecurityEnabled on; 3 ]2 e( _8 [% n8 |& N0 ?; J- K
ModSecurityConfig modsecurity.conf;
3 D0 t3 l1 b9 s
) d( ]) G+ y+ V7 {% R) X, t2 W9 h root /web/wordpress;
1 j1 w+ W& y$ n; n* ~ index index.php index.html index.htm;
: H7 _' ]( Y9 q; D" K0 v3 t, x 1 z: n7 z( S2 N+ M- [
fastcgi_pass 127.0.0.1:9000;
* Q) n7 L# }8 J" u4 H fastcgi_index index.php;+ [1 R b P) [2 Z, [+ G1 }9 A/ v
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, V9 u: C9 C* k J include fastcgi_params;
4 k7 F' w. q w; Q0 \ }( N3 L4 G+ Z: M9 k
}
5 p) U- f% l8 j8 y' Supstream负载均衡:; ~6 e. V( Q& E6 T9 U0 D" h) G
: s4 h, n$ k# @
upstream 52os.net {
/ C0 [5 d \0 @ server 192.168.1.100:8080;
6 B+ |" v; ^ _4 K: J0 }7 R) Q5 d) n server 192.168.1.101:8080 backup;
' x( t. w+ R$ G5 L4 E1 \}: E# O! Q$ }) N
8 a- s9 P8 c' X& u8 ^8 T' W
server {( Y" m- E! R% E0 a; m
listen 80;
2 N/ p* H j+ h8 f+ d: t! ^, }0 pserver_name 52os.net www.52os.net;% R+ B R! {7 f% s& ^3 A
- t0 X$ F* a0 o8 m! @8 klocation / {1 d- p' H5 n# g$ X
ModSecurityEnabled on;
& |9 M; |' o M8 X2 Z ModSecurityConfig modsecurity.conf; 2 |3 @4 T8 p5 B. H# i3 M) a
, M& @" N7 G0 O5 v
proxy_pass http://online;
3 s- e# q2 Y2 d3 @% T3 t proxy_redirect off;+ f- N8 H: u2 I U- W5 p
proxy_set_header Host $host;
, k3 j, r0 C) u# n proxy_set_header X-Real-IP $remote_addr;- G' @) u( ^6 @- s
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 V0 e0 M2 ?3 K& l( T }
) A( l% e4 _9 r% ]% O8 }3 ^}
" {4 `# S) ?9 A( U' E- O六.测试
* I* B# t9 `/ ~% s$ J- p+ U8 E
2 L% C% B" p5 K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 o8 F; ] n( A4 a! [. [
3 b: p5 S. t% e+ h1 M* \# c<?php
: t* O' ^- e0 y phpinfo();
; @9 j7 [0 {: D0 { D?>
! s2 E0 N. d+ h5 G4 X3 ?在浏览器中访问:
2 h* A0 U, B4 x* ]% s* I3 N- _! Q$ T3 u8 q, G! H( P |
http://www.52os.net/phpinfo.php?id=1 正常显示。* o' l. u! d* v
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 ~4 |, Z! E% [7 Zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
$ Z" O( R% B3 y: w: w" x- s; y说明sql注入和xss已经被过滤了
# s2 y- D" M- }% V' \0 A: s3 j* }' u0 l
七、安装过程中排错' @6 l- j' K# `6 ~
! _" f( q$ i3 X: n" ?1.缺少APXS会报错
3 J' ~! Q6 ^& S2 l# t& Y. [- n5 x6 d$ T# B4 c; d5 V4 v
configure: looking for Apache module support via DSO through APXS
I9 f5 h2 V& p6 K5 gconfigure: error: couldn't find APXS- M m/ B$ B1 B+ j* o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( ~" J A0 s. z. m3 g, W
解决方法:
9 K; ~/ x) @0 U! D. }% J$ A
! s F& u, K% ^9 j3 wyum install httpd-devel8 M2 R- X- r+ L% F2 f8 b
2.没有pcre
6 v z2 j# s6 H! s1 Z% q9 q7 L& u _. e+ y0 v. [. B
configure: *** pcre library not found.
; y8 z [' t" |, ~5 @configure: error: pcre library is required
4 j! t& ^# Y: I& Q0 D; N解决方法:
; V/ o. |) ^% H Y: [
( t; \ p9 a0 k" tyum install pcre pcre-devel
. H$ S5 Z; p; x) x. D# O3.没有libxml2
; Y5 \3 N. g- b
0 t6 ?( d! U) \$ q1 l+ e4 |- J" F, V" W
configure: *** xml library not found.* \7 ]$ p- ?9 L, B
configure: error: libxml2 is required# U* y. E/ g; u# e( {; W! R
解决方法:) S' {2 A- V1 @) u
. }; `/ n* M* p- T: W: C
yum install libxml2 libxml2-devel0 D8 r3 U4 y2 i2 J P* f* g$ J5 j# {" F
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ E; m- W; g. s. f% M; s
1 P! ^8 g2 F: f: u, [9 g6 A: WTengine version: Tengine/2.1.0 (nginx/1.6.2)3 v) i: c9 u* c; \. E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: i5 `7 ~1 U O' R6 |
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ i3 e' s8 j. Q0 O, W% }2 N, V
! a4 A& m W& U1 n5 L6 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) q7 X( [7 r/ o, }2 G; r- }/ I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( |/ K, [5 X% g2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! V( Y# Z9 s4 g3 x& m A* t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& Y, Y. T! ?: P7 v, \/ J* N1 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 c( p' x/ u3 ?& r2 t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 F+ C- @5 ~8 o- v# K6 X
解决方法,移除低版本的APR (1.3.9)0 \, H g, K1 W* d
. R6 [% Y, @0 s/ U. _7 cyum remove apr
' e' d2 p* U% q+ y% V9 l5.Error.log中有: Audit log: Failed to lock global mutex8 `8 d& {: V1 ~" {; L, K
& B" i( D) w D7 C& h- p/ O0 F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
3 V/ z) c, r. K$ V5 ^1 ^* qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 M! M8 d1 ^3 M- s解决方法:
( E1 x# q& E$ E% r" T& ]编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 _, ^8 G! B' Y% y5 Z$ s9 h8 P# d4 v2 `) ~; u( [
SecAuditLogDirMode 0777
$ _! H9 z7 f; X5 {* k# dSecAuditLogFileMode 0550
0 b" `; K( Q9 t6 U! S; bSecAuditLogStorageDir /var/log/modsecurity
7 o9 H2 z$ z$ ^SecAuditLogType Concurrent
0 i+ n: j6 D, W3 p+ p* {. c参考文章:
# d; J' ^% ^# l' L5 S& i' Q# fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 z$ ?8 }" G' R) A* ^4 Q3 {http://drops.wooyun.org/tips/2614 |
|