找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10845|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。! `/ ?- x0 t# x2 H3 Q7 @

; @# Q, q, T0 Q% o4 Y! @; z7 L
4 q4 V6 c/ r) E在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; S! N+ s( L0 S! |- k7 j! N
4 k' h0 q8 ~$ I) i" E% @
. ?# X& Q# w3 \9 V" n; a0 ], @% w什么是ModSecurity
+ k  V+ v  ?' ]  a7 s2 [ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
0 L. U) h$ t% p" a0 I
& q$ ?' @" i/ ~8 U( ~4 s! q* G3 q. G
! ^0 ^9 Q$ |8 U9 I4 d, vModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) H: n5 J7 `- `6 h& N7 p
, h" R- P+ Q7 `5 {$ T8 n. M

; E4 k7 g4 l, x: U& S. F0 k& k4 kModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
# i9 O. w- a! d9 g4 I) N, m  O! r$ {7 ], w

7 X5 J" x9 Z( g0 M8 ^' ?( }官网: https://www.modsecurity.org/
0 z& B0 \. E1 u' M7 ]* L
7 F" R# B/ q0 S% N
$ ]: @( z2 V, [1 R2 d5 a什么是OWASP CRS
. t+ V5 J* i6 \1 {8 NOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
4 w* A6 F% U1 z6 u2 l: m- O5 b# X2 D
( ~/ p7 W+ a' V) {/ q/ s3 l# A6 N2 I, I2 \
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。# o3 Q( F, m6 A1 E/ v  H

# V8 f, ?: i3 Y7 m" {$ k( S; P. T+ @, l. M4 v" P
HTTP Protection(HTTP防御)
7 k& }3 P' m. i9 D4 i  O, [  FHTTP协议和本地定义使用的detectsviolations策略。) e: U& G7 B. a
% d5 v" b, C# z

, W7 ~" \1 C* _& MReal-time Blacklist Lookups(实时黑名单查询)3 E: i3 C; c1 I6 j1 B$ L
利用第三方IP名单。
: E% ]; [) d' H, [# S, x& o; K; z/ {; t

3 N, ]. d4 v; D. z! n8 x2 [HTTP Denial of Service Protections(HTTP的拒绝服务保护)
* Y1 O( Q2 N$ C) ~; S0 n  u防御HTTP的洪水攻击和HTTP Dos攻击。
5 Q" j+ x) a% T  ^) u+ i% n  T6 L( B; B  K2 o

0 M9 }" v1 {7 G/ F) G/ ~Common Web Attacks Protection(常见的Web攻击防护)& z3 j* l1 \& E% g5 f. k
检测常见的Web应用程序的安全攻击。
) ]) p5 U- t  g( r' x1 Q
: P8 B8 R+ U# H) Y3 M$ i  T
; v/ B1 A1 b- }- d8 NAutomation Detection(自动化检测)" _$ L; T4 y5 ~! e, ~
检测机器人,爬虫,扫描仪和其他表面恶意活动。
! c- N% R/ L7 d2 n
8 l) N2 I( E* @8 s
6 k: ?: e5 j+ C/ n, y( ]Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 Y5 U) m$ }# Z; W5 C检测通过Web应用程序上传的恶意文件。
: _! E2 S: _# q- C% U7 g$ n
( T) F- n2 {. L% d, f9 [+ |" A! F- E$ x4 |8 o0 |
Tracking Sensitive Data(跟踪敏感数据)1 t- Q# }  C& j/ t
信用卡通道的使用,并阻止泄漏。$ \5 }* i! ]. ^4 a( }
& \8 m$ m  V! h; C  [

% f8 ^/ o6 Z; F2 G8 CTrojan Protection(木马防护)0 w9 ^  k  j0 ^/ a6 X4 ?" n( V
检测访问木马。* J& p) o7 I! J+ I

* a! ]$ K( Z- Y+ p' @8 f$ Z3 p! S2 V
  j' S2 @1 t: t. w, j: h/ QIdentification of Application Defects(应用程序缺陷的鉴定); Z1 U+ b% l& w, s. `: ^5 y  I+ t. t
检测应用程序的错误配置警报。
; D5 E5 O# ~" e/ e. H8 r
& K  q3 t# c5 ^: P% j8 @: J+ \+ B: ]) h0 J: H. @( R; Q6 W! g; O* i
Error Detection and Hiding(错误检测和隐藏), [6 V+ _" K: `% S2 `- J
检测伪装服务器发送错误消息。
6 l3 h5 H5 ?2 h9 u& r- O$ x0 g' |; v7 [- ?$ B
$ v- s8 W- N; V4 L5 o! I' f
安装ModSecurity
: e9 M, P# }; \( R# d& l0 [  D4 q; |软件基础环境准备) c5 D7 q5 J# Y2 a6 P5 ]$ M7 ]# v
下载对应软件包  G0 g+ A& l6 U& {$ E
$ cd /root
7 ?& ~+ k8 j7 J3 \2 d5 L/ P$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'* m. ~- E2 {7 D  D
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz. k8 v: s4 L/ A, x. s
安装Nginx和ModSecurity依赖包
+ R5 r3 ]8 Y. x# {8 k6 {Centos/RHEL
0 i1 f+ m  u0 t# S/ p5 c" l
/ V  |9 @0 b( m+ |3 g! Y5 |5 K( v- i/ r& ?) G0 k( }
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 T& l! z$ c/ ~8 `  @" u/ p2 R
Ubuntu/Debian$ z. ?7 s1 V3 |* d4 ~' j0 c

  R9 `. q% X  Y0 K& `. X
6 m+ {+ W- _" |3 w9 r! e7 O$ 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
# ^( {7 h1 G# S6 C编译安装ModSecurity) P& V2 y4 q, |3 B, w8 w8 p$ S
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
% F8 Y" e- x/ n  _1 _- [1 c- D8 W; W  ~- [* v
, h  ]  h1 k: k9 t9 Y) _- m+ O) f6 e
方法一:编译为Nginx静态模块: W6 v7 b! c4 f

% m4 ]- w, ~: w4 N4 ]9 o- @, S/ b8 K5 l# ~7 a. q# G
编译为独立模块(modsecurity-2.9.1)% W% R" P( n. d6 _# ~6 \" g
$ tar xzvf modsecurity-2.9.1.tar.gz! o' ?3 z. t! N& q7 k+ q
$ cd modsecurity-2.9.1/
( D# h7 m$ A9 j; n1 W$ ./autogen.sh
) K* X/ T9 t  @; S' L$ ./configure --enable-standalone-module --disable-mlogc4 S2 X; e' `" d% t6 s, W
$ make* \) A9 Q8 W; j0 ]- L
编译安装Nginx并添加ModSecurity模块
6 b, |  ^! g. I. l4 a  V; H7 _$ tar xzvf nginx-1.9.2.tar.gz
/ _. l1 q' `% C8 s2 F$ cd nginx-1.9.23 s9 W3 U8 _1 Y" s- s% X
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/, }! H' z( ?; i6 U$ g+ ?
$ make && make install
- A8 X; G4 C/ ]+ f方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
2 H% k& ~' ~1 h) }. k& \
8 y, C2 C6 H. M
! t6 x9 b( U5 C/ y. }( o编译LibModSecurity(modsecurity-3.0)
# |# u) F) X+ F  ?1 Q$ cd /root
$ \8 C1 [6 L. p8 n* P) g3 a$ git clone https://github.com/SpiderLabs/ModSecurity
6 H% {3 N7 q; t7 Y$ cd ModSecurity5 E* F* K) |) J  z- Y
$ git checkout -b v3/master origin/v3/master/ ^. O8 W% v0 b+ `" _
$ sh build.sh# c0 F# v; s9 B* m' J7 l
$ git submodule init
7 H$ C5 d+ h4 \+ Z1 ^$ git submodule update. b. Y3 Y- g2 i1 [/ A
$ ./configure$ o4 n6 H  n' a# ?" [0 d
$ make0 [6 s  x) u  F/ k4 h- e
$ make install
1 V$ O! Z' f; k$ O2 C2 L, ALibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。2 k. w( N" z$ Q* ]/ o( _

8 b" A; V/ w4 N8 X6 I
- V3 q( {& |) |4 C( M$ ls /usr/local/modsecurity/lib
1 ^  y7 b$ |) @& Alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0# G/ }9 a% j: r
编译安装Nginx并添加ModSecurity-Nginx Connector模块
4 F7 a+ A  v. o/ W. S使用ModSecurity-Nginx模块来连接LibModSecurity
  {1 {$ ~" F" {0 c* ]* {
5 B+ e# }! F( K) {) V/ q' o8 o0 l7 ?1 ~; r0 T
$ cd /root2 Y9 |: V1 C) R6 S' S! p1 g
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx4 R2 f  Z/ m" Z2 E( F
$ tar xzvf nginx-1.9.2.tar.gz. c0 n$ Y- n0 k& O
$ cd nginx-1.9.20 `$ ]$ t' D; @
$ ./configure --add-module=/root/modsecurity-nginx
. N' U0 W3 P) s; H3 y  D7 [$ make' ^4 K3 F# H0 o+ t1 d' V
$ make && make install
* ^+ o# M! U" O! _添加OWASP规则1 r7 l# _7 Q7 p& M1 H
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。& n' U0 ~8 A3 J( d# f. b" s1 ]
( G: I. i/ u  }3 _

) T; c9 n) P- o9 I下载OWASP规则并生成配置文件% G. Q* c; |) t, a3 G( j) `
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git/ R6 L  l- t( ~  Y7 {5 R
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/5 Q7 g  q, q( S/ C9 ~
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
" G: [6 `, w) O% F: `$ cp crs-setup.conf.example  crs-setup.conf- c$ G5 d6 I# Q7 O: T
配置OWASP规则+ e) d0 M! w, E$ ^" m; D
编辑crs-setup.conf文件  r% Y2 B0 w: Y2 x1 V) p
- Y4 m& W0 |5 Z% {
5 H2 B+ m% O2 i2 g5 F
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; A1 o! v$ V3 ]9 e5 [5 Y$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
* s0 l8 m. y- j5 w4 ~/ A$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
- o3 o, e" U9 F' [; A$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf. u5 e; R) @5 `4 }" g7 q- a
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
$ y5 h9 m5 S( y" B' }( g/ h5 y! |* \* y% w$ f8 N9 f7 P: l
! k- r- o- g9 H3 Y
启用ModSecurity模块和CRS规则
; P) u; w1 F/ T0 b复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& b; x" Z! Z2 t/ m9 G
6 P8 A- V0 n* R" [3 C
. L/ p: p* A' b; r# @! a; lmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 W: k& u8 k$ b# s6 i5 Q
" c1 O; x( w( z& S2 n

9 |. D0 x' Z+ ~$ cd /root/modsecurity-2.9.1/
& g5 J6 o4 e4 u! K3 [( G$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * N# C' G: X5 E# t# T5 A5 s
$ cp unicode.mapping  /usr/local/nginx/conf/
+ y! V# F2 z. c6 C# A, J将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。0 m4 B. n/ I- L( L

8 }' G: E% Q0 v; }; ]: Q) d5 W3 U* a
$ vim /usr/local/nginx/conf/modsecurity.conf. w4 m3 F% v& O4 a# d; O) W
SecRuleEngine On+ _5 I, N% Q5 f/ x& f% Z8 f
ModSecurity中几个常用配置说明:
0 N3 \, t7 U2 J2 K& F2 z5 p9 T+ y7 K" S& l: l+ }3 e4 e

$ S4 ?: C' o3 k9 m  r; {7 Z+ M8 @1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
0 L, o8 b1 [' l" n" X/ z) b8 Y7 ?  {# k. L

8 F' y" s# O# d) V" H) a2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。% Y3 h$ e% s5 a
5 q2 T2 U' Z# M& w/ n/ g

( N" Y3 ]& s5 l. \1 U% `3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。, Z7 H8 D% s* B4 h8 s, S

  e; V9 U9 C( h4 X( V: T3 _* h- o  h1 w% v" o' @# h, G* {! |
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
4 B* R/ i  U( g# ]9 C+ `6 w( [7 R+ {# e/ i+ ]) K' L

4 }8 ^" x" n9 Y$ |' I) C在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
2 t; N" }# c0 |, ^- E3 r. m+ v2 a6 [5 Y+ l& I' Y: e
; k) p8 O) z+ k
3.x版本CRS6 _. D4 d; k0 G( Z3 Z' y( C
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs' T+ j' ~, z& [/ r
# 生成例外排除请求的配置文件: r2 Y  x. e$ C. p: e
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf% S0 f& @7 H( i. [
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
# a+ T3 c- W4 p% t! ^$ w% O$ cp rules/*.data /usr/local/nginx/conf
9 x* R+ a8 A9 y1 X& T为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。( ?# b2 B, S. Z" _8 a3 g+ ]

0 _" Z$ ]* K6 P5 d4 D8 \: b. I; f# d* I) ?$ Y  m" S+ _- X- |
$ vim /usr/local/nginx/conf/modsec_includes.conf
. _, ^1 Z- s5 {/ D  c. F& H; _* {% T; G: @$ I$ q
[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

7 D4 Q' D1 c2 g0 y( b8 k: l
2 `4 R  \! l' x1 O9 h注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
4 q5 t- I9 J7 L. M* N( }0 O! `& l4 D9 c8 ?, c

# y& J/ n; b8 ~: G' |配置Nginx支持Modsecurity0 j/ a* V) [1 k+ z" q+ ?8 h
启用Modsecurity2 i# N4 X6 G; `  d1 I- B
使用静态模块加载的配置方法
" N7 h# K% a/ Y' C在需要启用Modsecurity的主机的location下面加入下面两行即可:
' {8 W: E  @$ c- R- n# F% o0 D5 q: {  n$ q" |( z: C# ]
( a  k! U( w- f; ?# l
ModSecurityEnabled on;  o3 e1 g& @# L* f  I5 b& l
ModSecurityConfig modsec_includes.conf;, c" S8 r7 ]1 X6 P
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 {6 u0 q3 G6 o  s5 O6 A( v$ \

+ B* V; K0 _  S& @5 ^; a6 m$ d- _4 Z$ M3 @9 A
$ vim /usr/local/nginx/conf/nginx.conf4 i9 _+ }" K) V( y

1 ~  o  q) M3 N6 ^- B3 ]. K+ V
! Y" Y2 h9 d% |9 u# Wserver {. {3 I3 H( W/ ^/ q7 B+ M
  listen       80;1 [: b. e/ W' |: x6 g/ V
  server_name  example.com;6 W, ?! ]' _0 F

6 U, B0 ~+ ]% X7 I: K
$ R3 f% @3 U$ I4 \, P# ~* o! y  location / {- k# J1 p) K* J/ c" ?2 Q/ b
    ModSecurityEnabled on;
, j" w! v- e, Y    ModSecurityConfig modsec_includes.conf;6 g7 J) F$ ~3 M( K
    root   html;
" s5 p+ M! C8 }! U    index  index.html index.htm;
4 g. c* j% a4 {) B3 r& F. p  }# A. ^$ g) Y% s  f4 O
}
) w3 I" ?: l7 l! T使用动态模块加载的配置方法3 C1 t: L& `% h# Z$ {
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 r0 ~! z: q5 q( C6 m
& m  ]" ]3 V1 q3 x7 {8 _/ h  ~; I8 [% W0 h' J% Q' X
modsecurity on;
& t& x' w& q& f# amodsecurity_rules_file modsec_includes.conf;
* B1 T1 x- t5 Q. l& U修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& d  k4 ~- Q) R& r$ P
9 S$ I9 f, T! t3 ?; d0 n# U' P# c$ G+ e* ]  z4 t8 C1 A
$ vim /usr/local/nginx/conf/nginx.conf
; V/ O9 b: r+ H- v4 ?' a7 v/ Z! `, I/ q6 v

/ m' [  M" O5 T* J  l* {& _: gserver {
& N3 ~- G5 X5 v6 j* C+ Z! a. l  listen  80;
4 v$ y& ?7 Q! Y- Z  server_name localhost mike.hi-linux.com;
0 g" D. O7 O4 v9 J0 {' y- `2 G1 ?  access_log /var/log/nginx/yourdomain.log;
# v' M/ u/ t! g; A4 j3 ]3 W
# R2 w% N. S/ N4 H! ?) U9 b1 O& w
  location / {6 m: H. ?, V+ i  W: x9 f1 Y4 q% I

- f+ Z5 p) E' x) t$ e% A" B" }4 M: q+ N/ V
  modsecurity on;% k" O9 E1 k* b$ G1 V8 w. f% @3 S
  modsecurity_rules_file modsec_includes.conf;
5 D6 Z3 E. K8 R7 ?6 k  root   html;. b+ l# \; i1 ~, q
  index  index.html index.htm;
' B$ v) S) A2 V& V}$ Z- t8 X. {5 R4 V& q2 K7 `
}& ^3 e9 K$ B1 p# H$ Q! K+ k
验证Nginx配置文件: B* j, ]: O/ y; @5 t* }$ f
$ /usr/local/nginx/sbin/nginx -t
# G& U- i  `& \% K- H/ anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
0 }% H/ Z4 ?& }5 p* s0 c( J5 Anginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
: D" E/ W- \; `5 S' Y& W启动Nginx
8 H1 V. s) W' i+ V. I  I; D+ l2 W- U$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. d' n* n( H% h  b/ `" d  H
$ M3 O. ?# P4 N" w) B! H# j

测试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 t; u3 m% G8 Y' ~

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-4-15 02:08 , Processed in 0.077743 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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