找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12110|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。$ q* y" D' G: ]* z9 s! h7 \
  T  s/ [, }' H. ?1 d6 J7 S+ v

7 Q4 C* v0 @9 `( p$ i' a6 T8 ^; h在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
, d, V/ F' w! b) |& a$ }- \) m0 l4 ?4 [

2 k4 E) l/ ?- \* S# s5 D什么是ModSecurity* F  {5 R* q  P/ d
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 k1 V9 E! Z5 x% U/ o& l* O: j/ g
2 c& ^; k6 \" ?9 H, R) l
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 }- b' U9 u4 z7 ^
5 e' T7 o! B2 C
4 {) X1 e. X0 K5 Z
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。1 W' b7 L1 B. H3 b5 R
/ r6 X. M6 a3 @7 O/ N( B2 _

: O3 K: p0 t, {  A/ l) j. e官网: https://www.modsecurity.org/  z' v* [* U5 U

2 n2 O/ |' z& H6 i# r  c" V) o0 Y' v) P# b
什么是OWASP CRS
1 C6 ^+ r& L1 Y/ L! yOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  f- p$ Q. l+ I3 w! ?
8 j1 \( C+ H# Z% \( q
. F9 d" i; ~. {. ?ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
8 f" w8 S/ \$ Z$ W8 n5 ^! ~4 r5 ~: H& ~

& ~+ @% a* T( Y# ~, EHTTP Protection(HTTP防御)
; f3 @+ Z, i" Y- j; S/ C3 gHTTP协议和本地定义使用的detectsviolations策略。
  }$ w3 z& j( U! [+ F, O+ v: `9 m; o. W; a( m

. N) p( G, y4 @" GReal-time Blacklist Lookups(实时黑名单查询)
- |# }: R5 i0 E/ F% C- T' {利用第三方IP名单。
' S2 _/ k6 \' ]8 g
- W' P, {+ E' T! w# b; i* z- W& E2 f* n5 u5 ~$ n9 Z
HTTP Denial of Service Protections(HTTP的拒绝服务保护)0 g4 f. G2 A. S  Q. }* M. n. F% q
防御HTTP的洪水攻击和HTTP Dos攻击。
' }: T: m% q  W" H; y! t
! b# ^8 B/ G) E6 a8 S3 t- T2 R1 G1 [. ]7 }7 y
Common Web Attacks Protection(常见的Web攻击防护)8 n5 F4 D+ ^1 a9 Y$ P8 }# M
检测常见的Web应用程序的安全攻击。
9 r3 J& t3 H( R  k. q! m% u
+ }( x( O! u# X6 \5 B+ H( ^3 a% M7 g! q
Automation Detection(自动化检测)  ?' y! B$ X: o# `4 {0 W2 a4 q6 x% K
检测机器人,爬虫,扫描仪和其他表面恶意活动。
* e* u4 L  U3 }, w. W$ c! B  @
) ~: v) q5 |' a5 I; }* O, W) B8 @- D) r: h' q
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
& i6 I, W3 y2 P7 R' _  _. ?检测通过Web应用程序上传的恶意文件。
3 s- P2 b2 L2 A3 M
. u; L- `  B9 n% A1 i$ H0 v# [2 i' b7 F8 F$ o5 s
Tracking Sensitive Data(跟踪敏感数据)7 b  G. {3 l( s
信用卡通道的使用,并阻止泄漏。: ~$ w9 O( k+ z! \. `$ F8 q$ g# L8 Q

5 v6 A) [0 \) z7 u0 D& g: ]7 y! A( f; y, A6 U( R
Trojan Protection(木马防护)
* f6 a: x2 y1 S* _0 O- U检测访问木马。
4 S# D; k2 F  ^& J5 W: ?
# P+ F4 I! b1 @9 ?8 v, ^6 m% j1 l/ U# T7 l3 D  j; y( R
Identification of Application Defects(应用程序缺陷的鉴定)1 K  P% x1 @# O9 v7 M4 b
检测应用程序的错误配置警报。
3 S! w1 r. v1 t% r, [- B* {: |, j9 x# ?* C

' S' E8 c5 B. M! u/ ~5 B4 dError Detection and Hiding(错误检测和隐藏)
* O3 b6 ]# n4 Z2 x' @  E5 L  n检测伪装服务器发送错误消息。9 r. }' h) l8 D+ Y4 B- h

% D" s' P; G9 p7 v: X  H; x/ _+ O* @; [, y6 \- T: t0 A& O4 O7 s# J
安装ModSecurity
: Y* O' ]' V! Z2 {软件基础环境准备- F+ z1 E5 G8 J( `0 Y6 d
下载对应软件包
1 h3 p! Z: ^& Q6 }1 e- c% i$ cd /root
# r! X) Q# g: G6 @) v$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
- o0 X: \! A9 E4 ?9 V  c4 P$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
% d8 L1 C, K! a- {0 R$ F安装Nginx和ModSecurity依赖包
' h$ @; m0 l8 o; R8 eCentos/RHEL  W  S7 o6 Z6 q# S
$ X6 o1 r* u9 n# O
: P3 T- O/ i( q/ ~3 f) l
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
4 j" \0 x: {" y) I& p$ i  k$ V5 SUbuntu/Debian1 k% ^2 l: G3 o* M

# A& R3 Z; H3 h+ z$ Y3 a6 c7 t/ u! G1 a) ?: ]- m3 U1 C
$ 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++-dev6 T2 k) @4 e! {, |4 }$ }
编译安装ModSecurity
  n' W" G/ e$ S- s: R' Z9 SNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# x+ |4 j, F' b0 |( [* C3 x9 ^7 |3 `2 x7 k# |
) a7 K) v' \; Z% }  z
方法一:编译为Nginx静态模块
9 e; ]) G; b2 k
5 }! A9 C( o2 a' I' ?: F1 c( p* n+ `; K
编译为独立模块(modsecurity-2.9.1): l6 p" f9 x0 N# p. J  j$ O# d
$ tar xzvf modsecurity-2.9.1.tar.gz
- D4 ]: d. K) V& y# R+ K1 m& x$ cd modsecurity-2.9.1/
: k5 x6 \, r" Q$ ./autogen.sh' {) O3 ?( k' `1 N
$ ./configure --enable-standalone-module --disable-mlogc
: {2 P1 \9 P4 X: A% q$ make
, v! Z0 M0 P, ~5 C# P编译安装Nginx并添加ModSecurity模块2 s6 w" T* S  F/ U- W9 o
$ tar xzvf nginx-1.9.2.tar.gz, S( n: K* V8 i$ K- w; v4 Y
$ cd nginx-1.9.2
: O% G7 P4 D' H2 a6 s: F2 r- y$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
0 U8 E, X7 k$ q8 x* M$ make && make install
' i+ }. O: r' s2 [8 p  @方法二:编译通过ModSecurity-Nginx Connector加载的动态模块5 ]& e+ ]( P( d1 v; o0 G
5 B. |: p- @7 z

5 E' W( r# T, H编译LibModSecurity(modsecurity-3.0)3 b& Q" h: C: k
$ cd /root
& G7 }0 |- `/ ~( [- h$ git clone https://github.com/SpiderLabs/ModSecurity) F8 @7 t- d: E+ _* R1 G# k# S
$ cd ModSecurity! O- e, K. s$ F0 u
$ git checkout -b v3/master origin/v3/master2 K, C- M7 m8 r& b1 P" G, h1 |2 U2 h
$ sh build.sh
2 H: _# D' ?* ]6 e) N8 h& V$ git submodule init; q0 }( l! M" ?4 m- G4 I
$ git submodule update: q/ h# V. W3 B3 D, t6 s' Y
$ ./configure. U- u7 t; g  Q& D
$ make
/ i0 S* `6 W1 Z3 @% A, ]$ make install2 j) C* |# D+ U3 J& u
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。' t4 s4 D. h+ ]# {9 D6 s5 j1 n

- j, V7 \1 Y1 P$ t  k7 B- n; V  S: l, g5 o: U- K! [' h
$ ls /usr/local/modsecurity/lib6 z/ l# l* v; R( E' g9 a; Q& O( i
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
0 y  U& Y9 a# c7 g编译安装Nginx并添加ModSecurity-Nginx Connector模块
. N4 `- v, w. A3 _, [使用ModSecurity-Nginx模块来连接LibModSecurity. y5 m7 K- o: `  K
& l  q" e! D  W
9 Y& j' {$ m0 M, g3 e8 v- d# {
$ cd /root, _6 C8 J; S5 q# k; `
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
0 c9 F2 P' X( K( s' S: p$ tar xzvf nginx-1.9.2.tar.gz" p5 V' i& P1 u8 D& l; x
$ cd nginx-1.9.2- a5 Y3 V2 o/ \2 A( A
$ ./configure --add-module=/root/modsecurity-nginx) m0 ~; t9 x& y: P. c
$ make
! k( p* @, D: P7 `- c( y$ r' m$ make && make install
: r: ]  D" N6 R$ z5 g# k9 P添加OWASP规则) ]; i# @* x/ n! f- E1 w
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。1 U4 y! I1 E0 V" K
  C' c0 o+ v# N, N4 H

1 w) |# e6 q3 V/ L下载OWASP规则并生成配置文件1 |0 M4 w* n' S: U
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; i7 F* X9 s* c$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
/ J- `* i# \: v- L) Z) h# c, w) @$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
, {4 {  o% S. p/ v( O2 i7 H$ cp crs-setup.conf.example  crs-setup.conf
' N" q5 e+ h( y7 S" X配置OWASP规则
' T+ v4 U  m8 A. ~编辑crs-setup.conf文件$ e; c8 X' ^" @  T- s& x3 O2 q8 f
# C) g0 C1 S- m

0 a; B/ s$ b$ h# @" h$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ z5 |3 k3 m5 e. ^$ z. K2 {! D
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 P% i6 j4 @( }6 q* R1 Q$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
" J7 U# |7 v% o1 N$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
$ v7 f0 |/ I0 N1 Y默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。6 s  P$ ^+ u3 x' j
6 c) `2 F2 l. m2 @" Z0 [

4 E; ^6 c1 H5 u8 c* S启用ModSecurity模块和CRS规则
2 S( D+ _( I+ @2 b9 u* e* Y复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: ~7 |4 s' C5 y
" L! D5 u' J: t7 Q4 f# C5 }! T% s4 F
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
* y2 z2 n. X; a. \: i. `0 i3 G3 S+ ~' G+ T) M0 r; D- h
4 F6 O& T+ P9 z7 W2 Q! V
$ cd /root/modsecurity-2.9.1/; F: ]# }, C9 ^* K4 e
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  - D) Z' h: m2 F" z3 ~& T
$ cp unicode.mapping  /usr/local/nginx/conf/8 t) t- d% T3 s! g/ F  o# q
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
! z/ f; y# M- f  Q
7 X/ }7 Y, y% W: B8 }
8 z! l5 M+ K, t7 m% z$ vim /usr/local/nginx/conf/modsecurity.conf( H5 O1 t2 `& }* u. W2 G# j; l
SecRuleEngine On
6 C3 ?$ j7 X0 H/ ]2 X, `9 kModSecurity中几个常用配置说明:' A% k4 `' g4 M: }0 k; @
. d6 u6 X' x) ~* D2 \( l6 A
- Y/ ?9 e2 R3 s
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。. Q/ `9 h$ R: }, ~

- \( m' @) x6 a1 x/ z9 U7 c
1 f/ N6 K7 r2 M' ]0 _6 A2 A2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。0 ]% I4 y! D# U. Q4 `9 u' J
1 I6 `1 T" o4 D
' A$ d) {) E, k
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 \! i# H3 V$ b6 i$ _9 Q/ U
" W" E2 r, _% Y5 z' Y  ~# O

0 p( g/ K- h: t) x4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; Z0 b9 d9 n( W2 e. V' t- J
# i7 ^/ x) b+ G" v: Y% D) {$ b5 t+ H7 n. ^8 \* A( y2 }. h; b
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
3 _/ ^5 y8 C7 u7 E) v. J3 L5 m( S# I/ o# O
( M7 G# h- a8 D: d
3.x版本CRS2 r; E0 a; `- Z$ c: _1 z
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) c% ^9 U* j7 n" Y" ~# 生成例外排除请求的配置文件
! k# ]  m( `4 k. j$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
% ~3 q- j& W# h  A: x5 e$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
. D2 K6 n) E5 t" j" V" r7 g$ cp rules/*.data /usr/local/nginx/conf% U7 v% J$ m; ?2 x! h8 `7 q
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。* f) i7 w! `+ X# U

1 @+ y# }3 Q* ?- |& T
0 S9 B/ Q" f* S: ^2 b$ vim /usr/local/nginx/conf/modsec_includes.conf$ R3 {, j8 [1 N
" G" p1 t# T, [: _6 q7 A
[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. o0 U' w3 x4 G+ B  z8 w) G5 j( O5 F* S
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。5 }& z4 A' m- L% r; u  b

* @# F) z) H/ _" w/ w
$ z7 P) i4 `+ V9 D配置Nginx支持Modsecurity
) F  k7 f8 ^, A, H7 r+ W启用Modsecurity
+ S. e. s) @5 P* |4 }使用静态模块加载的配置方法7 e/ G2 |8 C2 E/ U. F/ I9 J6 C- m
在需要启用Modsecurity的主机的location下面加入下面两行即可:: Y. y! T: K7 x" R; r8 P
/ K) a+ m3 w  I" c1 A1 @" o+ i

6 ?8 E/ h8 a0 q  S- F# YModSecurityEnabled on;0 J! a& N! Y0 k' I9 ~
ModSecurityConfig modsec_includes.conf;" I! A$ [" J1 L! u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。7 B; X4 Z7 @' a6 {7 n, v3 l7 r' U
$ R0 J6 e. x8 O  w& r% E3 ?

4 p7 Z' k, ?) r% ]# Y1 `$ vim /usr/local/nginx/conf/nginx.conf
1 z, Y! r6 {' x. A% P) j, N0 x: Y+ z+ d1 p5 F& g+ c
: X5 x$ G1 s7 c. _4 A& N
server {
( j1 u: Z5 w8 ]4 ?+ u5 ]5 p$ l8 [  listen       80;
) z: L' {0 {+ b  server_name  example.com;) O* X+ i) [# r. U  n

6 s, v4 ?5 }, U: U5 L
* ~, H$ q+ V2 K9 m$ Z; W# t6 w/ K  location / {$ e/ a5 Z% t; G* S2 L
    ModSecurityEnabled on;
# p( H$ T& E" b- |0 Q( N) e' U2 B    ModSecurityConfig modsec_includes.conf;
! `  b: r7 R' f    root   html;/ [0 k) u0 y. ]& x
    index  index.html index.htm;
' n% Z8 w3 D" l) w: W  }
: y, A: N2 U( m$ t& m  h}) T" c' w, [9 K+ F0 j' p8 o6 m3 h
使用动态模块加载的配置方法. {( A* I# L  ~9 _8 ^$ ^+ d
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 A: B! X# \1 l, q$ y% {3 c
4 p) H( N2 U  A- ~' v: D

) b0 T: [$ ?+ {9 @- k2 Bmodsecurity on;) O# S6 s/ u5 Q  W4 Y
modsecurity_rules_file modsec_includes.conf;7 E- ?& |1 d3 @) f; n1 E
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。: \0 ]3 y, x' b3 W
% q) y" Y! w: P" p* ?0 ~. D
) h$ I! L5 g& w. B, p
$ vim /usr/local/nginx/conf/nginx.conf% T8 D  n" f$ D2 u, P

1 D: t! I. K, h, i1 l4 \$ Y% I! u6 K$ ~2 I8 F2 m( Y
server {
6 x$ ?0 {' F3 ^6 [- m- N  listen  80;% y8 g. [9 |' ?  t9 d# p4 \
  server_name localhost mike.hi-linux.com;
! L5 m) M/ Z" S1 U9 {' V  access_log /var/log/nginx/yourdomain.log;7 v$ z0 x: M1 v4 O- ^; G
4 z6 |6 E( U/ |( m( {. Q3 p

" }% e9 o3 A$ L8 P5 y- s$ I  location / {
" j  m5 F; r' j) l
; D/ H7 N0 |4 c3 F$ v5 k' s  B
; y# \: H* _7 B, F$ M) B) o9 B  modsecurity on;
  F  a6 S: H( L  modsecurity_rules_file modsec_includes.conf;% j/ i0 O) g/ O; v  v8 r0 q
  root   html;
. w; J) d. J/ o  k/ s  ^  index  index.html index.htm;
" E" N5 w  l' w+ I; F9 }}
- S+ o8 H2 @; B. j0 x& b}
$ L% z, L9 C3 j0 z! h/ I4 `% \+ x9 c验证Nginx配置文件
9 _: z+ |- d5 T' B( b% f$ /usr/local/nginx/sbin/nginx -t+ S' g# k% D6 t7 L$ q. H
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" C# C' g- j2 k, Q; L" s
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 S; B7 g) u2 X+ u
启动Nginx* X- s$ F$ a3 u0 P2 I
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
8 R* T! R# z; G. G& z( z# x7 u6 v' N- e; }: V

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


3 A- \" W- G7 q% c: A

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-17 12:39 , Processed in 0.080664 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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