找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10952|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。( R* K0 u1 b& s  u' ~" P) r

+ }8 E+ b' S) \) ]8 J
8 I4 t, g/ S; e: B4 J6 O在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
2 p6 T& E  z) x  m
, `6 H8 b! E" @8 r9 E9 @$ k4 n0 D+ t4 ?% Q
什么是ModSecurity
2 E& J0 }% B# ^& w; Y9 o% p1 qModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, S# P5 h; Q6 _! J, D- j  E! ^* b0 X. u3 e
+ |9 J- D8 s1 `7 Q; X" g
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。7 R: N2 {, v7 \# y) z8 o1 P

' P) q" d3 m" Q! x' w4 k* k# E
* `  y) c+ l5 B- i# F* b. jModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。: y9 ?% l5 Q: Q" I( x9 O% j

" u4 {5 c: A  }6 }: N" E1 R+ b1 k, N7 P
官网: https://www.modsecurity.org/
- v1 F0 H4 V& N
5 j2 {8 n6 t( O- j; H  r0 ?5 `4 n& [, U  J( ^
什么是OWASP CRS+ G" F/ r6 u4 m& s3 E
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  O. z5 s6 A) f; w* i6 A  E7 E4 H5 d& J& j$ o

. A9 f; A, o0 f9 \ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
. t) _/ Q! z4 w5 S2 s+ M- K6 d# L
: M8 W) U; P. t5 L4 d6 H8 R% ^* |) H$ y2 D) {& r& k, a
HTTP Protection(HTTP防御)5 i) X5 ]0 g* L7 @+ @8 o8 p
HTTP协议和本地定义使用的detectsviolations策略。9 ?4 z. `' T& B* Y* w& S
, r5 D5 B- ^; b; k
+ o1 k* @- \' D+ o
Real-time Blacklist Lookups(实时黑名单查询)
2 u& F1 T1 A  |( \利用第三方IP名单。
% h1 B( ~+ w/ g3 I0 l! k4 _
  L. W  l7 H' }1 x6 _% \/ j  P& x- n& Z3 }: ~
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
  v. v% h- a1 r8 t1 V' F防御HTTP的洪水攻击和HTTP Dos攻击。% d5 L9 h' l3 X% t1 z1 P
) x# q' M# s4 R! {2 x( a" Z' B
; l( p4 a2 x# y& w6 Q% E1 X5 z
Common Web Attacks Protection(常见的Web攻击防护)
" W! a. N1 Z- D/ C" Z9 g8 r! J! z# B4 t检测常见的Web应用程序的安全攻击。" m/ m6 G. F$ T) |4 Q4 J

9 |, _8 N; Z3 O7 Z% L4 u4 j1 o
3 a1 l2 M; @  o* [Automation Detection(自动化检测)
/ Q2 U. z6 g' K# N$ f; b$ w; G检测机器人,爬虫,扫描仪和其他表面恶意活动。# _0 c; P# d8 F$ g: t; g
0 _6 Q0 h1 y" L  w2 O5 g7 y
1 x# N1 q5 m& ~! }" ^. N: _
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
" }2 R) D( I9 f  E. F1 O1 _检测通过Web应用程序上传的恶意文件。
  O8 [: M" G9 x: g" t: D2 {, q, r! r& M$ k! Q; x
6 ?% W3 J5 V  x/ \& M3 w
Tracking Sensitive Data(跟踪敏感数据)
. N5 y; }4 ~; l信用卡通道的使用,并阻止泄漏。
, b% i  F0 B, n- r& J/ y, J7 i
) V; ^2 E' {" y1 h; [, O* t% Z8 B" P. ~) s+ A8 ^* ~" a$ K0 o: m
Trojan Protection(木马防护)8 l( b, F# Z- c# B& T! b0 v' _# @
检测访问木马。
  D4 f3 z4 J# p5 B* W4 p7 e  Q
( Y6 m! z; T3 Q) d
2 R; J, P( t7 F( aIdentification of Application Defects(应用程序缺陷的鉴定)
& H) H0 F7 h6 M, i, B" B检测应用程序的错误配置警报。1 i0 h1 m( t) a6 }: @& Q+ Z

  e/ q$ O# S" J. `. _
3 u) I/ c* O7 H* `- v/ O9 d; QError Detection and Hiding(错误检测和隐藏)0 j% q( B/ ^0 A6 J, P
检测伪装服务器发送错误消息。+ a% G+ [9 Q$ c+ g, b5 a/ ]( X" m0 P

! q# @/ q9 u4 K4 a! V3 d5 f" S2 d  e- m% N4 B
安装ModSecurity
2 |0 G6 o6 B# T* k  E0 R0 ~: b# P软件基础环境准备
! ?5 |6 T- ]9 r下载对应软件包+ t% q8 _. a5 f% ^4 H9 ]# r
$ cd /root
# j$ _( D2 M- H3 L. C% F- m$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'  }7 O  u. V6 E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
" Y' K2 N1 P- b: Y" j5 ]9 h安装Nginx和ModSecurity依赖包
0 S2 Z0 D$ R# D) [' YCentos/RHEL; V! Z! {4 L$ g8 E. e% b

$ l4 I( \5 M. p' }
- w0 Y8 S# p9 E6 j$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel. B9 V' X) _6 O/ I9 R0 Q
Ubuntu/Debian1 l" I) K  Y5 A% d, @# ]2 R

9 e/ E  M( t" }# Y" P2 I. i' Q4 t9 ?9 d6 J
$ 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
. I& R) I5 G5 w3 ^) t编译安装ModSecurity2 L! H+ n2 c& L! g) Q
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ D5 y$ g- S. b& C# y
, B" o% ^8 m$ }5 ^2 \8 y" N( o# h. o
方法一:编译为Nginx静态模块
- T8 j. I' ]4 X. q* Z3 g2 C' d/ z+ t) i: j

" |2 I: M$ I" R! _编译为独立模块(modsecurity-2.9.1)( F. m3 e; N8 j+ `5 I4 ]- ?2 ]
$ tar xzvf modsecurity-2.9.1.tar.gz
, [- y/ _; ~* a$ ?1 C) N. a$ cd modsecurity-2.9.1/
  O& m& y) y, q4 B6 r* X1 z0 q$ ./autogen.sh, N. j+ \: c- P# B, J
$ ./configure --enable-standalone-module --disable-mlogc
% ~) f5 q, R1 k+ O, m3 k/ Z$ make6 V: S+ C; Q& I6 S0 Q
编译安装Nginx并添加ModSecurity模块
& O5 X; p, E. z: n$ tar xzvf nginx-1.9.2.tar.gz
2 |  I+ L- O1 M% S7 \$ cd nginx-1.9.2; n  s% ~8 |% h
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
% f2 y9 S$ _- V( E$ make && make install- ?% ?5 [' j& m
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- k* c! T- D) Y( |, C/ r5 d2 ~- p( z

6 W$ ^" ]& w9 ^5 s编译LibModSecurity(modsecurity-3.0)5 i2 z- v! I' B, u0 j
$ cd /root6 l6 s) x+ U+ \" T7 h$ C
$ git clone https://github.com/SpiderLabs/ModSecurity
# ]3 K1 q% {7 l" X; P& F$ cd ModSecurity
. L5 T& c4 p* p- g0 C0 p$ git checkout -b v3/master origin/v3/master# b' s0 N2 q+ m- Z% I7 d
$ sh build.sh
- [( b2 M4 R9 s1 q- v$ z# G$ git submodule init( }; {, q+ J# B) m1 G9 I' `
$ git submodule update& [: B  n3 A7 K5 S
$ ./configure
3 V5 Z+ c- j. p' [! y! m$ make: Q1 ]2 C3 Q- q5 g( ]
$ make install- A! V- e7 V: x: C
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。. F* E+ @. x- M, e' R
( Q' R# F$ @7 O2 B
% {: ?0 K" N- }7 [8 ^; S
$ ls /usr/local/modsecurity/lib
4 ^9 _( E8 r8 I  N0 c- Q; y/ I  flibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 j9 g: J, }  Q4 n- q
编译安装Nginx并添加ModSecurity-Nginx Connector模块& ^; }) Z( U' c* U0 [, C) C$ ~
使用ModSecurity-Nginx模块来连接LibModSecurity
) H- J8 z9 O9 |7 S* A; k' R, f) }4 [! u4 B4 {
5 l3 Q1 Z9 c5 H5 V  g7 m5 S
$ cd /root. `% H- z% l6 L. z" o
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx* e/ @" E! p7 |  |$ n5 H
$ tar xzvf nginx-1.9.2.tar.gz) ^* f: x+ S7 O1 I
$ cd nginx-1.9.2
9 }( v8 `. q$ [/ R% X$ ./configure --add-module=/root/modsecurity-nginx
7 l; \; W; R* L4 {$ make7 G( Q9 v/ K3 j! `
$ make && make install& z& T/ @0 }" b- y+ R
添加OWASP规则; }, K2 M4 P9 [1 f' [# d
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
, l) V$ j* r/ N7 @/ {4 t$ S) D2 r# @7 ~5 l; \

: v7 h' h4 t7 F6 o下载OWASP规则并生成配置文件
$ j' d! O1 Z( W2 f  l8 |$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git. a; z% i0 c/ N0 P
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/8 d7 f" h6 L7 R- S+ ]% {+ c
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) o; ~& l/ S; X2 A& J# h$ cp crs-setup.conf.example  crs-setup.conf1 j& d! s7 g% {( {
配置OWASP规则/ u" c* J( v$ S2 Q
编辑crs-setup.conf文件; Z8 V4 R  E( i+ V* {
- Y! H. K0 d4 d5 f

/ }( O# c3 |& d' x+ y8 m, ^' L$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
( c, v4 L: a2 d2 ?$ r2 }$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
- Y) z7 f# U9 A$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
: d3 l4 j; ]# U$ p2 }- r$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 ~. J+ D- y, k0 A# Y默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。( b; r3 u+ R/ n

3 I6 z6 u0 w' Z: G/ X+ n7 n2 ?: }; O$ H( S% U0 |' x4 Z, Z& M
启用ModSecurity模块和CRS规则
4 J0 Y8 [: I  h" E复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。- S% q- x# y$ d' _2 k; ^; o, ^) a- l
3 W* P" e* z  i/ h) c( `
9 u3 F5 E. d7 U2 r# _
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
+ l: S/ n$ N2 a! Z) s) ]/ o
7 i' S! @5 z  _, J3 f
. n- j! A2 U8 v1 L$ cd /root/modsecurity-2.9.1/
/ T+ }$ f2 t, k, T, E) \/ e6 T$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
5 |4 m4 \" W- c$ cp unicode.mapping  /usr/local/nginx/conf/
* k, N8 C  }. z& E3 R将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。9 i( y1 S8 D( u- V) N# K' Z9 N
! e! U6 Q' m3 T9 U) d
3 d2 [4 Z2 u; c, U) o
$ vim /usr/local/nginx/conf/modsecurity.conf
! S! H) m- e8 a4 O9 Y2 a$ R" xSecRuleEngine On
4 V/ M- I. K* a8 M2 M$ OModSecurity中几个常用配置说明:! x% F; Y0 A( p) W
9 }$ r  \3 z" w9 ?/ o& y3 Z7 R
* v3 U: [3 O  ?1 e3 f
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, K4 M; s( K5 o) j5 E8 p$ ^$ O
# M( Y( H  l7 G4 j  a3 a
, C) h9 v5 a& K3 ?% d
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。4 U, i- u7 l# ]. G' M

$ T: k0 A$ |: n) |# ]
* w6 U( C/ Q  |0 P5 q( m& w5 G" I3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
* a! o7 r) y  f$ o( }' I+ y3 R% r& u6 l9 R( I0 B, B% s

9 M, Z. z& O1 \8 v: C8 q# l! O3 \% D" K4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
9 e) h+ d  g0 x
* N0 a! a! H5 J3 c) r2 m! x1 ~: N  |& f% S# [$ J
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。$ z( t: [" A0 l9 B; B$ m

# N! g5 ?% f% X  b+ @7 w( \7 B( U/ [: q* L, g! H9 v4 P
3.x版本CRS9 |, W9 y! t' s/ g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ ~# f+ X; Z9 u
# 生成例外排除请求的配置文件
$ K5 X) i3 n: X& H- W$ \$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
& f1 w7 y) z6 {$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf7 D% Q: A# O  }+ P- s/ R
$ cp rules/*.data /usr/local/nginx/conf
6 T1 ^4 }. c  q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ F2 H/ m5 }4 h6 K, H
8 B0 o1 y) N1 Z! Z# ~/ I& W
( R( K4 K$ \6 Y
$ vim /usr/local/nginx/conf/modsec_includes.conf. q8 F9 @! i/ G& ^3 u; X
  w. E: {! M  o: I- e2 g% J9 e  @
[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 ^: ?: |- Y/ m

. C* X8 u+ a/ l  J" Y! L- ~注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
8 K- a( N, o& V& M! w" T; K
$ \7 y3 I4 G/ d' D7 J
$ l5 v' _: {. R5 L  B配置Nginx支持Modsecurity( i+ _2 l$ z  m- }" r
启用Modsecurity7 E3 ~/ K$ T2 \% N' z
使用静态模块加载的配置方法
' ?8 I  C! \$ Z( P* d在需要启用Modsecurity的主机的location下面加入下面两行即可:
5 @! I) z7 ?# m; Y8 [( m+ B
- Q" f  ]% x! G4 Q' r
4 p" F' ]+ h' }$ V5 T0 L5 UModSecurityEnabled on;
; M, f) ^$ I0 M' O; kModSecurityConfig modsec_includes.conf;
0 _0 Y$ k5 ]! K2 X6 m3 `  b6 _& l修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
4 J% N/ N0 S0 _9 N8 C# K
9 f& {/ G& }" {( H. l$ Z1 M. @0 A' r
$ vim /usr/local/nginx/conf/nginx.conf
9 N' ?# w1 y1 D4 O' r4 U9 f
7 Q! r9 l& A+ H6 W5 B. `8 o3 Q& ~, C; R2 d) R( [8 Z# f
server {
# S! F) A! g$ b; \% l7 A+ T  listen       80;
, f0 l. U  H: `: m  server_name  example.com;
+ N7 p* ?2 R7 F! c2 E0 g  h/ t
1 G( A% _: w3 {% ~  y1 _! b; c; U+ D& S/ l5 z$ V5 A/ J9 {
  location / {
2 g* X& h1 `6 X    ModSecurityEnabled on;
6 e! z3 p+ \+ z0 W  X& k+ u    ModSecurityConfig modsec_includes.conf;$ `( P8 H* U$ O6 |) o% ^4 P0 \0 g
    root   html;
1 U/ J7 c2 l1 M- u    index  index.html index.htm;- H% `0 l: ^0 P) i: Y
  }
2 A" e3 O: n. ^, j9 T}
- |5 ]* P/ n! S使用动态模块加载的配置方法
4 b3 L1 p- v+ s5 t在需要启用Modsecurity的主机的location下面加入下面两行即可:) w, f! G" ?4 n  e  W

# h) J6 n- N  g: a) q
; ~- b$ |! |: J3 |2 M$ amodsecurity on;
& a5 Y2 _' x% E/ kmodsecurity_rules_file modsec_includes.conf;" E1 I: R2 ~; n- _  L, {
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ l4 q: ?- B# T: l: S1 W4 e; _! d/ \, c& o5 y

! ^# m+ `5 K4 f! r$ vim /usr/local/nginx/conf/nginx.conf
3 ~2 z* y# `& u! B; [! ?
8 V0 y2 g& z$ E3 }6 _3 m* i# b8 R! K: ?: g7 G2 B3 c: G& Q/ @- g
server {
3 Z# q0 x" h+ M4 b3 A% L; g  listen  80;$ v) v: q  ]: }) J3 I" H! X. {
  server_name localhost mike.hi-linux.com;
$ S" L) l7 {% K: t& h9 A% |+ B  access_log /var/log/nginx/yourdomain.log;
& c7 y" {4 a1 f8 Z# p+ P
! b' L6 p- m( e6 g/ x/ O- D
- K) ]9 _' [* A' q2 G+ E  location / {" |" j: n0 y7 v0 H, X5 ^

5 c8 z, }; t2 a8 Z3 r
, k1 L6 j& r; Q) f3 V+ V  modsecurity on;
; ?! {1 W, @# Z5 W+ _5 M- m5 f  J/ H. @  modsecurity_rules_file modsec_includes.conf;
1 {" W/ ]% o: {" Q  root   html;! ]$ q) b8 J. f( Q
  index  index.html index.htm;
0 Z( ]8 n: ?; v1 c}( A* G2 V* J* S. [& A
}
1 `/ J$ {" K4 M8 n验证Nginx配置文件$ S1 e8 x, o4 g4 ?
$ /usr/local/nginx/sbin/nginx -t, y9 U+ Q/ X; q7 l8 ?7 \+ |
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ ~  @8 d* w) @
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
! D$ m" K6 V9 d2 `: L4 v启动Nginx
& \" _% j2 i+ v4 _) d$ f. e$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
, k/ ^, l3 C2 N3 q$ L2 T, \
  e% P" O  y( i  m2 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 [! p9 ^# d& n

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-27 06:50 , Processed in 0.073376 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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