找回密码
 立即注册
查看: 6169|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
, ?1 `+ _! q' _& [0 [/ Q. _& p" a

' A& @( j& x- k' |* T9 o: I在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。- f  R; U7 u, A) N4 [- }, G3 a" ~

5 X" j2 @# X" Z. v" R! M9 R
& n- P/ v+ |3 u) H; k, y什么是ModSecurity
6 [- h8 G& w& b4 K. {: t- gModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 S+ N0 k6 y0 X- ^# b; s
9 h; d' V$ `5 i" w4 ~9 c" q: C8 x
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。3 {( g$ T3 C9 l, z5 p- V

3 O# W' O' q* A: E# f5 D$ u# U) s% A5 z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 {8 I3 w4 ~5 P% \7 K  X

# ?. j  V- ], z8 i
9 D/ m, V: T0 r1 k$ A官网: https://www.modsecurity.org/
# s. d- @4 M: r* }' H1 ~* u) t1 c3 ?4 ]# U* C# K6 w. Y  {
1 n. A8 S; _, O9 D" a2 f
什么是OWASP CRS
' w( W9 x6 y1 ^1 Z4 ~6 T# I+ N" UOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! c8 r5 U5 k$ d9 V( O* f4 f' z, e$ A- Y$ @

! D4 |* q2 j4 o* {, A( H# dModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。: |; m' A& K& L+ y, m

+ |: O$ t. N/ s' ^8 }4 b! W
7 ]: d" t7 Q2 `1 j/ G; ^/ LHTTP Protection(HTTP防御)
/ D$ Z+ x; D$ @9 [' ~HTTP协议和本地定义使用的detectsviolations策略。
0 _7 b: m) R; a" i' _" y9 _+ H  Y1 Q. r- P* U5 M

6 t* R: N" r/ `# d- ^( H' WReal-time Blacklist Lookups(实时黑名单查询): n0 [8 ?6 q' R5 T+ g6 H! Q% |; A! Z, [
利用第三方IP名单。/ o" {5 W- R0 t' I' f" g
9 f0 C3 I" }1 `: g6 T, V

- P4 \3 E# p8 gHTTP Denial of Service Protections(HTTP的拒绝服务保护)
; s" d  T$ U: y7 K6 t防御HTTP的洪水攻击和HTTP Dos攻击。, R- m9 T. n! }. K
6 _, Y/ w" h* k

/ G  I, j& \; q& E6 h' lCommon Web Attacks Protection(常见的Web攻击防护)1 J; H9 e" u) [1 g
检测常见的Web应用程序的安全攻击。! m' l& s6 d9 a+ A. @

3 w5 z9 T: C+ y, {& H( v. N0 W
- X7 r: x4 J" f) kAutomation Detection(自动化检测)
- H! e! S+ W5 T检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 Z' U( k% L( y8 c
% Z+ j$ n6 r: b+ W
8 X' w1 J. H6 f1 S: Y6 F' I# N$ cIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
# T2 d9 Y7 O( U  `2 E检测通过Web应用程序上传的恶意文件。4 L4 E/ O5 Q3 @

' b. }- Z8 p" A8 n- {1 w# N" @+ e
Tracking Sensitive Data(跟踪敏感数据)( y7 f$ N0 ], C4 `, D
信用卡通道的使用,并阻止泄漏。# j5 N3 |# m. q/ }3 |3 T# o) V1 @
& \" X" b0 k8 Y2 Y8 r7 E& G$ C. a2 E" ~
% \" I6 u: _+ i; ]( v8 a! q
Trojan Protection(木马防护)0 \; C6 R( W, X* o$ r
检测访问木马。
: N  i  o( n2 E- Z; K* i3 @4 J

; e- S! m% d9 f8 ~3 IIdentification of Application Defects(应用程序缺陷的鉴定)
. F4 a9 S; r8 F) {) z+ s2 a( H& J& J检测应用程序的错误配置警报。0 A8 W) s# c4 q0 }# g

8 c8 f( B1 S: _. u
9 J4 f' Z' |5 |% ~% K) WError Detection and Hiding(错误检测和隐藏)
% N" z9 K6 q* D检测伪装服务器发送错误消息。% u7 ~1 s6 p- n9 {5 b- e2 w, U$ Y
; u, Q( ?6 g! }( l

0 L; Z' Q& C/ T: J) D" \  y安装ModSecurity) c+ o  A$ d; b
软件基础环境准备( E$ i4 K7 X3 I3 e7 x' V
下载对应软件包* j0 u# X" w- D8 V+ W
$ cd /root
9 C% z3 h% t7 ]1 `$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
) n, U% ~6 Q( `4 P$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz* d0 f; N9 u7 O
安装Nginx和ModSecurity依赖包
* T& M) J" u  U( r' U2 L# W4 L+ e6 ~Centos/RHEL6 @3 B* D' L! N# L8 f9 o
* K6 K2 L# r, x  h' @$ u' W0 I

  u  w  B' ^, n8 o) `3 Y$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
' f1 A7 E# H0 E" A. R- PUbuntu/Debian1 N) @9 n1 U, u" L4 Z$ s
) {7 @7 x7 Z0 V6 i
- h* B7 Q' V5 Y+ }/ h2 o" J6 x
$ 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
, V; j7 G) J0 |$ R% E( }& b  n5 ~编译安装ModSecurity
0 R' b& z0 z) ~2 n. zNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。' d4 l0 z) ]7 V
& i9 ?3 J$ S1 V) n( N- x! _

: E' l* U( E: a7 K  q& T& K7 t方法一:编译为Nginx静态模块
: U2 x1 s5 E: o& m
0 A6 m, l$ ?3 y5 S9 i+ C- ~# Q  A) z% H2 n8 m2 v
编译为独立模块(modsecurity-2.9.1)
' {" J8 r" K+ h5 r4 ]$ tar xzvf modsecurity-2.9.1.tar.gz
3 |" I& q4 e4 s6 o: G4 g$ cd modsecurity-2.9.1/
& r- }" v8 n# f" X# C9 w$ ./autogen.sh! d% K$ m) Y% @% ]+ ^" |  D
$ ./configure --enable-standalone-module --disable-mlogc- P3 k/ C) f0 x: `- J
$ make
- P: c% v7 C( H- }5 n编译安装Nginx并添加ModSecurity模块
2 t& p6 H7 t+ o5 d8 f& ^9 ^$ tar xzvf nginx-1.9.2.tar.gz) s+ A! _' Y  g( s/ O; _
$ cd nginx-1.9.2" w) }) L$ ^) O
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/- j( P2 d5 }$ Q8 W; s
$ make && make install
4 l5 O5 S$ F! M! M8 l2 p方法二:编译通过ModSecurity-Nginx Connector加载的动态模块1 z) [$ q9 i, g9 Q! b0 w7 Y% p. }

# H. M0 b  l, @) B( n1 t# t' X7 c: r" i6 K6 J
编译LibModSecurity(modsecurity-3.0)0 R8 p9 x) P- w
$ cd /root
3 I6 V9 }6 O& c% R$ git clone https://github.com/SpiderLabs/ModSecurity* j0 l. [  x) J/ w
$ cd ModSecurity1 K8 R: P9 K, m! v  I& l
$ git checkout -b v3/master origin/v3/master0 W  N, u) l$ a4 d
$ sh build.sh
# u  ~6 i; S7 E$ git submodule init" S) ~  X  Y* K8 {. l
$ git submodule update
: @- i: L# T" V. Z# {( ]) @4 z$ ./configure0 \- ^* L* ~# y+ V/ N: x1 y
$ make' n" \; `% A. @2 L$ j9 P) F
$ make install  u: e3 g1 U. Z5 ?
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
/ b. e# X& ^- e4 J( A9 v: i  c6 ~  v& @; z: j
' v* Q' [9 {* O, \& \0 z; c$ r: ^% t
$ ls /usr/local/modsecurity/lib
/ m( o' }1 X; @8 b& {libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
. \6 W1 @, R) f! Y0 m6 E2 N' x! h2 g编译安装Nginx并添加ModSecurity-Nginx Connector模块
  T) k; @8 c3 \4 ]: T使用ModSecurity-Nginx模块来连接LibModSecurity! E- @/ v1 C3 ]3 [8 ]2 s

4 n' j$ W. Z' v4 n$ O4 p1 f* i- B+ u9 D0 [: p; [
$ cd /root9 h; y1 W. M. g* B! I
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
( a% M( |- w! O/ j$ tar xzvf nginx-1.9.2.tar.gz
. g$ i# c) F0 _+ r7 y9 o# `$ cd nginx-1.9.2
* {( P# r! P: b! w2 v  U& }  p8 R$ ./configure --add-module=/root/modsecurity-nginx4 P; r% Z' h4 X+ `1 d, M6 Q" h/ k
$ make4 t7 q3 r0 i' j" u/ E& v
$ make && make install
. Z8 P" L8 b0 \* v7 N添加OWASP规则
% F7 U0 F& j  fModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。* L, M/ S* r0 I% M2 f6 G. H

- O2 G; T; g9 _+ J7 ?: ~; K% q# \4 C! ?! V& o1 p
下载OWASP规则并生成配置文件; e7 `# Z5 U' v  M: z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git- }) K  Z5 ?* L; i- H- A+ m
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/" Y9 ?1 J  @* v" {3 |0 E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 m: a( d4 W5 y6 @2 M+ [$ cp crs-setup.conf.example  crs-setup.conf
. _) }) ^' O8 {% Z配置OWASP规则8 p* S4 X! I6 f/ R3 R
编辑crs-setup.conf文件
  ^+ |# ?  ~6 |, C; x2 _1 a3 c& |; [3 ~& M, [, y6 _6 }

: h* k( w! `1 H7 C* w* }  G3 Q$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf0 D5 X1 a; }# G/ j" U& @
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( @/ \1 Q" e% w6 `* M: L% H5 C3 Y$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- n, N* K2 X* V/ g" ^
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
9 h# I! ?* `% f3 H! I; v默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
( @/ i2 q+ l3 r5 T) C# l4 }
' C/ ~1 X4 t9 c: Q- x# T0 h
6 _  V! v2 ?8 u  q2 r& p5 b启用ModSecurity模块和CRS规则
" G1 J, E4 m- k9 {0 ~( c" R% |复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 q5 B& E3 F# F5 q: y$ J! O% l3 d
% X6 H7 f6 z# {& ~5 ?1 o" X
; X6 P* [- A/ h& Tmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。9 G5 r1 M- y( Q* C* m  X. F

3 T. U$ ]$ D  s7 T* `4 G2 G$ @  E2 Y& c5 j9 X
$ cd /root/modsecurity-2.9.1/
( F( T* V9 |8 M5 b: @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
! C% V6 B; V# ^3 F& f$ cp unicode.mapping  /usr/local/nginx/conf/- u( V* u0 [& r( ~  @
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。+ `7 x. X3 Z4 d- J4 r6 c

/ X' g7 j1 Y+ m& V5 n( S) I  I
% E: w$ g- @" w' ~( \! V# u$ vim /usr/local/nginx/conf/modsecurity.conf9 A) k7 a& y  x$ W6 }# e0 e$ R
SecRuleEngine On
; [, p+ v: f  _6 f6 dModSecurity中几个常用配置说明:& }$ A* l' A, L: j. b5 g

5 G2 E" [7 {1 O  U9 [& b4 e' d2 S+ W$ |
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
* X8 X: H" u8 h% q! [( h" V3 A  V1 ~3 x* o. m) j

  p" M2 ^: z" u7 Z  Z& c2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。) X! n% h5 Y) i, {7 |
; A8 _1 j8 c3 S  l' o: K3 g- K

- n% Y7 C/ g5 Y# c4 W: [3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! t+ C# E+ w8 j- C/ H- Y2 Z' k: Z" U7 s  S4 E9 ^- q. T) b

0 D5 I" o+ M8 q4 c0 J$ h4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
6 a0 O% k5 d' {# U$ C7 w0 o5 [1 j4 R2 P8 g

/ w8 g' ~/ Z" s$ v+ |在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。4 j* i; k- |2 W5 c" ]# Z$ L, T

8 W. u' }; {5 e$ B
5 ^1 ~* w9 N9 L3.x版本CRS/ I6 K- m; j- C$ }
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 K8 d. f' W. r  d5 W  l
# 生成例外排除请求的配置文件$ w) N; o) \6 R7 b1 e, L# y# h. e
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
4 j; x4 }; s2 |* M$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  t7 W) y( l8 g1 y
$ cp rules/*.data /usr/local/nginx/conf1 y. F1 X: O2 N" k- W
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 C3 q% }1 y6 L- K" I- A

/ H9 v- q( F! C$ Y3 G
4 O4 g* `8 ^; N$ vim /usr/local/nginx/conf/modsec_includes.conf
3 o8 y( _) W" f, v6 j
/ v" d' i+ S% R; i/ O' p3 C[mw_shl_code=bash,true]include modsecurity.conf0 g  l5 X7 h7 r& f/ {
include owasp-modsecurity-crs/crs-setup.conf
# M3 n& B# h; n+ e6 ~! a# yinclude owasp-modsecurity-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf" s( K6 b3 Q2 Y
include owasp-modsecurity-crs/rules/REQUEST-901-INITIALIZATION.conf% e% a8 [. E% K& j: b
Include owasp-modsecurity-crs/rules/REQUEST-903.9002-WORDPRESS-EXCLUSION-RULES.conf
. I0 k  x/ F. ?' s9 D4 o: Dinclude owasp-modsecurity-crs/rules/REQUEST-905-COMMON-EXCEPTIONS.conf
* B) l0 c+ H" K6 v& qinclude owasp-modsecurity-crs/rules/REQUEST-910-IP-REPUTATION.conf/ e" A( V$ a6 |+ ^7 J+ D
include owasp-modsecurity-crs/rules/REQUEST-911-METHOD-ENFORCEMENT.conf
, y3 S# [7 v- Y6 C5 ~. qinclude owasp-modsecurity-crs/rules/REQUEST-912-DOS-PROTECTION.conf
9 l! V: J) [9 hinclude owasp-modsecurity-crs/rules/REQUEST-913-SCANNER-DETECTION.conf' e% p* x. M, Z
include owasp-modsecurity-crs/rules/REQUEST-920-PROTOCOL-ENFORCEMENT.conf% @. l* b( p, B3 ~- L8 h6 T
include owasp-modsecurity-crs/rules/REQUEST-921-PROTOCOL-ATTACK.conf+ J5 G! Q8 s, \- H1 o7 Z9 N7 s: m
include owasp-modsecurity-crs/rules/REQUEST-930-APPLICATION-ATTACK-LFI.conf& @+ {$ g2 k4 U" D, n! w
include owasp-modsecurity-crs/rules/REQUEST-931-APPLICATION-ATTACK-RFI.conf
; d  Q0 m1 ]- E4 o, Linclude owasp-modsecurity-crs/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf
/ v- U9 ]6 n/ I0 E  M  Sinclude owasp-modsecurity-crs/rules/REQUEST-933-APPLICATION-ATTACK-PHP.conf
& F/ G: r& Q( I9 ~: Oinclude owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf* j% L- R2 }4 X; v3 q
include owasp-modsecurity-crs/rules/REQUEST-942-APPLICATION-ATTACK-SQLI.conf
  V! e1 h* ~& A) Zinclude owasp-modsecurity-crs/rules/REQUEST-943-APPLICATION-ATTACK-SESSION-FIXATION.conf: V' o3 i: ]0 f" A) J) u
include owasp-modsecurity-crs/rules/REQUEST-949-BLOCKING-EVALUATION.conf* w& R' I+ _4 e- G0 a2 y
include owasp-modsecurity-crs/rules/RESPONSE-950-DATA-LEAKAGES.conf
7 X/ g$ E* l  K$ D+ linclude owasp-modsecurity-crs/rules/RESPONSE-951-DATA-LEAKAGES-SQL.conf* g, I' F; p$ x  x  y
include owasp-modsecurity-crs/rules/RESPONSE-952-DATA-LEAKAGES-JAVA.conf7 \& `0 s  o; V( f2 L% z
include owasp-modsecurity-crs/rules/RESPONSE-953-DATA-LEAKAGES-PHP.conf& p/ ^. ~' r& a
include owasp-modsecurity-crs/rules/RESPONSE-954-DATA-LEAKAGES-IIS.conf
) p3 K# G3 S* r6 k$ z5 T) h$ C# `* @include owasp-modsecurity-crs/rules/RESPONSE-959-BLOCKING-EVALUATION.conf* U) [, A& K3 z* c# _' m# Y
include owasp-modsecurity-crs/rules/RESPONSE-980-CORRELATION.conf3 K+ O) \" e" `5 s8 F
include owasp-modsecurity-crs/rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf[/mw_shl_code]4 X( g5 v- _  D! w

% \& P6 Q) D+ g1 p1 t( ]% Q注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。; J( n( b  ]% {' P: {- k/ b, k  D
' R. J- Z4 a4 m3 p0 @; Z

  |8 C. Q$ l6 ]  {  v: N3 ?配置Nginx支持Modsecurity
6 I9 m  B! e4 M# j, d) I启用Modsecurity
/ l3 j2 j( A2 K2 S使用静态模块加载的配置方法
5 A* n4 l# o6 r4 a$ ]9 g5 O在需要启用Modsecurity的主机的location下面加入下面两行即可:! s4 \& F& R- S7 U( F, Z
& V6 `' I7 A* G
& j$ a& ~8 r' u3 s) }
ModSecurityEnabled on;9 L0 \5 |4 n" o- T) n6 v
ModSecurityConfig modsec_includes.conf;' I1 J6 x9 ~  n
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ \& k: |: o7 S5 K% s! P" f- N( e1 S' x9 l
, E/ o" x( i' o; a* ?
$ vim /usr/local/nginx/conf/nginx.conf
3 u# o5 Y$ j, O+ U4 `! X
, R) C6 i8 N+ i; o# ^) [2 }) }1 @0 E$ w
server {( T& M" m. I9 K# }9 L7 {$ y
  listen       80;( R9 h# ^% ?* N" Z/ z; t
  server_name  example.com;; j7 P3 ]9 M% x* g% i3 y

8 |* }8 T1 r2 W4 i1 \
" ^& ~4 E! ~3 \2 J6 N  location / {) L& O8 X* S% u2 d: W" d
    ModSecurityEnabled on;. M3 x' @! a1 d* u# T" |: w& e
    ModSecurityConfig modsec_includes.conf;
  m; e# o8 R6 h' S' \! {    root   html;
* s" Z( J& K  M6 K1 m0 W4 ^! N    index  index.html index.htm;4 U0 j$ h: e. F' p8 x9 y
  }
2 P' |6 @' h9 R7 _}$ I7 [, @3 d: |4 m
使用动态模块加载的配置方法
1 p5 B8 P2 a" I+ c在需要启用Modsecurity的主机的location下面加入下面两行即可:
" N/ O1 W; P- U5 c8 a  E5 R$ z+ o/ X2 q4 y- Q% |
/ I. I- M8 e3 `' q  d4 W6 {
modsecurity on;0 k% I* ^1 p; j2 @/ R7 L. G
modsecurity_rules_file modsec_includes.conf;6 O2 i' w* @5 p" R( E1 X8 _  H1 ?
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 C0 T( p+ n6 |1 F. ]

/ v' G0 O8 Y+ U% v7 i
$ l  \0 G1 f% q8 ]8 [9 ?! m8 X$ vim /usr/local/nginx/conf/nginx.conf3 ?  Y0 p% b! n& D

7 n  G% X2 r; U  w- @- g
' M- U8 y# F( P  f# c" B' V0 xserver {
- X9 E8 Z% x% F# Q- R) q  listen  80;- B7 O. c* O' U1 U" e5 f8 z
  server_name localhost mike.hi-linux.com;( {7 I6 w+ _5 v- t+ `
  access_log /var/log/nginx/yourdomain.log;
/ v1 H1 s- w  Q: s6 a$ S
; z0 y( J* b  Y2 y0 _- x2 g2 G9 n& y0 U1 }) [8 T
  location / {& \8 ]# P* q+ ^1 F: Y  F6 Y

% _6 o+ m  U# h& v5 ]7 M9 T: c6 P" a- U' U
  modsecurity on;2 b1 C3 v; d5 P6 x4 i4 P
  modsecurity_rules_file modsec_includes.conf;
2 u2 r# Z6 s2 Y, v' ~- u  root   html;0 E# z# b  @$ h  ?5 ]* z
  index  index.html index.htm;8 x3 k, x' L; a4 f% b* z
}# j: d6 e. o6 Q* D7 S2 A* x  U
}
% M- q- p  L' |- _& ]2 h" e验证Nginx配置文件
4 W5 t  Y3 C6 z* e$ /usr/local/nginx/sbin/nginx -t# p( a4 P- R: i2 _5 g
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
) D# }, h9 j; N' Qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful% X* q! c- ^* F: p7 Z1 G- d: ^' j, e
启动Nginx- y0 ]. ]9 k& _. k+ C% y  S, Y
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
; n% k$ H- {. J3 \/ ^: p2 k  r- ?/ G. C. }% l, v) m7 @

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


  K2 B4 D  l7 L

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2024-4-20 03:00 , Processed in 0.149940 second(s), 31 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

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