找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9752|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。' B- n/ z& R4 v+ Y3 R& v2 E( b* A- \

( }, o* c- \8 V7 Z9 r! R( I7 C
& T) Y( Q2 M3 Q5 Z! I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* e$ t4 w5 F9 C7 ^6 @$ g. V6 i

, f2 ^7 f$ n( ~
4 _. g% \; H; `# v什么是ModSecurity
1 P3 K/ O# F! ^  K' A  H, d+ zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' o' S- G+ `) y& R" V" ]8 C* Q: `8 m! b! a4 n
  f% @7 H6 M4 T, T
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。% f2 N1 U! S2 Y

/ K1 y- E3 H( |0 [6 O$ D, q7 K4 A
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 ?9 h5 l& I. z( ~
' B/ \$ t$ I2 a- X7 [+ r( j" {
$ e; d# j& C& G* `: J+ o3 A; B官网: https://www.modsecurity.org/
* W, g3 M/ K0 q( q7 f* S% `1 l& H  V! H. l# u3 b
. q+ I& F% z2 C& ]- `* Q
什么是OWASP CRS! A- R7 F* Q1 F; S# U2 R  w9 b) [
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* e5 q+ k' U. u
, R% i6 H$ p; Z( x# S; V! i' X

  l+ v) c4 O% e( V: F5 YModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ v( e: G% A3 [+ O% e' R9 f5 ^  Z7 G& B( P8 L) j. O
% l- q8 P+ a( C: a& t3 G+ `
HTTP Protection(HTTP防御)
. }1 |8 M. i: ~  jHTTP协议和本地定义使用的detectsviolations策略。
, G/ V5 C7 f7 _4 w+ U6 }! Y% _" {
2 }0 u+ A" N. g5 R+ }& p
8 V7 I, m0 A- |Real-time Blacklist Lookups(实时黑名单查询)+ `$ ~/ N, J/ H# V4 M
利用第三方IP名单。
* C" q8 J) F' V- k# u; r' W( f+ h* t. X, a. y  r; J; ]2 Z( r+ \

( f! h: W) C( S/ \9 ~, n1 F* O5 GHTTP Denial of Service Protections(HTTP的拒绝服务保护)
- T8 b7 Q! d: t! t2 D" [* h防御HTTP的洪水攻击和HTTP Dos攻击。! Z& L; Z7 z( a
# B5 q2 s( F9 |& k% e( W

2 N, o) _7 X/ ?9 {8 PCommon Web Attacks Protection(常见的Web攻击防护)
' B4 ~% k- C" ^& x检测常见的Web应用程序的安全攻击。  f2 I+ d( y3 R9 G- t# s* M

& _. ~! j& \8 U) h! d: Z; |/ d  p# H+ m/ p& I. U0 ?# u
Automation Detection(自动化检测)
4 _& Q/ x( s0 W* j6 @. n检测机器人,爬虫,扫描仪和其他表面恶意活动。- c9 y, }4 R: m7 J  @
  V/ ?: p! E5 |8 B

7 R/ `0 C: e8 p7 \; EIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)3 C/ d0 s7 V& }1 g8 l
检测通过Web应用程序上传的恶意文件。
& v) f. Z3 Z4 ]5 {, h3 Z* e% |% G& ]3 x: h+ ~' L$ f( U

; s: d" C! Q0 U" z8 o( iTracking Sensitive Data(跟踪敏感数据)
( A- z, O8 V& a1 q! j( T信用卡通道的使用,并阻止泄漏。
( [% A8 E" G" X
% y7 D" j6 [. t$ W  e( [% f/ i2 ?5 l2 ^, M2 b! F5 S; F
Trojan Protection(木马防护)
7 H1 m. z/ S- d" A检测访问木马。+ u7 p, r$ Q7 Z5 S

$ |$ K, z( J( I5 _
, t8 }$ Z4 ]: J/ i  ^. }* nIdentification of Application Defects(应用程序缺陷的鉴定)
" D, w- H( G+ g: i$ X# @3 v  v3 K2 S/ Z检测应用程序的错误配置警报。
" N0 p/ ^7 x: h% [
- u  D" r" J5 U& i3 N$ \$ u+ A6 H% T0 }6 z
Error Detection and Hiding(错误检测和隐藏)
) @, i6 K( \( h4 k6 _8 R检测伪装服务器发送错误消息。
) M  }/ Z9 g3 a$ O" c4 B
) r  a% i# T$ C) k. h: |( Y* H5 D1 g7 b5 g8 C: w
安装ModSecurity
# h0 o$ b5 w& ~软件基础环境准备
$ I5 y+ f" w/ Y* z& m; e5 \下载对应软件包
# E* L! ?1 s* }* N- c$ cd /root
$ W2 o1 w; O2 v" m0 A$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 g5 K; T$ j1 F6 O; q  U$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
# ^9 ?' V2 \. }1 G+ [( U# I安装Nginx和ModSecurity依赖包
/ W: B& }& S* S5 W1 }4 w- E: vCentos/RHEL
+ w% ^5 V7 S3 s/ q" r" }, V2 Z3 Z* r: F8 C

' D, c  c! r  ^& S# @. i$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel0 B! b6 A+ A* D9 G" V
Ubuntu/Debian
( f: v: t, c2 z7 N+ ?4 N% f+ s( p3 ?7 \0 r! R

. b4 K% W' Q* V0 x$ 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- u" W" \! i. H5 x. U/ t
编译安装ModSecurity) Y/ P* Z. t5 p
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。" ~% G, s  _1 P0 l0 D, x0 z

# Z5 B. I7 k2 z2 W( Z
# M1 w1 c6 I7 O# O0 I5 T方法一:编译为Nginx静态模块; [% `5 m; R( I+ d' D+ U* z
/ k9 `" {& e% X  ~; S7 Z2 }% ^3 v4 s

4 B& J: _3 Q& e( k- c9 z编译为独立模块(modsecurity-2.9.1)* a6 y6 k/ H' [6 h, U" p! P
$ tar xzvf modsecurity-2.9.1.tar.gz
! A' K8 J" |! H4 k7 o  X) c$ cd modsecurity-2.9.1/0 S# |, l8 e/ J
$ ./autogen.sh/ \- p5 k5 n4 d  T6 Y+ x! l
$ ./configure --enable-standalone-module --disable-mlogc* P5 p3 ?2 |  _7 m6 x3 i7 e
$ make
' a8 U! Q1 x# P# O2 k: R' M5 f编译安装Nginx并添加ModSecurity模块: z. B7 K4 i/ i3 I' ^
$ tar xzvf nginx-1.9.2.tar.gz
6 \( o2 H5 Z6 v6 R1 }$ cd nginx-1.9.2% b5 m4 q6 |. t. w
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
- t- }- y- [$ |3 A$ make && make install( ]" m% L! G. U6 J
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# m9 S. G. {' Z+ A
- L& K% }* J4 e( G

5 U" ?& o. ^% P编译LibModSecurity(modsecurity-3.0)
0 B& p2 e- l* ~! @4 o+ d; T1 c* ^$ cd /root. a: d- @: m! n
$ git clone https://github.com/SpiderLabs/ModSecurity. t' b6 A3 V$ o. t4 Z) o+ p) o
$ cd ModSecurity" p9 i% i, Q, G+ |
$ git checkout -b v3/master origin/v3/master
# {" b* ]) @6 f* v( `% m$ sh build.sh
8 c7 B; T' e$ A" \- D$ git submodule init
# v2 |7 R9 w8 o, ?1 L, z$ git submodule update- {6 Q7 h5 p% j% A) J7 G4 H) u+ D
$ ./configure7 x1 q! T' c# M+ O
$ make
& Q4 Z4 \+ D# e) Y$ make install
7 H  }5 b" u) BLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。: j  l* \. H; m/ l+ Q( O

- A6 Q1 I# @8 T) O* z5 k( s
& A3 d0 x$ o, |; z7 D$ ls /usr/local/modsecurity/lib
4 u% c: k5 T5 i# M& Llibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0# M$ Q1 C5 G3 n5 D" @6 v( a5 J
编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 V$ ~2 e, p2 k+ x4 k. \使用ModSecurity-Nginx模块来连接LibModSecurity2 V* {9 w# M6 A! E5 F
8 n& D$ L. J& x
9 k4 _1 q1 f# G- {) M) @/ O( R7 a
$ cd /root6 T2 G/ i1 J+ B/ ~; _2 ^8 t
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx) s' W, ^3 f% K8 N/ j; A+ q; B
$ tar xzvf nginx-1.9.2.tar.gz, M. B" i, _+ U4 {% h
$ cd nginx-1.9.2% b3 p9 D( Z  C3 Z& K: y1 i
$ ./configure --add-module=/root/modsecurity-nginx
8 \+ o' q, {, T! R7 p: N, ~$ make9 n. F- V; d& Z
$ make && make install
$ W1 K* U; [/ n1 K7 T6 G% d添加OWASP规则
, B2 h' `& d( {4 L7 g# g; P  yModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* y0 _5 d% A7 L/ t4 b) T

# E4 _( ?( |+ ]3 d" Z" @5 M4 Z5 K8 y' Q/ o0 L5 @0 C
下载OWASP规则并生成配置文件
: s$ |1 O3 q+ C, i- Y! O" L, ]$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: v4 G$ j# Y4 M2 o9 |
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
5 j7 H6 n1 H& ?, @  {& S/ B& s$ cd /usr/local/nginx/conf/owasp-modsecurity-crs# K8 S7 S. K' P# q9 |7 E* I: T! M
$ cp crs-setup.conf.example  crs-setup.conf
3 W& `! g3 g/ U4 C5 t9 b配置OWASP规则3 N. }1 q# P7 h0 y* G
编辑crs-setup.conf文件6 k0 s: I7 `: m" k$ ?& p3 x

. `. X3 X, t: O- ^$ `
. |: b$ Q, d0 a6 Z9 z1 ?$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 E* k4 ^6 u( Z: R9 M) E5 f* X8 m' U" `$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; E; _& u" y# H3 }2 s. a$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 a1 M/ ?, s1 q( }/ y' A
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
2 z, E0 p* v9 {0 m7 ?, r( ~- H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 X! w+ f6 l  l7 G

$ F" ^% |  D8 E5 |( ?
* G( u6 H( [2 V( L$ I) H' Y0 S启用ModSecurity模块和CRS规则) F4 o! \! i- m8 g
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  U$ o8 ~: c( J$ W2 Y4 c7 F/ {7 }/ ?) F9 M4 B6 q
0 B9 k# m  R  M" I
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ ~2 k1 l# T6 ~9 I0 u
7 V8 P+ l6 @- j4 G8 S7 e
, e$ ~0 M0 b6 I" Y9 D; g
$ cd /root/modsecurity-2.9.1/; T! M+ \9 a9 D% f) t+ C) o9 `
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  6 a) G! m) E& v6 `. z' H8 W4 B
$ cp unicode.mapping  /usr/local/nginx/conf/
5 f; n2 K% ^8 X$ }将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' j7 t' T7 s( `$ j# e% z
3 x; l  {- S6 ^" T1 @. W

2 p8 y7 P1 x) W0 Y4 ~5 x0 x$ vim /usr/local/nginx/conf/modsecurity.conf- z2 \& g4 c# f$ J% U0 R
SecRuleEngine On8 w% x. y2 F+ ^. q1 Q
ModSecurity中几个常用配置说明:2 Z* }" E, r2 g
9 Z  G- g7 m) ^+ S2 Z
6 ?7 K! e: B: c
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! b8 o8 c2 N) O2 o2 T" P* ]: l2 `  i* m4 o8 R6 K
! B" y& _4 G1 Q1 Y$ H
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。4 q# B+ l8 Q+ k& l$ f/ X" f- L
  F! `% }3 c4 B: y# D4 _0 e% |% ]

$ R; o5 C" J7 O& c3 [3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。7 c0 G! U& R7 x3 _8 a
' |% q9 E% T( @8 H* L& g# B8 B

/ }# v7 e2 J0 i/ T4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
6 ^" |2 J. S% }& q+ x5 `0 m7 V% h1 ~# L! l! m, a5 M
& k, x+ E0 j( r& {8 t! ~2 V
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
/ @& d5 Z& O  ^) n$ q2 q9 |2 j0 ~& Y- `9 g
+ r2 X4 [, ?( x% l3 Y+ S
3.x版本CRS$ ]0 Z- Q3 Z' g) S- k. o4 _  h& @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
! @! A( ]7 G1 y% K& C# 生成例外排除请求的配置文件2 r# H3 \. N  z
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf; n8 X. o9 m" P' n; B: b; G/ S8 O; B
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
6 }1 x3 T) U9 r4 q  A& N$ cp rules/*.data /usr/local/nginx/conf
5 d/ z' q) n& k+ ?为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 A! m$ k. e- q; y) x9 H8 v; {& A2 G( T3 x

: ]7 E! v) Q8 V/ c( `$ vim /usr/local/nginx/conf/modsec_includes.conf
; H- F: n; u6 q, L. V
) v( K! I& i+ R- V- l' a
[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

, p7 y7 M7 N" [
; ?! e" x9 a- h! J4 D注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ q6 Z+ y7 N/ |  Q# O, x8 Z# V: I5 [

/ I7 A0 j! c$ x  B1 T, ~4 b1 P' \配置Nginx支持Modsecurity. j0 o8 Z$ T; d1 |: }/ X
启用Modsecurity
5 v- H/ _/ a* n$ O使用静态模块加载的配置方法9 c  `: P" F# l2 N# W, _# p8 z5 O' y
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 G) s5 M" R) \! ]* B
! S% }1 a4 l' i! \3 B' L- y4 k
3 g; @' S2 T$ t- Y# |
ModSecurityEnabled on;
' f" X. k, r8 g8 e$ LModSecurityConfig modsec_includes.conf;
7 M. m4 S5 x+ R# C6 [修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; `2 v  i, u& g
8 }& }# _) Z8 @1 J! \) E
$ F5 x2 R9 I- A" g$ vim /usr/local/nginx/conf/nginx.conf
6 C5 ^# p1 P% M% J$ a5 u. [0 N+ |* V* R, d+ G8 }/ q

# m3 Z) C, z6 iserver {
$ W1 o& g; H2 J2 U0 L- w! p  listen       80;9 A4 R' F" C9 g
  server_name  example.com;
) k/ {9 i: O$ Y& }5 v$ V) z$ b: D8 k9 x+ s9 Z
3 ]1 \+ @  K* p+ M: b/ I
  location / {' e/ W, u; Q2 o+ {
    ModSecurityEnabled on;
8 f# Y# a; X: U: g9 V- m) U( t    ModSecurityConfig modsec_includes.conf;
/ F3 t! B9 X) |    root   html;
  ]; F+ Z7 ]. w+ A' i    index  index.html index.htm;
. \; p. N9 I' }  }
) j' r& h& Z$ F6 ]! F- ?: \}
( _) D8 S1 S& u9 o) n* O使用动态模块加载的配置方法
5 I. k  v+ I# a, J. b4 Y在需要启用Modsecurity的主机的location下面加入下面两行即可:$ n' D" D+ b% q  R$ p, e

$ s5 h1 X1 u# s. P1 B3 N: L& o8 Q
modsecurity on;$ P) [, `1 i- m! o1 j: B6 ^
modsecurity_rules_file modsec_includes.conf;
" `- I) B; _" f4 ~修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。5 j6 C/ i5 E2 D5 ]1 X% M' f5 ?/ D0 N: {

. p! k; j: [. h
7 r4 k: V6 X1 f* z' a" s7 A$ vim /usr/local/nginx/conf/nginx.conf
9 k8 u7 M5 A& K0 K, C" o& L. ~: D2 ?; u- i+ f8 o9 x
/ S4 M' G. a& S' S5 L
server {3 ^; S/ h& m$ g$ l3 m
  listen  80;  S% p& i7 e3 o! s+ P0 j' x# s
  server_name localhost mike.hi-linux.com;  C! Z; G7 I8 j! O  @0 s  C, X4 X
  access_log /var/log/nginx/yourdomain.log;
  m2 j1 z6 Z: ?- |! d
, H$ ]) F* `0 c: U1 i
- q/ P- o& R5 L  location / {
4 R9 V; U1 S- @  o/ Q2 X( W( g% F  Y
( z, N  e% z! [% E2 `0 I7 j7 o
  modsecurity on;
# Y# k$ A) @6 s- v  modsecurity_rules_file modsec_includes.conf;& J) \; A( ^4 W) o% D  B
  root   html;: W5 J* i$ y' E) d
  index  index.html index.htm;5 u) l# l" X! J* e* ]
}% G. _' M4 C- N( ~
}
( V  M# q- C) U, Y' Z! q验证Nginx配置文件
7 e: M  m: b: |2 j$ /usr/local/nginx/sbin/nginx -t: {4 m- D4 K2 l8 J! w
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ Y- r2 q% r& k- lnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& M: q3 U* h% L4 Y4 `, e0 X6 ^
启动Nginx' d1 G$ w. j1 Z8 r1 [7 O5 ^/ z. p
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf& U* A. F- U" ^' {3 A
  a3 _$ N) ^. O" J, E

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


& ?7 Q$ }) j( j* E6 f

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-16 21:02 , Processed in 0.074177 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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