找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8906|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。7 U! X% C9 ^" w! h

, k& H3 e( H. i! C$ T
9 U5 m  z* B- Z& N. T在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* Q8 o9 @9 k# W/ I, G
5 x. m1 I! p: G/ a: b  [6 x

' N; x2 r8 S6 @4 n, a& G( g& [' z. M' I什么是ModSecurity
' i0 h2 X) P0 {5 D0 L) y9 _ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 X3 @" _+ r8 K2 }$ s* x$ \3 T2 m0 a8 d& r2 Q3 V. b
0 M: k- r7 C7 h  m8 t9 {% I. z
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。/ e; [! b+ q3 \' v& ~, A6 E
, L/ p: y3 k2 f4 W' g. t

# c& v. O2 f5 d9 LModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
) v' d  B/ t$ I; W6 E* w$ c( L2 a" P/ b, x3 L0 v# h5 A

6 @% E* w; g4 L2 M$ C( i$ D4 d官网: https://www.modsecurity.org/5 R) D, e5 [0 _6 b( h* w

6 u: S$ |% ?# `* D1 \: b$ y8 Y# P+ t/ R3 E
什么是OWASP CRS! R9 P" X2 ]6 m% a7 s% U1 C
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
# d) N  L4 l: l8 Y5 F! C8 C8 H. Q( w. ?

; _( i7 p6 V+ i, w6 g; b# n- rModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 s) E6 j  `& |& k4 \
  D4 u2 ?: X2 v' g+ f/ U, J
. y% ~: h' ^6 K" Q, @9 O
HTTP Protection(HTTP防御)
7 e0 F5 H) w* v$ f: t4 O# }- MHTTP协议和本地定义使用的detectsviolations策略。
( @- p, P% Z) [& b, N
) e! N" H, Y6 p# s, F6 A, o! N$ m0 l8 V
, s& p% N8 o5 v6 h  lReal-time Blacklist Lookups(实时黑名单查询)# F& y* [) X5 o; n! t9 H
利用第三方IP名单。1 y8 n- S2 I* M, t: k2 n8 P

9 D" w# s4 i. w2 b. _. A" |( m1 A. f) y6 N6 R0 F' g) O
HTTP Denial of Service Protections(HTTP的拒绝服务保护): [3 Y8 e0 N, G. d. z
防御HTTP的洪水攻击和HTTP Dos攻击。5 X+ v7 T* @- \. ]$ d& \5 ?- A

9 x. a4 J( X# E/ {8 i
  g$ }& q* V  e! @4 m: |8 N  MCommon Web Attacks Protection(常见的Web攻击防护)  u+ Z0 B1 V7 q+ ?- [, J, ~
检测常见的Web应用程序的安全攻击。
( J8 Q# W3 o& M0 T. A3 r: _' q0 |0 p6 s+ j4 |: `
+ E, m0 {/ I& _
Automation Detection(自动化检测)
4 x; L) N1 I& w- p9 ~1 |检测机器人,爬虫,扫描仪和其他表面恶意活动。0 A% e* O' Y" \$ O, ~1 v' X' {

4 _2 R9 y2 |* L, c/ y* H; ]
0 a; d' Q" i2 Q; a- v1 [Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 F9 ?, Y7 [8 }6 |9 d
检测通过Web应用程序上传的恶意文件。
# A+ o/ z1 b8 f/ @! k; O' m0 ]7 Z* M% B2 ?: R- o  e* I
) R+ k& `: T& T/ e) R( L; A! }
Tracking Sensitive Data(跟踪敏感数据)
* G" T" J7 I" o信用卡通道的使用,并阻止泄漏。
0 K) b. n* r. o8 d" z' Z& R, a$ y3 L4 E
- r0 Q9 U9 Q* Q; P
Trojan Protection(木马防护)
4 v5 F1 F  ]0 C0 j0 Z$ m检测访问木马。5 g1 l! }1 k6 L" ~7 N8 V

6 ~7 C6 D+ @5 T1 a. k, _. l  K( ]- [' R) d! {; l0 X: M8 s
Identification of Application Defects(应用程序缺陷的鉴定)' N+ m2 F2 ]) T& f$ T; q5 I
检测应用程序的错误配置警报。) `2 z# o/ H* n4 [$ K- P0 [
0 ~- Y) S/ n1 {$ l. s+ P  _

- M2 \# P1 Y  j, }# lError Detection and Hiding(错误检测和隐藏)4 J* {4 a9 E% e9 A' G- {, V8 k7 V# C
检测伪装服务器发送错误消息。
# V- J2 [; X+ a7 [6 e
* o; f( I0 l$ w" n" D3 v) r8 x  _: [' I6 }
安装ModSecurity
% _, j7 @: o, V1 h' N4 c. h& r( o软件基础环境准备
! O1 l. I8 H6 I- z& |/ H4 U下载对应软件包; _* x: x0 ]) ]. N
$ cd /root
" ^& L; l* f& G' W2 p2 t7 [( C- V$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' y' e2 P7 N# e: h' ~1 @
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) q  c" n4 H, J. s, x3 p安装Nginx和ModSecurity依赖包
, h1 Z0 X! o& P& s7 S! ]Centos/RHEL
* z7 s$ ?' C2 s: O1 |0 m8 C' C
( ~! j! e; R2 {& b# M3 I) Y# d4 U# b; f" a" p. x
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 Q/ M1 v- k* t) Y% U- p3 X. O
Ubuntu/Debian  \6 p% ~' I' N) n1 X* T

2 G- A$ X5 d1 v! U/ ~7 l* n, R. u5 d/ z; B3 r
$ 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
8 A$ N: v8 x: k8 W编译安装ModSecurity* [: r! B8 l* f  i8 i+ @
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。& Q2 c0 {* p. ?  R1 p3 ^

4 r, _$ M, K2 o7 X, s9 ], p" m
* F" y$ R: F, R" |方法一:编译为Nginx静态模块; Q: E$ S* b; p- y, I' ~/ R2 D, `' a

6 I, x8 e+ K+ \7 q) [+ i) o2 }- b7 b
4 @9 m) ~' m: h$ m3 f* m8 B9 \8 c编译为独立模块(modsecurity-2.9.1)* v2 S5 R7 r- M7 t1 U0 g
$ tar xzvf modsecurity-2.9.1.tar.gz) G1 [0 `7 A" I2 z6 H0 B: \; j
$ cd modsecurity-2.9.1/
/ C& f9 H& J2 m0 y/ [$ ./autogen.sh' y$ T$ p/ j# K# d$ @3 }" t  f
$ ./configure --enable-standalone-module --disable-mlogc+ J0 A5 e# U' O2 d9 b7 G, J0 \
$ make
7 S8 B- L' P- Q8 C+ v" b4 W3 e% n编译安装Nginx并添加ModSecurity模块6 Y  P5 ^. n  g9 Z) e# y& E
$ tar xzvf nginx-1.9.2.tar.gz
0 |8 n# @3 {8 h6 d$ cd nginx-1.9.2- R% u0 H: D4 ~+ v. q8 N( z
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
5 C5 L1 V) ~# B4 o! k$ make && make install! r" ?4 k7 g+ v: n# }* ~. i9 {
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
% c- ?) ^2 O) m% A7 \  t! e; t
- d# S/ w5 `" c- Z
编译LibModSecurity(modsecurity-3.0)& p2 ^; m, p7 D+ N: c
$ cd /root: C0 }* t9 X) X8 v
$ git clone https://github.com/SpiderLabs/ModSecurity( m0 V( A/ \% f0 W
$ cd ModSecurity
) U$ [, B* c+ [& v- _1 l8 G" J, ]$ git checkout -b v3/master origin/v3/master
% C0 ]1 Z- n  S# H$ sh build.sh0 f; t0 y- o, B( C' r9 v" n
$ git submodule init0 e2 y; T* z( Y4 k/ |; G5 _
$ git submodule update( J: Q* V* u$ X/ B" A
$ ./configure
! d' C: `8 A  ?3 t9 v3 ?5 r$ make
. m# i3 s7 e: g. N: q) D$ make install/ o" r# @/ H( T, |
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。# ~% ]4 ?) I" Q7 w# T* m

9 w& Q$ f, t: V4 [2 B, Z: l' W9 c$ {
; r- ^2 _+ y5 H- R, w7 E$ ls /usr/local/modsecurity/lib
0 |( a/ a$ p' K; N, }libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.01 a* {" O8 i4 e$ Z
编译安装Nginx并添加ModSecurity-Nginx Connector模块% w" r: ~; W% Y! j+ s
使用ModSecurity-Nginx模块来连接LibModSecurity
' D0 h1 n7 c- c4 h: e% b5 l5 c  C/ y  E8 R7 u

, s4 I( f" q8 q: P, Q5 y. I5 n" h9 s1 e$ cd /root& L$ j$ ^; g) H3 [! @" j
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ h; p, I. G% t) E: r' S0 H$ tar xzvf nginx-1.9.2.tar.gz
' j" T9 `, g2 G3 d+ n$ cd nginx-1.9.2  V. [' ^( b1 M* f$ [
$ ./configure --add-module=/root/modsecurity-nginx/ b  h' y  K0 |' w8 H) r/ _* l
$ make
8 V% C) E; J9 B- x$ make && make install
/ d6 d3 T* @. @/ _4 X添加OWASP规则' `  y7 ~' J6 U/ _4 b6 Q- A: z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。- I$ {" U5 C9 j: d/ M( q2 j& p8 z0 x
& N# ]7 Z* v+ K6 R0 w7 _! I8 R( {! L/ |

% U! N  z# r- u6 v% k0 l; t( z下载OWASP规则并生成配置文件; |5 f2 \* H4 u& K' ~! g" t
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git. F, w% X6 u; D5 l. t3 ?
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/. `- n% p. [5 G$ D2 o. Q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ f8 O! }% Y; v8 a+ I. Y8 I$ cp crs-setup.conf.example  crs-setup.conf
4 \3 K3 F0 u- p; E# |, Y& x" `- E7 _配置OWASP规则
  p6 G! b. g" q' F$ E# o5 l/ g2 o5 x编辑crs-setup.conf文件
& I( U5 ^  i6 d- U$ H% E+ U3 U
* x" r$ ]5 l+ s- T
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
- }  e; B5 H' s! }$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. P0 R1 ^2 x# V+ F+ H- @- b7 R
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf; A% b& r; h2 b( t) u
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, ~# L+ L- L+ U; L, K. C
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。/ }" z& F. S$ c! ^' Q
7 U% `9 H2 r" @) \0 {1 |

/ N0 u; @4 W8 r. o启用ModSecurity模块和CRS规则
. V3 l1 x. t/ j& o! }  C复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" q' s: G' p$ ~* A
' s8 a& K& E0 L7 V. p$ F
( q% z; U" g9 n1 |/ u( [modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。0 u6 N4 m: v/ N# s

; s; @! r- I. {) `+ Z; h0 W% Q- F
$ cd /root/modsecurity-2.9.1/
' c: g( @' s1 a$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  5 k5 O6 D& q; K0 p4 t; O
$ cp unicode.mapping  /usr/local/nginx/conf/
  n! _# a$ {+ f( O3 \$ h将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
$ q- ?7 U6 b" \
0 j/ i6 _; I) _0 w8 d# G5 P' \3 Q" ]2 Q: _! V1 K* M0 O
$ vim /usr/local/nginx/conf/modsecurity.conf- ^" `7 M! B2 F" `8 i9 p' t
SecRuleEngine On
# x/ M) }  n. b8 ^3 H0 @6 aModSecurity中几个常用配置说明:/ g6 Z/ `& Q, }2 ~! c6 v

3 O  Z) ~, K' ], u, B$ Q( p; Z
, y. b- y1 K# z# W6 w1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。( x! o" ?$ k3 }2 x

5 ^/ Z, M: [( @( Q+ h7 F
# ]4 M' h5 i6 @/ f% y( h2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
; \5 D) ]* ?" u  o' [: u! k. `! N; K. @# @
  V" N* U# \0 ?4 C* C" y1 ~# c4 n( x0 v; d
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 s2 i: o' \; [2 g. x' J" i# O& s* Z3 T# G1 S
) ^1 T2 ?; a" B5 b2 f& {- w
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。# s- a! y- v( w* ^' x
/ h0 r, G6 V0 z3 t8 Z4 U: h
2 y* k+ M) Y8 `5 Z& ]. Z  f
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。& ?' x0 r) j. L5 t- B+ a9 C# G- k
' d. _* |2 X' i5 v9 }! P% m, A1 \

1 c! V( {1 D( r: e6 L- `3.x版本CRS
. f! u6 r0 F0 t4 y5 u$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ V- N9 U; A* |, M: [# 生成例外排除请求的配置文件
; M& J& {. ~7 r( N% V( d$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
1 t$ }, |& _; x' l# v$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 n, G: c2 r- n: }
$ cp rules/*.data /usr/local/nginx/conf
4 T- w6 e: \: I- V. K# n6 H为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。, `6 U3 W! o# W( L* A5 o1 b( H
/ `+ L$ f( \( |

, H- z+ A) r$ V$ vim /usr/local/nginx/conf/modsec_includes.conf: V2 i  O7 x) I/ v7 ?

! P  E& Z' m5 g% q$ i& i" @# R
[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
. ?  \% c) d: x! d! g+ ~$ M
9 J+ z4 q& G/ e3 c$ r) X' E/ D/ Y2 y. n/ |
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。2 h* L: h  K1 t

# \6 \; \0 N2 S
7 Z+ k- [* @. H6 g( T配置Nginx支持Modsecurity
4 I% G+ m% }7 ^! _启用Modsecurity
7 S5 a$ F% [/ z使用静态模块加载的配置方法% N2 \' g! c& ~1 x
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 w: O& v4 @$ N2 e- G- ]
5 }" Z5 r6 }9 I$ \0 e

6 P7 W* P5 @0 A' a2 bModSecurityEnabled on;
* g: @3 u, Y$ h  {( _  BModSecurityConfig modsec_includes.conf;8 @6 A9 p% ?; y. n  h" I9 L: K1 A1 W
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
5 G; f4 x( S) y( p3 V0 B0 y6 y) c8 o2 F" R4 O

& A/ L- t0 j7 u3 \7 s* k$ x$ vim /usr/local/nginx/conf/nginx.conf; r6 P! B0 o& g) b1 n3 x' |5 Y

' T, ^4 y! `/ D  L& t
, F4 E' F" _) Q& d+ Jserver {" k; e( N3 D* u
  listen       80;& W5 b3 k) s4 d* L1 }! v  X
  server_name  example.com;; @! h5 A& }: F( A9 c* l2 K

0 ^( _" ~3 J2 r$ X8 z# W6 ]$ B1 P: Y9 V8 O( }' h
  location / {% a; ]3 c" r  M- J9 i' m- o( j
    ModSecurityEnabled on;9 w/ }. v5 _" x* ?( s7 H0 l
    ModSecurityConfig modsec_includes.conf;
' I9 f* v* ]1 _  i    root   html;; K1 ~* a. x/ T' f! G4 L) x( s
    index  index.html index.htm;2 W) c1 d9 X/ [  ]* S
  }' ?- [, l* y" @& }
}9 l, K' A) F1 h) O# }
使用动态模块加载的配置方法
0 A8 J3 h: d0 W/ f" a* a# K在需要启用Modsecurity的主机的location下面加入下面两行即可:$ f6 E2 I( ]# x* F
6 t2 c. N8 J1 q8 O, S; {
8 L4 P6 r- O3 Q* r% R' w: T. p/ s
modsecurity on;0 A! k, Q% O  Q" O* \8 n8 Y7 q" e3 z
modsecurity_rules_file modsec_includes.conf;
# Z; w$ Y) I9 ^6 p5 C5 o6 ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) Y7 a  ~6 Z6 {- ]( w; t
$ s& G+ O/ i6 Y3 J3 {8 x: j

3 k0 M+ B& Y$ V. K$ vim /usr/local/nginx/conf/nginx.conf
7 S6 K2 Y* N0 ^  ~5 ^; l2 _. W# `. N5 A, U8 d& I
2 g3 u2 G2 ~5 j+ v5 |
server {' W4 p. N$ p" t# v% w; }: G" O
  listen  80;) E% y1 w. [/ q9 |& j
  server_name localhost mike.hi-linux.com;: P6 i& X) e- \  F) k5 M8 \) Q
  access_log /var/log/nginx/yourdomain.log;
! y% u) F7 M. `8 M+ ^
2 c$ D. P# }2 @. ~8 B2 K) m( r, {8 y% b+ I0 A/ J( J
  location / {: v$ {4 {% F- h' i
( k& S! {( [# O% x6 _

1 }% e0 [( C" l  [5 i! T  modsecurity on;, m! c6 s# A3 h5 a
  modsecurity_rules_file modsec_includes.conf;
; @- M: T% F  s' h: b7 o1 u0 Q  root   html;3 }; B: @! W) k: C: h( b* V
  index  index.html index.htm;" s5 l/ M1 e3 `3 D3 g8 j3 ?
}
5 Q& L, z# j( k, O}
( C4 W$ Q. t( D& i" N9 ?验证Nginx配置文件
% [; G, o. i2 a$ /usr/local/nginx/sbin/nginx -t  ~- f! {& O8 v+ k5 [
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok) ?+ g0 A- f( d+ o- ~/ X3 o+ R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
' ^/ k7 B& Y4 R8 t3 h启动Nginx" _, v* ^' i  {% h& r
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 ]" A3 w7 S/ ?2 j+ k. r
* D) c1 M0 s+ A. o: |! R% ~7 f

测试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能够战胜更多复杂/未知/混淆的攻击模式。


5 ?% Q& a% J* I' |; I7 A5 c

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-7-19 07:59 , Processed in 0.080394 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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