找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12340|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 b5 P& n0 ^& E9 s
( k+ x0 E" K' g
! H) ~4 g5 l9 J6 b4 F! |在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
3 m3 _3 Q! ]+ p( v5 [
3 z  O+ s1 z8 K0 C( t2 U6 }) _* E0 N
什么是ModSecurity! m- m1 u1 r! Q) q, |
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: |6 @- s, H% ^8 a9 v* y9 d! G
9 ?! l0 w3 B; z# w) S. I) s* T

# U! `4 Z- [. t% O" |2 f. ?5 NModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 d6 X- [' t6 x& U6 l0 B# N4 j4 ?1 x6 S6 y6 h" M+ n3 W
- A" Q2 W: ~0 W# T; e# Z0 W  A1 ~4 v
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
7 ?6 }; R0 c( b! [
6 u3 M) \. k, {
5 E4 J# I7 J$ m3 `4 `7 [- \8 A官网: https://www.modsecurity.org/1 C2 J% Q2 P9 Y, |/ w. o

! u' ^' P7 i- C# u+ r$ R
2 E+ z6 e4 Q& e+ t什么是OWASP CRS' D; M$ h. J% A1 c# U( z6 d2 `7 {
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。: u; b7 W7 X5 c, w; T( S; a
# {7 n4 W% |' a$ I/ C

2 o/ B2 E1 ~, gModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。1 |4 m; @3 y" h7 r2 Q1 y
8 b' A0 ~' M/ A* j9 j& |; X
4 Q3 H) z, W' s) N( T
HTTP Protection(HTTP防御)3 X  R, x- f* ^. l5 Z
HTTP协议和本地定义使用的detectsviolations策略。
6 ?& X$ K6 E% a& Q7 O: K3 S' Z4 U: s6 C
' R8 d/ V: u3 }# p9 k7 V
Real-time Blacklist Lookups(实时黑名单查询)" N! e7 i# w' ]; c5 l8 q) n
利用第三方IP名单。# T3 i) b; N4 B( X7 u
( [0 Q0 V0 `' g0 N, w  g4 `  M

- ]5 @& H" z7 U0 P: r; lHTTP Denial of Service Protections(HTTP的拒绝服务保护)3 R! \1 [9 M6 H4 {' M7 ^) {% x
防御HTTP的洪水攻击和HTTP Dos攻击。
6 t+ b5 y4 x. P; j$ I# H6 l6 B+ E) |% p) v0 Y
  w6 i" r& M) f" U0 x
Common Web Attacks Protection(常见的Web攻击防护)4 E+ Y3 A  f7 g$ i! J' }
检测常见的Web应用程序的安全攻击。
, h& J8 U6 a0 S& Y9 E4 o7 r: Y4 J  A" K8 Q7 g8 T- `

6 x# q# B: _; EAutomation Detection(自动化检测)
2 W) V9 f0 T7 e. s; R. _检测机器人,爬虫,扫描仪和其他表面恶意活动。
: V4 z: i2 {7 p4 B# e0 z- N6 M3 V
! {! j9 A7 j* L) _! ^1 I) |
% W# P7 l. J* V- n8 v  ~Integration with AV Scanning for File Uploads(文件上传防病毒扫描)$ \# W  u( C8 y) ^( V. V
检测通过Web应用程序上传的恶意文件。
2 f! f; u' T) ?7 {* e) ^/ D( D. \# ~  X, c

: ]& Q9 }1 @5 B( rTracking Sensitive Data(跟踪敏感数据)
* m4 [3 ^6 [+ m% T信用卡通道的使用,并阻止泄漏。
2 \# E9 Z7 j& ?0 H1 H7 l4 s+ s% u4 h7 {7 V, O0 E- \, g

4 C( Y8 k3 W9 o. Q$ L! h& {Trojan Protection(木马防护)
9 m; T* ^3 [- q) F: y; @4 {7 [检测访问木马。
' i' d- F5 o* K+ M/ o5 J0 X$ P# g
9 N1 J7 ^7 @: f* s' r- }
Identification of Application Defects(应用程序缺陷的鉴定)9 n# F. Q7 Z" i
检测应用程序的错误配置警报。- G4 @& n0 o) k- B) {: l: e% G
6 U- B3 Z% A0 K1 K- A  \

- {! C' k+ P$ E; c. kError Detection and Hiding(错误检测和隐藏)! z# @7 f* ?+ D4 O$ }$ s- F8 T
检测伪装服务器发送错误消息。8 \3 N$ A  Y9 _; e6 V5 y6 \
0 V) l* f* L; W4 S9 f' J" J9 I$ l. r- S
: K& J9 A1 h* r/ x( V
安装ModSecurity
+ x( L! k& Q: r! b1 A6 T软件基础环境准备
& u. i1 Z" M' A8 a1 ]4 Z" E下载对应软件包2 B: V& S* Y0 ~4 w8 P! B
$ cd /root
: i- u1 n' `6 F! X0 R3 c* p/ d/ R$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
% ?3 D3 k4 Q" k% V$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
: m! O4 R, u0 J, f$ G, \6 C安装Nginx和ModSecurity依赖包
. R5 U) e4 {* |( O0 y* s/ OCentos/RHEL
2 f: y6 h6 E$ ^1 j& B$ B
: f) ?' {+ l7 Q4 O7 |0 Y
0 w) [' ]) w# z6 R/ O$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel7 G2 P: `, F3 N/ K1 X
Ubuntu/Debian
) S! z; n9 j4 a7 R7 e: \& n
* N, y4 K1 a$ y7 f- D4 u
* a  _$ A$ t+ j  J) ]' ~- o$ 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
- i0 C  F) ~- o1 J! \编译安装ModSecurity% K1 V5 `; I6 l
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。" n; F" a+ i4 _& d4 p

, m+ K5 [; b( H6 E% p, ^1 R
4 ^4 I* B/ `6 P+ J0 j* v6 t) n方法一:编译为Nginx静态模块% ]% q9 {2 E0 G/ x2 M/ C

% Y) A' o+ b8 j$ ~- P! y8 X6 y8 s2 V
编译为独立模块(modsecurity-2.9.1)
6 S- p6 W  ~: U, r/ ^$ tar xzvf modsecurity-2.9.1.tar.gz
% z( ^: [2 H; x3 s, E  \$ cd modsecurity-2.9.1/
, s) F9 |" t. ]$ ./autogen.sh4 [' ~; S+ U' q3 q
$ ./configure --enable-standalone-module --disable-mlogc
4 W# k5 N) w& U# D7 T& e$ make
+ s' ~: B* R' s3 t+ r编译安装Nginx并添加ModSecurity模块
0 m, u; z2 N6 n8 K% K$ tar xzvf nginx-1.9.2.tar.gz
* f+ i( c; x4 Q& \9 z$ cd nginx-1.9.2
/ c$ N8 W% V+ t3 p9 @( y" L4 ?$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/& m8 N0 V* `! i+ A( I' [& P5 D3 z
$ make && make install
& ~8 ^; r' y8 Z# `1 p! ]5 a) S8 h/ s方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
( E# Q; N: G* n5 D! o3 Z/ M1 ~+ [1 I2 |; s0 x% W7 p

& d! P, r) C% I5 ?& B6 N# G编译LibModSecurity(modsecurity-3.0)
9 {! l8 Q* B& C: j6 R8 {$ cd /root
: w0 V0 x/ U& c/ s: _3 v/ t# T$ git clone https://github.com/SpiderLabs/ModSecurity2 s* ]9 Y- P. z% f0 L4 i1 i9 e# o
$ cd ModSecurity3 k2 Z7 V. q% o3 G$ ?
$ git checkout -b v3/master origin/v3/master1 ]. h/ E! W7 l! P, g/ p8 h. A# s
$ sh build.sh
) r  W7 g6 T/ c2 C$ git submodule init) ~; J( M; u; p# @. R3 B' I
$ git submodule update
; m. \# {. `( }4 X- Z$ ./configure
6 Z% Z/ G9 \3 N9 t1 X/ ^$ make5 M! J, T) Y5 P  S
$ make install
. E# L. z7 A6 Q2 x" YLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。6 \4 x7 M: M9 G: b% u$ c* }/ R
0 [" g9 r8 C: F
: Y8 D$ u( I9 p  _
$ ls /usr/local/modsecurity/lib; A1 @* n, }1 Y! q/ E) m
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
, e& I1 B) ?- E8 p9 U编译安装Nginx并添加ModSecurity-Nginx Connector模块2 F1 V( w/ L; i* l' M. d  U
使用ModSecurity-Nginx模块来连接LibModSecurity
0 I) c9 @( H. A& z$ Q1 p0 X
( q' M# y# S* M3 D4 V; m
2 X+ s! V5 |( I6 U$ f  k$ cd /root
  Q  p; J- c3 J0 S. g5 h$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
, N# D5 X  m' t! }6 c0 Q/ j+ G& @$ tar xzvf nginx-1.9.2.tar.gz, y4 [8 y# P2 V0 d' M6 J1 _8 u3 G8 {! _
$ cd nginx-1.9.2
' o4 c# F; T* s" z+ G/ g$ ./configure --add-module=/root/modsecurity-nginx6 P8 _8 I( I4 ?- y- R7 U
$ make
0 u) q. C8 y* y2 z$ ~' {4 P$ make && make install
. z9 I4 P; @! t4 U; ?添加OWASP规则
1 p  @2 R: K" S' t  c7 NModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 B. A% ?1 L6 z: z; X9 a. f4 I9 z/ J7 z$ O( y
/ Y; n1 c7 |2 b4 t( U7 ?0 `
下载OWASP规则并生成配置文件
% I- W8 J6 M9 x3 u5 f$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 L$ @" X9 J: n; x# z4 U: Y
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
+ W8 Y* E2 ?' U! R: e$ l6 E$ cd /usr/local/nginx/conf/owasp-modsecurity-crs6 u8 O! p  [, N+ d# R9 e$ D
$ cp crs-setup.conf.example  crs-setup.conf- ^0 G: n; W8 i
配置OWASP规则
& u$ S, z+ G/ b& s2 Z: S编辑crs-setup.conf文件( V# A$ f4 Y( ^0 W, |3 \

! K" }7 R* O+ R" ]2 M: M0 S$ Q/ A& r$ c, y- E+ m+ ?3 P
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf3 j: h9 Z# o! w% z( M  o+ z; S
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf" Q8 x5 x2 ?7 {
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf% k  ^5 z5 |8 h: ]0 g% D, F0 h
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
+ q$ ~: g! q8 Y0 K默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
. O  h; q# y. q5 W. I5 V/ s5 m3 U0 K* J5 Y/ T1 P/ l

  N& L, U; T- P; S0 I, ?) C& o* T启用ModSecurity模块和CRS规则
4 J  j6 s! \) y# [+ A; Z* `复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! w: _# y7 T0 f0 t  R! y0 Q! e% L' i+ s% A8 Q
$ P" R9 c2 T- {+ ~1 T' `* U$ O4 y2 A
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。0 Z9 \& T. Q0 q* c' B
) U# q5 b2 J; i5 p
" f, a( K" d, R, V2 c2 @7 y" ^
$ cd /root/modsecurity-2.9.1// Q/ r; I$ j  n3 W8 h6 x% m7 D5 d) ~: n
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
) T! R" {& n! f( A* {% U! _+ z* Z$ cp unicode.mapping  /usr/local/nginx/conf/
6 V  y# {1 x+ G1 R1 u8 k将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。$ P8 W: ?( _! `9 G: ^7 Q8 j

6 C# q! U: x6 V8 z1 K6 u
) F9 z; t, d- Y% ^; }( X, c% d' m$ vim /usr/local/nginx/conf/modsecurity.conf
/ ]+ A4 u9 B9 R7 ?1 _# Q0 zSecRuleEngine On
6 C) \+ z* Y/ A4 o2 aModSecurity中几个常用配置说明:. W. L. {) E0 S( F4 }' _0 S

0 j( p& r1 Z: A4 s& t$ d- o% r" H7 c0 e
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
' @- @5 z, X$ |. D% p0 i& O/ M2 m; P- Y7 J' I7 ^$ o" K- @  m

* p" @% _, Y# A7 W  L6 d! c) j2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。1 Q% u+ ~* X) N$ x' ?

* \- C2 t; Z3 f! c* ~0 c4 M0 f& @9 a) {, [
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
9 m& y& N( b$ U: X: h# N' g8 E: d$ f( t/ e

/ Z7 ~2 b8 L5 x7 S0 K  m) O4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
& A  O. B2 P- k3 q- x% ~
/ p8 I, p7 [# O" w+ K( i$ X
; u' {2 {: E. r3 g3 ^& r* T在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。; z% F' s9 u* f' j; s

0 d; A! }" X' |8 t! Q) Z6 `9 w
% K' u' I6 R. ^. ]3.x版本CRS$ t! ]! H+ P8 W7 Y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  c6 `' t( X; D( b# R) H; X
# 生成例外排除请求的配置文件. l4 s& C5 }0 m
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
2 a( f! @7 o0 }* x+ y$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf0 Z) H6 k+ N0 Z6 R( F0 b
$ cp rules/*.data /usr/local/nginx/conf
  J8 O' b4 g4 _' Y为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。3 y/ d, L/ }% g, W" C, _  O  S
$ [7 X) u: L9 P3 P& L$ o2 y

: c, C6 g4 U/ @# ?/ ?$ vim /usr/local/nginx/conf/modsec_includes.conf5 y2 Z* p4 P( G/ [% N1 ?0 z9 S

- Q) s: w+ [# E" @9 k; U8 T/ L# u3 o
[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

) u* E1 t5 e. b/ t5 b8 c4 `9 D$ A9 Q% ^. |7 ~6 x
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。' F: Z1 D" ?( u* v) `% c( O

* a1 M2 n9 L: e" s0 D* W% L3 S' B3 Y6 P5 v/ w& ~
配置Nginx支持Modsecurity4 K- f1 \% {' ~8 d' a
启用Modsecurity
( G8 p5 D9 P. n8 U使用静态模块加载的配置方法
5 [6 G  X- ]% |8 l0 F) u在需要启用Modsecurity的主机的location下面加入下面两行即可:; T  G, j6 u1 k$ {) `1 O  F! |

! _1 C7 [$ x4 ], _1 a% E' `% K+ \. u* N7 U2 H
ModSecurityEnabled on;
6 O  E. f0 w! O, X3 H9 OModSecurityConfig modsec_includes.conf;
; h: C% Y8 F2 H0 \$ E8 a& f* U% O. @3 j修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 i1 e! h, D2 `7 U6 ]3 [; @
  A# X! C4 d8 v, c% Z! D, f5 v7 t5 ]& X
$ vim /usr/local/nginx/conf/nginx.conf
* y$ W# D$ Y" t& _' _# |
! B& C, o. ~7 Z3 V6 [# @
! T  T5 D% c" N# b7 vserver {  R- U$ H6 p, _# y
  listen       80;3 R4 S; ]% ^( b# J& e) d) W; ~- ?0 y
  server_name  example.com;0 k/ ^) W- g9 c+ y) A" T

% w( U+ `: T+ T3 ~+ J4 T6 B0 Q' G& V
3 }3 `; c7 I! K3 \& ?: U8 f  location / {
: w& x. `/ H) F$ z% y0 R    ModSecurityEnabled on;) h8 G8 {  |4 j+ h
    ModSecurityConfig modsec_includes.conf;# G6 _7 J- n: B* _3 a
    root   html;
5 m2 a; U% \$ `) b6 t" n6 x! J3 R9 F. c    index  index.html index.htm;# d; N4 W7 E# p3 ?8 }/ `- X6 ?  ~6 z4 W
  }
& Q0 [- r. d4 |# ~% `' }2 |$ d& o/ }}
/ d9 s+ l, u# f& t使用动态模块加载的配置方法$ w" G" P1 y1 W3 q1 V
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. A/ v4 l7 w; m6 e- A# O, w! [5 L$ H+ ?0 e# i

1 K' }" T+ ^: B/ G( P" C5 w; ^modsecurity on;
' i% N0 O, {2 d: e0 H; t9 ?modsecurity_rules_file modsec_includes.conf;
* L( h, W* m& o. b* Z9 U2 w& j/ r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 D4 n  J/ j7 K; K1 i& B" m

$ Y. d( J7 o% B4 R6 \0 j
8 y6 w! p" W) G6 M; [$ vim /usr/local/nginx/conf/nginx.conf
1 w8 r- ^. w, u9 J
# e  Q  K. j4 v& G
0 ~  m; \" ]9 n8 e5 F) b0 m. Wserver {/ q' ^  I* K( Y# s& i% f& e
  listen  80;6 B( t* C5 f' P+ Y
  server_name localhost mike.hi-linux.com;  k- R1 k  F' k. x2 \
  access_log /var/log/nginx/yourdomain.log;
5 y" |) {7 ~9 L  n, q4 `4 e7 A. ?# R3 B- [. {6 o. g5 s2 s* R

1 I9 m8 \6 e% A2 E; b  location / {* l: B$ B* U( y7 Y5 A8 z9 K9 H
; W$ Z5 g+ _2 H* U
+ E" d% h3 L. [! P% s7 O
  modsecurity on;
9 g0 ^1 m" }' N: ^& t  modsecurity_rules_file modsec_includes.conf;) ~+ G& z/ i! J: R. s9 [& g' j
  root   html;
4 i; H- U! Q; @  index  index.html index.htm;  X) p, [. b; w0 Q
}' c% i/ P* m  P9 k* F
}
0 C0 S4 u' O( O1 T, S' H验证Nginx配置文件
! b' q: O9 @6 x2 w2 u6 f' \$ /usr/local/nginx/sbin/nginx -t
' x& M8 d) w9 [' f. c5 w4 B' jnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. o' f6 P/ S1 G% ]& Y
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# u0 U# O( H7 n: S2 @& Z启动Nginx
6 s: k) g5 u" R; \6 `$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 |3 V9 ~- E# M& B; O
+ L$ V/ o, r: B6 ^* t! s# o

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

- A, v$ _" z6 w) F

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-9-13 11:19 , Processed in 0.079914 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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