找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10003|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
4 |/ I, e% l. ^1 H8 P; a- k* F9 o
  ^+ q5 S1 z# ?  J# f& [
! q& d3 p) J, W0 g7 n5 ?在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。; G9 y2 u1 u: k

+ B- |0 w; j2 f) J5 D6 H7 R7 z- d, J% l* j1 \- A, N
什么是ModSecurity! L9 w7 a; h; ~( ^, F" ^" T
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。$ V3 |, S1 m" p- J

' `7 P& I, {- c, Y9 u' D! q
# R/ c1 E7 A% t' W8 U1 c  UModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
. g) a: V3 K; q: w8 Y+ [
& `5 X/ m! S; j( t3 R0 c" }. V
1 G( s+ ?6 l6 v# H1 R5 x- KModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。* }8 X$ w, q+ o1 {" u
% _% k- J) y3 G( Z9 }; \  i

1 |. J3 I7 r' t# }. K4 o官网: https://www.modsecurity.org/
9 @/ h& I  ?9 Z. q% u+ p  I
+ M- [6 }  G( Y: S6 v) c' i' K0 i9 T: x( i0 f3 d6 o6 s4 v
什么是OWASP CRS
7 U3 a) ?1 {7 H5 t8 P. w' eOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。% I) i1 o7 M( w% z( Z

- w/ P0 ?1 G$ [. |# k, f) {+ @+ `% a" G1 {( ?
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
0 B" }1 w' m1 r5 x3 Q' J7 v
% y4 m$ m! q1 n! S
+ v/ t- h$ q' k/ y9 W9 SHTTP Protection(HTTP防御)! {1 K3 L6 V, q( e5 \% G
HTTP协议和本地定义使用的detectsviolations策略。6 B7 Q5 E% N/ ]/ V# M# V: L+ b; H
* f* j6 b+ \0 `. j; |
+ p4 d8 w7 I1 g% C# @
Real-time Blacklist Lookups(实时黑名单查询)! }3 E6 t8 f; F
利用第三方IP名单。
; [8 ?3 v+ b, T% c/ h- Z% F( V
: p8 @# S; r: }0 `: L: ?, O1 ?* p$ p7 x$ Q) }
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
% l# h. c! p5 u8 T- m. q防御HTTP的洪水攻击和HTTP Dos攻击。: `+ m% o! V7 G( O

& \( Y# O) U# U' `  I, C. z
- f7 K& N, u! U; W/ S* O& }# u5 RCommon Web Attacks Protection(常见的Web攻击防护). c" ^4 \2 c3 T4 \: ?4 T* b' B
检测常见的Web应用程序的安全攻击。
/ z' l- t. M/ F2 A5 s( A. u& X" d4 U/ |+ N0 }  B

- g; I$ w: Q* sAutomation Detection(自动化检测)9 }0 t: s/ ], Z! O3 Q% B  y
检测机器人,爬虫,扫描仪和其他表面恶意活动。
' `; q" k9 s  ~; v9 D/ v+ r( K5 N& W
* ?+ V. a. z: a8 m/ P6 S
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
' Z9 B" P1 ]5 Y" u* W( }; E检测通过Web应用程序上传的恶意文件。
* E* I" [6 a, g# D. k
* k! d# s8 J8 [4 ]9 W7 `4 ?5 Q1 E! i  W  }! f
Tracking Sensitive Data(跟踪敏感数据)
& {$ T/ T7 z7 w信用卡通道的使用,并阻止泄漏。" ~. M' D8 D9 V$ Z' ]

0 \: T1 v# ^6 v5 d; M$ C) q, q
$ `) F. `# B, N1 L! yTrojan Protection(木马防护)0 }* w  A6 \, r, g: l  i7 C
检测访问木马。4 r0 i* F: }* z

; R; Z$ t" F$ y. A. K
2 I6 j% |4 I# b2 `2 j/ x  QIdentification of Application Defects(应用程序缺陷的鉴定)! d3 `5 A/ ?! J9 f; P! A
检测应用程序的错误配置警报。! p* Z- ^. s$ A0 U$ |

# t  Y2 G- Q. k. m  ?7 a5 Y$ |" C0 w5 H
Error Detection and Hiding(错误检测和隐藏)
! {% G! @+ M- B; I检测伪装服务器发送错误消息。: v3 B" X3 m6 d' D2 M
( N8 C; d5 v" A$ \2 x- C

5 A0 D; |- n5 T& ^8 X安装ModSecurity  Z0 y7 y( k; w3 K* x& R
软件基础环境准备
  U' B# n4 L! X下载对应软件包
7 ~$ q& m( k( o5 ?, V1 u$ cd /root
- G, A% Y3 T) [$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' d+ p5 L  {- X2 f, e: }, u0 L
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz  @5 `$ S; g& A* |: j1 u# O' n
安装Nginx和ModSecurity依赖包  l8 X6 K& ?" `, q& b
Centos/RHEL
# t: y* b& I+ W2 S% R% `
% ^' f( m" p: d0 v" B6 T  H+ O
, ?3 T0 p) _& R2 R7 S$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) D& b8 C1 }4 ~, F: F) nUbuntu/Debian7 K% L" W3 y2 k* Q' M; R( R5 V
2 O3 z( X: }$ a; C( L9 U
3 H9 c3 y/ \% y, a9 l
$ 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
# ~9 x$ F2 x8 k  _4 i编译安装ModSecurity
! w' |1 Q5 k% T* JNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。" X8 V' B- F: i- @8 O. F
, ~8 {1 ]7 N) t
/ }- T; V/ ^$ w" m
方法一:编译为Nginx静态模块) m' @) \" x3 n; G* K( Y
) I0 r. @! C' L, t/ O9 z6 n
% l1 z5 |9 Z+ q; V0 y8 j! H
编译为独立模块(modsecurity-2.9.1)3 }3 p* v1 L) C8 T) T; I
$ tar xzvf modsecurity-2.9.1.tar.gz( Z, j: ^: f, H, q' U
$ cd modsecurity-2.9.1/+ Y% I. I7 q4 ^% K
$ ./autogen.sh
/ g$ I# w1 g: t2 L$ ./configure --enable-standalone-module --disable-mlogc
+ `( Y# c6 L( U" n) W$ L$ make
5 i& {( [% |  e* j编译安装Nginx并添加ModSecurity模块
4 D' {! M/ j7 N6 Q9 H# V$ tar xzvf nginx-1.9.2.tar.gz5 d  r+ I5 X& X5 M" W
$ cd nginx-1.9.24 @: z) I5 ?4 |6 D7 p
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
5 k' ]6 A* o4 y: m7 _$ make && make install
* p0 R6 `% b& y* m方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
% m5 {5 g5 ?& f) `- H, h  J$ Q, }! q0 Y0 Q5 W; k# ^: ?: T) y

2 k. W' {" i4 |编译LibModSecurity(modsecurity-3.0)
  N, E+ `/ h2 v& W( K( _" Y. S0 `$ cd /root
6 S, `. L) H3 s% _$ git clone https://github.com/SpiderLabs/ModSecurity
  r4 I1 J. D6 E$ cd ModSecurity0 P+ U2 Q9 l; I" `) b! f
$ git checkout -b v3/master origin/v3/master
2 H7 ~; C$ D% R, P7 k$ sh build.sh
" ^' D0 }: K3 U$ git submodule init
2 M0 l5 |' r& o! N1 A: @% o  _3 z$ git submodule update
+ ?; `+ ]' V* R0 E$ ./configure( O# |" K- e/ F6 e  K1 U
$ make
( i% `# E& R5 c! O( k+ Y$ make install
" R% E9 f$ [( g8 `& {* O% ELibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
, i3 _3 E; r7 O  \" b
$ d6 y9 O0 ]7 W* t" ?3 H) U5 M( W/ B% g: Z' L& w. k2 s, w3 k
$ ls /usr/local/modsecurity/lib
7 p' a7 `1 p1 T* |" J; m, ^; ?libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" ~: [( H  x) ~, C7 Q& D4 B
编译安装Nginx并添加ModSecurity-Nginx Connector模块% f  I7 A) @. B
使用ModSecurity-Nginx模块来连接LibModSecurity
7 |: \/ r" ^. ]6 b% w
9 D3 g2 D. o* ~6 f; Y
! E& ?* o6 l# z8 P, V$ cd /root
  H) t% ^5 C' M$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ I9 M' ]3 W: @' b, v& T
$ tar xzvf nginx-1.9.2.tar.gz3 h+ u+ @% B; h. T
$ cd nginx-1.9.2* e- a8 A! b! F9 g  [; P3 b7 Z- G
$ ./configure --add-module=/root/modsecurity-nginx& Y: l2 q1 d' i6 B( B; F; u
$ make
4 M" E' u3 R6 \9 O$ ~$ make && make install3 ^9 Z7 @% u# r. k0 G7 o
添加OWASP规则
4 M. t$ l: r, H4 l+ `ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。+ T5 J1 ^  j' R
& ~! i7 x& N% @; U6 k( ]' ?
3 p5 @: W/ ]0 J, ?1 M  P' R
下载OWASP规则并生成配置文件4 v  V6 a" w! y! S
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, C% l; o% u4 G/ N& i1 u" X8 A
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 t7 e5 G/ ~  F5 I( a8 \$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
4 n  V% k4 f, I" r; e4 H4 y$ cp crs-setup.conf.example  crs-setup.conf
2 z* b( i. @: K7 g5 O' ~配置OWASP规则3 E' Z. i. V( X! @( q5 C" x4 X
编辑crs-setup.conf文件
8 d, `0 S+ s, V. x7 o# B! x. r: y
  B3 k  s+ Z1 R6 P: @4 t9 D
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
4 P4 \3 b6 B# r/ R9 S; n! s  j$ U5 \' }$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ F" C! z7 x% b4 E! w9 C6 c$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( _1 J. H3 I0 A2 ^
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf$ S2 k8 F$ c9 B- \
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
/ @3 a, P& X/ u8 G$ m
: S; i& t7 @; F+ i7 j8 T
0 ^5 U0 B1 F" @0 n8 ?2 r1 G启用ModSecurity模块和CRS规则
6 P7 ~. G5 Y6 a3 G4 Z% x复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 J) o. b8 y; Q1 e4 `
# w; r3 ]0 p! _' E. N1 C3 z3 [( u
" T$ g% N1 B& F; W7 H
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 m! Q3 t9 [: o6 i4 a; o0 J3 z6 \
. l, Y) o$ ]* n) G
1 \! B' K! d. H2 R1 D$ cd /root/modsecurity-2.9.1/
) A! h) J" q( o2 C- L0 ?2 _$ C6 C$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
: A# R. _/ P+ a3 V$ cp unicode.mapping  /usr/local/nginx/conf/# A/ l6 n; n6 l. x% d% k$ H$ ^
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。* {# c2 v3 X4 v# Z, k
! X5 E3 n6 n, @

  f0 p$ W, s* o( A4 R, X$ vim /usr/local/nginx/conf/modsecurity.conf+ b$ F& F0 J' y: g' U% m
SecRuleEngine On$ _. e7 O: e) R" B7 y
ModSecurity中几个常用配置说明:
( Y0 h# `& c( x4 [  D# X# j! A: O# d5 ~& k
8 y- E) c0 Y# N: A5 r$ @
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
; \3 K( S- A$ l* X
7 Z6 `% b# I: O0 ]/ E( y! h1 a: m. x/ k  q' X
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 @, c/ V0 v# u+ y- y# I7 ^7 s3 W% p6 k& T( v" T

3 i3 ], i" l# R0 k8 }. K) ]3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。0 N6 T6 T* k% {! o& y) i6 w+ t
7 Q4 y) x  P  c, m$ W

2 i3 O+ j* J# k- |* ~1 y7 z4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" H" p- r/ n; w8 q# ^
2 p3 j. p9 ]* N+ f4 l/ a; s* s  V4 }( J" B3 u4 X  `1 V
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
4 K8 {- Y% t0 i# s
0 @9 Z2 J' N$ T7 g1 C( N' f( t  K) ^+ n5 _
3.x版本CRS
+ ~: C6 S) F# g# d: M$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* {5 [: I8 l7 L$ ]. ?# 生成例外排除请求的配置文件2 T, d) N) y% U4 ]/ J
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf' Q) [0 y8 i* ~) k  g% n
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf" j! N- \. C- g. n
$ cp rules/*.data /usr/local/nginx/conf8 t- C/ I! t' ?( t$ ]8 m
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。0 q2 A# @" a" U

  L5 x, [0 p6 Z3 z& ^* N. X* j3 m/ M6 q0 Y
$ vim /usr/local/nginx/conf/modsec_includes.conf$ W3 F8 O4 v2 W- F" A8 I
7 a' m* a5 [% h& ?* p" 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

- l0 Z: o% K7 \: _" Q: ?1 M% H( i! C5 A# [1 V) A, f
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 [8 a% U7 S/ O5 @3 l

% e; o/ p5 }$ Q. d5 l: b
" s$ V0 _8 _- @. |4 U0 ?4 @配置Nginx支持Modsecurity
4 M' w/ G# N& q* `& E; A! G启用Modsecurity
6 P* R9 W+ Q( s使用静态模块加载的配置方法
1 q9 L- J: r/ y1 f- _4 V4 z0 h. f在需要启用Modsecurity的主机的location下面加入下面两行即可:
; ^  y9 n4 b) M! n$ v/ T
( D  S% _/ b% M! M% f- G" `. C# K9 Y1 k2 Z# U
ModSecurityEnabled on;/ Z% Z& n$ C6 v! M( \
ModSecurityConfig modsec_includes.conf;/ E8 _' Z/ Z4 W' v
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
; t4 F1 O6 m. V/ t9 @, g2 g/ H! b' m  {. h/ {; U

  k/ [( G8 s! \$ l! [8 Y$ vim /usr/local/nginx/conf/nginx.conf
+ b- [7 B& h; _! V& I1 u6 ]. F6 q# Z
2 A- v8 _6 N. m7 _: I# C9 g
server {
. ]& ~2 f6 X" h, D! p5 i  listen       80;
. h" |0 S2 A, G, K7 g! o/ P  server_name  example.com;
/ c. w( h1 }4 s& T8 {/ d: j. e, B: x; l4 s) ~! }* J

/ F' u$ ~4 j$ y5 z! }, |7 _# O% s/ |  location / {  ~7 y) {1 t2 x2 ~4 N9 g$ ]- T4 E
    ModSecurityEnabled on;
. J5 i9 y2 S# k' G- w( ^    ModSecurityConfig modsec_includes.conf;
; W$ g8 V9 j$ ~* X( r    root   html;
, @# Y+ K" r* L- q7 p' X! ^$ `* D    index  index.html index.htm;( O( w9 R1 b4 v* Z/ ^  ~3 Z' X8 c7 q
  }
3 c6 H4 N9 x9 e4 ], B7 }% _+ [: t}
, Y, i; I) z9 A+ ?( A9 v+ u5 F0 N9 J% C使用动态模块加载的配置方法" Y. V& y2 a. ~. l( R2 J! ^6 v9 G: m
在需要启用Modsecurity的主机的location下面加入下面两行即可:* M( {3 {: O; l0 |
" d4 Z( K6 n, r% |5 p3 @

& @, x1 V0 K; r  r' C' R; nmodsecurity on;4 _# Q- \8 Y1 o
modsecurity_rules_file modsec_includes.conf;1 @9 C# d) Q( N; M* V/ @
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) k2 {. P# r+ P1 n1 e5 h
- x; {3 K$ v% s) ?8 q

/ T1 E+ e' }- z: t3 ~  Y" z) R$ vim /usr/local/nginx/conf/nginx.conf
" e1 ~) ]1 ^8 V: V
2 X6 O+ O+ X8 G) j& n. s; M5 ^& O; v6 e8 u! K, B3 V3 G
server {
" B6 |4 M+ \: Y1 c/ R& I( g  listen  80;) C+ Y" a) j8 ~/ J  j5 J* a
  server_name localhost mike.hi-linux.com;' a7 ?! L" A% K
  access_log /var/log/nginx/yourdomain.log;
" i/ C& {1 ?; |8 x' t7 F+ K' W* M; `% J" n- f& q

9 Y+ B1 [$ U# p8 q$ Q0 i, {  location / {
8 h- P$ }" R0 ]  a4 G8 e! j
6 K( ~5 j% s3 z, B) n8 u; o/ ^+ n/ k2 R. T; i' z! M) Q
  modsecurity on;
3 r6 f+ a& ^1 l; Z3 k6 B  modsecurity_rules_file modsec_includes.conf;
; o2 L" s) a1 c: v' k  root   html;
) `0 x0 m6 G; o) H, {. u; D  index  index.html index.htm;
7 _; U& o1 u# `! i}
* J* Q- r8 H# v* _+ a" J% a}5 B7 c1 [# B  L  J
验证Nginx配置文件- A0 ?- x% f' w; u% L& _! M1 @( i; }
$ /usr/local/nginx/sbin/nginx -t+ ~" ?; f# c: Y3 I( d% ?. U
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
6 I! x, H5 s1 Q. R) dnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful4 }' G% f. L  w
启动Nginx2 i1 P! {+ f5 ]
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf8 o5 t! a5 H) g3 N

8 {% l6 G8 V& N! F1 R

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

% P8 Z% c& J0 O& @

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-21 23:56 , Processed in 0.046490 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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