找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11511|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。. l: j6 O6 p" l" H; C+ H) \

2 r0 _/ P- e, b) n; s. i# y6 d2 A, M6 ]2 ?/ S6 w7 a) d
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。0 h/ u2 v, h8 [- k& ]( T* W

1 G& p: h5 [; O( n- l  C  a% Y4 J- a" k3 L' q( Y0 y$ l: h' A+ p5 L
什么是ModSecurity
' W" B' i& G8 Q# \. f' qModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! X+ _9 d8 s, R  @9 u  K
! x* z& A0 E/ T
$ [: ?. X7 K- P8 i& g
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
  Q% Z+ H( y+ t' i4 J/ ?% Z5 c8 @& b6 y2 m. P3 O) I
7 Q2 }: ]  u& @. Q
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。8 A! N1 p* i. |! k* y
" m. }& t; r* A% F/ Z* X# Z0 i: ?
7 O  f) ?+ I5 L% |
官网: https://www.modsecurity.org/& E9 |$ o8 H) H+ W2 ?

' q- \0 d% ^. ~3 H2 k% ~( _. |+ R& Y
什么是OWASP CRS: B# N( c0 i* V1 X
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。- r& T( D# t: ]( z( v

" D7 q8 O; c1 _- b
' ?/ f$ b6 j5 g0 LModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 B0 K2 @8 t% J) E- u3 b1 {  V0 D9 _4 x9 u
3 L0 @/ ?5 V$ r4 T1 f/ H
HTTP Protection(HTTP防御)
! I2 X# J: X9 p/ b. \6 r, [  |6 B; eHTTP协议和本地定义使用的detectsviolations策略。  Z* t, Z! T, X: N4 ~
) c! w* e! w5 d3 W

) d1 p: k( B7 D, s/ P1 F" h" w# SReal-time Blacklist Lookups(实时黑名单查询)
0 a9 m$ p1 g! y+ d; J" A- P( l  x利用第三方IP名单。
& v8 ^9 x2 e  t* O: Q% J
. d7 E6 j# ~! U; |7 p4 q
7 v2 i; h1 l" w9 e. t  Z/ n' WHTTP Denial of Service Protections(HTTP的拒绝服务保护)
0 f1 |# J& D6 s, I( k防御HTTP的洪水攻击和HTTP Dos攻击。; y+ V- v) h: ~0 n4 G" ]) O
- L8 ]! |! b9 ]# i# t. c3 D
& \: m$ s( T- X: N1 }4 `
Common Web Attacks Protection(常见的Web攻击防护)
5 W) P( Z+ P- n2 l检测常见的Web应用程序的安全攻击。4 o' O& a4 ?! g  f% E9 [0 ?; L; ^
% r4 L! ]3 o. l. L$ I9 O" P9 ^
5 j3 N/ T" T0 }' Z2 a# b3 w
Automation Detection(自动化检测)6 T( O( K9 j, y3 ^. }) m
检测机器人,爬虫,扫描仪和其他表面恶意活动。
) f+ W5 P& Y1 d. r
) i8 L1 @$ D+ Q; R! q! J
5 G2 K% \' {" }Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
5 c9 N% F) J9 F9 k( v2 D; P1 Z检测通过Web应用程序上传的恶意文件。( T6 Y' K. }- o3 r, ~! c

/ a4 i! ?7 L4 W. b) ]% P9 v- s$ J3 ?! X7 J4 Z. T$ o" J
Tracking Sensitive Data(跟踪敏感数据)+ P/ K9 @% q7 r1 O6 @* I
信用卡通道的使用,并阻止泄漏。3 b3 E8 F) v* K/ j2 d
) g8 |& {. r# N2 ?; o

7 ^; H4 k( |* V; I6 V; VTrojan Protection(木马防护)
! O( @3 c7 a+ ?+ A( \1 b/ j: n检测访问木马。! r5 R* G+ ~4 u$ {# x

5 g. l+ V0 l9 t$ I( ^# P( u2 a" H* s7 ^6 u9 O1 C: W/ i
Identification of Application Defects(应用程序缺陷的鉴定)' B. A5 w2 Q0 J: r9 P
检测应用程序的错误配置警报。
* T; {& T3 k" P" M$ Z
1 r2 J, |& ]! v5 ~
9 j( Z  K$ g' K! ~& M4 CError Detection and Hiding(错误检测和隐藏)( v5 Z# B5 K3 ]& z3 l& y. _+ [2 F
检测伪装服务器发送错误消息。
: y2 m2 P3 |& v2 V; O
9 h% G, t0 c2 G/ i0 v0 E! V' B" I9 P
安装ModSecurity" ?- J" D) c) F. Z
软件基础环境准备/ k& {& B6 y/ D$ E6 ~" ?. B
下载对应软件包
. s1 j8 ?- ?9 k+ s$ cd /root
. X' X% Y$ m# H) H& A5 y/ z* w$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 V4 B8 n' n4 }6 G, V( O$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz# \; u, d9 I- \, z: [+ h; T
安装Nginx和ModSecurity依赖包+ @, |9 i! w5 d8 l
Centos/RHEL
, n& u" E( j8 [6 M# l$ g; Q1 _0 [$ M: c7 S( H( D
' _( ^, f" x5 P0 h' n( j
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
( S( ?4 v3 G8 CUbuntu/Debian
6 i/ G9 U; a0 m0 u4 f: {- O0 R6 V7 v( x' r2 S/ k

1 Z6 d# }7 V3 b7 R  a$ 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  _+ w2 j# |$ x$ e$ n& X
编译安装ModSecurity
* j* d6 N8 g* A5 B/ U3 Z# j( uNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。  J/ i2 K5 D4 D% y0 V

6 Q0 l0 y5 C( }2 u  `
* `' l: |: k$ D# h  A- G4 U4 n方法一:编译为Nginx静态模块+ w  f4 N9 n' x1 c) O0 |0 r! l
: f2 C4 E# Z3 ^
, b& l- l! X9 @' O' _! ?7 J
编译为独立模块(modsecurity-2.9.1)0 i- [- y  K' v* x4 ^. w& o
$ tar xzvf modsecurity-2.9.1.tar.gz  [% V; [4 F: \
$ cd modsecurity-2.9.1/- J/ u- S- O2 A& G) O; g& |* R0 I( c
$ ./autogen.sh  o9 U8 n0 g: R; J
$ ./configure --enable-standalone-module --disable-mlogc
' z9 |4 p) |" P1 h  _' c$ make
' i* H) \2 O" v& j. ], B. \' O编译安装Nginx并添加ModSecurity模块/ y* d- G8 ^" q
$ tar xzvf nginx-1.9.2.tar.gz
" h5 T7 h# z' R+ I$ cd nginx-1.9.2
8 B- w, b9 t2 k3 L3 s( `$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
8 X2 E, q- r& i$ make && make install
1 N. T6 t+ A3 d$ h" h  |1 R方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
) \0 D  @, `! @
; ?$ R8 K) F/ I# u+ B. X' B7 e; ^
编译LibModSecurity(modsecurity-3.0)
0 S- t' y" c9 F3 w3 D$ cd /root
+ O4 P, a7 }( J6 P- {2 E" L2 t0 @2 h$ git clone https://github.com/SpiderLabs/ModSecurity. o# f* E* _% C: G7 f' A
$ cd ModSecurity
; f: @; o7 Z# s0 ~% V  A4 i' [$ git checkout -b v3/master origin/v3/master# w0 n1 w6 s  N2 h5 v
$ sh build.sh
9 s* ?, i9 c% x6 \- [$ git submodule init; a9 R7 ^, ?, P3 D( T
$ git submodule update
2 k/ a+ R9 j& B9 I9 q- S" g$ ./configure$ c1 ^; E* t/ @8 I3 G5 Q
$ make
4 S5 a; x! }  e; E5 V' l$ make install
& n9 b) g& l1 }$ S6 qLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ Z2 C2 d0 U! s
6 k+ J/ `" y9 q7 J
5 A: R5 r& E0 ^. H. U! @
$ ls /usr/local/modsecurity/lib* c# w0 q, `( t' }8 k5 X
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
- u$ P" r$ E& c4 K# c2 t编译安装Nginx并添加ModSecurity-Nginx Connector模块) f8 Z( z6 i" ~) L
使用ModSecurity-Nginx模块来连接LibModSecurity7 Z5 D) j5 d* i) C: q5 w1 q2 h

6 M1 f6 ]$ k0 |/ ^% J$ V! K
3 `/ ]+ F7 i- h3 n4 D. W. y$ cd /root8 _2 g. m# D' y, }- n
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
) \( z- b: G; F# m$ @$ tar xzvf nginx-1.9.2.tar.gz
- X5 Q2 l5 t5 e0 [$ cd nginx-1.9.2
) }4 Z- C* A9 b$ ./configure --add-module=/root/modsecurity-nginx) }9 S; M% W& u: }7 G% c
$ make
/ u  M; B) ]# i4 x$ make && make install
  T! y* a+ z' p- I6 `添加OWASP规则3 Z$ }4 ~, r/ j7 B
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。1 s" N) j5 i0 y0 a2 e3 y, W

, q/ c) H/ u: r7 F* T4 Y, V, S$ U- S. q2 S
下载OWASP规则并生成配置文件8 u7 U' D5 T8 t
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git8 S# u, ^, Q" f3 r
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
$ i+ [1 Q/ N5 Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 O1 D: {! L5 p/ ]7 l/ h, m
$ cp crs-setup.conf.example  crs-setup.conf
  A/ |& k6 t% Y配置OWASP规则$ L! F3 ~' u! q3 @/ V
编辑crs-setup.conf文件
' ?6 Q6 K7 |1 h) O9 b( g
5 }% x; h" c4 P, t! A# q" e+ j2 K
- e5 r6 w# }$ v# f; t- V$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 ^9 i3 j' O8 R; v
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf6 o5 J5 {+ M! I" N  C% |4 ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, @4 X9 g$ s- I. ^+ Q  D1 ?* h9 w$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf9 w- T: I  V% Q' {' `0 y7 i
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
: M4 Z. T6 ^+ K8 ^' O
3 k3 i' Z2 N) w/ A# d5 @, b7 p7 B6 Y% k# r- ?" q0 {* w
启用ModSecurity模块和CRS规则
2 [: @' Z; a8 z3 o复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! K" `, [3 y- N

0 M/ `' W4 d1 P! x
2 O" b. ?0 F1 J" [modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! _' d4 `3 H' q( n+ _/ d( j" x' S

: c$ C7 h! D! P+ s' F. `
6 @0 y- `: A4 ^* E$ cd /root/modsecurity-2.9.1/
, G! l  F- k* [6 H$ `$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  # a' I! X8 U+ _" n& l
$ cp unicode.mapping  /usr/local/nginx/conf/
0 W/ V' u' X) j+ L9 B: T将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。/ |1 Q, E/ i; ]% G- ?8 X, V* s
7 S; i. j' E7 h1 ^3 B2 j

, X5 o' b5 M0 v: r3 ?$ vim /usr/local/nginx/conf/modsecurity.conf* s  D6 D! r/ N' N* y
SecRuleEngine On
$ @3 e$ v( F3 \- j$ R. |ModSecurity中几个常用配置说明:
$ U' \# K+ M$ J7 H3 M5 g, E/ W4 t4 r6 V' j) Q6 i% `

. R/ n3 @7 B$ f8 K1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, k4 ^$ q0 o7 g6 P! c* H$ F/ Z
. \+ d% ^8 v% x
( t0 L7 p3 }* A. I4 p& p+ y2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 p* s4 _9 \- S4 w: @# }( H7 G9 t: n& t2 }9 k$ ^" `

3 g: R/ l3 l  b0 p" f% {3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! R9 v6 Z, Q' z7 g- c* A# k- S" J( ^

; B4 b* @8 b6 W& R+ [- r4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
) p, C7 U4 t2 x1 y+ C; b6 q, v0 c. d) U3 i- C3 K

5 X5 R$ ^  F) ^$ c# G, k! A$ m在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ R, t  R4 N' M

. q: s2 L3 \8 i& c6 v. R! }, w* o1 l
3.x版本CRS8 H6 d* W7 b/ _" i1 {- s
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% w& m9 O5 O8 g7 U4 K% ?% w& J) T# 生成例外排除请求的配置文件
4 s3 |1 Z4 x+ u6 l% ^' h3 T6 f) d6 Z5 O$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 S7 A4 y/ C/ |3 j
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
0 ^& W& ^. {2 K' ^$ cp rules/*.data /usr/local/nginx/conf
) @6 Z: \" S, O% r% T9 o( J6 M5 z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
6 s! @+ m1 B% _9 \2 {
; c* r5 B4 Z2 a5 U; v
; q% U+ s. g% e$ vim /usr/local/nginx/conf/modsec_includes.conf7 L7 h+ I1 C# t) f& y

  |9 ?- M" x& u2 ~- n% J
[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

, I$ c$ |; M: I! A' L) I5 J0 O+ k+ Z
$ `7 H, Z( J- n% n  Z6 Y3 |6 y注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' q' U  B8 ?; B8 V) m; T; H& J2 M0 U" V6 F: F+ w

* M/ V! p# Y2 A3 M+ i( T6 Y配置Nginx支持Modsecurity! `# n' x1 T! F
启用Modsecurity
3 z- j6 q* i9 k  K使用静态模块加载的配置方法% \, s0 V9 d' v9 i8 P
在需要启用Modsecurity的主机的location下面加入下面两行即可:& U2 ^. k3 A" b9 C4 E' T# P
( z4 a- L6 x8 r: S# f4 m% q% O

  I& g% m( v4 h6 D$ V$ FModSecurityEnabled on;
  ]3 w, B3 \" j( q8 g2 \ModSecurityConfig modsec_includes.conf;
# Z( A5 b8 [4 g, H1 X修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 J4 v/ X& F1 M6 Q

4 m3 ~' N! l% k* s4 `/ y% D  ?3 {
$ q) J" S3 J. l* B  y4 c2 R& @4 X3 l$ vim /usr/local/nginx/conf/nginx.conf
: B2 [* Q* X% w& W) F  J6 G' o6 x
3 j( F& l# F9 d4 i% }" Z6 y# V# }& s) n: ^7 c. R
server {& b0 l% [5 t' A! G# z+ r
  listen       80;& ~" g) `& N- T( p
  server_name  example.com;
! R7 S$ u5 p. H5 C5 X; o2 M
" k8 _% u2 Q: C
. G* Y1 S7 O0 x  location / {
$ S7 E  ^; y& O) e- n* Y" t3 V    ModSecurityEnabled on;2 [( Y- \9 l" \6 z, }
    ModSecurityConfig modsec_includes.conf;  A8 x9 f" r2 C; z+ X0 I2 p5 J2 C
    root   html;" M3 z9 P) {$ Q
    index  index.html index.htm;9 A3 }3 @+ q3 K6 z! P8 j0 z1 W, O
  }; l6 s2 o: A/ k9 s' n( t" s" f
}( `2 n. l% F' ^, z5 y( e: r6 u
使用动态模块加载的配置方法/ ^6 z8 q% T# D! ~8 b0 r% `
在需要启用Modsecurity的主机的location下面加入下面两行即可:
( F( n. Z! ]# h' u& K# D- N/ F
: w# s' k% u5 U6 q* C8 b3 \% k% B
modsecurity on;
0 e$ Q- c: }; V" lmodsecurity_rules_file modsec_includes.conf;! x9 D1 I# Y2 U, Z5 b+ K* P
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! |, n- T; f. n0 V- c+ ^0 R
5 h9 r$ S. Q; I* E

* o! |+ h1 D0 M9 l$ vim /usr/local/nginx/conf/nginx.conf
1 Z: B7 ?4 A/ z8 B+ n2 w: I2 q  z' {  ]1 A1 W% u4 ]: |+ S- N1 _& I
; t' h- f( H" D( _$ L0 d1 Z1 Y/ I
server {" T$ W) s3 w# {" z# z. D
  listen  80;
) r: s# d3 w- }. n; Z5 r$ r  server_name localhost mike.hi-linux.com;8 D: d3 ^2 j! u' U. u2 M5 I& o) S% Z2 \
  access_log /var/log/nginx/yourdomain.log;' s' n0 e; Q' ?

+ P+ n, N7 y% H; l) n5 Z, j5 K2 f# D- w6 c7 o
  location / {
9 S( r  n4 v& b, ?' v8 E( F
4 @$ r9 q/ D0 u$ P1 c, w9 h/ U- Y/ F) I! X# @
  modsecurity on;/ d. F9 e) Z0 i1 Z
  modsecurity_rules_file modsec_includes.conf;! Z0 J; q  Z0 g4 O0 E2 c6 O+ G
  root   html;6 l' \2 ^; R- L& b
  index  index.html index.htm;5 R% \( }/ Z7 j' ~
}
  z8 G/ |+ j7 U0 S}5 U" C: t  q( A% [
验证Nginx配置文件! G0 n, g6 I5 `* I2 R' h1 i8 V7 j
$ /usr/local/nginx/sbin/nginx -t
& V. X4 c# f, H) b) u' A" y) \nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok# C' Y: w" k" e
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5 m5 y: H, S5 Y+ q2 c启动Nginx  g, _( ~. v% J8 X8 c: k0 ?' e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
+ ]6 C, }7 L& F! z- s
5 O* U* r& a, ]& p) O+ ^

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

4 [3 v, R- E- W# Q# I

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-19 04:41 , Processed in 0.087455 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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