找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12121|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
  @# [% C. |6 _* |' a0 V' o
8 m+ J* B1 m7 x4 }" {
# A# a0 M9 [$ h6 g$ t在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% d" k3 c$ q& X$ _
: ?% @5 m) j) Z4 V9 B& p, u- x2 R- p# Q' @* o$ @* y4 d- q
什么是ModSecurity+ H0 n7 |" ^% \+ ~& h- k1 f3 I
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
. Y) Y2 B' D+ q$ X) W7 A/ [7 f# i6 ?* s# t3 ~' s1 E

3 X' j/ w9 v1 P' q: P& zModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 \  X3 U& x/ F2 P
) I/ ^! u3 p$ z  y
8 S4 N9 Y& r, B: y5 lModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。6 M& u8 k1 T7 i4 v& C

: ^6 d$ d: Z! Q6 F, \$ g% G' `
5 ]! j+ N" L$ D  W6 N9 ~) z官网: https://www.modsecurity.org/) L6 }( s5 N6 P* L. s
  c4 g' j8 v, p. z& L4 ^

/ j* L# \% s, K9 C+ r什么是OWASP CRS; c5 i' ^/ j+ O6 K
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
0 I0 L# e: [$ t$ x1 W% j; _7 j; w$ u2 h/ M
" F& w8 p8 T5 |% y' b3 ]
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
: R6 E. |' l; s
/ {. V+ B  R# ]8 B( W
0 |- w% O6 ?' _, g& t3 B, \HTTP Protection(HTTP防御)8 f' [- N& ^- F: I. u2 W
HTTP协议和本地定义使用的detectsviolations策略。
/ z7 V0 s( S( f: u0 i
( U7 v5 f, m- x( j+ F7 k
- @! k6 x. |1 EReal-time Blacklist Lookups(实时黑名单查询)6 k! l4 k4 o. x' _3 q
利用第三方IP名单。
+ i; W4 v/ X$ z6 A& Y( q
# P$ ~) Q& y6 \% t! o* W; b3 C1 C' `7 H0 \0 z, q3 h
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
& i5 P( G( _% X& L/ U7 a. _防御HTTP的洪水攻击和HTTP Dos攻击。
# u" A' z5 Y% j3 T, M0 _" W
3 j. u! f$ U- X% K4 r6 [) ]6 `/ C
' V( J! m7 z* \6 @Common Web Attacks Protection(常见的Web攻击防护)
7 T: R3 [7 e+ J6 Z1 Z, o9 m' d检测常见的Web应用程序的安全攻击。! R+ ~7 K1 M: R8 ?8 }. W- R

. Z6 o! E+ e, [2 Y6 h4 k- S
  s8 P# n3 u; a; UAutomation Detection(自动化检测)
7 y7 h6 H$ [( U) X$ b检测机器人,爬虫,扫描仪和其他表面恶意活动。
  o# l0 ~) W: M1 R/ L+ l) e4 k# E; g; r6 x; n

& E3 \1 U# |( o+ b; \7 ^7 ?Integration with AV Scanning for File Uploads(文件上传防病毒扫描), {: o4 j: @4 j( i: L7 p8 D
检测通过Web应用程序上传的恶意文件。
  h! w' s& a5 j5 y7 u) v4 y
, }- z3 H& s4 D" b
2 c0 q; f, d. J, x* }( T) VTracking Sensitive Data(跟踪敏感数据)
* ]# r) ]2 L' b4 M信用卡通道的使用,并阻止泄漏。
% h& {$ ?  F/ Y
* r4 X& D* g# g; m; t; n
; q1 a5 x! _; o7 a4 vTrojan Protection(木马防护)
7 x9 s+ A9 g  ]/ Y0 y. [0 ?; j5 i, M1 z检测访问木马。& p& Q9 p, ~; R3 H  l) Z& s
: V, k- l& Q. S. F

1 I; a4 v: o3 P; O) c3 pIdentification of Application Defects(应用程序缺陷的鉴定)
- j; s% j( `, R1 G, z- z检测应用程序的错误配置警报。
" q2 R1 U3 O; a8 Q+ C% Q; A- n/ b- x7 u3 h7 ?

# ^; k! M$ Z& N0 d" k2 T) oError Detection and Hiding(错误检测和隐藏)
$ J  D6 u& i6 y/ ]  C+ I检测伪装服务器发送错误消息。' g( X: O- n. Y

! H6 \& ?' @9 f$ e
9 B& m+ B! r8 h0 N安装ModSecurity+ d- h; m/ D; ]
软件基础环境准备  u9 U, ~$ x% {; F9 c" R6 ]' S
下载对应软件包
" C0 {+ E2 E0 t* |+ t* O$ cd /root
: u9 d& p- g9 y0 v$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% v: ]: c% u6 g( r  ]& Q
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz1 C7 \! e: M  [; a6 U) j3 p# V! n
安装Nginx和ModSecurity依赖包
6 O7 d( b. [- }+ l; s+ {Centos/RHEL' p4 P7 X2 c/ ~5 j2 H! x" u
8 T9 {3 N1 ?* ?
* C* j: B: V0 W  x0 g
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel. \( B& k1 E4 f
Ubuntu/Debian
: F! K+ V/ F" k4 d4 x
/ g- @* P6 g! Q. I! a
* j6 T1 `( P4 E  i3 }$ 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* t" d0 w1 @  Z. e0 O* B( f  b+ J
编译安装ModSecurity
4 c9 V: b% O' Q$ GNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。7 ]6 E0 g. |% _

+ l9 \* i' F/ {+ \* ?/ \6 _: a# r
方法一:编译为Nginx静态模块* x* i0 Y( m- r& @
9 G# i2 D0 o! N9 P7 L
' L0 Q9 [# p' l, |% I/ h
编译为独立模块(modsecurity-2.9.1)
$ A1 P4 f& f: Y1 Y$ tar xzvf modsecurity-2.9.1.tar.gz
. @8 P) X" ?3 n$ J$ cd modsecurity-2.9.1/
' s8 c# X- a+ M# x7 ?$ ./autogen.sh
- Y" b0 e1 n/ C! I% x, v" S$ ./configure --enable-standalone-module --disable-mlogc  y/ g9 _: q5 W
$ make
2 c+ N/ ^1 [, x: O2 @编译安装Nginx并添加ModSecurity模块
+ B( M9 C* \- _0 b3 c) c% @( C! p$ tar xzvf nginx-1.9.2.tar.gz  R7 w% v2 F; b& V6 i2 j
$ cd nginx-1.9.2
- m' L% L7 O) Z* r1 W. ]8 ^$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
8 B& v3 k) w. y  x& T$ make && make install
( Q9 z  C& u4 L1 m2 H8 ~方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, h$ `' I) ?8 c; o# l: G  D5 y) K5 v  c; D0 {
$ b4 H# {9 J' c; n. ]4 P  E
编译LibModSecurity(modsecurity-3.0)+ q& k7 }+ ]) R' K- k) J% R
$ cd /root& E1 `- J* @. d% B% g. a+ g
$ git clone https://github.com/SpiderLabs/ModSecurity
) N) q& O+ B1 R$ cd ModSecurity
' W$ Q% R* G$ s  _; R( ~6 G$ git checkout -b v3/master origin/v3/master
( p% P5 K+ G9 a, }' ^$ sh build.sh
9 Q4 g- v! v6 \' n+ v7 Y2 U. z& I$ git submodule init
' t. o0 }$ P/ N$ git submodule update
, {5 b. X5 \, Q7 i( i  W6 E. j$ ./configure
# H0 x) {. U; G' U$ |* `$ make
# s- o& b/ K9 h6 d% `5 b$ make install, Q# L* I( u7 P( Q/ ?5 a
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
! D6 Y3 i/ l& a9 M+ H; E- k" I. I$ k; d2 {, Q
% N7 j$ x4 S, ?# L& }, r1 B
$ ls /usr/local/modsecurity/lib
5 N  O3 s1 K& K$ {, e6 qlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.03 o2 Y) G: `7 U  U- s
编译安装Nginx并添加ModSecurity-Nginx Connector模块/ P! P7 _; n) [& N  b
使用ModSecurity-Nginx模块来连接LibModSecurity/ t# [. c+ ^0 {

$ K; t9 ^# E. J0 T  u" Q( D
1 R/ E3 z1 ?7 n1 @$ Z# L/ p$ cd /root: g( Y- ?% i) A" l1 ~# Y  x
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
& p9 G) v* Y* C$ tar xzvf nginx-1.9.2.tar.gz
. t) k- @# ]- b5 m+ P) C3 V$ cd nginx-1.9.2
9 E" h: u; m6 ?  [$ ./configure --add-module=/root/modsecurity-nginx$ P6 K% ?8 |7 u# T6 o/ o
$ make
, |# ^% o/ N: O* p8 O$ make && make install- w. ~! G+ q# `" D( c
添加OWASP规则! A- T2 t! w4 S
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。! N# g0 a, ?& i2 d

- y  A0 q) q1 D  j  N0 ^  f! e/ F( e6 t4 E4 W
下载OWASP规则并生成配置文件2 H4 M( d( I4 F5 }* S( ~* r# |* V
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git, V. F; u( K6 ], o1 x2 P5 k
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/6 j2 J% D1 x  v
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
( `) ~7 r9 ^$ ]9 b1 f$ cp crs-setup.conf.example  crs-setup.conf0 j; o1 O, x2 z0 p' T+ X7 k  R$ m! t
配置OWASP规则/ u* y& [1 t( L. |6 t' ]6 i* J
编辑crs-setup.conf文件
! ^* O4 L% Z8 D' I$ u0 g. s3 Y' D0 u

0 @/ ~8 E$ L2 q% w! t$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf) G2 F) r- o% ?, J4 @$ n$ C6 |0 H; Z
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf; K& G' o* }* w" z( ]* J
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' o, \$ _4 G1 `" t$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
' G9 w) K, y" @9 o& q9 a: v默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
/ n, i. v6 [9 J8 O* g
' S% z  \" M, l' t, K+ P: [( ]  |  e! E
启用ModSecurity模块和CRS规则
0 l- g" i6 G1 y+ O  G& c复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% i$ {- K: N  B
5 e0 x0 p) V+ \$ o- G0 q- ^! b+ o+ |
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' X1 P1 ]6 I/ P0 t& D, \
- Q. n4 o9 `0 d* ]

6 ~; X1 c" L. E1 |' R6 H) }/ j$ cd /root/modsecurity-2.9.1/
0 f7 U2 m+ ]' A* g! S( {6 @5 ~: g$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  ; U! @7 x* u, ?# B9 t' u
$ cp unicode.mapping  /usr/local/nginx/conf/& v1 |& U4 t; _& l$ Z
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' P7 E% f; z; n3 N- y& q$ x% v

( R$ o6 a: a" Q7 F! g
# m! o2 i6 v' C9 _7 S$ vim /usr/local/nginx/conf/modsecurity.conf: [+ u, H8 x$ r8 P  ]' ?
SecRuleEngine On- z& q! s7 Q9 g* E% u9 s
ModSecurity中几个常用配置说明:
8 ]# f% h3 T, ^
  h! ]( b& H# {$ b( S8 t: g- d- O% }/ N' Z
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ h8 j/ C4 c8 m# v5 D% m3 o! T9 i1 a
, k% l1 P' ]; u6 E% Z* K" ^
* c% x2 g, H3 @( V5 K& R2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 U/ i  n$ o% s1 i6 Q( I: e7 p! u: [6 M, U8 _2 _, V
% V  j5 u) }- e1 }8 `
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
% N) p& r0 ~' r6 V! J) I! d+ G) Q+ ^7 P' X4 S( i
' q) I7 t! q6 d9 k% K
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
- E7 d7 Q8 j7 u9 S
0 A. ~$ t7 Q$ M$ D6 n
- _( @2 s1 p: g/ o4 @9 @# c在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
( Q5 @; ?* Q4 c) Y6 G- k
* A% U5 f1 H; _" j: y2 ~! P7 t7 w* n5 C) W
3.x版本CRS2 g4 I5 a: \% ~4 Z' a/ b
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs. H" f9 @8 D( T7 ]
# 生成例外排除请求的配置文件
! ]+ ^- T/ \* y# p9 @5 C$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf/ ]- h8 U9 i5 R
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
1 I0 ?& r& u% T4 X$ cp rules/*.data /usr/local/nginx/conf
- Y& e  t) o8 d+ }" J为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
- Y2 w- l' z6 W1 G% E4 d( b) x- ~0 I) G

2 p- Q/ _- f1 u+ S1 Y& @; H6 @$ vim /usr/local/nginx/conf/modsec_includes.conf0 l: S0 n3 M" p
% x# g( K" E' p9 |, b2 U3 v( d
[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
; |$ G9 |/ w9 u& j* x6 W

; G3 R8 m2 B; D" s& c4 R8 ?' P注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. |# I& f4 x. b) [* K* h7 U: e3 X
3 j" {% L3 e- U+ I# N" {
# J% b0 u9 t! g/ |4 b配置Nginx支持Modsecurity5 m4 S8 _/ l, q, S
启用Modsecurity
( M8 |3 G; {7 f$ W/ d( U使用静态模块加载的配置方法/ N# x( G3 c& u9 x6 ?3 Q, m
在需要启用Modsecurity的主机的location下面加入下面两行即可:9 ^0 D% k' k! Y8 w
" f2 @! ^/ S9 D

( V  A+ x5 [( `1 _ModSecurityEnabled on;6 t- P+ G) [" K; l8 n# J
ModSecurityConfig modsec_includes.conf;: I$ F, Q& M2 H1 `3 `$ d# h: C) c
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
: v' n' h. X  t. G% s$ d) h1 U
& I& P6 c; r% l% D
0 p1 Y6 v' c' r% ]2 I& e% [$ vim /usr/local/nginx/conf/nginx.conf2 a# _4 ~! c, o( P
! O3 k' ~: i- W( v+ V6 H/ F  f* O
2 p; Y4 T$ i; K
server {* t3 a# d" w* G) P6 E
  listen       80;
  t$ V5 g. s1 H* n  server_name  example.com;$ r& \) t( u5 F2 ~) b
7 K+ u0 E* k0 C- ~$ S' j
6 l# F* y% d+ }
  location / {8 ?0 }7 z3 X" X% Q) J/ a
    ModSecurityEnabled on;
& d: W$ ~9 P5 A( e    ModSecurityConfig modsec_includes.conf;/ X3 H7 D; j/ U- V1 B% [7 Z; m
    root   html;
- [8 ?. q% {! j9 z    index  index.html index.htm;
% K$ S. ]$ t3 @' I  }7 q6 m1 S9 {$ e* j3 h3 [5 W
}
) p* ~$ o: S  q5 P使用动态模块加载的配置方法
8 t' u% z" \7 q: V在需要启用Modsecurity的主机的location下面加入下面两行即可:
3 z" S$ n2 p) M/ v/ L
4 ~; v) q" X/ |( s& H
. h) d: |2 g7 Pmodsecurity on;1 c1 I; {+ H$ P% d
modsecurity_rules_file modsec_includes.conf;
- o* i- l$ P: L- m& V修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 l2 d8 F  h7 F& I3 A* Q7 k7 x2 y' q8 H4 R* u8 T, U( g/ J6 O+ o. X

4 V$ q+ P: E. t" E( j8 Z$ vim /usr/local/nginx/conf/nginx.conf: T! C* L+ x* u1 q. n7 Z; y

+ Z* T0 b/ s4 t9 a& I' k2 _* v
2 j1 T1 m( k: a; _' Y, mserver {; c. b2 @0 W6 z& j( A
  listen  80;$ u+ {5 b1 Y, B
  server_name localhost mike.hi-linux.com;
# U2 X0 T; c5 F2 @6 _  access_log /var/log/nginx/yourdomain.log;3 d' m! P; [% b" x2 C
# |" ?: T" L- B5 d, ~

& R+ c/ C# _& R6 a+ I  location / {
# m, j1 _9 w, Z1 W6 m
. g( H2 {7 \: T% k8 z. j
2 A3 n2 f: E; O! Y  modsecurity on;% x3 e9 C% s7 P
  modsecurity_rules_file modsec_includes.conf;- C3 V/ ?3 o# J: x* X& c9 {7 G9 `; n
  root   html;
2 s( [! \7 y4 I( U7 q8 _  index  index.html index.htm;( T$ V& K6 z1 \) o4 t
}% F+ X8 X, N+ D. {# g
}
' Z- j1 Q; b# e5 Q验证Nginx配置文件( K5 @0 b# e/ @, i  u9 Z8 l
$ /usr/local/nginx/sbin/nginx -t9 q5 R9 |* r: K4 ]0 y3 V
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok. b: @/ p! Y% n
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful3 H2 ?, ]7 |$ a. z; u6 y3 ~
启动Nginx
' q3 n$ Y! q% Z# p6 ?0 t; t; P$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
$ q/ r' A* A* N6 I
; X. X% i1 ~+ h: H9 F; J  d

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

! T3 A1 m, P' [, ?+ w4 S! ?* ~/ e

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-8-18 14:14 , Processed in 0.115081 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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