找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12253|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 I# n4 y! x4 m( Q7 X+ U2 Y4 E( [3 W* e' t5 H
; Y4 A) b" S6 f
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ x6 U  A- N% j
7 A- \2 c7 @! I) N! F
$ n) |0 ]  N" z, P4 g什么是ModSecurity6 O+ A' O% l" i8 d* b: ]$ @
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。. m2 @- z4 C8 z% B" S0 R
$ S2 y, t" ^' v3 m8 H- c$ E

9 k4 O2 u  N, M3 S) uModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
! P+ m7 h* a3 a1 _2 @
3 `0 q& C( i& y. R1 P8 v+ S
' I3 n) n- [, u9 n3 \# WModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
9 O1 r8 [3 X. y, z: j& D; J% S- a# I% g6 m- L. o' A% {
) F/ D/ v- E! R6 X" ^+ G# H9 \
官网: https://www.modsecurity.org/
  y0 x! [/ S9 d! U- u2 l& H8 S1 Z) p7 D7 s2 k3 `, ]- A
6 P5 p: ]" Z3 s5 v8 d. o7 U0 t
什么是OWASP CRS
8 q. z. n1 i& H; {6 yOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
/ f: N9 B4 \0 i( A  E4 f2 |1 A+ Y' b" z( V$ n7 K5 b& y

4 K0 ~. d0 Z8 v* V/ P- w# [/ jModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
  u/ c' D, E& F  P: _7 _+ Z$ n9 C5 D" h0 ], h
+ f9 G& _! E& Z0 h, n- a
HTTP Protection(HTTP防御)9 {3 |2 L2 U, M4 P' M
HTTP协议和本地定义使用的detectsviolations策略。
' v! }! ?7 B& K% }+ g4 v. e: Y$ x# P: z; b

8 O9 H/ V# Q( c( P; M3 y* YReal-time Blacklist Lookups(实时黑名单查询)# h# q" A; C, p, ?  u0 U
利用第三方IP名单。6 e* w( Y& H+ |2 N! W. C

7 N2 d$ S- @/ D* a* j1 a2 T4 |0 T' E
1 s: N- k* X, Z% nHTTP Denial of Service Protections(HTTP的拒绝服务保护)/ l% u3 A1 J" R3 \. a9 Z! N& Q
防御HTTP的洪水攻击和HTTP Dos攻击。$ K, D1 e# L5 ~. p" `  B# D2 J4 _
+ w% r) K( G7 I3 H! h

7 f. Z' `5 u( q9 z& E+ M  k: ZCommon Web Attacks Protection(常见的Web攻击防护)
! X2 l1 I0 a% q7 d7 `) B  P检测常见的Web应用程序的安全攻击。
3 ~( E6 e, K: I
* J" o: |- J- A; C3 l( B- W- h
8 M+ V9 ]3 }  i3 r$ cAutomation Detection(自动化检测)' W" g, T- C. G- l, M
检测机器人,爬虫,扫描仪和其他表面恶意活动。
; {  n8 X9 a  l" ]& c# a- e% ~# w/ h! X, W+ }( o2 F5 `" y; w3 v

+ W* I0 i! v8 h( E( E. X" ZIntegration with AV Scanning for File Uploads(文件上传防病毒扫描), h# \- j( c/ r  ~
检测通过Web应用程序上传的恶意文件。! }4 e  V  y, H3 ^
5 q% m# l( b0 o9 H
! h# G, f/ q6 n6 Z0 A3 D6 l, J3 }4 N
Tracking Sensitive Data(跟踪敏感数据)
7 F; ]  O' I4 U5 A) \信用卡通道的使用,并阻止泄漏。, |. O3 ^1 t, k2 w( D% F, ~" o

* C& Z0 r& D! w) e6 h" f, J9 c0 e! ]" ~0 q/ K3 J2 O- C/ s1 k
Trojan Protection(木马防护)
+ Z; V9 X$ [4 V) x6 w; c检测访问木马。
* `) x8 |$ [; d% }# n) H* L
) _. q" S2 M- d
, {2 [  D% P$ Y. a( L9 xIdentification of Application Defects(应用程序缺陷的鉴定)# l# J! k9 G6 q7 _$ P
检测应用程序的错误配置警报。7 `; U9 P# A5 h1 ]
+ }1 |: e& f" K. p6 }, L2 I
1 `- y- f$ N. A- ]( F
Error Detection and Hiding(错误检测和隐藏)  c$ ?- {, P! t# ]1 r
检测伪装服务器发送错误消息。+ V+ _& j. p1 ^  g
8 F$ q( F+ V$ ^  _  G: c
$ E: j# E) \+ K/ N( u4 E/ s, q
安装ModSecurity0 V& d, w$ H; i  j- c' k  K! P4 C
软件基础环境准备0 ~  T0 z. j* j; s1 ^1 Q( w% R
下载对应软件包* Q: }, |7 `( V* X5 s2 H
$ cd /root* n' c0 V" u6 j3 N3 ]
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
# y: X7 ^9 r* C, e# L( _" O$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz% g% I9 m" a# h# K
安装Nginx和ModSecurity依赖包) P& {. ?4 c# V2 n- f* L- y' e
Centos/RHEL
, I( r% D! a6 m, `, ~; B7 n: c: L. a0 N" _( F  t! B
; P7 c( t$ Q+ w
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
6 j4 P7 \) z) Y8 I8 \0 bUbuntu/Debian
! H& M5 v  |9 {
! G8 `8 L( M5 b9 b9 X  m4 ]8 `. Q( I) _) I
$ 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+ H7 G, p1 E) ~4 u5 ^2 _3 K4 Z6 d
编译安装ModSecurity0 V: \4 j7 c( `  D# {$ f
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 q2 p4 _6 g( Z

' S+ M# x* T: l3 _8 l7 P# V: v. e3 v  D3 {8 q
方法一:编译为Nginx静态模块; y1 b% s/ U( ~9 t$ c

; F/ s# r& d7 c# R
( i. X% Y3 P/ h6 b5 L, U: ?5 W: l编译为独立模块(modsecurity-2.9.1)1 J: h- e0 ~9 y8 Y) ~3 Q/ P
$ tar xzvf modsecurity-2.9.1.tar.gz  j  W+ n/ r+ [
$ cd modsecurity-2.9.1// f# f! g1 h4 i7 b5 j4 i
$ ./autogen.sh
' M  x8 m8 ~5 k6 f$ ./configure --enable-standalone-module --disable-mlogc
+ B, y, ]9 i  b0 G$ make
% Y8 [' |) I# ?8 t  v' O/ ^( A编译安装Nginx并添加ModSecurity模块
4 ]# h! u% N9 W$ X, z' V, B$ tar xzvf nginx-1.9.2.tar.gz
5 F( {) y' ^# B1 x( y( Y% P6 O( @9 {$ cd nginx-1.9.2- I/ X; }6 ~. K$ p. i3 x
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! X  f+ c6 w' J  l0 o9 p$ make && make install
# m1 x, O% k  U方法二:编译通过ModSecurity-Nginx Connector加载的动态模块2 f6 a8 B8 I5 z
) C* l' G1 @6 {. D

" u3 u7 z/ _, k0 }) m; I编译LibModSecurity(modsecurity-3.0)
* o1 ~. [4 q- i' i; I6 a# V$ cd /root
0 }% c# |4 Y% e7 }$ git clone https://github.com/SpiderLabs/ModSecurity1 _9 k6 o$ {6 c$ t" ?
$ cd ModSecurity; j" G% |. M, i- X5 A
$ git checkout -b v3/master origin/v3/master
! _# H9 r; R" ^* s! i6 u0 g5 F$ sh build.sh
7 Q6 G4 p$ Y/ C) c+ T$ git submodule init3 A. S0 K1 d; `* p5 o0 F
$ git submodule update) ^6 C# R3 W. \2 I/ v7 k2 a
$ ./configure
. D' M5 h" G3 Z9 i4 U7 s$ make! h; O, ~2 X6 L
$ make install% k- \& Q* U$ u( Z( Z6 ?5 R/ B) _
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
" P9 p( }2 @  ?1 l& q1 T% }
4 e5 L6 @( Z1 x
: i  t2 t9 e" l6 K7 _1 Z$ ls /usr/local/modsecurity/lib
" [- y8 x* U9 j4 hlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 L) a2 X4 _: d. n& P; s! t6 a" _; c
编译安装Nginx并添加ModSecurity-Nginx Connector模块6 x1 [+ h7 P) Q1 C) s0 @; U
使用ModSecurity-Nginx模块来连接LibModSecurity
, g+ B+ s1 Q" k$ a/ t% i9 e
/ g2 ]+ a1 n$ V# G5 P  L
0 J! R+ ?" C- a7 \+ p7 _$ cd /root
+ ~0 T3 X) u5 J1 I: {$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx% ]! [, i* C4 C% Q
$ tar xzvf nginx-1.9.2.tar.gz
6 F/ x8 J* G# [- ]" M+ X$ cd nginx-1.9.2
0 z  Q# C7 Q# {: G' a) S* E- h$ ./configure --add-module=/root/modsecurity-nginx- P7 \" o& P# d% n
$ make
2 ~/ F3 u! z) f, i! T" o% x$ make && make install
0 J  u* h6 B4 I& M3 i添加OWASP规则
6 }2 m" l1 j0 W9 P3 tModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
2 R- A2 s! n  f" p! d. f$ K  m2 p* t  s4 o1 Y7 F

. i( V6 g, R) E  ^下载OWASP规则并生成配置文件
) e0 y* j; f5 w  ]; a- b* y2 x, W# u$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git  K! s7 ]1 l: |1 D) f$ N* e
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
2 I4 {2 @5 P2 z/ Z. S7 i# o* f$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- P! c3 @5 e4 Y" W( X' ~4 f. w1 \
$ cp crs-setup.conf.example  crs-setup.conf  N' r+ y2 W8 _4 w8 n) L
配置OWASP规则
% f6 p! d# s" q, V7 H+ H8 T% b8 ^: _编辑crs-setup.conf文件" A) u  d5 O; K

! e; n" o, C& z& \* [! ?' |& {/ u. y$ L8 v! c7 S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; P! P9 D5 W2 k6 x  u! n8 {7 k+ A8 S$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf$ Z) H6 _  W% l2 }4 Y5 C4 t' ?
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: Z' c7 J1 F2 c+ r6 V: s* V$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
0 d6 y# i+ F$ N7 E2 [默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 X4 y+ P1 u8 X6 q
4 s% y+ v, ~+ w5 r4 F

! V4 e( ]# C/ }# x启用ModSecurity模块和CRS规则; V! M3 r( ]' W: }
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。  }7 D' e* o: ^% ]# `7 E

! A3 O, g% V; O7 W% U! _5 R! g9 ]* D- _" p5 k& P% i# A
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
+ F0 K2 \) L( |7 b( L( _. }$ d& n& U' t/ y! _1 ?- m9 }; }9 ?$ a

8 W. e2 u4 r/ _: ^( s( X5 _* U7 u$ cd /root/modsecurity-2.9.1/9 m$ q% L" M: g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
# t5 c* V! ~. ~# M7 M9 X8 l$ cp unicode.mapping  /usr/local/nginx/conf// \8 |+ b. ~8 [' l6 a# K: F! F: e
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。: S1 T: z# ?1 e7 D6 y
# V9 E9 g8 y% [+ N  x5 G
! j# p# }" Q6 x' C4 m
$ vim /usr/local/nginx/conf/modsecurity.conf( J* k0 ]; l/ h
SecRuleEngine On5 {; U7 D9 k" x  J, [" {0 H6 U
ModSecurity中几个常用配置说明:9 {% A' U; A4 [+ s8 c  L' u

+ z4 `( _- l6 Z  R9 v
* p$ \0 R. x. a7 p1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。- j$ ]! j- s! d* R* M, [- c. D
1 G; p& c5 p1 T$ @. a+ x& j

9 w0 }+ |7 I9 }& l7 K$ E+ K2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。6 Q2 Y% d7 x+ X3 j( t( Z
- @3 ?  u2 w7 w# B, t* Q0 Z
* u. a% J! t/ w) U* L8 g
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
* G: g8 b! w7 R
9 w8 l/ _' J! v2 O8 c9 U" W; G# ~8 u. B
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。5 \* o! n% C; T6 ~. _( h

" {1 ?& `% t  r6 s% @/ P2 v
/ W2 s0 V0 L8 ]6 A/ i3 t" @在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
5 D" I  S8 f8 V
8 k5 d: ~% `. n7 C* q3 i3 K6 @
' r* o1 B- m3 t6 h; B3.x版本CRS3 T6 Z# ^4 {9 f- e7 t4 K
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 w. j1 v/ ~" H: o. ^
# 生成例外排除请求的配置文件3 z7 L9 f% S; e# o5 Y: u$ w- S
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf. v/ ]8 ~# o% {5 ]* d
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. {- b/ h1 T& L  r% X
$ cp rules/*.data /usr/local/nginx/conf
* U" o% i( h  a为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
3 z( w- r% h8 ~1 P+ ~- p. }. W# i% o

4 c4 a  c3 M. Q# P/ g$ vim /usr/local/nginx/conf/modsec_includes.conf
3 A+ A8 @9 s- ~) @/ r* ]; ~7 P
1 K# c: A9 i  P' J
[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& t% M! V9 K# R9 z
/ [# U0 y  X3 V9 \- d, R8 [( f5 `
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。  m) y" ]3 _  L1 H% u

2 b3 q4 o7 D  R& W0 t0 w0 o# {
7 E' h% M' i4 E3 j$ s; Z7 x- [( k% ?配置Nginx支持Modsecurity( \- v; p$ }6 j2 |( e
启用Modsecurity
  j0 v1 x6 U3 P# ?使用静态模块加载的配置方法
( o& E# l" y5 f! ]4 F: j在需要启用Modsecurity的主机的location下面加入下面两行即可:+ O0 k3 X+ s( Z3 w$ {5 \% E
  _% _6 f" w$ ?8 K) s. ~4 N4 \
/ r5 L9 `. ^1 w: w$ Q9 H& ?5 j
ModSecurityEnabled on;* e' Z( `" S# Y5 g6 o8 e
ModSecurityConfig modsec_includes.conf;6 h- G$ p) {( @  K" O8 q2 Q
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' l3 e/ B: b$ u1 ~; v0 b( R; |( @$ U% S' [4 _' B

1 A1 e* J. m# A: ?2 Y$ vim /usr/local/nginx/conf/nginx.conf
4 X3 `( r( N8 K" r, w2 m  v+ _. t% Q2 `0 @1 ]7 x

6 T) A" o! W  a( X, U! ]9 ^server {7 F  Q, V5 c* h) K) Q. K$ J  l
  listen       80;5 U$ K5 I+ Z7 h% @9 M
  server_name  example.com;
4 j2 q# q; [5 `$ }- D$ p
! Q9 B0 `% }; h1 L8 v: o3 {8 H$ Q; S$ }9 E6 m9 f1 o0 D& C
  location / {
2 F! M# {* b1 s9 M4 o2 _7 a    ModSecurityEnabled on;
/ F8 c2 g* ?- g+ @# @! Z/ j+ m    ModSecurityConfig modsec_includes.conf;
3 O5 S" p$ w; [    root   html;/ _% i% U+ W4 U- D% x7 C* @5 p
    index  index.html index.htm;
* ^5 g' R/ a" t+ J4 s  }- `. A4 p) A9 |
}
5 y: I( p9 }, A( Q' g使用动态模块加载的配置方法* s/ U$ s) `6 N- L; K# l
在需要启用Modsecurity的主机的location下面加入下面两行即可:+ Y' `( P( y8 O& B0 w) g
: e- k% }) f3 W% g# W9 I$ y$ G

2 S) y% O  g" k% c) gmodsecurity on;
% u; Y% m" Z) D2 E# Wmodsecurity_rules_file modsec_includes.conf;
- {3 \5 j; U- B/ k5 j+ u修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 A, f- s9 n: C, J" Q# h" q

0 M4 r. ^* e2 ?" e2 Q/ H2 ?3 c, T
: M  ]/ U3 ?% o5 M; G$ vim /usr/local/nginx/conf/nginx.conf/ r5 ]/ s* K) \1 M, h! \
! k; M2 _; W; u* z& y& e: \# L
. _/ J3 Y1 G( G  b& U
server {; x9 k+ |( X8 @" G1 u
  listen  80;
7 [9 m( }$ S4 O3 J+ T  server_name localhost mike.hi-linux.com;
; S  Q3 H* T$ y7 @5 _  access_log /var/log/nginx/yourdomain.log;
1 G, L. n4 s7 h! O/ i% p- }+ r/ S6 }" a- a- X
' @+ z* m8 f! U5 o; }* a
  location / {- N+ @7 W* N3 K/ E
9 b3 O' Q: G* i! X; ?

( o& J6 E2 Z* w( \  modsecurity on;: ^& L* K$ n% O1 Q$ j+ E  Z
  modsecurity_rules_file modsec_includes.conf;5 d/ I/ _# k" W8 g) ~- R! B& a
  root   html;
) I# `1 v7 ]+ W6 o' i7 K+ w  index  index.html index.htm;
$ l4 o# `8 C4 K" T- U9 i}- u7 H2 g! x! ?
}
- r) f" m& |3 R  d/ a! B验证Nginx配置文件3 B" X  c) s8 N6 p! g0 \3 o
$ /usr/local/nginx/sbin/nginx -t
- O& _- k/ Q  `4 Snginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
( y. r6 L) [! H" ?nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 s3 m/ b! i/ w1 b5 V. g' d
启动Nginx
; G1 H/ `% \9 b1 _5 y& Y$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 ^9 j; k; Z! g( U" L  T  B8 \2 e
" G* D" Q$ m7 [2 `! B+ e2 R

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

( Y8 B' |$ H  x

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-4 01:20 , Processed in 0.083928 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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