找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9948|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
* D* C: x0 f8 l9 w& L) u* b4 Q' J' n1 w. n$ S8 v7 Y
6 R' F# o4 o: j7 a) V4 K' n
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。! H/ t6 v. s' i) f) k
: G  ^/ J. O7 q) [/ W* B
9 w# w0 @1 H$ z, _6 ^$ E
什么是ModSecurity
0 a4 b9 j+ [4 Y- C7 zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 }& q- ~* {3 Q% c
. Z; s. O$ X# x1 E3 b7 D0 |6 U) f; ^  m
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。( o; ^$ B/ J! j' T. I' T
% _. H+ S) m; z, `  d; {# s
: j# ]3 e4 F, c" Z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
& q- l9 ?/ g; T" U+ X- p# {) b- X# [4 W5 y2 y6 Y

8 S+ I  _! x- s官网: https://www.modsecurity.org/' f* }. z: H% f) Y. s  ~
4 p: ]# ]" w6 I* F6 @, ~
* b+ {# O$ [7 w" r. ]$ R4 G
什么是OWASP CRS9 p6 B3 Z: F7 a3 T
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。9 `* h% f; a; k, w0 E8 s! @+ e

) ]9 i3 l1 |& t, c* \
+ [# T3 Z% B0 X1 rModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。. l, E% r8 G# s) a: g; Y; T" u

6 _& x' L$ A2 u6 o  b
5 N) G9 {; H+ H1 v1 C" f4 JHTTP Protection(HTTP防御)1 @. e0 X6 X" j9 I
HTTP协议和本地定义使用的detectsviolations策略。5 F1 ~$ ^- v2 ?, D; t: e
7 I+ u8 C& }& o6 i
2 i! [! E0 v3 u5 L# Z
Real-time Blacklist Lookups(实时黑名单查询)6 [* e# W! ~0 E* I5 ?8 Y& T
利用第三方IP名单。8 i. }" D6 [' ~' _& ^
' @% V" _+ p3 ^6 N+ }. B3 h
1 h3 a9 J' i- X# P: W- s1 ^& k- H
HTTP Denial of Service Protections(HTTP的拒绝服务保护)8 e# m  F# K. {4 u. O* O. ^
防御HTTP的洪水攻击和HTTP Dos攻击。% k9 D# G% x4 H; ^, W; H' Q# a. Y
' @( D" ~: I* b8 c! E$ P) j  h- `

7 Y2 q2 m+ `& L4 S" KCommon Web Attacks Protection(常见的Web攻击防护)
2 _+ g' p/ I) k6 U8 k3 m检测常见的Web应用程序的安全攻击。
+ u+ A& C$ V: d$ Y% r3 r# ?' P& F) Z3 i3 d5 u+ Q2 u

  B& W9 d, j2 c! P2 f3 aAutomation Detection(自动化检测)
0 x8 R0 D8 H5 _6 y% v! _5 S检测机器人,爬虫,扫描仪和其他表面恶意活动。9 S6 E9 i& P/ i& K& q. U  K7 \

$ k+ s; i3 r# l4 l  A: ^: G3 u2 f. h3 X: }9 u) p4 A
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 z$ {( o  ~! e4 z! {2 [检测通过Web应用程序上传的恶意文件。/ a! _# n2 ?! n  I1 k3 `
5 J3 H$ L, q0 r3 v0 f+ Z* b
, Z9 ]/ l. X, p/ v
Tracking Sensitive Data(跟踪敏感数据)/ y) K9 O9 i: K3 u( d% N
信用卡通道的使用,并阻止泄漏。
  v: e; e& y% D5 ]9 o% F7 N7 K2 \# [0 ?0 S5 w

: V3 f9 x* D, H0 JTrojan Protection(木马防护)
+ q  ]+ S, V  v/ I0 ]7 M检测访问木马。' v  A2 H) h" @: D$ ?2 N& L

9 C" `$ c# G! Z2 f' b! J+ G; Y* @- q$ c$ r: B% f+ o- W9 E
Identification of Application Defects(应用程序缺陷的鉴定)) w8 E. j4 z' X/ J  ]
检测应用程序的错误配置警报。
/ n; z% Q$ }" I. d# `
% P: U2 g0 ^0 L* h  {2 Z7 b$ f) I  r8 S3 }, g$ e. a
Error Detection and Hiding(错误检测和隐藏)
" o3 w; n2 f/ L( @+ ^8 P检测伪装服务器发送错误消息。0 A: E8 X" i5 U/ ?
" B% v  a& O5 j8 }6 r/ e# X
$ }9 y$ r$ v" K5 w
安装ModSecurity6 i5 k- X5 f  i4 D+ a1 P% F0 P9 n
软件基础环境准备
6 }. c% {9 l; ?8 a下载对应软件包+ |8 x, V9 a" p' c
$ cd /root
/ N  o* T9 k* M# X0 e1 B$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
4 }6 O- f  I. h1 l5 e$ 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' G7 `5 c- a) @9 H安装Nginx和ModSecurity依赖包
6 ~0 L# g2 i% G6 ICentos/RHEL
; C5 |$ l: e/ c
% r) `- a$ E( I7 m6 E: g. d
* S/ C: \6 Z  x/ w" w$ F. Z$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
" [: C! ?, L) ?+ c% {( RUbuntu/Debian3 E) x1 Z( f# C( O6 Q

% i" ]& j0 x2 e9 H. M9 g: j0 X6 p, V3 p% ?0 g4 v
$ 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++-dev6 X  N) N/ t  c5 o6 @
编译安装ModSecurity
* l* t. z9 E- z( j+ S$ X! ?Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) \  [3 k% q" c3 L

4 V6 Z1 F, r7 L4 {0 P. s- j9 Y# g$ a( J4 O; }5 o
方法一:编译为Nginx静态模块* G3 S/ E. W4 t. d$ n$ V" N

* {+ L+ J8 E2 g7 @. ~, D; S
3 D' A) v  M" y* U5 C# e编译为独立模块(modsecurity-2.9.1)5 o5 z5 i" w& J1 k
$ tar xzvf modsecurity-2.9.1.tar.gz
" k. K: c: W7 N. B9 p$ cd modsecurity-2.9.1/' [) Y) G( t+ r" `
$ ./autogen.sh6 a' _$ Z; g0 ]- S% c! \
$ ./configure --enable-standalone-module --disable-mlogc. }  P" \/ [) `  W9 w& h! k( A3 @2 E
$ make+ A0 N. ]% G. l
编译安装Nginx并添加ModSecurity模块
" Y& L4 \( `6 p) D! @2 f$ tar xzvf nginx-1.9.2.tar.gz6 ?0 i/ i! E5 Y" y
$ cd nginx-1.9.2" }# ?) n, c5 j( {! b1 K
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 R( y. e" @. q' Q5 b0 d& ^* y8 j3 i$ make && make install
/ f# Z8 v: r; z方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 b8 w0 `. a; k) Y; V; u7 v2 `; z

0 W4 A- H  s! m4 T, s/ g' r, w# c
  U2 z7 Z" T* B+ h编译LibModSecurity(modsecurity-3.0)
% r2 m8 _3 X8 M, v& N- s, e$ cd /root" ?& @2 g& G% y: D9 ?$ H
$ git clone https://github.com/SpiderLabs/ModSecurity- m1 }4 G3 ~, d2 t
$ cd ModSecurity4 E/ }9 i" |: V: v1 G! u
$ git checkout -b v3/master origin/v3/master" ~3 T+ A( Y2 I+ H& r9 k
$ sh build.sh
" s9 g7 q; X. s4 N7 {. r( `$ git submodule init4 Z4 n0 X6 [9 L7 p  Z5 t  n4 U
$ git submodule update
8 p. H5 N# s; n( q' _$ ./configure
" H# n5 o7 {0 c0 c( Z; O3 m$ make0 y$ S# }9 Q+ b! `/ M$ u
$ make install9 O: H: B+ f1 c/ Y
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 q. @' W* O, Q
6 v5 Q! h: J3 k. j8 p
- `* m) n) b" j# E6 x$ ls /usr/local/modsecurity/lib
9 J9 t* n2 m  A) plibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0: e& I2 `! @2 y) M% \
编译安装Nginx并添加ModSecurity-Nginx Connector模块6 A+ Y+ a7 T* m; b3 L& k. i+ h
使用ModSecurity-Nginx模块来连接LibModSecurity
" N! ]3 z* X- s9 X2 Y  a- e+ P, j) w/ Y
( X5 n+ w" R/ L
$ cd /root# X* D& L8 c7 _8 ?/ z0 {; c
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% V! D" v9 H! ^# ~% y% }+ t0 ~
$ tar xzvf nginx-1.9.2.tar.gz+ @& O# V* `& w. |  x4 x* l2 k
$ cd nginx-1.9.2
  {+ }4 D% O8 G" D7 N$ ./configure --add-module=/root/modsecurity-nginx; I$ u* J6 o+ ~$ C& h9 S2 N
$ make
/ e% N" H' ?1 w2 O8 `. j9 ?/ i$ make && make install
6 V; ~2 n' g* i5 v添加OWASP规则
+ Y- D7 f% U* M* V% P4 @( e" c2 q6 R8 eModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
, v) |9 z+ b2 v0 M9 j5 A9 l' l+ N! T- ]) s0 b+ b

. S1 N* o5 J" y' Q5 p下载OWASP规则并生成配置文件6 a; {! M9 v3 ~6 G! I2 h, B
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
. A, v0 n6 k; h$ M. B$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
/ o$ L" M& H, A7 A/ s$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 q* v% i, X/ Q) n- o' K
$ cp crs-setup.conf.example  crs-setup.conf3 q0 C4 h2 m) W: l6 l. |) ?' G7 L- k
配置OWASP规则3 F& e$ G) D; b$ I$ p1 A" S6 k
编辑crs-setup.conf文件4 ~% g1 [4 X$ t- U" p

4 W0 X  O- t) }! E
: k6 V/ Y" U4 L5 E, X. U  g$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" E1 C' Q! M2 u$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf7 X6 H  a0 N4 T+ h
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: b+ |  h* z! w( K$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ R' \% O% \0 b' j0 d
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
: _# o0 q; C- Q; a; k6 ?
8 j8 |& g. \' k% M$ v9 f: j0 @8 b) \
启用ModSecurity模块和CRS规则
- z5 z  Y  {; E! k) V复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( }, h6 d2 Z% x2 I3 T5 y. {* w8 _' f0 a

8 s1 e  d. l  |: ^8 fmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ H1 g- r/ |$ H) [; ]" p9 D8 `! l: [: ^( ^. `; Y: R. E
0 U& o6 c2 W& H# i9 Y
$ cd /root/modsecurity-2.9.1/
7 C. c, w+ e) H3 Q3 W% ^" y. h$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  / b) }: s4 R* {/ v$ c
$ cp unicode.mapping  /usr/local/nginx/conf/
, R& j3 ]# o$ s! W  Q将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 F1 [' O$ @: H9 b6 M! `
2 ~" Z6 |6 I3 m

1 x, }0 L* j; g2 G+ I4 p; J$ vim /usr/local/nginx/conf/modsecurity.conf5 _7 T1 G! |. I- M/ P
SecRuleEngine On- c4 m* y4 r* h& y! |+ S
ModSecurity中几个常用配置说明:( S7 A# `3 S$ q7 p' {

# Z2 O/ A5 T1 c6 q3 L- i% y$ ]  u
- {/ r* f( w6 U; I* K; O1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。5 k" C4 V. G. [3 e
& }, B9 u" t' ?! q" t0 k) c

8 x$ R" X5 ~; ?( `; F2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。" S+ ~; s, ~% F$ g* x1 [( R

4 ?* O# \" S% ~- ]/ n( R. L9 I; G" U3 U
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。6 d" ?$ h" n5 A6 Z, h

8 `8 }$ `! V+ j! S
5 m( \+ ?2 B* ^9 o4 D  _9 T4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# m, \! z& |% h2 a3 s) j- q& Z0 Y
- W% a3 k$ l  P. c8 s# x$ i! [  F9 D7 ]
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
7 f5 X& o4 E! u& W: K( ^. O
/ I! Z2 h5 H4 [
) X' G2 c) G0 R4 o. t# T6 O3.x版本CRS
3 B7 G; P& {0 ]. W1 C1 ?1 ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 E' b0 X; n8 b$ E
# 生成例外排除请求的配置文件
6 I  }, Y8 O2 }9 n% e. I# j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. U  P) }  }" y5 d+ P$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
* x5 R+ R; @( a2 l$ cp rules/*.data /usr/local/nginx/conf7 K# r* K- M& s. b/ O0 l$ t, ]5 z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' N8 R4 b, z% W2 {$ P; Z

! f' o, T$ r! A0 y) C+ M! o+ r/ f" E  J( V% @4 m; ]: p
$ vim /usr/local/nginx/conf/modsec_includes.conf$ [, W. H9 Y7 v

* |& X# {  Z" s9 U& h: F. w7 D( _
[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
* w1 l# h! m. W: k) p4 i  I
3 j) g; c" e: J6 v% v. b
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
! \8 U$ r; t& _/ k6 `) n' I& g" z4 j

' k1 P" V9 V+ p5 r1 m6 M1 V配置Nginx支持Modsecurity
3 }/ _+ A; u/ _4 x启用Modsecurity
! t2 s8 C* O2 `0 e" k& d1 X" P9 n9 {使用静态模块加载的配置方法6 j0 _9 X0 N, c/ E! p0 \& t
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 L7 [0 W5 l) [2 F

' f8 u8 \/ l: d, g8 j4 c7 z
4 y; `0 q9 H6 {6 @! i" g1 vModSecurityEnabled on;7 h  L' H# S5 _7 ?+ ~% j' Z1 F
ModSecurityConfig modsec_includes.conf;" [6 K5 a  u( N
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# [/ ~$ q' W9 s3 ?0 j
4 i; ~. s7 L8 ^
  g1 f( L6 F: n$ vim /usr/local/nginx/conf/nginx.conf& n& L: _6 P' E( m
- ~1 ], g7 I0 {/ ], \3 Y( `
& v; t8 n# r- ^" g6 H
server {' E7 V0 \# B& v" o0 E
  listen       80;$ e( o  L/ e% b0 k2 ~- k3 J  J
  server_name  example.com;
$ f5 L, I; C- o( J: F+ J6 @7 N% u1 t& S- _! L6 P$ A

# R; v! }  w5 `% x5 P, G9 ]  location / {
# I4 ^6 N7 V( Z8 S    ModSecurityEnabled on;
' W% I/ c4 H: `    ModSecurityConfig modsec_includes.conf;9 G( D; t  h$ A1 o8 j7 u2 L+ Z
    root   html;
: R; R$ }0 k: \    index  index.html index.htm;
& s+ q, A, ^; x; O! l. }! t+ {* K8 ^4 m  }0 g0 X4 u4 U  o& C9 K. e& Q" `2 R
}
# V! ~" M* s( T! S* T( H使用动态模块加载的配置方法# A, k) _% B  k. L3 x  p6 Z) S. T
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: c! ]* J5 U. m, A' A+ z& A/ C% k$ \! |2 w1 A

( A$ }1 G1 F- K, rmodsecurity on;
' ]$ {5 n: g! k# Q6 T6 d9 Rmodsecurity_rules_file modsec_includes.conf;
7 T, r& V) a+ o+ `) z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
7 p# t( m9 t4 o' O; \/ ]. @( s7 s- |
  c& V2 w$ D, }  q0 @
$ vim /usr/local/nginx/conf/nginx.conf! j6 ]7 Y' M: n( c: q# H5 B$ o

' O# T3 [: \6 [4 s9 ^# Y/ H; ~; |2 f6 `, f( T/ Y7 s$ S
server {
0 x4 L; y  R+ b: ~! `" T) Z  listen  80;
( {: Z8 e& V9 @" m: [  server_name localhost mike.hi-linux.com;
) q8 `, ?/ y  B0 M' y  access_log /var/log/nginx/yourdomain.log;
  B6 a: L/ l% c+ K8 E8 Y5 C
) Q* u; U% Y1 l, j( O6 _) K( z; F9 q$ G- s4 g/ v4 M: N
  location / {
' |' ?8 z. h, c0 G$ X0 B$ ?0 q  S0 q/ p

. ~) L4 @& ^6 |4 e  modsecurity on;
6 A/ c1 b$ w# W9 z) a- j0 E; y7 U  modsecurity_rules_file modsec_includes.conf;
0 j$ y: r$ i; j  root   html;
" M) ]" Q: p) V: e( d  Y- x  index  index.html index.htm;
& g* x4 _# B1 w8 D7 z}
- d' y. J8 u$ P, X6 N% A  b}
; q+ c2 I  S0 r0 z! ~$ d验证Nginx配置文件; |( U+ |0 e8 x) r2 o, N
$ /usr/local/nginx/sbin/nginx -t
) D' c8 j' B0 Onginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok5 u2 a# A6 h& v0 R3 _( e$ u
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful" g  b+ c3 s' L" z' E& t/ }
启动Nginx' h& ^# a, z4 i  o) P, y$ K
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 ]$ t+ J; M3 M# F8 L& |1 ]- i  n" [2 Q9 M: w5 i  C7 q) A4 J

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


+ g# F! `% z* o2 ~

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-15 08:53 , Processed in 0.058157 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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