找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10339|回复: 0

利用ModSecurity在Nginx上构建WAF

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

# `- f4 f3 M' R6 A9 A- j) u/ J( T% m# }* ~/ \  f4 D* T
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。8 [* m( I0 q7 ~& U% W0 z

/ a7 j( ]& D3 u: h, j( ~  ]  v/ {+ B  C
什么是ModSecurity
4 o4 q% n2 J( T7 {ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
! U, n7 r  G3 T" W9 u% K' g' b- D0 a% _% f& T9 M4 P! \+ Z

/ q, h' M6 i$ Q4 |ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
. Y& E- o' P( M$ n6 b2 r* z8 r$ Q# H5 r: @; y' E8 p
: R0 Q3 j4 C# O; G& q0 g
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
' C6 i' {8 p4 `$ d/ k" b' w( D# U! s3 s# D; I+ e) k3 I
6 D+ \. n: u$ S; S7 |
官网: https://www.modsecurity.org/. n' P/ A7 N! j  z, l0 N1 ^

2 ]. |. p/ L4 P( s/ `" l6 v( R' i1 S; N
什么是OWASP CRS; P1 l- [* e5 O! K; t8 o% Z
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% P1 ~' g, c3 E1 `9 M; S0 t- r4 B& ^- J* q7 y) P" e

5 E, H8 V: d1 N4 XModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ c: D9 p, Q, L% |1 _# H
4 v: q: q& b3 [7 Q- V8 S
' D! F8 ^6 T: |, P" e
HTTP Protection(HTTP防御)  U' [0 }4 K0 q
HTTP协议和本地定义使用的detectsviolations策略。
; o& q$ u# m" O" R2 h# `) C% r
( R0 e* x+ Q) I
6 ^3 g8 B2 i6 ?+ k5 jReal-time Blacklist Lookups(实时黑名单查询)
' i! Z+ `. D2 q/ \& ]+ _3 L利用第三方IP名单。  \6 Q* i5 Z/ ~* q% h+ G
& R# E4 }: t; d& }$ n
$ M. z/ Z( j: v1 @$ r* ?
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 K' ]& ^2 R9 V, M( t防御HTTP的洪水攻击和HTTP Dos攻击。
/ F' V' v/ z3 n2 f
; T/ A3 d( F# n) |0 |" \0 R5 y, V
Common Web Attacks Protection(常见的Web攻击防护)
+ `% y$ t: Y% V, c1 q& {5 `检测常见的Web应用程序的安全攻击。
( {' L3 v: t3 l& e6 M. o( H" e- g  W% d$ P+ ~  [$ g5 {0 Y

9 i) n: O+ u. [, o8 ~Automation Detection(自动化检测)
1 ?9 ?* |: q4 v& I( Y$ ], S. D检测机器人,爬虫,扫描仪和其他表面恶意活动。4 K2 O* L8 P$ W+ ^# z& p- g
) n1 n, }) A9 J6 Y
( l; i6 h: K( V) J  j
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)% S2 @4 P  o6 Z2 b* P% m$ `" R
检测通过Web应用程序上传的恶意文件。* l7 Q0 p0 n  F/ [; E1 {
& N3 H  y% L& M0 Q- w; k

& C3 h$ f6 ^6 q9 w/ U; kTracking Sensitive Data(跟踪敏感数据)
5 u" s- Z0 w; K  f信用卡通道的使用,并阻止泄漏。
8 y- _) W: J2 [9 m# @
" l8 r$ v) }6 n! _8 n% z; x/ }6 |* Z& t) S  {) g/ S. b7 f
Trojan Protection(木马防护)
' b7 i6 V4 O  i. B5 x5 q检测访问木马。. F3 c$ v( _( ~1 ]7 p$ {
" h: _2 q" y9 k* b! `& i' ~& u
, d# _7 y5 [/ h9 Y4 g4 M2 W' Y
Identification of Application Defects(应用程序缺陷的鉴定)
4 Y: t$ p+ S  |  t( {% X9 v/ L检测应用程序的错误配置警报。
/ P3 x& {+ y6 X. u7 K  I; G& Y( E, j7 s; L% N
0 O! e. u& ~0 I+ Z+ o9 x) ?
Error Detection and Hiding(错误检测和隐藏)( A! r) m2 @8 C9 g$ h7 w/ h: U- L$ U
检测伪装服务器发送错误消息。& B# [& T2 D5 B6 u; q: Z8 j9 h
" `' ^1 p7 F; F% B7 V

3 {) {$ \: S3 N+ \6 V6 |) Y+ C6 F安装ModSecurity
: F% T" Q. V9 T4 c软件基础环境准备0 f' i: J: z3 Z3 ]0 y" O+ {
下载对应软件包6 ~# n7 `  n4 }
$ cd /root3 o0 m' u6 c* ^# v! p6 t
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'8 x  i1 n1 i# D* Q
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz! Z0 s4 N  a% M) {
安装Nginx和ModSecurity依赖包
" e* z2 l( m: j1 o+ tCentos/RHEL5 r4 o) Y% n2 t' H3 b8 I

* y. A1 ?6 A+ v+ Z1 {% z+ w
5 Y' b8 {5 g/ u1 R: V# J$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
6 m- {! B  A- N& Q5 X# JUbuntu/Debian
& g* ]5 k! b1 i" q2 D  p3 I4 r& P$ I% `5 r) w. i+ T9 u! m, E
0 {9 R2 }* w( 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
0 E4 y  R, L" t9 Q4 q0 B+ y( g编译安装ModSecurity
+ x8 K( o, ]2 SNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。3 x  a% V7 w; d3 G) W- q
  B! K; J7 C, Y% ]

! O4 |2 n) a. n% A( K0 c8 U方法一:编译为Nginx静态模块
9 o8 w! |! S8 x
# W$ b+ K( d3 i+ @% n) ^5 F* v& h0 z6 K1 i2 o( R
编译为独立模块(modsecurity-2.9.1)
: J7 u' [/ h+ J9 b$ X! s# C4 Y. O$ tar xzvf modsecurity-2.9.1.tar.gz3 b  x7 k# V4 b2 W9 P
$ cd modsecurity-2.9.1/" E) }; P) }3 r7 ?
$ ./autogen.sh0 Z) G0 }0 V* }
$ ./configure --enable-standalone-module --disable-mlogc
( [( U9 |6 s% U% W$ make
* A4 q0 k: A  ?; `/ q编译安装Nginx并添加ModSecurity模块( F- b/ K3 ~3 F1 M
$ tar xzvf nginx-1.9.2.tar.gz  [9 l2 _2 m0 R& m% e) @/ n  I) z
$ cd nginx-1.9.2
' O' l  M4 @, P% P2 I/ m$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
4 ]3 {' B0 }: e( f; t: }$ make && make install. X6 H# a6 z1 t' Y# o1 M. @$ ?
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块% {1 u+ [% V9 J$ p8 F8 A, r6 E3 S

9 V' \- K9 B7 H' Z7 E! R! W" x( m' T) }6 j, C5 ~& t  {8 ]" R
编译LibModSecurity(modsecurity-3.0)5 a- H+ w# J2 h1 e% H
$ cd /root" f. y; B, ^; ~* e
$ git clone https://github.com/SpiderLabs/ModSecurity, q' V3 z$ G4 O3 g# z# \
$ cd ModSecurity
$ G0 i' l$ i4 w( e8 A) `$ git checkout -b v3/master origin/v3/master, |" N; q" Z( l, p4 V
$ sh build.sh. c; z; Q6 X1 q* j- C2 i
$ git submodule init
9 W1 j* h9 {! s  W" d$ git submodule update% q1 B  C4 A+ b  q, N
$ ./configure( F, \% c& F* F
$ make( S" f' g# [, j) g& `& j5 ]& E" _
$ make install3 {3 _7 E8 p' ]
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。3 m4 z8 u) O8 `: C) n2 a- n6 u! ~

  g: ?# g& U+ M" `9 s- w
% [/ w! L# t; k6 I/ X& S3 `$ ls /usr/local/modsecurity/lib/ A1 O! {* c% W
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.09 N& ]; j8 W4 _% o/ ?: t
编译安装Nginx并添加ModSecurity-Nginx Connector模块% o8 j2 q1 ]5 U3 ~
使用ModSecurity-Nginx模块来连接LibModSecurity
1 n& Z8 [9 J0 ]; M  v7 R( c  h1 a$ u
. {6 R  d  Z# e  n" {
$ cd /root
# |  L& O" M5 N$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ e9 b  c& q* ^. ]& i& N1 t2 f
$ tar xzvf nginx-1.9.2.tar.gz: g* s/ Y: T+ ^0 ]5 B
$ cd nginx-1.9.2" Z+ M" K, j. s$ o
$ ./configure --add-module=/root/modsecurity-nginx
: w; c3 u! n$ t( j7 X! o( a9 U  _( [* I$ make* j3 i9 d/ H) Z# P# K" q
$ make && make install0 x# R$ M' O* e2 Y) L
添加OWASP规则6 k0 C9 }+ A/ `
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
1 O7 W' b6 J  t" Y8 z3 }$ K+ e
# o% `) ^+ ^. k, Y+ w; K; f) ]* U1 V8 `1 r5 j
下载OWASP规则并生成配置文件9 Z% h3 S: A1 }* d2 Z1 ^0 h3 ?
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
9 i) `+ L* b* n/ G$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
6 y( C; ~2 N3 l& i. f$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
2 F: V7 A* Y* f+ r$ cp crs-setup.conf.example  crs-setup.conf
/ R8 h6 L4 P& _7 d3 y9 O+ }配置OWASP规则( d- x6 w3 x: }& @: A/ m6 p; W
编辑crs-setup.conf文件" F" S* p$ _" s7 Z" z, f4 d

: K2 s- z; E4 U' s+ F1 ?4 |! W/ _/ {9 o  C  \8 ~3 b% q
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ l+ V* }6 D+ G4 H( ^$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 v& b% }$ S% ?+ `+ K. v# N$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
$ |$ @) J# v1 ^# Q+ n$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 |, }! T6 m1 {/ K默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
% R: \4 v$ b+ G+ l; Z  G! M* p$ d1 r
1 G% ?# ^2 ]) B% }* O2 G: I: ?
启用ModSecurity模块和CRS规则" p6 W1 r( E( Q, ~+ I2 G/ z
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 v) H7 W4 v. O6 ~0 v% {* Z- T2 N2 x
- m! y/ i6 x, W- k) ?
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。* C9 W& X! V( ^* b0 }: y, d0 ~' G! x
/ A  f$ j5 @, K0 m5 T# n

0 H! x0 Q1 S) W( {* c* e) K" S' m$ cd /root/modsecurity-2.9.1/3 O: U1 q  A' E' d! ]& `; d
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
' ]: z! x2 z; O3 D3 e: v  q$ cp unicode.mapping  /usr/local/nginx/conf/8 N& `4 q' a7 q% y! Z- o
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
8 a3 R$ z  X  n' }- H
3 M$ U0 ?. }% ^$ b
2 O2 E# r5 h8 M$ vim /usr/local/nginx/conf/modsecurity.conf
; e, K4 o6 U/ c7 R# ^$ p4 }SecRuleEngine On( i$ S! P5 A+ V; s
ModSecurity中几个常用配置说明:5 a; j" F  j, p1 c: p
) `  z; E% U  W8 Z( q) W
8 E, R4 s! L3 y7 z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ K. f& D0 u: R" `! M; e6 v. T
1 Q! j+ G. E3 Q! T
3 M* q4 {. S* }) p2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。0 I- N/ N5 t1 r- @) v/ n! b

8 w* {8 H& g" t) p8 Q: |0 ~4 Y; E( ]6 }. V  V8 C
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。% Z$ E6 W3 \6 l1 U

6 v$ {% B* m7 U' A; a
# m* a  V, }+ `9 S' [4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。9 o4 L( Q& n) d- P, H: \

/ c1 B& o; T5 d2 k) `/ J6 ?3 o
* r$ K2 l& O; t% O在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
) H: w- `" k, L$ `8 R  _- C1 H( r$ _9 K5 C# X4 p+ U; H7 T

" o) Q5 ~9 y4 ~% \) g+ G3.x版本CRS
! Q( k; a: i, Q' ~9 E; P$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. r7 u9 Q: k1 {
# 生成例外排除请求的配置文件: W- \' R# A- a( t
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf0 J: e8 j! l- Y7 H' v$ p  \9 E
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ o7 z+ U6 V& p! N3 P) n4 x) V# b
$ cp rules/*.data /usr/local/nginx/conf
* f: V- P' v3 E* V1 I9 {$ u为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
4 ~$ z" o$ W) m" J
7 w9 t3 k; f6 T, C) P  q) S( b6 Z; z0 |, ~* K% t6 ~9 {
$ vim /usr/local/nginx/conf/modsec_includes.conf9 n1 {' n; R& f% X* p2 [5 `

. S4 B0 o6 ]2 {* ^1 ?* V1 T
[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

4 `8 S6 E4 c8 J" I2 ^
, ]) g: R4 n! A6 @, y. R3 Z4 w3 |注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。, J* _- U6 w  ]) X% ^( X' g
( t5 O/ Y1 ?4 ~* J
7 q$ @. e$ j6 b; j2 @$ X
配置Nginx支持Modsecurity# t9 ]; ?5 y5 M( R( P6 N5 P9 U
启用Modsecurity& A: H8 j/ @8 |8 ^
使用静态模块加载的配置方法
! X% |) t" Y+ g0 l在需要启用Modsecurity的主机的location下面加入下面两行即可:' Q/ b! d+ y$ A! a1 K' F

$ \+ D/ a$ v: B. Q& U+ {* b4 v6 g! }* ~3 S6 o
ModSecurityEnabled on;& X$ \, m! M, E( x- e
ModSecurityConfig modsec_includes.conf;
) R  ^. A, S0 l& S修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& I) g6 {, u3 N0 V  g: h" a- i
( w! _3 B# g" t( _, T
' H1 w- e/ S$ L5 i
$ vim /usr/local/nginx/conf/nginx.conf
1 y1 q3 H! `/ i1 a
/ R1 Y6 Y0 z) n" G1 Z" B4 i
! [0 A! ?3 u0 s8 K! P& ]8 userver {
; q1 j% Q9 t4 j! ~9 Q" ^  listen       80;
" G, ^. [" Q3 `  w2 g  server_name  example.com;( w8 I! @& b4 X. \
2 M7 ?3 D( |5 C$ `
: }* _- w# |6 t3 ~! b$ n
  location / {
" I% {4 {  T8 M    ModSecurityEnabled on;
9 @* s, s& h9 B  Y' \) A% u! j    ModSecurityConfig modsec_includes.conf;
3 u! k" y+ Z6 w6 h    root   html;: {* C0 W" r+ D. Q
    index  index.html index.htm;
& ]6 c( m6 @0 Y' N+ Z: x0 R/ g  }! @& |" n. u0 h
}
# P# I# c( d+ c1 Q7 q使用动态模块加载的配置方法
. \& Y! W5 G: C在需要启用Modsecurity的主机的location下面加入下面两行即可:
* a' a0 @* }7 L3 s
/ p* h% C8 [+ i+ _) \1 ]* w
  L! s7 a% o. x$ A. V% O# vmodsecurity on;' J4 u# G$ ^9 S6 i0 R5 Y+ b! U
modsecurity_rules_file modsec_includes.conf;
; s) @4 T' |: p; k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& O" L( W2 o" w' b$ g  U$ y; a7 g. R' ^- E$ {/ Z

; d2 d. ?8 @- W% J& ^7 r5 G$ vim /usr/local/nginx/conf/nginx.conf
* Q. y0 b& I+ W2 s) e" `9 R# N( `2 ?- B4 `0 s9 ?$ t
; b; F" x+ T8 @# D
server {( @# i3 O# j' V5 W2 y9 S4 D
  listen  80;0 T0 V! t. J0 A: U& ?2 q5 W
  server_name localhost mike.hi-linux.com;
3 `8 U& I, z/ @, B. `# @. `6 Y% U  access_log /var/log/nginx/yourdomain.log;
3 j3 \' L8 \: @0 [6 P" y% V/ m7 p8 j* q& ^/ f& |/ Q4 [

+ p) G* y' R. p# N  location / {+ t) N0 D  y$ M% h
" E- n9 n1 x4 v  D' _

! C/ D1 p& o1 \3 X* x  modsecurity on;
+ w  X% I8 i' ~1 c5 s  d  modsecurity_rules_file modsec_includes.conf;8 k( g" O- x. z, C% u  f$ C
  root   html;) A* d6 S  \* d
  index  index.html index.htm;1 `6 r7 }+ V& s
}
- D# w" L3 ]6 ]/ |2 o- e! z}
) ^+ T$ f4 ]2 i0 f8 z5 {" _验证Nginx配置文件5 l1 z9 Y+ @: u' U& Z) l  q# \
$ /usr/local/nginx/sbin/nginx -t* i7 D5 E6 S7 b4 r8 G
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
. g) x0 l7 b" g1 W1 x& pnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful  J* g% p  l" k+ C, r% ^
启动Nginx
" o1 d8 |, W6 x* z( j% V* t  L$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 M+ b& f5 @1 L  m: b( l; r* C
7 S" b5 K* t3 W9 }+ Y5 i, c" X. M: ^

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

4 Y9 j, o) [; P

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-27 05:54 , Processed in 0.078104 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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