找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9830|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
8 ?1 v0 t- Z. w+ R8 v: {( k
3 z9 p( ]7 D! R/ E4 Z4 }2 x% Q+ S' Y" O7 E+ R1 i
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 r9 L& P5 W8 i/ \* b) H+ u# A
! i& k9 W" y/ ?
* R4 B  I: K' F. y什么是ModSecurity
3 _' O3 p2 E' u& {* N& OModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
7 ~3 ?$ J. j5 h4 R9 e/ M5 |
" |4 m7 U" f* P8 F0 d- R$ D9 b8 W& e
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
  n, _: I4 f4 {2 c- Y! v/ f
# E- |- `  b- h& B2 [) g2 [7 ^+ ]8 C: s1 f9 q$ j6 S# i2 F5 I7 ?
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 F4 I7 ?+ b( g; V
+ F( A' x8 Q5 H' k/ d

+ }9 w) w0 @/ O0 |1 m8 ^+ N官网: https://www.modsecurity.org/
1 V( j, n  p4 Z5 y0 o
5 Y: x( S# j% ?& K% v6 u3 z4 o, `+ i/ W0 c  A6 C' e
什么是OWASP CRS  Z# ^3 w4 f* I8 U) r
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。8 a1 F' ~  {' B7 g+ q" P8 D5 t

! r( y+ C, e7 p- ?
+ n0 A/ F7 t# P9 p6 RModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。- b+ m( P* [) L2 w7 g( J

) S8 @4 v' l( N% X
0 K) V9 w1 P* t" ?HTTP Protection(HTTP防御)
3 Q0 y4 v: X; [% rHTTP协议和本地定义使用的detectsviolations策略。) E+ G* h1 N' y

( A- m* C4 k, S/ P; t3 P
0 a2 H% g; q' l+ S# H: cReal-time Blacklist Lookups(实时黑名单查询)
) _( @6 T8 q: |& [1 U2 h利用第三方IP名单。
7 b+ |* X" s5 E! ^0 I( y4 n3 W: ~! I; q8 X9 X
; \" D: Z2 u1 @  y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
  D0 ?+ E5 x$ T防御HTTP的洪水攻击和HTTP Dos攻击。0 z2 U4 l: h2 t0 `6 ^. d2 Q+ @
) ]. m# z3 s& B) B6 X5 K% I

' @5 A1 {3 {: h: Q# zCommon Web Attacks Protection(常见的Web攻击防护)9 Y+ @9 L% c& {* k
检测常见的Web应用程序的安全攻击。
# C% T0 V  D% n6 A1 O* f/ f8 [. ~0 g! y3 J. g6 w

3 J0 ], u0 b! H  m4 M0 Y2 JAutomation Detection(自动化检测)
' l7 j0 O3 n# Q! P检测机器人,爬虫,扫描仪和其他表面恶意活动。
5 E6 a, ?) V' ?8 T$ u
- C. U" Y/ V1 y/ |+ R% Y: g3 h7 A/ n) U5 F
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)6 C! {- c  s+ ~5 {. \1 I
检测通过Web应用程序上传的恶意文件。3 a9 _# E8 S- B' c& C0 ?
( z: O; [8 a3 P; D, i5 I+ i, ?2 @

+ \+ C4 `- a/ ]; |9 OTracking Sensitive Data(跟踪敏感数据)
  u; a% B5 ]7 `+ G' d5 C) X) o  n信用卡通道的使用,并阻止泄漏。& d2 C9 o. U) {* u7 _% o  p
! _) P0 g3 n% _# P% r
- h# U# w6 n6 {4 J) A) _
Trojan Protection(木马防护)9 L9 @$ Y2 n2 [
检测访问木马。
' H/ y' C" T6 A3 a5 c& a4 v( X, w$ A# A- S

, f( @; a3 f4 gIdentification of Application Defects(应用程序缺陷的鉴定)
# f4 |! `; G  |1 S) {$ X, ^2 Z检测应用程序的错误配置警报。. A9 r5 s3 R: d+ @+ j

9 ^, h% \2 I* `
! }: u+ c% s0 f" B: P" ]* cError Detection and Hiding(错误检测和隐藏)
' R+ T2 \/ x) T( k! w# h8 Q检测伪装服务器发送错误消息。
) t0 x; [0 n; v( i) u9 A" m% k4 _: ^
' i& S0 ~6 |3 }2 \, B: u; D
安装ModSecurity2 X  U2 A* r- J; n+ F* Z
软件基础环境准备
. ], c$ d3 d& p% w; _7 y下载对应软件包6 R6 |! C5 n  H
$ cd /root0 I; j2 s( v. g! c/ b0 y* q2 y( d
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ r9 T1 @. Q8 Q- g
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
6 {0 [, E, U5 v安装Nginx和ModSecurity依赖包
! m7 O1 E7 \- t' Y, ZCentos/RHEL4 z$ e/ C( v* r* i$ [* i3 V

! l7 j' f2 z. T! M0 J6 Q% ^$ s5 f9 y8 _& ]4 }# ~9 }9 F. n! a) N
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
. w! {5 A/ [& x- SUbuntu/Debian% G( x/ n6 f& @: u: M* i5 ?

4 o% S  G( M! x4 {) C' z, m. ~8 |: ^/ I0 h1 O" @  B- ^
$ apt-get install libreadline-dev libncurses5-dev libssl-dev perl make build-essential git  libpcre3 libpcre3-dev libtool autoconf apache2-dev libxml2 libxml2-dev libcurl4-openssl-dev g++ flex bison curl doxygen libyajl-dev libgeoip-dev dh-autoreconf libpcre++-dev& _2 W, z' x9 ^
编译安装ModSecurity
+ F& J6 N( n) D' T- O8 v6 ?' m5 tNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- X5 u$ K; ]. l  e

. {* n) C: \! ?0 T
! Z! D, ~- ?/ ?# x方法一:编译为Nginx静态模块
$ y+ z, ~1 y5 E4 J6 r
9 l3 b: @9 g  S8 l& W$ \" G0 D4 G9 T& M/ t" p
编译为独立模块(modsecurity-2.9.1)
) d+ L. J' C2 X  ]$ tar xzvf modsecurity-2.9.1.tar.gz
. W9 \# D5 v5 c( p5 P$ cd modsecurity-2.9.1/
) B7 I& {, x/ i( C$ ./autogen.sh$ j, _( f5 D  e/ v/ x
$ ./configure --enable-standalone-module --disable-mlogc
+ K' z/ A( @/ [& s3 Z$ make  `+ h- |! z6 Y- c
编译安装Nginx并添加ModSecurity模块
2 I3 N* W& w# f; R' P5 y$ tar xzvf nginx-1.9.2.tar.gz
% y6 M3 f# N: S0 [3 ]4 g+ z' l$ cd nginx-1.9.2* }9 b( c* s% ?$ g
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/5 p! p2 M; o. x4 \) j
$ make && make install
# S. H; H7 V& |, M2 c方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& T/ N9 d: p& p/ i
) t; J. f# c' J6 j

- }4 u- N0 p1 r4 r0 K编译LibModSecurity(modsecurity-3.0)" a% Q+ g6 X+ j5 o" @6 l
$ cd /root
3 F6 n! r( G/ t8 A: Q$ git clone https://github.com/SpiderLabs/ModSecurity7 T5 E/ U* |/ i
$ cd ModSecurity
/ W, L3 ?4 U/ Z  J$ git checkout -b v3/master origin/v3/master2 H- O. q; I4 }8 r. U  q/ N
$ sh build.sh* x0 n( @. c4 T: L! @# s& X- W
$ git submodule init
9 ^' @  n, z. r7 D! r# @$ git submodule update# o; l& ?+ r! N. w# W6 h0 e
$ ./configure* G$ N) r+ s% }" y0 c6 H
$ make0 D" x8 Y  g* e2 Q7 O* a: \( ]
$ make install
! K' E4 i; w8 d7 d( h. L* V  eLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。! _; Q$ w$ ^- j% F5 p" f+ K

. q% d: ~! P" R  h4 e: K8 Q9 K, S! I4 u& g% u
$ ls /usr/local/modsecurity/lib
% D1 x2 O# W9 j! J/ Vlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
! F9 J! w3 H0 C0 q" j' ?3 u! z; E编译安装Nginx并添加ModSecurity-Nginx Connector模块
! p+ y5 `# \9 }" R0 k3 X使用ModSecurity-Nginx模块来连接LibModSecurity, z% K. l% d8 Z0 P5 L
2 N% T% n; N$ }( d+ }0 W
' H$ i  r7 k5 O% G6 f
$ cd /root/ m. B1 Q8 ?; d8 j% y, U6 N" a
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx0 }9 _' e% ^. W% `/ n# ?; N
$ tar xzvf nginx-1.9.2.tar.gz; L( S& A3 ]5 J2 O! y) O
$ cd nginx-1.9.2
+ }9 }6 x$ A4 e( o8 ]2 a$ ./configure --add-module=/root/modsecurity-nginx
$ {: L/ k& A! R, @; e$ make
, K/ T3 N3 D- W; C4 W$ make && make install9 ~9 f% D- O8 V9 c# y8 z
添加OWASP规则% [( N1 }+ G9 p0 e( Y! c6 V6 y0 E
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
3 O. n2 m9 s7 Q& M8 [! O3 D" @# z. h% O$ i- x- F: I% }
. L0 w  ~6 f" ^2 D! W7 q, C
下载OWASP规则并生成配置文件
8 y, C: F5 d6 x" V* D- l' h  d7 O3 E4 s$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
: }2 V2 ^  ?9 [! t3 l* @8 q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/- y5 T+ b' a  x7 U
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ U4 k$ {3 E& J$ @1 ~2 O
$ cp crs-setup.conf.example  crs-setup.conf
6 V3 ]1 Y, g$ Z配置OWASP规则4 R3 d% u; x4 W, S+ {8 o. z
编辑crs-setup.conf文件
& I  N8 W) v4 p/ i6 p
7 `5 v# ]1 \  ]0 x: u2 r% n4 r3 B7 ~) t' v* K
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf" z/ C9 p; f5 f4 c+ I4 s$ c
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
, a1 h# j! }6 n& E2 y" j/ y$ p$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- c& B+ z0 o8 _3 Z8 ]
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
; `( }$ Q' S& v( d  w$ }9 M默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
+ s  x4 _8 D$ p$ L- [+ K/ g1 f7 c' g+ z7 n
* V! v4 I6 Y1 t" V* o6 q6 f6 u
启用ModSecurity模块和CRS规则
2 D6 g) S/ i) ~- B/ a复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( i, U1 P1 t( A$ y; X# j8 P0 E

% A4 {3 v. P3 n& l/ W7 Z( a$ N' O0 _  \+ P1 z
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ c# r& M9 q; J$ k8 y

, {4 i# z4 B( S/ n3 C# `& V2 t3 C, h2 C* H
$ cd /root/modsecurity-2.9.1/
- E) c+ x8 F% ]3 x  s4 B$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * |) m, m: O3 ?. e8 x
$ cp unicode.mapping  /usr/local/nginx/conf/
  X+ e5 P3 k* t+ g将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( p. ]4 _. ?: l2 h( Q$ T6 S$ P  o' x  |# r9 [0 g8 M0 d$ r1 S, G; H
  p8 O0 m) O8 ]
$ vim /usr/local/nginx/conf/modsecurity.conf
. Z- \* j$ B: t& h; mSecRuleEngine On/ C/ b, A' n; B; u
ModSecurity中几个常用配置说明:* Q" N& ~- ?$ r) @+ p3 l, p
! N2 H5 [( Y  ]6 W& n
" W3 _' q1 h  p7 T/ X
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
9 g! I/ n6 J" l1 m9 B
! X/ o9 [  D* Q& a7 _& i
) q" }, R3 ~& N" C0 K8 a5 D2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
; W' s' B+ r, g7 P4 o; s" y
' u, `  o3 H9 e5 \  k- c0 d. z" v% w+ O' H  r  t
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% l: s  x. q- p" a2 g1 |* o" F1 |! T
. P9 t% A' P" y1 ?) E+ c! o9 e6 z8 `1 u. \3 w' h3 H" Z+ m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
' x$ ~' g$ p4 M2 V9 ~2 Y8 f7 g5 A- u- _1 u5 Z. i

  G  E+ F8 ~6 x) {! e1 M在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 H: m; ]2 G- f& S  W; K* m- ^8 w- V' U- u
/ L+ |" v: ^! D/ K5 T
3.x版本CRS
( d5 Z" x1 n  d5 u$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% B' T' i+ @  }$ H% P2 K, P" t# 生成例外排除请求的配置文件
. P# P4 ~) G- b2 E5 K$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& g5 o4 ]: A% \
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 ^4 j7 q/ l8 c$ \* @$ |  D$ cp rules/*.data /usr/local/nginx/conf3 ?& A2 M  U# D  u3 B& y2 N
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' \& C$ h+ W5 {/ P8 J8 A# q$ @6 k
, t# Z0 u6 A/ {' h/ K" e0 ?
) x5 c$ j9 K  V4 Z0 S1 D2 R
$ vim /usr/local/nginx/conf/modsec_includes.conf
1 U( i& {0 `% ]5 Y: X& F- @* s' a1 c1 ~1 I- @% V+ W" E' \3 ^, I
[Bash shell] 纯文本查看 复制代码
include modsecurity.conf
include owasp-modsecurity-crs/crs-setup.conf
include owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
include owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
include owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
include owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
include owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
include owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
include owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
include owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

  w3 n( D* A# [! m4 c0 u; `: Q0 C7 A# o2 c# B1 m
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
: f/ q; M; [. p# A/ n- M- x
! n4 c4 S( k% \
% o0 g# k3 ]" x  K" m: o配置Nginx支持Modsecurity
1 F+ o7 ^, M7 T, ?0 x启用Modsecurity% j1 a8 k% k  J* P
使用静态模块加载的配置方法# {0 t2 A9 t1 \' u* I+ [/ V
在需要启用Modsecurity的主机的location下面加入下面两行即可:: [( J; z8 r0 n) |- f( a
: k- g0 O2 B: s6 r+ `% k
9 C. G$ Q, A0 S. V# m
ModSecurityEnabled on;
! m4 D! U1 q7 N( D8 J" I, `! @3 ?ModSecurityConfig modsec_includes.conf;
7 g: V8 t0 C7 ?5 C修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; V6 E8 a6 ], P: E+ c
# k! r* Z" S( X. N, m

' U( Y" L; ?  e4 ^3 A7 S& G5 K$ vim /usr/local/nginx/conf/nginx.conf
$ H  y& C! E5 F0 V0 S
1 Q/ T+ ^! V* Q6 E! `" r3 p0 T# W7 p. \, w) i1 X8 c
server {- s! M# P6 M/ n
  listen       80;8 ~! l& @7 r5 z; x% i6 ^  R* u
  server_name  example.com;
1 m, ]1 S7 x* b0 W% o$ k' ]1 N" e5 a) S# A
, _3 d+ L5 P* N( j
  location / {
8 j9 A3 X4 E; c. \3 p" s  E7 W    ModSecurityEnabled on;
0 x( O7 t2 d. M    ModSecurityConfig modsec_includes.conf;! Z- k4 k6 j' I; r& ^7 b' t) |8 O
    root   html;
5 l/ q  P  Q# U( U* t3 Q, J    index  index.html index.htm;
7 O( E. d* |1 q+ j4 c% w0 x; ^  }2 o+ O1 m- Y# K" r" v3 V) c
}
* b# T: Z& D: R4 T9 _使用动态模块加载的配置方法
( R! H3 W. k+ Q: Z在需要启用Modsecurity的主机的location下面加入下面两行即可:6 q9 [, S: b$ Y) J- _5 a

0 J; k8 a( q8 u$ g+ h: t0 `  u7 f( G
modsecurity on;7 I* D  \( F$ f# d  Z
modsecurity_rules_file modsec_includes.conf;8 A" n, q8 T* f6 ~" @: H+ |6 q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% L) F1 M: K& f4 F9 u1 G
3 D' {; P0 k8 @5 k
! t. {) x9 [, }( ~; V$ vim /usr/local/nginx/conf/nginx.conf: h' o# B8 I: S& k5 q5 ^3 h6 O

1 G1 N, `# W9 @- d, J2 _4 V- L3 x! O& b" N, j4 t; P/ o
server {& I1 r& ]7 U: s8 o
  listen  80;
: w5 X3 m/ R4 H- U4 j  server_name localhost mike.hi-linux.com;
, W9 @8 R2 _* ?' x! v( ]  access_log /var/log/nginx/yourdomain.log;4 X9 c6 f" A; K; C7 H: d+ g5 A' A8 C

' W: ^. z$ D# `  Q; f' H8 |( B. W0 y. s- `1 x  [9 D+ O2 f5 U, w
  location / {
# }& O) N( n8 u+ y8 t3 e2 @, L% A5 T, F' v
0 H" E) s* P* H8 m
  modsecurity on;0 ]2 m) _3 L2 H( M
  modsecurity_rules_file modsec_includes.conf;
: N* {6 e2 l: l3 d2 }  root   html;
7 W* C" v8 T- y+ k  index  index.html index.htm;
+ C% K1 C1 W) J. [% ?}
' S& I1 q' `  i  A}0 ~$ k+ o( U' f' e% M/ o
验证Nginx配置文件
0 k: }+ a  \1 r8 F/ N( ^$ /usr/local/nginx/sbin/nginx -t
8 B# y  J, {+ F2 |/ e6 Z4 Q' v  Cnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2 P) G+ {1 J, k! d- Z4 D) n- H9 }nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# Z6 N; X% y" ]启动Nginx; S; X5 I6 q/ `4 u& F" U; \
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" F* W' K, J2 q0 }# S3 @4 D" _

# R+ z& @2 j  N( t

测试Modsecurity

ModSecurity现在已经成功配置了OWASP的规则。现在我们将测试对一些最常见的Web应用攻击。来测试ModSecurity是否挡住了攻击。这里我们启用了XSS和SQL注入的过滤规则,下面的例子中不正常的请求会直接返回403。

在浏览器中访问默认首页,会看到Nginx默认的欢迎页:

[/url]

这时我们在网址后面自己加上正常参数,例如: 。同样会看到Nginx默认的欢迎页:

[url=http://img.colabug.com/2017/06/842f48f203c6c2cd30144f29b57af97a.png]

接下来,我们在前面正常参数的基础上再加上  ,整个请求变成:

[/url]

就会看到Nginx返回403 Forbidden的信息了,说明Modsecurity成功拦截了此请求。再来看一个的例子,同样会被Modsecurity拦截。

[url=http://img.colabug.com/2017/06/246ce28e95310a32f791893d4f5c55ca.png]

查看Modsecurity日志

[url=http://img.colabug.com/2017/06/ae44dcb58b8a4a0ea761317e398b3101.png][/url]

所有命中规则的外部攻击均会存在modsec_audit.log,用户可以对这个文件中记录进行审计。Log文件位置在modsecurity.conf中SecAuditLog选项配置,Linux默认在 /var/log/modsec_audit.log 。

$ cat /usr/local/nginx/conf/modsecurity.confSecAuditLog /var/log/modsec_audit.log

Modsecurity主要是规则验证(验证已知漏洞),Nginx下还有另一个功能强大的WAF模块Naxsi。Naxsi最大特点是可以设置学习模式,抓取您的网站产生必要的白名单,以避免误报!Naxsi不依赖于预先定义的签名,Naxsi能够战胜更多复杂/未知/混淆的攻击模式。

  Y& @) H$ ?2 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2025-12-30 00:25 , Processed in 0.048686 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表