找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9797|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ b% a  M5 Y$ C" s
) Z% q% [. T, _6 t: K. [! l, ~( c0 ?+ ]
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& u8 o3 t4 p+ N( K9 }
3 `6 d+ L9 w5 V; N' f% P" B* W! W
6 H# z3 a  w( X; E; `/ @# G% Y7 P
什么是ModSecurity8 Q1 p$ N/ X) N: r% V# l
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 |  k# K) s. P9 y: K. M" [0 z& T- \, R. K" M6 E

; f1 x0 m* o8 @- }( VModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。$ p! S# Y! F; x2 M0 p6 \- q
" z/ g: c' T% S& {& k. K# R8 P
3 w( n: r" {! O3 E" e! c
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% i. d' h  r& K  \
4 l) ^2 m" P. v# ^- t& {
" I% g0 j2 t* a( q( x5 D; Z( J
官网: https://www.modsecurity.org/! L/ @2 M0 b% a. Y
: H, c; Z) |7 u/ v" s  I
! N0 F( S+ D; s$ j5 ~/ P% o
什么是OWASP CRS
$ D9 Z! S- M1 p: l5 }/ {3 r6 a- r/ LOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
" H( U# @7 v7 f: ~5 _/ |
; Z+ _/ D$ U7 {9 c) A; H+ b! v/ x
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
, D1 T+ T; {8 s' I# q" X, A4 ]( j" V0 f% e, s

2 d, F! ^$ I, J/ S! X; H; Z) hHTTP Protection(HTTP防御): u4 s$ \& X9 V' i
HTTP协议和本地定义使用的detectsviolations策略。/ Y9 B/ ?1 }- D5 o* F# p5 t- o  X

" W2 B( {, r3 K" m: e
* _6 O! H0 f2 x5 wReal-time Blacklist Lookups(实时黑名单查询)$ G( }6 L: _, T
利用第三方IP名单。, e3 P; n) S* a: t- Z" B8 W9 }

8 c9 ^! M! L& t- d1 ?- q9 Q: ]# W5 z" \# U4 s- @- ]
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
, I: k% k3 j& E/ n) e防御HTTP的洪水攻击和HTTP Dos攻击。. z# Q2 ^2 [9 d7 w/ b

: t. w! w6 V& x; `* k/ P0 Z/ p' d/ B4 J
Common Web Attacks Protection(常见的Web攻击防护)
+ b: j6 u9 f0 }$ Z! g  D5 P检测常见的Web应用程序的安全攻击。* q! Z; k# N" _0 \

6 n" h; Y# Q* ]! [4 ~9 j" |3 s: k$ ]! \6 i2 n) M5 j
Automation Detection(自动化检测)
  Q) t# X" h7 r( _! x6 c2 p( q检测机器人,爬虫,扫描仪和其他表面恶意活动。8 ?" c0 c1 c: E  z1 L
2 u$ K$ I8 y5 y1 k4 h
: z! d5 q1 l; ]9 |. U5 L' I
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)  a& G+ \5 [. c$ V$ U5 s6 c
检测通过Web应用程序上传的恶意文件。( w7 v0 c, o) A$ `0 M

# B5 ?6 _& W2 E, o% k) d9 m7 M0 W/ V9 ?! [& I4 s* d  H5 I% t4 w3 T
Tracking Sensitive Data(跟踪敏感数据)
; e- Z" D3 g6 w( W9 j信用卡通道的使用,并阻止泄漏。7 J7 G/ D# a# b

/ k) t3 J- I. f6 m3 }6 F1 \6 H$ c
* W+ _1 E$ ~! @- yTrojan Protection(木马防护)
8 G$ A; q$ b5 n" _% j- K  ^检测访问木马。' Z; D! }7 z& G8 F

$ X7 P3 d0 O% U  {0 g$ P; w
* t- w4 g: u0 YIdentification of Application Defects(应用程序缺陷的鉴定)1 `% y3 Y, Q7 C3 D# L) L* U* z
检测应用程序的错误配置警报。
4 R3 @* v. o& |; U3 l3 g+ G
8 Z/ ^  @2 v4 I
, f9 H2 Q# e2 D- I( eError Detection and Hiding(错误检测和隐藏)' @" {. H8 B( M! L
检测伪装服务器发送错误消息。
. ~# e+ p! b6 D0 v) T4 Q. }  s4 {6 T/ g/ U7 c' g( D0 L2 K8 D: G: g
* {! t9 z6 T5 Z
安装ModSecurity* W& `  _7 U) D! j# i0 H
软件基础环境准备$ |9 q& q+ F6 z; z0 r9 P
下载对应软件包
2 d" h0 v# s( A, F) s4 V( D; a4 B/ {$ cd /root7 @6 r5 Z3 S' V7 t, x* y, k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
: U# r, j6 Y9 Y" k7 F. m$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
- G$ z5 _' N# X; t安装Nginx和ModSecurity依赖包
/ W( \2 u% Z0 R, F. ~+ W# FCentos/RHEL+ c( S7 p/ p% M& [! a6 ]
# x4 `6 h) y& h1 e5 c
5 C; O* |- ]: B5 f0 J. ?* V9 Y
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel9 L  p. k$ n% P( S
Ubuntu/Debian
+ C" U; C- r, z7 J- f& E2 A- j
7 j+ t( A$ U$ @4 w' o# B6 s
: o# L. s* g1 A$ k4 f( ]$ 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++-dev1 w3 F$ ?% f" B2 k" M& h
编译安装ModSecurity- a' ^7 c1 z! V4 P2 I+ Q! [- l# R
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。. M6 \5 z5 F/ Q3 O! E9 f! B

/ K8 P6 k9 r' ^% ^4 m% S! z2 a, l$ s7 D
方法一:编译为Nginx静态模块- k5 V/ b; t; C( O
( e4 X! Y0 l  |/ S
' w* j: s6 H6 O
编译为独立模块(modsecurity-2.9.1)
; p- q& [. q2 @; C- v$ tar xzvf modsecurity-2.9.1.tar.gz
7 c( |5 L% t8 y, e$ `4 F$ cd modsecurity-2.9.1/
- ^# x5 y: `$ p$ n$ ~1 D$ ./autogen.sh* i  P! C* d2 u9 Y* H% M) \
$ ./configure --enable-standalone-module --disable-mlogc
3 p! z2 {" ]4 k7 ^$ make! {' I: r: G9 J! D9 o' M  Q
编译安装Nginx并添加ModSecurity模块
5 y6 V8 `8 e+ K3 M. C9 Y# R6 T$ tar xzvf nginx-1.9.2.tar.gz
% i" w% c' ~  \1 g- ?1 @5 k; R( N  E0 m$ cd nginx-1.9.25 c8 c2 a( ~: @1 k- l% m& y
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/! e  K/ Y4 ?1 d- V, a
$ make && make install- M* l- [& q, a) I: n) c, {' Y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
  D4 s0 o/ t% ?# n4 g/ L5 D
) m& j# D7 ?0 V2 n4 y7 w" C
: k7 t) s6 _9 w5 v$ x' [编译LibModSecurity(modsecurity-3.0)
7 d8 i: \7 c5 N) ]$ cd /root4 }3 f: D0 _& S3 _# N" T5 {
$ git clone https://github.com/SpiderLabs/ModSecurity2 y2 J6 C- E1 E, i1 I) B- a
$ cd ModSecurity' `  Q' f6 I1 Q
$ git checkout -b v3/master origin/v3/master/ |  J. l: u8 S
$ sh build.sh, V0 x) ^. P% Q$ R- {$ }
$ git submodule init
/ ?4 [% u& T3 F/ k- f$ git submodule update' j' \# `& @$ n1 S2 G( @; O
$ ./configure
, n. J9 G) K" F$ make
! C, h- W1 \) m3 D! O9 F8 p$ make install
, y8 G! r2 m7 k3 fLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
' {$ w0 I/ z4 z. d2 d6 m) v- a5 ^, P9 C* I

* E* i; d0 E8 j& }8 g$ ls /usr/local/modsecurity/lib
% K; J8 W- O- G: S8 A" O/ jlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0% `0 k) F- y' y( b7 y& H* J( K! h- \& I
编译安装Nginx并添加ModSecurity-Nginx Connector模块
( c/ |0 Q" Y' V# V* L  i使用ModSecurity-Nginx模块来连接LibModSecurity
0 s7 c  r/ G0 c1 Q5 i7 q! l$ p7 s& e, M0 M
* e0 e: w$ Q+ h* m- B( S
$ cd /root3 ^7 t- C) U7 R; w1 s& w  Q
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: N3 T& ?' I- k/ @, N0 ^5 h- [: B$ tar xzvf nginx-1.9.2.tar.gz; H9 v* @* d5 B0 D, _  J/ Y$ n0 x
$ cd nginx-1.9.2
4 M0 V/ X% D0 e7 t" k0 w. B2 \$ ./configure --add-module=/root/modsecurity-nginx
/ y; Q4 m& z9 x! p' b$ make! q4 a2 ~! d  S/ D; E
$ make && make install
, P9 Q" U  i  G8 [" C添加OWASP规则
) v1 [9 F4 F+ t, o4 Y) c/ b9 R8 JModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. a* m5 \, X2 h; E& G' A: `- r( P( }3 f! X  x

/ d  P5 o# o2 n' E6 C0 y下载OWASP规则并生成配置文件8 o/ W/ t& C  m; `* H% h
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git) f( W& l8 e3 N- r
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/# |' z. t/ a0 q7 c5 z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 Z; k& O7 r# V! v$ Q: G6 Y
$ cp crs-setup.conf.example  crs-setup.conf4 V# T% y5 M) ]
配置OWASP规则7 \0 m: g3 a( O# @9 T. g
编辑crs-setup.conf文件5 G' ]/ r! a5 ~3 b4 Q

. E/ t5 a. t4 H8 \9 t, a5 J) g5 _9 F/ f) \$ ~
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf# _% P' i; ]' D4 e  c& _
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ Q4 u5 q( P  k! g- U( F$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
, ~; V$ X! F: I( `8 J0 Q9 C$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf+ l6 H, |' H! c. G( p7 |% B* P6 w
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ O4 R4 G2 w' V

- p8 T% Z7 v( h3 d6 G2 f9 K2 [7 b7 f+ B& C7 W
启用ModSecurity模块和CRS规则0 E# n" k: a$ T
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( _& H' [: @% v1 i9 }. Z

; B3 K) G, z- \6 v; c2 w, `& w: s
7 P+ o" S2 I# d) mmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
; t- N, E5 S2 {' y7 v0 _; z1 w- J6 O6 i7 l- V

+ x5 @' b6 t3 K. t$ cd /root/modsecurity-2.9.1/
8 i! A5 u% r; j6 R9 a$ u$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
7 m0 g; k/ r% E5 e$ cp unicode.mapping  /usr/local/nginx/conf/
5 @. T( A% e% N- m0 u% n0 C% _% b( p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。8 d: |+ n+ \% A6 g  }! `

$ E- e/ d6 v1 _4 \4 y; h6 j  E+ A( H7 X8 E5 n
$ vim /usr/local/nginx/conf/modsecurity.conf
. D! w7 |7 p! c2 N, z. MSecRuleEngine On  Z7 y9 m& N+ ?* i6 s, o8 s. ^( Z$ x
ModSecurity中几个常用配置说明:# h) o3 M7 }2 _5 X( r
  s( k: i# }: V
* B1 g7 o4 P+ E3 x  b8 q2 F
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
/ \8 k. A7 X/ I# W8 L2 V7 \, _! l3 q6 ~! q: C* f, Q9 a$ \& P
2 O  q7 s% s& q% m( H# W1 v5 }- `) P
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# K1 M# V$ M# ^, J  W
! \3 l* V; d0 Q1 W2 b4 F, i
$ s: [# l* w0 G7 J
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
- W% z: w9 I" s! f( C3 @) V0 ?/ W, l+ T
/ N( I, e; O  h( K2 r7 Z* K, @
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
# W- |9 B/ l9 q, Y" v. O1 I7 {2 e7 e2 D$ f" l" |6 }
( Q8 Q$ y% ]/ Y/ m! s' |( S  G
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。- U5 m7 }0 ]4 T

* ?. c1 J9 A3 Y9 X4 T7 G$ ?( ]: G5 k2 f; L( @
3.x版本CRS) X1 b% g7 W3 I( Y. v) B
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( L( c- g. Y* u: I9 s5 [
# 生成例外排除请求的配置文件
, Y& J( e. f  t. A9 t: i/ [: j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf, n' T. \" S  P2 Q9 k
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf- \- N3 w+ S, }7 x7 f0 l+ H
$ cp rules/*.data /usr/local/nginx/conf$ m5 l5 J, [4 f% z
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
# k) E1 W$ D7 G, S- m' W3 y, D" m$ C% D

/ R% k! {, U8 s' t$ vim /usr/local/nginx/conf/modsec_includes.conf7 {" Q! r/ I6 l6 U
7 O1 D! P8 ^* O- k
[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
  N" T" v" e! v% M8 l
0 c/ C% w9 L- W
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 m) @8 h+ n/ C

  x) E0 C" T* [, x1 g# c  n5 G+ N- J& w: e- r  i
配置Nginx支持Modsecurity
% f; n  o) p4 {5 e( b  h启用Modsecurity5 e' `# K2 q  E/ N" U; L
使用静态模块加载的配置方法
- V# W& ?+ K) {) D( E. ^' I在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ V* F3 }4 S/ k0 _; Y7 M
- @3 m) k1 h) W; P/ I  x6 e
  h) `) e% h" _7 {* ]4 s5 FModSecurityEnabled on;
/ J9 O9 E% F9 m( K) _3 G' aModSecurityConfig modsec_includes.conf;
! X4 ^% d1 s( B5 _* [" ]2 m0 {! v+ U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
" R4 k- i4 j+ o# n
0 N# U* J7 B+ R, C0 |5 K2 |( V9 X& x% Z
$ vim /usr/local/nginx/conf/nginx.conf& d% \/ d6 k( [6 M$ ~; y

/ F+ `! W0 ^* E3 ]2 c3 U/ ~2 U+ r/ C1 B
server {
. ]% `/ l' w2 ]4 Z  listen       80;
0 m) a: [$ Z2 I  server_name  example.com;. m% c+ s+ w5 ~3 _. E7 n

( I  E  [' t2 ?9 D3 W! ?% y
' I0 Y* o4 }( U2 Q+ |  location / {" _" N; S  B8 {8 U9 M; u
    ModSecurityEnabled on;+ d; \5 n: K, W% X& T% o& S( F+ ]8 i
    ModSecurityConfig modsec_includes.conf;
) l1 Q: Z9 X1 \; l    root   html;
) X; O1 V3 F/ ^$ ?5 `) J; n    index  index.html index.htm;( l" _$ p4 A$ P! u' B4 }/ P9 F4 Z
  }
" S9 D3 l" M1 V# [}8 D' O: Z* P* f* r! D: a6 n9 H
使用动态模块加载的配置方法7 o* Q( d1 Z5 h; D) @' L
在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 F0 }6 O% W7 f' T- J; D9 Y1 w9 C9 n' x0 z

+ K4 C! ~3 H6 s7 B# q* ymodsecurity on;
# f$ [9 N( v( \! ^modsecurity_rules_file modsec_includes.conf;
# ?% G: k6 ?* k4 u- s' v( n/ V修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
1 L  q( R& b, E3 T) z- }6 Q% s4 m+ j  ]- g6 l- }5 C! x% u4 |" j

  w' L4 d0 j; H- K4 m$ vim /usr/local/nginx/conf/nginx.conf
" e- z( K* `2 q+ Q2 P! f8 m
, b( Z3 k1 `9 j! J( \2 J
+ @' W  Q7 o# E; h+ Dserver {
* @4 D' _+ d; x  listen  80;
2 V7 g" w" b6 C, x, _) Y' Y  server_name localhost mike.hi-linux.com;
/ V! G3 L& h- P8 s  access_log /var/log/nginx/yourdomain.log;
$ K) A+ V* r" ^; J0 r9 ~9 i; r; w' C! X" n0 F
# j6 s, V; v! ]! E! J9 W5 q, R$ `
  location / {. T/ {5 E0 H% D9 d+ \( l0 `
. q+ P4 _5 y- @7 z
! i8 [8 l4 o/ P$ a
  modsecurity on;
' \% Z5 J9 l1 D9 U/ J  modsecurity_rules_file modsec_includes.conf;* |/ R5 Q6 K2 o
  root   html;
* c- A; V9 o) Q4 p& b  index  index.html index.htm;
# ]1 J; U% Y+ R1 N+ ?8 `/ ?& J}
+ B& U! ]+ f* ]}
3 v/ T7 T* }# ~3 X- D验证Nginx配置文件  C0 Z. b  ?, W, @$ d9 f: {6 z& E2 G+ H
$ /usr/local/nginx/sbin/nginx -t
) X. x/ p; c$ X( M6 Knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
' b: A* P1 q$ ^/ o* enginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 U3 Z: D4 E; m5 V1 T6 L* Q
启动Nginx( i( a/ D3 d" W* R
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 c9 C9 [1 E/ q/ r! }
$ e/ p5 c( Y, x! A4 p, 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能够战胜更多复杂/未知/混淆的攻击模式。

9 s" N( }, N" G: D2 |% @' H

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-24 17:25 , Processed in 0.049112 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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