找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12596|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
5 H, t1 l+ V" W& u4 h# N& e, E  P1 y' D  P" K+ W/ i

- @$ l5 b) r6 M, i% F1 w$ t) ?/ B在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。* }  M: {* ~* |4 O

' n$ h8 g; ?% n+ U
3 L0 U+ h1 o# O5 ]什么是ModSecurity: P4 |: p; D' {5 O, c" ?
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 l1 G$ ?$ w" E$ ]' M  W2 P
* f/ ^$ S3 L: t
/ F" D4 c* q! G+ D$ q$ [
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
" |) ~4 M" }0 |8 I0 a! Q; p; c, g$ e( K* R) u

! _! _: n" u4 ^: U4 mModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。9 L& L( b9 Z' x5 Q1 b1 V+ `
/ {7 m: t( k, C( W& ]2 p& v6 P

0 [! z) ?, u( F) {  Y2 ]8 @* M官网: https://www.modsecurity.org/
# O" k# `( M0 F: i& d2 G
2 i( ^$ s0 W  X4 P1 l7 h% `
0 _# ^/ W$ P6 A7 q  o2 i) Z5 S" O  ~什么是OWASP CRS2 [7 S5 m1 m1 m7 {  j0 w/ d
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
) f9 G: d2 r, v7 y/ m7 w1 o! ]2 v; J' m+ ]

0 |1 s8 h+ K  t' h0 x3 qModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。+ g/ C5 Z0 T( l# z2 F) m' c8 L

+ u* u# @# i! L+ d% d* |" t. p2 i& v+ m
HTTP Protection(HTTP防御)# M. m4 E9 M9 u2 j' `! Z
HTTP协议和本地定义使用的detectsviolations策略。
6 B# k4 y% [4 X* ]' B. i- n* b7 j1 m0 R( _5 {# Q4 n( U

# x! z7 {! v6 tReal-time Blacklist Lookups(实时黑名单查询)! d( h" `* H0 d: J/ Q
利用第三方IP名单。
4 L' O5 E2 B; S0 {
. X3 O( l9 H) U5 S$ C) g, k
) K9 ]' Z/ G" v2 N6 F" F7 vHTTP Denial of Service Protections(HTTP的拒绝服务保护)
% m+ k7 m, U; ?8 i* Z防御HTTP的洪水攻击和HTTP Dos攻击。) S( \7 o* D. W% W. u6 ^
  x. D- K6 `' \
; p( }* n; k$ t8 Y
Common Web Attacks Protection(常见的Web攻击防护)
, V  E- A4 z( V2 M3 ^  A检测常见的Web应用程序的安全攻击。% j2 e8 d) P9 Z6 h' V6 b& r: ]6 L
  ^. b3 u, o8 U7 s9 R  ^8 t
* d8 L8 l$ o, m* B4 K; I
Automation Detection(自动化检测), n3 c7 ~  r' i% T+ e2 ?
检测机器人,爬虫,扫描仪和其他表面恶意活动。
8 R* [* c# V! Q" c; U, U, ?
* I! J& i/ t( c
1 x4 e: h6 L" z# f( e5 ^0 |& D/ m: xIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)+ U9 L" V& \7 |7 y' I, r4 `$ ^
检测通过Web应用程序上传的恶意文件。
. M" z7 B) A7 S. U. V* J. i2 F; y& d+ J* `  a" s1 T: S8 \+ B

% U4 o6 P2 a+ Y: z. H" `5 ?' gTracking Sensitive Data(跟踪敏感数据)5 p7 Q' @7 ^9 J0 h& j! _. d
信用卡通道的使用,并阻止泄漏。
6 }" H7 I5 X5 B4 }: ?% ?( F& n  ~! f, t+ S. `4 a  V
1 w. z6 a, m4 p, Y3 Y( I: q
Trojan Protection(木马防护)8 L- |: H9 I3 m
检测访问木马。
2 V  y. {, \* x  G. p
" j# F3 f! P; F, M
$ V7 D' l9 q. H( S7 R# QIdentification of Application Defects(应用程序缺陷的鉴定)
3 z% `1 W& X* K/ W2 g; R3 g% c检测应用程序的错误配置警报。
- c$ V/ a' p6 K9 M2 Q2 k2 r7 _& N
4 u; p1 G6 O3 E+ A6 Q+ u
7 G6 M: j- O$ l8 y2 kError Detection and Hiding(错误检测和隐藏)
5 ^  e, ^5 S9 x5 f: x7 k5 D检测伪装服务器发送错误消息。* b+ x+ [& w0 k* X& Q7 j
& U( W7 a+ h( b3 a
0 H4 ]8 O9 ^, ~
安装ModSecurity
, i9 L9 N, z! G软件基础环境准备
$ }1 a0 `$ b. z' j8 N  n下载对应软件包
, F6 B+ |# S4 a$ \/ D$ X$ ^, x$ cd /root0 C3 k  ^8 o3 S
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* @. ]) {* B& @& P- h% N3 B
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ G1 H& R) o% D* {3 S安装Nginx和ModSecurity依赖包
* |7 P1 w6 n, \7 ECentos/RHEL
, F2 o: E  |8 Q% K4 X2 L% ^7 H$ Z9 c/ Z2 g" P* w3 I, J2 V
, Z7 j8 m/ M: O  l& a
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) z* }: h) H0 _% r, c" Z7 K) e! kUbuntu/Debian9 `' t& o, C" y, T

  K! ~* U5 k, h( Z9 ?
7 G9 ?+ r0 b, t$ 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) {$ A. r& P# `/ c5 W; A( a
编译安装ModSecurity
$ F2 G3 `) Q$ O4 |, CNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。8 w$ ]  h: j, F# ]+ k0 @! k

! |/ x- ]) |6 n& g
3 _1 U2 U, {2 _4 \' M6 }5 t7 z& O方法一:编译为Nginx静态模块
% |& V7 b% P% C# `" I; H0 m
2 q% ?7 }* k7 O. {0 k6 q9 D8 f+ ?+ p$ I. P+ K/ q
编译为独立模块(modsecurity-2.9.1)3 y' L" [4 E0 }; L3 Y5 S
$ tar xzvf modsecurity-2.9.1.tar.gz
; ^3 B0 K* d. ?4 k$ cd modsecurity-2.9.1/0 w* D! C' I3 Y6 c; w
$ ./autogen.sh
! d: b2 M1 t& E1 t$ ./configure --enable-standalone-module --disable-mlogc3 P, }% g: H9 A' d7 E
$ make
4 E7 Z6 h4 P% u7 u& F编译安装Nginx并添加ModSecurity模块5 `; K# Z1 q! M7 [
$ tar xzvf nginx-1.9.2.tar.gz
# @) k+ p7 \  E) t6 H$ l$ cd nginx-1.9.2& j0 ~! Q2 i+ c
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
$ ^& F* k$ F2 L4 p  n$ make && make install& @. y* x2 m  _# I8 T; m5 H
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块" C& ]" y4 b8 S3 g% t7 {, }
, I# l" G! g: \' d/ S( K& @
* O2 b; f- V1 C' s0 C) K3 \
编译LibModSecurity(modsecurity-3.0)
6 t8 j/ r! ~$ ]6 E. [* e0 n" n0 Z$ cd /root/ N9 H1 }; ~3 X6 P1 c
$ git clone https://github.com/SpiderLabs/ModSecurity
8 g% z* J6 C" A# O9 y8 o$ cd ModSecurity
1 ]5 c4 z0 T+ p% R' _9 _$ [' u$ git checkout -b v3/master origin/v3/master
7 ~7 T$ F9 ~* F& k/ [  B$ sh build.sh, E( D- u0 z& l
$ git submodule init2 q5 o9 @4 K1 D1 h
$ git submodule update
. n8 l+ L9 ?$ c1 ?" v. ?' U$ |: `$ ./configure; a& Y2 ?5 a, c6 R* k% v- i8 K
$ make
/ s3 o/ |0 r+ M8 ]$ make install$ w" Z) n1 A( }. v. o, z
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 l  g2 G: Q' `* ]4 v7 z

; [/ ]. [( [, F9 L9 p3 Q( Z" v0 b5 D+ g6 s7 L  f
$ ls /usr/local/modsecurity/lib
* o# ~, }- Z6 i% ^3 W, |libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
7 `+ X9 ~$ o3 O1 j+ p" U( O. c编译安装Nginx并添加ModSecurity-Nginx Connector模块6 f+ E. E) X( h" k( n) W
使用ModSecurity-Nginx模块来连接LibModSecurity& Y- R0 E  X2 i( v5 ~/ j9 U

# C0 S8 [1 k! T
; S: P) a; N' M3 Q$ cd /root( ]' c' M$ {, {" I4 c0 v
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
7 h9 E1 E- N+ N- j! S  d5 M7 }7 S$ tar xzvf nginx-1.9.2.tar.gz0 b7 F  u- F" e/ g
$ cd nginx-1.9.2
) d4 r7 N' L0 m# R$ ./configure --add-module=/root/modsecurity-nginx4 Q  t, N" I& E- A  B: l
$ make- `# w  q: u: [) Y8 }) ?  ~
$ make && make install4 A) Q+ e' M% t) c7 o1 ?, J" V0 W
添加OWASP规则
/ z  |% ^1 }3 P) e/ r! `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 s& i3 n: B) q) i8 r
3 w' Z* z2 k3 U/ C6 c2 T& s3 P0 @* P- }9 @% E, r1 l
下载OWASP规则并生成配置文件
/ Z* g' h7 I) ?/ d/ u. ^" U- p3 E$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
- U  f/ Z; R# ]. u$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
; `: O. d% z  a9 k* U$ O. x! x# J6 K6 F$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. M9 I; }+ t4 f  m. |
$ cp crs-setup.conf.example  crs-setup.conf/ x! t8 {( D& ^
配置OWASP规则
$ p7 z. o% c3 z, d, G编辑crs-setup.conf文件
* g+ k5 `. k. A' o$ M1 x2 V! P' ], f# {* v

% i7 u( J& q7 U9 ?2 V* l+ |$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* H! `* C+ V/ o$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf8 Q4 `+ Z- l5 A6 t6 r6 `
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
  [8 h  \/ @' l1 C. L7 k$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: N4 @" s) Y) d. r" t  _0 l
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 n( O1 V6 }2 ?2 M1 F& d$ L( q
* E# C' a  V8 F1 |

' F- Q" ]" ?  ^/ e启用ModSecurity模块和CRS规则
- n5 H4 J1 K! _8 a3 p复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# m4 [+ }% Y. Y# P5 `# q. ~) z
& @! {( g. a/ \4 p! k. O

0 `! y2 d2 B' ~5 ]! Fmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% g0 G' D  x! ^) a0 J
# r$ j. w1 b% t5 N  v1 h
$ x" q' e0 y3 Z  O& v7 q2 G
$ cd /root/modsecurity-2.9.1/
1 g8 F, w9 C% @$ D7 I) Q3 q$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
9 K! K* v; ]" ~' P$ cp unicode.mapping  /usr/local/nginx/conf/- H& X6 K4 S$ T1 _% s% U$ p5 Z
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
0 O) G5 b; N5 V! k* E1 k3 G
( R9 `6 _: [1 l) @3 Z, \6 L% T# V  p) S) ?
$ vim /usr/local/nginx/conf/modsecurity.conf) M& E. _  D4 l; [5 U9 e) Q( ~4 s
SecRuleEngine On
/ H8 M  o$ o. ?+ |2 n4 h' SModSecurity中几个常用配置说明:
* @- k/ Y% }7 r+ Q7 C0 T& k# _8 c, g
  z# U9 w7 G; T; i* ]3 @  R6 z! X' ^- L- K
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
5 \- g$ g  w1 p! Y7 X
* M; w+ H1 k! r- c/ X# v- T0 _
8 u" T5 J/ c: k" M- e2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。$ f4 l. {$ W7 X" p8 [) ]9 {

5 c# y* F6 r# f% }: |2 }: j. `; L& ]( W' P6 p6 t/ [) d" c; X
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。5 x0 Z1 U+ o- I
. ?7 `/ u- x2 ~. {. @- b1 m
0 U/ d4 n4 {! A! Y+ x- X
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
7 c8 Y( z! m; m# v, D9 F5 L4 V0 y  l  T& j' z8 j0 q3 t
7 ]4 }  c" {3 P7 W) W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
9 z4 \3 S7 a0 X( ^; z3 p, d8 z9 S  L
3 B# E( [3 E+ m+ r
3.x版本CRS- C) @# z8 Q6 @1 w; D2 Y! T/ v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) z, Z) j4 w, r1 h- m
# 生成例外排除请求的配置文件
# m" e7 x& V3 I/ i$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ M: d' {7 S) |* }: B
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
: r8 D3 C: J( t9 O1 P$ cp rules/*.data /usr/local/nginx/conf- S" Q, X0 a2 i* |3 u6 z: E
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
+ Q- Z6 K# d4 A1 m0 E8 t2 b* L1 U# F# B! t4 y9 G

* b! j2 Q6 i" V3 R' j1 u$ vim /usr/local/nginx/conf/modsec_includes.conf& K9 x: S0 J( D! M. J' ]9 @: Z  Q

+ `: p, N7 |) l
[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/ z4 r7 l# g" {5 i! B
( i% B' Q/ W) `; A9 U/ t. `8 T& B# L注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
& _- g* c# }& B1 Q0 d' p. z4 k
, q: y& {7 H9 i: O) V% W- H" U. R. |
' ?0 ]$ Z6 y' F# c3 ]# w/ {配置Nginx支持Modsecurity' I# \1 R9 `2 e9 r+ f
启用Modsecurity
8 C' t$ T2 R2 E4 p! r  y  l' O9 N使用静态模块加载的配置方法
5 T( L' l" ~6 o7 c1 P, x) q在需要启用Modsecurity的主机的location下面加入下面两行即可:
) E% S4 S0 E5 U+ u# j+ f7 W, p; s2 e. V" d
* O% B2 {2 P2 |7 [* H9 B$ L7 F
ModSecurityEnabled on;
7 x6 `8 X+ J; N& OModSecurityConfig modsec_includes.conf;
( L/ J: \; T0 @: d, _0 S1 m' ?修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) ?* O5 o2 k- e2 D* j* u# |! q8 [
4 s% [; e' S  J2 ?+ @- e3 L
% D! C, Q7 C) M! `
$ vim /usr/local/nginx/conf/nginx.conf  z$ r3 t0 L) a

4 ^7 d! U% \+ k" h
6 r* r  h( n/ ?' A1 Mserver {
5 H( }" `3 k; W  listen       80;. o$ R6 k" ~' X0 ?
  server_name  example.com;
# d3 e7 |% @1 G3 S3 o& W
* p2 n, }. ~2 w+ g% ^
) V' h$ r( u: A: e7 W) _0 l  location / {
* A, p, W' S$ D  e! e% R    ModSecurityEnabled on;$ M! s4 O: G) o$ v
    ModSecurityConfig modsec_includes.conf;
- ?# {6 V5 w, P; @8 h    root   html;
9 l; ~) [7 S2 Z8 ^2 |    index  index.html index.htm;6 ]. A, R, W$ ~8 W' v
  }
% G# D9 T! }2 t3 F# D2 x- n}
9 Z  B; E$ r6 \' h8 f使用动态模块加载的配置方法
: x7 M6 n! A, g* w在需要启用Modsecurity的主机的location下面加入下面两行即可:. D: T$ f0 j$ q, E( b0 c' N
' E5 X0 t5 k) s' p  r5 u8 M* d

9 u9 {! J' B  vmodsecurity on;. g1 w$ K) l: b3 W
modsecurity_rules_file modsec_includes.conf;
& O  F$ L. Q- i' n6 b修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 b( r" `' R* ~# L
; Z4 T  d& d, A2 Y  R# E6 N. j
1 {7 Q, L# n  U! {
$ vim /usr/local/nginx/conf/nginx.conf
9 j! w$ H6 T( e. K
8 ~& M7 w% [# G! U7 V
3 c# w9 w; w, M3 L; Oserver {) _* O2 G/ @. B& T0 O4 W
  listen  80;
' ~. c4 X" O0 m" O3 @- F# `1 f2 h  server_name localhost mike.hi-linux.com;
; |- p4 W' ?$ ^7 W4 j  access_log /var/log/nginx/yourdomain.log;* r7 A2 u4 L$ p, U* r! d9 A
) M1 w6 A; U8 P( N/ d
. o8 x, r4 O- }  C* [# D6 X+ e
  location / {
0 {5 t: @& R; }8 {8 S
. M- X! D! U& D3 ~& c$ K( p3 x
* z+ d' M. a. W. e9 O: v4 O  modsecurity on;; U1 W% c( w4 m8 ^; K9 {7 w
  modsecurity_rules_file modsec_includes.conf;$ D3 `# K- Y' {2 g8 K
  root   html;( Z' F' R3 r1 |
  index  index.html index.htm;
# m8 R1 t9 ]; D8 {}
5 y1 T8 c  x: p( `* d}
# U/ N5 ?/ ]' r. W6 X验证Nginx配置文件0 g# d3 [: e: E' r6 m( |, j, q. Q) x8 v
$ /usr/local/nginx/sbin/nginx -t
- W! p( w2 V6 Cnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& O8 q& {) O6 `# ~1 ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5 P6 D; d: ~3 z9 G( u0 ^+ M& \- j2 y启动Nginx
5 o/ c- N0 K5 i, }" c8 f$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 M, R: m1 J9 A/ K  f
: O1 D/ y  [4 X  U  A0 A

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

) K: _4 w9 N( k; ?" c+ u

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-17 02:25 , Processed in 0.073425 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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