找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11984|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! u& w3 g, i- Z- _0 Q9 ?# K& s

$ i# g0 H& Y# d! Q
" q" r$ e8 T- p+ x: Y. L* d在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ x3 c" h8 r7 g2 T. P2 o- j% b' P! Z

4 q2 Y- G1 n# P, ^+ G什么是ModSecurity! a; s, Y! K1 J; \
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
6 l3 q9 z; g. g* f" n  }/ ?
4 m# N$ o( O0 w" D' L% T1 `* O
7 k/ P' J7 }- N) ]; @7 jModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。; K( Q/ p) u) Q' z

! n7 h, \/ v: E) |
) d, t9 K, D  r6 CModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。, n% L8 y- U4 x' m6 D5 ]" c
1 a# K5 h! i1 _" u
' ~  V, e4 c% j) E
官网: https://www.modsecurity.org/
" ^" b  G7 x+ O4 A1 G/ J" H0 c) q1 K& X$ z% z# v4 X

: h) q1 ?% v6 L6 @7 j什么是OWASP CRS
$ ]% L; W% h% B0 d5 C% R8 V; xOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
: g7 W7 |  Y4 S! ], Z* ~
. K; y  W) U- R2 }5 o" Q, R  M
! I5 l: q1 ~2 W3 X- q( O1 A4 ~ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 t6 _: p; n. B! y, h
8 Y4 M1 C0 @$ n" b1 _9 [7 O4 Z' {. G: T$ h# [$ h8 {
HTTP Protection(HTTP防御)! d8 X' A* [7 M, c) ~
HTTP协议和本地定义使用的detectsviolations策略。
! w- N- H3 N" V) _  [& z9 k0 M
7 b; g, ~1 m% w% t
8 A  n$ u3 T3 M5 K% K3 {; _Real-time Blacklist Lookups(实时黑名单查询)" O! C0 I" q- ~# q
利用第三方IP名单。
6 S8 v  a( {: Q$ F, [5 s% E, T
" e9 u/ g# {9 e6 G' k  h) C: d' L4 a$ @2 F- b8 |
HTTP Denial of Service Protections(HTTP的拒绝服务保护)8 P: T$ J: y  d+ W& Y7 v
防御HTTP的洪水攻击和HTTP Dos攻击。
& I0 ~2 z7 |6 o5 I! i
3 A1 h% x1 l+ c3 M! f. v5 I  c) Y9 N  V. t' c8 [2 i
Common Web Attacks Protection(常见的Web攻击防护)
  d9 _1 ?, ]6 P# M1 y检测常见的Web应用程序的安全攻击。. _7 w' N  B1 P7 D
- H( r& \4 T& ]4 S  i7 ^& T

7 y7 ]6 ]4 y4 n! a/ r% V" rAutomation Detection(自动化检测)
7 B, b) X5 t$ l+ [6 F, D检测机器人,爬虫,扫描仪和其他表面恶意活动。7 x0 W  m2 P, U9 R) x

$ Q0 j5 Z7 A2 }, V$ P# i8 u! P: O8 s7 f8 r4 n
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
4 T4 F9 g9 P" C: [' ?% r检测通过Web应用程序上传的恶意文件。4 ]; S+ @+ q2 a# g* j% e: h, M- S

1 v) t5 l4 m& B. g. S7 I6 s5 Z" D. ?1 M" S$ a$ [4 M- z. [
Tracking Sensitive Data(跟踪敏感数据)6 P* X) A  t5 ~- K: F, k/ H: y
信用卡通道的使用,并阻止泄漏。& g( w6 `; u/ U# }) ?; \
4 K9 L, j- }$ C
# U" w! W( x8 g# [/ D% M
Trojan Protection(木马防护)" C3 _4 [( S& b' k* G+ P0 ?
检测访问木马。, w+ t! G$ A4 ~9 R$ E) S/ I
! j/ }1 c" w+ B% a4 v& c/ J' m
- j  i" s( Z6 F- h# @* `  g0 p
Identification of Application Defects(应用程序缺陷的鉴定)
/ m2 ~7 a7 L: D- P3 ]检测应用程序的错误配置警报。- b3 x. M, H2 u2 _* A; j

  u1 |8 _+ p# S; y% B. I* `% C* p
3 w$ n" S1 o0 W9 t- WError Detection and Hiding(错误检测和隐藏)
. p; z+ S$ v! U5 I; w& S/ j1 X检测伪装服务器发送错误消息。
1 N" s8 U; F4 O8 q# @
8 k! g" N5 }# V0 D& r! [* C  Y& F6 D' C" X' [% d2 \
安装ModSecurity1 t6 h! u- s! a) u- z" C8 `
软件基础环境准备
; p: ]6 C6 _2 ]- r5 g5 Y下载对应软件包# R0 R: _6 t: ?+ n: M
$ cd /root
- `$ R2 r( n+ h4 U# a; D9 t* M$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
+ O9 i2 Q6 M4 s4 d) K$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
+ V1 e. t4 o0 n% ~安装Nginx和ModSecurity依赖包
) R8 u5 J7 b" SCentos/RHEL
* y# i0 n# Z( ^+ S# s# p* E, }& }* z- h9 |0 c
( Y- g3 m# h8 T$ s
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel+ v* ^' C- C$ _) y3 t+ G% @$ I
Ubuntu/Debian" G# q; f4 H# }+ l" W8 g: r

% Q9 [6 t( A/ q' R8 k4 K" v. M- C
* y+ V. E* {: n  ]" ?1 V0 Y* ]$ 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 m- v2 w' ^* j& n! }1 }3 b编译安装ModSecurity
+ q1 k! t9 e2 k, n% ^5 mNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。9 a' U; \! S) x, _

  |) |" A: b+ o3 O. v9 g: U
! j; L/ o' S1 K方法一:编译为Nginx静态模块( Y1 B; N/ _( @4 ]9 B( H

4 A5 j/ V* J  L! H2 ?( U$ ?5 P* M* }, U8 B) z1 m" R
编译为独立模块(modsecurity-2.9.1)' P2 @/ X) s* h7 _" b2 e
$ tar xzvf modsecurity-2.9.1.tar.gz
& K- @4 ?& F' W4 S$ cd modsecurity-2.9.1/
* G9 {1 o: P6 Q! q$ ./autogen.sh# i6 x* Z0 V  H( Y5 K
$ ./configure --enable-standalone-module --disable-mlogc
. K: j8 }- {' J: t; s# L$ make
3 ^+ g" Y6 [$ }1 G) l编译安装Nginx并添加ModSecurity模块; A% m! }6 k$ P
$ tar xzvf nginx-1.9.2.tar.gz
  w: o8 h& a" O1 W# k! U& g3 G9 T$ cd nginx-1.9.2; l" c" U  N- f8 r4 a
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
" F; u# S2 @' q, n$ make && make install
9 y& X/ @! [3 b! a  l; x方法二:编译通过ModSecurity-Nginx Connector加载的动态模块& a& U: p1 N& n

: ?; ^6 B, ~% g8 _2 x  [6 Q3 Z" j1 a6 }+ n: @3 h, [& D
编译LibModSecurity(modsecurity-3.0)8 l# l1 p7 W# }' D
$ cd /root
; l: R- p% V& u: ?% w5 W$ git clone https://github.com/SpiderLabs/ModSecurity+ l0 O8 a) r* K; ]+ E3 h* e
$ cd ModSecurity( z5 {' L! F: T" H& f% d5 U
$ git checkout -b v3/master origin/v3/master
" R8 h2 N, n$ K$ sh build.sh
, M! p8 Q+ P; U6 v' I$ }$ git submodule init* \' K* x2 z8 G' u
$ git submodule update# D+ X+ [3 B% C) h" O
$ ./configure
# r4 Q. W4 t8 o9 M# `  A% {$ make
5 h0 A( A: D4 _9 R- d/ a2 t$ make install
  a+ ]0 o$ O$ ^% i" }) V- ^LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
  J4 l% T0 X# y9 ^4 k; `
5 k: y7 a. b9 ^; e) w4 F3 Y& s% \, G
$ ls /usr/local/modsecurity/lib. j+ j8 t; [& |1 q- C
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
- }+ d2 E3 Y3 Q, P% ]1 i5 U( c编译安装Nginx并添加ModSecurity-Nginx Connector模块
4 o) c2 e; }# _+ a使用ModSecurity-Nginx模块来连接LibModSecurity' `' b. H. ?, f( `$ I; ^  u( r& B
8 T) B# B! C. f; N6 b. ?- x. \2 u
  i# j2 n% }# b! o9 K, h9 q
$ cd /root2 s3 H7 o" U4 `$ j
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx! y% ^! D# n9 e4 I/ ~$ B
$ tar xzvf nginx-1.9.2.tar.gz1 N7 C( O/ F; Y5 h
$ cd nginx-1.9.2
8 S7 P, Z3 Y6 f9 J6 T! H$ ./configure --add-module=/root/modsecurity-nginx
- T% \0 k% l/ D4 d% o; A- A$ make
: r+ s  p6 [) }9 y5 G. K$ make && make install
) X2 s2 S/ z- H  [0 E3 r, c$ y  {添加OWASP规则
7 ?. |% u3 F5 e' i7 x7 vModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 A; R; V6 ~: ~+ N4 y6 y& N4 a; x

# ?  m# ~6 h9 s2 M: ~) I: c9 _6 {6 o: c5 c
下载OWASP规则并生成配置文件
* `6 v/ G, U6 Z& y8 C$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
, i$ t$ [6 e5 a; V- u% M; G) p$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/1 P# N& Y' I1 d
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 j; m5 k4 M0 P' a3 D$ cp crs-setup.conf.example  crs-setup.conf; @0 i- ~' @  R  \8 [
配置OWASP规则
8 O% A5 u& J, P% P, x+ Y& |编辑crs-setup.conf文件
8 _0 F$ W0 M0 E3 o" J  z5 c" N# X$ U4 o
' O/ B) j9 \' g; z  t. P
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
- q) b  Z, b, g% \3 H+ n$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ G) |9 w9 [5 e4 R& |+ M; }) b1 l& I3 J) q/ L$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
- o7 \0 E* k  y0 k  t+ h$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf' M% x! U/ `* g& m# I1 M' @* Z4 _
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。; j" j4 s2 q# f5 a* ^, M( }

& L2 D2 t$ z& v; t- Z( _( p0 l& y+ O6 @/ @$ r- H% P
启用ModSecurity模块和CRS规则" ^* ~# G* @  Q* F1 b. _& B$ t
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! z/ B+ S1 c3 ?# Q8 q& \

0 w- h$ _1 M/ T' N3 w5 p! S
+ i: ^. V9 I) a  U0 cmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。" a* C* k' P6 M( t* i6 W  [& s
& L4 ^0 W% p, g+ ~* w
/ @# {) T0 ^* S) @1 I/ X$ a
$ cd /root/modsecurity-2.9.1/
* n7 R* V. r) i( m0 w, |, A$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  1 G1 X. `. L6 C
$ cp unicode.mapping  /usr/local/nginx/conf/
  k6 O. v3 O3 J' R- S将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
  [6 d( _# w0 l' x1 X1 j; b  @6 c5 {# \7 [' x

: p* e; l$ n! o* ^' ?$ vim /usr/local/nginx/conf/modsecurity.conf( x" ]% [+ t& M4 S* x
SecRuleEngine On
/ S0 n$ V" Y: ]& l7 F; Q' a6 kModSecurity中几个常用配置说明:
4 T7 g$ N/ d+ t4 J% v4 J5 b9 c- Q5 P- x7 L0 c2 p

" p" E3 w% W3 {$ |1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。" ?! ?; w2 \" X
3 i( z/ C1 a  M3 ~" n
5 i* m4 x: [- d) c/ L
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# Q8 U0 n  d! o2 x

+ ~( e! M1 j- S1 ?5 h* {8 N- L; D/ G$ ^. N# o3 z. o  O
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 V6 R6 C6 f6 Y7 {1 C4 c4 A1 E2 z) u2 P. n. ]6 g5 l

5 v7 a) s1 q; D2 U! v- T4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! Z7 u: H! @2 U% U9 ~* V  t" N) r
5 d* r5 w& k7 O! }3 R
/ I7 e( {2 N4 `. q* M$ a4 K( T
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。. Z5 D2 E- S, H  V2 q' ^! o! |; y
4 q/ U4 u( ]2 `

3 d/ Z1 T/ v( d. j3.x版本CRS
/ p5 K8 B2 ?7 _7 C- L7 W2 j$ z$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ n: c% Y0 x4 \) h  t
# 生成例外排除请求的配置文件
" T6 u( @6 {0 |7 {+ K$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
8 |, b/ v* k  ^) f# t4 e$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* z6 q( [, l0 x! L+ x
$ cp rules/*.data /usr/local/nginx/conf" x6 J$ d2 i- `4 e% C- ?1 L
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
. D# T0 G0 {' A( [$ F% K! \2 V% Y, F6 p9 _

/ Q/ [; A5 B" D, x: \: j$ vim /usr/local/nginx/conf/modsec_includes.conf# H8 \( u$ w! ?9 D5 t
9 \1 W8 m' |+ ]8 W: Z3 ~
[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
8 k- k6 A" f3 Y5 L6 r/ k! B5 Z# A4 ^

5 t8 Q' s7 B  O9 T4 O注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 z2 ]" u6 j. _) ~2 u, Z  X9 @0 }0 X  ~" f3 H' V; I3 p

* `! [" b: J$ I  V* v0 ?% d4 R配置Nginx支持Modsecurity
/ M1 d8 X7 v$ T启用Modsecurity2 B. C5 k. i4 J
使用静态模块加载的配置方法
+ w" _4 b& U: W* U; A在需要启用Modsecurity的主机的location下面加入下面两行即可:
* m+ y, J; g. ~5 o. V5 I' E/ g1 s$ k% b9 B" K
6 d( Q- G. r# W/ k7 ^6 z
ModSecurityEnabled on;  e* T9 B& q0 }9 r
ModSecurityConfig modsec_includes.conf;
6 l2 V& W0 }. A- G* n6 S  k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. @3 p! A# d3 _
, L. f3 ^9 `' K8 A  f3 u: t" U) z; O" [* ~
$ vim /usr/local/nginx/conf/nginx.conf
& E# j& l2 e7 g% M" q: r& g9 q
3 ]3 e! X6 \9 c2 D+ \) H6 s  }
; \( ^5 Y, z3 K. @server {
2 k* L; }. D/ Y6 K6 c% Z' t7 i  listen       80;
8 Q# P% y2 n: }1 n' A( ?. S0 `: L  server_name  example.com;* @* B" g2 e/ I6 m! G8 W

" {- S& o1 p- _2 q3 I) |3 }& U+ ~8 X) A& ^: d5 |: k
  location / {) i6 R' A, S9 Z# `& `3 \8 e
    ModSecurityEnabled on;; ]/ Y( ?0 t4 m
    ModSecurityConfig modsec_includes.conf;; ]6 G6 Q4 s4 a# k& s/ d) n
    root   html;- z: J: K; r$ b, T
    index  index.html index.htm;
# j  z; Q# T; r  }- k: L4 E4 C1 y
}3 ]$ @- o3 q, R9 v; R
使用动态模块加载的配置方法
! T$ |6 f" x+ h3 n在需要启用Modsecurity的主机的location下面加入下面两行即可:
! r: l! B# ~& R7 ^( K( ^! F, C: }" U+ H: s8 e
9 Q, s) d. V. s8 t7 G4 c( L
modsecurity on;% P! q& t0 w, l; w0 D5 ~, l
modsecurity_rules_file modsec_includes.conf;7 X% B$ d: z1 f7 L
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 G3 U0 n  W2 x3 H4 I' t, Y+ z- Z. ~8 d: c: T

# ~! B2 C3 A# t" R$ vim /usr/local/nginx/conf/nginx.conf
: L8 h& _6 [' `& p7 z) L! K; k2 ?1 S- L* B  H" s

4 X+ L  f+ X/ m. h( Xserver {4 _) G9 A: k! m/ m# L/ K
  listen  80;
  i: g( O8 s  Z8 X  server_name localhost mike.hi-linux.com;9 n7 ^% f; F7 A* W! Z+ _
  access_log /var/log/nginx/yourdomain.log;9 x( z$ V; h3 d( N" {

3 V  e# i6 N5 Y* ?& {  e1 [8 C" B6 X- s# I
  location / {
+ D: s* J% Q4 ?0 w2 W  T; E3 _9 s  ^' U  g3 W- q* |# p( e$ R

% B) J8 t9 c: \  v  modsecurity on;  q  Y' Q6 I8 T! N% U8 z: N
  modsecurity_rules_file modsec_includes.conf;: O( q  L' {  M& B0 c7 X
  root   html;# g8 F6 S, N, [! ]) t- Z% d* ^% C
  index  index.html index.htm;
) X1 S( E4 ]9 y& [/ M; b& H. A}
) z2 h- d$ e# V# f3 z0 Y}
7 u2 A# p  w% A; \8 |9 Z& p验证Nginx配置文件4 Z( M5 \' D# P& M6 ]
$ /usr/local/nginx/sbin/nginx -t# j4 G( q% @* |% h: h% m7 L
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& U, R+ I: G3 A9 Qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' w# O9 a. ]8 e
启动Nginx
1 P$ K, x, r% D/ _2 t6 J$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 T9 A7 l3 x; u7 w: ?
' L0 u6 K2 [) d- i1 o9 `

测试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! Y4 ]& N3 k" S  q$ t: w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-6 01:32 , Processed in 0.080408 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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