找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9957|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
' K. w8 e7 l+ z- x$ K5 ~  m  l
& G! k7 w. h8 m& y
, n3 z% @6 Z9 E4 P: N1 q/ @' ~在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。7 e; `* u& f, D% W8 x1 _
8 q) |: H' u) J; @9 O
$ ], e8 x1 W, T8 p# ~
什么是ModSecurity/ z6 b0 b4 F1 H0 z8 }: J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
; |9 g; m  Z7 b7 d$ @$ o3 ?  f! d5 X! e" p, I7 t

- Z6 o- i; a7 i$ LModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。& a7 Z$ I; p5 ~3 s9 B

! ~- P3 O& S" f6 }6 r% B' h
- }5 `# w) Y* K4 h% f' l& oModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
2 L& e4 v; K+ x4 l  q) H: y* A' H8 j
( S# j9 k6 {) y& K' g# n4 d
% K1 ?5 k+ H/ y. g; Q9 B+ L官网: https://www.modsecurity.org/
' I0 @: j+ s& c6 W+ k6 J) Z4 \; C2 C8 |
* D, o$ k; r  B) ~
什么是OWASP CRS  m. K. J* O, [6 O; n6 s4 c
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  o( q' @2 ]# D1 C
; I: \% ]* x: h+ B, Z  Q
6 \) U0 w% u3 p2 eModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
3 J" J) f& K, P" E: }
& w% b- |4 R5 C, A
( \2 u( \6 e: `! e4 O+ e2 NHTTP Protection(HTTP防御)% h1 I9 H: B( R2 m5 X) R# _
HTTP协议和本地定义使用的detectsviolations策略。* j' R) H/ o: F1 ~: w, S  \8 y
8 S# @& L+ C" G; k, f7 v6 `

- j: I, j0 |: ~% I2 H3 T8 TReal-time Blacklist Lookups(实时黑名单查询)
2 o  o, i! {1 i3 w5 L* ]利用第三方IP名单。
: x# o  A. h  J* y2 v& h2 w0 E# H1 p% T  R& u. S
) g2 G3 o& o) ^/ v
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ ~. C( L; Q5 Y& [* X9 ?
防御HTTP的洪水攻击和HTTP Dos攻击。% I; x8 }0 E* b1 Q2 B% w
; n8 Z( H! C( D' P$ D& ]. E

, B4 A/ v- d: `5 v  G5 MCommon Web Attacks Protection(常见的Web攻击防护)) ^7 O/ V7 x6 n
检测常见的Web应用程序的安全攻击。6 C$ _! t2 {4 n, N; U2 Q

+ o# Q& h% v' S  A. a. f, q
8 G' `) \+ ?8 lAutomation Detection(自动化检测)
, w' \, N) L" }7 \1 i' D7 m; ^检测机器人,爬虫,扫描仪和其他表面恶意活动。" P, S- O8 v3 r% M2 p

2 Q( p, h8 z) k8 ^4 f% U6 k" U0 F, f! ~7 [0 U" M
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)) u0 y# F3 r3 e9 l; o
检测通过Web应用程序上传的恶意文件。. p2 t( z) c2 w/ h+ i- L& |

" s( o4 @3 C! R, N, P0 j4 ^7 l; {) I9 Z& T$ _
Tracking Sensitive Data(跟踪敏感数据)4 r! M( b5 ~4 B- P* F0 o+ ?' W
信用卡通道的使用,并阻止泄漏。
% X4 W) d6 m* i/ L
' u4 p/ c2 g# ~9 {1 ~' X5 w: R  B9 H. o" Y: L
Trojan Protection(木马防护)
9 l8 Q5 X. J+ g检测访问木马。7 o- {6 B; K6 \6 Q2 l
. b& c8 v( ~. g3 ], f  L3 ~& K

- s6 z, z+ K5 ?& ?' f7 GIdentification of Application Defects(应用程序缺陷的鉴定)
5 T; Y5 R; w$ q检测应用程序的错误配置警报。& |& G: \, H, [# c+ j& o

3 J$ p. p7 O8 K
" j# ^! U. }6 s; R" b, q! f2 {Error Detection and Hiding(错误检测和隐藏)
2 i9 n7 ~. x* R' }9 D检测伪装服务器发送错误消息。
, R2 D) @( p& I8 m$ |
9 D% i, p- `9 l$ m% Q) R0 F" [) l9 Z& Z% Y5 r
安装ModSecurity
2 {  a$ R4 l% Q* g$ U' c软件基础环境准备' [2 b$ c) Z# S% v8 z
下载对应软件包. c) e- f) R( D+ ^
$ cd /root, g  l+ P4 L# P! E' t: Z( l  }
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'( q# d$ ^6 x( q8 C# K3 y9 k0 J
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; ?; T5 g4 ]/ h; ]7 ]9 Z8 V
安装Nginx和ModSecurity依赖包
0 |& L1 A2 d* Q1 k7 L3 ^Centos/RHEL4 h8 z& u8 [8 M$ p

% {* k2 e, c( m/ a3 T/ F$ X+ Y/ u" h
) r, R6 S: C- b1 D% Q' f6 S- ]& k: j$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 ]! `0 ]2 @; p5 Q" v& X9 W1 nUbuntu/Debian
. d  j, J0 K' A+ ^# e
# ~% p8 n9 P" L6 P/ z' U9 y, W  {) D
$ 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++-dev8 F- X. \. m+ l9 L6 K; Y& {4 z
编译安装ModSecurity
0 A9 S; ?2 t4 O8 N& v. e6 CNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
5 q. y0 j) P% V( Q* S
8 o% N5 F8 t* z" ^9 B- ~! Q3 E2 M8 S: M9 S7 y% }
方法一:编译为Nginx静态模块! W& e8 z9 s. `. h+ ?
. Z$ @# s- F9 G7 P3 {" E" i
9 d0 [- b: ^( {3 J! D4 G; E. N; Q# @
编译为独立模块(modsecurity-2.9.1)
1 G! j# v0 ?. e. W% b+ d$ tar xzvf modsecurity-2.9.1.tar.gz# S6 |  {- U% i# j
$ cd modsecurity-2.9.1/
& i& j1 g: X7 I4 P8 O& x) L$ ./autogen.sh
$ P- F5 h; Z4 z% A$ ./configure --enable-standalone-module --disable-mlogc: ]" Z) H' M8 p$ a' L# P3 g
$ make5 d% k  _1 G6 R- W) M
编译安装Nginx并添加ModSecurity模块- a2 I2 N5 u4 I2 f
$ tar xzvf nginx-1.9.2.tar.gz
) _; g$ F. ~  @6 x  z$ cd nginx-1.9.2( H5 T. b8 N) C
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: v$ \% z1 H9 l' A, }9 H$ b6 Q& V$ make && make install; l7 K. {2 x6 u8 J% q2 G
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块' r& ?& q  c( H! I5 Y/ `* H# u

+ ], A' _: a8 O' x" k- x* f& J! W2 ^, w" }/ y
编译LibModSecurity(modsecurity-3.0)
  h3 |- \; {5 P: x$ cd /root
# g6 i1 f4 l) [9 r$ git clone https://github.com/SpiderLabs/ModSecurity7 D3 w1 q9 L$ i$ {% ^( a0 ?
$ cd ModSecurity
' z* i; j2 S+ W0 P: ?4 e! z$ git checkout -b v3/master origin/v3/master
+ ]# O: i; m+ Z# h7 u. Y$ sh build.sh: Z: B/ x4 g* ~  h; B! a
$ git submodule init5 D& N# ]9 C! S( |+ A$ C
$ git submodule update' L  b7 p: r# c
$ ./configure# @- B3 {  x- r/ g0 `! Q2 S" j9 r$ X
$ make1 O+ m3 i5 d" m4 ^4 s( {+ Q! [
$ make install. O: w+ R, C7 d  [, J2 _9 x# h
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。8 A* J* k2 Z# e) N

1 w: x1 E1 j+ ^+ N
; }; M" M2 k/ r5 N. N, q) g$ ls /usr/local/modsecurity/lib6 ~( ]" I* ]6 l% q- M
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0  z& @% K9 n6 E- n
编译安装Nginx并添加ModSecurity-Nginx Connector模块
' U3 h$ n' h1 m6 u' ?5 p使用ModSecurity-Nginx模块来连接LibModSecurity
( \* j! j2 {. p! y7 C1 i
* X! v6 S1 q2 [; o: H* f$ L' b0 @, ~- l7 V1 x: j8 \/ E
$ cd /root5 f0 `. V* i  F+ l' [2 P" i
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx; l, u7 ]1 r2 d3 ~' T2 o/ E) P
$ tar xzvf nginx-1.9.2.tar.gz
2 `& J" D8 N# l! \+ e  _$ cd nginx-1.9.2( f' E) E4 M. j/ D$ e2 g6 p: ]
$ ./configure --add-module=/root/modsecurity-nginx7 ?2 J% W# G3 ]3 r4 s. `6 c% Y) u; R
$ make
+ H$ n; G6 k+ `9 Z$ make && make install
6 c/ [  n, V4 S/ U! f添加OWASP规则9 V2 t8 {' L1 ^7 G0 o, i
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。5 ]- I& M; R: ~, t. w& m& ?

) ], I6 m6 s3 V5 p. G5 ]" G& {2 V4 `  ^9 i5 K
下载OWASP规则并生成配置文件
: S8 a& y( Y0 L1 R$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) q0 L1 H  D# l; t$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
$ e, i4 L* @6 S5 m$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 I9 d! l6 k+ p9 Q$ cp crs-setup.conf.example  crs-setup.conf
5 g3 ^8 Y+ u+ s5 x) O( r6 R+ B配置OWASP规则; T2 s/ _1 T1 ]6 d
编辑crs-setup.conf文件
/ A1 y1 S" H! W
; p- y# o! j' {* c5 a
/ `% ]9 Y  {( i! t$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf% M0 z- x- r8 j3 H8 b
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" u( I* |; d0 C
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
) w- }7 i" x; V. C& I2 v# s" j$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
1 k7 l8 V4 H0 S" Y: t- k* A' Q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) j0 j/ \3 z: B* A: ]' K
7 e- R* i  B2 m7 |4 n2 |' g
; T8 G9 e# r$ B; O6 V/ L启用ModSecurity模块和CRS规则
( V; J6 f$ m4 R复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 g  L, O$ W2 ]: G: |* `
) w, n5 t' p* `. E( \1 H! w
3 r1 c% t$ i$ X5 o6 I# f) `6 ^9 s4 gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。7 ~- |! J; \) c' I  T% d" W

4 S( k1 I* O9 C" O* R' Z
1 @  e% n7 R7 K, w4 b! i( s$ cd /root/modsecurity-2.9.1/% r# Z" T( Z5 a8 y, i7 Z
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
7 b, j, t1 Q& x" @! I$ cp unicode.mapping  /usr/local/nginx/conf/
. s- C. o9 i" Y1 u将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 p' e# A% Q& _3 H* O
0 Y! t. C; H7 J* m
2 J3 }1 j# T* B# q  x8 \
$ vim /usr/local/nginx/conf/modsecurity.conf
5 e, ^2 Q/ Q5 C1 K" hSecRuleEngine On) `! F7 K8 v3 A- h2 r6 t
ModSecurity中几个常用配置说明:
. Q" h2 M% g/ Q2 f+ M& C( |- Y: e2 l
: i/ v0 ]0 v- M. }& d0 |( x  X7 ]
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。( W2 U0 t% a  |* ]

  P% Z: T& g) ], ~
; V) g% p7 P/ n2 {' K4 D  p7 |2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。, e& ^) M. ^5 T

0 Y  O* V  X0 x9 R$ `  H1 }5 ?  e
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% I4 d, ]5 }: z6 v; d1 Q, ?
  X5 R+ D: J" ]# E7 X' J0 V+ U+ a
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
2 R% g6 H+ Y' \8 Q* e
# V9 m2 W9 p+ S* |6 P8 }! D; s7 J+ L- u6 c
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
4 c5 t- i. U, T2 y5 B+ k* B! T" q  E2 w7 B

5 F! P3 E4 z  @% C3 O3.x版本CRS
& m* O! H* r- G4 ]5 V$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 u( P8 S8 ~; {+ Y
# 生成例外排除请求的配置文件
# Y! M) \' R6 A' j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf' Z0 z% B6 Y6 I; Q. ~! p# g4 M0 g  N  ?
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
% l3 H" H+ {( ]7 E$ cp rules/*.data /usr/local/nginx/conf
, @) B) Z* I& t3 s! x7 L5 S% Q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 {6 @( }7 i6 Q) y; r* T0 m, e) }* j! \& T4 O

2 T4 F1 O' [# B4 J$ vim /usr/local/nginx/conf/modsec_includes.conf$ A$ b" i) @' u* U; C9 ?, w# D4 I, `

2 {' s. a: _9 ^: R/ v
[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
: X! e7 ?, K$ N/ S8 M
5 p- Q/ i, V% w- A
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, A& R$ r' K2 K8 i9 \0 O
. i' T' p! a6 F! }. X; m  a% _

( b* k) F. ^4 O9 F2 G; s配置Nginx支持Modsecurity6 {3 x) y/ z0 X& }9 f
启用Modsecurity
. h8 I. p" R/ _/ s使用静态模块加载的配置方法$ Z2 t. t' A( D" }  C3 @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% ?/ {1 g* {$ s6 v
3 `+ o& F4 D7 b& q; @& T4 Z. C0 r. `
# E8 C! y, W: ?( `; @ModSecurityEnabled on;
* k. _# d& ^" j; X" C* _ModSecurityConfig modsec_includes.conf;: @" G5 ?4 c. I+ C
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! _9 `) D# E! T

. M8 Z  Z/ S- w+ r$ t$ S5 o1 H& y) O
$ vim /usr/local/nginx/conf/nginx.conf6 z* V" B  O7 S4 k6 `
! [" ]- q5 G1 @/ o$ B; {
5 ?8 K* h' U: C' ]/ Q9 e* @
server {
/ P6 D* z. z6 a  l! Y+ ~9 B  listen       80;
0 G  c1 }/ u9 ?' f) ^  server_name  example.com;# A/ G0 t, w  L9 B& k- V4 Z
$ z# H3 ~$ P$ s7 A( f0 t4 c: u

+ J& n1 ^  R: L9 J( [/ `( n$ s- P  location / {4 O* s( w- O: n- P0 V
    ModSecurityEnabled on;3 Z; y8 `) o' Y: z% j+ |9 y" v
    ModSecurityConfig modsec_includes.conf;
* v( C3 i; J1 C: f    root   html;
$ z; U9 [3 [; E& r& W. `$ V0 A    index  index.html index.htm;
+ t  n1 w) }' g, \2 y  {  }6 S9 M9 N" H$ ^3 ^
}% q3 o" w8 ]; t( r
使用动态模块加载的配置方法4 W/ F/ \2 J9 U4 s
在需要启用Modsecurity的主机的location下面加入下面两行即可:. p9 M% O3 C3 \0 Y

& ]+ f6 t: m0 n/ ^. W$ w: ~' X/ R! i# d2 @1 a  X2 p* L# W: ~( T& ?2 x
modsecurity on;
7 p3 i! d5 g# r3 Pmodsecurity_rules_file modsec_includes.conf;! Q6 d9 h0 j3 a% W( ?
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。2 |- ?4 h& U" V* ~: P* U
. i+ b7 X7 \7 P! e: {

5 @/ t3 v, D' F' u0 c/ b$ vim /usr/local/nginx/conf/nginx.conf; y/ i/ k- y/ A: D

$ x" C# v/ m1 U* `7 @
' n$ I+ }  }! }2 N& M: n5 Z2 h1 Y9 `server {
* ^& j: q" q( W' R  listen  80;( J3 T$ i! C. W+ f# f/ z
  server_name localhost mike.hi-linux.com;0 H8 e+ N& K2 ~4 Y- U3 B+ |: {
  access_log /var/log/nginx/yourdomain.log;7 I! G: w4 j  Q  b7 d3 U/ c6 `
% J! B7 N# @% k* e

# c6 G/ v* _; D  location / {
8 E! v0 j2 u4 L0 w! P* o" m& v6 u3 ]4 B% o# m
  o9 f9 b" P! J3 d# M1 M
  modsecurity on;8 Z+ P$ Z( e) a$ K
  modsecurity_rules_file modsec_includes.conf;
, I, g$ ?& X9 Y% H- Y3 {  root   html;
: l( r4 l- I; u; @$ L) L  index  index.html index.htm;! p0 z+ Q% Q4 u# T
}: }/ [" f+ }$ m  r  x* B, B# d" `
}
# B2 U. W! X% i验证Nginx配置文件
" ]5 p# Q% ^- a5 b8 T( d$ /usr/local/nginx/sbin/nginx -t7 ]2 w5 A; F4 u" I* j
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
7 ?' A5 V  c  P# Pnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful8 ]( W% r/ p2 f
启动Nginx/ [5 b. L9 B' Y2 W: X1 p
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ R1 Y. V0 I# C6 Y" v+ s/ m
5 j, u7 M! m$ H/ a! n

测试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 g) X& V$ h6 ?4 T; f* x) W

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-16 05:28 , Processed in 0.040018 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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