找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12895|回复: 0

利用ModSecurity在Nginx上构建WAF

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

+ ]& I! R  y, @2 Q- H$ g; O3 f4 W) M' e5 z6 k
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
! _" b8 V4 p6 V, ]" k! w3 F
- J# r7 j3 v9 J% B' x
* m( N' J/ o* R6 }什么是ModSecurity1 Z/ G/ A! t+ d' J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, u: c: t6 |$ {! y
9 @! E# L: U8 [( ]* K. _% c, F  R& R8 L7 Y. c+ c, _' N
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。- p  F+ E3 [2 t+ f  ]3 U
) L* U# e# k: g- G7 d" l6 x4 Y
, D" M/ z1 ?) {, {8 H$ O4 h/ z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% o) s) h1 i' i$ Z% T; n
, ?1 a- c4 ~- d0 z4 E; r0 k
+ J6 h5 @" |; C0 w4 w6 h4 x
官网: https://www.modsecurity.org/2 D) K9 G9 p' T) ], h2 s
; ~$ U: l; S/ J8 [# |
4 y8 j5 K; c. H" n3 K9 v; k* ^
什么是OWASP CRS# q) i0 i" c% E, s: v+ t: v; K
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。( ?/ M, A2 Y7 u9 Z7 i# c

$ r9 v1 S  _1 ]7 d( _, g( [' Q; f% O) A$ q/ t9 `$ Z
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# }- `. C; [5 H; j3 `
( Q; x) Q- |. G! k  w. t# `1 E
: d9 h$ K! s% n* Z3 ]+ w
HTTP Protection(HTTP防御)
9 O9 I* o" b# DHTTP协议和本地定义使用的detectsviolations策略。- r) B7 C( M) x3 H& L
( M) a) c7 @* F6 n" u
" h6 \- U+ |' n( ?8 A& K6 L
Real-time Blacklist Lookups(实时黑名单查询)
( c5 a' M' @8 g6 z利用第三方IP名单。
9 _/ ?% O1 T$ M; R) u- R& V0 v7 H: J8 i* d

8 V6 r, j8 t9 Y. m: e/ kHTTP Denial of Service Protections(HTTP的拒绝服务保护)2 a! P4 k% y9 G) C/ N
防御HTTP的洪水攻击和HTTP Dos攻击。
5 w$ N+ S- E$ ^1 S. j3 n+ C
" r/ X9 |: f+ x8 F$ V# m6 |9 ^; D$ ?$ o: F
Common Web Attacks Protection(常见的Web攻击防护)( p6 G- l6 ^( s( X
检测常见的Web应用程序的安全攻击。
. ?& k! Z: `' g
2 n) |* g% v5 Q; |) }$ [4 }. J8 @' m% ?( }  j  m2 V
Automation Detection(自动化检测)
( j2 @+ b1 S9 w检测机器人,爬虫,扫描仪和其他表面恶意活动。$ d0 @5 \/ q/ c& V! }% X

7 `  J$ _) b0 O3 `2 j( ^; B
/ F+ ^1 O; [9 AIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
" A5 n; V$ C. l: s! \4 Q  }检测通过Web应用程序上传的恶意文件。
! |. T8 j; \+ i! }/ Y/ J" [- w% N2 v/ r! Y
% }) C# k& i9 J. d5 [6 k' ^
Tracking Sensitive Data(跟踪敏感数据)" C+ C3 D5 F" {: |
信用卡通道的使用,并阻止泄漏。
( i  \& x: l' L5 U+ y
$ X; y9 j7 E1 o( k8 L9 G5 A- i4 `$ x) C  g* [
Trojan Protection(木马防护)
* e" J$ r- u" d% p" y6 i检测访问木马。0 b3 U' d% u/ X
8 {% Z; [, \" R
; \2 }3 a* x3 h( }- ?! l/ j! Z
Identification of Application Defects(应用程序缺陷的鉴定)
6 b+ T0 |1 r. ^. p3 |! b& |! i检测应用程序的错误配置警报。
+ g" T6 F. Z5 g4 R' s* t4 T8 G2 A; I' C
: s$ \0 P- M; x4 o
Error Detection and Hiding(错误检测和隐藏)
0 i5 C8 d. D" k4 [  B9 ]$ M. b检测伪装服务器发送错误消息。
$ a! T+ h# I5 h* n& Z2 t
/ G! [) C, l# ?* E6 D; A3 |% i) E% j
安装ModSecurity
. `4 P! b3 P, L软件基础环境准备
9 x3 ]. h  c# P1 u下载对应软件包
* z0 T# A) m" ^2 P$ cd /root
+ E; E1 B0 g# t0 n3 _' T$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'& s% h  b0 d0 }7 h' I- E2 l) ?5 L
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz! K5 f& i) W1 }$ `2 f8 ^
安装Nginx和ModSecurity依赖包
. G- G  s! a4 S" \2 R: HCentos/RHEL" {8 C$ L8 K* X1 w: e6 s9 V. _
0 \* {3 n+ Z6 s* x( |" a
: U) f3 r: _7 r  G4 _* D; E7 f
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
. K4 F! R+ ~: r! DUbuntu/Debian0 L9 ?: x! `+ w' P6 W* L. @
, S2 h* z, x2 y  |, C) B

& k, p2 n) `& P$ 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
, @0 X. H4 X+ D% l1 t7 y7 a& v编译安装ModSecurity; W0 M+ K6 w$ k# A; d  i1 P
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 u$ a- f1 x: ]4 d
) r# v. v6 I' O, }
; e( M( k$ I6 j
方法一:编译为Nginx静态模块$ j6 p3 V  @8 u+ p- t8 \7 M7 p

! A3 K2 u# G8 A
4 g6 z" [4 D3 d7 w编译为独立模块(modsecurity-2.9.1)& ~# V. y' d1 Q2 n& A
$ tar xzvf modsecurity-2.9.1.tar.gz7 y) q+ m0 @- ?4 P( |
$ cd modsecurity-2.9.1/
+ z( E  m1 a- g9 _- d" o+ _9 ^, V$ ./autogen.sh
; r; D/ h7 T* I' \+ J* ~$ ./configure --enable-standalone-module --disable-mlogc2 f# s/ @0 h5 A  D* W7 i: x
$ make- i2 T2 j) I6 x5 g
编译安装Nginx并添加ModSecurity模块
' p2 n4 O5 c. h/ l$ tar xzvf nginx-1.9.2.tar.gz" }5 a% B: X; F! M# I5 A  ^
$ cd nginx-1.9.2( c5 i. k/ H1 H" n' j  `
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/+ Q( l. w8 [* A& l7 ^
$ make && make install
& `. G2 S4 t+ Z; C方法二:编译通过ModSecurity-Nginx Connector加载的动态模块( t6 S& @+ {7 {. h& \

- |; [) `" q' D( w
7 k8 K! N( n6 C编译LibModSecurity(modsecurity-3.0)
$ n0 \  |9 {" w2 b2 P$ cd /root  d# \; q. }7 G0 q8 L4 H( E
$ git clone https://github.com/SpiderLabs/ModSecurity
3 c: f: k( E$ f6 X) W7 ?. r. ~$ cd ModSecurity
( \2 E/ h9 k/ ]$ git checkout -b v3/master origin/v3/master; P' X. z3 V7 ^. q% L* d9 J
$ sh build.sh
4 ]2 }, \& I; e! V. O% w$ git submodule init
; `* F$ r2 ?; r4 y7 m$ git submodule update
' {1 ?( a7 I. b% c4 n$ ./configure
- {- A8 Z: I  c5 X; F! ^) V$ make
+ w) C! V1 c8 W$ make install
+ |3 J- w6 v# k: T% [; e9 l) e$ @LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& b( I' ]$ ?: Z: `  z2 \. Z  j, B
8 _) E7 R; }& G2 }" b$ F4 F% r4 F9 V

' C- L- y; E+ p- x. f( x* R) S$ ls /usr/local/modsecurity/lib
7 ?) l' c5 b  {" p& tlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
& k, |( Z4 Z. x( A2 ~: ]编译安装Nginx并添加ModSecurity-Nginx Connector模块
' N+ H6 R& x7 t7 ~$ C使用ModSecurity-Nginx模块来连接LibModSecurity
: |4 E* [% `( C% ^+ P0 g2 }2 b' @; t) {6 A- ^9 [2 ?/ ]# a- H

* Y' {# ]) {7 O( [$ cd /root
$ V/ Q- [' Q4 f9 S( S$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx* ^4 D5 D1 Z  `
$ tar xzvf nginx-1.9.2.tar.gz/ b2 Z5 ]) I8 Z! |; x9 j
$ cd nginx-1.9.2& M% H& [/ ]! w# s7 h
$ ./configure --add-module=/root/modsecurity-nginx. Y0 a' w2 j; ^6 h; `- E9 R
$ make% o$ m4 z( B& T/ o
$ make && make install6 P1 p( i+ ?: {
添加OWASP规则9 f* H* u! X% P$ i
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& R! ^% Q' a' Q& V
" j5 y. ~; e" J; X
2 o! E  y0 N# g) S9 ~4 e4 \, d# Q下载OWASP规则并生成配置文件
$ O. M4 \) u6 E+ n5 B. z, t. n/ d$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
" q/ C$ i  g, x: W6 R' a9 w. `$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 z. D" {7 S3 U  Z% ?2 q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: E* |. o  \; S
$ cp crs-setup.conf.example  crs-setup.conf
2 q8 I: z1 j, i# w9 W7 n配置OWASP规则
3 A  J& |! J  J6 r. M3 u9 i编辑crs-setup.conf文件
' E  p# u! y' A
1 q- {/ ]: s& V: q1 H+ I( }& [& j& r. _  S7 a0 N
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf* x* U. `# r  J9 ?1 i
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
" k" I* u# W. a- Q2 F: \9 r. I- n$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 e( y$ m! e- R3 F' d: {
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) D. m$ F: x3 e4 D# x, K默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 j* p, T0 {5 D6 a) a( H8 E. L
0 ]" H# }, M# C8 q! Z  x5 j7 _

$ R, }5 C1 W6 L2 K- v启用ModSecurity模块和CRS规则
5 N: w: E6 C. w, }' m$ x3 r. f复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# _1 y/ |4 W/ Q. k  }
8 _2 g) R3 E# ?( x6 l8 k6 W

( B: l2 t. z6 E4 ?) ^+ R( h! V2 M$ \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& S! z& \% [: o3 Q  `

* r# g) S# k4 i, u: i) P0 l' b+ P  ~, t) U- C% P7 D: B
$ cd /root/modsecurity-2.9.1/% w' D5 N1 d- ^9 h8 S: ], b
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  5 z- I8 X6 }9 K% n
$ cp unicode.mapping  /usr/local/nginx/conf/
( `% U# g& _$ e7 Q8 ]  ]' e将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
4 h. f" S6 o3 Z' R; Z0 |* O7 h3 q- Z* \: P8 z  N  s
) f& ^; J8 |) G- n! E+ I+ R6 G
$ vim /usr/local/nginx/conf/modsecurity.conf
0 }1 w4 {7 ^. q% hSecRuleEngine On
6 d6 u, ]( r$ }2 {- H& t9 ]ModSecurity中几个常用配置说明:
  O8 N1 u4 f& J6 o9 A4 x( A4 v* U7 G; g4 T2 |

; ~# ~6 o- ?3 E/ Z4 P& V1 d9 P1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
' G, `2 S6 P( B# A; `- l0 ~2 ?' z- X0 c6 Z3 ^; _/ u0 O# c9 G
5 U+ |: j* l1 Z- q" V; i, w
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
3 y  w2 m2 u9 g' u( Y8 I# [( ?" `$ V# @' }' W( {1 R5 D

0 U1 a% H0 E4 s' i/ i) K- }/ @5 w3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。6 L9 k; G1 ~0 V7 c+ s
4 W- D# C. g) I

' @. ?& \! G; p' S8 r6 k4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: @7 J& T' m7 s* \0 J3 l# L$ [

$ y$ m: v9 f- n# W5 V/ ]1 p1 h  y. P7 q" j2 B; d* u' P
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 c/ O! F& c8 z0 Q4 H- l* ?9 C3 A! c

+ \" c0 R6 _, t% t6 L* R3.x版本CRS) I9 e. i1 C$ r
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs; i% x  k7 w4 ?7 j/ v- y4 j5 C1 C/ z6 {
# 生成例外排除请求的配置文件5 S% G: T) Y) q3 H( G: B
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) C- w* i" o: M1 p% e5 C  i$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 H; b2 C& N! i5 Q
$ cp rules/*.data /usr/local/nginx/conf
: _/ z/ Z+ K8 y- v2 V为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
) l6 [" a" U( F# w5 m" M) s* d- v3 ~6 V6 r* c/ S
; x  F/ |- k" r  m: @
$ vim /usr/local/nginx/conf/modsec_includes.conf
: D2 \/ y2 P- J8 K/ }. ^5 I$ f6 X& j! |; k
[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
" q8 b" G1 u! `! \6 |8 c! c3 r
# i$ ^0 E, p! d) O( Z% ^
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
3 V$ V, i/ O, |3 U3 k
3 ^+ n( `- J! D2 M0 A. l0 I
. w# c  }6 [* s& w8 ]$ u配置Nginx支持Modsecurity
, b0 l+ ]/ d8 ^( ]" H/ m启用Modsecurity/ M7 B* p( e& o$ ]3 n- m
使用静态模块加载的配置方法- P) G7 P) b, t) k7 E2 a0 ]1 k* A
在需要启用Modsecurity的主机的location下面加入下面两行即可:; D4 M2 E$ T, `6 ~9 |7 R1 g
! u& d. e" O! C/ e

, n+ N; I( @2 e" u8 t0 R: y4 sModSecurityEnabled on;7 Z( g& v( u3 i& X0 D& ?
ModSecurityConfig modsec_includes.conf;. a/ W$ \: |& W% f9 U" J2 }( t$ x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" B/ G4 E) G3 `  V, o1 i( B$ N/ t7 l# t4 W+ m3 a$ A1 T
3 i3 f3 r5 q! I2 V. X3 f6 I
$ vim /usr/local/nginx/conf/nginx.conf. I: L  `: }5 a0 w/ B6 z( ]
+ ^1 a  T/ r! C0 v5 i* _
- g! T6 v1 B0 y& ^" n. z7 `
server {
$ k* w; W4 c$ i9 t8 |4 n- y5 S  listen       80;
& Z6 m' [2 z+ w* e3 F  server_name  example.com;
# C4 Y- t& t+ h4 \4 d3 _& Z
/ _$ e4 U* N& d/ T7 ]' ]  d& ]2 T0 D
  location / {
8 U  i4 e( }/ }6 c% w    ModSecurityEnabled on;! V3 C/ X# r8 u% R' z. Q# J' E
    ModSecurityConfig modsec_includes.conf;' ^& ^8 L2 p1 G8 ?% R. ~) q2 J
    root   html;- ]5 i$ N3 Q# v7 `( R
    index  index.html index.htm;
/ p& @% A, z+ I" l2 K! M& a  }. U0 n# K5 g) E5 ^) O4 Q
}/ n& b& k. u7 n5 T  M. h1 `! {3 M
使用动态模块加载的配置方法
; ~2 D1 M* `8 d7 R7 v% Z在需要启用Modsecurity的主机的location下面加入下面两行即可:
9 ?# D3 b4 l8 X/ `4 ^- g% r( \" ~4 P# @% y/ E( s2 Q: C  F, K7 [9 ?% Z0 l

+ o2 B) ?- p) d: }. O5 Ymodsecurity on;
8 S- W5 q; u* g! c% B% j' Amodsecurity_rules_file modsec_includes.conf;8 E6 V9 T/ Q* K" z! r
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 p3 n1 k( u1 g  u: D, A/ J' M, }) o# G% p+ s

. `0 M7 U* C. ]$ vim /usr/local/nginx/conf/nginx.conf% _" k5 }, b# B. p3 t- V& I7 t+ `
! y$ U$ t8 A) x! d6 i2 ?

6 w8 }; x  N7 B( Aserver {7 e" C, n" s0 G7 D
  listen  80;5 l6 J! S' J5 n& }% [5 _* d
  server_name localhost mike.hi-linux.com;- r5 s; z2 T9 ~2 A& N/ x- c
  access_log /var/log/nginx/yourdomain.log;
6 X9 `0 c  w) b4 D, U" X: |6 ?# {6 }9 w9 h

/ z% q, \1 J5 F- U9 f" I3 n  location / {1 K" x' s' c8 y- U9 A
! j" ~4 Q) l4 V8 A

, G6 _4 ~4 P' X2 t4 U. s  modsecurity on;3 c! \7 n/ H( K4 H9 D3 j
  modsecurity_rules_file modsec_includes.conf;( N5 m1 b- i, t4 t# O
  root   html;
9 C! s3 H- R: x3 u; X8 w. Y  P0 r  index  index.html index.htm;
" W) n5 Q- I$ L6 u1 ?! m3 b}5 V; _! S9 s, D* \& G1 u
}
3 J: d& U1 r+ H$ R8 Z# t! N# B验证Nginx配置文件
" J" o3 S2 h: |$ /usr/local/nginx/sbin/nginx -t6 E" y4 Z8 q! W
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
; y9 d  n' ^4 Y2 [4 W: {( g9 @nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
% s6 B' b( ]5 ?$ u( ^" `3 P2 }  M5 k$ L启动Nginx
* w6 V2 M( r# {: @$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 B: u% d0 y, R) u& }0 m7 E  N; s
7 F: ~* _3 x5 `# y

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

; f4 n, w: ~' ?" k6 g, |

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-11-29 08:02 , Processed in 0.071745 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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