找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10308|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% v7 e& V, g7 q" ?5 ^- s, E' T. k. ^+ r
) z# w9 ?, L3 ~$ d- v
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。: u) W- |* [; ?8 H6 J! |( a

; Z' q  ~6 j8 U& M: `, b: X7 d& {: R) v5 ^- ^* h
什么是ModSecurity0 h. G+ a3 Y0 }3 \
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 S# H6 U' g6 S

6 I( X; v  ~" t7 ~) ]2 G( [; O$ i' r
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) v' l1 K% S) l; a
* @6 M" g, s: c3 E& p7 q1 n! i. h
( Y3 E$ m; l- ~6 W+ B+ _
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。! [$ ^5 C1 A8 X2 Y& n

0 W5 }; V  o, P
9 F3 h; ^, ^( u官网: https://www.modsecurity.org/2 j% T( u/ \. k6 m( A, z

+ c& V4 W3 \$ K: j6 X
# f$ L  ?+ J9 u9 C0 {% I什么是OWASP CRS) q8 f2 I  g: F9 w; ]" o/ k1 o
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
; m- E6 T+ d0 W: a* G  w4 @( v+ o$ U+ [& ^# {' r0 P* z- A( x1 o
' E: b# d% @8 }; f2 j- e0 ]
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: U3 G' f; P9 ~9 k+ w5 D
7 R* S8 r; r# z4 R0 p, B

' S7 _# Y0 e7 J" e; zHTTP Protection(HTTP防御)
! Y0 y% J6 }; P# nHTTP协议和本地定义使用的detectsviolations策略。+ P+ L  _6 _7 z; ]5 @/ @# ?
# t7 B0 r7 E$ ]9 |6 w9 N8 o

, c8 y& F% q6 K# e# NReal-time Blacklist Lookups(实时黑名单查询)
  J3 F( A% T$ c; {利用第三方IP名单。
4 Q1 h) s1 W. n7 Y$ d; ]
2 D% j2 N2 Y; `$ F8 W
" r/ g6 d: |8 Q5 MHTTP Denial of Service Protections(HTTP的拒绝服务保护)
! c2 W4 V9 D( ~) @# e6 }4 X防御HTTP的洪水攻击和HTTP Dos攻击。
+ \3 E+ d( r0 E2 Y: f5 u; l
# @! X) @; H- U  A! C* \/ r
" g  Z9 Q) }2 b! k; g6 zCommon Web Attacks Protection(常见的Web攻击防护)4 V. p- k+ ~2 ]  H( k! B. H/ W
检测常见的Web应用程序的安全攻击。/ L" {* b+ A1 G4 N, P/ Y- z7 f. \
/ {% A3 x/ t& P

, O, f! l5 t" x7 Q# Y( OAutomation Detection(自动化检测)
  ^+ F9 T0 C; r+ a* F9 m检测机器人,爬虫,扫描仪和其他表面恶意活动。% t: _  V8 M* ^( g! f/ z+ [

& j* j9 h0 B$ |* E/ \2 Q3 s/ W- _: P, Q3 Z
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
+ `3 `: S+ v* q' x4 J检测通过Web应用程序上传的恶意文件。3 t# v9 Z7 ?0 l) ^" p" f4 [$ [6 ?# C

5 ~  D9 f- O3 _. b" Q( X% H
2 }( r1 B5 Z3 q! b9 M: z, U6 ATracking Sensitive Data(跟踪敏感数据)
$ s  `1 O1 N% \+ C" ?信用卡通道的使用,并阻止泄漏。. r: O" ~" g$ R2 c, w# ^

; Z& v* B* `/ x& t5 ^2 T; X7 U) j
$ ?$ T5 t' W1 w! }Trojan Protection(木马防护)2 ~* s# y9 d7 t) T' R9 A: ^) c
检测访问木马。" o9 Q1 K5 O6 X; h# r4 ~8 p
' R3 X7 F+ a3 @/ m

8 F% S- z, w2 e% R$ pIdentification of Application Defects(应用程序缺陷的鉴定)
" G/ W* }& y" `" y! }6 I检测应用程序的错误配置警报。$ [, o5 U5 \( }9 W. @8 `
! V7 }5 f) M* T2 s! `
: y9 L" _$ c% L, O9 O& m
Error Detection and Hiding(错误检测和隐藏)
) k$ O$ I: ^6 e+ h( B# o& }检测伪装服务器发送错误消息。
/ P' y# u3 a6 ]9 d$ T2 h2 G
, E, I5 E5 d8 c' N) h, ~5 d; A  W) S2 ^/ v
安装ModSecurity
' j" [. D) B( |0 O软件基础环境准备
$ w: P  i3 W7 h! @8 Q下载对应软件包: `9 O% c: T! J% Z( j+ t$ }  S
$ cd /root; m1 |$ ?3 Z+ i1 d" \5 B2 l
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'8 B! y4 {' T5 R7 Q6 f. F: H- B. J. G( G
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
- C2 b: ]+ U5 K5 U* a0 d% `6 }安装Nginx和ModSecurity依赖包
; w; L6 D  B- M8 J3 }# k" {6 c$ eCentos/RHEL
- k' P& D3 v2 `* c/ [, C1 X$ q; U+ B& [' b% R- R0 ~& M! C$ P

; l. U4 k( P) s8 j# N$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
3 f8 u7 I. n2 l3 x7 p; s$ w% m& PUbuntu/Debian4 Y% ^9 E3 o5 r: s* N- i3 v: d
6 c; ~) D+ F" `3 V7 k. M
: O! F6 }" H* V4 G/ V4 ^6 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 k' I2 W+ e  K! A
编译安装ModSecurity+ V3 J# R9 B  m+ _
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。7 h. X( Y5 N: I

9 {2 R8 b0 g/ |0 [' P5 |) R" I! a7 h* s& ]) r1 [
方法一:编译为Nginx静态模块
% ]3 s9 T7 u3 Z; @+ Q; Q" Y  C, Y  O3 o. ]& A3 P2 G
$ q* S( D: Y, m$ M
编译为独立模块(modsecurity-2.9.1); V" Y( s1 W8 v4 i
$ tar xzvf modsecurity-2.9.1.tar.gz
. x! F4 Y( p9 [" V7 f& S- g3 [$ cd modsecurity-2.9.1/
3 _- x' z6 J: Y( ^* u* i5 v# E$ ./autogen.sh
1 o! j: m$ N6 Q: n! Z7 q% S$ ^* f$ N$ ./configure --enable-standalone-module --disable-mlogc; R5 `/ m7 o( C& s9 E5 X
$ make
4 j9 c* h2 ~3 U' r3 ?3 @, x8 q! C编译安装Nginx并添加ModSecurity模块
) l. q$ d5 F$ o$ f6 P* A$ tar xzvf nginx-1.9.2.tar.gz
( F! r+ F/ u0 y% y5 g' g, `- A$ cd nginx-1.9.2/ {( _# @! t+ N/ y" f2 V9 j
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/" Z. Z2 ]) f* b. Y( f5 o0 c; g; F* U
$ make && make install$ E7 Z7 _5 g8 T% x$ E+ I
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块+ [2 i% t) a& w

0 o& [) o/ q6 O1 U7 O% A7 g" @( |& F3 v8 `2 d' }
编译LibModSecurity(modsecurity-3.0)
/ [7 e5 m& @+ A: C# M& Z% W, F9 u$ cd /root. O7 s( t: L2 L, o7 l0 m5 ]
$ git clone https://github.com/SpiderLabs/ModSecurity5 e. A* G6 s1 j9 ^+ z
$ cd ModSecurity& Q* ]$ M. t6 x: b2 i& k
$ git checkout -b v3/master origin/v3/master
& n: x" h! R4 [- }: w$ sh build.sh
* ^6 ?' V/ f; d# T/ e$ git submodule init
* Y6 K$ s3 `; l3 X2 v5 Z& q$ git submodule update8 h# m* O5 `/ V' F
$ ./configure8 q  ?( |$ y. B% u, ]
$ make
5 L  f6 ^5 {: a$ make install
! M  m2 E: w. O. q: Y7 ^/ f) ULibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 N$ J, }- E( F$ |* @3 ^' I
8 t1 p. T+ p, \/ d4 {$ k- M5 C
2 N1 |7 y2 w; f. f# Y2 Q3 t* ?$ ls /usr/local/modsecurity/lib
! H! V7 o0 ^: ]* q  e% alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
( k1 n& h3 M& O% s4 Z编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ ~/ w. }1 i* s! R1 r* S5 T, B使用ModSecurity-Nginx模块来连接LibModSecurity
- n2 b5 R" y6 B  ?* s
4 [/ w# `& u" t9 b- i, V) v6 H4 T2 Y, t+ I2 s% ?4 C# F
$ cd /root
- P8 I" k: ^: c1 h$ H$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# v" R& Q. j! x, a/ P
$ tar xzvf nginx-1.9.2.tar.gz
; h; M+ h# Z" ]! ]$ cd nginx-1.9.2
' Q2 c8 A! M9 V9 N$ ./configure --add-module=/root/modsecurity-nginx7 Y1 y0 N7 F- ?7 r+ U6 v+ C4 u- s' K
$ make
1 T& J6 G* l4 N' y5 A- R0 r2 I1 s7 k$ make && make install
6 b% h. K! z2 ?; D0 T) I添加OWASP规则% D' t9 K1 v" ]2 k
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 d2 O2 O8 Z" A" S5 B1 {5 Q, M
6 T2 f* X& }/ Y- m3 e% I' b! I
- e6 S! a9 c( e  _2 a1 v下载OWASP规则并生成配置文件
& V/ C+ e+ e7 z+ F# S: g$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ _% y( `; c" Z$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/7 {5 i% E: {1 K1 z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 x+ t# f& I! J, I0 N
$ cp crs-setup.conf.example  crs-setup.conf' F& c4 Z9 G! U* o. C+ M6 Y" G* @/ _
配置OWASP规则
2 x- I! q& f5 X  P7 [4 a, t5 t( Y编辑crs-setup.conf文件
5 }2 o5 c/ a4 F1 p" Q& R1 \3 @
6 W) G$ ~, z9 W# M2 \) m! C! i* W# D9 G" ~
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
) J+ E- O8 L, `4 W$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf) i9 o# `4 N9 M2 a. [' z% R2 t/ |
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf$ J! y" O4 p' U: I! n
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. ^  G1 t) ]' y3 E默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 X2 u# m0 K! K& z1 c! H
& h2 x5 }+ n$ g7 ?
, J) ]# U# w' p" P, H! n5 T
启用ModSecurity模块和CRS规则/ B) Z; a( J$ B- S
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 f, M' a5 q; m
7 Y" h" t) V2 G% N
1 M  G$ q9 ^; m( Z: Qmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& ?  q  g& X; F$ R, i
# v+ ?0 I- a! v6 P: p+ c, y

/ d  w+ n0 x8 S. Z" t6 n& G1 I2 _$ cd /root/modsecurity-2.9.1/
7 N0 `$ j6 L* X, M$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ) H8 O5 v& p# h! ^6 p% A
$ cp unicode.mapping  /usr/local/nginx/conf/
6 N1 r/ M  R3 c4 b7 R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
2 F) V  d! e6 t, j
- \& Z9 q+ A# ]( ?; g
  L9 s" m. g  `# U7 L0 A4 {+ g$ vim /usr/local/nginx/conf/modsecurity.conf
3 O6 U! {: @! tSecRuleEngine On
+ ]6 E- R& b3 B0 `- D9 cModSecurity中几个常用配置说明:
  V. v6 n* {. r# A2 W- w4 f1 ?8 A7 o+ W" }0 Z! {9 Q7 N, w

( o; K& b+ r+ Y; `, v) }8 O1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。4 K0 \2 Y( X4 G0 G6 s

8 j9 f" S* G) s  n$ J
2 s) t; _2 \4 u6 Z# b5 `  w2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
7 |5 d" h, J* M! F2 M2 D. H7 K8 H) H! T5 `! D. J

6 ?' x5 N/ U, O- x, P) U3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。- ~5 B+ P2 d) W6 q$ \( A
/ ]* |% ^  Z2 A+ I% b2 M. Z! n
: i3 U& \/ I5 C8 t
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。8 N, z4 q! P9 @( V* E1 [

8 D$ Y* u+ ]/ r4 g
( O& L) o3 K, n8 g在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。2 X( o$ A7 \' w
+ H1 _* P/ l& b8 y0 Z0 p
0 Q  I' I' A& V" r
3.x版本CRS* {: A+ @, W4 r- ?" h, w
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 `4 z* _. q+ m  P1 H) B$ l* ?
# 生成例外排除请求的配置文件$ T2 E) ~6 [' p( D9 @+ Y
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf' v6 o. V' q+ j3 X1 j2 T
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf1 j5 V; R/ Y8 r  N
$ cp rules/*.data /usr/local/nginx/conf
5 N. U: Y$ L4 v" ]4 a) w; |% }5 {为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。- J  }( s% W& {
8 m0 s" X* Z" h9 H8 t4 [3 m. E0 m0 p' @) B

% g" C! ~. V" g+ a0 L$ vim /usr/local/nginx/conf/modsec_includes.conf( Q# D6 T6 j: @* W5 B, F* f1 j
5 J  p8 `5 g' L/ i% e2 @* ~
[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

4 u9 c; I/ w2 }4 ]- Q8 j9 N! [) e/ ^! }- e8 Z- e0 R( ]! I
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. T- u, M3 I1 D0 L- x0 {
* M+ Y5 g- H7 O: `' \! F, j
& j0 H# H, u. K配置Nginx支持Modsecurity. k6 ]0 E" S: S9 B6 G! ?% p0 e
启用Modsecurity
, E& ]3 Z1 |7 L0 F使用静态模块加载的配置方法
  `$ R' |1 S9 r6 G4 ]+ \: n在需要启用Modsecurity的主机的location下面加入下面两行即可:) w8 a" ?; S$ g# N
0 r4 ]; }4 I% O

3 U0 S+ y0 ?/ d. t. M; h2 K- j9 \ModSecurityEnabled on;6 d( J- ^. F: {7 m/ M& ?
ModSecurityConfig modsec_includes.conf;
  e! I# s8 _& ~: m$ n修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  T8 j% k) z# I) l9 J6 i) h
+ ]! l9 |, z9 @

, o2 {3 F1 j' h2 ^6 T$ vim /usr/local/nginx/conf/nginx.conf
1 ^2 \7 r( S+ d9 z4 ]! n" ^% _
( T9 N! t/ j. d/ k9 I9 x! {) x1 `! Y2 E, A; w) B
server {
& ^; k7 u5 w( S  listen       80;
1 v. y. y7 [4 A  server_name  example.com;) w* g$ O; P1 [% X

( J: ]+ @8 D( b# y% s$ t( X0 L' d6 L6 d% R7 w1 a1 D/ @  g
  location / {
4 r- o2 |+ J2 B6 ^+ B    ModSecurityEnabled on;
" \, }6 l; n1 m/ ~) j    ModSecurityConfig modsec_includes.conf;- _6 B$ N' `) J. ~' r" F0 u
    root   html;
& _6 e. W7 ~; s, r# a6 Z    index  index.html index.htm;7 r$ p- x& x" b/ Z
  }
0 p7 \% w( ~  Y) }}
5 i! w1 M. s* Q* c2 [7 G( Z使用动态模块加载的配置方法- [9 l, x5 @& J7 s+ C1 ]! ?! j
在需要启用Modsecurity的主机的location下面加入下面两行即可:
: p) e. i% Q' y: P4 p) u9 k
1 ^/ R* u$ K' _% l: T1 [% g& X9 x1 j
modsecurity on;
- ~/ U! p4 H9 ]& E) J+ {" r! Umodsecurity_rules_file modsec_includes.conf;
" Q# ^2 C5 W: B& I0 K3 O- E修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
) D) F( O8 l3 m6 Y5 O( e7 g8 O* f2 K$ u; q

  k3 a6 }) {$ W) a/ N5 N$ vim /usr/local/nginx/conf/nginx.conf
: A4 p7 P$ m4 Z5 T! V* R1 G
. @3 c# [2 K/ `" C7 l( |
: ^, a4 N2 f/ J: o* \( Z; _$ C7 ~server {
5 o8 Y% P. p9 R) }9 t  listen  80;
, z# i3 ^4 F! O6 ?2 K) M  server_name localhost mike.hi-linux.com;* C" i* i, ~( N" R8 X+ s% ]$ q
  access_log /var/log/nginx/yourdomain.log;( H3 P; x) @$ t4 i% w" l; y2 i
- D( W& g6 R9 h
8 G  t3 q# o5 q( O# c* h
  location / {% g1 F' A- ]9 w* E
: q- d0 m4 U( O" o* l

1 M# j$ N# n8 d. T8 t  modsecurity on;0 {! N# n7 a6 j
  modsecurity_rules_file modsec_includes.conf;: u# n) F4 A4 b; t, q& G7 r
  root   html;
, {5 O. `1 w: Q: o- ~$ s  index  index.html index.htm;
1 I. |$ G9 r9 I* Y. ^4 k) f}
: ~. V, O) {3 ?$ X. W0 O}; ^2 C" W# |/ I- J
验证Nginx配置文件' v( B$ n1 G1 f2 P
$ /usr/local/nginx/sbin/nginx -t7 z! Q# m7 F2 }1 r
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
/ ]0 _  @3 U' @7 N6 D% Vnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
( o. p/ L$ g8 _3 v& s  y启动Nginx
, u3 o7 c' U- N$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
( \$ c4 S% }# l. L! G" ^! h! O4 l2 m. R, 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能够战胜更多复杂/未知/混淆的攻击模式。

% w! _" G- U1 K  r

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-22 07:05 , Processed in 0.076489 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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