找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9833|回复: 0

利用ModSecurity在Nginx上构建WAF

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

4 N( L- U3 A5 b0 D5 c在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
1 |  W9 k9 t1 E
5 r* h. O6 x9 ~$ i4 t1 h7 B# f% C* ]4 h
什么是ModSecurity
/ r* g+ s4 x. @  Y: xModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
3 Q; R3 v/ ]5 x- b- h* p
8 q8 U( Q; K2 |% Q! O% Z+ S# F) n" Y
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 m( k  ?9 ^  f( \5 }

* E* {1 p) S: V3 b' P3 B
* `8 a$ v) x" p  G# v# g2 F( J  wModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。) p! r9 K' x* f9 I' U
& J, |$ b& c+ o3 S$ e* P; |! {
4 l% K- ], ]8 x5 E; N+ U9 A
官网: https://www.modsecurity.org/
. j) y6 V. V& A% D' _  M5 y% ?8 _! M! }% ]
4 l. P6 i3 u) k
什么是OWASP CRS/ @: K6 z- Z0 C! m# }
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。6 \0 S2 I6 Q% Z" A6 ]6 E1 c

: b1 g2 S7 s( q$ {* F, e' J3 C& R6 t6 @- Z) r; m
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ I( M1 r( _1 X2 L  C; O
& l1 {0 y& ~2 U0 J7 a
% h. L+ @# O' w5 }/ A0 XHTTP Protection(HTTP防御)% f1 m1 {( @7 O5 `7 y
HTTP协议和本地定义使用的detectsviolations策略。
9 s9 }. Z4 _8 t* M4 m' h6 i
* F, R" R- T- c; a- x$ V! D1 |# Y+ E$ a6 D/ G; y
Real-time Blacklist Lookups(实时黑名单查询)
! c: X8 F+ x$ |# B; ^2 g1 c. U利用第三方IP名单。# N0 a  V7 L3 b) f

4 ]# A$ H" A7 M
" q  h. n& R5 U* K: UHTTP Denial of Service Protections(HTTP的拒绝服务保护)
+ l5 s6 `' {* Q+ ]/ K防御HTTP的洪水攻击和HTTP Dos攻击。. k3 r/ _" ~2 n0 D# _; B" G5 \! W" z, e
0 {7 ~9 }" \% A5 C1 ~& j

& o$ z) X0 q- C: T* C% w) kCommon Web Attacks Protection(常见的Web攻击防护)
8 ]" B: S4 i( X# I检测常见的Web应用程序的安全攻击。& ?0 L7 B2 d6 a0 A( Y1 }( o
( T" M4 ^+ f, @
0 o: ?" `$ o% {* J9 u
Automation Detection(自动化检测)
, E! e- \' {  D" T检测机器人,爬虫,扫描仪和其他表面恶意活动。
! x$ ]7 U% _! o1 x( h
6 v( s' c( v6 X, T
! A8 p$ r' {: p" [* D. `1 e$ gIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ r- P- M6 H( y* H/ l6 R1 l
检测通过Web应用程序上传的恶意文件。# Y7 `1 T, i. O  X
5 O2 V  h, h$ E
- X$ x- j5 z% N( _- Q
Tracking Sensitive Data(跟踪敏感数据)
; K2 V/ q3 a. l4 x) g2 ^( G! z) }信用卡通道的使用,并阻止泄漏。
2 X- \* W' ?9 q+ T0 Y. y: v3 F: w6 C( w2 P6 d( Z, h

% v, S- g" B* G; ]) l/ J1 RTrojan Protection(木马防护)0 h5 h% n- |" O" K2 t7 B, s
检测访问木马。
7 ^' @  c& c. e
/ Y3 D, |& ~  r7 W: I: [4 u- N6 z. ^, [& ~) b
Identification of Application Defects(应用程序缺陷的鉴定)
0 ?: u* q* q0 I: d" w; j检测应用程序的错误配置警报。* b1 o5 x" W7 Y" w$ ^! ]+ y. l
) p$ m2 e! P- g* M8 O; U

7 k8 @. G& t' lError Detection and Hiding(错误检测和隐藏)
5 ?3 w( N* C( O  ?% h4 v, }检测伪装服务器发送错误消息。
3 n' a1 W$ F5 {) ^
4 y4 m- u( Y' G( M% ^! F, P
5 `3 \5 ~7 ?& c8 ~- C, A3 C安装ModSecurity; ~0 i" Q6 v4 E% {( U$ f0 v
软件基础环境准备/ _/ p, ^& s6 ~8 C6 {
下载对应软件包5 H3 q& a( {# C- a
$ cd /root
$ _! J6 ~- J7 I: Y" M5 Y$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'+ J' K& G) c5 P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz8 k' P  F1 j  w
安装Nginx和ModSecurity依赖包& x) E: X8 l2 q6 h, _5 J9 D1 _; E. e
Centos/RHEL
: t, |! K0 P! m7 }) @  N- _6 Z- R. W) Q' {& P2 q3 G' Z( D( ]3 v, R
) ~0 C) j$ ]9 q1 ]" j# C5 k
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel& }$ H& F( L6 ^/ Y* o( b/ o& X5 i
Ubuntu/Debian: q. L: a& |5 F$ c
6 G3 i2 h8 h1 G/ G! [6 h  Z

% D, a1 }& {, Z1 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
7 w  @5 o2 x' _* ]! w编译安装ModSecurity5 ~$ O9 j7 z) O% R( {
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。! C7 ]- [: d. l7 l! C, o6 \* o

2 `% b- W% y4 f* X- J9 P/ {; c7 J/ n$ t- H
方法一:编译为Nginx静态模块! q* o; A! X4 D; G( @! v
& d5 b/ V% c& t; V/ j! N, Q& ?

. Q/ {4 E+ W6 `# n编译为独立模块(modsecurity-2.9.1)" y" a. u  x+ s  m
$ tar xzvf modsecurity-2.9.1.tar.gz6 }% `' @$ g' v
$ cd modsecurity-2.9.1/
* E  D" R) r. e+ F$ O$ ./autogen.sh
8 M) [( |/ M2 i. H5 l- Y' A2 o$ ./configure --enable-standalone-module --disable-mlogc1 y0 l7 m, w& P/ L: ~3 x; ]- I% d
$ make: \% C% v' w4 |& F" D  s7 R
编译安装Nginx并添加ModSecurity模块  d2 f8 A  x+ }: o% s1 F7 G
$ tar xzvf nginx-1.9.2.tar.gz
1 k- m  Q4 w6 S% F$ cd nginx-1.9.2! e0 ~* H) V$ F" r
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/; ]7 u- H3 D$ x
$ make && make install
2 m4 A: K" u# t' K5 R8 L. V方法二:编译通过ModSecurity-Nginx Connector加载的动态模块3 {, A* r; I; i5 ]
: ~, s1 P. r# \$ Y- d! T) a

; Z; p. P1 ]: m* p编译LibModSecurity(modsecurity-3.0)
( F9 O! m7 @, @4 B- f/ l) k$ cd /root
( m) n  M3 e/ o# O$ o$ git clone https://github.com/SpiderLabs/ModSecurity. G9 H+ {' T  R# O. I- o( y) F
$ cd ModSecurity
6 I: j. U' N! U( H- n+ U$ git checkout -b v3/master origin/v3/master
3 d! P6 E8 \; N, \/ p$ sh build.sh! P! I/ [  V) k# ]# n+ k
$ git submodule init
  m: a' l4 J8 i$ C# Q6 j% T$ git submodule update
' t: B" y* q, F" h1 h: \, N$ ./configure
/ d1 L$ i0 M; _2 W. s$ make
% a3 q+ W  \; P, C$ U- e6 ?, O$ make install
% T2 o& `( f; \+ J0 _LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
' S$ g% M# O3 l2 _/ X$ V; M
* J& C4 G: g9 h% R, o7 O
) `% e6 v2 y7 p7 X- g! M5 ~9 s$ ls /usr/local/modsecurity/lib9 G+ {# m0 t- _) J. y/ K
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0( Q( w# h% x4 l# t
编译安装Nginx并添加ModSecurity-Nginx Connector模块
6 z4 E4 L7 d% C6 w, L) B使用ModSecurity-Nginx模块来连接LibModSecurity
; H" b8 X0 W2 t
- w) o' P! i% ]; G0 {7 F  R# J" C
% F' g4 v. J$ S4 Y) V$ cd /root4 \) T2 k/ ^2 D
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
8 c3 E( P( u# X* u/ G" m1 v# D$ tar xzvf nginx-1.9.2.tar.gz8 g& q: {& b; k" R1 s4 M4 p4 \  @
$ cd nginx-1.9.2. g4 U& w; V) `  v
$ ./configure --add-module=/root/modsecurity-nginx& U$ O* ?  k1 e0 k- C* B0 R8 k
$ make% N. L- [$ }' |4 k: s1 W7 y
$ make && make install
, e5 {, J# q: O添加OWASP规则! b+ o1 b8 F: @( J9 ?. k! e
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。. S+ m- _7 w9 A8 B+ N' f7 q: T5 G
# Q( P0 _# I, C3 P- y9 [$ ~+ c$ Q; @
) H  k7 }/ V% O
下载OWASP规则并生成配置文件  Z0 _3 y' F" y- i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
4 X6 x# ]# J* \5 e. k7 }" L- [$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 L5 B+ Q2 e# A, r& x7 u
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' G# [3 a1 H# B' Y& w0 Z* q- X8 S$ cp crs-setup.conf.example  crs-setup.conf3 V7 s. ~0 p' p% D" g( |
配置OWASP规则
6 j  z- _$ V: Z* {$ L编辑crs-setup.conf文件
$ t1 a4 f0 L" Q2 |; C0 N# |4 H; T, f0 C2 x5 D
9 V2 I- Z/ D$ Z+ H$ Y
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf: \! {4 W& d' |! k2 B1 L9 f& i
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
+ g' w. v$ v" u' d3 C$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
2 G9 q: h! Y- Z# M" _$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf6 B4 A. E5 K/ [+ |1 [
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
5 `% S8 c7 o. \; b  A# O. b. n( Y! P. |9 U  H. z
+ ]4 ?- x: U' L& @6 V. `4 s
启用ModSecurity模块和CRS规则# B. W8 j) o. O" l/ z$ A
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* c9 d% H" q2 S' Z: U6 Q" n" f7 d  p& A# K$ r' O5 B- L& W+ G! m' _
' Z0 ?$ B- r: W) A- T( k
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
& b  J' F( S( q1 D( i% ?; e) x- |  I, B9 B# O! W5 q" D+ x
1 z) \* m& `) l8 R+ Q7 \0 u: h
$ cd /root/modsecurity-2.9.1/
1 J5 _4 \; ]( E$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
9 M1 O6 H$ v6 k% f! _. |: u$ cp unicode.mapping  /usr/local/nginx/conf/1 J* ]$ @+ ?7 {0 _
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
7 W2 }7 R( y  A0 g: C
" i: P1 ]/ m& }: i& t2 \: w! M. j7 r; F, T
$ vim /usr/local/nginx/conf/modsecurity.conf
- ^4 ?. e( a' iSecRuleEngine On/ g: \6 D8 I/ y' Q( r7 S
ModSecurity中几个常用配置说明:
+ t( k* J% m7 |1 J" E  u. A/ W+ r! ]4 b3 A" U; A, r3 |

9 L+ Q4 }3 f2 V, {) [# `# t. K1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。$ k5 f" }& A0 ?+ Q

- o2 M6 Q$ a' u: ^# V6 b, _, p& c" c) \
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。3 s1 b/ @6 T0 |( C/ l5 ^  y$ r8 z

; e% U6 f( j( A3 ]
* |# M9 S9 Z" b# \0 u3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% A; ~* k+ S$ V4 P, g" j2 J, Z4 E" ^; q9 D, o- t9 c; i: a
& P$ t4 L1 T, L0 }0 s7 T5 M
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。4 d9 F4 }! a7 t/ J( g: l- U3 c

: n3 i6 F4 f& Q% o0 B+ F
! \+ r3 x7 _7 T在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。; T$ P* G5 u+ `" U) r
6 N$ ^5 N+ Q) v1 D* ?' l4 f
+ f2 e" f) I! n) Z
3.x版本CRS
3 b8 e- @. G* g7 R% t9 K( C$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 b( ^6 Z9 [: R- ^5 d
# 生成例外排除请求的配置文件
& ]+ {4 A& d- H- d! n" F$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& _; p( w+ {8 V) F
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
9 S) U# k: S. _" W- C. L$ cp rules/*.data /usr/local/nginx/conf
+ r8 ^# ~* H$ p为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。3 h% [/ B/ ~, k) w" }$ y
- _+ ]+ a* W. k& ?" D, w
4 x4 n6 e- l& a
$ vim /usr/local/nginx/conf/modsec_includes.conf
5 @" w# S/ C; O9 Y2 S  ]; @4 P" T4 C( S0 d/ h" U+ x$ I" D
[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
$ y9 K% I6 I5 I
( y0 a" a9 u" \2 r) J* N
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
0 i" ^- n% o( d7 l% \; P/ C& R0 }  v! ?, _
/ l8 O8 ?: A3 m( E: j' {
配置Nginx支持Modsecurity
$ S3 u% u3 ~+ C5 Z( w4 {启用Modsecurity
; B) d: e. @4 P使用静态模块加载的配置方法
, Y0 h. |8 c! K/ t* H在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 o; S  W8 [+ e! _- m% A. @# S* u& p2 M+ J1 S0 l
1 `! a) K4 f* ~) @2 m8 U) O' o  {1 L6 I
ModSecurityEnabled on;/ I* Z! n7 S9 L: s5 h
ModSecurityConfig modsec_includes.conf;8 Y% ~! i6 a7 `: o+ O+ b! {" X
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。+ P; B  b4 c  q/ e. ~  o  J
- ^6 V3 x4 H, c7 V' B. f" _& T' D
0 U7 j" U, V3 V& o# L
$ vim /usr/local/nginx/conf/nginx.conf1 h' J0 I! {. p5 e4 t6 \

. \6 p9 m& |) Z/ Q+ q" R
5 M0 Q6 z* |& i: X, O4 Wserver {$ u. A6 a4 E- S$ s6 ^2 c. ?  u
  listen       80;
. W( @" F2 x2 G2 v1 y1 {2 K  server_name  example.com;
3 m: E2 B2 B4 o% |* c3 N: u+ N! v" g. F. V% W( w$ C9 }
9 M; E7 _7 w* U* M. h: R% Q
  location / {
( Q5 s, Z$ F) u# h$ M8 F    ModSecurityEnabled on;7 H* ^  q: V: x1 ?
    ModSecurityConfig modsec_includes.conf;
+ ~& X6 o! f! n9 ^; t  [6 X    root   html;% g+ u9 V7 d2 ~# a/ k/ q# x! C
    index  index.html index.htm;
0 y9 [# e" `, b8 ]& b* p  }2 e4 B# q: g! c
}, _$ M# H1 e" h& ~9 {- s: l
使用动态模块加载的配置方法
! }7 j5 u8 K. F, s2 D/ m, U在需要启用Modsecurity的主机的location下面加入下面两行即可:# y) n% j0 g" C5 }

- y- ?# w0 P. o* X8 S. f, t
+ F2 h0 j# S" @0 i; Wmodsecurity on;
3 k! l% Y2 |! L$ f2 s/ u8 Xmodsecurity_rules_file modsec_includes.conf;# {% f  _, g* ^1 F+ D. s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: y9 N  n0 {9 B9 s% u8 A0 z/ _. x9 J4 `9 A3 ~

( J8 Q1 F7 \1 G1 v% P# F- F4 ?$ vim /usr/local/nginx/conf/nginx.conf: s* d6 Q8 O+ X8 y; G: h9 x. A- E

* h  O! P! v8 ~  O% n, ?
+ ~! K- k) z) t# j) o# jserver {
6 _( @/ R) Q" S( q% N  listen  80;9 v+ w7 U2 d6 l1 j' `7 j
  server_name localhost mike.hi-linux.com;1 |% o7 g$ W& d2 l% e, L' K& Z3 s4 c
  access_log /var/log/nginx/yourdomain.log;
3 L/ d! m2 H" \- ?0 f2 V" @0 q8 w6 }7 w! @+ E- K
1 y* ~9 i5 }$ E6 F" v4 k* R6 w! H6 x
  location / {6 ~. r8 S# u8 s4 L
) d" \! O8 h$ w' Q/ u8 o- ^) Y: v

9 s  q7 H4 z- B/ \% c  modsecurity on;
- r# z+ a! W. t  modsecurity_rules_file modsec_includes.conf;
: s: j  S5 m% }  root   html;
! i5 z4 Z7 X2 N* f7 [  index  index.html index.htm;
' p" C6 C" t" ?. g. G}% K  X9 w# |% p9 P! I' w" s
}
$ a2 D( J7 Q) n" V验证Nginx配置文件
& X% {% ^8 m( h( O& a' X$ /usr/local/nginx/sbin/nginx -t) M0 u' f  W; G6 n( ]/ K; n( Z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" z" c7 E( C( K! d3 nnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful. z. X2 F* d4 V( Q
启动Nginx9 z6 j  {6 M" }7 O
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf$ L1 O# p# f/ Z* G+ W
: _% W3 P  F. s) I6 P/ t

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

0 I, y) u' x' m" j, Q3 n+ A/ A

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-30 08:12 , Processed in 0.087324 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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