找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11143|回复: 0

利用ModSecurity在Nginx上构建WAF

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

! q8 _  @1 j" r! l/ y3 z; H( b$ S
# j  Q, G1 l/ j: c0 X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% e4 _# j1 O* T. `: g9 O
$ _2 g3 l' ~# K, q% S
7 q- `7 E3 l" O什么是ModSecurity
: r- z6 S; r9 v0 p9 i/ UModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
9 @3 I  U  G: o  |
6 o/ c* |  o! W1 `6 T% `
: }  k" d$ u3 X* v9 E/ a+ bModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
! u- Q/ r1 y4 w1 X+ r: c5 u- V4 s4 z7 ^& ~

2 l9 c* c3 l1 B( F& MModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。0 ]* c) U: a8 Q' [

0 r/ L. c0 E- M/ f
2 P; [+ Z$ t( r& j1 r& W官网: https://www.modsecurity.org/
# q% @% z' ^! F1 _0 r3 U8 ?; C) ]2 w: l% Q! d- w

! D0 r- @5 |4 U+ _) s( N6 }什么是OWASP CRS- G' H: T' e" L6 c6 A- N8 D* l+ y& d5 m
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
3 Z8 S# k/ Y4 p  K: t1 C$ d3 S* h3 W) b2 M. _, ?( G
. R) Z2 a& T, s/ A
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
( `0 M; y" }2 |; F3 @7 W- V+ ?* q: S" [+ u
5 T, J2 p8 Z# G4 \& F' ]
HTTP Protection(HTTP防御)
& s8 S! U+ [; }6 I1 f  a8 A" pHTTP协议和本地定义使用的detectsviolations策略。  `; o6 a* I# |1 I
, N3 G& z+ v  Y2 d/ F

: D. m6 P7 K/ l) s" QReal-time Blacklist Lookups(实时黑名单查询)! X  m5 V; U8 ?7 a' M/ Z( `8 _" S, _
利用第三方IP名单。5 A) r4 i9 v; |5 n

! G( m/ _5 C$ P; Z
' q1 Z) l+ K/ S& pHTTP Denial of Service Protections(HTTP的拒绝服务保护): [( v" z6 E7 @# f! |
防御HTTP的洪水攻击和HTTP Dos攻击。
) j% h; l) a8 j% t
: `& P4 w+ B% m5 u) \; {  z) A) {  v- V5 ]% R, _$ z2 v
Common Web Attacks Protection(常见的Web攻击防护)
$ x* B* g2 f5 Q4 |2 g检测常见的Web应用程序的安全攻击。
3 _( ]% D: c: S$ i! [
( R1 v: t# o" ?3 H
/ Q) }0 r4 H" @Automation Detection(自动化检测)$ w4 m) C$ ], v
检测机器人,爬虫,扫描仪和其他表面恶意活动。3 J- K( u' W/ V

; p1 `* L  S  @
) |3 m7 h& d9 V1 \9 jIntegration with AV Scanning for File Uploads(文件上传防病毒扫描): M. B4 c* ?: @" \# E& m3 l3 Z
检测通过Web应用程序上传的恶意文件。
9 D( b& L. r4 J5 Z5 ~+ }$ l7 ^; S. G# |7 g! z& F8 l$ k9 h' R
; i' J$ f* |) x& [* W2 N* u' Z4 G
Tracking Sensitive Data(跟踪敏感数据)% [  m2 l+ O. X7 s6 A
信用卡通道的使用,并阻止泄漏。9 b+ s' ^& @! @
1 |' m; d' s! M# Y7 f6 ^

! r6 i* {. e- Q' Y8 R$ \3 U" YTrojan Protection(木马防护)
$ m# Z* |8 k3 \/ \) ?检测访问木马。
) g9 Z6 w) Q, C' T- x9 O* Y7 o. e/ u3 k
' A8 C% l! N7 j: f! U+ P' _/ N
Identification of Application Defects(应用程序缺陷的鉴定)
8 p% f* k- i$ t1 D/ z" d' `! u检测应用程序的错误配置警报。
2 i# @8 K- X5 e3 E& H  X9 D: e  M2 v9 g" ~( V

1 B/ N; K4 [& t% p% r& f: ?Error Detection and Hiding(错误检测和隐藏)
2 d" e, q# L+ P  e* Q检测伪装服务器发送错误消息。
$ ]4 \! i# e: c: Z  j7 t" ]; b2 ~' @
+ a3 s" W6 @$ Q2 ?6 A; O% d& l1 O1 {/ G0 {  d* C* R/ Q. k' L0 Z
安装ModSecurity+ l8 N" T2 s! I& z
软件基础环境准备  ^$ _- S3 ?. @9 M
下载对应软件包
4 {. I+ _+ B1 h/ G4 L  p3 v$ cd /root
% f5 R& ^6 \$ A) P  L+ [$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'' M9 |) Q# Y* A0 N, x6 e
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
) R' G: G+ f/ P+ [1 A6 q安装Nginx和ModSecurity依赖包% }: g) Z- j$ T; v! i3 i# A
Centos/RHEL5 V7 y) i" y. n! y! }0 E
! S$ K6 Y! o& v1 E; R

2 Z( T, H1 L! l7 x4 `6 N$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
6 W- k8 U9 E+ J' P: ~2 Y# [Ubuntu/Debian
1 d0 c9 f- o! c. n% G' C' Z! ~% B' p4 N1 H

  }2 |( E5 G+ Q+ o! H$ 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 K) D* i# A2 S/ X编译安装ModSecurity
5 c1 n1 g# @5 ^0 O0 yNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 F" g% h  a0 d7 K9 N' g

) f: l3 u2 \  }% g7 M1 k$ a. v2 `9 {& f
方法一:编译为Nginx静态模块
& C9 n0 m2 O0 g/ ?) M* ]8 l6 }, U! S7 k

2 Q% r, \& ?# r% H# h编译为独立模块(modsecurity-2.9.1)
/ u0 Q8 u; q! Q  g: v$ tar xzvf modsecurity-2.9.1.tar.gz
: N8 K$ D  Z% e/ @$ cd modsecurity-2.9.1/
# v3 S& V* P2 _4 h$ ./autogen.sh: [& h: H' U7 `$ I, ]
$ ./configure --enable-standalone-module --disable-mlogc, q7 [+ p$ @6 _6 Y8 S& M" V& p
$ make& l- H: y( [  J: [2 @$ |, N/ T
编译安装Nginx并添加ModSecurity模块: q* v! g1 e$ ~' I2 X( m
$ tar xzvf nginx-1.9.2.tar.gz
5 b- f7 L; n: c; I. G9 M$ cd nginx-1.9.2
& W4 j" j* c4 K0 \9 G7 u* B6 o$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity// Q7 |( ?* ]8 O3 q9 S
$ make && make install- z8 {$ Z% Z% C$ h1 M' x
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块! s! [0 ^6 O! D
; S% g% k3 j; _/ [; f" {& m
! ]+ X: [) R& Z0 @0 V
编译LibModSecurity(modsecurity-3.0)
$ [/ ]& I% Z# ]5 u: a- t0 {- C$ cd /root/ E, R5 j* z! x; i, g4 v3 k
$ git clone https://github.com/SpiderLabs/ModSecurity2 j" O, j6 e. u3 [% {! n
$ cd ModSecurity6 g, W7 L" z+ v$ }, r
$ git checkout -b v3/master origin/v3/master
" c8 S/ o* Z* d" W+ h6 N; E- L+ S$ sh build.sh
# _% U+ |- d+ k0 p9 w$ git submodule init" r5 P1 t2 K8 W! Q/ s: i  ~
$ git submodule update3 }' k+ z( p* m
$ ./configure
7 H( V( z! d8 E- ]6 e; j) w/ F. E$ make5 {- S9 p' \! }, ]1 X  C7 ]$ x  x
$ make install
' Z% i' r+ ?3 g. K: k/ JLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。" W/ L+ z' e$ G6 R$ Y# x

, W9 X- z' ?. t  k3 R5 g0 }9 s! s7 E$ K5 _0 s. C6 B" v0 U0 c
$ ls /usr/local/modsecurity/lib
$ X8 {/ W8 Q& n% F3 o5 clibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
# l8 K9 d4 G. [: b' s* D8 h, Z0 k) f编译安装Nginx并添加ModSecurity-Nginx Connector模块
5 Z. A9 D9 C# N% T! Z/ H% _! m使用ModSecurity-Nginx模块来连接LibModSecurity1 U4 C, g1 Y2 m% |
- T3 U+ l, R. z5 O  W
% T- t/ i) o6 q) e8 o
$ cd /root% H+ e) l+ I$ y9 U. x' c
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx  p% V9 |4 Z' y7 F1 w/ G
$ tar xzvf nginx-1.9.2.tar.gz
  D* {2 t" `6 s" g% s( x, j$ cd nginx-1.9.23 @" Z4 j$ Z( z' d# S: u- c1 I
$ ./configure --add-module=/root/modsecurity-nginx/ Z4 L( m1 Y- F. ]& V
$ make
6 h5 ^/ C9 a7 f$ make && make install4 |( x5 q1 ^$ `; d8 S) j; l
添加OWASP规则6 f9 u7 `0 u0 [: `& z
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。% R# Y0 q/ x$ m1 h! `. \
  W- i' o, u+ m; h5 f3 K

* G0 Z2 v! M3 ?( Z: w/ H+ t下载OWASP规则并生成配置文件% T' B( \9 v) L  i$ ^/ V6 T! N+ d7 w
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
% A& c( v' Y, Z6 M$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
$ J0 j# j1 x5 _2 J8 i$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
6 C) Y3 c6 E3 ^5 F& ~$ cp crs-setup.conf.example  crs-setup.conf* r3 c1 p* v# p% }7 z
配置OWASP规则
# O8 ~) M" j" [4 z3 N: G) [' K" D编辑crs-setup.conf文件# m+ D$ Z! ?( S8 e& a
5 j3 A/ I, `) @
$ K4 `. ^! B2 C) T! Y
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
" c) z- Q) z% G0 \6 ?' ~. c1 u6 A$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
7 Y* j0 X1 O# Z- B' `$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf( ]( N+ {7 U6 C6 x4 a% w- {
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf, Y* e# l- P, M( S
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" F; n& b4 m" F$ f) z' v. C& D

6 B; N4 J) v( v( s9 Z1 r( ~2 U6 ~/ g1 P4 ]  l& Y5 }
启用ModSecurity模块和CRS规则
% n7 n$ G+ ]; |复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( v) P4 o+ h# Z8 p1 S
+ ?. \0 ]2 l- D, ?* G- ]5 G# M# H* \4 h
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
1 ]% n8 `/ G0 I$ `$ C( ]
& ~3 P0 r% H3 G: |# I0 v( c9 I% U8 C8 s7 t1 {( q4 N
$ cd /root/modsecurity-2.9.1/  F7 |" U8 H4 q0 H3 |' w& u
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
4 ^% m2 d0 \" c9 n( T$ cp unicode.mapping  /usr/local/nginx/conf/
5 Y' o7 V$ g( ?; _0 N将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
# ]7 ~$ _  L# }' c. P% p8 R) X. r, f0 o! Q/ K) B+ X

/ `1 C# }, `9 V, ]+ {$ vim /usr/local/nginx/conf/modsecurity.conf
# {, v% ~% b0 r, i* OSecRuleEngine On
9 o% B1 d( J) u3 T5 N7 OModSecurity中几个常用配置说明:
! v" i/ [$ {% S4 Q$ {# y) ?  p. g$ \8 A  d7 L
/ @& `$ F7 `8 ]8 t' d+ ~5 k7 d: p
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, E, i9 [1 w, {9 m( c3 h1 r- R
& X3 Y1 W: ?. q

# y8 H  d6 [( o2 a2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。; S% R/ K- v( M  H9 l2 G: x5 W* y
) u. Y8 D+ w6 `
8 F# ]9 G2 U' F) t/ s* L
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
3 j9 y& u( P% Z0 b- H' }1 r2 \5 i
5 [+ f8 C8 e3 z. l* n: |3 j
9 r+ ]% [. u2 k( l4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。$ B$ \4 M2 A2 `' t& f" h4 ^

- O/ Q6 \" W$ p( A2 k2 t9 @5 g/ i8 n7 `4 g& Y1 B. W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
2 x) ~8 i9 C3 z  V( a5 {+ G/ m' E% k; H
1 I, {) I( z& \) S: C" n' c
3.x版本CRS
) F" I6 ]) j, c9 c/ b7 Q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 w" n# D. I/ y  ?
# 生成例外排除请求的配置文件
! |& H0 `6 D9 ~/ b2 G4 e- ~$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
" I6 y! r: f) u, {2 k$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
2 O4 L, g# ]9 d. Z$ cp rules/*.data /usr/local/nginx/conf7 Y. }+ t' g0 V! v3 b, i
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。6 I' ]0 h0 }' e& _3 A! e8 k5 o
7 q: O( R/ m5 s7 K. O2 P' D1 }* I* K
6 `) n$ z* C" P* |! k' d! p
$ vim /usr/local/nginx/conf/modsec_includes.conf
1 z8 A( |' E; ~$ W3 f, l+ B  `3 l; [1 I1 v: w+ t
[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

! G( f2 [7 q; ?5 ^
: X# L& c$ [( O# ~注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。8 A! a' Z/ T9 j0 S' H) g4 R

/ W5 r) S) i  `
) i5 v: p3 B1 H& u, f4 O( d配置Nginx支持Modsecurity& ]- Z$ n1 j' E' A& s
启用Modsecurity" t) P& V  J4 z2 i
使用静态模块加载的配置方法
; L! h8 f% \+ h5 B- X2 W在需要启用Modsecurity的主机的location下面加入下面两行即可:: I8 c3 L9 z! X$ F" j

6 m+ Q! l8 W- t7 O) a* f* I7 E! `7 E2 \& Y' F$ W  p
ModSecurityEnabled on;4 w1 }4 y, ?3 n: N  E
ModSecurityConfig modsec_includes.conf;
6 ~; Q2 P7 I" Q4 L修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ n( \: P$ B2 _/ e/ J1 {0 {) x% H

+ J3 ^1 B0 L$ w- t( o
7 D$ l3 M* H6 k; q& u' H( C$ vim /usr/local/nginx/conf/nginx.conf
8 |) }/ A/ ?/ ?
8 k/ ^5 h' w! n0 I4 a2 A' y& R; d) o4 x. e% @: B
server {5 ]# A1 V: ?( Y  n
  listen       80;, ^# U% ?# ]7 {% q4 A
  server_name  example.com;( t; A* k3 U/ u% L: u

6 |* W* w  z/ U3 k* l. b2 R5 P- f& l& Q" {& g, _* f
  location / {
0 H/ q. T+ T" r$ Y* J    ModSecurityEnabled on;
. m% z  `" v% ?& Z- `    ModSecurityConfig modsec_includes.conf;
) [' p+ \& h, F! _    root   html;
$ \7 u% w7 P5 m' W    index  index.html index.htm;
* X: h! X9 D5 }# i& r  }1 m# y# A1 w: e& U% F+ \( B
}9 [1 Z; \5 _: l; O0 c
使用动态模块加载的配置方法, V7 _* f# f1 O
在需要启用Modsecurity的主机的location下面加入下面两行即可:, z: r; ?3 M3 E0 ?' i0 ~4 H
$ c! Z. d" E6 `9 H

# a# _) s2 I* c. B/ V. P" `modsecurity on;8 H6 r8 K/ k$ M8 g
modsecurity_rules_file modsec_includes.conf;+ }* ?8 J4 }) [% G& s4 w) _# |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; t5 _* G  ?3 }" n: l
( v- z. X0 W# Q/ d

1 _) I& F; T1 U6 J$ vim /usr/local/nginx/conf/nginx.conf! a6 \$ F0 O" a# w  H7 Q9 T3 {! ~
. ~" W5 X6 z& |
4 U* s" j* u, r4 [
server {
3 {; z5 y, M/ x; x  listen  80;
! v# t8 G1 d6 _  server_name localhost mike.hi-linux.com;
6 v4 x2 W; R3 j. x9 P  access_log /var/log/nginx/yourdomain.log;
, ~& t; j# H  k5 a( b0 m9 [/ O
  K. n# f% _* _" m0 ~: A7 {
5 n2 C4 m' P6 K# P' c+ s  location / {
/ c) u1 b8 A* G
, m6 H( c* l' d3 H
4 o$ I" q" n& n8 M  modsecurity on;
7 n3 }8 N) _8 a! u4 U$ ]5 q  modsecurity_rules_file modsec_includes.conf;; {, h% T1 C: Z5 c' S
  root   html;
5 D! H. e( O5 K  index  index.html index.htm;
* m+ o/ I+ ^! D3 I, v  s8 P3 d}( d3 z) c+ o& O) ^6 Q1 N
}
6 T7 c6 h! d; v6 f( ^( Z验证Nginx配置文件0 m$ s$ V; n/ i2 [8 `4 d8 I. _
$ /usr/local/nginx/sbin/nginx -t
) `2 B* C3 ?0 q% @( Q& T) `/ Y' u( }nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
& @$ s9 c5 _" G; ?1 H2 ]1 Fnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
$ i  C: \+ j4 J) b& s0 I. M& p启动Nginx
2 b% L: s+ g* `6 u2 K, K$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf6 c7 O( ?8 Z7 P
& V* j/ a* l  Y& w- \

测试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 s% l, M8 j0 H9 L7 o) {

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-11 18:29 , Processed in 0.060465 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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