找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11580|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
+ |7 g2 `9 Q+ t: D
0 G; s% m; Z2 P& x' [! h
3 @1 i4 W2 o: ~; `# l在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。  T7 Q4 l4 Q6 e8 y1 H0 q

9 @% g2 S7 W- o$ f) {8 A) {* K
: a2 X, L9 L" p7 ^2 }9 `& `什么是ModSecurity
/ p7 J* `+ ~2 B! rModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
9 T2 c1 i- G- r0 @* b; E% M
; M( D1 P/ N+ k- Y# Y
; S) ]8 q# L! G! ]- r" {+ A& ^8 CModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
* S$ Q. \1 p& z+ f6 J9 s- _- O2 j( v" D" M" I! j
- v: f4 `/ q" f3 t5 V& j
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 c6 k" O" y& r- j
6 ~2 h7 x4 M- c( B( v2 U
1 n: A/ |- g- L* n% B
官网: https://www.modsecurity.org/
* m- A: y* ^9 f+ p! y. d
/ N( \" P% j' B' N% o4 ]) ~2 y1 h# `" \' V" B
什么是OWASP CRS
) U) m& J! S+ N: R- g( O& O& ]! Q( SOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
0 ^+ S  i, ?, l- i: W- ]3 S* ~0 D) Y

/ {+ m5 R5 [( Y/ kModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。. i) `! R  ~- h& i# I6 O0 p7 r+ }
, k- b1 R6 X$ H

8 L) P1 _. K8 f8 QHTTP Protection(HTTP防御)
- ^7 C8 \+ r9 ^$ I1 y0 dHTTP协议和本地定义使用的detectsviolations策略。
# ]& Q, F, R4 m/ O0 G/ _
0 G) X' c* I4 p# H) J. ^- V6 @+ B
" J0 [! y4 D/ x, H6 L9 Q3 I# jReal-time Blacklist Lookups(实时黑名单查询)
* _2 m4 O5 `. v* c7 R利用第三方IP名单。
" L: F6 E# v) y: R+ r" M2 ]- ~# o  m2 a6 v
/ z/ {- V2 k0 [: J
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
) R' H* g7 b/ a% Q: k防御HTTP的洪水攻击和HTTP Dos攻击。
5 m6 P3 S' c% ]! X$ m! y) H+ D( ]/ L0 _9 K$ O; K: U: T5 Y+ a1 M% o3 J1 a

# S- o, u% L0 `* l4 KCommon Web Attacks Protection(常见的Web攻击防护)
# W5 E9 d+ u: E: T% c& w9 W% G检测常见的Web应用程序的安全攻击。
% o6 P0 ~5 g/ p( r! A* L" N! ]; q* E5 l( N4 _
3 o8 a% l, S* s9 U2 ~& G6 F' r* m  [3 G
Automation Detection(自动化检测)
7 H( q$ y- E# e  k% d6 {检测机器人,爬虫,扫描仪和其他表面恶意活动。
, K) |' s& ]: r& O: _
2 u2 L9 f* M' _7 S
& s, ]1 X7 r/ p. j; o& RIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)- a0 |9 a+ ^% ]6 X0 e; [
检测通过Web应用程序上传的恶意文件。
6 }# z3 l5 Q7 s( v) u2 ^9 z/ ?" Q+ X; q  m
9 U: m0 B/ S! U# a- _
Tracking Sensitive Data(跟踪敏感数据)
; q* ^5 p0 o1 O7 l+ ]( W) m" n信用卡通道的使用,并阻止泄漏。
$ B, t, D; ~% E( A% V! C4 L6 Q! {! p9 {
0 D5 o% i: G7 l! v
Trojan Protection(木马防护)
4 m: n) o4 L8 L% O1 l4 y8 I6 A: o检测访问木马。
' g) k0 {; w6 H- S; {5 q+ z# \9 M/ |! r$ w  Y2 N* g
" |) b2 w- ^1 F+ R4 J9 D
Identification of Application Defects(应用程序缺陷的鉴定)0 I" A$ l/ S9 ~0 ~( z9 R) m& h
检测应用程序的错误配置警报。
! M  B1 Q$ A# B1 V7 h% B9 ~- g' l5 w6 l' q% R

; J; i2 P5 \9 r% b- g" [Error Detection and Hiding(错误检测和隐藏)
6 a" W  X! I/ K- F6 X9 a* d检测伪装服务器发送错误消息。' f* v' b4 u* f2 k3 z. ^* ^8 ]% `) W

( t1 a+ e$ l2 u  ~
" F3 B. D; z2 Y; f8 N安装ModSecurity
" d8 }# a6 q3 L: X" u/ i软件基础环境准备: |6 S% T! V, }) S' q8 J4 E& s
下载对应软件包+ C# a! z, x. ~! L
$ cd /root
4 H" A) o% a7 s) G4 K3 [8 Y7 t$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
9 v6 A. O, L6 Z; f( }$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz3 n: f/ ~: n) ?, e; l
安装Nginx和ModSecurity依赖包+ W+ w  k  j2 O: j" y9 |
Centos/RHEL
. q! D! h6 I$ L% a6 x- t; O6 s# Q
2 p& {( q" ^3 F! y2 ]. W1 \" I( b
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
$ x! j! [4 G5 G1 UUbuntu/Debian
1 C. n# N% V! {+ Z. K
7 `4 s% C% P5 j0 Y5 N% @0 [3 B
4 r8 `5 j/ u4 S! ]6 }$ 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
$ O" W3 A' ?. s编译安装ModSecurity
2 z& u! e0 ^+ i4 j+ y8 D! {" _0 vNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。& Y4 Q2 o- F7 t

& \$ L; z, c/ @4 ?$ h
' v# k* L" ^3 R方法一:编译为Nginx静态模块+ X0 m( C! Y* p, j/ v* ]# h
9 }! m* d0 M  _  o$ x! b0 G1 g% z
. W6 a( `% T  x# }+ }# Q
编译为独立模块(modsecurity-2.9.1)8 p% y( ~+ U! e8 X' D
$ tar xzvf modsecurity-2.9.1.tar.gz
8 Q" j! D) t* R- l$ B. C8 j$ cd modsecurity-2.9.1/
9 F" S3 |  z2 I$ ./autogen.sh/ o1 O6 u9 a( I- g1 V0 T5 o; ^/ f
$ ./configure --enable-standalone-module --disable-mlogc1 K- O, d) l: z- \5 n( ]3 y2 h
$ make, |! v" p) N8 J8 S5 G6 r# r  ~
编译安装Nginx并添加ModSecurity模块
3 J6 k$ l" j4 D/ u: U' N$ tar xzvf nginx-1.9.2.tar.gz
: \6 I9 k, {' l5 i; f; g8 E$ cd nginx-1.9.2
0 E3 _: Y: G7 S9 d- ~4 m$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/2 }3 m$ O) \4 i  m4 P; v
$ make && make install
" T) u  A( Q0 O% j9 x7 U方法二:编译通过ModSecurity-Nginx Connector加载的动态模块" ]1 M9 s6 A) n8 E

+ k8 @0 @% E# ]8 K/ {
, {3 _& M! j2 @, Y1 R编译LibModSecurity(modsecurity-3.0)/ X7 d/ k9 Q9 e% O6 ~" g
$ cd /root  l: C. q. J3 a. l
$ git clone https://github.com/SpiderLabs/ModSecurity$ q9 R8 [7 _* k3 t' v; i+ j. J
$ cd ModSecurity
8 d- ~5 B, F) @& f' q. u$ git checkout -b v3/master origin/v3/master/ r6 H8 @# h4 u4 Y# ~/ a& Y
$ sh build.sh
' S8 b* G0 x2 a- |$ git submodule init
" g& M5 _8 Y2 y$ git submodule update) k% G6 x* U0 I, G; `$ @' D( i
$ ./configure$ g9 f0 I( M  @! q9 _) f2 V
$ make
( Q' w8 r2 N/ j# \+ K/ r) ?$ make install, t: Q: a; N  B9 {' o8 t4 c
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
7 u8 W9 q  |" t  L5 F9 B4 m. R, K2 o' r( [4 A8 W8 z9 P& J
" U) R' n+ W2 X/ i. {2 H  f, O6 A" I
$ ls /usr/local/modsecurity/lib2 ]9 u+ ]; O$ l6 q! G
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0* U7 Z* j6 i4 S% }
编译安装Nginx并添加ModSecurity-Nginx Connector模块
/ G! b; ^1 o- _4 Y使用ModSecurity-Nginx模块来连接LibModSecurity
% w" f: V6 X5 G8 i; h2 L: h4 S0 K. E7 V. w3 v/ O5 q7 a

: `+ A: J! G: T$ cd /root9 p( W! w' w  u& U! b% I
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
+ |4 U3 u/ x4 ^& x) Y  }$ tar xzvf nginx-1.9.2.tar.gz
, I. p+ h. ?! X5 k$ cd nginx-1.9.2
  L, X; I! |0 R! a/ |& [3 }# x$ ./configure --add-module=/root/modsecurity-nginx
" W7 _8 C8 F+ d9 O7 [5 _" W6 w+ o2 g$ make: B' m8 W; p* s" [  c2 C; o
$ make && make install) e( S: I, l! F( s! u
添加OWASP规则
0 H, ?0 `. _7 SModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
, W9 }0 P3 w2 Z5 R' ?2 n& H( y# O
4 k/ `" j; Z) f8 ?: }+ L
下载OWASP规则并生成配置文件
0 G* p) _# E- L- M+ q- i/ G$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git3 f) n+ p0 @4 ]# j# Z* w% Y
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/1 R# x4 M4 A! ]6 j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( @: `0 D- H) S9 y' N
$ cp crs-setup.conf.example  crs-setup.conf
* @3 b& C7 J3 k; m配置OWASP规则. F( @- A& Z4 v5 A8 q- P
编辑crs-setup.conf文件4 H. v/ L+ X( L" ~% ]& |
6 h' Q- Z' @) a

9 j; ]  T, A: `" f* E" S$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
/ n! \. {8 [+ Y( @$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
4 Q" i/ m+ T) b- d5 F$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
; c  ]; G; p2 \4 Q/ x1 R$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf5 w& g7 ?9 z+ x5 b
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 c1 J  X) v4 T' p- a, {! z9 @; m  A0 N# c' ]# k

& d$ R4 p0 m! {2 C) l& }* R$ V启用ModSecurity模块和CRS规则1 y! F! P% W4 W
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& b) p8 R7 x, q1 q: S. X
/ I! d1 D( e  U( N! x! w0 a& x& L. A

/ ^; L. z6 Y! K% J! d4 I4 p; Pmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
. k1 B  Z, }8 U0 f+ a0 l
! a0 U$ n9 }+ i% k4 ~/ t
+ g/ @: E0 v7 `5 f  i3 q. ~4 H$ cd /root/modsecurity-2.9.1/
+ _; F- I2 ~, I& k# j# e0 i2 S. }$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
5 ~- q6 K0 m  b% W* N$ cp unicode.mapping  /usr/local/nginx/conf/
8 m' l0 b6 M$ E$ |, i6 t% E# |( Q; g将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。1 q" V; @$ S5 p' l: P  }
$ z" n+ A+ _% a; w: p
/ g& W: T3 H+ Y; o7 V
$ vim /usr/local/nginx/conf/modsecurity.conf
4 _" F4 V! K: Q# V( @! S4 VSecRuleEngine On
8 e* S( A5 @* v5 Z4 n) dModSecurity中几个常用配置说明:
5 s7 f1 a9 q; v1 Z* Z# w/ f, Y$ @: Y& ~: F
4 S) v( n- c6 L4 }  Q  |- L
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。/ L, Y) w- D1 Z! D0 z

6 u: n' B7 [4 T( C4 y. x/ R) [! m" g0 u7 B# b- Z
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 L, t$ g  H( ~) c  s* c
; x9 D7 Q) u! E. z5 J) d
& O1 C0 s0 W$ R; M- j3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。6 S1 r4 h, x; _" K3 L8 `
0 n6 P1 V, d1 [8 ]/ Y4 A
( G" b2 O. `/ I( [% Z* D
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。: @, h; R5 i8 |: l7 L
- z5 y$ F# T$ B% b: M8 G" u

2 D8 t; R. S8 V6 ~, ^) g( w在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
3 h# v/ W% Q( h  i+ i3 [+ @) n) ]4 d

5 D# T! a2 }+ t! e3.x版本CRS
. M! c: O. v, J$ y$ P& a7 j! ^9 a$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
9 ?0 c+ P" C9 i( t7 @# 生成例外排除请求的配置文件
& W; p2 E- y' X+ u$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
/ g0 Q. e  V8 l1 c( h$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- S4 {& v  Y4 c/ a
$ cp rules/*.data /usr/local/nginx/conf9 a4 ]0 @2 h2 Z( T, t" u9 g* L
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
$ |  P6 m4 b8 _/ e" {' l2 ?
6 n! M/ O9 Q' s  l4 @
2 F! f  e5 H% N8 P/ U# K/ x  M$ vim /usr/local/nginx/conf/modsec_includes.conf
/ B7 g/ a( r& Q7 s) ?- z2 ?1 ~8 {" B1 x" A- y, Q7 n  q5 O5 _
[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
) ?' u9 n* y: B2 \; V) h
, M0 P( z# m7 H+ D
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
  X1 ~/ n; ^1 D: V6 {
; g2 Y# y& z& l/ x/ V  @
0 y: D: n& w; E) i; ]" c配置Nginx支持Modsecurity
- a- n6 y8 i6 H5 ^4 L启用Modsecurity
7 d2 b* Y7 Z/ Y& X使用静态模块加载的配置方法6 ^2 f: i, P2 W. K* Y9 U
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 O) ]" `5 y0 F9 i. O1 k4 W6 S/ {) P/ e
) N$ C3 L3 U7 Y
( ~! k" D" L% q0 A+ Y# g+ y7 y
ModSecurityEnabled on;7 D+ w/ R0 c& u2 z, j3 A
ModSecurityConfig modsec_includes.conf;$ f" L3 r+ _) W; A! h, L3 {7 }; O
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。' m% y$ g: w4 Y  Q: f9 [0 O+ K% t

& J, Y: E: g  q7 @9 o; o, K9 o9 K% n7 o
$ vim /usr/local/nginx/conf/nginx.conf1 F& ]+ o* {: d& K

! o' k; V6 O- g+ k" @7 `) p! K& e; J  [- m. f% R# \
server {- h& y" ]# A2 |( u, J8 C
  listen       80;- S  l& R; `$ N7 Y9 A
  server_name  example.com;
& N" A; N. v, H6 y  y- r
3 m/ Y5 l3 a0 d* r! z; A  F( s" D  a7 E3 q8 l5 g, O7 `
  location / {+ V  j. {- d8 P2 k' B& h: S
    ModSecurityEnabled on;
# ~* |3 y3 G* x# u# ~    ModSecurityConfig modsec_includes.conf;5 m4 I) ]7 i$ B' r, L
    root   html;0 u  ?8 F7 V+ x3 b' o
    index  index.html index.htm;
  M/ G* L& _8 Z1 W" ?  }
7 t8 Q/ o6 t' N. [5 [}6 Q  y  R& h% T5 I8 j. h
使用动态模块加载的配置方法
' g9 P% Y5 m! U$ z! u% f6 ]在需要启用Modsecurity的主机的location下面加入下面两行即可:
" Q/ u; v( [! {4 g3 Y) _* y. i/ R' ~
- J  Z8 R4 B* h
modsecurity on;# n$ o4 y. T* _" E6 u5 ?5 A
modsecurity_rules_file modsec_includes.conf;
$ `6 @' B) w. M/ \0 {修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ s6 t  h) `8 z4 U" i% }3 Z
# r* v* }1 t5 X% c# m& k
4 g8 [9 Q# N9 g, c2 l
$ vim /usr/local/nginx/conf/nginx.conf# G; ?& V' h" `. u( J
! g  |0 J2 l. `( N/ K& h

( l9 J8 X0 }. x( F# h# D2 R% [server {* Y9 Q, l  n- \; F
  listen  80;
% g! u0 J+ M' A$ C2 [  server_name localhost mike.hi-linux.com;
5 F) ]. O; G1 C+ Y) o- s  access_log /var/log/nginx/yourdomain.log;7 Q" ]. \# u- V9 P
- o! q% W5 w( @4 D+ L+ ^

- ^7 c  a! [7 |% l. J# e0 x  location / {) U: h$ f2 e/ B) Y- t1 k0 o
/ p( U3 X% u1 h- w

% v& O& }, @( S, _3 p( s  modsecurity on;
, s5 B  M' C8 o% Z' \  modsecurity_rules_file modsec_includes.conf;
% J2 |0 r; G1 `$ Q  ?9 h  root   html;' L$ m+ p% d4 P4 P8 }& w, [( u
  index  index.html index.htm;
! r( i* O2 I) Q6 w# V/ f}
8 U. z# i6 v2 r0 F5 ]}) p, q6 x8 [( r, [1 D  J4 L& I: g
验证Nginx配置文件
& y% ?9 B" ?7 `6 h  Q' m3 ~& U$ /usr/local/nginx/sbin/nginx -t; d5 i) x0 u- W" f  N9 W( d8 Z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 R) {3 z; s$ b' h# u, S* Y8 b4 c3 O
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful' {2 T3 m( E# G/ B$ w1 [
启动Nginx$ c, o, C$ O6 [( h$ P( f
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 K0 B8 S; s" @) k0 G" l
: ]% U8 L- r) G/ y/ U: s1 ]- q; M

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

+ s  b0 P9 i7 _9 V! Y

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-25 09:53 , Processed in 0.064890 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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