找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12657|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。3 J! }! w7 l9 r# B
+ P, g9 i9 `1 A% D% C* V. |) |

8 O. J/ W1 a" J6 B/ [- J$ x5 Q3 Y6 K在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; w0 e- w: T8 `3 Z( O+ h
' \) i7 {/ k* }% q+ `9 T" h! D, A, P
什么是ModSecurity
# h& @3 d1 b9 `ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, c7 {' h7 i( C4 ]" I/ m, b; D. ~0 Q, K1 q1 U

: w8 [) r+ ]: Y! z0 [$ ?0 a" zModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
) B) t+ I( u  j2 K' O
/ F7 Z0 ^5 G2 D, h/ `" h
+ T: Q/ F/ K& c# A: m1 SModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。; \" V) Q6 `( g! [! n" b
/ k0 c3 ~4 v5 C5 G  x( H" ]
" ^% C* E" n2 Q) H9 W
官网: https://www.modsecurity.org/
5 u: a# I+ k5 o
; h' e7 i3 L9 _; \( U0 \3 M2 }9 m, e
什么是OWASP CRS
: m! L) e5 e% _0 Q  n6 ?+ ZOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。2 V; D9 `) O; n6 e. ~. C" B

6 }$ J; O+ j- o- ]$ I
' C3 W$ a& R: Z5 n8 X4 t3 @ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。' q  n$ f- C7 z3 k

! Z. l; R8 c, v4 u6 J/ P  D5 W& Y- N$ i6 Q/ l6 t
HTTP Protection(HTTP防御)5 {( ^! Q. ]8 ~7 y0 v
HTTP协议和本地定义使用的detectsviolations策略。
/ k3 @0 I+ U7 I1 I4 ~
% s8 _4 |$ J2 G3 O  [& k4 H  n! i+ T. Q
Real-time Blacklist Lookups(实时黑名单查询)- {% g7 }% ]5 q; }0 ?- O' ?: h
利用第三方IP名单。( r( f! s7 J; e" e6 F( {) ?

3 }5 c/ I) i) m8 F
. g2 j3 y& s5 {1 A& gHTTP Denial of Service Protections(HTTP的拒绝服务保护)2 A; `" n$ E5 o0 m/ v
防御HTTP的洪水攻击和HTTP Dos攻击。
7 E; l7 E% I* W, K6 D8 f) Y7 h- R% x2 S; y% ?7 f

+ ~8 b9 A9 X  b6 }: y" LCommon Web Attacks Protection(常见的Web攻击防护)
' N2 g. R1 E. y检测常见的Web应用程序的安全攻击。  P$ E: g: c+ o- y$ [4 W3 h
! X* V5 G1 {# i
& y, H! R2 E' L9 W+ R8 B0 f
Automation Detection(自动化检测)
( G# V' v. [8 d2 v/ x( Y% C" e* L检测机器人,爬虫,扫描仪和其他表面恶意活动。
8 U; S9 H* ~. Y9 E. [# W/ d9 i7 v* b, I
. Q! g) m$ c  K' [7 c
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)# s% r$ c# }) u  P4 m
检测通过Web应用程序上传的恶意文件。
; t9 u  N& g/ [! {. {- {+ F5 P
' v& R5 L2 q2 o( ^" l# W- R
3 z+ y) C1 j. f9 X/ v1 p( C4 r  i' \# `Tracking Sensitive Data(跟踪敏感数据)
2 t) D9 N2 `5 c: v& ^信用卡通道的使用,并阻止泄漏。+ w5 @; K0 z0 e; m6 r
- N' m) n/ x) r5 r: E
7 I: |( @# P  o6 `: X& q
Trojan Protection(木马防护)0 \+ f7 D1 |; g7 L1 l1 l
检测访问木马。
7 O: x6 r5 O! v/ G( U9 f! w3 P) R5 Z) s( D- O
4 |- A$ F9 X) n, g  }" U* w/ ~/ [
Identification of Application Defects(应用程序缺陷的鉴定)
' d* |) O* r; ]- P检测应用程序的错误配置警报。% |" r% A' x/ E! O- W
3 Y0 ]; w. F1 S9 m4 l; s
6 k& j7 P. ^( ]. c0 p" ?: e
Error Detection and Hiding(错误检测和隐藏)
1 p* S/ J2 x4 m- r! y  [+ u检测伪装服务器发送错误消息。3 `0 S: C- u6 i& l6 ^; Z7 ^
: m* l- |$ B- `
9 N$ i9 g9 s/ T; F9 e
安装ModSecurity9 N$ W4 H! F! E1 J
软件基础环境准备/ ^) F2 t* ?5 A3 T) ^* f
下载对应软件包" J. t. B6 l) q
$ cd /root
$ F8 ~/ O* d# E# @9 Q) S2 O$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 t7 _9 t, ~0 v5 M$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 o& `6 N/ Y3 t
安装Nginx和ModSecurity依赖包  [* x  P% y% D8 B& g7 z
Centos/RHEL
) R8 t7 b! \3 j0 H
; O* N; i$ r2 ~1 r# Z9 q9 c
& H3 R& _" a  f: r2 j% U$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 K/ c8 B, ~8 _9 ~3 N
Ubuntu/Debian
9 K. i* l" l* O# N5 T# P: ~* ^" A+ _# d9 b. _9 _

: [7 Z8 j/ z2 d# N$ B1 T9 S$ 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
" J- Q) }  g+ y  E+ V2 N9 _# F- n# T1 t编译安装ModSecurity
8 z3 H& N0 l( }! D; a6 iNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
/ F5 z- u1 L  s  w; ^8 P* v: y1 H
9 I" O0 `. H/ ~9 u+ l
方法一:编译为Nginx静态模块. s8 S$ M3 b( S9 R
6 z5 B, _! l* I9 Y- u% a3 T

0 }% l/ y+ H3 {$ ?+ I, V编译为独立模块(modsecurity-2.9.1)1 M$ Q; m  ]2 @
$ tar xzvf modsecurity-2.9.1.tar.gz7 x1 y) n- V0 J' V$ a/ _
$ cd modsecurity-2.9.1/
% @8 N* D0 c! q; P- @  l1 {$ ./autogen.sh% l) `& z; L( ]* v
$ ./configure --enable-standalone-module --disable-mlogc/ F: q& N, D/ ?1 t' y/ y3 A0 u" S3 r3 ?
$ make
* c  n# d+ L5 p' U1 w. c$ m7 N编译安装Nginx并添加ModSecurity模块
3 }; b' a9 y, b, e$ I$ tar xzvf nginx-1.9.2.tar.gz
  I& g$ L9 w' b$ cd nginx-1.9.2
) k( j0 D: `' g3 Q2 F, c$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/. ?0 j# J7 u9 M
$ make && make install
) `, J* g# Q" U8 |7 C- G方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
; G7 m5 b0 t3 P  n- ~/ `) s9 [
, k. N$ C0 O1 i. Z# ?
. B; x% ?- x/ Q( W: V1 J编译LibModSecurity(modsecurity-3.0)
' A, E9 d  Z1 s6 v3 t# I- P8 `: P$ cd /root
( Y$ s4 R+ ^6 H9 h# A2 B% a4 d$ git clone https://github.com/SpiderLabs/ModSecurity
" R0 f" y& L  d8 U+ D1 }$ V$ cd ModSecurity
4 w6 j5 J" C+ L8 C2 m5 M# v/ O$ git checkout -b v3/master origin/v3/master: D/ Z- y; ]! ?: s# H
$ sh build.sh2 r1 V4 A: i# m
$ git submodule init
3 p0 ~' X" j2 v# y- V# r$ git submodule update1 p2 S2 ]% i0 E( ^! a
$ ./configure; A  }- B' C! k" |# h) b3 {* ]
$ make( p2 @! `$ P3 K7 p( D6 Z- F% \9 T+ F
$ make install
* x/ R* L0 ^0 CLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
, b% X# u) F" f5 n
! O' W( Q% [6 z, i( ]; x+ \/ |- K& a" r* f4 r$ R
$ ls /usr/local/modsecurity/lib
$ h( Y! C- S- w3 `9 ?' x; Slibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.04 |. z& u8 }4 X$ a7 z
编译安装Nginx并添加ModSecurity-Nginx Connector模块6 E2 K4 E, D2 _
使用ModSecurity-Nginx模块来连接LibModSecurity' {1 k; v9 s/ f

8 o, S+ f, q* H) c/ w+ R* X# h- Y- A( l4 I0 [5 \
$ cd /root- p; B4 e" P. }8 i) i# j
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: _" B0 [3 N6 C. {5 @$ tar xzvf nginx-1.9.2.tar.gz
& _2 w9 B3 Z* Z6 u6 V9 o# j6 j% C$ cd nginx-1.9.2
  o7 c9 C4 }7 k9 B. T$ ./configure --add-module=/root/modsecurity-nginx" B, N  S! A# V5 W- j) T) Y  G
$ make# P& T# J, g) n6 K$ v
$ make && make install" j+ r% C* d3 h, L" s' t
添加OWASP规则* ^( j8 L& b) t6 Y+ e# t+ p
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 g' {# o  L5 Y5 }$ N- O" T" [% z+ W2 g; `! x, A5 S; ^
! f- g0 h- h7 E4 ^, ~/ r
下载OWASP规则并生成配置文件
( m- g9 y3 c6 q; ]$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
' p5 g+ r# c  A" v, m2 x$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
5 d  ^' L! E$ D/ f& j$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
3 S2 {1 j0 N* p$ cp crs-setup.conf.example  crs-setup.conf
* i; k' I$ }) @0 B4 b8 d配置OWASP规则
& ~& @% m6 G9 i, n+ v编辑crs-setup.conf文件
+ i% S6 K, H* s: M/ j! O. p
4 @$ A( Z3 t8 {9 s$ _; ?
! ~6 Y/ C, X7 _- p7 b3 W2 B$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
2 Q5 X9 i3 b( y' h1 _$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' |! p8 x( A" r9 H$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
. Q1 V0 ^/ G+ G* d  K$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
. A- @; J' N# R% X- H默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
# U0 U. }. {5 f' w$ l8 v
' \) `7 r* O3 h" i
' u8 d6 _: N/ X$ c9 }$ w  K启用ModSecurity模块和CRS规则' U% P$ y2 _; }* O0 y0 d
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 K' S8 V# S, }( K( _, A' P; V- H5 L2 t( l( E- W/ ~( t
; g+ ^1 N# n* T; X9 T2 N
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。6 I( j; r6 q7 l( ]3 |$ L
! o* v; {, v$ u, U! x, B, W7 j
- v; B# }/ b" b1 `
$ cd /root/modsecurity-2.9.1/
$ F2 n/ n$ M, o  S: [' \9 d. W/ a$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  7 h# P& s+ \- g
$ cp unicode.mapping  /usr/local/nginx/conf/" O# Q- M5 ^, n/ s& K" s  F
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。7 ^1 j" E. l  B! @9 U2 T4 l

# h, \. D! j6 A0 T. z  j2 J0 N' c3 f
$ vim /usr/local/nginx/conf/modsecurity.conf
$ j6 q6 \! f7 t# S; A: S5 TSecRuleEngine On2 B- y# }. d! a, H5 t1 [* \( q
ModSecurity中几个常用配置说明:
! `% O! [1 J$ B' u
+ G* ^" m' ]/ j; v
7 o* _% R. ^) i4 T8 h1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
- P; H- }) y' @; m/ [/ z8 L
, m# d. l3 t' i6 p+ a, L+ T
. S+ G# E! n5 v4 j, X8 W) t: S2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
8 l- ~% C! u  G6 ?; `; F
" V; T0 B9 U" C' P. B+ H0 q, ~+ \: N) W( m$ {  n! m
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! Z( u  a7 r  r5 a& N9 ~

, R! W& A( H* }  s( t" u1 f3 E8 D! f+ O  [" m  y, w
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
0 B' b; r6 @& C! s; X# @- w1 |% Q% b; v4 o
: r, e$ u# K6 f7 m! M2 h/ P2 s
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ ]# e& j$ Q. p8 m: M* |8 `
, m# z, E, q) s" J6 r
* |& v# @+ N8 M3 O+ ~% V  z& T
3.x版本CRS
+ m/ X5 c3 R$ q. a, |/ C/ ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 @: P. U( Q0 O9 Y4 {# 生成例外排除请求的配置文件- D- _1 U$ ]6 r- T4 _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
3 r% Y$ S# C3 o5 z0 Z$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
) V3 I, ^( M( O( ?! Y$ cp rules/*.data /usr/local/nginx/conf
1 r% I5 x& X, r( Q+ u为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 X5 u5 M6 k  T/ N5 F

+ N0 q5 `: R8 Z/ _' l1 f2 t% @' H
7 l5 T; ?  U- {( R0 x1 f; E. q$ vim /usr/local/nginx/conf/modsec_includes.conf2 g. p( I4 A/ d% {, O; q8 J2 ]

3 R" f# z1 v( ~0 x8 C  o' D
[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
! o) a% q* C, j* g! i! @

6 {7 o( U: b( z! x注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。5 t# X0 x, H1 z! G/ @

1 x/ |/ f" s5 f8 r) Y" b# D5 {7 b$ X# e
配置Nginx支持Modsecurity
" f; p" V- z8 D) r: c启用Modsecurity
6 R( j3 f/ R+ o% d使用静态模块加载的配置方法. o- ^9 H8 B& c6 u% K' d
在需要启用Modsecurity的主机的location下面加入下面两行即可:
- Q' h! Z, F3 `; h
7 f# K& F# i- J! f% m2 t3 f1 D% Y* h* K- D! m# w
ModSecurityEnabled on;+ C* H1 Q) i4 T4 ^% d
ModSecurityConfig modsec_includes.conf;
) _* G  s- {' R6 y) ~3 O修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 _+ U; M: J/ s  o
/ k8 f4 v( p( U- h- x) P9 y. E& f
3 J+ f2 z, |( B# `
$ vim /usr/local/nginx/conf/nginx.conf% W2 N6 X7 J9 f# `3 a2 u1 X
# K, F! t4 c1 n% `6 P$ a5 C

  H. j+ I1 ~' W) n5 l% r2 mserver {
0 g  D8 B" T! x1 O1 ]# l+ K' h  listen       80;
( v' `* d3 E: s- K/ {  server_name  example.com;
+ e2 ^. g# X8 @8 ?( O/ U. z0 z, e4 c
& Q$ L9 X8 T3 \* x( w8 O  t+ ^9 P9 i# e
  location / {
6 f. i7 l( T( w0 o5 u0 C5 O, D    ModSecurityEnabled on;7 i8 ]1 d( U& A) C
    ModSecurityConfig modsec_includes.conf;& F! w3 g" t: w* X( D8 c$ b$ v1 Y
    root   html;
0 f! H, N; `8 V3 F& Y8 f    index  index.html index.htm;" D: D* \' G, A: q) L
  }
- k0 V$ S* x) U. r% D0 G( U}9 {, c( B8 z( s+ U' t
使用动态模块加载的配置方法$ ~6 [4 i- Y" e. R
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! x8 T& i$ Z/ _$ k5 y" n) q
$ N: p8 K5 O+ t0 _1 c) t+ e* @
4 T! o0 @1 D4 |0 dmodsecurity on;8 s! v: G( v% I; v: A) v0 s' J
modsecurity_rules_file modsec_includes.conf;" [+ \* `6 G9 J* R0 \. p
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; O( h, d1 f+ [) z

. c9 t5 \8 j! Y& x$ i! D7 w
: A! S( x8 h2 T; ?# ?$ vim /usr/local/nginx/conf/nginx.conf  E4 Y0 n% E; J/ t2 m2 m. \, o) X
5 n$ M3 P& ]7 s! U0 y

/ y% B; [/ z4 Oserver {7 \2 n6 e8 S# i) `
  listen  80;- s1 l2 Y* e# w* [
  server_name localhost mike.hi-linux.com;
6 W7 Y7 C( ]" f; K0 o  access_log /var/log/nginx/yourdomain.log;
$ Z) k3 ?, ~  X( B9 h9 \* v
9 y, Z8 v: z4 t2 I
" v8 d; S. V$ Z7 O! t3 W( o7 @  location / {
2 a9 o- K* [4 N9 M- K3 S! c) _/ v9 Y8 [; Z( l5 f  h- M$ T$ O! H8 k

) N$ |, u* _5 K  modsecurity on;
; v. ]% A' {) Y8 t" O  modsecurity_rules_file modsec_includes.conf;
, X! a* I% s9 x7 D8 z% ^  root   html;$ U) {8 k) G8 O- U! x
  index  index.html index.htm;! ~- V+ v8 c. O3 f
}
+ ?: @* J# n0 j% Z* ]; F. W- Z" _}
  |% K. u/ j8 A6 N0 c验证Nginx配置文件
) r& T( E0 F7 n2 H$ /usr/local/nginx/sbin/nginx -t
  U7 S  p; ^) G8 }/ ?nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 |0 p5 n( F. E8 n& ^4 m3 J
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; j2 y* y* G' R, V3 `  ~
启动Nginx; {7 o7 |7 `3 ?( s# C
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
; n& W% u% L. X) j$ {) W, P
/ M, k1 f9 j, x0 c

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


% i9 o% V5 V1 `- L

本帖子中包含更多资源

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

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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