找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10534|回复: 0

利用ModSecurity在Nginx上构建WAF

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

: D# n, S, |4 J1 [7 x5 Q2 h9 r& W9 X! q/ q* m: }/ q
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。( I  l( I  r6 {( |8 z4 T

* Y5 \2 L' L1 G, O% T4 H: X
$ ], w3 k; Y, R. Q& a4 s8 t2 d- e什么是ModSecurity
% w& H( |* }- @( n7 tModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 _7 R8 p* D, t4 F' T& d# V" U5 g* A0 _& m1 x9 N
" `8 q$ j6 [( ~: C: c
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。1 p( O: q" [! o

% U! v* d) v. n8 F' c8 e5 {. d4 Y9 }) x/ z% F+ J% f! @% P
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。# B. Q, w- T/ p2 b2 N
( Q0 l  _9 J- B0 B
7 O' a6 L$ h* p7 ]0 J" e
官网: https://www.modsecurity.org/
" z% b4 Z: J+ t  h! x+ V
: ~) v, R1 t; j% ?' _8 A7 D( s3 s/ B$ S
什么是OWASP CRS0 G  C. D( ^  @4 L: V
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 S' L% ]+ n1 I
. `8 W% j, N1 `" H" y( W, R

5 m, m( F5 l8 E& Y/ NModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
# A3 Q, N' C& `, y# F9 H# \4 T" ~. A, L4 _" o$ O" T! }

; \" `+ {6 w7 R$ h" W0 }HTTP Protection(HTTP防御)% t8 o% N1 {0 I5 g& g' c& n4 V
HTTP协议和本地定义使用的detectsviolations策略。
: W8 J1 E9 E, p  m& I  U: o+ T& D3 r

. W8 ~( R: J) t4 V2 ]9 h+ S" u+ IReal-time Blacklist Lookups(实时黑名单查询)( Q, Z5 D+ I6 l* J; \" l) Y% j
利用第三方IP名单。- ^5 I/ O. {2 K) y% b
( I  @& _0 a, w. D# y

# @* n* B/ z% h6 A2 r& h- SHTTP Denial of Service Protections(HTTP的拒绝服务保护)/ k4 G/ G9 s# s1 @+ ]
防御HTTP的洪水攻击和HTTP Dos攻击。, w9 ]; }5 s0 Y/ o2 {9 B8 U
' j$ i4 N7 d$ ?& n9 l% f

+ t( D' [/ r; a4 r' y" s6 h& qCommon Web Attacks Protection(常见的Web攻击防护)2 n3 Q4 {  y  i* t2 U& `: w. S
检测常见的Web应用程序的安全攻击。
$ P4 S2 T6 {8 Q8 ^1 R% j0 q
) z2 v$ V" \  y' `8 v
- _. y: S8 l" E$ HAutomation Detection(自动化检测)# ~) s/ j. n% _' W, x
检测机器人,爬虫,扫描仪和其他表面恶意活动。% i6 B- _8 v  o6 {( i: N
! l; }5 E3 a+ n) o+ c
7 c& ^+ T. n# A& ?. i$ ?* }  d6 ?. s; i& l3 K
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)# E( _1 ?7 j# }- u* ^
检测通过Web应用程序上传的恶意文件。3 Z( [. n: d$ c3 V8 v

8 J" B# ]) `! b# y( L) r
2 {) T; |" V8 HTracking Sensitive Data(跟踪敏感数据)
4 q" M) S% v+ Z6 o0 }2 \信用卡通道的使用,并阻止泄漏。
5 z5 K3 `5 Z3 U! c; Q- d
0 n) E: K; t+ j$ P. v6 I% h: v# [4 z7 H& O. h0 \* e# F
Trojan Protection(木马防护)% I+ Y5 b( y: O
检测访问木马。
; [! I$ q1 P% a2 g1 O1 {/ B, C  P0 M& S: i& S& u& C9 o% w

! G1 y! e/ R& S& l7 h. I: L9 `  wIdentification of Application Defects(应用程序缺陷的鉴定)
9 I) d$ d( A% h8 q+ k检测应用程序的错误配置警报。
  |/ A+ ?. L" K5 I6 Q4 u! s! _5 a! X. ^4 Q% Q
: _5 F) S9 M7 W# L' \: A. B/ B
Error Detection and Hiding(错误检测和隐藏)
( N9 z( N" a, r检测伪装服务器发送错误消息。
9 }" b) W# k2 o
1 R6 j6 H0 s" T, n2 f$ M* _6 |* S
安装ModSecurity& d$ t( |6 v6 l0 A% E! t* z9 x
软件基础环境准备& \7 P* B, E: M* r* ^: [* o
下载对应软件包
! B8 g) n" e& r: y( w& z$ cd /root
' N# R5 f) {, n; Z4 H6 l0 H$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
% \' e$ X4 k5 v7 w6 _% 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
, k% z& Q4 v. \/ R安装Nginx和ModSecurity依赖包
  p+ P& ]+ v! U$ x% tCentos/RHEL
  S* j) ^9 b4 e" L
" i5 ~% R0 [1 j' Y
, q& i# G& Z& K+ j2 s$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel6 Y: ]8 }' {% d' i, Z8 E
Ubuntu/Debian
% L8 S, f7 J3 Z  K% \( j& \/ v$ A" F- T0 D2 {
( p" P' x: W" u& 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/ ^2 a3 n. T# v3 ~  {
编译安装ModSecurity
% R& {, J0 B* z8 q3 uNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。; Q, m# ~$ G2 k/ J$ M
) M8 T, _% d' i- E% q) ~
- s: P9 _6 l- B- ]- V2 k$ M1 b$ ?
方法一:编译为Nginx静态模块
2 L+ N  ?) z# \% o/ x9 F+ o2 }# K$ P4 [4 U
1 E! v  J4 V# _' a* ?* K
编译为独立模块(modsecurity-2.9.1)
3 ~9 k/ L! W- d2 V/ h: o$ tar xzvf modsecurity-2.9.1.tar.gz! I& [1 ]6 {: s6 m) v6 [! P
$ cd modsecurity-2.9.1/
' y3 J  L: [. I& V! z* i3 z$ ./autogen.sh
7 l1 O$ d5 q0 P( Q, o# t# _$ ./configure --enable-standalone-module --disable-mlogc
! s+ b; w( e/ U5 P. g$ make  x! _/ b) D) ~: \
编译安装Nginx并添加ModSecurity模块
' ]# R6 q5 j0 q8 ~4 V! g$ tar xzvf nginx-1.9.2.tar.gz$ l, Y% v5 g( e: I
$ cd nginx-1.9.28 c% u3 Z7 w9 ^0 M
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/6 j0 b; E0 K8 d4 i9 g8 s
$ make && make install  Q7 s3 Q8 A5 K8 t
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块( l1 Q- M- L% x5 q/ R3 ^
( B# o+ w2 w+ z2 _$ H5 m
& z2 R5 l" @. g8 n# h& o
编译LibModSecurity(modsecurity-3.0)
" K! H) p2 [, V- S# ~, _$ ?$ cd /root
. b+ |5 h6 S" V$ git clone https://github.com/SpiderLabs/ModSecurity+ ?4 R: a, ?4 V2 g3 ^" n* B
$ cd ModSecurity
6 q/ `8 r2 K; G" G( }: V' W8 X# V$ git checkout -b v3/master origin/v3/master8 m5 D4 f: k% z' K
$ sh build.sh
9 A# |& `! N) ^- ~4 F3 x$ git submodule init# H6 Z8 t9 L1 }/ f
$ git submodule update" J; I1 d) H4 D/ S
$ ./configure( Z+ ]. @, }% p5 s5 y
$ make
6 O! z2 j+ l  H, M0 G$ make install7 m/ z" k  s. s+ ~2 N
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( J8 z+ @2 y2 h, K, y4 x
" X9 u" C! R3 e* K& j0 G0 q9 Y) C) i( D6 k- R
$ ls /usr/local/modsecurity/lib
7 U+ a( r+ h2 g  N$ ulibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
+ M) V  u# A/ A; v+ d: V2 f1 M编译安装Nginx并添加ModSecurity-Nginx Connector模块3 z5 M/ V  a: j5 f( e4 K; j
使用ModSecurity-Nginx模块来连接LibModSecurity
) m+ d) c9 I# F9 R
1 p" F$ J5 m" W. D$ S$ {4 [, t4 K
$ cd /root, R6 U" F" O9 Z7 l: T. i* _
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
  D/ C, K/ K+ C' `# }/ W- W$ tar xzvf nginx-1.9.2.tar.gz
# F" n# c, S3 ~+ L$ cd nginx-1.9.2
% a- C2 }2 F9 y3 [, i$ ./configure --add-module=/root/modsecurity-nginx; V! U& b9 @& ]; N% O
$ make1 ]) d1 }- o8 x& _
$ make && make install+ o$ J# ]  n; k' C
添加OWASP规则1 J$ k/ L, u6 A/ q- \: H; g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
0 m$ D; A  A! Z6 r6 H
* \5 s4 q4 c0 q3 U) u' p) E; D7 V3 ]& M4 i9 W9 y" j3 t* K! c" d. ^+ o$ R
下载OWASP规则并生成配置文件
& H1 V* U7 x# Z0 f' k$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) `. O0 L9 r, A; t5 t$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
" d' {% Q& z& t4 I$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) r0 e1 M: @" _6 u+ l5 C; Z$ cp crs-setup.conf.example  crs-setup.conf) w' E. F/ C4 j; G' w7 H2 l
配置OWASP规则7 F. Z4 {$ w$ @$ [& l
编辑crs-setup.conf文件
5 y* u0 M0 f$ \9 [6 _1 A5 w. _" f: ?/ p( m" \7 r  [/ V

# e3 ^6 }# P- p5 e4 L3 K( j3 K$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
6 T. s, I+ ?7 \: A# M! u2 [$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
  P" Q# m& f- |$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf" b9 T/ O! s/ V: I. X9 m, [8 b
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 ], Q- \6 R7 x) |. K$ @4 z7 ]默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。, d/ E3 |( k. J3 }
( G/ f" s5 |9 d2 l4 z3 H- x% r8 }' P
5 T: c& n; H( E0 n, }
启用ModSecurity模块和CRS规则
' G  d9 `$ v$ G; \4 n# C) \复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ A( D* ~2 X+ n( _* d$ v- M* @2 U4 C
4 B! C. k( ~8 g# T" h* R* v' P6 o5 H. u
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。4 F9 K% t7 A9 |
, J. J1 q% c. Q$ ]/ v0 r
" l4 u6 A- B" }! }3 \8 g
$ cd /root/modsecurity-2.9.1/# U- @! N( s$ b+ E# R7 U+ ^
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% B& j8 L' h- e9 y9 P) X% V- D1 l$ cp unicode.mapping  /usr/local/nginx/conf/
5 d2 @+ E5 S9 f1 J  `' l% x2 `) Y将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
  c2 |8 y2 v* j; @) u3 k! G* @
8 y. o# \0 i4 Y" h& N- t! \6 k6 p2 W& D, K& `1 z' J
$ vim /usr/local/nginx/conf/modsecurity.conf
3 C0 ]# ^( B1 J; C2 VSecRuleEngine On2 S; v* s/ j8 Z/ V8 @% ?4 t
ModSecurity中几个常用配置说明:$ u7 Z1 r! {5 B# a
% h+ A) h8 z, H

1 k4 V8 p0 N  C  c6 ~$ q" a" s; A1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* L+ L; B$ b; J; p

; u  d, C& l% T' p, M: l
- m& N! i9 }+ j2 a2 t. d& K2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。& n$ u0 ^7 ^0 {
$ Q8 ]/ u% M3 y/ S8 a8 b

  u  ^+ x3 |* E. Q; H( n3 N3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 [. x2 I! l$ h! f7 E5 z

' X+ S) P. V8 i, l' U. h1 r9 V2 i! l9 ]8 F( \6 i
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。9 P9 V4 a; K) w/ l; [5 Z
% C1 Z) [0 z$ X3 k" b
1 l3 U9 u, S& T; t; E; N4 D
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。' l- _! l! b- M4 g
4 K. s/ N" l2 L3 }& ]
/ [5 f  N4 \" U. \$ Q. _8 a
3.x版本CRS  S" [% J5 I7 g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  ?8 g! w9 t9 C  F4 Q# 生成例外排除请求的配置文件
# H9 ^# ?# g0 m' g$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf/ N" N. q" Z6 G
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
! c; K' H" G. Q9 f8 U$ cp rules/*.data /usr/local/nginx/conf3 y8 ?  M# ]4 C3 R" {  c
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。2 a: i3 k8 G9 V; |8 {: }" C1 W+ O9 I

# ]2 f' N$ Q# [6 y) e# h; Z
1 U: s4 S4 h& T& k! b0 n$ vim /usr/local/nginx/conf/modsec_includes.conf& w6 q* @) w6 q
! `: ^) I9 q# N* w- U. O+ D  V
[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
9 g9 y) Q& p/ Z( {) ?

; q+ c0 R; F# w注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。: J9 ^* Z: u8 l7 S: C

/ E3 q' e5 H( c7 ~( K; x3 ?
: L- a, ?: J' K( J- J4 J配置Nginx支持Modsecurity5 T2 ^" N# H! ^% K  S- N5 s. X
启用Modsecurity
% M1 R8 h' f, R/ Z' E! {5 K" f使用静态模块加载的配置方法
& i% X% Z& P2 ~# i. I  C在需要启用Modsecurity的主机的location下面加入下面两行即可:' o' {6 B! z) U: @' Y

: K, B5 O+ F2 I$ v7 w4 X9 j- R. S( {+ R) P" Y1 r1 E  D
ModSecurityEnabled on;. p& t1 T) G8 W8 K' Z& n/ n
ModSecurityConfig modsec_includes.conf;
- V* S" N% q9 Q. p修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' A* S1 K% L3 {, v" R
8 G, H" H5 Z, L7 w0 ]: A' L9 l

! v* N! y6 o2 J# y: S! q( V$ vim /usr/local/nginx/conf/nginx.conf
4 s2 w( a" }" _* K, F1 H& l  [0 m4 \# t6 [% B. a+ d" t

, m1 |  B: L9 a) y" U2 d! p. aserver {3 ]( W* V. N( F: D$ Y1 f
  listen       80;8 A( ?# P7 Y( \  T+ m$ N
  server_name  example.com;2 u; z0 H  M; y  [  H1 d6 `9 Z

& a- E( }/ F* e! Z; O& s" y
- M+ s. F: j% ^: x9 x  location / {, Q8 f# D( E5 q) P
    ModSecurityEnabled on;
" x' E+ Q2 D: q, I    ModSecurityConfig modsec_includes.conf;( z( }" p4 _5 z1 n* p5 j
    root   html;* J4 `) F% z( k- t
    index  index.html index.htm;
' [2 Z. I* ?: Z  }
$ e' _* n/ _( y) a3 j9 v7 t}, L7 {$ X- k3 [) a
使用动态模块加载的配置方法
% t7 z1 @/ x7 ]+ [* e: n在需要启用Modsecurity的主机的location下面加入下面两行即可:2 B# Z) @6 g# N$ U
% Q7 L( F! \3 E6 ?+ o4 u
; B" q; O- M3 ~5 d$ ^$ O$ P
modsecurity on;; s" z/ N2 s: U) X- ^7 b
modsecurity_rules_file modsec_includes.conf;
, j$ U) k; a0 t% J" L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' @9 W. m$ u) `" v
# v8 X$ E& R; d3 Q! J6 r
5 T; U1 S5 [9 I, G9 w; C" l
$ vim /usr/local/nginx/conf/nginx.conf
1 w% g+ }6 y# a2 x5 ~: U3 F8 P& n* R( Y8 [  \( n; }# u

, ^0 Q. {8 e( ~' e* f  Y8 xserver {( Z) g& Z; x; B0 T+ ~8 G9 e  p+ s
  listen  80;6 \7 B5 R4 V1 p% }( j" f: k5 b
  server_name localhost mike.hi-linux.com;
7 r! d8 P, x( z) W  access_log /var/log/nginx/yourdomain.log;
! ~) R" d! |( s" ?: K
% v# [; z+ I0 L& @
' p* v+ n" @0 h4 s4 l! i  location / {
" B- `) k6 B7 x& S
3 Z# u9 {1 I% Q& Y9 B8 f- p$ T+ \* {- ~  \. e+ D
  modsecurity on;% p4 |; t4 [: t# y  S
  modsecurity_rules_file modsec_includes.conf;/ S' i3 \9 x: o  f3 H
  root   html;
( e% {3 Q/ s: {& R( k  index  index.html index.htm;
: P3 f& J, R0 ]. q4 G}) [1 F5 F3 X( t# W" R7 K. B
}
1 H  c+ f1 g# w" M( g9 y3 Z6 d  q验证Nginx配置文件
) K2 a1 t% W. w$ /usr/local/nginx/sbin/nginx -t8 o$ S* O- ~9 }1 ], E" i
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
; r2 r+ M. ~8 o4 f, J" o( e) p; Enginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
: m5 Y6 y0 ~+ ?启动Nginx9 f$ v% b5 h3 i( i2 E5 ^) E8 L
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 y7 r' }. I, y4 j5 u, ~, u4 v6 C  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能够战胜更多复杂/未知/混淆的攻击模式。


0 C4 f, [6 S- Q: _

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-18 05:45 , Processed in 0.075624 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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