找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10504|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% l& Q% o$ ]+ e5 ?0 _
" ]! S# p: a  Q- G; |$ B9 B6 T% t( [# I) c- e7 E
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* }0 Z+ {  u/ F* v3 e% Z
5 D4 l1 o: c. s2 Z! e
1 M) G' S% a0 J% S5 k
什么是ModSecurity. I- F4 d. I& G0 U$ o! c% ^: M
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
: S$ t9 A4 @: q* Z2 r8 Z- d  B
) d0 `% x! g. J2 Y9 h/ T/ n" k2 E7 J
; P6 m& }' X0 W) b. E) i( ^ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 W3 h, w, M! i1 L2 b; y) O0 R$ K! y% M% m0 ?: n
9 C) I7 F) m- f0 ~  b! K: l) m
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
  p$ L' U, {6 ~
. C- I& C" m/ r% |4 Q& J6 E# N# P: g
官网: https://www.modsecurity.org/! n) @5 c) B5 n

* ]5 l* t3 R# C9 w0 x
3 i2 v4 T) M$ u9 ?' N8 ]/ w什么是OWASP CRS2 s* B7 E: C5 S
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
( K8 V& x! X: [+ F. w2 [# L0 D  a( c" s( C

' Z8 |: X) }- p' S; q7 uModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。' n8 x4 c# T, |% ]! O, E
+ I/ b6 W, v" r
& h" J- c% _8 J+ l: l
HTTP Protection(HTTP防御): s" a0 v# U- j+ I
HTTP协议和本地定义使用的detectsviolations策略。7 v, }' }4 R  T/ M

) ]- L2 G9 g; n, W+ Y; Z, ]0 x% `( \; G7 v
Real-time Blacklist Lookups(实时黑名单查询)) _9 X1 j8 H  k1 Y( j
利用第三方IP名单。
) P/ \+ a- ~; D* i1 [% g0 P2 r2 O. D' P' Z0 S4 a/ M' w, z

) E$ q2 t3 w  p9 Z8 D4 UHTTP Denial of Service Protections(HTTP的拒绝服务保护)
. l. g. E/ d  d3 N" _3 {. V防御HTTP的洪水攻击和HTTP Dos攻击。9 J: H/ D  g+ f+ t; z7 G  h' i
8 y' O8 @) u3 u& b3 K
/ ?1 }4 d3 \$ X6 k3 w1 H! _
Common Web Attacks Protection(常见的Web攻击防护)
6 y7 s6 x/ w* [& h6 y检测常见的Web应用程序的安全攻击。
$ ~; V/ Q! P, v* Z
' m' B2 _! Z4 b( A
5 X8 R/ Z' Z( ]( Y1 j# _Automation Detection(自动化检测)
& v2 ]* W7 a( H' j3 p检测机器人,爬虫,扫描仪和其他表面恶意活动。5 @4 a/ q. q+ u6 H5 R  D

, [# ]6 P( A6 u( Z; q7 i/ N
# h4 j4 J8 W) H, k+ k* r/ w; @  mIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
7 Z8 y: g- k5 ]: d6 i! }2 A检测通过Web应用程序上传的恶意文件。
5 X+ v! w( P/ _! N+ Q
+ S1 c, s7 _: Z# {8 t: Q+ E
- v5 f/ r% y1 J) l. JTracking Sensitive Data(跟踪敏感数据)
( D8 b/ d% ?9 U+ k, C8 c信用卡通道的使用,并阻止泄漏。
1 n" h( o- _( r, v/ Q2 m
4 R/ q- g  H& m/ m) [2 w2 f. g
) N' f+ u/ ?: Q' S) }3 K% b0 RTrojan Protection(木马防护)
8 w6 g2 m% g1 y7 ?: _# Z# L: o检测访问木马。
) \3 j# }9 F, F7 e9 `
" ]8 g9 h$ u: D' B' Q! y3 t6 y% E+ ~! Q/ i; m( {/ s0 P
Identification of Application Defects(应用程序缺陷的鉴定)
* v+ g) M4 ]5 l) O* A检测应用程序的错误配置警报。8 ^; n& V9 ]+ F+ s
1 \2 Q& I2 J" N3 h- i/ _1 D( K
+ A9 M) n, V+ j
Error Detection and Hiding(错误检测和隐藏)
' a7 `6 ?. C& I, h" Z) M# Z2 x检测伪装服务器发送错误消息。6 ?% P7 D8 O: R( ~, K

5 W. |3 h7 P4 U: r6 R0 ]
  E% [3 z/ p# b5 @安装ModSecurity
5 g6 {% H" o3 U" a& p) r" `软件基础环境准备
7 B! c6 ~' H, Q' j7 p8 g下载对应软件包! V' b" s- M7 |% l- R7 R
$ cd /root4 t* m6 y$ i, ]% N: H
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'  K; S" I  X! g4 ^  m" `) g
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz7 J1 K6 q! j4 C
安装Nginx和ModSecurity依赖包
8 z! X8 @7 P6 sCentos/RHEL
8 l5 b! P$ B6 R- K$ _2 f3 a! [- }7 I

9 G" p1 ]; m9 j0 U$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% B) R: d- L7 A6 a
Ubuntu/Debian2 h- b6 r9 ^9 ?( b4 N7 }$ E

) o* F% _% U. x5 F" S/ h
; o( P  X' N% C+ Y; V" 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++-dev3 ?# |# E% \6 z% l; U$ K) j. _; x) `
编译安装ModSecurity) Z. C3 m# g$ D: M
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 t1 U) O$ ^8 q  e2 P$ M  ^
' p* x. F9 x* F

; G5 B4 f/ s" A" ~# s2 k( e方法一:编译为Nginx静态模块
( c1 `  o% O! U9 t+ z7 j9 i. j, c' g0 f# H

: Z6 v' S- W& p9 T+ {编译为独立模块(modsecurity-2.9.1)
( d6 l$ w4 ]0 t8 _: Z4 P$ tar xzvf modsecurity-2.9.1.tar.gz
/ W4 f& r0 r4 n* `2 R0 ]$ cd modsecurity-2.9.1/
+ n) J$ w! c4 h& Q2 B$ ./autogen.sh7 k& C( P! b" q
$ ./configure --enable-standalone-module --disable-mlogc
* Q4 k6 A) L  E8 B  e0 L$ make& V) E/ p; E) S0 i
编译安装Nginx并添加ModSecurity模块5 p# k9 m6 C2 ~
$ tar xzvf nginx-1.9.2.tar.gz8 C: D/ _2 h! Y" j+ d" ~
$ cd nginx-1.9.27 ?% z2 \! s+ D( Y, n% L
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/9 t" b6 x2 _( A2 Z5 x0 f3 p
$ make && make install
- _1 E' N7 r/ f- r* k1 f/ G- j方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
* d$ P# V3 a5 d. r3 W% g7 B* ~* V2 C2 _

: W9 M! E  z% L7 ~$ p编译LibModSecurity(modsecurity-3.0)
5 C9 ]; p: }  C- _$ cd /root2 g9 d/ ^7 L' N1 ]
$ git clone https://github.com/SpiderLabs/ModSecurity2 A) |$ E6 |4 i( U" X2 S
$ cd ModSecurity# x/ p$ b9 D4 z& h3 y" u, W- ]
$ git checkout -b v3/master origin/v3/master& F) W% j, Y0 N. _# p# [* V8 ^6 a
$ sh build.sh
4 h+ t( U$ z! L8 X+ F, l% d2 @$ git submodule init/ F, r: G9 J& C% y  c$ b0 g
$ git submodule update- q3 I7 i9 ?1 j" K
$ ./configure' c' B1 o( t; s
$ make
+ [( V' g* H5 W7 K* k7 X" N$ W$ make install2 r: s8 K  [9 B  i
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ Z1 o$ T# N9 K1 s" f8 H. x
" L8 i) y: }" H# A- X
3 x6 {7 r( X9 K. }/ L$ ls /usr/local/modsecurity/lib/ b* ]6 L/ n! r; j1 z6 L
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
; ~" J8 W+ E6 Z6 _编译安装Nginx并添加ModSecurity-Nginx Connector模块) U  D' ^1 y; S" b( e
使用ModSecurity-Nginx模块来连接LibModSecurity
; x# b2 P* }: K  o
4 X5 d1 Z9 U4 |7 M2 q% f" V7 r  Y" j' {# P
$ cd /root
& G/ q9 [+ R+ T$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx7 a! ~/ h- o- \* ^1 d7 q  g" c
$ tar xzvf nginx-1.9.2.tar.gz2 u7 c6 E: z6 |- K. ~
$ cd nginx-1.9.2: I2 A4 g5 {7 S, j9 o0 c, r
$ ./configure --add-module=/root/modsecurity-nginx# R) l: Z* h" C3 y& I6 a8 Y( s5 |
$ make* }% L* K* }9 c
$ make && make install; E6 x7 j% ^* V0 C! G4 D9 c
添加OWASP规则& u7 _& L# S4 @  l" J; d5 b
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) b" W/ q! ]( g, e5 w: j5 p& j9 e6 G: z
0 ?9 h8 ?% `; M  B9 E. y
下载OWASP规则并生成配置文件* X, ^( _3 q/ ?4 _" p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git1 ~  s) e& A% K8 z. A
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
! z  c0 I$ X8 D) }0 L$ cd /usr/local/nginx/conf/owasp-modsecurity-crs* a$ c5 Y# ^! N" M
$ cp crs-setup.conf.example  crs-setup.conf
& ]* a2 \, Q0 \9 h1 `; Y% L1 E; a配置OWASP规则
9 G7 F: _4 _7 m# X. t编辑crs-setup.conf文件
, h6 X" L* B* ?3 J1 r# z3 g1 E
0 S2 D  G( R) G  l; k/ k
. z8 D+ H) ^6 N; }2 x+ B0 o1 h$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf7 B# F$ B9 Z+ x# C& Z. {9 }
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf+ n. H+ Y. U, d# F2 j* `+ A7 c
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
9 U* A) ~+ I' j0 ~$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
- e1 j6 T) ?0 P8 O& k( S3 t默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。' ?2 j) s1 l' x. |. M
) Q8 X, `6 j& D, t  n

8 a/ B. h3 b( [& j" r$ K启用ModSecurity模块和CRS规则& Y' G4 l/ U' ^5 X( J, H% P0 f1 j3 H  w5 y
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 G3 s: h& L/ J/ q1 d0 a, A6 o+ r+ u( `

4 X  h/ U% ~1 y# }; i6 g3 _modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
! z* N/ G$ Y9 q( A" {  S- M5 j" k% N+ k

% \3 h0 g; ]1 M* P; O$ cd /root/modsecurity-2.9.1/
3 _; k- J5 H9 J$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  4 b' r2 c+ C# I! ~$ G
$ cp unicode.mapping  /usr/local/nginx/conf/- z0 A) m/ |4 a9 x; _
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。( e, S8 q% h7 X* y* R/ J- b5 }
+ T! x1 s9 i7 t% Y7 r+ k
( W8 [( t2 m& f* {1 u
$ vim /usr/local/nginx/conf/modsecurity.conf
! w# J9 e# e- J5 T3 K7 D% pSecRuleEngine On1 @! b* A3 c$ n5 P9 R/ H3 z* `
ModSecurity中几个常用配置说明:
. _% e1 U0 E, x  |7 I' j; F6 q3 F- U2 C
+ V2 H+ c) c' f# f1 B8 ~$ t  q
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。2 V5 S* g1 x, k5 ?

3 L1 O& p3 X7 r+ a  l2 l4 [. @" I+ }# Y. x, Q: G2 M- J
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
# i. J4 J. s7 _0 K1 b: m' }; Z& |6 R

# G7 v4 Z" u5 P7 Q3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: ], U1 f1 p3 s
$ B$ |3 u# X) q; a+ X
& Y6 ~. m  S0 d1 _0 |) I1 q4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" m0 o5 a+ w$ J+ u4 f! Z; A9 }0 J5 d; S" a( ~0 j6 ?

6 x6 N% |6 }6 C$ f在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
5 ]: V$ u9 j6 u6 N; t$ k2 B
* D+ @, Y, Q8 C' j4 T, J1 ^( ~0 ~3 V$ r
3.x版本CRS: w) I& G* B9 J' b6 j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 f" f% N0 L& U) L' e) x' @# 生成例外排除请求的配置文件
; g$ z# Y' k% O! R, E$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
# ~! b+ ~2 F/ t  l: F6 n& c$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf# w  n1 A& F: D6 ~4 \% |9 \
$ cp rules/*.data /usr/local/nginx/conf# W0 e& I4 e) B: H
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
0 X6 L, S1 |) d( Q6 W
) X, Y  q! b  }; X) [" q' d- z3 [, L+ U# T8 }$ D. I+ @
$ vim /usr/local/nginx/conf/modsec_includes.conf% d1 T( }' t8 P3 V

/ `9 |- u0 t1 c- 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

$ y+ w8 g! d8 S# p- i0 i
4 B2 Q! J) k! f7 Q6 Y& r- W注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。* Z, |5 A! b1 G, d

+ j. a* ^, O( t: ]4 [. T& d4 O6 l' `9 w: I2 f$ m' W* y
配置Nginx支持Modsecurity
. h) |+ \2 z* i3 j" C7 V- P/ [启用Modsecurity
" ]/ F8 ?$ T6 Z& D; ~9 L使用静态模块加载的配置方法
: C0 A  ], x* f0 d" B在需要启用Modsecurity的主机的location下面加入下面两行即可:/ T: Q4 `; ^! a5 x# T" R
1 @& g4 E+ M# I6 b9 G# T- P
, Z+ E' O2 x* s- b: w% e
ModSecurityEnabled on;
% s- T5 G0 F8 f) s# x2 s! ~ModSecurityConfig modsec_includes.conf;
- ?$ i8 u9 Z5 O* x3 ~4 x- R修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 b7 s4 I, d8 l9 Y, Q+ M! j; l' N6 g4 t+ _) g+ e. u
* o* k6 y( u( N1 W$ L3 A
$ vim /usr/local/nginx/conf/nginx.conf
; e! @1 K' y% C" A- M$ u3 D0 A; j
' ?" ~3 A( k4 _) s+ S8 G
server {# S2 A1 [; h- C4 G4 f
  listen       80;
2 D$ R6 F9 R0 I  F  server_name  example.com;2 i$ \0 _+ P* p5 x- }. ]

9 C0 f( X4 V$ ^# T8 W  {5 w$ ~& ?& K" l' Y7 R1 X
  location / {0 H2 t* n$ J5 \. d/ n1 e0 ^
    ModSecurityEnabled on;2 M: @( V" n* y" }6 p5 \
    ModSecurityConfig modsec_includes.conf;# D5 x# L  B0 I6 Y6 p+ u
    root   html;4 j3 _& y) J/ S
    index  index.html index.htm;
9 }0 g0 {( ]) |, y  }
* |% b! k8 F/ P. h# N4 F}- m# x3 p5 f: z' u1 J
使用动态模块加载的配置方法
1 r+ O" N* P' L) z在需要启用Modsecurity的主机的location下面加入下面两行即可:: |+ x* x# o" z1 T2 J) S8 \

2 i1 @- z9 S) A; Z- y# E1 m7 m, d1 H! G0 y, x# J9 h
modsecurity on;
& S  D+ G% w9 D3 E: R" vmodsecurity_rules_file modsec_includes.conf;
3 \1 [9 t4 S5 n+ n' v* ?( q% c2 `0 h修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" [; W, B: t5 x% j; l7 s
' h' ~% k: w( g: L. w* i! B7 Z1 m( j4 ?2 J! _% I6 D5 S2 P8 u
$ vim /usr/local/nginx/conf/nginx.conf4 r4 O+ K- H' H2 n9 g  O! m

2 S- I' z, ]" E5 Q' E
% D6 l4 [1 x1 {4 L9 hserver {
; ^0 }+ R# X0 f0 G1 v  listen  80;4 u; C+ b) M% s0 u2 h/ O6 z; i
  server_name localhost mike.hi-linux.com;; d. S' |; t3 n0 T1 G* Y( |0 h5 ~. F
  access_log /var/log/nginx/yourdomain.log;
9 V  j. U2 [2 {  L9 o" v4 o0 {# }" v# O- i/ z
+ e7 H# ~5 m# N
  location / {6 N- G( K0 Q4 D5 {1 i: t

9 ?( P  g+ l- p* E6 r. T' J$ x9 s: d& n4 |: m2 U
  modsecurity on;
2 W4 o( J$ K7 ]/ m/ P  modsecurity_rules_file modsec_includes.conf;
1 k$ q: I; |" L/ Y% M2 {8 D1 F- Y  root   html;. @2 K# ^8 C. [: C9 b: l
  index  index.html index.htm;
$ E% f- x  e2 d2 e+ F}
% B1 G1 ]+ c$ }; L  C/ n. ]  O}
9 y# x' Y3 c2 g+ q验证Nginx配置文件5 n: b' O+ Q+ O5 o2 \) T; e- B7 D( R. f
$ /usr/local/nginx/sbin/nginx -t
! w+ Y- l7 w9 c' \nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# M9 ]* [& ^2 E' d) anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful1 t3 a8 Y- L2 m" m3 Y% O
启动Nginx0 S, @& U7 k) j0 g* l) G1 g
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
7 X1 j  Q, e+ Q- j
! p3 ?5 c4 m9 y# 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能够战胜更多复杂/未知/混淆的攻击模式。


$ y) ~2 J$ Z. H- G

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-14 12:23 , Processed in 0.074536 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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