找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 8903|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 r- V  B$ Z9 h% @  g3 j
4 s. t' \; m, P, [" A# u9 l6 w; G( f4 t
1 `* H/ V4 h. s3 ?0 C+ r& g$ G在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。( b/ ]! p) U9 o4 O- E0 C

: \! y8 Q2 T7 }1 |  \! R! r7 J0 M4 e2 x5 T
什么是ModSecurity
! `5 k$ k, W4 j# H' `# JModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ `4 b4 K& ?( a+ B1 B4 `
: j  i  A! D$ o8 w6 x6 f; ^1 e+ ~& c9 F
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。. q' Q+ H6 [, z

5 J0 ^( X* K: L" C- B) T; z' v
9 L6 L% ?. T: Z3 z$ u2 wModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。& z+ M! m1 q9 z" V: u  x* k0 S2 l

& h" b, I1 O1 ]5 b/ C+ y2 o" n* i8 `+ e8 T& m3 q8 }1 x7 y. S% _. k
官网: https://www.modsecurity.org/
) L& t) |4 F% c6 B! r' P! q) [. p+ r; u" O

$ E7 {( X/ `' F; R5 ]什么是OWASP CRS
; j) z" E( ~: G6 V+ n; }OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
/ x- x9 u0 E) Z. V) P9 s
& X. `  M; A( K$ o% ^- }2 k
$ W# Y1 x. @8 k3 L2 H2 P5 YModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
$ E$ H" T( }  C
1 u  l" ]' Z  o2 C9 J" ?& n4 l  R
  Q; N$ s2 Q, e/ fHTTP Protection(HTTP防御)  q, E( a$ }4 i$ m1 ^1 ~
HTTP协议和本地定义使用的detectsviolations策略。6 G+ B' i- P% ~, r1 R$ g2 Y

3 A1 |' K) F  p+ ?. ^5 L% w% N
/ h0 @0 E4 c& Y2 x0 rReal-time Blacklist Lookups(实时黑名单查询)
) F. J6 S! B* Z* L# s% a利用第三方IP名单。$ Y4 |6 h* O. j5 W" X9 K6 W

' Y" @* l( w/ I8 W; D3 {* Z+ m$ l! O7 p/ a, D4 w
HTTP Denial of Service Protections(HTTP的拒绝服务保护)! v6 P% j: w5 \
防御HTTP的洪水攻击和HTTP Dos攻击。
1 b( ]7 U  H8 Z5 r' E, O4 }/ q; L1 W7 n% H& Q  y
! f# M, ]* Q3 t$ e
Common Web Attacks Protection(常见的Web攻击防护)
0 F) O4 b' W* _& |2 l  ~$ q4 p检测常见的Web应用程序的安全攻击。
2 o. M% L7 t& j2 U& \- w, \; o# x8 F
+ Q/ L# {$ V  G+ D  e7 W, j( p. |; g' \) }$ Y7 o- ~. K% m
Automation Detection(自动化检测)
" ?, a( e9 D9 {! g+ v0 E检测机器人,爬虫,扫描仪和其他表面恶意活动。
! R4 f2 F) j, O( x3 z4 i! D6 P3 n

7 f7 i  g, ^0 R# x- hIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 a" X; P$ G% z6 u3 q1 d3 J检测通过Web应用程序上传的恶意文件。5 ?0 x, `0 D, J( `  Q9 h
/ \& V. f/ b; ^3 b& u) J! u. x

; A# B  G0 g+ o% ATracking Sensitive Data(跟踪敏感数据)) M1 s* k+ s4 M
信用卡通道的使用,并阻止泄漏。7 r5 g7 |) x* S, |# q
- S2 A* r. T: W) M5 @! h/ j. b& }
# L5 w" o% j! J& z9 a
Trojan Protection(木马防护)
$ y6 M4 k# M! r) g  T检测访问木马。
3 H) y+ \7 X3 S& ~6 j1 \) u4 M9 a# G" u) H8 n% ~( m

: V8 `0 T4 M& g$ w. y# O/ QIdentification of Application Defects(应用程序缺陷的鉴定)
9 i" k# B; ^4 ^检测应用程序的错误配置警报。' s& z" `3 n& N

. ^1 l0 C& T# g8 J9 ^7 k( N" p% n
) d; C& m& e7 Y7 W3 NError Detection and Hiding(错误检测和隐藏)
) `8 Q- q7 w4 c6 B' b7 z6 \7 v检测伪装服务器发送错误消息。% [7 f( s+ Q% B0 P9 t  L
+ F& ?0 ~+ J2 A6 L  L( O3 |& R

/ F: l" X& ], F, o+ k, O8 j安装ModSecurity
( Z3 {" m, O. l: h2 \" q& \$ `软件基础环境准备
, n& D' K" {' P4 K8 p3 B下载对应软件包7 ?. E( ?& N$ z+ o3 Z2 M5 j
$ cd /root
2 V3 u. k- j+ `$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'" v6 @; Y# i) }' v
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ j* C3 J% [2 ?2 y% f  N# T* h* M' Z2 V: W安装Nginx和ModSecurity依赖包. J2 v8 ~7 _; ~! h
Centos/RHEL
# o) Q( f( y* Z/ x% c& s5 r
7 |. a0 y  c- @5 y% H- e1 b
' `8 c) Y; o* Z8 z! y$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
% h+ y% Z- L# `8 e! w" \Ubuntu/Debian# p+ P* w3 g$ P7 \

  h: B( [" n) T# I; v/ t' V7 K/ ?9 `+ r
$ 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% N' T8 Y8 d" T( b9 d# P, W9 z/ U/ P
编译安装ModSecurity
2 Z" s  {9 ^2 h0 D$ ONginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。- R! e, _# e% S" {2 V
+ N1 B$ ?0 v# R* C
. z2 d4 v: {: n4 u  I. E0 B. k
方法一:编译为Nginx静态模块
( E+ Z/ d- e6 w3 d2 l
4 w3 U% ^9 z0 M3 z% X' S7 z- l  W: p
编译为独立模块(modsecurity-2.9.1). P) r+ l& A5 n
$ tar xzvf modsecurity-2.9.1.tar.gz
, ~5 |5 z9 `* g. q$ cd modsecurity-2.9.1/7 l6 H7 o9 P0 Z
$ ./autogen.sh8 v2 Z& b. R7 i% h( @- F4 [- P- Q- G
$ ./configure --enable-standalone-module --disable-mlogc
/ z# J0 ?/ ?( o/ y$ make
# ~9 b* A" s2 A编译安装Nginx并添加ModSecurity模块9 X3 ]+ ?% I/ b, Y; v/ n2 ]& X
$ tar xzvf nginx-1.9.2.tar.gz0 t' H" T! P1 w9 ]6 m8 K
$ cd nginx-1.9.2  n  e2 L7 E  @5 S. o
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/, {8 o3 C, a/ S9 n; i
$ make && make install/ D+ e9 R/ J( [$ I: y
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块8 |3 D5 X1 K8 d: x2 w

( Q5 Y' {- v; t) n0 U" s  o! r2 ^0 v8 i$ Z" s) @
编译LibModSecurity(modsecurity-3.0)& \% ^4 F: x& S7 o- F6 u
$ cd /root! w, `/ `2 L6 h2 `! X( e
$ git clone https://github.com/SpiderLabs/ModSecurity
% E) T( |; C& l2 m- R6 v3 j$ cd ModSecurity
  W+ o* ^  h, l7 _6 l! ^* o! W$ git checkout -b v3/master origin/v3/master' h. x' D5 |1 _' p4 J2 ?
$ sh build.sh& _+ H/ ?; T5 r- B4 U! {0 M6 A( z3 x
$ git submodule init
; }- b1 v1 r7 b8 p1 o% L, v$ git submodule update! a. R; [# c% W6 g7 }  y; d) B& B
$ ./configure
3 i8 [6 M$ o1 S9 u0 Z$ {2 c3 p" `$ make( ?. r9 K2 {& L* U
$ make install5 j5 B$ H: k; o( u. D8 m5 F4 k
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& ~# Q8 d' ]* h

; }/ T1 P& g3 ?7 i+ ]. L5 I* @. Z/ f8 j2 u0 {
$ ls /usr/local/modsecurity/lib
; p/ M* ]- [+ P5 K4 ?libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0" [) S+ q2 D' w- v! |) d
编译安装Nginx并添加ModSecurity-Nginx Connector模块* o$ [; z3 S: J" q; i
使用ModSecurity-Nginx模块来连接LibModSecurity
* f4 Q0 j4 c3 x" ?8 k4 T
$ O7 B$ {& m1 e. ^. h# ^3 ~* p# M$ ~) a
$ cd /root" q, y, j" a, A. f( u! ^
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx6 I0 e* M  H. j) X
$ tar xzvf nginx-1.9.2.tar.gz9 R+ v" ^8 {7 ~* m: u. h
$ cd nginx-1.9.2% i( W7 x% `5 @- O; r6 o
$ ./configure --add-module=/root/modsecurity-nginx+ C5 z* S/ G- h$ v! D- h% e
$ make
$ N/ D% a7 _5 f  k  _  `$ make && make install/ t7 F) K  l0 H: m- P7 |
添加OWASP规则
4 p4 A2 o' P4 kModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
9 o. L3 H: q4 g- x; B9 U  J0 S- Q# ~0 c; _* g! [5 @  F* ]  b

- ?* R: Z; d6 ]下载OWASP规则并生成配置文件9 x5 Y9 a3 n( Z. p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git/ T, W$ e. E3 O8 y1 K. o
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/8 g) d2 [& y& O3 h$ \" p
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; [# c% D( \8 i: `# \7 d/ E+ n$ M$ cp crs-setup.conf.example  crs-setup.conf
3 u1 z+ [6 [' d4 o, _配置OWASP规则
9 R! b, @. `% `7 p2 p编辑crs-setup.conf文件
( a4 z! o% `4 v7 ~) x  g6 z: g' n$ }2 }  m7 A8 {
0 |$ W! t8 w) L. E9 ]0 ?% P6 [
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf& n6 h5 \$ X: y# _0 e( U# Y
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% y: f2 c: x$ R( ]4 U
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) b% H3 O& |( v6 x, u8 _
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
! ~5 S& t6 E7 W默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
) o8 |* i/ I6 {8 X
5 }2 m4 H/ j6 x6 N! u0 h0 A- d9 r% _$ P4 E: E. c: |
启用ModSecurity模块和CRS规则
0 u- v! F4 h/ w: n9 t% T复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ N* s( r+ p6 h9 [
  E% E* l) s" T
" T1 B; ?: _- k& F2 ?% I1 `modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。, m* B  ]$ |1 p) A2 W

* I2 n8 F# _. E" Y, q/ J" t5 x& |+ r6 t' ^+ H. C
$ cd /root/modsecurity-2.9.1/
- |& [, h# V  U4 t+ {# e$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
! z, {; n# a3 F$ cp unicode.mapping  /usr/local/nginx/conf/4 Z$ f# m' o, ~9 j
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
4 B) y4 E6 r0 ^/ F6 ]4 ]; ?9 w% \' S  u; ?5 i
1 k, h( Z5 T/ S1 B$ w' l
$ vim /usr/local/nginx/conf/modsecurity.conf# Q: ]9 Y% I" Q1 {! s
SecRuleEngine On+ e( n/ u( V; x( M. a% p
ModSecurity中几个常用配置说明:
2 t7 p4 [3 ?) T* J( H# y
% r% f' c# ^6 W+ l4 |& d7 D! U6 f
! e) @# N  v1 y/ e: H, w; g3 p+ L6 h1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。% g" t1 w( t" w5 Y
& l( x& s9 t* a+ q+ j

+ F7 D$ {) X' k2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。+ |6 k+ J9 g% O0 P$ Z/ b( h7 @% J

) H5 J) T$ Q* |4 z& K( a4 x4 ~2 n% i! E/ M; `& J* s& l5 ^5 H6 `
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
( c* `$ {1 m5 N, N: b
* Z9 C0 v+ l2 b: P2 T! S' s% z0 X% E& }
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
; x& A: \0 f! c- V' R
2 P6 a& R% Z; |- n
! H7 L+ g2 G. R$ C) x. M在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
. L7 S; Z( B) y5 h
7 `8 a7 m- n5 D1 I6 X; t5 t. _  t8 o5 g% u! q4 n- M
3.x版本CRS6 o& _/ e. d; [' Z- W
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& j, y% @; E9 m/ d* l& C) n# 生成例外排除请求的配置文件
8 k/ v$ J( m) C8 Y+ E9 Z) p$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf% W3 _, ^& P0 l" x, j! E
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf6 w: B, b1 r) M$ k: P
$ cp rules/*.data /usr/local/nginx/conf( }' x& r# |+ c! F+ ~0 F9 [: f
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。$ V# e, y7 w4 c

4 Z6 ?/ ~( X& u& z
8 ~$ |& x" _/ ]' a$ vim /usr/local/nginx/conf/modsec_includes.conf2 w( ^& D/ H. `( F4 Q
& ?0 k6 d4 c0 y6 ]
[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
" H/ S: J! [# @2 p
, i- A4 H! i* c$ E+ ]" \
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
+ {5 A, b3 z, @# {5 s& c7 F5 L& q& c9 ~' B" u2 _
! S! h% X9 M( Q( I; Q5 o9 f+ S
配置Nginx支持Modsecurity
7 A; c9 ], n0 W, I/ v启用Modsecurity
0 }& \0 c& c6 D+ o/ g使用静态模块加载的配置方法
+ p/ c0 _* U! i; H- t在需要启用Modsecurity的主机的location下面加入下面两行即可:5 M* p6 k0 P9 Q: D+ W

3 o( I) e6 k) D; c% ?$ `/ [/ t0 n4 _7 Q( [
ModSecurityEnabled on;
/ d+ @; U% K( ^ModSecurityConfig modsec_includes.conf;
; l( u$ d( t9 r$ s: p$ o7 j修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
  w5 r6 \& O" ~/ L( D9 i* a* ], X" Z. Q% e" G
# Y/ f* D/ ?6 A# V- G
$ vim /usr/local/nginx/conf/nginx.conf+ S1 d# a# m$ ~" L5 ]7 H% y% `3 m

" C& g. k4 k# Q+ w% B# @6 V
3 T, f/ t- O/ p' x" Bserver {& O- ]8 u/ |' j5 ]1 y" ^
  listen       80;
' @- Q; s6 b( b! w+ I  server_name  example.com;& a; I7 D0 J6 |* m! q1 l

/ _- H8 W0 n8 Z4 R4 Q0 H2 C/ P' P9 R$ G0 t, v9 L
  location / {
9 O, ]" c. S! [$ r' M: S    ModSecurityEnabled on;' T$ y3 C- C, g7 g1 O
    ModSecurityConfig modsec_includes.conf;
# p2 [2 O( u; E! Q    root   html;1 p- }( i+ G8 R1 H
    index  index.html index.htm;
: B3 i" v5 q% g" H3 v: K/ F) p) R  }
! e$ D) {" D# v9 O}4 d' V+ d' H6 v3 A4 r$ \& k
使用动态模块加载的配置方法; L) c- R+ I5 T: F- D$ \; `0 [7 m7 Z
在需要启用Modsecurity的主机的location下面加入下面两行即可:$ ?. q2 p, ?9 J5 ~% f7 y% K% S

3 x- Y; Z( S0 v% t0 f  U3 M5 L) A" X9 d  z5 P0 v7 d8 o
modsecurity on;' d. t4 r7 u# X. k& ^6 Y# R
modsecurity_rules_file modsec_includes.conf;$ B' e# t! Y* b% v% D3 {
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. l  x; M: |/ l! a; n6 G' r4 S: ~4 I2 N

( C9 Q' b7 Z8 O9 a6 b! d& D% e' [$ vim /usr/local/nginx/conf/nginx.conf- z5 L, |1 V8 ~  }

' E  u9 ]$ ?$ _
  T3 v  [' h  C* O/ u9 n0 [# Dserver {
/ P5 x- `: _$ }: K* O' p( j/ B  listen  80;
6 a3 m) j1 }5 k* r' Q% O! f! h& o  server_name localhost mike.hi-linux.com;
3 f( d5 K; c0 ~( v+ A  access_log /var/log/nginx/yourdomain.log;
9 |& d* h( E' `1 @: t# {6 z4 ?9 r- v# l6 o$ r  W) Y0 i, d
- J# t& q4 F3 |) G+ @3 e2 d
  location / {
4 c* `! e/ m4 m$ K
8 o$ K2 O1 M# ~4 I
, L: n$ A1 a7 t7 @* a2 N  modsecurity on;
6 A. I. U0 P5 |  `& }' ]  modsecurity_rules_file modsec_includes.conf;
4 w+ W+ R% i; r3 ~3 t  root   html;
* D" @( l6 x4 V  index  index.html index.htm;
) c( r; j" P, w. V  I+ Z}$ K' l: T% a" p% o! w
}
- K+ K# n7 X9 c) g& u验证Nginx配置文件) P/ @; q! [) d
$ /usr/local/nginx/sbin/nginx -t9 J; Q  L0 v% j* Z! n
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 c/ _& y) w2 M" G5 E
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5 @: ?. u) a5 Z" I9 n# I启动Nginx
* d/ w4 ~" C1 P4 z, d$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf5 T1 F2 L, g# K" {# ~* j3 L
! p; M5 ^8 z6 l, ^. h% e

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

  u1 Z1 S) M8 x. ~: }" i) B9 O$ \* s+ `

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-7-18 21:44 , Processed in 0.075899 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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