找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9630|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
; F* N) b" ^$ u4 U' W2 D  A8 e; l/ |' Y3 H  H
# w; b1 ]0 F1 ]) t0 D
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
2 r! b( [9 j1 G: Q8 r* X! S% _/ N! @) r. u. h

. x' x0 M7 O% m9 L' I什么是ModSecurity
, w& Z' A7 z) J; n% i# p0 f) i) WModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
) J* i0 `+ X4 o% G6 h9 Y# ^, {& U* c
9 B: s! \' T0 D( r  X
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
4 s) L, @: Q5 r/ R4 o$ }0 b0 [- z) I7 J

+ O! c  q2 p1 M3 T' b1 R. j5 d! N. EModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 Q( l9 b0 Z' C5 c/ Z/ t% E$ ^' ~* b8 L0 b" _( @6 @0 [8 ~

/ Y/ t6 u1 u0 _+ q6 b/ D0 z官网: https://www.modsecurity.org/0 p! N# @! G3 P: l

, A6 k4 k: V) O+ W. S
' K2 ^, i7 l! B; g( F什么是OWASP CRS3 Y9 a5 c3 s6 |1 z
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* i" N  C& W9 u. k# m- c

! p$ Z3 Y6 R/ R# m& v7 H' M# W
* a) ~, q* M9 \' Z( _# V2 DModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 ]. O2 l6 {! l  \/ K& s4 B
" h4 a  E; D2 f3 z, K2 f+ K  t7 e* \- \
HTTP Protection(HTTP防御)
4 Z* H7 E: q: A7 e; rHTTP协议和本地定义使用的detectsviolations策略。/ U; K; a% h8 |8 A# ~0 S2 Q; S; c

0 e& H! j& ]- {8 ~
  R. e% j1 A6 T4 o9 p+ z  vReal-time Blacklist Lookups(实时黑名单查询)
& E4 N) I2 |8 F1 X利用第三方IP名单。
6 u/ @" c  i% O
/ P1 n4 P' w& V7 B8 Q: G5 ^" W) j+ I: N9 n' s
HTTP Denial of Service Protections(HTTP的拒绝服务保护)/ Y6 p* N- k- t. L
防御HTTP的洪水攻击和HTTP Dos攻击。- a2 T+ b) L$ @6 D. w

- A& s3 H2 P5 G$ Q, A2 t8 m" X' [! D5 Q, M- V) o2 v
Common Web Attacks Protection(常见的Web攻击防护)% j" r, Q% R) }# s1 v
检测常见的Web应用程序的安全攻击。
( m# \9 a3 M( [' D! f2 D/ T( N2 x  @3 C8 `, F& L) h8 h& ^* p* E
$ S' N* z. ?$ l3 M8 s7 o
Automation Detection(自动化检测)
* m8 U) |6 G1 p! g, u, n检测机器人,爬虫,扫描仪和其他表面恶意活动。- F! ^0 ~- \* D4 ~
& A3 y8 R- N! [; t2 @+ R3 n) d

, z  f6 d3 Y- q3 U" g" h1 ?Integration with AV Scanning for File Uploads(文件上传防病毒扫描), {& G3 m5 ~- c' Z) C
检测通过Web应用程序上传的恶意文件。
8 p# y+ i# s- t( _0 F6 ?
. n3 _! z. d; p0 g: z0 A
3 H) P$ W" a9 ]& z& j4 yTracking Sensitive Data(跟踪敏感数据); i; E3 z( I: ~3 v' J
信用卡通道的使用,并阻止泄漏。
  ^# _  J& x6 k) ^3 l* w7 ?5 ?: @- x
  g& x$ R& K3 t# \
Trojan Protection(木马防护)
7 @7 K- R% l' ^! t检测访问木马。
, M; l' Z; v1 r6 }) J
, C1 L$ g# r2 T/ G
7 F* T( M; ~; \, pIdentification of Application Defects(应用程序缺陷的鉴定)) E2 @  G- r4 V# v1 z  Q- T- B! J6 t) z
检测应用程序的错误配置警报。
/ `' a6 J; w. x; t- l7 r0 }
3 ^7 T0 K8 M# e& E+ N$ g
( [1 i* m5 ~% C. mError Detection and Hiding(错误检测和隐藏)
6 h9 T6 s( ^5 d9 A: o+ H! ~检测伪装服务器发送错误消息。
, r2 O/ N. l" |
- U. p' @- y; D( X- m) `! o' Q  U- J  Q: e
" ]7 a* t, ^; S9 }( ^7 Q" x安装ModSecurity
; \7 E0 ?3 ^. L# M2 }* J软件基础环境准备1 I+ L3 t' W- B* J% W4 N
下载对应软件包6 l+ _/ W7 e9 G* h
$ cd /root$ L( E2 p5 {( G9 D2 W& b' p9 `
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
& r/ y, d9 L7 v& ?6 n# Z. i$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz$ U/ h- d, n) E5 h3 K! F/ Y
安装Nginx和ModSecurity依赖包9 r& C3 ]% [, |0 o3 y
Centos/RHEL
9 N! Z$ D/ b7 s: j! m$ V/ p  |
. |" ~8 b4 I3 C8 B8 O; r  V  Z/ x! h6 k) @, w1 H
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
7 ~4 L2 o  d# m% n3 mUbuntu/Debian2 N3 ?  G( `( a6 r; [
5 M. d$ N& S) z5 z4 x$ \7 \* z- a

4 j+ ^* c) q) z& {( i% q$ 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" k) P8 Q4 g  c2 }6 u" ?
编译安装ModSecurity
; v. G* g3 }. R/ a( T2 Z; c6 rNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 [, c! T2 m) j' z' L2 B/ {8 t0 H
3 `% b$ c2 G8 R& Q& |
7 W; G) d6 }3 N; z. `方法一:编译为Nginx静态模块
2 e% r: A$ ]# @
2 _5 M8 F, ?* E  H0 o% |- B4 e4 j& u# F- b- t+ X
编译为独立模块(modsecurity-2.9.1)
, }' P  q4 }5 x- B, A$ tar xzvf modsecurity-2.9.1.tar.gz
( X1 v4 W6 ^# S3 {8 G# {& P" x$ cd modsecurity-2.9.1/
8 ^2 l' C; X7 u& o$ C  G$ ./autogen.sh9 c* t* M0 Y9 Z8 K) S4 A* r- Z1 B
$ ./configure --enable-standalone-module --disable-mlogc
4 z5 r% V3 Z6 h1 t$ make$ y! X6 ]$ J. V. L' Z+ ^
编译安装Nginx并添加ModSecurity模块
% a6 I) B9 b& E- l! j$ tar xzvf nginx-1.9.2.tar.gz) @+ v/ C* Y3 `& i
$ cd nginx-1.9.2
6 e7 U' Y. C# D6 D4 i- @4 g$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
! B3 r3 N0 B1 S$ make && make install
$ V, q1 f! w2 e方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. e3 ~: }$ ^8 z* w4 B9 h2 b
8 z! e' l4 G3 j5 z$ M5 b: R
" x: l6 K0 A0 I4 {5 B. }( Q. v编译LibModSecurity(modsecurity-3.0)
/ l: I) `  J& F1 }$ cd /root
! s$ |- G/ U/ ^$ Z, p; `$ git clone https://github.com/SpiderLabs/ModSecurity
9 ^2 `0 \% k, x! Y- y" S. A$ cd ModSecurity
4 ^3 {% R: N/ e$ git checkout -b v3/master origin/v3/master# b" F% Q# D" ]' U% M
$ sh build.sh
$ Z, M# ]7 p. l9 }; _% l8 B$ git submodule init
, O; _6 b8 D; z, v: E$ git submodule update- g3 M/ m! e9 A. C
$ ./configure8 ?. \0 ?+ N) C2 @/ G3 x# }
$ make
- ~& [, P* j9 V5 i0 _$ make install
: C) R9 `  d+ W5 p+ B1 JLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 F4 x% Z0 F4 c4 T6 `; H' v/ B+ L7 ]  }1 x9 ]4 [. Y! R: Z+ D

9 Y0 k7 k* ]  a! L, y$ m6 R$ ls /usr/local/modsecurity/lib" r) C- l6 `. J4 {3 V
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
( N" o2 `6 E, X编译安装Nginx并添加ModSecurity-Nginx Connector模块$ T. |. l# B% _
使用ModSecurity-Nginx模块来连接LibModSecurity
/ G3 \3 u* W, j% r* f4 x
/ x6 V  H( F, e1 ?7 S3 v5 g" B
- T) S" Q7 n/ S# G+ T" F! z/ ^2 `$ cd /root. G2 g* p2 a% f: ~: V
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ l( x3 G  y+ Q: b) A8 V: s' ~
$ tar xzvf nginx-1.9.2.tar.gz( |& S: r, {$ I/ i! c/ O) B
$ cd nginx-1.9.29 l4 H! d( J4 b4 G( p# [( C1 c, K- T
$ ./configure --add-module=/root/modsecurity-nginx
1 N& ?  C2 @% O, f& u$ make
7 [- ?1 T% q: r! [$ make && make install, j9 g% j# i. U$ R
添加OWASP规则
  ?0 r1 J# b* s: wModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
2 L8 N+ }* i$ {2 `7 i
$ Z2 M& ^; j# Y% D5 F8 @9 E- R# f$ {, `* F) J+ `
下载OWASP规则并生成配置文件
" ], c% J- U& o! k) O5 F$ g$ k$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; T0 i; [0 v* S& C2 }8 K$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 B+ @$ H5 ~" }$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  K1 [! j/ E7 a) B# t0 o$ cp crs-setup.conf.example  crs-setup.conf
! y9 Q& V" i" f# |* x配置OWASP规则6 o- _" a' g5 a; l$ z, A
编辑crs-setup.conf文件
6 _# Y1 E- b# K$ x+ O8 W6 o# X5 f6 d6 L$ k, X. s
; z0 [) p$ Q* G/ a0 w
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
$ }/ z% K6 I# l- j% Q  q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf, D% V" A7 ~0 I" ]  u# y- }, K9 F
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
- P. ~: @* Y: {# Y: [$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf( P4 B. V8 V, i
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
6 d0 g+ @! t" h  `9 C; I# p2 M% E! D* W2 p5 _1 K. l8 b9 V

, w8 i, I1 S% z( n4 [启用ModSecurity模块和CRS规则
9 H5 y" L. z2 x' |9 s复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& x" j- _) z, M) @, B4 Z8 J6 Q1 R6 s: W1 r

) t' M* }, ^8 @- U; w; q4 \modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
! m- V# @: y8 a) d$ J5 h( S7 V* z+ t4 g8 S: j
2 M9 a* s+ T  A# I: C! t
$ cd /root/modsecurity-2.9.1/
. l) n5 W; W9 M$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% H" P2 ]4 r% |6 q- X/ [+ M2 I$ cp unicode.mapping  /usr/local/nginx/conf/8 q, {: e+ @; a2 @% X4 c3 ^
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
  i3 h* d; Z; @, H9 c! \5 G' H
% u4 \. a3 {( ]. [/ ~; m7 w; ~0 {3 G4 r0 |' I8 j
$ vim /usr/local/nginx/conf/modsecurity.conf
6 s4 P0 C1 H0 r1 j$ m! xSecRuleEngine On7 r& ^; E! g: a
ModSecurity中几个常用配置说明:: _$ y6 _% g2 `$ I' X
) l7 O2 Q: h% w! `0 U# Z) ^. _
' [9 b9 C2 n0 a: c' S
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
( M  c  o6 t5 ^( V9 l% G# Z# R0 j, ^( t2 H1 f8 k# p. M& G; a) [
0 I, D2 g" v6 h+ K1 I8 j
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。* g, R  G3 G8 O5 C/ Z
" |9 v- G- p. V, ~% w, i  q; ~: _

* G0 G$ d! n; s3 h; C' q' E6 B3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。% f. p3 C" `+ A/ t9 g6 M

2 U" Q4 a2 c! _  t4 _7 V: P# \' ?
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" s) I. K1 D% e! N
6 D5 C9 c  d! j4 X1 i" b5 i; ?
/ C1 R/ K+ b" F7 u: }
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
. t0 O9 I# W! K- [# m, L' g$ e# @  L, w) {

' w2 t8 N5 ?; M1 T3.x版本CRS0 L# K! `3 R# ^
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 U$ W; y8 i7 ^7 ]. ?# 生成例外排除请求的配置文件
$ R  P$ u* O. S4 m( X# w/ S8 Y$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf, v% r0 x  ^! f  a+ K7 l0 J
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf' @! M: ~1 s9 V' Q% {# T
$ cp rules/*.data /usr/local/nginx/conf
$ V1 }/ r3 N4 s- k% ]9 c为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
9 B# O" `7 ~' d( H1 `9 ~+ U
3 l8 w# E4 p$ D4 e( J
+ @; M0 \0 H2 \* A$ vim /usr/local/nginx/conf/modsec_includes.conf
* \: h. z7 ~3 R' G
3 k4 V2 H, x0 W" {" Y
[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

8 g/ A) a$ F* t! ]3 k3 U
1 `8 F7 }' B/ G7 h注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 }2 [2 s- d7 o- S& h; e" h, ]) ^" K* R  k1 Y' O! m
6 c" Y  e& M: d# l3 N5 l( V, f' u
配置Nginx支持Modsecurity0 c' q2 M* ~3 ]! k
启用Modsecurity
! Y" `$ O' C0 ?8 X( G2 z' S9 @' v  \使用静态模块加载的配置方法
+ g. E5 }" l' b1 t7 g9 n3 O: b在需要启用Modsecurity的主机的location下面加入下面两行即可:
+ g) m8 N9 ~- O" F& w9 j" k4 A5 V* S  l- e! m& q3 r9 x

, S3 b5 w: a$ C3 w, z) s" ]ModSecurityEnabled on;$ [, q8 |  @( e9 g
ModSecurityConfig modsec_includes.conf;
' n7 r7 z& Y! a( r% L+ @: L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, c, k0 Q9 R! j: ]/ L1 b) a
7 r) s, B! o: ]  a1 k) |! b* D# A2 Q
& ?, l+ E3 q/ m8 E5 k, H
$ vim /usr/local/nginx/conf/nginx.conf
4 D) L5 a' i6 p- y) _; k6 ]$ G8 [! _' K1 ]* R
! k6 t: k# v% F9 t1 ~/ F! |( J' q
server {% ]9 Q1 L" T9 ]/ D
  listen       80;
& ~6 p& n* b& J: W: L  i" U$ j4 F  server_name  example.com;' R+ ^9 k* Y/ G. j& ]' u( |

9 E; z- H( S4 C$ d' T0 G
8 E# z! A8 j" I4 W; M5 \/ P  location / {
9 U9 e0 I( g. G( D    ModSecurityEnabled on;, j4 N( [8 B( ^3 N% @( V
    ModSecurityConfig modsec_includes.conf;/ s; |% z+ Z# }7 b- W
    root   html;# e" v3 n  z+ N; C/ R
    index  index.html index.htm;0 A0 `2 D7 A( i$ I
  }* n  R" w! X+ P+ Q1 a1 d. E
}4 z- D- C1 f, G2 Z8 N9 b
使用动态模块加载的配置方法
) c2 _2 C4 b/ k8 u1 z) Y在需要启用Modsecurity的主机的location下面加入下面两行即可:6 L/ ~6 V8 _* Q
! P8 Z8 }# k* d" Q
4 ]9 M$ u% }" }9 x1 ]
modsecurity on;# W0 \: V  C; {" r/ w% ]! A
modsecurity_rules_file modsec_includes.conf;3 S/ }3 l0 w7 p" M: ]' h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- W( T, Y& n4 C( k! ]# ]: ]: d
3 K$ J" g/ i2 T8 H& K6 Y! \6 L8 I' ^' i/ p  Z1 V; E5 K- F( U
$ vim /usr/local/nginx/conf/nginx.conf& s+ l) R, F) u
, i) X6 P, x  x+ y3 O' `
) P( n! j9 s9 x: v0 W* f: C
server {
+ _; F1 V; F5 L1 h( r' X( v  z  listen  80;: w5 _9 g0 B9 N* E+ E% l
  server_name localhost mike.hi-linux.com;
- [4 ~' s/ j% ]  q  access_log /var/log/nginx/yourdomain.log;( s  `, p( z: d6 g* l. j

0 L. `% @4 n/ e7 b' E; f% L6 G! Q2 @' a! k# U0 T$ {- T5 K
  location / {( L) X; }7 N" T

2 P  @, q+ L9 t5 U# s
# |7 a3 _; P0 l$ k  b" h3 Q3 Y) |1 ^; ^  modsecurity on;
! C7 I2 f; G( }- U1 w* B5 d5 `2 w  modsecurity_rules_file modsec_includes.conf;
1 a+ _6 N* q9 \6 W  root   html;  r6 K- s# d% S5 W3 h+ C* @
  index  index.html index.htm;" b+ w. e; N7 C- w+ f
}8 c+ }9 B0 N5 n) Z0 [5 M
}
2 b5 U3 _6 q9 u' c) t  s验证Nginx配置文件
; j# i/ @' k) V+ |' Z) b/ v$ /usr/local/nginx/sbin/nginx -t/ V8 }$ p0 l8 @7 M: d7 U
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok9 b) [5 E. H$ T: X# z* Y( k# X9 ^9 t
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 l! Y7 t# f5 y( f# T: p3 n启动Nginx
8 P) H2 |5 E/ X; Y) c$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 q9 k# X( P6 t1 d/ w; L) U$ M2 f% d4 d0 p5 r" [- g' U) s. k. y' L

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


$ v' e# S  k, m0 {

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-2 02:51 , Processed in 0.073445 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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