找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12796|回复: 0

利用ModSecurity在Nginx上构建WAF

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

% A0 e3 u' j% F; t
; Q7 U; E, _6 ]2 @( o在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 i- u$ U* ^3 i% U0 s- G3 {, j& ]% ~2 w  Y1 l) r5 X

2 [+ F) `8 P7 b0 h8 g# F# x! x什么是ModSecurity/ r  ~( m; v8 T' Y& o+ E) L8 \
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
' O6 e! O$ }" Y/ s. ]9 \$ T! V+ c- r4 C
7 B3 J) @" B$ |* Q
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
2 ]8 B+ z% G/ h% M8 h
- B# A4 O0 F. K# _) Y7 @! G: a$ E  I. t8 z# }" @- T* O, N; ?
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。) g$ S$ N/ U" T0 _/ ^, J7 ]

8 n; s7 Y5 c6 I$ ?& C& O) H$ v6 a- a  Z. @1 H. m
官网: https://www.modsecurity.org/
  }  Q% Y/ @' @. L( g3 i* [  c, ?  T# o+ r3 m) ^8 x: d

8 \- l" X7 d/ a7 F* U什么是OWASP CRS
% y  B3 N- R- j7 Z7 J0 j& jOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。1 d, z. z. U2 Y' l
; O# l+ S; t' ~; {* G6 i2 v+ l
6 P# H! K& ^( o" ^2 H
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。  Z/ d4 n8 E. T7 w% m' e; _

4 l- h7 _$ S9 P8 L$ v
; `. b- }6 a' Y  o/ z$ uHTTP Protection(HTTP防御)" w7 q; T9 A$ z
HTTP协议和本地定义使用的detectsviolations策略。- E9 V1 i$ L- `" F) D: ]# R

( f( z- U( J) J/ z- N! f$ M$ j" }2 y( @+ f; `. \- x
Real-time Blacklist Lookups(实时黑名单查询)8 {' y! q' f, Q2 }
利用第三方IP名单。8 j5 q2 S4 I7 c. P

- i4 Z! O. K: `  N9 G. d% N! x$ E1 ~7 M+ ^3 W) D
HTTP Denial of Service Protections(HTTP的拒绝服务保护)/ T! J4 J: z: j; w3 u
防御HTTP的洪水攻击和HTTP Dos攻击。/ f/ E3 L' @9 j( X/ _# C

9 [' n8 J: p* Y4 ?5 o* u1 M
  ?1 p; E% H2 b  [( [# CCommon Web Attacks Protection(常见的Web攻击防护)& l8 ^9 }  J& n
检测常见的Web应用程序的安全攻击。0 R! B9 Z0 q: c7 l3 k# c

$ Y4 M5 n7 B  O' O1 m" u, R+ H  _1 N3 p$ A- H. f( i
Automation Detection(自动化检测)
5 f$ `. H& G  x2 U3 M检测机器人,爬虫,扫描仪和其他表面恶意活动。
( j" ^; P7 s. N  d# I
) `2 C! Z5 w6 X  j- s& l9 s8 K4 D5 B7 p
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)+ r; U( y9 i7 F
检测通过Web应用程序上传的恶意文件。8 r1 i7 Q3 y/ X! y- E/ [2 g, g: u
# I( Z& S" z, G" ]6 d! G# {! l; L

) k/ C/ Z( V1 YTracking Sensitive Data(跟踪敏感数据)
: c5 B+ t0 l3 `  z5 o" U5 Y信用卡通道的使用,并阻止泄漏。
8 o# e' w1 T* F  S& k1 w* u1 ~, O4 r& I/ O& V' q# ~6 ~

; p! q* h3 e+ V5 Y' m9 u( x3 MTrojan Protection(木马防护)8 }/ k/ d! d. a" a6 R
检测访问木马。! y( R  {2 R; Z  ]! j8 g

9 q$ g3 ~# o' r9 R/ y" A
8 o" _& i  O0 f( \/ z9 H8 }' Y- a  T2 Y( H4 ]Identification of Application Defects(应用程序缺陷的鉴定)
6 j& L9 G' Z9 [检测应用程序的错误配置警报。% i* \- U0 j8 Z* ~/ r
5 P: o+ I5 z5 d

* @* @& x: s; y8 P  KError Detection and Hiding(错误检测和隐藏)
, ?4 `; F5 W3 P检测伪装服务器发送错误消息。
' b; A3 }% K% i, q
# [2 Q4 Y, E- Z5 @/ R) C% c/ b. h
# F# D# q- R; M7 Y8 K9 i4 A- |. B安装ModSecurity: M( @- A( {4 [/ R
软件基础环境准备8 r9 g, I, K0 q$ n* M* s+ o/ Z7 z- {) l
下载对应软件包
' ^9 @& l+ g* Y- }; T$ cd /root
; C  W4 I9 Q7 S+ s; j0 P  K; d2 }$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
; Y3 \) ?2 x/ T3 B' a3 V$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
3 |2 v9 A" x, ]# i& m安装Nginx和ModSecurity依赖包
; [6 A8 U) V9 b$ S$ \. RCentos/RHEL/ j" b: s( d4 Z. H- z, |7 \( ]

9 _* X1 X. k% D4 Q$ _% ~. t3 H8 s, W* q9 M+ U; {
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% ^* t6 h% v( M" TUbuntu/Debian
; f; Y' P" k, A2 h; K% A0 z
  l6 c  |; F2 Y, S- R. l3 V$ {! T
$ 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
& @: Q: T" _/ I+ i% a# `; b4 C编译安装ModSecurity
$ K" y( j& \6 u: f2 A. t1 r& [Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
4 U' t& Q9 x3 i0 M3 ~0 L4 z5 d6 u" R. E$ z8 |" S

  [, o9 L! ?7 z* @) ~) F方法一:编译为Nginx静态模块
/ R; i% f+ Q3 N6 n- q" U$ a+ p. Q& D7 q- E" }0 A5 I. `
" f8 B( D. v: z/ f
编译为独立模块(modsecurity-2.9.1)* F9 P' i! d$ v. ^4 r
$ tar xzvf modsecurity-2.9.1.tar.gz
/ @* X: k" W7 R1 S4 f) A4 g$ cd modsecurity-2.9.1/' ~$ v, F1 W0 X8 L% @
$ ./autogen.sh
1 `$ O( h% o/ Q8 H4 ^) z$ ./configure --enable-standalone-module --disable-mlogc- }, @8 F( `% d" E  y
$ make  q1 E. J! I  A. O- d4 T9 }
编译安装Nginx并添加ModSecurity模块
+ `2 m+ {1 W/ `% h0 C$ tar xzvf nginx-1.9.2.tar.gz
  a& C$ L2 `' e1 S+ M$ cd nginx-1.9.2% Z' ~, ~. w3 e, X4 i
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/: y! J* w+ i6 Z. s/ ^6 |# `
$ make && make install8 d" E" \2 d$ d/ f& d
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* f6 V5 E" h% e# }% ~
- h  j' B! U1 F. y4 ~6 \5 K6 F( B

/ u2 I) n& t  \9 R; M编译LibModSecurity(modsecurity-3.0)2 G3 ]1 l+ W" |6 X( H
$ cd /root( n  ]1 k  K/ e+ A
$ git clone https://github.com/SpiderLabs/ModSecurity/ W, x2 j3 }; y" }7 x
$ cd ModSecurity' x* c6 y1 x3 x( K* s
$ git checkout -b v3/master origin/v3/master
* l* q* G- D' l7 _" e$ G$ sh build.sh( C2 x; c9 W8 `5 O' D$ T
$ git submodule init
- G6 }0 A5 ^* o6 \' w# g. y* c$ git submodule update5 z" X! G; ^1 ]0 O1 l: d( U% @
$ ./configure5 G% \6 D  h3 g: P/ c, r1 ]" Y. j
$ make
/ k2 j6 U+ ]8 ?  J$ make install: c2 i" U& K- v
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
- o6 C. m/ N6 w# A. p5 S) ?! {0 v: R. E( U) L/ c* Z$ O
4 Z3 Z2 @* P' j
$ ls /usr/local/modsecurity/lib
1 I- N+ X0 f* {' {libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0/ R% v, `8 R( U/ D
编译安装Nginx并添加ModSecurity-Nginx Connector模块5 U# ?' E; l, _
使用ModSecurity-Nginx模块来连接LibModSecurity. z3 x! L) W1 h8 O0 ~) d4 E( }1 X5 p
  V# P  _9 \5 W, [9 r1 J5 r7 ]! s

2 W) U% |" Q6 t$ `5 H$ cd /root% Z) J/ }- s( ?7 N* D
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
6 `/ s8 r0 E  E9 n0 w$ tar xzvf nginx-1.9.2.tar.gz
, g/ X. M/ w7 e8 j$ cd nginx-1.9.2# {3 E- K4 f* A5 _* F3 F* ]+ M) O
$ ./configure --add-module=/root/modsecurity-nginx
6 [! `) [. ]' `" h$ make7 q  D# {) t: v9 v2 `
$ make && make install
. d+ L. k0 z- n2 E添加OWASP规则- I! ?( N. R- U+ C, }* V
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。' \8 Q: [; d0 ^
) R, s& ?8 R3 o3 W4 @
  u( n7 `5 {6 ^
下载OWASP规则并生成配置文件6 q" h. Q) J- b, [6 t- J
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
( x. i' K% b; S) q, c$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, H- u2 U3 E, A5 N) ~4 u! u, a$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 @! T6 k% ^' A( Q9 s  I$ cp crs-setup.conf.example  crs-setup.conf
+ m9 W) K8 C- O% T% G配置OWASP规则+ x' o2 J. W) V' Q
编辑crs-setup.conf文件2 d. j  {- ~4 \; U8 V' j! L
) [! T0 B+ @& ^8 |% M% D

, c- S4 r+ [7 x& G. X. W$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
  R/ e% u+ w( E' P: d$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf4 p$ \" P2 z; W9 D: U
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 x4 ~9 v5 w9 l$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf# `: o+ Q" H$ N. K7 v% ?
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。9 v  h! r5 A  N3 d$ R7 e* j  p8 ]
9 C6 |# {5 T! u/ ]* x. L: [

  ^  G; w/ ]& O% t( d启用ModSecurity模块和CRS规则/ `) N7 X* h/ K4 A- G% l7 z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 J) Y" O; {* j- {1 {% m7 D# K7 i6 R( g  P! }% Y

3 [% a# R8 _7 t- `modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
1 }" Z7 ~& h' u0 \( T7 {( _8 k# r
; h" L2 o9 a, @# d
7 @( S& A) F) i" I6 A) U" }' d$ cd /root/modsecurity-2.9.1/
8 M2 E4 z' n1 I& S7 q7 X0 q6 H. B$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
* `6 m+ q* @6 [3 M! L) g5 u$ cp unicode.mapping  /usr/local/nginx/conf/' N9 S- D6 w) f) d% L% P: F; \% o
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。* w" K$ J) u1 v% p% {9 Z$ e

1 T0 J9 ?. R8 B1 j) F3 y( S: T( ?! @6 C4 ?8 a! Z
$ vim /usr/local/nginx/conf/modsecurity.conf9 y* z& S) j6 N' a& i2 G
SecRuleEngine On1 z8 n# o9 Y0 \1 C8 P, w
ModSecurity中几个常用配置说明:
$ d; K, D! D! m7 A$ d; D1 V
! @6 f# y" E- R9 u* E1 R$ n# K/ _6 }  C! y+ `
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。3 S% Q& V! P3 J7 @% M& M
& u; [- o( @& {% V' z+ g: ?
& |: ?! x- J* n' c- [
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
, _8 }' Q! N1 z8 d! d: d/ ~6 b2 B1 w- e& P6 d* a/ @5 R7 n
. `" e( d5 q. \/ |' a5 T
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ i4 v2 [( \% K! ~" }6 F6 j; T7 _
6 m# p, o) Q% O6 q* {5 Z
5 R$ }' `6 Z8 B# Z; A. B
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" X& s) H7 u6 V' X; t, g( k# X. o: R$ y1 ^' O6 G; d

# A7 {' k, ]6 H2 ]% L! ~& o2 |在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ t2 Q  V6 L4 c2 x
% x8 I/ {+ _8 m# s9 L6 W
% n$ v5 a5 S  H( a
3.x版本CRS8 V/ L7 m. Q' E' C, o  r$ `
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- A, ?4 q2 V. o% x
# 生成例外排除请求的配置文件
) D4 ~4 Q  d3 |+ l$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
* I7 Y, ^0 P2 q$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 y. v% ^' T' s" `% |* Y1 K
$ cp rules/*.data /usr/local/nginx/conf
" b, s6 J# A) R+ H7 l为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。% Y3 }+ z; J( Q; h, s
  k9 B% Z$ ?3 f
7 S& Z; d" y7 A' |, K) y
$ vim /usr/local/nginx/conf/modsec_includes.conf
- |; L: P! F, F; o) x" E2 Q5 X' g8 b; h* t3 H' Z
[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
) w$ l* T( O# E) c
" w; [/ g9 M! `5 F' g7 U' ~+ X; ]1 t0 J
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 i. f& O: Y- S, y1 |4 v
6 H8 E. g% R# b
7 I5 _' W1 L' R' n
配置Nginx支持Modsecurity
4 ~% n1 v2 T" A" F1 J. f启用Modsecurity1 {3 J" P, J# d7 {( o3 p4 K
使用静态模块加载的配置方法) X7 U" F$ A1 E+ B, {/ r
在需要启用Modsecurity的主机的location下面加入下面两行即可:
; _. v2 h$ }2 {' ?2 _. H7 G4 d; v+ {+ v0 C; F6 l/ z
8 C: i; d4 P$ p8 t
ModSecurityEnabled on;7 L' G& w; T! C" @! b1 o6 |
ModSecurityConfig modsec_includes.conf;
+ t2 I, d6 l8 `: b) }修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 d9 Z. [+ j7 f  s: v$ w

0 x4 f' v1 v4 R* }. v
* k. u1 k. p, B) h# @! _$ vim /usr/local/nginx/conf/nginx.conf' L) Z! Y' e6 y. h/ L# d

3 y* K) f; X6 S* h) W. j8 q% P0 Q% S! @- S7 L
server {5 L" A* P7 T; P
  listen       80;* ?) b* v) A9 c; D
  server_name  example.com;% p3 M* p0 d+ I, O
( y9 h: _' `* U" _) K

) e5 N3 I& B5 O$ |  location / {8 V& G$ C8 f# v; V( ?
    ModSecurityEnabled on;
. O5 ?! q( h' v# R    ModSecurityConfig modsec_includes.conf;. k3 s9 R5 H& }3 i2 C
    root   html;7 N6 j  D4 F/ \7 W
    index  index.html index.htm;
  T: S; d" I1 \8 o  }" K: W% m: O5 H
}
) C% N+ D: n2 p) {0 D" l; `0 ~1 U# d使用动态模块加载的配置方法( Q: Y" D" K4 s  n$ H3 ?- j$ w- c
在需要启用Modsecurity的主机的location下面加入下面两行即可:
% Z. L! i- [4 v1 [8 o" E/ y' I; A1 w, A' T

! U) c' i: f( z$ ?' N" k- imodsecurity on;
$ D$ T6 l5 ]. A4 jmodsecurity_rules_file modsec_includes.conf;( p# f8 Z! u9 o
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ }7 J: H) I' L& q$ d. S
1 v' h7 \0 \" a7 ?9 f4 Z# U
6 R# R( e1 g1 M$ vim /usr/local/nginx/conf/nginx.conf
0 n, F' ~! m# N  ^- s
( E3 u3 u- o" J5 I6 V9 p' Z$ Y. v! h2 a. K
server {
- O/ N8 ~; L) c0 A/ A( l  listen  80;6 l( V" P4 i0 |. t( i. k
  server_name localhost mike.hi-linux.com;
, k% f. K6 ?; M/ s/ T  access_log /var/log/nginx/yourdomain.log;* D, L2 y8 W+ E: R" a1 V" a

: Q- L' f$ v9 a: Y3 C
3 v3 z( y/ P( H  location / {
$ x9 U1 s0 P5 H0 G1 ^
* O* d0 E2 O; x; b$ b
' M+ H2 P7 V) W3 @  modsecurity on;; b* B1 c* S* e% R8 m" q
  modsecurity_rules_file modsec_includes.conf;
6 i2 R; G  s6 v) [7 P) g# L% z, F  root   html;
, @2 q1 H8 {& L  u1 b  y. |  index  index.html index.htm;7 s* s+ {2 b1 X
}  g2 |2 D1 b& d/ E
}
/ h* q) M9 ~2 ~7 V1 n, M验证Nginx配置文件
' X" w- s# u- P2 K$ /usr/local/nginx/sbin/nginx -t0 W, o+ N- b6 h  b+ J: a, Q% r0 m3 m
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
. y) I$ P. E6 f4 A! Ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3 f  ?/ c4 H, K6 g4 v8 x, ]启动Nginx) @/ y1 Q# f" M
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf; `, F) [4 ]( v" f9 ^7 p& ]
+ |/ y/ F4 E; P1 m% 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能够战胜更多复杂/未知/混淆的攻击模式。

- x* D1 w! L  `9 r- ?0 L, i! M5 b0 F

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-16 19:14 , Processed in 0.068168 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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