找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11276|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
. x3 Q; m, p5 P0 f8 J& Q/ D( r8 {, Z- Z- Q8 c, e1 ~

" L5 x7 B0 |& i6 X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。( ]# \5 c+ L- }1 I! _4 b4 U# Q+ d+ E
- v0 `- Z) _9 Z( G/ [* q. R

. W# C/ W0 J$ E9 n+ j什么是ModSecurity
& Q% c6 i5 x# \  w# Z9 a6 O- sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。7 G" E3 b5 r3 I% {% n) J/ D$ {) l9 w8 F3 r

+ v- Q' {- h5 |9 f  c+ v0 p. x0 [
4 o  M, m- y6 o  ]* f$ |ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。# b& e( m/ _! p/ i) `' y+ ]7 g

+ o) ~$ Q* f2 L% A, K: d. O  T; ^- R9 ?3 q
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 R5 D1 h/ M) i# s* u

7 ^3 N* U% B9 Q* X5 B
: L0 i( d) U0 L, e  {官网: https://www.modsecurity.org/
) p7 \! V7 B0 ]! j* r: Z1 Q/ ]* b3 `" Y: J9 `

% R0 j; n4 e1 |# f7 X5 x% E什么是OWASP CRS; ^: K! D/ x3 z' H; L8 j
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 N& F9 l0 {4 U" e4 X$ q! r# `

8 ~: \9 Y1 ~" B- s
) @5 Q5 |0 c) GModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 t3 ]% p9 D" F  j  j/ s& J2 O

- n. T( v6 j  k! B* U% Q
. A. |$ _  B5 ^4 f1 @HTTP Protection(HTTP防御)9 @; `2 i! n3 l0 v7 m8 J
HTTP协议和本地定义使用的detectsviolations策略。
/ \( t. u* F! r1 g3 y2 x/ {; b3 y- P: V2 K7 f

! G. J& h" J) U6 _+ p! v9 z* v9 [Real-time Blacklist Lookups(实时黑名单查询)
( \/ N. T) N% R1 c. R  H1 e9 b利用第三方IP名单。
. r8 P$ l7 G9 I5 R5 g2 r! W% N$ x- K. V. t) U( \* c. P

2 _. @* U0 ^4 h- J- s+ uHTTP Denial of Service Protections(HTTP的拒绝服务保护)+ {2 l0 G  N  \( D- T
防御HTTP的洪水攻击和HTTP Dos攻击。, b/ U! f: [" d$ ~2 F# o
2 I7 r) M: q5 N( F, J) y

# k" C& \4 Y2 d- OCommon Web Attacks Protection(常见的Web攻击防护)/ Z# o* T% W5 O  |2 p: Z
检测常见的Web应用程序的安全攻击。
# }2 }9 h0 [, ], F4 E/ Q8 E8 b
1 V) p/ y1 f) Q5 |3 a$ R! k
& m3 g" v# S7 G+ zAutomation Detection(自动化检测)
5 k) U/ g& ~- q* a检测机器人,爬虫,扫描仪和其他表面恶意活动。. w  X' B- V9 k: r5 x4 G9 l
. U+ n- x( r. _; ?
0 h4 B3 T9 k4 W9 P( d( t6 ]. E
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
  R1 M3 u8 I* g) A* [( Z. u# \0 h, F检测通过Web应用程序上传的恶意文件。. S+ w1 N5 y# F& p0 A; k  r1 z3 S& }
+ p% C6 `' L0 g/ X8 I0 y

9 b" K; o) ^5 L. ^$ C# C1 o3 UTracking Sensitive Data(跟踪敏感数据)
- j9 J2 s4 b1 c  _& p信用卡通道的使用,并阻止泄漏。* f8 W/ r( R. V. [1 y7 A

' w" k1 E% W7 N" ]+ _4 K/ `1 D' g+ ~; p( o, _. N3 }# T
Trojan Protection(木马防护); @% a9 m8 ?) z! H! S/ _
检测访问木马。
$ m) f, _, v5 V6 C6 Z! m
: o' G. q$ l$ U- Q' \
3 V2 K/ |- o0 e1 Z; \7 z, uIdentification of Application Defects(应用程序缺陷的鉴定)
: M( o! ?6 q- a) T检测应用程序的错误配置警报。( k, r$ T% c* S8 O. b* J
* @7 P2 W# I" C/ u4 N0 S
1 e/ U0 r+ c2 @8 \5 e
Error Detection and Hiding(错误检测和隐藏)* b& n# ~. e1 `1 ^
检测伪装服务器发送错误消息。' {# e, `5 I: n9 x: P! \1 N$ e2 x
. K' O! b  D" {9 w& |
! J2 S! P: ~6 G
安装ModSecurity
5 q; d. w3 T# c0 l8 Q) a8 i: f& d8 H软件基础环境准备; c$ S& l9 u2 x  h( T
下载对应软件包
) _) J- S: _+ a' l) X, k$ cd /root; C+ U  ]- [2 `8 q8 T: V" `3 r* t7 J
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' J) `3 y2 {/ v: O$ g# G! z" H
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 D' p- d1 M; @" g
安装Nginx和ModSecurity依赖包
' z: u0 l4 E" \/ O" @6 wCentos/RHEL
: J# E) y1 K, E3 ~% X7 \9 X4 O; |, C* r# X, b: R/ N

8 J2 ~* F' S$ b  P! n$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
2 T: Z( o- b4 K% u: }2 W  cUbuntu/Debian$ J. @% p( j8 c7 g+ b( _

/ n6 l. d" T7 p$ e6 i8 i6 n; `* l4 D+ e; L+ B
$ 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/ @+ K, C, @* Y. E# \
编译安装ModSecurity
4 o) J% \9 w4 q( \. mNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。# x. r9 T* m: i

/ a" D3 B4 e( ?; p  [" @$ \  T. Y4 F' ~' `
方法一:编译为Nginx静态模块
- F. _6 }& }: O* Q9 y& \0 d$ g& K$ L6 Z& c& Q. ^* z; j0 i1 B
. B/ H" L) s- l8 g8 H! l# f0 E
编译为独立模块(modsecurity-2.9.1)
$ ~5 L" g, ]( W- v" ~, K$ tar xzvf modsecurity-2.9.1.tar.gz4 x1 l. f8 J# D* K# ~) T9 |- R" G
$ cd modsecurity-2.9.1/; X9 `8 h$ d/ E9 H( c6 B% ^6 q( U
$ ./autogen.sh
2 {+ W/ [( z/ F$ ./configure --enable-standalone-module --disable-mlogc& \' n2 }& v! k- @' Q7 `5 f4 {
$ make
) g' p4 T- v. ?7 i6 g& `编译安装Nginx并添加ModSecurity模块+ d- h" _, R7 u* b: \# q
$ tar xzvf nginx-1.9.2.tar.gz
7 x# v( S" m8 u$ I$ cd nginx-1.9.2; p! A5 @( K& U: `/ l' @
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
  z  G' e& i3 V3 g! z0 K$ make && make install
" H. R! T& v) [. m" d4 t方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 h- q( a3 \, W" ?! U" T0 \& p; X: K& c/ S0 s# [, e6 l0 Y

% l) \  s2 a8 E2 C. w编译LibModSecurity(modsecurity-3.0)
# A2 @' x: l2 a" t2 [$ cd /root( W) M0 K: o* `
$ git clone https://github.com/SpiderLabs/ModSecurity2 V( j6 L  o* n" {( n  s
$ cd ModSecurity
, r, R/ u0 E! @$ git checkout -b v3/master origin/v3/master: a0 V2 s7 W% a0 x
$ sh build.sh& `1 \& N+ z8 x7 ^$ F7 o
$ git submodule init8 j: j$ b. r/ r# [: m
$ git submodule update6 ?% u$ @( z4 l0 M. B' o; G2 s
$ ./configure! g, N" c3 u- x1 E' ~/ Q
$ make* S3 f! z3 @! f. U
$ make install* x# ?  O3 m# U2 {& o; a* Z
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
2 Y1 @' B; B$ N' }% b: H7 H% [5 w! L
8 n; C1 g4 o% r7 V% i( v
$ ls /usr/local/modsecurity/lib
1 }# s; c- v5 m7 c* blibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
. V4 y3 D# _* U2 N编译安装Nginx并添加ModSecurity-Nginx Connector模块
, @* ?/ Y% [+ ^使用ModSecurity-Nginx模块来连接LibModSecurity
# d# X( `+ A) |' _: \9 k
7 l; k3 e0 Z' c7 ~) \. w* T" w" |5 z$ y+ H4 f% j( R
$ cd /root
% j( }' d( E7 g$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ V0 E( X4 y; t
$ tar xzvf nginx-1.9.2.tar.gz  D( R% @% F& M* N+ D5 S
$ cd nginx-1.9.2
$ O7 y4 D: \4 `" _; k$ ./configure --add-module=/root/modsecurity-nginx* C& J7 T3 E% ]* |2 A1 X
$ make
- h  Y# A2 }  T' X1 u, T. n! L$ make && make install
: z) J% }9 u; v6 s' f7 ^; |添加OWASP规则. Q6 t3 \* I; g' i" L8 a8 J9 [
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ c& h0 R4 Q) h0 V; T
8 T! f) M8 y* f9 P% y- S5 I/ A

- y& N0 d. d4 d8 n; `+ ^下载OWASP规则并生成配置文件4 e5 Z0 `% y& D- T4 P
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
  s8 H5 l8 F6 _0 g$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
( n3 R# A4 n+ l3 C0 ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: ]/ g- I0 ]" o7 r# O2 `
$ cp crs-setup.conf.example  crs-setup.conf( f4 E1 w0 m- `8 Y6 [2 e) `4 f/ y
配置OWASP规则
$ j# X& H- {9 |4 }: d编辑crs-setup.conf文件+ S0 P# g" u# c6 n6 Y% u' A" i% p( Y

" ]8 b& C7 U4 c, m- X
8 H3 |, h3 E& Y1 X$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
% c& d! b2 f: C/ O0 v$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf. M& Q7 p3 ~- u0 [( ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
* a7 @& X: x' s) ~# |9 x1 [$ p$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 F+ U8 K. o5 F! B" h" a- e
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。! {/ Z; K6 d, C4 X; i1 H2 j
& a, f3 Z: m7 ?3 L

4 A! T2 O9 B# |# w- \$ ]4 N启用ModSecurity模块和CRS规则9 o9 j# M  F# D, s2 ~  e6 L8 _
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 y' Q0 K" X: m* c0 T/ k2 a3 i& f& z8 |2 S, j

1 D5 k, e+ r+ c& k% [# Xmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 H! z; e) L8 _: {5 h, i
$ j, U# C5 h9 s+ Z

& W' D& X3 E9 Q! u/ [$ cd /root/modsecurity-2.9.1/
! i/ L% D+ i: M/ R$ Y8 {$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  9 n: f0 f; z. x
$ cp unicode.mapping  /usr/local/nginx/conf/1 P( Q1 V; d0 e! \. j: p0 G
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。# E5 t: r5 v: u' L
7 x; R- j# z0 k: G
1 L9 G$ f6 T4 F/ s
$ vim /usr/local/nginx/conf/modsecurity.conf) `5 E5 n6 z; I
SecRuleEngine On5 a9 P' m: F% _# y% Q
ModSecurity中几个常用配置说明:! g1 f! \2 Y+ ]- M% a' d: D( D" c, |

% }  _5 U) h7 c7 |
% F1 S! \- S5 c- `2 `1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. k% r/ g. N7 m& E3 R

: z# g9 i8 p, Z  _3 ?/ I8 K# K- p9 u' F
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
$ p6 @6 \- Z/ F; w+ }* O! x
( \! U4 x- k8 `4 f) H- H
7 ^$ d, |2 R2 m& R+ s% s) b1 j; e3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
8 \2 ?; y# T  A+ P* H% K) _9 Q* G& _  z+ L, a

3 r# `$ O: _; x1 B$ G* }8 F2 z4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" i4 B4 J. `9 w( D7 [
9 h' S- d+ I) o+ |+ }0 x
0 L1 {4 T' ?0 W& d: {4 M在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。: k9 h; w; I. S

9 M" p. C5 i0 ^+ l6 k6 L. u$ p/ f* ?, ^" i3 i5 L" i6 B1 P: S
3.x版本CRS
. x& Z" m8 d1 W6 z0 X) ~! z4 }$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, L0 B. K4 \/ @6 G# 生成例外排除请求的配置文件$ E, U/ {8 I, Z
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf% S5 G% a3 V; `0 A4 p2 u$ L
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
9 A0 B$ ^+ p' T. ?% Z$ cp rules/*.data /usr/local/nginx/conf
. g1 G$ K) d" K为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; k5 h& g0 l6 P, n  P: Q- U9 ]! H: b9 U+ O4 x* N! C1 m9 ?
# Z3 Z* W' G6 t% F" O
$ vim /usr/local/nginx/conf/modsec_includes.conf
5 M" M- E. z9 T3 l6 s
( `" r, n* S: \5 w
[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

  B& X( t' E; e0 U2 \
+ J1 w8 O8 h7 O, A- p( L注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。# K& M5 N4 |( M, N
4 s8 e( V" n4 V
0 X' g& W, q/ k5 K: I+ O$ j) v3 l
配置Nginx支持Modsecurity
# v" o. M) ]% C8 q启用Modsecurity
# s% T! A2 \. q: f0 P使用静态模块加载的配置方法( @* M# m6 J7 s7 V
在需要启用Modsecurity的主机的location下面加入下面两行即可:; j/ C$ w( d% ^1 f

( ^6 @" ?3 v1 U3 _
2 ]" \9 T9 x  M# M. f" K6 p) H0 IModSecurityEnabled on;
7 V9 J+ `1 {( d6 h5 zModSecurityConfig modsec_includes.conf;
$ u: S' M, F3 P5 N修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
0 }: l- l8 k5 ~7 C' A8 h+ l1 o; S$ D2 g  @5 v' F% x; u$ s
  Q9 }' K! n; k6 N% I
$ vim /usr/local/nginx/conf/nginx.conf! g- E9 C/ V1 {: p% {" w
3 C: }6 K0 i4 s, w9 D+ a, I

; H- }* @- _: _1 D. \8 r& wserver {
9 G+ \& F$ O3 j  S& F- \# v2 `0 t  listen       80;4 o# F5 y* S9 u
  server_name  example.com;
+ t3 d- h# e, w+ `# I. O3 a( d9 X  }5 l8 a# S* p, g$ b

9 i: N$ b1 T# ?) }% B  location / {. c/ {) I: `  y  b: t
    ModSecurityEnabled on;
5 o. }7 e$ |; A! {$ Z    ModSecurityConfig modsec_includes.conf;. k5 w  m, T& J2 r6 b3 i, t
    root   html;0 k7 t9 ^9 c7 p- O# J: d$ h( Z$ t
    index  index.html index.htm;
) S+ E" t9 P, P6 ]7 {! b  }
' R- E6 W) T  x7 Z: A4 A}: q( ^# u; p9 g9 u3 [3 L2 h
使用动态模块加载的配置方法
" F0 _# a9 o. F, \在需要启用Modsecurity的主机的location下面加入下面两行即可:
# H/ d5 i% E8 Y& J3 q+ d* A/ P$ b1 `, N! _& L3 I4 _+ B

; V1 [# G7 {) n, `" G* imodsecurity on;
7 T1 @$ [5 o$ ?) f& ^modsecurity_rules_file modsec_includes.conf;
9 p" B) G0 ^! ?: K+ n8 i8 k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
( J0 _2 i9 W7 ~2 s9 f
8 z7 r# [3 C+ N+ l
, W) h# b2 q! c' c+ `$ vim /usr/local/nginx/conf/nginx.conf; ]% t  ~& M: r
2 i6 E( S2 R+ C
! Q, _: E' F; s
server {
, i# A  P  J: q' `" g  listen  80;7 B8 G8 d' y; D% y2 Q3 q- w5 z- u: E
  server_name localhost mike.hi-linux.com;3 S0 G# T% T9 L+ N! t+ g' i
  access_log /var/log/nginx/yourdomain.log;. Y% K/ z, Q% V! j6 N3 e+ G

# O6 P" O6 G5 X1 \- s0 M8 J7 {4 b- D4 @; s: C
  location / {
( `* U( D, k5 l" L7 K, o5 u+ R* E. U; j% I" P" N0 o1 ~* |

5 r* t3 O" k2 Q  x6 H1 B  modsecurity on;7 X  V6 k$ n" o" I5 h
  modsecurity_rules_file modsec_includes.conf;+ V5 r+ }, F2 ]1 s6 e' T
  root   html;
3 g8 T1 X* {5 J# l9 G7 Q* \# S  index  index.html index.htm;' ]  d' h0 X) M
}
" n8 y' m. ^" m7 h. ~( M& f}( i! J% d) Q6 l" w+ t; {
验证Nginx配置文件8 d+ V  d/ O6 ~- h8 D+ r
$ /usr/local/nginx/sbin/nginx -t
, ^  U: I" o( D* B7 w; wnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ q3 L( L4 ?4 U. d8 V, Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful" N  K4 `$ Q3 V' D
启动Nginx
/ D" A. m. F% W2 t$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 ~' Z( f- C5 O1 C

- o  }# z! Q0 _, U5 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能够战胜更多复杂/未知/混淆的攻击模式。


) v& x, p( o6 e

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-24 03:05 , Processed in 0.057621 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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