找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12567|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。5 p0 y0 j. P1 ^9 r

+ _/ m, f- J( ]0 u2 L8 _% A9 c
& a, V. `7 O" @$ h2 V在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
/ `7 H8 E# ~" u" w( Y% `% i
0 _; ]9 A: ]1 \7 I0 v& h9 j3 s8 I  m) m) {
什么是ModSecurity9 Q+ D3 H) s0 |* V3 \+ ^; s
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: B& q2 b! i5 `; T1 ^$ J
; y2 z& R1 j- n& f6 {) u" t

$ Z% ~% G$ E5 f$ _ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 L& N3 \$ A6 H, ~# J& K

: }: J& R# e: t& Z& U# A3 w
* t4 x$ O, p+ o9 jModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 J- i) h6 h9 H% `, h* w
+ D* t5 E- ?4 F
* b! v( [  t+ a9 h2 R1 p0 ?
官网: https://www.modsecurity.org/! I7 c+ j" v1 a) e
) D+ f+ X( d. B, q. K7 A% x
; ?/ U  @4 L9 e" z$ }* Q+ [
什么是OWASP CRS
- w* k; x7 |2 z4 T" N  a* {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。; J, h3 a+ u: t  I" _
* F9 k% B! ~* C" f1 K

1 [& l  V& \! \& h; q$ EModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。5 I+ ^5 ^( R) i
% u* N6 H7 v1 n$ q* I  x4 X
4 r9 m( V$ j, o. [
HTTP Protection(HTTP防御)5 J1 I" q, n3 S& z, z
HTTP协议和本地定义使用的detectsviolations策略。$ w& E" \4 J$ c; |3 j
$ o$ P0 N1 L- k1 a

) s2 ]' y5 d0 h5 f2 BReal-time Blacklist Lookups(实时黑名单查询)
, Y% W# o, _0 c  K利用第三方IP名单。
. u1 A3 j  \4 W' E* d5 P
5 L% z& O, h& E1 e! Q2 F
6 e: s! D' p6 ?# [5 Z+ o; \HTTP Denial of Service Protections(HTTP的拒绝服务保护), n2 p% q) O  ?- Y) m
防御HTTP的洪水攻击和HTTP Dos攻击。) `6 F3 x% w( @; Y

% `$ j- [9 M4 b/ R% g) M# i) H0 t0 i4 q1 V) U( Y
Common Web Attacks Protection(常见的Web攻击防护)
) b5 p& T: @# e" K* s% e5 \, y检测常见的Web应用程序的安全攻击。
7 Y/ |" X4 O5 c7 Y8 k; m0 _& P1 A4 S: B

+ }: n* ?2 Q. T! ]1 [Automation Detection(自动化检测)
) w8 E- ]( ]. D( v检测机器人,爬虫,扫描仪和其他表面恶意活动。  S% V0 n6 c' E' Z2 t

  D* _/ c3 K3 I7 P7 L
2 e* K2 {: @9 L. S: G1 TIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
" y, g# w, N5 k8 E% s检测通过Web应用程序上传的恶意文件。
5 `' f5 I, r  c$ }' U- m. x$ Q2 D5 i9 v4 H

: j' ]& s2 Q( t# E, k% z: rTracking Sensitive Data(跟踪敏感数据)
* o6 F$ X+ `# V信用卡通道的使用,并阻止泄漏。
9 E- u+ a' S3 w! R$ u$ L8 z, T/ m) m* y! J7 C7 j) |7 ]5 B  G
( G5 _9 p- g0 D% {. L5 r
Trojan Protection(木马防护)
+ V* b1 _0 I6 x  k) b& Q" W. Z. C8 S检测访问木马。
6 R. @( ^: C5 ~+ ?$ r, v- S: i! U# N. T7 N
! _& C3 o$ `( {2 b
Identification of Application Defects(应用程序缺陷的鉴定)( Y$ c: a  `( e& l; L
检测应用程序的错误配置警报。+ S0 {* K1 {: l# \8 i9 V5 }

. G3 p2 F: k. \/ V! p
2 D5 H5 B( S8 E( o% V5 YError Detection and Hiding(错误检测和隐藏)
# _3 ~: A- T' f# J0 c  ?9 s; O检测伪装服务器发送错误消息。
6 |( S$ E$ Y& f$ k# D  r
3 n* u6 \5 w6 W8 C$ f2 D$ b1 |. x, n/ c; X
安装ModSecurity
2 [% ?, A0 o. f1 B) \# ]! j软件基础环境准备
; Q# n7 f! q4 J下载对应软件包0 C! V0 B  g* e. W/ O  h/ M  R
$ cd /root$ V# p. o8 k4 K5 a: d8 b
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz') K- Z2 }3 q* H3 c  ], A
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz; A! `. ~) G% j* V
安装Nginx和ModSecurity依赖包$ \4 |: k* L7 X4 x8 T
Centos/RHEL
6 u* s2 _4 k$ |" E" ]: g  M# C" j" a4 d" B

6 o! r1 z1 v+ q8 q$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel  }2 x$ P: l) L
Ubuntu/Debian
9 Z# P& S, a- T: u- O% M0 [  Z' q! w: {
/ q6 z+ Y2 f9 V5 r9 L( w
, T( y& d  F3 @+ l; F$ 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
8 Q6 w6 [6 F) c/ h- F编译安装ModSecurity
! a7 B3 C; P+ c/ @' i3 C9 E. H5 D  MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 O) U  q7 w) c3 {1 P) U* \& {0 [( P  U. D

3 o+ @# V9 S/ Y5 m. Z0 p! p" R方法一:编译为Nginx静态模块- a: J0 d% a+ u! I

' g. o8 F1 K6 t6 X. [: Z
& h2 y- V7 r" n编译为独立模块(modsecurity-2.9.1)
1 U# u. n: R; c. Q4 ]9 @+ M$ tar xzvf modsecurity-2.9.1.tar.gz
# s' l7 ^, ]% E, G' U$ cd modsecurity-2.9.1/2 U) A  Z0 K  Q
$ ./autogen.sh" y' H' ]7 W! G: r& X
$ ./configure --enable-standalone-module --disable-mlogc
9 f  t9 f& P& }$ g- e( X$ make
' B7 `0 }& c* t  s7 R9 p5 e% X编译安装Nginx并添加ModSecurity模块8 U- `3 C/ _+ I" S
$ tar xzvf nginx-1.9.2.tar.gz
4 l: J$ H7 Q8 Y$ Z0 S" M) L$ F# G$ cd nginx-1.9.2
9 N2 c$ Z' `% w+ v- O$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
2 `* ~0 k  h" V- t. a: e$ make && make install. H& b1 p. a# p. M3 V
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块/ d5 r' N( y" E
$ [6 i" \) F9 I! a: K/ J
% l  e0 o8 z; l$ F
编译LibModSecurity(modsecurity-3.0)
+ X; W7 l* e7 ^9 u% _' p5 v3 H1 t$ cd /root
8 d) L5 H  Q! @2 j. A$ git clone https://github.com/SpiderLabs/ModSecurity: |6 i* K3 ^. g+ I& b& {2 p
$ cd ModSecurity, f. k; n, m( ^1 h
$ git checkout -b v3/master origin/v3/master
; d' H# D! x$ B3 x' r% j; T$ sh build.sh. i) @" B' \0 S7 d* m
$ git submodule init/ I9 f7 N8 O+ p! a0 r" s9 G! z
$ git submodule update" l+ c* _4 d. e4 X3 r! j6 e
$ ./configure
7 G$ r+ Q4 S) ^: k  U9 i$ make! M: C! R2 b8 R9 U
$ make install+ g, l8 g7 U, @+ D
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
( g3 m/ N( N4 i- |# I4 m0 Z% l" k# \$ q5 t: R* T  `
5 z. t' P. @' H3 Q
$ ls /usr/local/modsecurity/lib& a% }4 T4 T  \
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0% e1 f" D, z# \- V, Q
编译安装Nginx并添加ModSecurity-Nginx Connector模块0 R7 l0 I  X9 x+ Z
使用ModSecurity-Nginx模块来连接LibModSecurity8 c' a6 \1 \7 ^1 h4 Y
8 v8 @/ H1 j) r! O2 E* f

9 b6 B: T, ]; h- G9 L$ cd /root
" N3 x. f2 d3 }5 c& A$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx8 ~$ y. V/ T! o9 {4 B, {
$ tar xzvf nginx-1.9.2.tar.gz. y. ~: H- `  @$ V
$ cd nginx-1.9.2
6 w+ ?0 x8 Z' t2 Q! ]2 u5 v; o$ ./configure --add-module=/root/modsecurity-nginx
# B- h- o1 `" T& [$ make, o* A$ W% ^. }$ _* L
$ make && make install
7 F9 r- Z3 k  S: j/ x8 R添加OWASP规则. b, c" o' L' O# s4 \1 u
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 f! R3 y1 j, l- D4 l+ Y
: w8 Q" r- I1 R% W6 g, f: j5 k/ a) s/ E" M. s7 D: A: j
下载OWASP规则并生成配置文件) g2 R& i4 u! j: n& C& i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
, c% S* I0 c* a6 ?& Q2 l! u$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
0 p: }# c% X% ~6 F8 v$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
7 v# W: @7 t% `( v  m$ cp crs-setup.conf.example  crs-setup.conf' Z. N2 Y# T* b' N  x; _
配置OWASP规则* a( h) o; _2 f- H, z- J6 \: _; Q
编辑crs-setup.conf文件
9 o/ x7 L" i6 Z! G/ D' v
  H8 Z& k9 J9 ]) p4 ]6 X5 O4 l2 I  p4 e
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf, H- U* U, G! m, ]  J6 w
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
! ?# H7 o. p8 I6 ^+ r; i$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf, Q9 e3 \! R9 {6 F' o3 g
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf3 x" e7 B( f( c  P) |
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
& u" X) K; r2 J6 e% K% [) {$ x8 `! p2 Z4 z" l
' ~# p" L" }9 h
启用ModSecurity模块和CRS规则
$ K6 ?8 V0 u2 F( A+ u复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ ^  P: ~2 W! }5 L$ Y9 r9 x

5 r6 C  ~5 }, D) C. r6 t& V% L0 F0 b, a- ~* u9 y0 ^
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
: i, Y5 q  Z0 q+ r5 o0 B( z7 r% o  M' I
0 X! L4 y/ h; k7 s  k
$ cd /root/modsecurity-2.9.1/( K: U7 ]+ A! @" D6 ~+ c
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  . e% C' s& P; l2 k
$ cp unicode.mapping  /usr/local/nginx/conf/  v( G5 x$ A1 ^+ U- I; Y6 ^# v- x
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。4 `0 _% h2 \, \6 ]! o

* s, ^2 e1 u; n/ `% e" A1 A7 K" p4 D
$ vim /usr/local/nginx/conf/modsecurity.conf: c- p& M0 h4 |; `* G2 X& J
SecRuleEngine On* s) o4 ]7 N" {4 o  V
ModSecurity中几个常用配置说明:4 I" }- z) H$ D# M' B) M' r0 W
2 V! D: M# q) K- z: A" L1 v
& K  T& X1 J) I" b0 e
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。9 w# j$ H' d6 J/ B5 O; `
7 J# x5 Q. `. q: K/ L% ?

% }, k1 ~% c. Z5 L" f. e2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
2 k" V7 C9 T0 c( q- H3 ^# o1 ~6 B$ {' l% \/ Z$ o

2 n% Q0 ?( j  l+ }6 r3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。. a2 l1 `* c) u0 Q& G7 ?

9 {" t& U+ V6 |& ]' t$ I) t6 t% b# E" T0 l3 ]
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 b) `7 v, D1 c+ t: @% T9 K( u
" _- v7 r6 ]. i/ h

" A. u% V$ p6 c1 i( j) V7 J在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。" D! S( ^4 ]$ D2 e/ R. j5 @

& O' W3 R, \* a  j& D1 i
, Y' x& Q1 e4 D8 @3.x版本CRS
4 V' B/ w3 {. O, _' ~6 t8 I$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
# E0 E. s& L3 q# 生成例外排除请求的配置文件/ y8 m) \' M- Z0 I# H! G4 O/ N- r8 X
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& ]2 N) z. J- D) L& [- B/ @
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf* ^+ w; g( X2 @+ J6 d: r
$ cp rules/*.data /usr/local/nginx/conf
* e; o8 X1 ~7 w3 f+ J为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 [- b. j+ M" S( Z8 ]! d( ]3 ?' U  {* }9 I% v

7 F! g) k( m: U7 c8 P# [' I$ vim /usr/local/nginx/conf/modsec_includes.conf
' t! D: g% q  J
. G  l9 \- u, _! p
[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
1 J' ], U( }0 o& Z8 g, C4 g
) X0 c9 T7 o3 f: n) A# ?
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。0 D+ q6 s% b" F9 e2 Q  d6 B

" v* [2 n+ m& I# @+ y' ^7 F$ X$ D9 U3 x' Z
配置Nginx支持Modsecurity
$ \0 f* c; I5 f4 R启用Modsecurity0 Z* k: a8 B0 i; o
使用静态模块加载的配置方法
2 u) e* C: v, X在需要启用Modsecurity的主机的location下面加入下面两行即可:
; G! V4 V# n0 t8 F& D) H. n
8 _3 A) c! e* s% [5 \& r0 S. P' T$ P3 S, P5 x; W5 }8 v/ W3 d) J4 A
ModSecurityEnabled on;
, f6 Y, ]! E/ Q9 X, LModSecurityConfig modsec_includes.conf;2 D; d7 |5 f, m+ x
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。, L# `3 s. U+ k$ Z% G. ?( R- A

& B9 M2 Y& I* `, U1 {  x
1 X  c7 {/ ~( q$ vim /usr/local/nginx/conf/nginx.conf
3 }" e! R2 M1 f+ }
! Y1 }' f3 G! l+ x, E( ~  i* \( ?9 \. i% l0 t0 }$ ^  |6 F/ M
server {
. r. m7 I5 s5 J0 t  listen       80;
7 ~8 M: W; T/ r% e  server_name  example.com;
! `6 z/ j2 h3 `. E; q
1 M1 c0 ^/ Y* E! z9 c3 _7 |" c7 e% Q- F
  location / {2 N, I) y& K+ i' U
    ModSecurityEnabled on;
8 x8 H3 m" {" M" ~1 D0 t    ModSecurityConfig modsec_includes.conf;
0 ]8 ]8 K3 c& t  U! t, V    root   html;2 M  ~; i) Q' j  Q6 x
    index  index.html index.htm;
2 X1 H' p$ P: Z' Q  }3 S1 d* m  d% Q2 P5 N
}0 S$ s0 N! J1 a% b$ @  F! \
使用动态模块加载的配置方法
3 }4 H  y0 w  |. _- ~4 c在需要启用Modsecurity的主机的location下面加入下面两行即可:
& ?1 Z) {* n8 K8 ?5 b
; x2 @; p, x- ?3 {' A* V  }3 a4 x& \- E$ _" i
modsecurity on;
; c# W3 L1 ~2 [modsecurity_rules_file modsec_includes.conf;$ h: q" v$ W; F6 {
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
& |1 f  `# P6 n# ^7 ^& s) F
  y5 Y% e; G1 n$ u, g- W# i( d0 Y( T& y: {
$ vim /usr/local/nginx/conf/nginx.conf% x& {2 z. A* [: {
& Y9 E- d# E/ ?0 h+ n( T, x/ }3 S+ B

/ Z- o& r/ D/ M6 i% |server {
! l; k' L; {8 m  listen  80;4 p' C4 k5 {: [! n
  server_name localhost mike.hi-linux.com;. a$ @: k5 b+ Z0 x9 k
  access_log /var/log/nginx/yourdomain.log;8 r1 v- R: G. [8 ]# e7 I% r2 w

: B; u; N; l/ u; W7 a) C! R0 g- F3 F$ \" \* N2 x. y% c$ t
  location / {
. u* S6 Z( h7 q8 y0 q& U1 Z8 m
  n7 w/ ?$ j6 g# e. u1 S
5 P) J1 Z! e8 J* i5 y- t8 f# y  modsecurity on;  W/ N! p6 l8 j8 G1 E( Z; l
  modsecurity_rules_file modsec_includes.conf;
) J* i% Q" n) B. Q  root   html;0 w" l9 }5 K! i9 U
  index  index.html index.htm;
( W. ~+ M! ^6 ^" X}' D4 e6 x- i7 R+ w1 O8 O9 ]
}
; G8 D: X' h6 @" O) @8 x验证Nginx配置文件/ v. w0 }, E; N/ @$ x% ], D0 _
$ /usr/local/nginx/sbin/nginx -t
/ k' z4 j/ i" Y! knginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# A  w! M  v% x2 x4 R* f' }6 r' {; Unginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8 V% e" W  S( r; i" P启动Nginx' c+ K4 Y- {3 j! W
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
: ], s) V6 A' z( q: Y; @" |% T/ c5 y5 W1 }7 u) U

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

. K  H: W5 W% g' k) J3 ~9 A* |

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-14 00:53 , Processed in 0.140188 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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