找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9458|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
. }+ ^4 O, Z  J$ u% e3 _( |$ r1 n' k$ s/ O% b
3 ~7 |$ d) R( H/ n* c$ M
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。( C8 W, S1 T; L, @' x

4 o5 ~% _! Z0 Q) z+ I$ P, V' ?: A' L
什么是ModSecurity
3 Z: j1 ?1 Y  `- `- m8 y! E3 WModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。& n. U# L: w% A( x9 Q1 [0 d

. A# k! [2 y, R3 W: b7 |! U& t( b+ S2 K6 d1 }/ k: R" ]
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) o8 R# R, y8 i& `
4 T) m8 s2 d" D$ D/ h- y# H

  M7 i6 M( S- o( g7 y% x! k% d9 LModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 |+ f( s  s8 x% D# z! O) p, O2 C; t5 l

+ u) A" y; Q4 \( J/ a3 {7 G, }官网: https://www.modsecurity.org/
% [( p5 j" m& t* \% A% I, A5 N2 R2 y7 |' G; r; q' `

! b5 z+ e4 y9 w什么是OWASP CRS
4 E9 |& G* ?/ K0 t5 u; \OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。3 V( Q1 t& n( Q' j$ Q4 }" r

+ q2 o* z8 @4 Z$ s6 R9 d) ~. v  h; ]) B1 ~1 W6 K4 w
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。2 G. V  G7 H- w8 V

' g6 o! p( ]' p. u0 u/ E" Q, o2 C6 V2 U  x# R. O: _7 N
HTTP Protection(HTTP防御)& W: ]3 F; r0 G* e2 |% J3 @/ d
HTTP协议和本地定义使用的detectsviolations策略。
: p4 _" G7 W6 `+ O% a& @8 o1 b; W+ T! u' h6 Y

& v8 w. @% p4 t4 K. OReal-time Blacklist Lookups(实时黑名单查询)* i3 M$ E% V& S2 n6 a% b5 {" g: W
利用第三方IP名单。5 [! B8 T' O# f! S8 P) S( M
4 U* R* b# b( O" ^& C
3 Y5 D3 e/ g1 Z% f" l8 _9 }
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
9 m% e2 \$ T5 c% q" ~防御HTTP的洪水攻击和HTTP Dos攻击。
1 e0 _# \3 y$ q9 h( Q
, S& y& `3 N! v9 Z( L8 ~5 n' E+ U+ K) J: S- ]
Common Web Attacks Protection(常见的Web攻击防护)
4 {7 T) G1 ^  n) b5 Z) K- V9 X- Z检测常见的Web应用程序的安全攻击。, c0 y0 |6 k) \! T/ E

6 {: M# R. N$ X
. z: l( n4 M, @2 z: ~" _) |. VAutomation Detection(自动化检测)" |/ e% {4 ?$ d/ x2 ?
检测机器人,爬虫,扫描仪和其他表面恶意活动。0 c( {+ h0 W. Q+ n" y
) e" G/ h' @; Y( a3 k1 D

, m# z$ y% N- D% f3 h4 C' CIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)2 C- ^0 {1 ^1 `! |$ s
检测通过Web应用程序上传的恶意文件。
  \9 S6 u+ o% _$ @$ a! I: m* I
" q; B. x2 ^" J1 I/ c
' W( T( x$ I) JTracking Sensitive Data(跟踪敏感数据)& I5 d1 J0 u& f6 p
信用卡通道的使用,并阻止泄漏。
+ g8 L/ J# R4 P* Y
* H: [3 Y# v% l* Z2 k4 G8 o& s# E8 E: d$ z, p
Trojan Protection(木马防护)
3 |2 \& [6 E) B& @# n# P检测访问木马。/ h+ u% o( V) @8 x
/ D% V! k' ~+ O6 ~1 W

* o( u+ W$ t0 ^) z0 s( ~Identification of Application Defects(应用程序缺陷的鉴定)' |/ R! U' c  d
检测应用程序的错误配置警报。
# K" j' T( ~. W
( l3 x) ?4 v7 J! l" b
+ s! z+ e; v' gError Detection and Hiding(错误检测和隐藏)
2 j& Q! _  ?5 x* S7 m检测伪装服务器发送错误消息。1 R( {; r7 z8 \# |6 O* ?9 o. \
/ p. \9 x7 `' Y' H
1 b) ?' A$ N+ ]3 U+ ?( e
安装ModSecurity' @5 H5 d3 E; D8 S  G! l
软件基础环境准备
& F9 k; N6 Q9 ~& P% l! v下载对应软件包8 M/ P; Z7 e/ l' e; @4 m/ L; N( g2 X/ e
$ cd /root# q0 }5 O9 s, y
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', e8 C& R% V" a6 [$ b+ Z
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
4 w# [; e  \' I8 W" M安装Nginx和ModSecurity依赖包
7 {7 Q  K4 q2 j7 UCentos/RHEL# a- I- o; i- M
- p2 d. m, v1 O& ~; Q7 x
+ m5 I3 L! ~* {7 G, m! ~
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% }9 G6 R" `  p0 X4 s8 a" UUbuntu/Debian2 I6 m+ [9 d; o/ P0 f
) v- Y% R' E7 V8 \& a4 _

* ?: H: P+ x) s* T; I9 t# y$ 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++-dev0 R* ?7 C9 ~" n' X1 y
编译安装ModSecurity
9 z( X5 F; c& h2 m" ^8 a/ bNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
! s& i5 u" z& j3 N7 ^4 v1 f( ?
6 p$ ?/ N; k( d1 g2 H% s8 x' b2 s& l
方法一:编译为Nginx静态模块
. a- v0 O, H  Z* _# V- u$ f; b" I( E+ i  g

' k9 |0 |% a' u: q编译为独立模块(modsecurity-2.9.1)# j& ^: p, q5 c
$ tar xzvf modsecurity-2.9.1.tar.gz
$ [5 O! i2 \5 p" g. p& M# p$ cd modsecurity-2.9.1/
6 O. f% [, {5 _: e9 `! a$ ./autogen.sh5 B& W4 e, Q4 x  M0 O4 i) t, l
$ ./configure --enable-standalone-module --disable-mlogc+ Z; A2 ]& C% V! l
$ make0 G. }/ {8 y' W' S7 K& S) u
编译安装Nginx并添加ModSecurity模块/ R# b+ k- o$ v6 E
$ tar xzvf nginx-1.9.2.tar.gz7 q5 u3 @0 n) O- k1 f& z! n! m  [
$ cd nginx-1.9.2
/ I& T, A* M! e; f1 i) [$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/4 p2 E( K5 D; t/ N
$ make && make install* t0 a5 p% l3 L0 Y; t1 i
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块# l; T8 P" @* m$ S2 t
2 k1 K2 K- `( b% s

' o# L) [4 r) w/ c6 M4 |编译LibModSecurity(modsecurity-3.0)
" t6 m: {7 x( y$ cd /root
0 K: s  P$ g' d( u6 p$ git clone https://github.com/SpiderLabs/ModSecurity
7 l3 S4 P' B, m& y$ cd ModSecurity
+ w2 l! C: H/ R$ git checkout -b v3/master origin/v3/master
0 t* e  u" y2 U7 @$ sh build.sh2 T, }8 P0 \$ N2 C: d
$ git submodule init
, t# m1 U8 p- X/ ^2 ~. w$ git submodule update% }+ r8 y* b+ r
$ ./configure- d+ T; b& U1 v4 h
$ make' _: p4 ^+ e/ Z6 e
$ make install! H( o( r3 x- u. E+ ~5 S
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。) j* z4 {" N( w0 I2 G

% `/ W! {% {$ n/ u* m
$ ]9 \, s, v, {9 L7 |6 }% {5 D/ }) |$ ls /usr/local/modsecurity/lib
8 E  m, G1 F$ J- nlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
) X- @1 b* r# p: A+ p编译安装Nginx并添加ModSecurity-Nginx Connector模块
: D2 d+ r+ }7 J. ?; Z$ Q. l: d使用ModSecurity-Nginx模块来连接LibModSecurity
+ {) O% ^$ V: h5 x
$ Q3 T$ u, N" h+ B# ^4 Q
7 p8 E# i5 E% y  c9 a4 C$ cd /root" D4 y+ }/ D. ~7 W. C" C% |3 L/ o
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
- |! y- q0 `6 K$ tar xzvf nginx-1.9.2.tar.gz" C; Y5 \. j9 Y2 Q( C
$ cd nginx-1.9.2# r" a- e$ S8 `+ T3 T6 A
$ ./configure --add-module=/root/modsecurity-nginx+ P, `8 p9 i" Z+ D7 z. V3 @
$ make
5 N, B/ K" U( r) T) ]; u$ make && make install
5 u2 a0 p' z* r% S/ Q2 x添加OWASP规则9 v* C5 C% X& j
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
$ w! I3 g6 g# d+ G+ p- C6 o) h( U! g4 A: \) A2 ^' ?

: u- w. g, a7 v9 E4 s; e; p下载OWASP规则并生成配置文件8 y9 M6 F  [* M5 Q% ?
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git4 V' q* \; m  E
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
/ S7 z% J1 C# j5 q+ D; B$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% h# i* A5 z  a
$ cp crs-setup.conf.example  crs-setup.conf
  N3 g# Z7 h% S7 r% P5 T. t配置OWASP规则
$ n) z. s/ P1 U) ]- h' D编辑crs-setup.conf文件, Z: T9 Q3 r$ ]7 i  _: [7 a- d" }

. p+ ~+ [5 N' M8 Q* h& q
; p' ^1 e( m0 e: a  g; k% I+ ?$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; f5 g, t3 p. H7 ?0 x0 r$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf8 S8 I3 w4 k/ L. G9 r
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf+ {7 D8 X! E6 \  m
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, ^: L  R% ^$ L( H4 [" L默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
! I" H; b- x2 `% F0 K* R! M
- V+ V* V2 [* o3 F3 J
$ ~! W# r" |- h  B  T6 v; d: I0 O/ {! @启用ModSecurity模块和CRS规则. o1 L; D. S; E3 I/ G5 a4 T
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ @2 G) K( i: W6 Y" s, i- _) h& O( \& t
) E, b$ D4 x7 A
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。! F& s- v- ]8 U4 T! n( M& t

; f' s5 \( a% I( F( j( r$ z/ @" M, d& A/ ]0 Y
$ cd /root/modsecurity-2.9.1/
9 f# S$ c1 g; H( k1 E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  % w) f2 B, N, ~( O
$ cp unicode.mapping  /usr/local/nginx/conf/3 l7 m  J' k0 y3 q9 t& @
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。; u1 s& J. J" |2 g' Y* D2 [+ z

5 x+ t& W% O2 L/ P9 y
/ r& y( {8 B; S* j: y# w$ vim /usr/local/nginx/conf/modsecurity.conf9 q3 w" e+ U- G  S
SecRuleEngine On
$ P" ], s' O( m( Y+ jModSecurity中几个常用配置说明:
1 g5 G( b6 I% B/ w* w8 y0 `" I( V5 |' p: \) [
/ Y2 y& c8 P) X6 d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
" K# y- ~# r0 e0 H: e/ l1 u1 ^. m! c, x1 d- |8 B/ F" U4 u( t. _( n
3 ]6 t: R7 s7 q: [8 l1 S4 Q
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
! \7 k: ]* b( i7 f. n: F+ z, d5 O8 g% E) n. x1 _7 _8 w5 c' A; |

- _8 H* f* P* b; Y7 V3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。& {, ^0 B! z8 f" }% D3 R6 V

: B. x' i# t& O/ G% |6 z
( g. f' h1 N3 v4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。& W" E& V# R) d- ]% t! k  q
- v3 m2 m# O% [. }6 j7 l, ]

5 E' O. I1 I" g7 e$ ?& L在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 Z8 m4 u; U  b7 C* K2 a# T

1 Y# a2 f; Q1 J  u, r+ R4 L7 L; W2 I9 C, `7 C; @- q" F
3.x版本CRS0 ~4 p2 U4 K4 f0 D- x6 q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 M) m) l9 u, C4 \
# 生成例外排除请求的配置文件: o0 L) \. M4 |! h/ `6 K) v% a
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
, t  m# }  D8 S$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 k3 V& u- ?6 n+ v7 V+ U$ cp rules/*.data /usr/local/nginx/conf" u& z: ?6 f3 E: r( |- H' ]4 D' M( J
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
* e$ X: j+ Z  J  t7 A+ j- E  X4 w+ l) S
5 ^3 W# d% E8 I! O/ a! P% Z2 V
$ vim /usr/local/nginx/conf/modsec_includes.conf
# O. p  j+ f, u. W4 L6 D
- s4 S+ `" X% L& b9 m
[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
! v" M5 a! B4 ~( P- v# Q" C1 t
0 ~8 u$ B; w' f
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 ^& P  u3 Y& Z3 f. y! _7 z! x% d3 C6 Z3 |: }( x

$ Y* @0 F1 k8 H8 C: B+ _配置Nginx支持Modsecurity
2 {- r0 j" ~& W: \' C* ]启用Modsecurity" L, ?2 I+ [* s. ^
使用静态模块加载的配置方法3 X" T% _4 Z1 U
在需要启用Modsecurity的主机的location下面加入下面两行即可:" t+ j8 ]$ C# S

, }" o$ l: s; G
# N$ c6 u4 Y0 W9 \8 jModSecurityEnabled on;
. R* i& D! k6 x! C2 e& O' RModSecurityConfig modsec_includes.conf;* R; O& Z  R2 |# a4 |1 S
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& }0 ]( \/ B* D) n" I; [
' F: v* c  {1 J' l. \3 Y4 v# \, K3 t
; U4 U6 ?9 k* @" U1 S9 k. N) J
$ vim /usr/local/nginx/conf/nginx.conf' i, p2 t% X- ~7 w. {& V" |' g
% z1 G4 y0 r- R: J- l( [0 T( p
  ]7 c5 M- s; h; V/ _
server {' ^1 l9 o5 D1 S( l5 G* w
  listen       80;
, A+ _9 _# W0 a& s, a. y  server_name  example.com;
1 K# N, D. B% h- j9 G& g# m4 d# K( D" c  m/ J! v: u
& ]9 o$ ~# \3 D+ Y
  location / {
7 ^6 K: M$ D# G: y( V    ModSecurityEnabled on;
- ^: w  t) G2 P; }* E; W4 \4 @    ModSecurityConfig modsec_includes.conf;
* |# Z, y6 v4 l+ j    root   html;
5 r; |* M8 ]5 M3 g/ X% z- `0 [5 H    index  index.html index.htm;: \; T. _4 x# x. d4 n
  }& e, m# L# E& @" a
}
& b* n5 G$ M: d- n使用动态模块加载的配置方法2 \8 F! j  ?- Y8 q  @, _1 v
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 D. }! P+ g0 E2 N4 }

4 B9 C& C4 ]! t; K3 t1 t
' y+ |8 K6 Y, v; A* d+ w+ Lmodsecurity on;
8 ~0 J, j5 b( B+ g. X$ emodsecurity_rules_file modsec_includes.conf;
1 z. D4 ?+ d9 [$ N+ ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
# ?: x3 ~1 q) r+ Z8 Z& a8 b2 ?" _( t0 x$ ^! {
% m8 q1 J: t( R3 [7 y! F
$ vim /usr/local/nginx/conf/nginx.conf
  S# o3 h5 B( e9 i+ H
( ~: a+ a3 c# n. ?3 H2 _* K/ D
3 C" Y1 r& m2 {. K2 J) n. G5 Jserver {
8 |2 h* ~3 a+ T7 o  U  listen  80;
/ Z5 c2 S2 a6 x4 X; l( n  server_name localhost mike.hi-linux.com;: s1 w' o  u$ j' H4 e
  access_log /var/log/nginx/yourdomain.log;1 g- ]  i+ |7 i% ^5 ?% U7 s* l8 T% ]
- F) \& r! s' b5 `- y7 {

& m4 p+ E$ b6 Y3 D9 ?( d  @  location / {4 [) U; Z8 J/ Z
* N; S7 D4 l0 V$ h: K" a6 o
' r( S3 ^9 x# ]+ g  T+ o3 Q
  modsecurity on;
4 Q, Z* b) Y0 }  d  modsecurity_rules_file modsec_includes.conf;! r! Z# L% A+ a6 k7 l8 W% T
  root   html;* `0 ^' Z! V) G" P. x/ p+ r
  index  index.html index.htm;
$ s, G6 i/ X" U& e' z2 s- K}* r2 o) ^% @' z0 @. _5 Y$ A' W$ ^7 i
}5 Z% J3 n2 o: {4 ]  m7 R; T
验证Nginx配置文件' ~0 b9 K( G. Q7 b8 b; `' @2 E
$ /usr/local/nginx/sbin/nginx -t
% L: D" C, ~7 ]3 U/ z: `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9 O# L, A1 K5 v8 snginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 ^$ d* `$ z: ]; q4 {2 E
启动Nginx
4 P" j1 Y8 Y2 g. G2 F8 G$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 e8 ~. e& i, {; I% v9 ^9 U6 _

/ y& s2 _- m3 c7 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能够战胜更多复杂/未知/混淆的攻击模式。

* ^8 `/ s5 c/ E" Y

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-11-4 21:03 , Processed in 0.077317 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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