找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12641|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。6 X! \: I6 y$ k4 b2 A  y1 Y

/ v( S3 _1 Y2 g2 e7 J& Z' b9 v. w3 H# E& w
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ S0 K* Y* @+ [, w

( z* I* ^0 A# k; t4 u: g7 m: A7 q/ p7 I; Q" d- L, l$ `
什么是ModSecurity
& U6 t7 S: o4 `" N5 g- S4 YModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, b( O; v- l% Z# |6 v9 `
2 o' L3 _3 r, P- y$ O  h' I) ^$ I! T
" r  h  T0 ^& m1 \: L* rModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# ~, B0 c. X- ?+ i: v# P3 d9 V4 ]9 }0 H3 B  P! \0 I! j/ }8 ]
" \; f3 Q$ A" v+ t& M! G
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。" I- Y1 J: d8 Y1 X7 s# H  X- L

) z, L) z+ W/ }  _1 m1 a2 m, A8 X* R- ^; G- J; V
官网: https://www.modsecurity.org/& I8 A% k) ]) L, p' T

5 @7 N+ f' [# H3 C; F
2 N4 z. C0 H& Z. R; Y什么是OWASP CRS
) I1 q2 B. K: N( `  kOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
2 T0 x: f) {2 `! s1 b6 }6 t! [7 d: o4 w1 S& t
  P  U' O. h3 c4 z% P9 r% [" O; H
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。! I( T- w/ R2 D. g; r# o/ L

& Q5 T- n% y3 ?# {" p8 e/ f8 h" P3 Z( ^* m$ H
HTTP Protection(HTTP防御)8 M: ?) _; @0 S0 @7 m
HTTP协议和本地定义使用的detectsviolations策略。$ |" }( f3 B4 I) V8 u- t+ N
" p8 S) m. |/ Y% x; R

+ ?& t& [$ j7 b% @/ B$ a( o( FReal-time Blacklist Lookups(实时黑名单查询)7 R! ?  h2 ^7 R  _& U
利用第三方IP名单。6 |6 ?4 z5 V9 h
+ X+ \5 z7 Z2 T* I

1 c, w7 W( ^& h+ S& LHTTP Denial of Service Protections(HTTP的拒绝服务保护)
- c  W+ F( f% g0 i% ]防御HTTP的洪水攻击和HTTP Dos攻击。
5 O8 f/ ?! e: u2 }
7 X1 z. H! H* |$ ^7 P& |8 z) P5 Q& e2 m* q4 h3 t1 B. H* `7 k) M# S
Common Web Attacks Protection(常见的Web攻击防护)
$ n. _& ^; A( H& S$ f检测常见的Web应用程序的安全攻击。
/ ]: Z+ U' B4 l7 }0 A- ]+ c( j; u$ t- c+ y! A8 K  k( _# G

* t3 y% c, c+ W% [3 |- }0 N* OAutomation Detection(自动化检测)$ P4 }5 c* B7 j
检测机器人,爬虫,扫描仪和其他表面恶意活动。3 P, B% N( d" Q% j

2 H5 k) |. c  M. E3 p6 {  z" w& S! J* }# A- I! {
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)2 A* I$ D+ _- K
检测通过Web应用程序上传的恶意文件。" q4 J5 a, o7 f. H3 m

7 P* r5 _( r8 X, {9 z  g4 y( D3 ?  O5 g- ]+ M+ F& G/ h
Tracking Sensitive Data(跟踪敏感数据)1 u* x  ~1 m! h0 C" O% Y7 W
信用卡通道的使用,并阻止泄漏。6 ~' T3 d2 L  H( G/ h% ^

* g5 ?0 j# T! }( |( y: s& p+ p4 ?
' d5 j. W  P( JTrojan Protection(木马防护)
1 u+ N1 q& W7 `5 U* j2 W检测访问木马。- G2 s, ~' e! F" ?! l. O) u
$ v; m( ?+ {# C3 S9 ?  k( d
/ }3 V" A8 _, \* i  {- q
Identification of Application Defects(应用程序缺陷的鉴定)
3 _8 u7 H4 \; X检测应用程序的错误配置警报。& ]8 ]% O2 w3 ~6 ^9 c

3 d3 G  S. G1 l/ m# o2 Z$ p$ C/ ~9 R& y( r" j
Error Detection and Hiding(错误检测和隐藏)6 m7 n+ x- y, |9 [3 G" N! ^+ c
检测伪装服务器发送错误消息。! y2 [/ }+ D* m6 H

# D- H8 m0 I& `* N( Q9 s' I8 [2 P9 U$ Z, `, y) J' [- l% ?8 y
安装ModSecurity. H; T) Q$ P9 y; a
软件基础环境准备6 {/ o. N/ o6 J( @
下载对应软件包* W' ?" s8 f! e' D/ i3 x
$ cd /root
4 e7 Z/ k, k2 L$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ u9 c: L- t  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
  W, f5 s8 s9 W6 n; O安装Nginx和ModSecurity依赖包
+ `. h6 o8 a7 J, S# u9 kCentos/RHEL' |3 @; n7 `, a( e) Z9 {2 G- K
1 H5 w$ K8 G2 ?" b* M; C8 S
) S* U8 h( a8 Z0 \/ i. _
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel2 Y- [0 d/ i$ _$ e+ Y
Ubuntu/Debian
% Z5 _% V1 ?9 W. ?) Y- V
# M/ _3 j$ J, M, I) r9 W: N9 H/ J) c* K, E! c8 n) C3 y! q: s7 y% s
$ 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
/ b6 h6 P: n$ [- z编译安装ModSecurity
6 ~, p: ~) A+ tNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
4 R* s. m0 @9 Q9 C" a- N% d( U9 R: t) |# _  N8 U

5 o9 s: r% ~3 w$ l5 C. Q方法一:编译为Nginx静态模块
) L( I5 C  q: l3 l  B; J9 Q0 _) s4 ]/ U9 Q1 p

. J( w1 [* f. X' n& V/ _% e编译为独立模块(modsecurity-2.9.1)
  T7 f6 K3 e+ @7 b% \( F% E6 \$ tar xzvf modsecurity-2.9.1.tar.gz
) J5 M6 M4 y3 s! {: w$ cd modsecurity-2.9.1/% d3 R% P! a: V
$ ./autogen.sh
1 `' k/ D7 R/ D& f6 Q: `( k$ ./configure --enable-standalone-module --disable-mlogc- Z3 s: r2 F8 m
$ make
: H  f5 e3 g# X4 U编译安装Nginx并添加ModSecurity模块
* p" F3 x% a  m- J2 q# _# L% f2 x( x5 z$ tar xzvf nginx-1.9.2.tar.gz
  T  u* h9 I8 Z5 _* |$ cd nginx-1.9.2
! f2 c5 K  W2 h# d6 n* o. _$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
* a" y( f6 o7 u  G0 p$ make && make install$ E7 `8 b2 P, M/ {
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块3 |4 [$ P9 l* ^  T

# {* c4 o+ b4 b" ?( ?. h3 _* m' D: Z) y
编译LibModSecurity(modsecurity-3.0)
8 Y' R# l$ X0 ]  K8 X$ cd /root5 S/ @4 `2 Q9 f. b3 x; O" b
$ git clone https://github.com/SpiderLabs/ModSecurity
9 O8 S* |5 Y( ?, w) z$ cd ModSecurity
& B$ L* J) }0 P7 Q* Z$ git checkout -b v3/master origin/v3/master
0 i( K! ]8 Z  N( l$ sh build.sh
5 m1 D5 w8 D5 g6 w* B8 G( {$ git submodule init
1 T# j3 Y, K8 o% X# d; W$ git submodule update3 @* t6 Q: b$ ~6 k% n1 J
$ ./configure
* P- O2 A6 o8 p0 c3 g3 M" m' z1 f! \1 i$ make
' m9 S6 n: l# z( t) ?2 l$ make install* n' i/ ]0 x* k4 r6 w
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
- W7 G; {2 n4 h0 D9 t
" o' N5 u2 s! k0 e
" o6 U) p$ w* E$ ls /usr/local/modsecurity/lib0 f3 [1 c) G. n# p% q, e, Z
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" s+ j" w( \9 {& q, A5 H
编译安装Nginx并添加ModSecurity-Nginx Connector模块0 c7 S& I% v" l% E; F  v5 \6 Y
使用ModSecurity-Nginx模块来连接LibModSecurity
" |& ^+ q9 ~+ @% p% x! d& I' X* L  y9 ^7 k! v4 t) h

  p% j% S6 c( T+ j. [" @3 o$ cd /root
) i5 ?9 D5 V. N$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
. A2 m0 I" n( q- |( t8 r/ i$ tar xzvf nginx-1.9.2.tar.gz: w7 k2 K& p3 E$ ~
$ cd nginx-1.9.2; V9 G2 J8 o" S1 f: m
$ ./configure --add-module=/root/modsecurity-nginx7 N/ I( L) s/ M6 f2 K
$ make
9 X( Q) v  N. C# @$ make && make install
- e7 d3 P/ ]2 `添加OWASP规则5 r' U/ ?" ~% K4 z" p2 `& W
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
/ }9 o+ ~& X6 h$ G  F- }/ k: T
  `2 a* ~% P  V5 c- i0 t- w5 }; Y+ \. n* E, E
下载OWASP规则并生成配置文件1 _8 m9 D- s7 R: c  p$ h" ?2 @
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
+ G: D/ L" o& c7 q$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf// g% U  B4 f8 E- [  g4 X
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) j6 n+ V7 x, y; a* \# `$ cp crs-setup.conf.example  crs-setup.conf
- T3 x2 k3 X2 D配置OWASP规则& m# m  m# ]' y/ D4 k3 c$ c
编辑crs-setup.conf文件/ U6 u( |' f8 V  V' X. R% ~

6 s2 `4 B) s+ n7 ?, P. O( _: P) |) A" a
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
, @, I0 G! b* r3 S# F+ o+ E+ m$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( f' R" W$ c/ _# }, s5 Y$ |$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
  W9 h. o7 G$ u3 F5 u8 Z8 y6 p$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf! {' R; i/ H- H* b
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。0 U5 k  S/ E$ J# Q" r/ `* R
, Y! L* c$ E5 r: E( l  q$ a( c) y
0 u& [& e7 N$ B
启用ModSecurity模块和CRS规则
* W  U5 U3 X* [  d/ E) i复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 t7 a: {- t% j6 ]& k+ s
2 N7 x# y& u) W4 Y$ ]  @: _+ [) V# N, y: m. D- h4 Q
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
- L8 E! w( s. ?) _0 O" T' X7 S/ [. v* B. r& v
  s8 s% m5 z  _# D) i, I) K( p  Y
$ cd /root/modsecurity-2.9.1/% v% W' O; p8 `$ d# \1 r
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  " F8 y8 L4 G9 b
$ cp unicode.mapping  /usr/local/nginx/conf/
% _+ j0 V0 w8 N( e将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
. X5 [1 }, j9 }+ K8 N! [5 O( ~" ?" W% T: A4 d
* l0 D9 H$ A+ p/ M9 D, H+ r8 k+ n
$ vim /usr/local/nginx/conf/modsecurity.conf
# [1 }$ _; y4 x+ OSecRuleEngine On' h4 D8 X4 f. v) E( g
ModSecurity中几个常用配置说明:
% ~: P+ e' k- D5 j: i" K9 {' Z( r% }: x

  G6 M4 Z2 o6 A7 X, n1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。8 R) ~) m  C% U4 g

  i* S3 x2 d2 S/ K% P# A
+ H  N4 n/ O$ n4 E  j0 h9 |2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。! H3 G/ h  D, S: ^: ]
) z; @3 Z( Q5 A1 }) U5 r
/ t2 _+ n; G8 G$ E) U( o
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。) D+ D$ b, [* A& k) {% L

, s% N) a" ^6 t! c3 F# V# B8 t7 k! z6 e6 a9 r/ ~
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# Z0 E. i* a* @5 x
9 C. a. G( Z3 C3 E3 J- ~0 H3 }& k2 A# v
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。7 S1 h( g+ G0 G- j5 V) `* p: T
, D% l! {+ q$ u. t
( J4 `8 J9 Q- B* }; C6 I
3.x版本CRS4 U6 b& I9 q( M; W$ P
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 M8 w# C! _0 |* K$ K  M3 }9 z, m
# 生成例外排除请求的配置文件
) M1 j& ~5 B  u  E$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf  o8 ]) E3 e" U' Q, Q8 O
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
1 e/ z2 y# l& m7 h  |$ cp rules/*.data /usr/local/nginx/conf
7 O3 y( V6 j, s# R, Z3 ]' `+ ~为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。! S9 @$ f+ R4 N4 j( @
/ l/ h( w4 g. }! M6 V  j
, C( N3 j, ]0 @( v/ o5 p
$ vim /usr/local/nginx/conf/modsec_includes.conf
* v! d2 {! v2 o6 k9 H8 M: y( V- \( y4 d; v6 d# P% f
[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
% ?! M5 a; S, C, {" z
" q; I: @6 {# f  @8 Q1 ]
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. y+ q9 z! r$ @$ S0 X
# ?" O3 f! T5 J) c
; \2 c/ [  J! Y& n
配置Nginx支持Modsecurity
1 ^1 a) f0 t" i8 H3 S. c: @启用Modsecurity/ `8 @/ p+ v9 Y( @' F2 h5 T
使用静态模块加载的配置方法6 j! H; v/ e- [1 K" ~1 y
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 H3 X( L" k) k

: b$ V. {0 Y! D: ?
. j; Q: r9 z3 Y/ L: OModSecurityEnabled on;! i: \$ @7 ~3 N- s. V( T
ModSecurityConfig modsec_includes.conf;
. |2 M  S9 o& n5 a0 ^+ [修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* P  M6 ~0 i* C0 N5 ?: x& @

5 q9 C% L' Z" X" j4 s( [6 F. X% j; |5 E! |4 n
$ vim /usr/local/nginx/conf/nginx.conf
" A  r4 ?+ d: v2 j$ _* j1 y  }/ H' d3 q1 q( I; C
( C! n/ \. y8 p, w
server {
. I7 t8 X4 }7 v9 Z' `2 |3 P  listen       80;" m* o9 `, F2 c  u" r' s3 j
  server_name  example.com;
# S4 T" h$ x1 j) G/ N! Y1 \, G0 q5 ?- Q2 T5 X, y. k4 Y: ^( R* y
' R3 _2 ~9 g! q) {4 Y- f. J
  location / {  v2 S, `3 f* m% \2 W" U& R: g
    ModSecurityEnabled on;5 o. f4 G$ X9 c7 s5 z) {- X% [
    ModSecurityConfig modsec_includes.conf;  o& \3 I6 I7 g  _  a1 L, f+ E
    root   html;/ ]; s+ Z9 ~0 Z! {1 {* L* w* _/ s' }
    index  index.html index.htm;5 w# C9 H- A3 U5 o* r5 X) F
  }
7 l! l* {' u: @  X+ ~}
% J% |, {; q( X- t2 {' b使用动态模块加载的配置方法% R- S; R) _9 o* W
在需要启用Modsecurity的主机的location下面加入下面两行即可:
- e8 t* C( I" w% ]8 t( h- m5 C, Q$ E/ G
! C$ c  v. H$ u
modsecurity on;
% U# W8 p& n% C: [) f/ [) Fmodsecurity_rules_file modsec_includes.conf;/ M& I0 L/ d2 C4 [
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
9 q: U' v% O: J" \1 B. Y& v3 \$ |( I* i$ K' {
1 ]! |1 n+ Y+ C3 |* Q' R! J, u
$ vim /usr/local/nginx/conf/nginx.conf2 \, A4 H# L3 T8 O

% y" w' v6 Z1 C+ i) }2 ?1 ^; }: r7 k  u3 ?
server {
  `' b: E. i" J5 e% h2 c3 i  listen  80;
' M7 J3 O" f( S+ t  server_name localhost mike.hi-linux.com;7 d* B6 E1 ]: T" U
  access_log /var/log/nginx/yourdomain.log;: o" q% ~: @0 T' W
' g/ _2 A; p4 b1 x! V/ D
: t: N7 K4 U! t6 o  ]: o: r
  location / {8 V' p) W2 R, S& {; _+ M) Y% b5 `

. ^! m) i0 N/ Q/ n: c6 l1 S7 w) @. X' |4 Q& `" r
  modsecurity on;
: B0 }1 Z. _( _  g6 r2 j  modsecurity_rules_file modsec_includes.conf;
* J% q' S" |4 D, ]& a; @4 V$ m% q0 l  root   html;
. y1 |. {: v- ^8 c5 L% J  index  index.html index.htm;$ q/ a! [- F4 E" X
}
% _/ {+ D+ s; A7 T: N" L}
9 U4 P' [* W5 G5 d  u验证Nginx配置文件0 u# e% S0 }1 O# {# g! g
$ /usr/local/nginx/sbin/nginx -t
. W% i' e, o' A( T( r9 N; }nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
! O, |& k7 _/ G* E# Ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
: O$ f3 C+ @; P启动Nginx
- L$ U8 H0 I5 z& L2 _. ~7 j8 W' d; h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf! L% B3 z6 U* \$ s

" j7 I; l, i/ e/ |$ S

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

3 l" U, W% b$ h) R0 D! w: t/ ?* k8 Y8 V

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-23 05:19 , Processed in 0.076462 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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