找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12390|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。5 z! Q. H# t3 x8 H
, {7 u1 a' [! ?4 |
' ^9 l, w3 ?% h$ n' p2 \3 E
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。- }. _. R- m' `5 s% G

. ~& e- m. v& W3 H3 @0 x( c# S
6 f; [  p7 I+ G1 |1 v什么是ModSecurity
' v+ b3 J( h' W/ ]& \+ EModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。! O3 [2 P( u% h

, \+ N2 d. S- c% C7 }7 D5 Q) d2 |/ {+ L( b5 {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。! B% d/ U  B' ~, y% w

9 b9 e2 M, I; X8 S  E2 @" Y: g
; @2 F' A9 A% y% [6 NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
1 ?# F1 q) I" V2 ^, |9 P8 Y8 U  J, p7 V, S* ?8 c9 q

! V' N; B" M( V  ]+ E! L7 N- Q$ d官网: https://www.modsecurity.org/
1 |0 a0 t1 x8 X' `. U0 o3 I% {7 K3 K' a7 S3 i5 U; {  S7 k
6 M0 _( l# y; ]  J
什么是OWASP CRS+ _  v1 b4 B! f. p3 E8 a2 m7 f
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
) X% B- f% k9 f. G# W+ k( d
8 O4 R4 a3 V0 `( T9 @! e9 {" {/ T7 V: F6 D4 h  r- H
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。3 y. z5 n2 a( M/ f; Q8 p6 {( o* v
0 w( L5 Z- r9 P1 ^2 Z. Q! f

4 c7 C* }/ x: u, `" l' mHTTP Protection(HTTP防御)$ ?/ S& }: q2 m& W+ w1 {$ w
HTTP协议和本地定义使用的detectsviolations策略。
4 u6 z/ }3 ?& u1 J4 m6 a# j& |: U9 @3 f/ u3 E% G( g6 s5 X

, s0 l% m: B/ U* w, `3 j% b' aReal-time Blacklist Lookups(实时黑名单查询)
! n* J# k* `5 d2 H/ @7 N: E$ h利用第三方IP名单。
# t( D* X1 a8 s# X
6 x, i$ i6 R! c9 w% U- @" h1 C
, l7 D/ }$ _) Y4 [8 D. \HTTP Denial of Service Protections(HTTP的拒绝服务保护)2 |9 l2 q. M$ h- K
防御HTTP的洪水攻击和HTTP Dos攻击。  n- _- @' N9 \& V0 t
8 J' N# X3 ^2 z6 g3 z! L
/ |/ S3 ]2 d0 s2 @7 b! `
Common Web Attacks Protection(常见的Web攻击防护)) n; B& G# U+ q' F" O8 X4 U* |6 C3 O
检测常见的Web应用程序的安全攻击。' j: n7 ]" |* Q! N4 f$ \, K
- t' b) I6 \5 c9 Z
# C2 [7 w# L7 s9 a- Q$ I* P) V
Automation Detection(自动化检测)/ r3 r$ p7 E- k! Z# o, Z5 M
检测机器人,爬虫,扫描仪和其他表面恶意活动。
0 f/ ?% _! k+ m" Z3 N4 Y" Z" d$ j  `2 ]- F4 `# i1 P$ E

. C; o+ b: V( I& ^6 N2 n5 z6 \Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 J# v% X: C2 |0 d检测通过Web应用程序上传的恶意文件。
8 a$ G/ G6 F; m
+ Z3 s& g; a" @0 a- s7 d) `0 w4 P. E0 J: t5 E
Tracking Sensitive Data(跟踪敏感数据)9 @+ d- G. \2 c8 Z
信用卡通道的使用,并阻止泄漏。3 }3 G0 W) u4 N  w: k

1 {5 D' N8 u% K# e) q. l0 c2 U1 Z4 v6 O
9 G) M7 i8 S7 K$ N2 W. [& K: BTrojan Protection(木马防护)# S$ k0 V$ @6 @8 O/ |! o( `
检测访问木马。
& Y; z' q$ L: ^0 T, B/ f# G1 Y# ?7 m  x( D! h  J

' _( K7 w. n$ `* }% `% d6 xIdentification of Application Defects(应用程序缺陷的鉴定)
3 O2 w+ _0 Y# m4 z: |* h检测应用程序的错误配置警报。8 J/ J: m! ^. y4 Z$ v

- k) c) ^2 v- r/ c5 O" V: F- c: C# F0 k$ t% Z
Error Detection and Hiding(错误检测和隐藏)
7 D% f9 @) l8 I7 |检测伪装服务器发送错误消息。0 h9 J, x2 |4 F, o1 W& _

% D! O' B; \7 v; q9 f4 _& H8 q) u# H  \7 m" u" {5 q
安装ModSecurity& V8 x2 Q$ p9 `# {* x# H% @
软件基础环境准备3 |; r. T' i9 ~$ ?8 E! I* s) Q
下载对应软件包
) S" \' T1 g) J. \: B$ cd /root0 _& D* n: J2 _/ M; k
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', x/ J! Q, e8 r3 e/ j% D
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
( i) ?# g6 }( \安装Nginx和ModSecurity依赖包% [+ K0 a6 d, f- k! n0 M+ N
Centos/RHEL2 H. r& @$ a7 m1 I2 [: J2 T& s

" d  G7 D, b: A
/ U# a2 W( Z5 t$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 t1 {& w5 w% k9 H( v# w# l4 J
Ubuntu/Debian4 w8 d( K' d3 _3 Y( u/ Y. _8 ]
; T7 ~! [' x. Q  C  L

1 e8 l0 y$ ~9 ]1 h+ L: ~: j: O* t) p$ 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: L( g  Q* K' t$ s% U& C, X* W
编译安装ModSecurity" |; I- a5 G4 P8 z# O; u8 i3 p9 ~+ _) M
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。* ]8 K+ T' E' O3 G5 |( ~
! L$ U$ D4 f7 M3 m' p/ K/ M
3 b1 X% X, b- o1 e: p0 i7 r( b) v
方法一:编译为Nginx静态模块* |3 q1 h/ [( C8 l

5 H% O' a* X$ ~+ g) ^5 [
# D* c$ l* T" o! K编译为独立模块(modsecurity-2.9.1)/ N" h( V) k5 |& d& b; h
$ tar xzvf modsecurity-2.9.1.tar.gz( k6 O1 I. o9 T7 g  E% @0 @% H: `
$ cd modsecurity-2.9.1/9 a/ W& K8 S- A$ K( |- _: h
$ ./autogen.sh
; _8 q$ E0 G" q2 O- K  E$ ./configure --enable-standalone-module --disable-mlogc1 M" k7 R5 L. L
$ make6 ^0 j6 ~- T9 k" [1 H, t9 z* K
编译安装Nginx并添加ModSecurity模块# g8 t6 w' |& |5 m$ R' b
$ tar xzvf nginx-1.9.2.tar.gz' b, f# C+ G! ]% ?' G7 |7 x" L
$ cd nginx-1.9.2  h2 K9 e" u: l2 Y2 ~
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
& M! X, A) U' ]) Z; j4 C$ make && make install
7 Q1 A, I. E% S% a# ]! _0 Z3 {; u方法二:编译通过ModSecurity-Nginx Connector加载的动态模块  X7 D8 N+ t+ o+ A1 K
, q3 C  p4 s' m# t

; z7 o/ B7 t6 q$ H+ R' d* Z! l编译LibModSecurity(modsecurity-3.0)
2 I9 X' X: x/ y" t$ cd /root( y+ D# v5 E4 y# T
$ git clone https://github.com/SpiderLabs/ModSecurity
, s2 @8 z4 e1 P$ cd ModSecurity0 z6 v' |$ ]4 b; W4 V2 r
$ git checkout -b v3/master origin/v3/master
- R8 h" B# ?( P" k+ h( P0 J# E$ sh build.sh' }. K+ y9 c' ^
$ git submodule init: Y) W0 _' E0 ~1 x: n, h2 t. h& E
$ git submodule update; f  u( C4 h" O" _# O. V
$ ./configure
3 u. i" k1 A$ }& j$ make
6 P) b7 ^# o( {/ r" z$ make install
6 P: v& z% p9 CLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。- Z4 o4 U& ~' ]) K# q- R& I* E0 ?
% H- y$ R0 ?! b  k( ~5 g) c

; w+ z6 h! t( z& K  W/ |$ ls /usr/local/modsecurity/lib. K. I! J  V4 v- ^% B
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0$ C3 ~. \( @; P
编译安装Nginx并添加ModSecurity-Nginx Connector模块
+ U9 \' b2 o4 i" I9 r: _  L使用ModSecurity-Nginx模块来连接LibModSecurity- J) d4 K# T. a

0 C8 @8 T7 n3 L8 b
* {9 s1 x/ f8 B5 M/ u$ cd /root
( B6 ~8 _% G3 H; m7 q+ z$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: R* L' C/ Y/ t; ~; W5 o8 e) Q
$ tar xzvf nginx-1.9.2.tar.gz/ v8 n6 d  i1 z: A) j
$ cd nginx-1.9.2
* T" e* f' N2 [5 y- Z/ H, \$ ./configure --add-module=/root/modsecurity-nginx
1 H/ Z+ V' M# [9 e$ make  F' l% l# }5 M4 c3 j
$ make && make install
! N1 a# i- @# }: k! C$ q( t# x$ c添加OWASP规则
5 c' D2 }7 v) O6 \$ |ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
( z& d& J- S) D% r8 u
% K* _( i" A  g/ b* t8 M
" z  [# C7 d# I% P4 z6 Q" ~下载OWASP规则并生成配置文件
7 h4 F( o! P) S1 ]0 l" }5 L5 r; M$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git! {! H6 N* {+ u" s9 D4 }& I5 P# s
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
  r2 \# g- P( m$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- C, |2 E* S! d$ cp crs-setup.conf.example  crs-setup.conf
9 `  m4 e$ `; E6 w  R配置OWASP规则) m  {% G( V( u- u3 Y& e8 d( Z
编辑crs-setup.conf文件$ s- H* q) H2 q0 i6 `9 s6 l

, D; U/ _! j' Z" `! P, Y5 N& b
) v7 ?  m. i8 h# ?" X$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf+ `% \4 t- M. Y$ L, s
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf5 w& B, n0 t7 S! I
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf! _  P0 n0 N# H8 {1 J
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf+ q# `0 ^; q! q" V) j* j4 S  n
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
: ]7 I) [: B7 p/ |! f4 _3 V1 @! ^/ `/ q
6 |" }& m: l: y8 T+ |- G& q
启用ModSecurity模块和CRS规则
+ w! f* n; X( z9 `4 d' D复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" h$ w+ _: w- J. |8 e
: q& f( n. G" j: w9 i  M" a
* U: |( L/ U# Smodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- y, S1 N/ ^+ M1 v
5 ~. D7 v0 B, G% x, E* Y, R

2 W) D9 M/ _1 h3 {/ h% L& t$ cd /root/modsecurity-2.9.1/
2 |2 ?% }6 D! k! _$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  9 d7 a; t/ o/ ^* k3 G% L" Q
$ cp unicode.mapping  /usr/local/nginx/conf/
0 I9 G. A& `4 u0 Y# s将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
, Z* B+ X" q$ }9 s/ q
7 A$ g" j  w! a/ {) G4 V" l& x4 X% }3 ]# V% B
$ vim /usr/local/nginx/conf/modsecurity.conf
2 d3 o0 q$ c1 c; g6 u% j; ISecRuleEngine On/ r' R) j* ~' A9 @$ R  ~6 h
ModSecurity中几个常用配置说明:5 C; ]. i' e% L/ `: ~- i! {# q

  k. c. v7 ~5 o6 E/ H
, l4 W/ u! Y5 t4 x1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。* Z. e  M5 J4 r5 }# d

; O1 n' y6 A$ r6 x8 v* E; {& Y, m) o8 s; n2 ~9 d6 H
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。8 i/ t' w& s# l: L" }: u) _2 ]- X" [

  s. F3 Z+ s% [2 m' |
( [- q+ `, |6 \2 G* C: }3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。$ |4 W; I: Z. z0 Y8 d# Z* a- J
! g$ t. \& a: A) t# G, Y9 u
3 f- s6 H3 h, |. f  k
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" n* n0 z, V7 h# `( A( c1 z
2 T: D# O7 l2 E9 c# ^
3 _2 W3 |2 L! t+ r
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。4 F9 j. O9 k+ |  `2 M1 b

  j$ M; p: k( K9 V7 X/ p7 b6 _4 y; _5 g4 T
3.x版本CRS' _6 M3 g2 R5 @+ ^
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& S; F) r" I) R  y; E# 生成例外排除请求的配置文件$ V- X: T3 b5 r/ O) w* L
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf/ C0 w  R; Y5 y5 ^. T; L9 O1 E
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf  Z. b# h+ ~, n3 ^* n6 \" ?; c
$ cp rules/*.data /usr/local/nginx/conf
7 w; M- h' Y! V+ l" |$ w为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
4 x( _1 V9 h, \+ p& t* M) q/ o" b3 z/ v  V( R9 P* y

7 c, n2 h4 w) m% s$ vim /usr/local/nginx/conf/modsec_includes.conf3 K' f! ]& b+ w0 E$ L
# a* Q) e! [! Q9 v- U/ b
[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
' Z/ u" }! a* T, v) H& l/ P

: w9 }( N; R) @) n+ ?) M6 C9 ^注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。. |- Z9 n+ \( z/ ~; X
) @. i0 ^; t- J9 |8 l& y$ _

% L* N8 B' X+ `1 G配置Nginx支持Modsecurity
+ L% K. p7 ~/ O! @5 E" n启用Modsecurity: B+ k$ b/ M7 I# c, L  g6 D  ?
使用静态模块加载的配置方法, c2 F. W4 u5 b
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! Q5 i$ L# z+ f; Z, p2 @5 z* a6 q  B7 a. R0 L: y- z
: _. x4 k4 Q( q. ?
ModSecurityEnabled on;1 i/ d" @, |( ?: H
ModSecurityConfig modsec_includes.conf;; ?, x/ T- g7 h$ @3 `: F
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* r; N- h2 Y! x( z) _9 D5 b: g) v: e9 ^5 \2 J/ v; L/ _0 T

" k+ \3 r4 J3 Z3 Q2 e+ Q$ vim /usr/local/nginx/conf/nginx.conf# Z" w( D. e1 |# ^
% W8 n& S* _1 j6 u

8 i8 z; {: B6 D, s7 Wserver {
. Z6 ~% @8 u+ Y. z  listen       80;8 H" U0 C1 G+ M. W4 r; I
  server_name  example.com;
2 b2 F8 U) x( ]  k) m# W
6 q/ y7 O) C4 w- x- d2 n6 \& l, B5 S/ s5 f
  location / {6 B/ \" g8 k9 q8 h4 T; I4 n" S
    ModSecurityEnabled on;. w% r& ?& S1 v* {/ s
    ModSecurityConfig modsec_includes.conf;5 d0 Y4 }2 f3 p3 i+ W
    root   html;
* U8 g/ v: A. k4 [2 [& L    index  index.html index.htm;
1 w4 G2 M% I: p  }# Q4 ?1 A9 P9 \7 u% Z3 h
}& D6 E) |8 ~0 G
使用动态模块加载的配置方法+ D. A" \  \' o9 S6 c$ h
在需要启用Modsecurity的主机的location下面加入下面两行即可:5 g. m  J9 c4 c7 n' F
/ b6 C' q' M0 L
4 Z- K' ]4 v/ F: E
modsecurity on;
, Z: @( y2 g9 Kmodsecurity_rules_file modsec_includes.conf;0 t& A6 U" b* u" Z
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。0 [9 \) P0 L3 o- F# ~. y  \
  I1 \/ ^9 l/ r
; x. O0 o- F# u5 ]2 `% e  L: a/ A
$ vim /usr/local/nginx/conf/nginx.conf# F! \9 z9 j6 m0 l

9 ?! v8 j8 V+ h; L% z- J$ q( D5 r% I: r; k( M# V
server {7 w: J' l# t; d5 D, g
  listen  80;$ ^$ U# V4 H3 l  x6 ^
  server_name localhost mike.hi-linux.com;
* _0 u  r9 t4 X' M, q9 h  access_log /var/log/nginx/yourdomain.log;' v# a1 C6 R$ q

, n, F2 d0 T  Z6 j/ B' k
$ y% K# s' W5 X9 w# G/ B+ d" Z' Z  location / {
4 n: n5 a9 O" f# f( \3 D; u1 j4 r# I
; E6 O$ `5 X. o( h
  modsecurity on;0 \1 h/ U$ g/ B$ w+ T; o8 J0 A8 @. ?
  modsecurity_rules_file modsec_includes.conf;
0 `% P/ I& w( w6 D  root   html;* T& h: E8 g/ e2 @$ D, {1 q
  index  index.html index.htm;$ V: n+ d2 g. U9 v
}& Z5 ^6 g; y/ R# S' o; j) ~
}# n, k9 V2 d$ b5 z
验证Nginx配置文件4 t$ a& D- Y2 X8 p3 C
$ /usr/local/nginx/sbin/nginx -t
9 \% e& R, e9 e7 s% B1 z. Nnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok/ `' h( ^' u# w, j! |
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful! }4 `1 W' q# ~- z3 P
启动Nginx
. J) v; r! L  U; {$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4 |% A% ?6 Z( E' t0 X: l+ D  Y4 s6 C. e+ b3 Y

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


/ g5 f+ A4 ?* j) [

本帖子中包含更多资源

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

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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