找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10914|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 E% \1 D/ g. F" @# B. R8 y/ U0 ^) i! [

3 z- m3 l4 q8 p$ ~) X; P* u* B在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。9 _8 E/ H  @9 n9 T% U% I/ M5 p; c- G

0 \3 g) y( g4 y- c1 [, b0 M$ u& G; D) \
什么是ModSecurity1 S; x! r9 [, Z# \1 e! _6 E! Q
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
. R$ `  f: H% Z3 {$ J+ L
& |9 ?- W& j4 t+ @
" E8 _5 T8 T6 z0 G* @9 u4 pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
' H' ^5 G% t# G- a8 b% M" W
1 ]/ p  l* V0 [  G: c, j0 }
: T2 F$ [/ r' z' j. g. }7 N1 G# XModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 A( Q/ g5 v. [& {" K. E& F
# M, L3 W: f6 ~# L$ L5 b
( P3 r4 ^# h! _3 s6 @0 ?* y5 @
官网: https://www.modsecurity.org/( c3 i( r' h: M2 L9 g
# w3 i$ v: h( D' m
) f- e( Z3 A1 w: ^. E
什么是OWASP CRS& M" L# ?" O1 Z0 }2 I* `
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
1 G9 O6 N! K- P- E! _
& O" Y* L" ]0 J; ?: m  d
4 e4 }! O" \; I  ^) w3 zModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
2 C9 M' s1 m5 n- {# x9 O- p; \
0 o) o+ I( S. w2 q7 v0 R% \) a# N/ D& L; J8 H  C- x3 \, I6 P
HTTP Protection(HTTP防御)% I1 B# _* y5 w) T* f& S4 c' J7 ~' A
HTTP协议和本地定义使用的detectsviolations策略。4 D, T: X3 k) v( i) _6 A
6 R; z8 }; j, S

- {0 \& e) d- M- |& IReal-time Blacklist Lookups(实时黑名单查询)
, U0 l7 U7 {- t" E# c利用第三方IP名单。
# g5 y7 K* O8 _
0 @' c/ e( Q& B: ?! f6 W7 a# G$ z" h! H+ n& p
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
0 a/ p$ r# o; N  \9 Y防御HTTP的洪水攻击和HTTP Dos攻击。
& d7 |% D1 V3 ?/ h) g7 |- B
, F0 Q% O4 K# I; H7 U
7 w- y/ U" E. ZCommon Web Attacks Protection(常见的Web攻击防护)+ {8 D- X. e$ y0 x3 I% R
检测常见的Web应用程序的安全攻击。
# r. W; B' b1 u! b% t9 F0 c' x- B6 _# P! J2 w( \; l# ~/ |# |$ f
5 W0 u9 E% O" o" _! Z2 r
Automation Detection(自动化检测)
1 T2 U9 c* p+ q2 U  @7 N: S8 i检测机器人,爬虫,扫描仪和其他表面恶意活动。/ p& N. m! @/ q+ f9 R0 |$ B5 J
' M! c% L1 ^! }- L  D& D$ w' z

  c! m! k8 Q( m5 kIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
6 a" y1 S( r2 M$ }* g3 M7 g7 c# @检测通过Web应用程序上传的恶意文件。
2 }* Z2 r" t" J
, Y" r, [7 D3 r, Y1 H+ P( q) G) A2 V, J% o5 m  u
Tracking Sensitive Data(跟踪敏感数据)
$ J/ N2 i3 O9 D" M信用卡通道的使用,并阻止泄漏。% A: r0 N8 L$ G: Y! i+ i* z0 Z( c

0 p* y/ f( U" A" g5 |, t3 q( N9 S6 C
Trojan Protection(木马防护)
! T* ^0 \2 w; q; A  s检测访问木马。2 _- q6 X" H: {& Q/ B2 y

4 P2 m. p2 w/ T9 ~3 X. j" B& {" |. A. f
Identification of Application Defects(应用程序缺陷的鉴定)
2 }2 ^, [$ N/ N) I4 V: d. |检测应用程序的错误配置警报。! x2 S4 F1 a" R5 O* P7 n- B% m8 g
0 X! R% {7 C, V& A- _; D. Q% c
, I  D& y3 j% L  v; Z
Error Detection and Hiding(错误检测和隐藏)
6 r- v& s* F' R. P. B) U* u; E3 X检测伪装服务器发送错误消息。; e& f1 D3 \8 \
4 s: ~' E4 @& E
+ X: d# j& V  t# t
安装ModSecurity
! W% y9 H6 p" O0 C( Q软件基础环境准备
% W7 |( l; }4 p& v) Y/ O下载对应软件包& d$ Y: p# t8 E5 A- e7 S. W% D
$ cd /root
: }. s7 `6 g: x( U0 g$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'7 p: i  m: r# K( W) ~& A' M, _7 P
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz0 }4 q! ~0 I  W, I8 y# }
安装Nginx和ModSecurity依赖包
: X1 G$ N- b0 ]7 K8 bCentos/RHEL
- ?& t+ m1 `) K. M! z
. ]' b' Z) W. H4 f, ^8 @/ V, Z: T
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
  R$ o: Z1 r  ~8 G" E, O8 D2 e' |Ubuntu/Debian
3 H( H# F! n/ c9 B( M  R: [; \$ Y( v0 t6 R7 Z( G

" t) s! P7 Q6 \5 V% p0 |. g$ 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 H$ n' M- V) Y! p. g- ^: B. H编译安装ModSecurity
! {5 J/ r" b7 `" D* K- ?4 sNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
  Q/ U: Z8 q& W1 C4 X0 }
2 \+ S2 j* P* z9 {; z4 q; |6 S6 @
6 J4 B) \  P  i8 a方法一:编译为Nginx静态模块- h7 N+ Q3 ^3 y

& I7 b- {+ Q) y4 U; \' w+ J
. {* m; @$ K/ x编译为独立模块(modsecurity-2.9.1)  `: d$ w7 M* H: U% S6 m
$ tar xzvf modsecurity-2.9.1.tar.gz3 U! R7 U  s3 I, W# r
$ cd modsecurity-2.9.1/
% e  Z1 O, s. x9 }% e$ ./autogen.sh# U& h+ N: B$ V8 @* a- r% X
$ ./configure --enable-standalone-module --disable-mlogc
* q6 Q) G4 d' U+ i, H, s7 T; W$ make6 m5 G* U+ z$ m, q& W7 ?& f
编译安装Nginx并添加ModSecurity模块0 a5 }2 z" C# \- P
$ tar xzvf nginx-1.9.2.tar.gz. p3 {' _- D$ |0 j) ^2 a$ ]+ c
$ cd nginx-1.9.2
+ T0 Y4 u( O. @. t: H$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# E+ X7 o: Q  v' @% A$ make && make install
1 j1 U- k% z+ a7 X+ u( u+ d. Y  P1 H方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- h! a: {! _9 |7 |6 O' j" n& [' K5 h: s$ q/ X4 @

; w3 F. q/ ^0 p编译LibModSecurity(modsecurity-3.0)
  p& I  y. {9 u$ cd /root& ~  I! m% |+ s1 e) j+ J- L' i) q
$ git clone https://github.com/SpiderLabs/ModSecurity$ S3 Q* _. ]2 \! m$ }
$ cd ModSecurity
/ [; ?3 G  C7 R0 G+ I& s$ git checkout -b v3/master origin/v3/master! z8 f4 X% i9 j9 a& e! F
$ sh build.sh
7 A2 _/ Y: N( f2 w8 }9 H  d# n$ git submodule init7 R& I$ O, ?- }8 Y
$ git submodule update
: n5 v  @9 _$ Y( b1 Q# P' R$ ./configure' X# V: V: S- n9 d7 V
$ make
+ M! y: I* O4 s5 S) m$ make install4 ?. a9 R$ X2 h8 g1 ^8 U: x
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
3 |% b; j  ~2 `% g- z5 C6 m4 H
8 h- ]! G. v+ ^& Q
0 z2 ~2 a; {3 Z5 `  L$ ls /usr/local/modsecurity/lib+ K! l9 ~- ]. x7 \
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
( F) B/ M% t+ o- D3 G" G8 I: s+ N' C编译安装Nginx并添加ModSecurity-Nginx Connector模块( N( P) J5 v+ j, t* c( T5 e
使用ModSecurity-Nginx模块来连接LibModSecurity( T6 X: F/ l' `

' V+ M& p# O# [2 F9 y/ `/ f/ W
: H2 j3 ?0 M) K+ G* g; Y$ cd /root
% l. r# E; q+ Y* h+ O$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
5 ?9 v: H5 i, b6 W: I: j$ tar xzvf nginx-1.9.2.tar.gz
% i" v, s9 K' |+ D. q& ^  N0 q9 l$ cd nginx-1.9.2' i; c2 X, O3 L$ C6 J3 o" S
$ ./configure --add-module=/root/modsecurity-nginx, Z' J. Z  c+ Q6 {$ Y
$ make4 Z  s- _  B1 E- e! C
$ make && make install
% k, d. v. x% e0 H- V添加OWASP规则
7 q8 e% X1 y$ Z8 C. B0 JModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。, S. G# y- h* l) T) ^4 h' k' {
" h* d2 B$ d4 R& j5 h! o  U0 L9 {
% }4 q3 `1 W0 o3 h; ?7 b
下载OWASP规则并生成配置文件
( J3 n) f+ i# p$ t& Y$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
* ], t! F/ ?% L1 S6 u$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/+ \' v( X7 l% Y1 C" N! ?) g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' e; ?8 l4 W+ L) }! s) S+ e9 K$ cp crs-setup.conf.example  crs-setup.conf
  \2 t! W- a% W/ c* W7 E+ M配置OWASP规则" m! o: N2 J" v  O) D& p
编辑crs-setup.conf文件9 H. z. i- o2 G

) t7 {* [& J: y3 d" N: z6 p) P$ h2 _
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
' c) T' L2 K6 W) |* f: h8 k' ^$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% f* K/ [: y$ R( I! F3 f/ L
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) O  f( m% Q1 g. }! q! y$ K" Q
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
, K' `$ I3 B& X; y默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
+ H/ X) a+ G, B% q) x: ]% v1 h5 o# i

/ @/ l& @" H8 _* Z5 e启用ModSecurity模块和CRS规则/ W5 J  `$ d8 R0 F3 ?2 h+ g0 }, M: `3 Z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 [& |( n% ]" Q* u, P6 L
3 c. f4 G$ z# k  f6 W

* ^2 c9 q- u* ?, G9 T3 J# l* Wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ h$ T- E- A, n' J  T$ m0 J

+ J+ H; |. t, n# H, B
$ i' d% `) D" n6 T% L$ cd /root/modsecurity-2.9.1/
/ c+ f' \" m; |2 V/ Z  e- @$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  : a( E) {# g, m3 L$ A; h( |& S% ?( G6 L" R
$ cp unicode.mapping  /usr/local/nginx/conf/
$ e7 s5 m9 ?; M将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
! c+ D& M' `) p! L' y2 P9 Q& D8 S. Q* _- i

5 t9 U3 y- v7 t$ vim /usr/local/nginx/conf/modsecurity.conf0 \8 \: |' s) y
SecRuleEngine On$ C! w  L. Y% |( h4 z" Y! o" R+ l
ModSecurity中几个常用配置说明:
+ L, k) ?1 e1 h5 A
8 S$ J2 e9 q9 U5 |
. J) k6 Y1 T4 N/ d. [% {8 |1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。: Y% d1 L$ ^# B5 p9 ?# L# o0 r6 v

* H7 w+ a9 I! }3 i3 b. x( Q( d( J1 e# V8 _& B
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。, `% g' I" ]7 P  e# a, U( ^$ T1 K

/ N% q, L% Z! M# p/ t# V" a
/ S, C% E0 F- j& O' H3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
/ l0 g; F1 F( a: O% Q. R" F- i  e, ?8 c' P

( s& H3 T3 ^& b7 m7 x4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。, u! i' }+ l5 Q
% g9 q" O% E. a- D" s
0 I% r1 Z& [8 d! u" w3 W, Y
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) ^' H, t+ }( [9 R: i" d
" j6 z) I+ i" F6 N* m7 c
$ f- e; p0 Z) [- \2 t' X3.x版本CRS
- I+ U+ Q1 j7 H2 w* ?$ cd /usr/local/nginx/conf/owasp-modsecurity-crs, Y2 [$ L* W& E4 S
# 生成例外排除请求的配置文件  m' y9 p2 r- R) y
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- |9 v6 e! p3 Z! `$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 B) }) J/ b0 s! ^
$ cp rules/*.data /usr/local/nginx/conf& Y1 T1 m* v& N$ c
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。) V" {; b8 I- W5 p! O# [

5 h+ A1 c3 j7 M7 r
% F3 d. k7 z( A) e& [' ?$ vim /usr/local/nginx/conf/modsec_includes.conf0 u! ~1 o1 G5 l# S' _5 e3 _

/ ^" j4 i: v6 O1 W5 c0 F
[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
1 }! ~1 {7 w: o5 [
; V3 s' R: o9 X4 ?2 b$ C
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
6 `1 t+ d* m# a/ y2 t/ u9 e: V" ^3 q

4 W' L# @8 v- D1 U) y+ X6 T: ~配置Nginx支持Modsecurity
! b& B& N+ i8 e$ u, r启用Modsecurity
7 v) [' `. R" @7 |+ ?; A4 z使用静态模块加载的配置方法0 @1 v# E: K7 |
在需要启用Modsecurity的主机的location下面加入下面两行即可:! r% C& @+ d" S, }8 o
# n1 k/ S8 V3 l# C8 k# E
: O0 V4 m+ u& g- I& W
ModSecurityEnabled on;4 @6 B& U7 c& f. \& p  V, i
ModSecurityConfig modsec_includes.conf;
, U1 t0 B2 f' U& g修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ C$ ~: v# b+ ]$ z9 K7 i0 T1 C7 d) m! E5 D

' }5 w2 U2 Q$ L$ vim /usr/local/nginx/conf/nginx.conf" \( c/ D9 t) g1 q% }* O% ?
3 A# m; j  R; _) g1 V- D# E

' G( e6 g- e* vserver {
! _2 R+ A  d" C4 C& o  listen       80;$ H% X8 O0 r2 s3 B. W
  server_name  example.com;
* y" k0 i$ s8 l/ A  d
$ Q4 T" F# _  s2 c' }0 k
0 C, k5 w0 z9 |7 F/ ~( p& G  location / {8 W- ~/ \( {7 z3 A( S) G1 b
    ModSecurityEnabled on;
0 [7 Y1 q- K/ Z' }. P# _; l    ModSecurityConfig modsec_includes.conf;7 ~. m/ n- B/ Z1 t& j
    root   html;. Q, P2 `3 W+ A8 m+ t$ }
    index  index.html index.htm;
: \5 S  c, D% K/ r, v! u' W  }
& |7 Q) N1 S  @/ B# G}
% F3 n0 N1 F5 A9 b使用动态模块加载的配置方法
: J" V+ g# ~7 E/ @9 D5 |9 b1 g/ {在需要启用Modsecurity的主机的location下面加入下面两行即可:+ o- \6 S. h0 m/ P. ]

7 w2 j( s. |& K$ r: p
) `2 x! `) K! b: {3 J# E! ]modsecurity on;) l- r/ o: B& m. w
modsecurity_rules_file modsec_includes.conf;
# ~% i; b9 e' d修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ R: c, ~% }2 E

* G3 s1 c/ K! @7 U& Z9 S) |7 d3 n; z" g
$ vim /usr/local/nginx/conf/nginx.conf
" y7 s9 I( u5 r* x$ P9 E9 a2 @4 Z  X
! z/ J# G" C0 Q7 ^) }
. ]& e( r* h* G4 S. d1 a3 Kserver {1 X: o0 p- i1 `  t3 N
  listen  80;+ M" I% E& i# v
  server_name localhost mike.hi-linux.com;5 r# t  r7 H# }/ j9 g
  access_log /var/log/nginx/yourdomain.log;
6 Y; c' n8 l- a+ Q, S
$ r( y# ?) T3 E9 G+ q; t, l- S5 I$ W$ `
  location / {
/ t& u+ f& f8 @+ U" N0 x
0 {8 K: d1 Y" f; v8 g% C, H$ W/ b5 H
  modsecurity on;' p; d9 k) O! g' v- Y: O
  modsecurity_rules_file modsec_includes.conf;
6 d" \+ |5 w: g2 B8 j) \- Y  root   html;2 X$ G3 k! g7 }" b9 u* l2 S( \6 W
  index  index.html index.htm;7 ?9 }: q5 ?) q0 D* X( I+ ?/ {
}3 j, A' }5 o* K
}
, z# S' {  h' ~* Q7 Q验证Nginx配置文件! S& G$ i: E8 w: Z0 k0 M
$ /usr/local/nginx/sbin/nginx -t
$ g; U6 h- P' @1 [nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. k1 ]! f+ e& }1 B
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
& k5 X7 x; W' G! \启动Nginx
6 E+ J2 p5 |$ x$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf+ O* y: D6 [) f6 Y. q( T0 ~& r5 V4 _

5 y$ D0 b! z' R' A9 O$ 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能够战胜更多复杂/未知/混淆的攻击模式。


) I; z5 }% ~) C

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-23 02:47 , Processed in 0.081261 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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