找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11153|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
3 w* r2 D' [$ S4 H* M6 h8 ?- H* B( p* B- k( |8 y# D" L8 L
7 [8 X5 s* V* o' D! b
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: J* N" {9 h- Z9 L" _
% q& Q# H/ Z2 o! D% P( e1 Q$ d/ U7 `3 E0 ~) ^- g
什么是ModSecurity4 c0 U, h" ^4 C1 p" l
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。0 m9 O* U( |$ b3 N/ I2 c! a

5 D7 \7 K+ K, y' H" w! x0 `, y$ S2 v4 q# S' ^: l5 b
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。  u  c' J4 u+ I/ M1 S/ w0 t7 t" {4 A$ y

6 j+ g4 v% k9 C, p! b/ {  Q5 J7 F% G
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。. i. ]( c& ^$ g- l8 y: s# G; d2 r/ D

' C3 F0 d/ o( L1 L4 n9 i+ \# n  s% p0 _7 h% I7 e" ^
官网: https://www.modsecurity.org/
: q8 @9 |4 ?3 X+ M7 o
% O/ i% \* R& f3 Q9 }. ^
( X7 u/ n) B7 i3 T$ w什么是OWASP CRS: @6 c$ _8 m/ S/ N  _
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。! P; h- C' x& t
; ~- b7 |$ Q: O
7 o. A( }% J; Z& T, \; ^5 u3 d5 ]
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。* X1 ~2 _1 ^1 G- e  l4 B

3 a. h& d. }8 c8 }
* A/ O" H# x0 p- g  U; l4 W/ V& rHTTP Protection(HTTP防御)
; C& U- _) }2 W6 j, NHTTP协议和本地定义使用的detectsviolations策略。: [5 j) G5 \' a3 X; x) f: P
0 w3 y( m  D; [. j; m$ F3 F% B

5 h2 c, E  I9 a4 _1 UReal-time Blacklist Lookups(实时黑名单查询)- [5 g8 k" w) a' R" p
利用第三方IP名单。' t* {, g4 T: m- T/ w

& c  D- w2 S/ n  U- |+ g6 W8 N8 Z7 ?0 |# d. \5 w( Y
HTTP Denial of Service Protections(HTTP的拒绝服务保护)) K2 u& `3 N9 c3 h" p2 L* F5 w
防御HTTP的洪水攻击和HTTP Dos攻击。2 t- F. ^  M" U! s  [4 b

+ S! t5 O3 w6 B, l* k: }- t" G, W! B: H! Q! ?( o! [7 l
Common Web Attacks Protection(常见的Web攻击防护)
4 ^3 y$ j, ]: P8 P% W( B检测常见的Web应用程序的安全攻击。
7 n" g8 A7 I+ O" z9 [% F9 d& @# N& P* D; B: s

# d* G* Y3 C% _- N7 v2 u# x7 nAutomation Detection(自动化检测)
* V# u& d& o) H3 r) w& d+ G9 m5 K检测机器人,爬虫,扫描仪和其他表面恶意活动。2 p, _6 ~5 {7 ]/ ^7 x6 @
5 m9 f! _# r0 S) t
2 U  [- z7 W9 P5 ~
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
# p$ u8 I9 [1 M$ h* G: U- |: y检测通过Web应用程序上传的恶意文件。: K) B+ L. u4 t1 \6 U

  k# T  V/ K' d0 Z4 N- c8 W+ K* u6 b' N4 b; ]1 E. w& D5 ~! z& P
Tracking Sensitive Data(跟踪敏感数据)
! F6 A9 j* G3 j信用卡通道的使用,并阻止泄漏。
# n- G! O+ Y) o8 E; y5 H: b! @! W* Q  O: V& i3 d/ e- E# v

9 L$ s: L7 C* w9 ITrojan Protection(木马防护)
4 e% E# ~5 I9 m1 t% l: u+ X检测访问木马。
" `" z0 n/ X: Q; Y( m# Q' J* p: i. i; S5 L# P0 n

) \1 O" j! ^9 d4 k3 T% [Identification of Application Defects(应用程序缺陷的鉴定)
! K) M. [# R0 n, T& j# p: y检测应用程序的错误配置警报。
+ R9 F/ x8 D- j$ d* f
7 v5 a% [% f/ E% s9 \! b/ x
0 D# A' G. ~; `5 KError Detection and Hiding(错误检测和隐藏)( f6 T  d( W2 S! L
检测伪装服务器发送错误消息。
# _, ~- {8 g8 [' I' p' m: e4 h7 E
! d% t1 M2 e+ {, E* x/ o* O' w% Q0 m
安装ModSecurity2 T4 C* a; v4 ]! y# N) B
软件基础环境准备6 N. d7 }9 R" Q8 J
下载对应软件包4 ^; @2 D- @/ f1 |  h8 L$ y
$ cd /root  F' @4 }1 P" O
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
8 ]1 @5 h' L1 x1 Z% [0 g$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz5 W* [5 y6 }2 ]# u
安装Nginx和ModSecurity依赖包
# i8 L0 Q( I! a3 NCentos/RHEL$ v; q8 Y" h7 @8 {4 k0 S6 |
. n/ n( F, c/ _0 W# |

. W, J5 c: A) `- t$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel9 p) v0 ~1 u' }3 D1 ?& E
Ubuntu/Debian
( r8 i( Y, n# R) A- m
) Y0 L( @- O4 L: A; s& x% d. m) J9 H5 _; ]+ R! k* |1 P
$ 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& R7 h4 S4 M0 _- U! _5 `
编译安装ModSecurity
" I$ K2 @8 p4 H$ _' Q5 N5 fNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
( B- O5 N) }* ^& {: {* \# L( U$ E  y
7 p0 k! s: A! c8 C7 Q" V
方法一:编译为Nginx静态模块( |) f, D5 ^/ f2 S

2 @! S8 I" \" S0 B  J) g$ C! R2 v0 k3 j7 {& Z* h
编译为独立模块(modsecurity-2.9.1)
- [8 L" M" r, j1 G5 v5 N( B  M! X$ tar xzvf modsecurity-2.9.1.tar.gz
* I3 l8 C) S( ^2 h- u; g$ cd modsecurity-2.9.1/
7 a1 G8 A8 P* C& q$ ./autogen.sh/ ^0 L+ q7 T9 l0 \: F4 k
$ ./configure --enable-standalone-module --disable-mlogc
, ?2 D) A- e9 J! z# t9 P4 U  I$ make2 `2 O- ?* [% z" h( f( W! }& c; x2 r
编译安装Nginx并添加ModSecurity模块) ?1 J. e# i% A- O/ a( I% D  E* e
$ tar xzvf nginx-1.9.2.tar.gz5 W8 O  M6 Z2 ]8 B
$ cd nginx-1.9.2
. a1 `5 M! r3 W- @# n$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/8 @1 h  @# z  q
$ make && make install3 e8 B1 E  V; n- T; f
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块- `! ]9 {7 _4 ]2 \
0 b  }4 x! P* a8 h: e. `

3 }* y5 h$ p3 l; U$ c2 E% H编译LibModSecurity(modsecurity-3.0)) [$ `5 P# _. |; B8 N) o+ ^% t3 o
$ cd /root
4 O9 I+ z6 X6 X( N$ git clone https://github.com/SpiderLabs/ModSecurity2 r6 G; B4 G5 W) p2 r$ M3 b
$ cd ModSecurity
% Q8 c/ V$ p& v1 v5 o% n6 ?3 A$ git checkout -b v3/master origin/v3/master
' e! {' V2 l4 r8 A7 ?$ sh build.sh
# o5 H+ |$ x* Y$ git submodule init5 P  e0 s1 W+ `! Z& D3 X4 e
$ git submodule update
' F- m$ L" h4 Q0 m$ ./configure8 G" G2 ~6 E; ^# [! v& m. ], N5 s+ l
$ make% ?( W- j3 q( r+ I% [$ o
$ make install
: G# _3 m. C4 Y% B) l# w8 s9 s% wLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。! _' h9 m" D: s

  G" S! N% p! }# R. s( P5 n" y' p9 t1 E6 o
$ ls /usr/local/modsecurity/lib& e* x2 A- I  k; r2 t
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
6 {' c, Y& R0 `! l& |编译安装Nginx并添加ModSecurity-Nginx Connector模块
7 _2 y% ~% A2 s使用ModSecurity-Nginx模块来连接LibModSecurity
0 Y4 A, P8 }7 r  K4 P5 N
1 X, L. d2 B+ Q* o) O  g; `) U2 C# O- y3 w6 A
$ cd /root' W% c' a8 {5 c+ J2 h
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
; _( d, N9 ^% m$ tar xzvf nginx-1.9.2.tar.gz. g" {# d" d. ?/ H7 k4 U, ?' k
$ cd nginx-1.9.2
4 [! {. y' y4 A2 ?$ ?/ X$ ./configure --add-module=/root/modsecurity-nginx
% d7 }' D1 n0 x8 J2 M7 j$ make
4 p+ q+ Z3 ^$ C$ F& L* z" O$ make && make install5 n' M% P# ~+ ]" r, T
添加OWASP规则
; b. ^4 _" X' k. C$ v' TModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( g2 f+ p4 [+ X( Q! v2 S
& c$ K' b3 Q* A7 H! @6 U2 S
5 l9 J- I% t5 z) v* x) n下载OWASP规则并生成配置文件
% G' m8 Q% Z$ L# q$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git; w6 s* u. _9 f3 }1 T  s1 q
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
% @" Z& j, H# I) g, ]& o" ~7 l$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ ^- m1 O% p- j" s2 r
$ cp crs-setup.conf.example  crs-setup.conf
, u% Y, A" R5 A8 v0 G配置OWASP规则
; d. B! l( [2 b7 F  f编辑crs-setup.conf文件7 b7 M6 _# w# F' z8 o$ R
" a  ?9 `/ G: j: z2 S
$ h7 X0 s' I5 K: A" F; p$ I
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf& `' w3 I' \$ f
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf6 c3 w7 y0 F- r# t
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
2 @/ \: B0 d* a- h. S, {7 ]: Z$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
; i- Z0 ?1 _* h5 W8 Q默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
, U, n- u) f& l+ J! n5 ^" j. j4 N
6 m$ ^* @. y, y- E1 b- C" `* b+ G* r) g% R4 L5 z
启用ModSecurity模块和CRS规则
# D& s5 _* i" j. [1 u复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 c6 `5 M4 y5 d7 L" u' v* A* `& x$ d3 l

" H- ]( H. V$ P# emodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% ?- {! v) T8 C/ l

6 A8 }: L/ S1 j0 p1 I1 S$ s/ P
8 A/ m; n" s5 G- n) T$ cd /root/modsecurity-2.9.1/6 m8 B9 d# _4 t/ w+ f9 o) a
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
! \. ~8 `1 N/ ?: w* o6 |5 }$ cp unicode.mapping  /usr/local/nginx/conf// u' R! I: `: H
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。- U! q' J$ x, G+ O3 n
7 {7 {- p0 v$ O3 O  z- w$ k9 g' i

; C, F' g+ u5 Q+ x$ vim /usr/local/nginx/conf/modsecurity.conf$ b8 d8 W+ {4 w) |3 k0 K
SecRuleEngine On, _+ h: u( _" o
ModSecurity中几个常用配置说明:
# H1 D2 B6 l' t: n# r6 w. r# ]' I5 B' a: d0 s+ I6 F6 B+ _: d

: [6 G" E& A  P, Q; |: P- }! J7 `1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
* [  p  o& O) T0 p
- t; x! X5 W2 J) E
. S3 m. ~+ }0 j5 K. |2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
5 ]9 s5 |! T# p5 Z5 L( v* Q7 c$ {+ _+ q: F3 V- y& ]# [4 B; D
! o1 m2 c0 a7 P$ M9 f
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。9 ]$ D5 D' [4 \3 R6 U6 b

  }8 @8 u& e0 Q3 q7 U) X; C" a- F: G2 K. o9 C7 h; \" E1 F
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( y* K- Z+ S% B) e' z
( s% x3 l9 f9 S( a% g( g
, k6 ?! ~( H  D. U在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。( {( s. k1 _# t* k/ @9 n: h

: \1 ]' ^) F7 M/ f$ g: h
  i/ K% q* @/ d' }3.x版本CRS1 ]& Z5 A- Q" {. p
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
* ~2 w$ S! K: C$ X# 生成例外排除请求的配置文件
' f3 ]. O( s" H$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
1 x+ ?/ v0 K; S5 m' C' Z/ W# x0 i$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  j1 k2 D3 d# V
$ cp rules/*.data /usr/local/nginx/conf
% D% W5 ?" Z' H! n& M  q为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。+ X4 x6 |7 e, N
& S" A; z% h6 O: h8 [* J9 c, j1 z+ p
# \7 \; G* x) A
$ vim /usr/local/nginx/conf/modsec_includes.conf! P) I) ^( X3 Z/ O( e/ q' Q' ~
7 N' G% ?# x/ u# K
[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

* j6 N" F8 t# b+ n. v7 ?: l! P- O* w2 p. R# c& f! f( U2 u1 i
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。& W) E% c: b" u' i2 P

  T3 G' \/ @* k' P. q5 j7 \0 Y- z! b* }" I6 j/ y. p3 A
配置Nginx支持Modsecurity
; h5 \, b( G  Q1 p9 q6 e启用Modsecurity& O# X3 Q  |/ B, `9 c7 t% f
使用静态模块加载的配置方法% a9 L. l8 u. h/ `5 s6 F7 l
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ m5 g4 f+ w' f9 P& A# y
7 }0 G+ y1 ^4 m! b) u" [6 Z" L- W0 L3 O! s1 p
ModSecurityEnabled on;
4 c. _. S% f& J$ K& g9 R* BModSecurityConfig modsec_includes.conf;
: a/ ?) e! e. ^修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: U. ~3 J1 a3 x- b/ u5 q( x) S6 z0 _* T! T  u6 ]0 L
8 H+ I! S8 U* `+ ~& r
$ vim /usr/local/nginx/conf/nginx.conf  B9 \8 y5 b: J: G+ r
" z1 y, s* t; S6 |$ B

4 K6 |9 y- ]; s$ \! [+ |/ \& i; Cserver {
' O1 n( V& t2 e# M  listen       80;
* b6 o" U7 B1 a* T$ @9 }5 H4 k1 c' n  server_name  example.com;
1 h+ I+ u9 H0 g- t1 _
( @+ Y7 w$ s( V$ ^0 B4 d: h. ]5 b9 U/ O; d, D, g& Z
  location / {& `& M& e/ h' h: c
    ModSecurityEnabled on;  ~9 a) ^+ ?/ s
    ModSecurityConfig modsec_includes.conf;
1 W: t+ Z5 Z: r3 V; `  s4 ~+ B2 R    root   html;: q& m0 n4 C, _+ ~# S5 V& w9 Y4 F3 {" o+ q
    index  index.html index.htm;
4 k" `9 n* \0 z2 \  }& J' ]* W6 T. Q, }9 ~
}
$ @* ?0 l/ K+ U; \% z使用动态模块加载的配置方法
$ }  U% A  m7 S) @6 e- l) e6 F在需要启用Modsecurity的主机的location下面加入下面两行即可:! A3 f$ D5 s, f1 J: u- ]9 _

2 T7 l6 x; l- ]) O& _4 H; F( G+ H( X" X9 H- S$ I3 d
modsecurity on;
1 ?: ]/ }/ [- @$ Nmodsecurity_rules_file modsec_includes.conf;
( H, j* ~& k! B9 _, ^0 Y" R修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. W2 ~) F2 X+ ]. Q% |- L$ L  u

7 j0 R  }+ G9 q/ h; w
5 n9 _% A) C! L$ vim /usr/local/nginx/conf/nginx.conf. u3 D, C) J* b$ E4 Z3 C1 o

/ |" r0 E  z) q9 }. b  N! q
0 B4 {4 z! h3 U8 F8 l$ S/ }9 r- @server {
/ l/ Q  \$ b% V) Y0 N  listen  80;
- Q$ x. C( H' Y( D- u8 v& D' a8 l% f9 v  server_name localhost mike.hi-linux.com;
1 n; y9 D4 [; {" X0 A8 r  access_log /var/log/nginx/yourdomain.log;- p* X* W7 m' l# y
2 c1 Z# k) u% f3 T
; m9 Z3 }, S2 c( S' _; W- }
  location / {4 |! ]% w# a* o

8 z; u; L( S1 u4 _4 S
+ |( h+ Y* W# W" @; Q# z  modsecurity on;' l0 b; n1 C+ b
  modsecurity_rules_file modsec_includes.conf;7 U1 P+ J, x+ Q. E
  root   html;, L* ^: _+ O( N! d1 }
  index  index.html index.htm;% p/ u9 f6 c1 U' t- [" O4 N
}
' Y# d/ _7 L8 t}
! l! U- G. W! P' L- N4 R验证Nginx配置文件
8 X/ G' K( |$ D1 f; j$ /usr/local/nginx/sbin/nginx -t5 l# M! E4 P& N7 H' j
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
" @& ?, X' B# @! v) P1 Xnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful6 B& t8 Q* o6 T% d4 v/ ~1 \
启动Nginx& G/ P' T$ k; y  j; w
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/ I2 `. u* A* g5 M' _1 S1 \) c7 E4 a5 I& k

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

9 Q; L0 _, ]/ j1 K8 v% X

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-12 19:15 , Processed in 0.071291 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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