找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12383|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。; d6 n) b2 L6 ^1 K$ U; f% f
4 i" f5 ~7 u' N3 J. ?. v

. G& E, i2 p: x: o/ U在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 n6 W1 C8 o$ e( H8 L* P" F7 F) J' E1 V9 h5 \
$ s0 m0 ~# n$ I' l9 t
什么是ModSecurity  C4 k/ E. H5 C$ c
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 D, w; ]+ [8 b2 D& b' \1 q
* `& Q* |8 o( m( l) c, w8 g
- i5 f# i$ h0 k( x4 f) fModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。2 _/ M* U8 I3 M! t; H7 H. P( ~

- K, C0 l/ J. _! r# W$ \  e3 V7 g5 |" _: o+ K2 n, [
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 }# I  J1 t( u/ f) g$ ?0 T
6 k$ h& F0 b, v( Y& O' c

' P; i( t6 F" V$ k& h0 b) A' f官网: https://www.modsecurity.org/
/ P& P1 F* d& \* u7 S, d+ f1 {6 z0 ~, L1 r9 Q+ h# G' B" h4 R
" m4 }4 n0 \; m+ W
什么是OWASP CRS+ L# c, Q2 n+ e9 E1 S& |
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
. \1 q3 j3 ?+ \0 ^7 }
" C9 \# Q3 i* n7 }0 d* j' b' @
" G1 p0 g; G( e& J2 KModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。+ Z4 b; d, p6 i  X  \4 k2 e

& p2 c( G, s7 O; P2 A$ m  {1 T
$ q' I- K: ?. T( G5 M5 g" U! kHTTP Protection(HTTP防御)
6 [+ T$ d. `) v" y# R3 \7 sHTTP协议和本地定义使用的detectsviolations策略。
% ^* ~4 a& f1 ?4 B7 D& q- A  T' s% d) X
* j& E- y/ I6 t8 K: D# ?
Real-time Blacklist Lookups(实时黑名单查询)6 o9 ^7 e) b. |- Q! ~: O$ \
利用第三方IP名单。" E1 h6 v# ?* Z$ {1 p! Z1 r" C! r
! G. |$ l4 l0 j# [6 Z4 u
' ^7 Y6 L$ y6 ?
HTTP Denial of Service Protections(HTTP的拒绝服务保护)2 _' g5 k3 {* h2 S1 z' \9 o
防御HTTP的洪水攻击和HTTP Dos攻击。' D# j# i/ k- T6 n

& v+ A" q6 s0 M$ `# `2 s  t7 e# |7 @6 p$ f
Common Web Attacks Protection(常见的Web攻击防护); y" c( C* n& E: _6 ]
检测常见的Web应用程序的安全攻击。
) ?* p; o: S: [
$ Y2 B: X: I  n& L) g! T' O( J) r& m# I
Automation Detection(自动化检测)# @, B( Y& Z9 a) c. T
检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 E* w) r4 }, C: d9 ^% N
0 c; v# z' X( q/ Z3 V
/ Q; R( {/ M- [  CIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 J6 P/ ^- Y; B9 W; K检测通过Web应用程序上传的恶意文件。0 j( I3 ^7 E; D& ]
, J/ B" [' C+ L1 f. O, K4 ~# q' q
5 {1 d, t; d$ n2 D* v  b6 a
Tracking Sensitive Data(跟踪敏感数据), a" X: z2 l1 n' T, h
信用卡通道的使用,并阻止泄漏。
: S4 S. X/ I; }: A* X4 g* b# J+ }3 ]0 [5 S- k
* E# v7 ~8 [& x) K0 Q) Q" x/ @
Trojan Protection(木马防护)
2 k8 U" d- ^1 k! N检测访问木马。" B  O( X* U+ B" T

# ^) B$ V! C  T+ {0 {. C5 `3 o4 v. e+ {% J2 ]5 S' g- Z, c
Identification of Application Defects(应用程序缺陷的鉴定)9 o$ o) P7 y  G9 `; y5 {* v' R! d
检测应用程序的错误配置警报。
( V+ p6 @3 X; x0 b2 o9 N
- B8 V/ e8 k7 e1 |- {9 \+ [) K$ F: r  G7 f! y; r
Error Detection and Hiding(错误检测和隐藏)
1 G# ?2 J! ^' h3 }: Q7 `  |检测伪装服务器发送错误消息。' F+ @7 w: M5 ^0 h7 e6 e
  T) P5 G9 [2 t) F& e

- D+ r  _" P4 n5 Z安装ModSecurity; i3 S# v5 p( I) y* D* p
软件基础环境准备! A% ^; }! [4 [
下载对应软件包& L  p1 X! k1 s  {( Z* l$ X$ f. d
$ cd /root3 |$ W+ V/ j3 ]. w7 d6 ?, @
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
: V0 h, b% a) O  f& R2 \- m; I$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz" w, r1 o* y' \2 }2 ?
安装Nginx和ModSecurity依赖包
+ l0 y3 N; t' W/ q+ B) P- O0 _Centos/RHEL
$ g) U  u; h: P, ^
- q- t$ l  K; V6 X/ N$ H0 }5 g# c# c5 S8 E6 L( U3 m
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
+ ^1 H$ M$ ?+ D  e" ~' i6 FUbuntu/Debian
% n" r' U( Z2 G7 Q
0 q: T3 y( L: W# _9 }, @6 n8 T! b8 K
$ 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++-dev4 l, d5 `) A- M5 b* y7 M/ j
编译安装ModSecurity9 W' J8 ]. H8 Z$ k/ P% x" R
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% X! L+ b* i) u& O. k8 b# o, E

2 @6 p; U0 v1 _' M+ H! I
$ U5 |" p) m" A方法一:编译为Nginx静态模块
" {/ i) O) N0 M" d2 ?( `& R$ j+ `& B) D# H) H, B; S  }( H

* \1 c/ t1 N2 c$ C1 I6 a- T编译为独立模块(modsecurity-2.9.1)
6 m: m! o5 j" c$ \! e! v3 g$ tar xzvf modsecurity-2.9.1.tar.gz8 k3 n2 @" H. ^9 i5 }+ k) k
$ cd modsecurity-2.9.1/
4 ^* M1 l+ C( _& g1 U; x8 |( M$ ./autogen.sh
0 M( O% K+ U) O* n+ u$ ./configure --enable-standalone-module --disable-mlogc
$ p( |0 E$ t3 w: F) [; {' l, a* v: Z, ?$ make' N( m% K* _0 U
编译安装Nginx并添加ModSecurity模块
( E3 T" b3 X  ]! {7 Y# D9 m  j$ tar xzvf nginx-1.9.2.tar.gz
4 K; S: S6 K( K) y+ E6 ~$ cd nginx-1.9.2
% u8 m: J7 b3 x9 ]$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/! f3 \3 O& z9 O0 H1 s
$ make && make install
9 ~2 ]) n2 x, m  M  K; ], H方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& o  q" f0 m. B# h9 s, I5 o" ?  f- Q
5 I( d6 z2 z  j
编译LibModSecurity(modsecurity-3.0)
0 }+ C, C8 _- C* P" d$ cd /root/ M# M8 |, S' F; y9 ?
$ git clone https://github.com/SpiderLabs/ModSecurity
/ O4 ?& V% V. s0 U& I$ cd ModSecurity
# V0 {. c: D; L2 S$ git checkout -b v3/master origin/v3/master$ j! F( {  }. |( ?& A
$ sh build.sh
; W: g: B( g. E/ ]: {$ git submodule init8 Y, x5 r7 V3 N- C& _& R5 ^4 s. x5 k
$ git submodule update" N* E* y6 o& q! _( t) S  W7 D
$ ./configure
$ T$ x5 W; _- w& z$ make) F( ~8 g' u/ S, [. j) H% S
$ make install
$ k+ N- b1 f# G. bLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
. X; d  q# E8 Z) l: m: T) i# u- G- @+ p: L; f

- S  [- u! M- f; m$ ls /usr/local/modsecurity/lib3 o. w( {" m* o. B) G2 y, m
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 Z5 x# m5 W; E# I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
0 P/ Q8 V# ?2 [6 b. K2 }3 H3 I使用ModSecurity-Nginx模块来连接LibModSecurity# v) k7 ^- F- M8 W1 M& Q/ |! ]2 o

: N3 `- x0 p( y( ~/ G# u5 @. ?( }* Y, y- m* J
$ cd /root
  n& H: H# K( z7 ?+ j$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
8 W3 ~/ t" ?$ C% W( J$ tar xzvf nginx-1.9.2.tar.gz# N: ?) }% Z  T' Q
$ cd nginx-1.9.26 g7 r4 M; D2 e& X$ i
$ ./configure --add-module=/root/modsecurity-nginx2 l& |; T: D4 |) H% W; L4 j
$ make, e' S# {. F4 i
$ make && make install
+ E9 B+ [0 x- f3 ?6 a& L添加OWASP规则. S# A& D6 \7 @; a% z/ L1 @) _. X$ c
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
0 x) f8 C: o/ D* H" H& K7 }0 `+ j  b- c  e% t7 E

9 P8 C+ D* {2 [+ f下载OWASP规则并生成配置文件
% Y3 z) y8 p. V$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 I! @0 h- y0 I' I% E- k8 ^0 k: P6 b$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
) E0 f4 P, N: Q5 F/ d& U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( l3 \1 f1 m: h7 z. O2 k+ T9 Z
$ cp crs-setup.conf.example  crs-setup.conf5 Y% A3 k  r% A. V  S% L
配置OWASP规则# Z; Z  u# M8 x( e& T+ P
编辑crs-setup.conf文件
4 t; R' j& a; z+ |  T3 t) h, G* T
6 ]- |5 D- N0 V/ q) ~5 ^. i) T' S; H8 O2 G# l/ j  G* @' _
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf' g% @; @: O8 @
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf' ^8 ~. t' d4 r) u7 |- Q
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, }. s7 h& y0 l4 }( V$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf  @  G- ~0 j1 h2 Z: B
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- A& g9 Y4 \/ {! \, M  v
1 e0 X# O7 v" b7 w. T& t, X- `# a  ]/ J1 G  g; R
启用ModSecurity模块和CRS规则
0 L' O# W' f6 ]( u4 D# L! n2 j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。  d# e# b) f) L5 ]7 g' k
' a- O7 Z. _. e/ \  l* I
8 U1 R0 M9 T3 y  A3 X  V
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' o% S' _8 W8 W/ x  Y. w" R

2 K6 w( V" U. R' D) l4 f) Z
" Z8 A* t8 V9 _$ cd /root/modsecurity-2.9.1/
( @" G6 u- m6 g! A+ `; m$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * S  F0 n( n5 \, c3 z
$ cp unicode.mapping  /usr/local/nginx/conf/
' X4 E) a2 p7 z7 F( _9 B  S+ R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& E. C9 v% E+ c* e3 d) N8 N" V; g8 w- Y9 T5 ]" a  K# O" g( @

. a# e' Y4 t8 M: z$ vim /usr/local/nginx/conf/modsecurity.conf
) d0 K# N$ m' GSecRuleEngine On9 n, a6 Q. ~3 u( M# U
ModSecurity中几个常用配置说明:0 @7 f1 x4 A5 A9 L( ~- ?
. k7 H$ C* B1 Q2 Q3 I- B+ @( i
, L4 D' a# ?" u  {
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。% T4 z9 J- c  @( |" J
8 @& i  W4 s2 Y
/ |: j: G; B' i. A
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  T  `. z( E+ z( n& H  `+ B8 C7 s
2 N1 X2 `: k/ }$ S  ]
6 }/ t& K) u7 c. f$ m
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 `  D6 P8 {5 Q* X

2 A1 r! h, N; V) B( t% Q( Y7 B% u. U& @/ e, d/ E! d. Q9 u
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。! G/ o. m4 T, B
! N3 _& w/ z. p5 d
9 @& m: c( X8 F) D9 ?9 W. u
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
1 h5 `+ b8 e, B( ~$ z" M  x  a
" u* Q/ m7 @* y7 v9 X
3.x版本CRS& F/ @# `+ ?- @. s+ h
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
+ Z3 ^# I4 V; f# 生成例外排除请求的配置文件
5 b, H2 v! f' L7 J  a$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
1 E' `, d. k9 ^$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
8 l  X# w; L: L# c5 [, O7 d$ cp rules/*.data /usr/local/nginx/conf# u; w% h1 r! c9 E
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
0 [9 C' Y4 j8 j6 P! h& V( \; G8 @* O7 n# T2 v" d7 u5 _+ p- m3 c

  M6 q9 x( |$ `$ vim /usr/local/nginx/conf/modsec_includes.conf
, n; V9 f. i! N8 g
. D1 o. `( _4 ?5 C8 J- X3 x
[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

' y. Z; d* p% ^
' }/ u4 q6 D7 D; ?注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
0 x0 x0 C# c6 t- z. s" ?" H& j, ]4 A  E' \; k. p

& ^# E* M% v" y+ K% p, t. \配置Nginx支持Modsecurity4 H  g8 C; `3 }1 G9 t$ w
启用Modsecurity) R( B: A1 Y  j8 I9 g7 H
使用静态模块加载的配置方法
. j) _) R0 c+ L7 `9 w2 w1 o在需要启用Modsecurity的主机的location下面加入下面两行即可:. x" k* {5 j# T
8 V0 b# D! ^6 e8 m" r# y
. W+ I7 U. W' f. a
ModSecurityEnabled on;7 P! O7 B+ |, u; Z
ModSecurityConfig modsec_includes.conf;
0 s9 N# v4 f5 A9 o& U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。8 p' A* j5 {$ @) I

3 U% k4 y, f% o( ^
; K6 U+ z+ f" ~2 q$ vim /usr/local/nginx/conf/nginx.conf8 P% Q( @7 ]+ o* y; X( Z8 G% d
3 |9 _# V  B5 Q% ~7 X+ S; k

% m1 D) l1 X3 k. wserver {
' ]: }! C# d: I( U$ \+ R  listen       80;
$ R; c9 j+ b% k7 ?  k$ h# [6 B# `5 |  server_name  example.com;" Q# S1 U7 K, a6 A4 \: l! ]
, l9 \7 @! N% C% u) P4 I% t

+ }, x; \$ ~  A4 b  location / {
( Q" k6 x) x$ \  L9 U    ModSecurityEnabled on;
/ N4 [( U2 \+ k8 s* \    ModSecurityConfig modsec_includes.conf;
: r) x: W1 q7 n. l3 U    root   html;
* f/ Y- I+ s8 j) i' q, G# i* E$ p    index  index.html index.htm;1 g& R$ z1 N& V  I$ @$ k0 I% _3 u
  }) \0 a1 T) \% W. a
}6 Z% `+ c7 \7 r  ?) w1 |
使用动态模块加载的配置方法
8 E, q% t5 d3 }# R; q在需要启用Modsecurity的主机的location下面加入下面两行即可:" |% J: z% B; O
2 p( o& K8 ^/ i- a$ d/ K2 |8 @
' D: J: H+ i5 [( S4 [
modsecurity on;3 Y( q' h  b$ B" W
modsecurity_rules_file modsec_includes.conf;: ^% X8 l2 ]' ~
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, u. j; ^, b* s- `( X+ W; k

5 B9 g0 B* o' s& H& x" A3 \
+ v1 l0 O% _: s8 q+ L4 u$ vim /usr/local/nginx/conf/nginx.conf
5 [% \# q4 \$ G; A$ v5 \# v
6 ^+ W& s7 f, U. U6 q
+ X9 {7 w- ~+ H. g8 G5 Q) A* Mserver {
" \! Q; J  x. s% I  listen  80;1 y& D% l% R1 n+ [: T% H
  server_name localhost mike.hi-linux.com;  O9 n6 b( \% b7 y3 n6 s
  access_log /var/log/nginx/yourdomain.log;/ h6 H$ y  \8 I. E. u

; ~1 j8 P2 Q- }# _' N, f4 v4 P5 J7 g( E8 p6 l7 S. g
  location / {/ p9 O9 g7 V; p" `0 M! `6 z2 c5 C

/ n5 ?: |2 D* z/ K: U6 u4 x; F, ~1 ]$ u' K
3 c6 B( e* \6 z  j+ ~6 x+ X  modsecurity on;
/ Z7 F+ H: |+ C% z7 T; {9 I, Z  modsecurity_rules_file modsec_includes.conf;
0 l: A7 a8 N5 l5 n4 S  root   html;
7 |1 [# Q" r8 M; R+ k/ ~2 ~5 q  index  index.html index.htm;( S, [0 q2 ]5 r
}" ^: U1 O* X3 _. M) D0 M9 F
}. I! u( j! H& O; V
验证Nginx配置文件
* Y: _5 o" [2 h3 l  f9 L; @) k$ /usr/local/nginx/sbin/nginx -t
9 t7 h( Z( }: ?nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 Z2 c" G: I7 X9 w- v( G3 m, L
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful8 q* l  J1 x4 Z+ F
启动Nginx
$ c; ?" X2 b, l7 M$ {- W5 a$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 c6 k3 b( a+ w0 t5 U! R4 B1 J$ }9 T2 b' C/ y% y5 }

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


! Q2 v4 g! V) J& Y; M

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-17 11:53 , Processed in 0.133769 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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