找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12134|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ G5 i- J' k- d) j
( N" ?+ w& t, n
* \. C' t7 ~0 N$ X, d. w/ M在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。$ U2 ~( k% ]- g4 f, L) L& ^( O8 n

. `" @" h& X2 L+ g: l6 R- B  n1 s  Y, v) n
什么是ModSecurity3 ?  _- J$ s4 y, q
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。# k5 d: ~* J$ m) B

7 k. B  q! Z5 u1 Y+ k5 e4 u2 \1 T' t- Z# ]  p- O- f/ v' {0 t
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。: t9 @/ }! ~+ @" N& |

" a6 o- b0 ^7 [- e' o* c% h
4 ~2 i5 h5 m, A8 U: t. V$ p$ zModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( m' g0 ]7 j# v4 A

+ [; x6 |# q$ q0 E4 \
$ ?: [. Z0 H! S+ K$ j/ V官网: https://www.modsecurity.org/- e! y* R8 m* A$ c6 t7 _. Q
" l5 G/ w, ~3 X' K- P

# n" L6 J$ b" [+ g+ f什么是OWASP CRS+ K) T+ I) S! a& o: o; X+ W1 a: w
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
. h) r: U: G$ o$ c1 [7 M" T2 Q6 W0 T( \. f
4 k5 \4 [* M( U
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
+ S2 r# H! m: x# V* ^( C2 w3 e( ]/ e/ @9 U& x
9 P( _! M8 J( [  c8 T8 W' h1 \- q
HTTP Protection(HTTP防御)' _& j4 ]/ N, \3 k  N
HTTP协议和本地定义使用的detectsviolations策略。
* v' x: U; e  x: Z. K5 k3 Q$ ^5 ]5 ]/ d  M7 J
- p% t8 j% i8 N6 ~) p+ J
Real-time Blacklist Lookups(实时黑名单查询)
9 i9 P6 y+ V7 u4 @利用第三方IP名单。7 l  i3 w" v. H* ]

& G7 J* Q$ r3 g1 X* Y- L  o( N1 _% e8 J: @: \
HTTP Denial of Service Protections(HTTP的拒绝服务保护)+ }6 O. b* ^, y9 D
防御HTTP的洪水攻击和HTTP Dos攻击。
$ e. C" Y. g+ w* h* a
( g& ^3 B  P- |- s* }1 C& Q% }& h8 t, n' k1 U4 q. ^
Common Web Attacks Protection(常见的Web攻击防护)5 f' g" x+ O- }+ j. q
检测常见的Web应用程序的安全攻击。% Y/ J6 X" }& V# `" X' m8 s# }' V8 M

- Y$ u) y, q" Z6 f' m4 `* a& L" c$ |
Automation Detection(自动化检测)- {! D* Z8 C. r! M, D, T/ _! f- T
检测机器人,爬虫,扫描仪和其他表面恶意活动。
7 m/ n1 h9 z3 H0 `! e
+ s; O% g* w* i: \1 _2 D$ v* ]4 v+ s. ^
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)& C$ \5 q3 w8 u  ?2 v
检测通过Web应用程序上传的恶意文件。. g1 w, _. h, I2 {) J9 G3 ?% e2 N  N
3 D( o; X  T: x2 l
: [3 j; K* v5 `
Tracking Sensitive Data(跟踪敏感数据)
" A* b' @9 U# s2 `9 N: [信用卡通道的使用,并阻止泄漏。
8 o  a) V! R# u: V6 i  z$ d
+ }* C$ N3 u9 n& }
$ b& K6 `9 j' A; Q7 KTrojan Protection(木马防护)  R0 D$ ?  Y% V8 N2 @! T* R
检测访问木马。; E' o4 H$ z% u: x, s1 s
/ h: M1 E; N$ C% }9 U  H; z

4 C0 |* i* [1 r, P$ j1 c2 MIdentification of Application Defects(应用程序缺陷的鉴定)  m1 y2 i% Y7 ?7 s% p7 l8 R/ H1 Y
检测应用程序的错误配置警报。
- @6 N, s3 M+ J( C9 H
; h2 E4 S( v$ |/ }8 z
2 j" c0 h/ C) `. }Error Detection and Hiding(错误检测和隐藏)
2 I- L! K) I1 r$ q" E检测伪装服务器发送错误消息。9 y% D/ ^% t1 H

1 b4 M8 u6 K, c7 z9 y% }+ _/ {. z( B$ p  `; k& A9 U& G+ A
安装ModSecurity1 _) X% d% G, P) u* t" a3 x& g& ~
软件基础环境准备
, u: B+ L8 m" M+ W7 a  p下载对应软件包
0 z( ?& D0 M2 s7 Z$ E: U/ J$ cd /root7 Z% Z3 m! V" l" l1 d1 V( B
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
, d/ m! j9 R$ \$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ V2 E  l; l' T安装Nginx和ModSecurity依赖包
- P4 i$ q, g! S' ~8 N2 PCentos/RHEL
) [( s0 k: G/ {5 X+ `
# a: o" F/ h. x& _1 u! M3 f' c$ F# }0 t& ~+ r2 A; o# b
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ X' `  }% l4 e/ R, x6 D+ G
Ubuntu/Debian5 f$ \1 c0 A, M% G- J+ L0 _
0 h1 p4 l5 p% F7 I) E  |

7 f  }4 e0 W  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++-dev. `9 T4 e# ]- O4 t$ z+ x* \
编译安装ModSecurity
# n, v' g; I( l3 Z* Y, R( M3 _Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。' h9 J; d7 R7 Q/ n6 _2 g. K/ c: ^
  e2 i* K1 W' {5 W

6 b) R+ Y: Q# [方法一:编译为Nginx静态模块# P+ V+ U# p* h! q8 l
& V( d' H, v( l) J: R; ^9 _

/ p- J/ [: N! E& A& L% `8 c& l编译为独立模块(modsecurity-2.9.1)6 Q, F* u8 }' e
$ tar xzvf modsecurity-2.9.1.tar.gz
, r0 l3 M/ g$ j" q& ~0 H) X0 \$ cd modsecurity-2.9.1/
1 r2 z# J+ y8 T0 s5 R# ]7 U" n$ ./autogen.sh; A' A4 y& `+ P/ l
$ ./configure --enable-standalone-module --disable-mlogc% d7 U, z7 h' A2 ]4 J3 e9 l
$ make
; E9 T2 e+ D! z4 x1 _0 _3 b编译安装Nginx并添加ModSecurity模块
* L. ]; L% U  G3 i* s* i4 \$ tar xzvf nginx-1.9.2.tar.gz9 T$ @4 a# K7 E
$ cd nginx-1.9.29 Q" ?$ q# t5 [+ u% z
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
% V. r' u+ _' q! t) H1 d, s$ make && make install$ {" a- {* F5 p5 Z& I5 y6 r( r3 ]
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块' e6 B2 R* E5 V2 x8 p9 n% S. \  s

- e/ E/ @  j) i, ^6 ]  q% l$ B7 k8 a0 u8 S
编译LibModSecurity(modsecurity-3.0)
+ i  X3 E- Y6 ]/ h; y. [$ cd /root
& j1 I. u4 ?. ~$ ^, T. @$ git clone https://github.com/SpiderLabs/ModSecurity- @+ x# }0 n2 |# h2 @7 k
$ cd ModSecurity+ R- a6 ^" c4 }# n
$ git checkout -b v3/master origin/v3/master! c7 |& z0 C. i6 I3 v, k7 ?9 v8 B) [
$ sh build.sh3 O& s2 y$ m# F% J$ X
$ git submodule init
5 Y2 R2 l& y% n$ Z4 w0 b7 E; K5 z7 k$ git submodule update
2 F* S' g4 v  O  \; \$ B$ ./configure  {0 }+ H: I, b. g# [4 |) s0 c
$ make
/ j* X3 {: p5 C3 T! C$ make install9 h1 K/ H! Y3 F6 p8 ^6 ?1 i# B7 t
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。  g: o7 f& W" H

' `% ]* F& w! m  Z& w
4 z8 i1 ?9 [/ Y7 n$ ls /usr/local/modsecurity/lib) V/ E  h+ c6 d8 r' Q
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0) V6 W2 |4 e  P6 m; b& [
编译安装Nginx并添加ModSecurity-Nginx Connector模块1 x- x8 Y$ o9 j% x4 s7 }" \1 U% P; J
使用ModSecurity-Nginx模块来连接LibModSecurity
' Y: M: H4 J4 W4 \5 L" S
8 T8 ?$ _) W$ C1 J- ^
2 S6 F8 ]( L% k  E7 ~+ p: c$ cd /root
3 [1 Y& w; h6 I4 c) n- ^* s. H( g$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ ?0 n, I% C1 k0 X' t
$ tar xzvf nginx-1.9.2.tar.gz. R7 k1 i1 u" a8 t
$ cd nginx-1.9.2
, n! P1 r" }5 Y8 r* e$ u. o$ ./configure --add-module=/root/modsecurity-nginx( [4 n1 b8 W1 S) G
$ make
" q1 _3 a( o) {. \4 T# E# _3 |) `$ make && make install
$ `1 L# L6 D: G& Z: K添加OWASP规则
/ d8 @- g% }, y& K+ P0 s* [ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。" L1 b* \2 ?+ [5 H
/ X/ `& }/ U* x" x# S
% d2 f+ J% |# n* D8 h' U* h
下载OWASP规则并生成配置文件
* ]& ^. \$ g4 ]3 G$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) B% {* o, b4 U- ~3 M$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/( E9 ]% |/ ?6 ~& c3 m/ ?5 j
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% K7 y9 P0 S' r1 g
$ cp crs-setup.conf.example  crs-setup.conf3 `: l/ T- U' t
配置OWASP规则8 w- g* }9 w" k2 I# _
编辑crs-setup.conf文件
: y; O9 \( E- j
; j. S) E5 m/ v
7 o0 f% x. p6 ?7 S, A, |/ |$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
/ l+ g% I7 G; {' o. {9 m2 L$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
$ ~9 S/ a! w6 p6 A  l$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf7 M' z1 C  K3 g) S
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
& a* C' }, T/ s$ |+ H9 f3 Z) `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。- f" Q! \0 E' [/ h" d1 E& v6 K

5 V9 h) H6 p0 x/ p5 B3 j8 a- j- O; b7 U1 t; Z; g
启用ModSecurity模块和CRS规则8 J! `. B: t, t2 D& `/ q! |
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. D! y$ Y0 s/ E1 S/ u/ _
7 a5 z7 g3 R9 e! t/ B6 q- h4 B1 I) A+ M2 {7 e4 U. r
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 ?7 Q0 y& F( V; r0 ^7 n( Q' B7 }4 G8 C4 G* d( z4 D$ N

" `$ u% j& g* D+ F, }$ cd /root/modsecurity-2.9.1/
: y" a, O  A- ?$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% F3 T7 u/ L1 Q* Q9 s$ cp unicode.mapping  /usr/local/nginx/conf/
$ M; X$ e2 \3 W7 \将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。% B" N4 d8 L% N/ g/ O4 d
+ [/ E0 }& W6 s" f7 ~
8 }! m% q0 m2 U+ }+ T* O
$ vim /usr/local/nginx/conf/modsecurity.conf) {* h9 A+ p, k5 z- ^
SecRuleEngine On  ~: t& `3 s9 j& b/ D
ModSecurity中几个常用配置说明:% @2 ]2 z* L0 }$ n5 i3 m) ~, ~
* p. p. `. p, u: W
& z3 h) U: L& b! k& Q# e
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
7 x, q( B3 Z0 P3 L4 ^2 q" ~7 Y. z0 u7 N% e/ `

  H0 w/ M$ d: ]" S8 Z7 R1 l2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 h) F6 g2 N9 m- X: n9 g1 h
, Y  h" |" u% p! N  m

- b2 @6 m. W) t, m3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。( \4 E) p0 X; _! y& h0 E! A
. f( z2 n* h; U* ]

2 B# ~; n( i/ s" M2 s4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
6 B, F; p1 u6 k$ K4 C! q
* Z( z" M8 [# I
* I% l5 J: j4 ]在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& ]7 t& J. [) U" V' t
, E* H: m/ b, Q8 ?
# A5 i! F/ f4 @2 j# f6 x4 R/ j3.x版本CRS
! m( M* ]: @/ [/ R5 ~- s) s- z! ~( h/ J$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
% ]* W0 H8 y  r9 ]* ]# 生成例外排除请求的配置文件
9 u3 Y! {- ~! Y( F& k* C% X$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
' S6 d+ {* p+ I1 i; Q, j$ J6 o$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf+ l' h4 @9 e3 B/ `
$ cp rules/*.data /usr/local/nginx/conf
7 D  F2 v' e& y; ?$ M为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。3 ~( _0 \  n: j

( L" e9 D( G9 q. q, v; M+ x( G! Z2 F
& a, C% F" R' C# }: ]. s) ?) f, d3 o$ vim /usr/local/nginx/conf/modsec_includes.conf1 Y& f2 F' H/ j5 j, i$ Y1 a

9 D  x1 s2 e" N) s) e, Y3 `1 ~7 G+ y, W
[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

) r' `$ s3 q' K( V! A6 `: G( M' _* c
. G+ t! j- i8 G. c+ [注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 K2 ?: M( Q6 _! l2 O5 y$ M
' i! W7 s% J& B7 D6 E+ U; K: e

7 q- G7 d( w. z& o0 W配置Nginx支持Modsecurity
& U% P$ k8 T5 E8 \' v! G启用Modsecurity
* _6 y+ ~$ q  k( @, K3 B& e  _使用静态模块加载的配置方法
" y4 {7 E% o! {2 ~0 l; O, I在需要启用Modsecurity的主机的location下面加入下面两行即可:2 O; p; j/ R/ K; x
# A7 r4 D- A0 {/ R5 F/ K

: K& R  N2 J# g: KModSecurityEnabled on;
. f$ g0 S" u9 _" C  d- G* e  X$ nModSecurityConfig modsec_includes.conf;; r# J" z# W9 @2 v& x* u  O
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 p- U: N+ }7 o! V% @4 k2 z( H9 q) A( |) s' E
4 v$ T' X+ Y: p& J
$ vim /usr/local/nginx/conf/nginx.conf
, S* c* M& }0 N$ Z* n; s) ?6 l6 a- k7 J9 L  @- f! i
+ z! O+ P9 B6 P/ @5 Z
server {
1 ]4 s* U4 ^& M  listen       80;
: A; ^% @8 h; ?- `$ I, j2 A4 L) i  server_name  example.com;7 |1 E- N" {; o; N/ D6 O5 G

, v2 A5 L7 a# l2 `' ~* v% G
4 ^+ e+ ^4 ?! W& R- V. R+ I  location / {1 C; q1 P# j( b& s& _
    ModSecurityEnabled on;7 n$ a; I9 Q% K- f* [" u+ K& Q9 ]
    ModSecurityConfig modsec_includes.conf;
" t* b' {1 q* X    root   html;
& ?0 T/ ]- L5 `' h# S4 N    index  index.html index.htm;; q; E2 R% A9 q+ Y0 i
  }; l+ h: U& [" R& N* I) V' Q
}  c4 d$ S9 l' t3 i3 F3 P) x. a
使用动态模块加载的配置方法
! p+ |9 c. q" x: r7 z在需要启用Modsecurity的主机的location下面加入下面两行即可:$ |% v) S/ G, U7 m! Z  w2 U) [7 Q0 v7 Z

5 c4 P+ B# O, ?4 n& P, z# n
+ z: r) A& r$ c% ~( f5 O1 o: Rmodsecurity on;- }" ?" e# `3 }0 y
modsecurity_rules_file modsec_includes.conf;7 l$ M6 r7 _* d, \! ?$ i
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% |4 {" _4 ^# ]/ W
  [$ Q; K0 m6 e: v; K9 D# F8 a, h* N9 j! ^0 r; W. X
$ vim /usr/local/nginx/conf/nginx.conf
0 \/ R0 f5 b9 T0 {; B( P( Z' e7 }+ ?8 W4 v& K" i. I- B" ^

. J6 F' d" i0 l3 m# Bserver {, f, b+ j/ j; I8 j4 D0 B3 j
  listen  80;
0 |. _- L, f, E1 _$ [1 |  server_name localhost mike.hi-linux.com;
  Y2 x% r* g. X* ~  access_log /var/log/nginx/yourdomain.log;0 s1 |! y1 {+ O, Q: s
- Y( R/ ]& w+ c: b# h0 V5 E
0 m1 X" N0 I* I1 p1 @+ Q
  location / {) j- W' C: z1 k! _  t

( q$ V$ ~* ^2 n
$ o' w$ P' r) h& ]  modsecurity on;) s* o7 [" W: {- P' r+ I
  modsecurity_rules_file modsec_includes.conf;
! b, G' J$ ]9 x  ^( c  root   html;- G3 E! `6 H$ _  w3 K/ X( q
  index  index.html index.htm;4 V! m0 \( e& L. b8 u: c
}
! |: W! v$ E. n9 @/ u3 ]( _}8 c- B" F5 J1 ~0 [' b
验证Nginx配置文件
: q$ l$ c, H  ~+ Z$ /usr/local/nginx/sbin/nginx -t: G3 Z. i2 Y6 A/ R! z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok; l# H: W0 D7 r8 S" w# `) ], B
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful% l6 y# b* f$ O, V& m& c
启动Nginx1 E/ n* x3 E; h8 `
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
. X5 n3 L2 Z/ ^- H' W+ ^( t& D' R3 t! R( _1 m1 m5 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能够战胜更多复杂/未知/混淆的攻击模式。


+ y1 `; k* b( ~3 k8 V( N+ ?2 R

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-19 14:44 , Processed in 0.101189 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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