找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12668|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 `! Y2 }( ]# h& K% x& ~; L3 T
/ i: Z& B. {- D4 [9 d
8 o( }+ [0 ^& W0 K在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 S! t" Q  y) \8 l) Y; v4 P' J# n3 q0 j- Y# ]  z( X9 C

8 [, D1 ^- l* V. f8 t什么是ModSecurity
" g; U  S. T% M( x$ v$ {+ j# QModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。( M- S. t+ Q( [2 l& q8 V. g
% V8 H) }. b& E
' K# x, c& g7 t7 C7 o  q  f
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。  b7 L7 R# Z6 U3 f/ w6 @

5 P- o: U5 m; N0 B  @8 g4 W7 {
! {" b  T% o7 u" z, x8 M1 y0 s8 r9 lModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
8 B. F% A' D+ y
" G2 C, O9 T1 y3 Q" y3 y7 o$ g& c- W, K# r8 ?' e5 l4 s. U  l! ?* d7 q0 Y
官网: https://www.modsecurity.org/
" O8 x. g5 M; r' O# S- P, M
$ S. p% ]/ r5 ]9 U- W* F$ k! c4 L( a# \# w/ z( Z* o+ g
什么是OWASP CRS
$ [; |! Y8 ?$ [% [" y! T' mOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
. ]( B  m) ]; }& M3 k1 ?' h( j( p+ Q
0 }+ K8 f" M* {5 E3 e0 T: }  z
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。6 |8 j# f" V5 ~4 y( ^$ i9 w
; c6 s# ^3 z! W" M+ z

% c/ V( M8 e+ e+ I0 \* ^5 j; ?6 [HTTP Protection(HTTP防御)
3 x2 |/ T+ ?* j+ b( T# YHTTP协议和本地定义使用的detectsviolations策略。
6 u/ [. N: n, c9 P! o0 N$ B3 _) L
( c4 @2 p5 Y' ^# d6 Z1 X4 O9 j, `
; w5 r7 L2 Z! O5 jReal-time Blacklist Lookups(实时黑名单查询): ]. q9 f  @. A& @- f! v& l. A
利用第三方IP名单。! d% q5 }8 m9 ]) k$ q
% U( u) f* X7 r

% R* ^) S, e* B- pHTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 G  P* Z. c: e* K# e& u+ r防御HTTP的洪水攻击和HTTP Dos攻击。
1 x- r6 L% `! Q+ V! ^! B6 k
  N; c' _" a! }* {. A5 L0 U* i" s* x( X6 l4 J# @6 Y1 x
Common Web Attacks Protection(常见的Web攻击防护)
; K8 X/ ]4 }# P检测常见的Web应用程序的安全攻击。, V4 O/ s" m0 y7 P2 L

- g! I- b7 {/ m7 e4 C5 B7 R* p
- B. ]: W" V1 ?# |: fAutomation Detection(自动化检测)
2 `3 j% s. L# l  C! v& h检测机器人,爬虫,扫描仪和其他表面恶意活动。
/ |- n; g$ W$ F' Z  L6 O( x0 b) b- r0 m

) {  _- T7 Z/ r- PIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
) n  j' X  }. j, J- b$ R检测通过Web应用程序上传的恶意文件。, t  g6 Z8 ~1 |# E% x
- X; F& \, J5 {/ y3 g

* f; f3 c3 w% [! W3 hTracking Sensitive Data(跟踪敏感数据)
  c" Y0 D& n! V信用卡通道的使用,并阻止泄漏。' N$ @% b; T, T, @9 n# g

1 y7 f  z% H+ L6 {( X) L  d
4 N. G) C- p) x8 ^3 NTrojan Protection(木马防护)
* x, }9 r* E( i3 K+ r检测访问木马。
& j% b5 q) K- P+ ~+ }' ?; t
! X8 i0 W# D( i* a2 }" s$ Y8 D, {7 b" Q* J1 Z0 g$ W9 y$ s
Identification of Application Defects(应用程序缺陷的鉴定)
- L6 E; Y4 o2 h3 g. w3 p检测应用程序的错误配置警报。
4 E3 c, u, S7 w  J, l
4 x! a8 F8 d3 w* |$ r; @, m$ m/ p5 l& {- o7 X
Error Detection and Hiding(错误检测和隐藏)4 O, p7 Z. ~( g0 t- z
检测伪装服务器发送错误消息。! ?8 z( a4 B, @0 @! P! t2 s& Z
. J: P" _, T$ ]5 q# k
6 O& t; @' {* t
安装ModSecurity- {$ g2 s# P4 O/ L
软件基础环境准备) O1 n( B8 k8 s  M- @
下载对应软件包
1 @2 d0 S6 l% ^7 y2 ^& U$ cd /root$ `; [) {& M" N& s2 c; ]4 X
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
9 r6 I) a5 M" a. l$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' i, i! z; g! H) Z6 ~* X+ r# d) |
安装Nginx和ModSecurity依赖包
6 A8 b* O! T9 b2 Q* r4 D2 ?3 Q- C# ?; PCentos/RHEL3 e; N6 }+ z4 d* C' X0 w+ e0 \6 [
! Z5 c6 X, M1 d! {2 m
$ M2 b: w" ]9 k: Z- d" S
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% L5 U* U  }0 A1 g5 q" [+ z1 l$ u
Ubuntu/Debian- a8 W* ?1 p4 _$ ~6 y1 I

# H" T1 d' \7 X: _
' |+ {6 h- j5 M% V9 f# d/ 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++-dev/ W& m6 {! m5 b# {' [& b
编译安装ModSecurity/ i$ c4 @. s+ o% j& ?6 N+ |. u
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ l$ u% B7 }2 E3 U6 D5 \8 z- G. H! n# o

  R4 b. J1 s0 c方法一:编译为Nginx静态模块. {+ N0 v3 j9 k$ D

2 Q: _8 ]; o; N& r
% @' T5 h. m& A5 t编译为独立模块(modsecurity-2.9.1)) r+ }+ D% k! E; [0 v
$ tar xzvf modsecurity-2.9.1.tar.gz
& o9 T5 Z- J/ ~1 ]$ cd modsecurity-2.9.1/
4 Z9 X% U7 F* D/ J! t6 f( R$ ./autogen.sh/ }7 q) s) ~/ O, S
$ ./configure --enable-standalone-module --disable-mlogc
+ ~7 O  F6 ?* x# O$ make
% a; _1 R% _: u) F编译安装Nginx并添加ModSecurity模块" z: ]& d1 I) {7 u' h" q% u% ?
$ tar xzvf nginx-1.9.2.tar.gz0 j* Z+ ^9 K# ~; R0 Q& a5 r- Y
$ cd nginx-1.9.2- |! i9 ]. I, r5 R# C9 }* u
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
8 f' ]: ^! N' W5 p; F$ make && make install
! ~6 T9 j. F9 v方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
" q9 o" ~. I; q5 Y! t* K' N& `8 G8 v% N
' S4 U7 \7 u3 j4 O" o; F4 r0 ]
编译LibModSecurity(modsecurity-3.0). h8 P3 @+ g. X8 z& m3 m
$ cd /root
! i2 I$ X  v' j4 G3 H% y7 ]$ git clone https://github.com/SpiderLabs/ModSecurity
0 G. s+ G9 u1 [# ?$ cd ModSecurity
! q% N. |  \- z5 ^9 ^9 s$ git checkout -b v3/master origin/v3/master
, \! z4 }4 V# r$ x$ sh build.sh) H. l3 P9 y! X6 T9 f
$ git submodule init
1 {- ~. N, u# A6 ]" z; w% S$ git submodule update8 ^( C3 D' A$ c
$ ./configure
3 {3 {- M5 @; _+ A. z6 K( T. l$ make% w/ Q7 L& _' a2 |) C+ O& o5 a
$ make install
6 J1 g$ ~7 b5 G# i3 a# ]/ b0 m% cLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
. G: ?% f& I2 w9 a% A( ]
6 ~; q/ G8 ~0 o: Q% k) g; p" C: z, ^+ S1 y4 D
$ ls /usr/local/modsecurity/lib
$ K4 t( O, I5 }9 Vlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0+ k/ |( ~% }" w9 H; z' m( w
编译安装Nginx并添加ModSecurity-Nginx Connector模块- A2 ]# \- Y& L/ o: x0 x4 @4 S
使用ModSecurity-Nginx模块来连接LibModSecurity9 |7 `! t) R. N
, f9 v8 G7 ]5 V

1 j  u# j/ F7 g$ cd /root
- x7 c: O; f) x: @$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx3 D+ J; c& u; y+ Y- s
$ tar xzvf nginx-1.9.2.tar.gz
0 x. g* Q$ b5 X; N$ cd nginx-1.9.2
' ^1 d1 ?7 f/ r7 @4 K$ ./configure --add-module=/root/modsecurity-nginx+ X4 h' x* ~0 R2 t
$ make% j( h' h) l6 X6 s4 o. U/ Y
$ make && make install
: x9 A& A' l7 \3 @  w2 t4 A; @添加OWASP规则$ O! E& N! u/ J% {  }5 h. L
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。# E, s" }* x8 M/ L9 P; H& `9 Q  `
* b8 l6 Z7 U, h6 o. n+ z( B
: T( L4 ~8 _/ @% H& d" G4 n
下载OWASP规则并生成配置文件) Q; u8 y1 A# Y6 e5 ?, h
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
4 `# M3 ~) U7 N8 U! ^$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/3 }4 {7 G8 E( s4 J4 Q
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs& H2 C9 D' B4 S0 _
$ cp crs-setup.conf.example  crs-setup.conf
4 u( u7 v3 ]' ?& q! N& }$ ]配置OWASP规则5 S# e. i$ ]' r# n$ {# g6 b4 I
编辑crs-setup.conf文件7 m0 C/ ~. Z- [# ?0 `

3 d" @, x: `4 K! m; H( }9 Z& e) J3 H
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ _6 J3 ?8 [# n  C$ _- Y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
4 B* ]3 a  L1 Z3 E! Q0 f+ }$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
& ?8 k! E: o% a& T$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 A/ u$ F: A7 v1 |默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。- j- c* B4 o" g1 \/ t" E% ?9 w8 O

6 Y. q% @, H$ B; B$ r5 C* ]1 {
启用ModSecurity模块和CRS规则* F. M3 ]1 {3 A! m  i  j8 p) P
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- E5 e# b$ @* x9 J. O$ ?# o! p* r: p; ^6 t! }, N9 _' v
: k2 h5 v: c3 l. ]; T
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
- O3 Q( h, M1 D* `: ^8 H. l# N: S2 v. P8 e
7 ]$ L9 m9 l; P& g) a$ g
$ cd /root/modsecurity-2.9.1/
9 \3 B5 C4 f7 `* [$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  % `/ P& F) g; a6 }7 I9 J* O) i
$ cp unicode.mapping  /usr/local/nginx/conf/
$ a* S" ^, X; r; k' ]& s( L7 J将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' k5 s: J6 l* H6 P3 c; W* }
( S6 d+ z- i% \6 F: I2 }# p) w0 d

, ^; h. n/ |" z' G! ~( B/ l$ vim /usr/local/nginx/conf/modsecurity.conf5 ~5 X% x. k  A# x2 t
SecRuleEngine On1 R4 f- I& `  K' g3 M9 Q1 C
ModSecurity中几个常用配置说明:
! ?. \$ K7 o9 h5 J$ G% b; K
0 T8 X+ N, C3 g( a4 v& ]* H7 Q0 k% H' o/ L9 y- T% f" B
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ l+ g. J  @7 K( L/ O
4 `9 b# x$ ?5 i1 P
; q( k; Q* E: f2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 W: z- n+ ?8 P
/ D3 F2 j/ A2 d- m8 _$ g$ T5 j4 A( q; J( j: x# t& D& b; U% B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。2 ^0 a7 q& E% ]2 M/ y
( F' D+ L( ]6 K
. W4 c5 D* @, C; _) R
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" |$ I8 S6 q& a, W/ v* q

, v- m- u9 w% g. d: i
" X; u  N  }( m1 m$ _8 ^, ?& J在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
2 z0 b3 u( s' ]* M9 t
. F, B" ?- \% P" L3 g% Y& h  S
3.x版本CRS& O4 _2 O1 B; J; K. ^- J; r
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs# j' o6 G; x/ t' p
# 生成例外排除请求的配置文件
' `* ^$ r+ J* ]6 J. w% h$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
8 [2 [6 ?$ z6 m9 K3 H$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
, N% N/ Q+ P/ m; o' e) O. |$ cp rules/*.data /usr/local/nginx/conf" [* d" V1 q" A- U  `$ [! X% g
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 k& ?4 z+ Z) X' f0 n9 l
7 b. J: r: P1 f: m, a. a* `* |
+ L8 l2 ^2 b4 \% h. o
$ vim /usr/local/nginx/conf/modsec_includes.conf! b: d/ t8 a- d- Y6 K+ U6 L3 L
1 p8 [7 z: ^$ A" g
[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

! G; ?( E& j: A1 J, b
( ~) }$ H% {1 B7 v. y注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
/ Q7 N3 g+ U7 b0 d% r3 i: a* U) H$ o9 z0 F
" k8 W6 s4 ?7 w7 i' F9 w$ H
配置Nginx支持Modsecurity
0 |4 I+ R1 I+ ?启用Modsecurity
3 G  q, ^2 X0 [. z& T% N( t使用静态模块加载的配置方法+ p, G$ I0 b5 }2 D
在需要启用Modsecurity的主机的location下面加入下面两行即可:% i  A+ \% c) A( ?7 d1 c
" Y" ]% o! c- X- g; j% M
9 r" J" c: H$ u% o$ l8 @
ModSecurityEnabled on;
' d) h% t( r$ {; WModSecurityConfig modsec_includes.conf;  S9 u2 P6 Y: E4 K" R$ ]
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ Y# L* s  y) Q( E* o! \

9 s. {1 V- v% P. L; a$ ?9 _" P% ?
$ vim /usr/local/nginx/conf/nginx.conf( ?/ O* M* b6 l6 u  w' a0 m! y

9 x; ?2 {7 p- m. e3 K2 o
% ], r0 v; [( T  fserver {/ w: i" U4 i1 X2 E
  listen       80;( Q- y; Y5 B# O  O  u
  server_name  example.com;$ O0 A0 F5 R# S) g/ |
2 e1 h* \, ~3 {6 R
& {/ @+ z+ S. ]! S  ^" h# a
  location / {
1 n9 w6 y1 y2 S9 b1 O    ModSecurityEnabled on;
, v+ x/ R* m8 |3 F3 K9 r) e    ModSecurityConfig modsec_includes.conf;
4 ^8 u; ?3 ?$ v' x    root   html;3 V) y  I8 u# g+ e: V
    index  index.html index.htm;
5 d+ W6 P1 ]3 R  }
) z, R( H# H- G9 v; {}) `7 ]8 E5 A3 o3 Q8 H
使用动态模块加载的配置方法
, o) J4 w" B% E6 Q1 m在需要启用Modsecurity的主机的location下面加入下面两行即可:+ I4 }9 u. [5 ]! e+ o! M3 y
# t3 n% \  U# e8 u# V

8 R% i4 Z" m+ {modsecurity on;
/ @  P; F, B7 h  Q9 |, h8 E( M* Amodsecurity_rules_file modsec_includes.conf;
- N) H9 j  R9 D% H* ]修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, h2 C& D% O1 s: I. O
2 _* @* s3 [* m8 h6 E% J+ E
1 Z( Q. s" ^. t
$ vim /usr/local/nginx/conf/nginx.conf
1 ]/ b! o+ I( W# Y  T( R/ P: c3 ~  ]: \+ a/ E1 l7 \
; o1 Z! {& Z3 s% c! _
server {
2 ^# Z9 o* c, c6 A& `1 N2 J/ M  listen  80;4 N' i1 x9 k3 |: z
  server_name localhost mike.hi-linux.com;" [. m! w4 u- P- A- X; a
  access_log /var/log/nginx/yourdomain.log;$ d) r' J( K: Z, O+ W) V- w

7 H8 n+ z9 T  W% {. R% z
0 {+ p6 U4 Y1 N  Q: i9 k  location / {
0 Q6 {. S7 Q. G: T& y3 o( o: @8 W, a% ~- k
5 Y) ]; }; q- i6 i' g; q
  modsecurity on;: u6 b3 W0 o  H6 h3 D
  modsecurity_rules_file modsec_includes.conf;
. U/ a2 P; d2 t* r. x  root   html;
3 F5 r7 B4 }( V  index  index.html index.htm;
7 D, ~$ g9 {% _" d1 |}
: z3 k' `- F& H8 j3 {9 w1 F, E. C+ O}
* w1 `, L7 f+ \. J: v验证Nginx配置文件+ Z) N0 U5 L4 j
$ /usr/local/nginx/sbin/nginx -t+ S% p+ T0 O- u- i/ C  Z. y
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok2 f8 c' K) g  b$ k5 o) ~* n4 b
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful. U  P  r  j8 T; V5 D/ _
启动Nginx: G* W2 ~: F. {) r4 _6 Q
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
0 g" S- U1 M1 D& E% t, ]* y0 C) _3 I: ~! X: _5 l- W

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

, ?( d3 i2 m! G& j' Y" q: L

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-27 10:34 , Processed in 0.102069 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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