找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9826|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
" u, I7 ?) x) Y6 n# ~1 K6 C0 {4 x& _* p% ^* Y4 E
( @# i/ n* B/ a3 s4 X. z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。+ ~" x' D) Z1 J
  l9 u& v& d" k: h# i6 D0 _
5 t6 ^. O2 A6 J7 {: w
什么是ModSecurity
& r* R3 m- H8 ]0 ]* P; O, v- sModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ S" Y3 n& j4 P; b0 i8 ]8 U

* }  Z3 c8 ^+ n9 F8 s% P# h
' A+ {6 H5 Y1 b+ h- C/ AModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 M6 Q$ Y+ @$ h- }% Q
8 [( c9 L; @; w: m, g

$ N5 O1 R+ q9 x3 P7 @ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
" ~) F/ Z% v3 R4 Q# D/ j; t. z7 A3 e. [" ?+ b- x/ P* |

- Z! \& p, H* X3 s: V官网: https://www.modsecurity.org/
  J5 d4 `* l  \4 e6 h3 T  \( U" n3 ]! w: a

2 c8 `4 |4 E0 u, o* f& Q什么是OWASP CRS6 |: A9 |; B$ l  k3 @: a% J: S0 w+ C
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。/ s" n7 h! m& f% t: h9 J' Y
; `* n! W) r. b% R( J
7 D4 r2 |/ M% B/ b; X  n
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。( A& W" c) i2 t: v$ n" E1 n

9 X# d; T3 _+ p9 Z; ?
3 P, ~& b& M4 x+ I1 _7 X1 O  mHTTP Protection(HTTP防御)
. f$ z; ]7 Z) Z  w  ], _9 tHTTP协议和本地定义使用的detectsviolations策略。, ?8 d' g6 Q$ y& A1 d: w

/ a+ A9 G1 ~  |5 N$ K: j2 D1 L# k) g% j3 s2 h( T: D1 O9 d, k
Real-time Blacklist Lookups(实时黑名单查询)
5 ]& `! E/ B, z+ V# X利用第三方IP名单。) \1 I! E2 Y! }# L6 N# u+ f
6 n4 E  _3 }" o, D$ L
, _  F( z9 a, z0 F  q: X( M6 |
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
3 ~, s4 T5 c* R防御HTTP的洪水攻击和HTTP Dos攻击。( C- _0 n$ y: Q+ o+ j  C

! y4 V8 c( B5 v9 G$ S/ B
1 N: `; f! {2 B  F7 I- HCommon Web Attacks Protection(常见的Web攻击防护)- J- y5 S) `7 P0 P
检测常见的Web应用程序的安全攻击。( Y$ O8 P1 W5 w4 u$ _' E' S
4 E6 i# S3 D% V7 Z  R' t
7 g5 E: |+ y3 l( |
Automation Detection(自动化检测)
7 N3 W4 v& q) c8 Q6 ]. B检测机器人,爬虫,扫描仪和其他表面恶意活动。
7 w5 o9 \0 a3 Q- G  ]  N" {0 a6 |3 u4 A4 I; a$ C% _' Z% ]% b

1 O: p9 e! d" y/ D9 \: T( pIntegration with AV Scanning for File Uploads(文件上传防病毒扫描). p( }, a; i! S, B1 k
检测通过Web应用程序上传的恶意文件。7 X: O2 Y) A& G. q' [: q7 c4 I
" D( w; s" X- ~' B4 O; k
: Z& G: J5 q3 p! u$ X
Tracking Sensitive Data(跟踪敏感数据)
1 Q% y4 i& L' S/ v0 Q9 m信用卡通道的使用,并阻止泄漏。
4 j' K2 J' S. g% ~# w) u2 S; l5 o: a2 U, M! f# {7 p

5 v1 [* [2 W7 f+ @Trojan Protection(木马防护)
/ k9 O4 ~) w, n1 ~' o" o检测访问木马。
0 V9 I% u1 ?0 m) r3 n: u9 T7 v& y/ P$ y) }) M. W
( P  R& H( Z. u0 ^0 w
Identification of Application Defects(应用程序缺陷的鉴定)
8 F% U4 d5 F5 S3 v( ?* M2 R, O检测应用程序的错误配置警报。
! e4 n% T% K; b2 b( F
/ ~! z* N: j5 f& k6 U) d2 b. p9 P9 ]( X/ A
Error Detection and Hiding(错误检测和隐藏). X3 w1 i% V) \1 }% Z
检测伪装服务器发送错误消息。) B/ `+ W) A/ P) G& G  k

" p' h$ l. i4 a+ J. [
/ ^: p' K3 G; b' {安装ModSecurity
+ k" X6 f" h0 v# P* u6 Z* n软件基础环境准备
# L# p3 b: b, r* x/ X9 f下载对应软件包' b+ A# k  U6 x+ K) k, o7 H0 I
$ cd /root2 [! r9 N* l- t5 c
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'" ]/ P7 Q+ x+ G4 O/ _% C
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz4 m: l! c: o8 K; D8 U3 V
安装Nginx和ModSecurity依赖包/ {& y7 e& I- l) p8 h/ \, @1 O
Centos/RHEL
( _+ h( G7 v& f; O% @
; ^% _2 _) z1 d9 f: p1 H7 e
. t/ C8 G. H/ _6 m' }0 Z. J2 c$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel4 o: x' ]3 _, g0 ?4 _9 r: w. |8 y' r
Ubuntu/Debian6 @1 B' ]+ f1 g5 Z  X: i

  x5 @8 i. H: [9 g, W1 y, o7 Y5 T" ~, J  F3 ?8 u' [+ [0 L
$ 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
3 ^- D0 i( E* m6 {编译安装ModSecurity
7 j9 Q7 U/ t' N* eNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) _" |, ?& P' T6 N0 p6 P+ q4 D

- ^4 K, J  H1 ^7 s( ~
4 H5 D0 L" O5 I) v) u方法一:编译为Nginx静态模块& D9 \% p( ]/ g* W$ O

3 E. R! J; A  x+ c/ O
! \8 ^6 v% X! v" c  G7 c编译为独立模块(modsecurity-2.9.1)
8 W  [9 m5 b5 I; ~3 U, N, K$ tar xzvf modsecurity-2.9.1.tar.gz
% e% t3 U, }! g8 `9 l) J) m- B$ cd modsecurity-2.9.1/
! R$ |2 S8 _& W' Z4 V$ D$ ./autogen.sh4 h% S5 S* f: u8 X) a
$ ./configure --enable-standalone-module --disable-mlogc
" B( S6 `6 c+ ~8 ]0 l0 t$ make. [; O- d- D$ Q8 w' B( Q! T
编译安装Nginx并添加ModSecurity模块7 k4 j8 S0 M& U! \: {6 C
$ tar xzvf nginx-1.9.2.tar.gz8 a% V7 u5 ~" S% w3 A
$ cd nginx-1.9.2
) [5 C( H6 |- ?4 k( Z$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 u- S1 k, D0 a% u$ q$ make && make install
' w- k$ V! B0 ^方法二:编译通过ModSecurity-Nginx Connector加载的动态模块* p9 d; H$ x6 X& N
4 d! _/ v  j3 @/ ]) t+ A; d. k

, n7 g& b. j: V4 V9 U编译LibModSecurity(modsecurity-3.0)6 p+ q$ a% }- S4 n3 A) b* u
$ cd /root
* J; W8 e7 ]  s$ git clone https://github.com/SpiderLabs/ModSecurity
, @. g: k0 ~% f1 K$ cd ModSecurity2 q6 K& G) Q% E: d9 x  K9 _/ v& V' @
$ git checkout -b v3/master origin/v3/master
$ u& R7 n( `- [0 c$ sh build.sh
9 M3 n" v( i& u3 K) ]$ git submodule init
$ U: X" m' ]: F' O: y; H$ git submodule update
" m  e# v0 A- t! j0 u4 ^% ]- g$ ./configure
9 }2 U9 ^$ z; s$ make
# s# y2 u$ e! ]' l, ^7 V8 u' u$ make install* x( B) h8 I: D# }: Z9 Z5 F: W) I
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。) A/ |# t: k3 t- O% O' a* s9 H

8 F2 ]: Y9 W/ h- l3 W+ {: G  |
3 s3 x; l7 I" s  T2 Z$ ls /usr/local/modsecurity/lib
% b7 `) {) z; Q& c. |1 D2 Plibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
" ?6 f! s$ z' B/ a编译安装Nginx并添加ModSecurity-Nginx Connector模块2 I; W% y  `4 Y7 Z
使用ModSecurity-Nginx模块来连接LibModSecurity
3 J2 v2 D8 ]; x- G1 x, x) b% Y7 I7 E0 q9 @/ H; G' b% R" N
* G8 ]/ Y+ y/ k0 N1 x# i# i; C* q
$ cd /root5 o$ S# X- {* B0 ]
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx; }6 I) |' a9 _* U1 t% S8 U2 |
$ tar xzvf nginx-1.9.2.tar.gz
! p( R4 j1 R: T* j2 M$ o$ cd nginx-1.9.29 H8 T( Q7 v) Y& X8 N7 j& s
$ ./configure --add-module=/root/modsecurity-nginx/ ?9 {  r# g3 ^! g" |& O5 c0 G
$ make
9 o& l3 ~, a" y8 M1 g" l5 x$ make && make install" k* R2 d; `4 }9 \
添加OWASP规则' k: a7 g2 t7 H9 Y# |5 n& d
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 X. t" c9 `: d" T9 s' c+ Y3 U
$ M* @9 I2 Y. i4 R8 s# Q
! x4 @/ \8 }2 C5 z" \
下载OWASP规则并生成配置文件6 M0 S3 Y% Y. d4 u; [5 f
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 F0 d3 H# ]( W: A& A6 M$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
5 C4 ^$ G* }4 f3 ]$ u3 k  ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  P, O' |7 n3 m% x* V( V$ _
$ cp crs-setup.conf.example  crs-setup.conf# V7 P$ @2 M# x2 M" L  r
配置OWASP规则
/ O& E; q# o. ?* i# }编辑crs-setup.conf文件
2 W8 k. \" W) i/ b$ K9 H: E4 Q- l
# v! f! o4 p* a0 s/ A7 f- A! `+ W- j8 |" r
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* W+ g( W. z5 p2 Z" u$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- |. r- f: f9 l+ ~
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf. C3 m1 y( X$ m4 p5 N+ |) b& o
$ 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% Z! p6 J2 ~7 F# s
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
8 m; X! i/ y& F9 V( [- p3 h3 g1 x, c8 {: m
0 {9 l9 Z: I  i/ U3 q# ]
启用ModSecurity模块和CRS规则
' C$ H7 }. p4 e9 F2 b- m* ~8 L复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。' B  K4 y1 D+ ~- z3 e9 U# q2 M: t
/ S; M- p  e4 L1 s% L, W  N

  i6 p3 c+ N8 Z$ z. ~" }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 F7 C+ F- m: m' K7 C
* X4 _2 p! D+ @7 g( ~1 Y

2 i) _) W3 \3 E$ V$ cd /root/modsecurity-2.9.1/
6 ^, u. P. @/ w7 I1 I2 H2 l2 H$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
+ I: l& l0 k9 q- h  R3 m$ cp unicode.mapping  /usr/local/nginx/conf/
/ B$ v7 \4 Q( h% ?/ N1 a将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。6 f! H6 p7 H% k
1 x3 O. [& u8 G  b/ i. Z
; O; n2 p/ }* p8 x% ]9 V* ^# n3 ^
$ vim /usr/local/nginx/conf/modsecurity.conf8 s' ^- V9 }3 }) H
SecRuleEngine On8 K! f, D4 I  g# O7 V: @8 [
ModSecurity中几个常用配置说明:, @/ s+ h5 z6 @4 C; Q1 a( U
( o+ u: W, s, S& l5 c
/ e& T' _$ W! L( W* G
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; K* j9 u; N9 w. Z- x9 y! ^
4 S& v5 d0 Y0 I! w5 j
% a% t. C0 f7 R  N0 X2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
+ \7 ]6 y6 y7 i+ N0 o7 L2 N& ~0 p) e3 b$ N* k
1 S1 Y! I" F$ p2 U" W; v
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
$ Q& o/ X# [' |) K( ~3 n  p! @7 X' Y1 Z7 u2 _/ C

/ ~( I( u' s: F" X4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。8 U* d( d" d7 l8 j

; G0 F) T  [" Q1 W7 O
1 D+ v0 c# o% l* y* H在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。1 ^/ |3 Y6 ]3 ]! w" j  a

+ y; {( X# q" i6 U4 d0 {& @2 i) U$ a
3.x版本CRS
$ R1 V4 h& V1 e0 q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs: Q+ Z# S1 U2 c: {; i6 F/ K: Q/ c9 @
# 生成例外排除请求的配置文件0 ]! `  I4 |& `6 w  ^; P; @! i' t# a
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf2 m/ v& X! G5 _7 p# f
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' h% H. k' U0 i
$ cp rules/*.data /usr/local/nginx/conf
9 @* J, X" w% E; ?7 a- J) S, @为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
, O" t% T( x/ R, q
* D, n. I3 |/ _1 p4 k
% A" j9 g( U  ^/ }: D1 {  S$ vim /usr/local/nginx/conf/modsec_includes.conf7 \* o( z. G2 B+ d  Q
( A9 R+ a2 o& b4 A5 J; S
[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
' @* w9 a3 G: V$ ^# r% b# p# Q

1 H% \% Y/ a: V6 l1 V注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
  u& c. k; W0 r8 ?% K' h
. V: e# S3 A. W% @, m1 o9 m5 B2 U5 _4 M- V+ C# _
配置Nginx支持Modsecurity
" T8 G* h$ A$ U" S/ Q  t9 ~/ ~启用Modsecurity
7 U# u! {6 I( O( H" w: K使用静态模块加载的配置方法
: g7 v; h% R1 o8 x. k1 E# l9 S- o在需要启用Modsecurity的主机的location下面加入下面两行即可:3 P& X# o' H1 S1 ]$ S
9 M1 r0 R; u& S2 I
& _, j; T  f% A# {2 X
ModSecurityEnabled on;% ?9 V" S& @- w. Q
ModSecurityConfig modsec_includes.conf;
  y' Z: T; [* Z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  g; z( J( p) O& A& r! L

5 x  k$ Z1 B4 b: C, L" _6 W) o4 j9 u' U
$ vim /usr/local/nginx/conf/nginx.conf
" ?6 j! W& r& }: M
9 i$ T- F& T' x, K) P: \( u. n0 [( C# O7 Y& f% _, n
server {
+ T) x( ^/ P  ?$ D2 D1 j) n: E' P7 k' [  listen       80;/ N5 z7 V; Z5 |& d; h4 a6 p
  server_name  example.com;
/ B9 R, o% Q1 N! a$ [# |% J! d% Q9 }( _
& _# I& i3 X0 U; y/ S# _7 Y) k$ d$ {1 J6 t& L. J
  location / {) Y5 \& c7 l* f& ?: G" O
    ModSecurityEnabled on;
& E0 }2 W* N* W9 u    ModSecurityConfig modsec_includes.conf;$ o8 o! a, R+ I; w2 ]
    root   html;
& \1 t- Z% R$ l0 y# ~1 F    index  index.html index.htm;
' Y" f- Q1 I6 B7 e# |3 P7 \" Z  }) ^8 A7 a6 \% E' Z( a/ p: G' C4 P
}
! g" W& `) r1 C8 ?+ J使用动态模块加载的配置方法9 x7 [. r3 e, D9 [
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 j% E, _7 @/ u0 ]; ]2 P0 }+ w( S
8 @& M! Y! G7 C! o

$ k0 K. c' s  d! z# z- a% ?modsecurity on;  w  s" X8 E0 Y( j1 }
modsecurity_rules_file modsec_includes.conf;
% H) |6 U" j& F修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
, d4 s1 \  j8 P9 o, [- g1 z: w* R
8 @  r3 ^) |+ D' B4 Q
$ vim /usr/local/nginx/conf/nginx.conf
8 `+ m! W: g; o6 K1 j3 E
/ E. S$ \; {# u8 k9 i% k! h; Q+ G% P+ x/ C, ?3 L" b: R" Z* x
server {! C* S/ e. z! s$ c6 u4 s
  listen  80;
7 }8 b! X( Y  u+ h+ [  server_name localhost mike.hi-linux.com;
0 w$ I4 z; q' }/ C0 M; {# Q  access_log /var/log/nginx/yourdomain.log;, N" x( `' o3 A
. d5 T/ }9 ?( b

' W2 [; y2 C) |1 q+ {  location / {
$ S* ^, F+ Y9 {. [
. @% F9 a2 `7 d7 E
1 Q) }& k$ T% E8 n  modsecurity on;) W8 y3 E& v' m: [* v: P
  modsecurity_rules_file modsec_includes.conf;
; Q  h( k# S0 x  root   html;
+ Q' B" p/ h9 q, N2 d4 m* v  index  index.html index.htm;8 @# ^) q% @* H' Q/ Z. @
}4 C( e1 q5 N. x; D: [% F9 \
}' M  J! F% _. m: b  Z. h
验证Nginx配置文件7 k1 {3 x  V# @+ Q' U/ C9 A
$ /usr/local/nginx/sbin/nginx -t/ o9 Q+ ?) R, H( a4 L. z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  ?1 e7 g( K' E, l: o" }  S9 Nnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& F& \8 Z. b. t% e0 O# V启动Nginx6 Y) G. C; _  E) Z7 ~
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 J+ H' J0 Q7 B
: j1 H2 g2 b$ j" e7 X

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

& L5 G" N1 y( [4 l# e6 e' S

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-29 04:13 , Processed in 0.046655 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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