找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12973|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
) _7 r# l2 q, L1 P' N3 r% p" {$ A8 L2 a% E0 n2 B) _

; W' f+ k0 L6 R在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。/ q" O8 L. f6 G# P9 U7 n

' ]( C3 n) }/ L( b- r# u& C0 o+ _. ~% s
什么是ModSecurity
6 W5 v1 B  f( c$ C7 E+ r; X+ KModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 P9 T. ]2 q1 N; i1 c) i0 V! S9 B3 `, I& K8 C5 k" I

9 ~' c3 S1 E. e" F4 K; r3 IModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。3 R: d& \$ t; J: e" D' S

; D* o" n! d; g" g4 p
* P9 Y4 J5 S4 ]  \  V. Q  bModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。& a8 V; T& S9 d) ]4 Y- a( Y
& r  x5 l5 a7 K; R) u; K1 l( z6 R# D

; \0 w) t# }$ ?" U/ t0 G' S3 c官网: https://www.modsecurity.org/
; u( e1 c5 m6 j
; Z+ O5 S: v/ z% }, {2 f. K0 ]( J7 B2 j8 V
什么是OWASP CRS
3 g, _4 l8 D/ M: [1 D+ ^OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
5 i+ O) s5 N9 ~. L) Z0 g% h% N( p# L# \% B$ h" E

' l8 |# t# S& K. m. e5 o7 RModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。( u9 z% i) j0 A
: `, u* U2 q/ ?
. f) u2 S, u- \
HTTP Protection(HTTP防御)% F0 y- T. }6 q, q
HTTP协议和本地定义使用的detectsviolations策略。' e5 ^3 [3 p  s+ n
# ?& ~- K+ f; w% H) n. v/ C) ~& R
. @( w, k7 b2 k! B( [& y
Real-time Blacklist Lookups(实时黑名单查询)" D% F. Z+ J2 s3 p& H& W1 |9 r
利用第三方IP名单。: l) `5 D. `: b) f# {7 b9 t7 y; G. K

! A4 Y+ j. i! E, X' u, ~* t
3 }5 U4 v5 Y/ H4 rHTTP Denial of Service Protections(HTTP的拒绝服务保护)* z  z& |/ l* l# `: P' T4 w( b
防御HTTP的洪水攻击和HTTP Dos攻击。
" m  T6 D, W: y- w# X/ e
* w3 n0 A  U1 L9 e' c5 }) K( N' a  T/ E* Q" ?8 O7 |9 _! R
Common Web Attacks Protection(常见的Web攻击防护)* s$ z/ I0 H7 K! S
检测常见的Web应用程序的安全攻击。& e, M3 i9 w: c

5 r; `" y5 @9 d: _% r! s) r0 z( e
Automation Detection(自动化检测)! t4 L  k: N4 T; g2 X! ~4 r
检测机器人,爬虫,扫描仪和其他表面恶意活动。
8 R' m) Z1 L: S" ^7 V0 o
+ D( N: ^5 J; D! i4 ~; v# x; _: |+ i% p0 S" G
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
% A: l- q) H  v; K0 j" ~* U检测通过Web应用程序上传的恶意文件。1 G1 n4 Q. U1 V+ E
* p1 f/ v) m7 v3 M# o* V4 |( @

5 s- k+ B% z$ v% cTracking Sensitive Data(跟踪敏感数据)! \$ r5 o1 `3 Q( Z+ K# b( h
信用卡通道的使用,并阻止泄漏。
. f* M- f7 a! F9 r8 t6 i4 i
4 I" Y% y- T  b/ `4 Q0 q* }
# R1 b0 z! r  ]8 Z* P5 S5 I/ wTrojan Protection(木马防护)
7 M6 `' [0 L; U检测访问木马。8 s6 Y; m) K7 G; g  C# g

* ?  r+ G4 O& z% W, k/ @
/ R' A; |/ s7 I2 |3 |Identification of Application Defects(应用程序缺陷的鉴定)5 b: N5 x+ w% K2 S0 Y' y
检测应用程序的错误配置警报。7 o+ B- _- [2 r' k

5 k, v/ z! d  j9 j
. z" `/ M7 q5 K% a: }Error Detection and Hiding(错误检测和隐藏)
+ L; S3 p6 H2 G% `3 \检测伪装服务器发送错误消息。
$ j/ i% d  P- M! q* a! ]
- w9 q! Q0 W  {) u; P; M& m* y4 V7 v3 I) s' l0 l; N
安装ModSecurity
2 q; `. c- _) `4 m% {软件基础环境准备
5 o: B4 @, ]4 ~8 B7 S# g9 m2 w下载对应软件包% q) [) Q# V' J9 w- k" k3 h. L
$ cd /root, W& D! m% p$ h3 G3 q8 t  l
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'3 X! b+ ^" m' I0 j7 s8 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
! _' Z# V/ _; i% z6 A$ R安装Nginx和ModSecurity依赖包
  w% P5 h+ O" a; w: z0 `% ?' c# bCentos/RHEL; T; d2 G: }& Y; B8 v3 s

8 i. J- H( s) u. I* ~- G& g
7 Y9 B: }& L& r. Y+ _4 N$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
# Q0 i" l9 b; I0 ?Ubuntu/Debian
; M8 @2 W0 n' g& `' E3 g0 L  t& s7 I) Q
- t- c5 d1 w+ @
$ 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, S1 \( a0 Q; K$ u: n
编译安装ModSecurity, i! S( S, y0 @8 ^4 r; ?
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。+ F; R' d4 `( \' J; ?

6 o- H, V' o0 R- ?/ }8 R
4 n8 \8 P0 Y0 Z$ ^$ r方法一:编译为Nginx静态模块
5 R' I: f) e- i# [/ C$ n3 ~
8 X8 r& D( T6 W
: }( U6 o! Q0 T0 w( ]编译为独立模块(modsecurity-2.9.1)
/ m0 ^4 I' X1 j' R8 \$ tar xzvf modsecurity-2.9.1.tar.gz/ Y+ C% I9 p- c4 t
$ cd modsecurity-2.9.1/
, C) ]8 \/ r5 S# g1 ]( y' J$ ./autogen.sh( K6 Z3 o+ j- ~* n/ {. {2 a! T2 O
$ ./configure --enable-standalone-module --disable-mlogc' f" n, p5 i# s' t: O$ v
$ make
8 x) M! a1 Q8 |' Y1 {' v% P编译安装Nginx并添加ModSecurity模块! v4 ?+ N0 @! }# P5 J
$ tar xzvf nginx-1.9.2.tar.gz
7 c+ u# p' Y* z  T$ cd nginx-1.9.2
# E# ~4 F, N* ~9 `) i$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 w1 b& J; N% H  q4 j7 q+ G$ make && make install% [1 y* L2 z6 J  S# o
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
0 }- V' Q) B, e; ?# S
. k' c- Y' \- F8 }) k6 o% u4 R' @0 m* r# Z& c
编译LibModSecurity(modsecurity-3.0)4 W& j: v# r" X8 j; z# k0 F& `
$ cd /root2 K! e2 w) v% Y8 Z
$ git clone https://github.com/SpiderLabs/ModSecurity7 p2 E0 j/ y( e7 h
$ cd ModSecurity
  M2 z* s; [# R" J+ D2 r& h$ git checkout -b v3/master origin/v3/master% C. r  ]" p; d/ o4 y  r$ k
$ sh build.sh/ G+ `* d- u+ p: }$ H
$ git submodule init
- ^. t% Z0 I, ?. R5 Q7 x1 ^; T$ git submodule update" [' A; l# T! h) B* Q) S" c1 W
$ ./configure
/ W# W% o7 q2 z% U" G8 \/ {$ make. `$ h1 |6 t; J5 [1 m0 P1 w6 i
$ make install
$ B/ K& {: m8 K# Q" d6 Q3 u* P! sLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
4 G: U1 |7 ^8 v, x, }
$ E+ l/ P8 W! [7 Z+ S- z
/ H% S8 v) m* k" [2 t: Y" r$ ls /usr/local/modsecurity/lib
% \$ i$ t  k0 t, j( v! ^, {libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
# I  ~7 s; t% G8 e5 J# ]编译安装Nginx并添加ModSecurity-Nginx Connector模块
- x- j7 P5 D2 G, d, }使用ModSecurity-Nginx模块来连接LibModSecurity' u/ t/ B" T. ?; S% l: Y7 d! }
( g) M8 c4 i  e& w9 V! g3 J% l
; N9 P2 F( }$ k0 d" p
$ cd /root0 T3 Z. }! Z6 E1 U
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx0 V8 r* a! z4 B) W5 }7 H7 T& G6 a
$ tar xzvf nginx-1.9.2.tar.gz! U. D" J7 T" D8 \; J0 M1 e2 C
$ cd nginx-1.9.2
$ T. u! e* X, N, r! l& d% `& d$ ./configure --add-module=/root/modsecurity-nginx
/ i7 u/ S+ p& K$ g2 A$ make
# ]7 C$ `& I$ s/ k2 m6 I4 |$ make && make install
/ h* K' T  q% r8 g# C0 E添加OWASP规则$ \8 ]! }3 v8 Z! @. y. n) z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
  f. u8 Q5 M( J" m9 o, B/ P8 `1 }! e0 b: C% l

9 g/ ?4 d) h4 Q2 B下载OWASP规则并生成配置文件
- s1 U" H* [" L6 M( j) D6 _( x$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 ?' M' j+ ^5 K  i4 w7 A. X8 w$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
" f; _8 g5 \0 |: e$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 G7 x( `8 H; k; Q3 B6 H- X, l
$ cp crs-setup.conf.example  crs-setup.conf- g( J7 u% N% E9 |$ o
配置OWASP规则# Z8 z( C7 X4 C: e& v
编辑crs-setup.conf文件' h, o7 L7 N& W5 U' R! |1 \- V6 }

( Y! C  X* L) a; [6 L  R+ X4 \! S$ n' ^
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf- W) ?9 I7 C" T# H$ ^0 S
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* c" f5 y6 p) q8 ]0 [- o( M4 T% h( n
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
6 F9 L3 q9 M2 a' W6 v$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
: i* o; [) Q7 r, S$ }7 X默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。8 T; z' F* u' K- a5 ^! F

0 |# g5 P. f1 p# H
) Z6 k2 h9 n3 s. \) l7 @9 S8 |; D启用ModSecurity模块和CRS规则1 P* f4 K5 I& u: F0 O! \1 k
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 m4 I# \: \$ _8 O  f# T( v" ~' i* l! ^& d- f( L; d

' R8 K! H6 f  f( [4 C; o. umodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。( _4 M$ ]/ r1 t5 e  X, f: g
$ C' h2 f  e/ q8 ~$ C9 K: j2 j
; m, G$ G4 D: O1 o9 G1 W4 R
$ cd /root/modsecurity-2.9.1/  F. R5 [2 z# c$ Y2 {4 g; ?
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
1 b* l# I, v) I0 k7 N$ O$ cp unicode.mapping  /usr/local/nginx/conf/( ?1 F" o8 d* h) _1 o1 l1 \2 D2 t
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。. O0 Z9 p# b& x$ G% M
7 t- U) U9 G7 @) T5 h) Z
6 g4 M- a2 y5 t  ^# v+ I) L$ {
$ vim /usr/local/nginx/conf/modsecurity.conf
) L0 a3 d2 s4 Z, }; SSecRuleEngine On
+ N* a- T/ Q; E; q3 Z4 M& N# \7 oModSecurity中几个常用配置说明:
0 c! [& F- f+ v! @, o/ l* b& {+ G: o, B

3 w5 I- P! F* \# l6 k1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。- _* a9 w, x" Z( O! s$ D

; q* v. g. w; j$ P8 P2 q% U3 j! r& K
! X. h  _# t) i" [$ M; M# a  I1 V$ r2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。- D! c/ U$ V+ e0 k2 K

9 f: @8 u  S( a* [  D8 A# W$ G2 f3 s$ V' Q1 m  J1 A$ _
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。, k2 u3 G1 ]! [5 v/ g4 G& W# \& U6 {
9 r2 d% \, l) f

; d9 ^7 V  O9 U+ Y5 ]$ p/ d4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
0 d1 q+ e4 F/ C' _& G! N) _! W! L
3 s* D2 n+ L4 u% E% G/ Z8 r9 [4 n$ W3 l& q& E$ H/ N
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 ^& C$ G  N0 v8 a' |  M
3 l! H  x  o+ I' f' b% v! `) N+ l- k) y+ @. I& d7 E2 K# E" K
3.x版本CRS
! z3 F6 n3 Z! o4 K' T1 i$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 r' F* x" K" K0 u% z- R# 生成例外排除请求的配置文件6 r: y9 Q5 l/ H1 W* q
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf( v& f( j" K, {0 s9 n5 V' N9 P% G8 F
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
: f0 x+ O5 R& Z6 I0 D2 \$ cp rules/*.data /usr/local/nginx/conf' m' S' D; z5 T- z* S/ k/ I" V
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( }! C4 h. Z* b; @$ V3 x, @

4 l4 s! X; q- S+ I7 Y7 v" x8 b2 O& T6 h( U% g9 v" Y
$ vim /usr/local/nginx/conf/modsec_includes.conf" [' A8 h. |% N" n6 P- r. b( ~) g
+ p+ x+ Y  o1 f& ^. \* 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

4 ~4 z( v  v2 E8 H& ]0 |4 y6 m: x; p0 j; T* P5 O$ b7 J1 s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
" R2 W2 d- Y' p1 ]0 V( Y9 l. m. b
! P; P& i# I7 g3 u
- }/ `- s1 v, R配置Nginx支持Modsecurity
  D( u/ F. H, m; f) X3 R启用Modsecurity
( E/ P$ P, K5 C  j+ _使用静态模块加载的配置方法0 ?1 ~; f. E% p, g6 c
在需要启用Modsecurity的主机的location下面加入下面两行即可:
) W& q3 `4 D6 M
; i+ E3 B( N0 K) t9 }6 r8 Z! O5 K$ e; B' Z) N8 N
ModSecurityEnabled on;; @7 W/ f  p7 Z" O3 M9 p5 z
ModSecurityConfig modsec_includes.conf;
% V4 j- F( k/ }. ?1 R4 B修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ Z5 U: ~( {: O7 k

/ m! _" X$ g! v& ?" z  Z4 J2 q; x/ @6 h/ J
$ vim /usr/local/nginx/conf/nginx.conf. q, d) x3 v5 b* W/ v
( W. n9 X, s; P" C7 z" H8 U# @0 g" a
9 i, n( t1 |, f. a! ?' x
server {
: D  H7 I+ g$ v  listen       80;8 w" n' A, o5 g" V; M8 R7 ^7 X- C: J
  server_name  example.com;. N9 N3 a( [6 M

# |4 r+ p) C1 A" D3 S, C3 @0 E
6 n) K& ^! H& K: @; H0 W  location / {; y# ]( d5 \7 A. T
    ModSecurityEnabled on;
/ M) i" O# r. d7 R9 M1 O    ModSecurityConfig modsec_includes.conf;1 x8 d: c% W9 f
    root   html;7 T/ n6 Q, O, ~5 k1 m) x
    index  index.html index.htm;
" ]3 @: |( @4 g/ z: s) t  }( i" b( E+ W( E5 p
}
2 A/ c* Q6 b, \+ o' ?$ O' G使用动态模块加载的配置方法9 |! S9 E# F. x8 F9 p8 i# R+ |
在需要启用Modsecurity的主机的location下面加入下面两行即可:& }3 ^& K  u& D8 w2 z1 q$ I1 h

; W- j& K$ L: L$ h- B9 f3 d7 s" k* _; X0 g  O. `9 }# N+ x: ^
modsecurity on;8 W4 t! }) J' t; n5 l
modsecurity_rules_file modsec_includes.conf;4 ~; h6 w: C" {. f  f
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. Z$ c- C9 O  E; R4 I5 ~* F7 a6 u" ?. G+ q
/ |' g- V# c. m! w- j. e  C/ f
$ vim /usr/local/nginx/conf/nginx.conf
+ o: E2 a, j+ U, ]5 i
4 e$ E$ y3 `0 ^. J, x" `! O% c0 }& I: U. Q" Z
server {
/ U; _0 C8 o: r' t1 R6 `  listen  80;
2 ]# [( I: l" F5 i! I# Z5 \  server_name localhost mike.hi-linux.com;8 p$ k* z9 x$ c, Y6 v1 z' A
  access_log /var/log/nginx/yourdomain.log;9 T5 L8 v7 v* U  R) a5 T2 E" @2 Z

2 `& X, g$ J9 V! F6 r* e  p
! q; x: {' H7 `8 b, [6 [8 H  location / {/ T( x) \4 n; ?) z, Q, ^3 i
# o2 O3 w: ]% t! Q+ t. ]

, d7 i/ z# A1 l5 C, y0 t  modsecurity on;0 t- P1 N# G, d' @! F3 }
  modsecurity_rules_file modsec_includes.conf;
0 k8 T  S4 s% M" N2 f& T  root   html;9 F/ A' ^" Q$ [8 I, Q$ T  @1 ^$ W
  index  index.html index.htm;+ U3 t; N  C: ^; A8 z+ Z. u& I4 h0 ?. Q
}
4 s& m. x2 }5 A. f# {" l% k* n' i1 w}
5 f3 }  V( ?' p; D6 P2 \验证Nginx配置文件  B( `1 G( q. K9 u8 {
$ /usr/local/nginx/sbin/nginx -t( J" c# t, x4 q1 ^$ G/ F
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok: [1 `' ~' b+ B7 s) V3 M; L5 ]
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
( G6 _2 C5 e% K5 j. ~, m启动Nginx
- K  \- P. E; U4 I6 K# L' l$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
- d" b. U" |6 T1 T9 f& G6 {& {4 ?, K$ L! k: u: o0 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能够战胜更多复杂/未知/混淆的攻击模式。


" E( n, b! k- c) A. k; Y

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-12-5 14:32 , Processed in 0.080070 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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