找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9972|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
% E* u& l( ^6 R6 D5 b+ r  L
* |; d$ p4 J3 r) M0 ^! K$ H, p+ A
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。# {. v, ~6 U+ f5 Q9 C
- r! Q' j* L0 r. _9 r
! |4 k3 l. M8 Y9 w
什么是ModSecurity
4 _9 f# p$ H1 l" K1 [' P  I% ]9 \ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
( _0 T8 M: p7 v6 F' W( D. K
! N+ e- S+ Z8 q
# _# H# ?( g! x4 SModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
# Q; {2 h, \7 s4 ]+ m! k5 Y% Q9 c8 H  A; M6 p" T! r; h" W# T6 }
) W- O/ p! L' d/ j% o3 h
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。: k) X1 `( Y4 Q
( }# F/ P) O# L) o* `& C

* t# D) C( b% K, M! f5 ]$ R官网: https://www.modsecurity.org/
1 k; e. @1 H/ p8 B1 C
4 q, J, a5 p" ~7 _/ D$ ]! i& k* C2 j! [# P
什么是OWASP CRS8 [7 h0 B+ T0 J! o6 t
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
$ Q& M. m2 [; P" j& {- i5 ^5 y3 L7 j, I2 S4 B# U# j
1 _2 W- k, P2 ], F1 [
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。/ P2 _1 x# J' h: Y, f0 Q% h* y
7 ?6 S( q, d- X2 v0 C0 y
6 p* ~) t; N" g; V9 M6 I: g
HTTP Protection(HTTP防御)' N( m. n$ H9 i3 V. P. V
HTTP协议和本地定义使用的detectsviolations策略。
8 Q, r( l) T) f. ^) r
, j. E4 @0 s5 _
3 K- J% x( K2 x0 t6 X! l% N' T" eReal-time Blacklist Lookups(实时黑名单查询)
3 y" c" p7 ^9 Q0 H利用第三方IP名单。8 M: I7 R3 r6 S% h
3 \* E' q0 [% \& `$ e. z. ^+ X
/ L( K$ C. B) M
HTTP Denial of Service Protections(HTTP的拒绝服务保护): M! i) l) H$ f1 y) d5 q- C
防御HTTP的洪水攻击和HTTP Dos攻击。/ r* t2 B' P1 M2 H4 {0 p& a
  g6 P8 @% V  X( I# ]* }, }! w

; P4 |4 W: y* }. iCommon Web Attacks Protection(常见的Web攻击防护)
- x1 e; A1 i: h4 c) }9 S检测常见的Web应用程序的安全攻击。1 w( p' L8 Q/ V
( @8 L8 I% s5 p) G, r

: h+ {* @2 ?5 l& e' ]* vAutomation Detection(自动化检测)" G6 j7 |0 T# t  |5 Q  B
检测机器人,爬虫,扫描仪和其他表面恶意活动。4 O9 L) x* L  x0 o4 Q

& G9 O: D% \& x+ H9 P4 C, c- V4 j  {/ ~
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)  O, W3 B+ q; [! M
检测通过Web应用程序上传的恶意文件。
- K, s( `8 K4 B/ z5 x- U" q( j1 B, X6 ^( a( B, s3 [4 _" ~- m+ \
( ~* z. |- _, }( i
Tracking Sensitive Data(跟踪敏感数据)
7 E  _# h9 C  y信用卡通道的使用,并阻止泄漏。
; P, i) ^  M- w) [! x
6 Q2 V- Q6 Z; ^% y! i. t
5 x" h# S0 H# J9 g; F7 H5 e2 zTrojan Protection(木马防护)
$ w2 G" r7 m- n3 d/ [% u检测访问木马。
- b" g. Q, Y$ r: ~% L( y: F3 p
% U/ n/ d  B* z3 h: M& F6 {$ i+ m' F) s
Identification of Application Defects(应用程序缺陷的鉴定)
+ o* k& X2 g+ I# V( `) V! X- I检测应用程序的错误配置警报。$ `- s  Z: q0 J! u# w) F. J: V
' F( }) A/ Z# U! W& W( `  U
& S7 {, Y& `: `/ z' c
Error Detection and Hiding(错误检测和隐藏)! }; m+ D+ Z- n4 K7 x9 }' B4 H
检测伪装服务器发送错误消息。& q3 h% q( ?0 R+ P
; F4 y; u; z' `2 \& H( l+ W
4 }& {/ J# \2 t5 |
安装ModSecurity, U1 a! z, P* Z- e! f
软件基础环境准备. a+ x/ A0 C* d; r3 c! w" N
下载对应软件包
- L5 Z# q9 Z& n+ O$ cd /root
9 i, A+ q# E/ e# H& S$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'4 v8 Y: C4 x4 |- T0 A& }
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* z0 s0 H4 U# `. W% D7 A6 s; o* B) V
安装Nginx和ModSecurity依赖包
4 m. c+ z3 J3 ]- UCentos/RHEL
$ @# G6 d/ f* K4 P( j+ P  Z2 R3 Q7 U# Q' a$ L) l$ W8 S! K4 T# w! S

6 ]# R1 }2 A# l% G' ?$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
1 N7 R+ A, U2 v1 [. H+ H* W) pUbuntu/Debian
' F& E0 H1 F+ i. d. x7 a3 w% `8 Y4 _! g# U7 {1 w/ ^9 R2 ^

2 G( j; l# Y$ K$ 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
+ J9 n" }1 M* L, v" E, K编译安装ModSecurity6 p- h: A# \! G' L3 `5 m
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
+ T; S" b! ~! ?  G# H& o  D' V, N' i! @9 i& e2 U* ]7 r. f; K

( G% h8 Z) E# W/ E方法一:编译为Nginx静态模块' `+ z! u( ?* p2 I6 @) v

, s& C" L/ n, z( B# q) l' B/ {8 o8 B8 J) p( ~! n
编译为独立模块(modsecurity-2.9.1)! K3 I% m. o5 x: }, K
$ tar xzvf modsecurity-2.9.1.tar.gz& |, X- K6 R2 V2 N  a0 H
$ cd modsecurity-2.9.1/
) V; f- G% ^' N" f- R$ ./autogen.sh3 s3 ]: P& U3 q) @
$ ./configure --enable-standalone-module --disable-mlogc7 v9 r/ ^4 s, Q8 H1 V
$ make# h% i) ?; j: C( {' P% s
编译安装Nginx并添加ModSecurity模块
6 B6 M6 I1 G8 t6 }) J% p+ V$ tar xzvf nginx-1.9.2.tar.gz
) j5 a: s( ^& y" K, s2 W$ cd nginx-1.9.27 _: |3 B0 j/ ~* P# E( K5 I
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/: p0 L! M" w4 d
$ make && make install* X" G& r/ b0 ^+ G3 k) Y$ H3 S
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 \' c# }/ f  U+ p
. D7 h" e( j. ]0 S
: Y1 e- v& I/ T  a9 g编译LibModSecurity(modsecurity-3.0)
1 ~7 w. M& I& J# k! C' g& z$ cd /root9 ^) _! Q" g9 i3 \6 j0 j
$ git clone https://github.com/SpiderLabs/ModSecurity2 s9 W3 N. t1 Z
$ cd ModSecurity
& b! H7 g2 _( E% T1 `! v$ git checkout -b v3/master origin/v3/master, R6 E+ ]' b: N7 `( A8 L' {" m) B
$ sh build.sh
; e. X8 Q: g* g$ git submodule init" i" w+ P4 Z" f& D" D
$ git submodule update
4 v( O$ l2 _% H( @& n- R$ T% }$ ./configure4 N) v, u& }+ I: {4 U$ X: A
$ make
3 ]- ^# {! B& Q$ make install
# @. i) k' T+ V  Q- q/ @9 {& HLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。  j1 }/ v8 h/ a4 e1 d/ f0 O

, R) e; w. W. {+ R
! q' h1 \- ]: |0 H$ ls /usr/local/modsecurity/lib! S5 \- `( t* x! Q" }
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0- j: R9 o3 b* E& H3 |+ t
编译安装Nginx并添加ModSecurity-Nginx Connector模块
1 ?. f+ H  A. U7 N! B" k2 k使用ModSecurity-Nginx模块来连接LibModSecurity
/ O  R6 h/ }3 V1 Q. M
2 }* J7 A! Y( \) ^, k- ^' O- r6 U- F- c) S" E2 V& T
$ cd /root
' v4 U0 D$ A) t9 j3 Y, ], k) o- O" `$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
3 @8 e! c; H" @& ]+ M$ U& b0 N/ K$ tar xzvf nginx-1.9.2.tar.gz
/ _3 Y- \3 @  \: J6 G- l$ cd nginx-1.9.23 u) @! F+ A2 l* M+ D- P; m  V
$ ./configure --add-module=/root/modsecurity-nginx
6 p* }1 M% m4 K" s/ o' I/ [$ make4 }, C! Q9 O' O- _$ U. v9 u
$ make && make install
5 V% E) S* T: ]- t# T添加OWASP规则6 J' |3 w) ?/ @; P& _
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。4 S8 s- b3 r+ K, s+ s

+ W3 ?" i3 s; n. r5 {9 U! z7 A: q0 j, I+ `6 u/ J" D
下载OWASP规则并生成配置文件
  S4 g2 y; u0 H0 P$ B8 Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git: F$ p* x5 {7 G" q
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
+ q2 Q9 v1 N+ x$ cd /usr/local/nginx/conf/owasp-modsecurity-crs8 p+ v5 P7 Z* B
$ cp crs-setup.conf.example  crs-setup.conf
: i1 C& Q$ B0 {, l配置OWASP规则) |# h0 U$ Q, v- ^' D! w
编辑crs-setup.conf文件
* x5 p6 P9 w1 `3 g( {$ W
9 O1 f/ n! L4 q! S
+ q# L% G$ U) {1 ]* G9 v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ o5 {( F7 Z  e0 `4 N
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% O* ]# U3 ?5 s, v0 d
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
5 E( @. x1 D$ v9 `+ x$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf- Z, g# d' J/ Q* X+ n
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ f6 Z- {0 z8 `! i' U: B

' s; E" i% M+ v' D1 C9 i6 T# X8 R! ?  R. h' m% h4 G! j* x. T
启用ModSecurity模块和CRS规则
; }2 D) E+ a+ f. r: v- m复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& g- m. r$ A/ S8 l; Z) e

, ^1 _# e1 H- I* j1 F6 o1 o: k' ]2 j4 O! l
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 W- u0 {" \5 S5 z
! O0 U" z/ }1 Y% ]

+ k" B9 V1 G) _$ cd /root/modsecurity-2.9.1/- a& y5 X& j9 A) u3 ~$ S; d
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
+ f" b. G# S: I  s) ~) q$ cp unicode.mapping  /usr/local/nginx/conf/3 b: Q/ Y3 Q  c) x% S
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
, A3 t1 y% ^: S6 u  A& l
; U3 p" B# T  E! E# A# m2 @& G0 H5 w0 V1 o
$ vim /usr/local/nginx/conf/modsecurity.conf
. I: O) d' c6 w6 _& J# i4 gSecRuleEngine On8 {0 ~4 N; P, i( a$ S% d; U& H0 u
ModSecurity中几个常用配置说明:0 h$ A4 x. ^/ D, T9 l4 z( b; m4 K

9 p- W8 f+ \4 F& _! @2 S7 ?* Q8 n: z3 ]3 Z* N4 s5 ?" d
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
$ x6 g& N( q9 M* \/ \
2 X! h0 z6 R+ T9 f' e5 ^
" ?: O/ H$ m0 ]) r$ j7 Q7 C0 E, p2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。% @: n% J7 H5 c" t  k6 J: Y/ m" c
7 q' b; Y9 M; o/ y0 W5 {
) Z7 y: G" W  z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 ^4 {4 x; U2 x6 E  I: s) o6 _, G% H+ R, b1 f3 T, R% g

; x' O' |6 }' q3 x6 _4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
+ A+ @" B, |0 w7 h
4 t2 e' y5 z7 _5 _4 P4 n4 [( z' V& `2 H
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) x6 o* k7 x+ I# [) q5 n3 Z1 O% t  @3 Y
" G9 u  g/ G! }  J/ ?
3.x版本CRS, x9 R7 n( k% ]( d' G1 A( c( U
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" _  R2 F2 V$ G7 i
# 生成例外排除请求的配置文件! D( h2 a5 s. g( R$ c0 O
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf, W4 Y1 f( `1 x
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
* \# s, k2 Y0 ?$ P9 p; z" @$ cp rules/*.data /usr/local/nginx/conf
5 P1 e. d# E0 @6 ~# J) D& K  o为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
; B7 ~! L6 x: i1 X% b+ I% N3 h* [/ r6 W8 S4 p
+ d* d+ a( U9 t! s' Q8 G
$ vim /usr/local/nginx/conf/modsec_includes.conf
- y( M* X( f" d) d
2 b/ Y6 X2 O/ f5 c( v) H0 X* l
[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
( ?0 D6 J3 j% M  e5 U
) h& Q+ W# ]4 Q% R0 z0 l
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。5 Q6 s" p$ C1 W
8 Y: W9 a2 x3 i2 t

, Y+ L. y; l0 ]' M% e配置Nginx支持Modsecurity
6 `0 F* \) G+ g; [# c2 x7 G4 x启用Modsecurity
5 e5 p* r! e8 E* C* y使用静态模块加载的配置方法: c3 a' m& r! y# V
在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 |4 a; `" _7 q: U% `3 o5 b2 B9 \; `, Q  E  b$ D

% f5 ~  ], z% _# C8 RModSecurityEnabled on;& L9 ?( b$ H. V6 ~
ModSecurityConfig modsec_includes.conf;$ z6 V! S/ w$ T2 n7 s! n
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: Y5 ^/ e. H% X8 e& Y! J5 h8 M! H0 p2 {' ]

* X" J6 a$ k/ O" g' l  q/ H$ vim /usr/local/nginx/conf/nginx.conf% f; U, ~, Y, D$ |# o
" Q6 i8 ^; k! k
. E5 [% y) n* N0 W3 T, K+ G+ t
server {) s9 k: _# ~# x: [0 }
  listen       80;' O% S$ T8 e/ d& s" h) _
  server_name  example.com;
3 s; G/ r9 p2 b9 C
$ o- h/ `+ @/ |8 u
5 N  z: g8 E) n# e& s7 Q  location / {; _& g2 x4 |" ^: e/ M9 @" n. ~/ X: x
    ModSecurityEnabled on;
. Z" E% C  p7 n    ModSecurityConfig modsec_includes.conf;7 H2 A" J, U' U0 a* }7 L
    root   html;0 U7 l0 g  R" X
    index  index.html index.htm;$ n* ~" F* U" u  q
  }  p: m* f+ d; w
}
0 o1 L* U4 t0 k+ s6 E% e' K: T" n使用动态模块加载的配置方法
) q# S' g  n: q- @' ]/ C) Z6 I在需要启用Modsecurity的主机的location下面加入下面两行即可:
) f0 B* w' Y. `* x$ O
3 O' ]0 v9 C3 @) G1 [1 X, N- T) N% U3 X9 {1 ]( Q3 g
modsecurity on;9 @2 w! q$ K* v( d8 Q3 N9 ^
modsecurity_rules_file modsec_includes.conf;% m0 h: w8 t& }
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- A( a; j# d0 t+ d7 I( `$ K3 F) M5 E; A# Z# A! i0 V# H
5 S. @6 ?) v% r
$ vim /usr/local/nginx/conf/nginx.conf- R- J& ]0 e6 w- D, V

: }& P6 L' R+ X: c4 W( S6 j- j- C' F6 r+ g$ y4 U
server {+ r) c6 e3 O) A9 G3 }- \8 l/ m. q
  listen  80;
5 M- r" U& F) N  server_name localhost mike.hi-linux.com;
# q% W; q1 h+ d  z9 D+ j  access_log /var/log/nginx/yourdomain.log;( x* I! \' v7 H' |% R- {3 `
2 G/ y( ~, @! x

' i8 m# A3 E5 i$ f6 d! y  location / {) g# Q$ @4 e2 Q: }9 H" b, x

( [7 O" S- L0 Y) |" N5 m- T# ~3 S0 `: f' F
  modsecurity on;
6 s) l1 n+ x! U+ F! Q3 {1 k5 y  modsecurity_rules_file modsec_includes.conf;3 b- P4 z1 B. t9 b8 N& A) B  J3 Q
  root   html;
+ M0 y" {; U3 ?  index  index.html index.htm;
3 A: N1 O8 |0 o' L( `; T& R; N}4 g$ P# o* u# d
}
8 K# O( J0 u0 e& a验证Nginx配置文件6 x8 b1 Y5 n& F" K7 w2 A
$ /usr/local/nginx/sbin/nginx -t3 I: [& {2 R) l- m4 s
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok: _- i& w- W1 T. {2 G. f! g
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) h( Z8 J! e8 D" M3 `& L6 l" m启动Nginx
/ ]! w- O) {  u, A/ F$ ]$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf3 b6 @( n- |$ V3 J( J+ j) Q0 k

1 d6 B8 T/ F( 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 ^# O3 r3 K6 [# Q+ |1 Q2 @

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-18 01:58 , Processed in 0.034152 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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