找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11264|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 f8 y. U/ f7 l8 K& b! B, @! a" N
& z6 n$ e/ M5 c  s$ p: L! |9 }* m) j
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。2 a% U/ W7 F! V. O& }8 X2 I9 @
8 h" d2 `+ b5 D" s

! z" Y3 S5 O, }7 o1 ^) i2 ^& {什么是ModSecurity8 a- Z4 h/ V; d0 w5 e# t
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。: D" _) v% x* r# `2 W: G3 R& H

" q$ Y5 ~- R' M1 y+ l$ N
2 `/ H6 N4 W# M7 F+ xModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
  I7 i8 f* S6 d+ t: i# T; T1 D# {' z1 \4 z; }, b6 q+ ], i

- G: v5 e2 F+ }) @* \& DModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。* m  o. N/ }5 K- g5 i( T6 {' F
7 r$ i0 W( p' R, @' }) e- w

! B5 |( v2 R. p7 w. G) O* Y# m官网: https://www.modsecurity.org/
* D+ ^* n) @, o. y! F% a! K" H! e  G" u* i
) q0 t; \" @3 s7 |/ A, l
什么是OWASP CRS
) `2 X8 I4 a; l' `# a; D8 ROWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。2 a, A3 F6 X# ?& S0 A+ A
8 ~* ^; ^# u) t" b. A+ ^% ?

) ~2 z8 w9 Q+ K( k; k3 \; M( ]ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! |% Z2 m5 z3 _4 J; F( c/ o$ x
5 l( x% X' y  @8 |' X* w! W) D
; X. Z3 C# x5 N3 }4 }2 [. IHTTP Protection(HTTP防御)
- A+ j3 P1 ?3 V$ e& fHTTP协议和本地定义使用的detectsviolations策略。
+ U  i/ D6 p% W! p" v3 Y9 M
$ D! h5 w1 H! Q: U- R; k$ v; o
Real-time Blacklist Lookups(实时黑名单查询): S4 T+ x. {  Z2 J
利用第三方IP名单。
/ a% s% O. h8 D# p6 M3 i5 ^) d; K; y$ b/ e- p
$ s. i/ u3 X; v* {1 }2 e' z" M4 s0 i
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" _2 L  z( b7 }& Q5 u防御HTTP的洪水攻击和HTTP Dos攻击。
/ N' A8 e- |2 s8 @( W! u  J( O) Z

) X! P2 L1 n# C, o9 eCommon Web Attacks Protection(常见的Web攻击防护)
9 {% r! N( y7 P% t检测常见的Web应用程序的安全攻击。: ?% I. a2 z4 f4 e; ?* K
. e/ R" b; c: x5 P/ W1 G

: E$ m/ W+ t! Y: `/ A: tAutomation Detection(自动化检测)
( `+ I3 A/ D" r5 m( ?检测机器人,爬虫,扫描仪和其他表面恶意活动。
( I3 Y) i% k& K5 ~# C8 u( f
' n6 J. o7 g& H9 L9 r9 \2 d
; s$ `$ p8 s( q1 RIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)% k' x& b/ A6 |& |/ ?- X7 W
检测通过Web应用程序上传的恶意文件。) q5 }% _  c9 }
  `$ S7 u1 H' o

$ n+ q! G. _* k/ [, D: F  \9 ETracking Sensitive Data(跟踪敏感数据)+ W% P$ \: ?3 ]# N" ]- u$ {
信用卡通道的使用,并阻止泄漏。
% @$ ?: b$ S1 g2 b& f4 c: X
  |6 ^1 C6 ?4 ~2 o% y, _" V) h' @' w7 h3 P. G0 H: L0 ?6 j
Trojan Protection(木马防护)
2 X1 X& d- E/ E! t+ Y检测访问木马。. j: A  J- |0 J8 X

, ~: \' V! Y! c7 u6 o: t* _: K4 r' V2 |7 }
Identification of Application Defects(应用程序缺陷的鉴定)7 r: k" M, Y/ }% k. K
检测应用程序的错误配置警报。0 v" M" d( U6 d
, s7 @: @# X9 M7 [. R# P

5 X) r3 r) f, K; {; K1 t$ RError Detection and Hiding(错误检测和隐藏)
) l  z* k1 [( @; v5 \1 T9 A) o检测伪装服务器发送错误消息。+ n1 P( ~1 ^' f" j( b  ]! X
+ h+ ?! l, F0 v! C2 w  w
3 y" h9 r3 g* b" O$ t  Z& @3 m
安装ModSecurity
& s) {+ o: k  z1 a) B软件基础环境准备0 v1 x+ |3 a$ b# N* c# `
下载对应软件包$ m" I, a5 z7 D2 b9 b3 ^/ ~
$ cd /root- l4 ]4 B! m. c2 P
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- G$ K1 @1 Y8 P0 [
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: c* q* @* d3 L$ L
安装Nginx和ModSecurity依赖包: R! j2 O+ B- ^, R4 q* c
Centos/RHEL8 @8 p5 ]% a6 Z: J9 y
% O  C, [! P& F1 Z) P
8 [; I1 Z3 Y7 W4 k3 M" X
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
8 ?' M6 ]# S! A1 i7 E6 f& nUbuntu/Debian
5 ^; o( [3 H; s0 L2 c1 j: ]+ Q6 g0 \: D6 Q, V4 h

! |. F, v# J* I7 d( T6 u. ]: r$ 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++-dev4 A7 v4 {$ B6 @2 I+ o4 W
编译安装ModSecurity6 w) G7 n; _0 b+ Z
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 \2 V4 e6 M1 w  o
' V$ e0 g; h$ l6 v+ V
% N4 {" e1 q0 B( t. J
方法一:编译为Nginx静态模块4 w$ A8 O6 [% v; b3 w5 a% Q
0 W& b( I- C) o( ^- k6 @

, |. S0 e8 m6 N8 y% S% }' u/ w编译为独立模块(modsecurity-2.9.1)
2 }5 s3 s0 f' ?5 H& x: P$ tar xzvf modsecurity-2.9.1.tar.gz
* r5 `% L- Q1 H/ N1 p; W: d( l$ cd modsecurity-2.9.1/, b7 r$ g( C" B) O; m7 B
$ ./autogen.sh8 o  O  u3 ]% c: V9 H, D
$ ./configure --enable-standalone-module --disable-mlogc
+ j; X3 Z# A/ ^: a# ^$ make
* u' i* J- Q7 K编译安装Nginx并添加ModSecurity模块9 q' R  J( h" ?9 c, j# O
$ tar xzvf nginx-1.9.2.tar.gz
$ g7 u- C$ p+ d8 Z$ cd nginx-1.9.2
3 C2 J- u& o3 e& w. c/ C$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/7 P# @2 O  D) w+ E. o5 B* l0 Z( Z
$ make && make install1 s: y7 y$ k0 Q! N: A  K7 H
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
( b* l: j# t# i5 a' ]
+ c  b6 C4 T( M# B4 H
6 J* L4 x9 ~# C  h* h$ ?编译LibModSecurity(modsecurity-3.0)# x+ S! J; t1 R" R' j$ W% x# K
$ cd /root
7 {, ~! U$ h! A8 p* k1 x; \$ git clone https://github.com/SpiderLabs/ModSecurity
$ `# w* Y# w0 n* D$ G' ^5 ~7 L$ cd ModSecurity
4 U6 L' w* @% D' _$ git checkout -b v3/master origin/v3/master
- W; m2 s- C# q0 }' Z* H& [4 N( s$ sh build.sh
# c  D4 x) @2 g) j& @4 D/ ]! m$ git submodule init5 }; D* W+ K/ ?* @4 ?/ P( W: X
$ git submodule update% G* }' E7 A) J9 Z8 t' r
$ ./configure
2 [: ?$ B, }3 L5 R5 @: p6 L5 {$ make
: V* H( G' [: F- `5 o9 Y3 h, H4 x$ make install
4 h8 \8 v7 L1 S+ h0 c' N9 fLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
9 F1 N1 y  ?: ]
7 b/ O  U. U* V
: Q3 _; o9 I2 t" z8 o$ ls /usr/local/modsecurity/lib, h2 O0 S2 a% `+ J7 H
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
8 `6 o6 G0 H- ?4 V编译安装Nginx并添加ModSecurity-Nginx Connector模块
5 t/ L( q; ^/ P* w1 {使用ModSecurity-Nginx模块来连接LibModSecurity
# E2 i- o: Q: }; S% O! R$ r5 W+ a  E* h7 }2 d; I& S
  X7 Y) I, F# {# H
$ cd /root! Y: k, J% B5 s6 w" T! Q; U' P
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx+ E/ x6 U  w5 O& Q. z5 ~
$ tar xzvf nginx-1.9.2.tar.gz
6 K1 Q. n: ^9 k2 T0 Q1 ?' o; ~$ cd nginx-1.9.2
2 M- z/ G# K* h  Z* y* u3 q- i$ ./configure --add-module=/root/modsecurity-nginx
; E: n' R7 f; z: [, B$ make: Q2 X& {/ y2 F7 f8 F% o3 Z8 z
$ make && make install9 m: C; W% e- r3 p$ c! V) @" v
添加OWASP规则
9 V( V; P7 D$ q' V. L. J% LModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
/ a9 B% f. i& J5 z, I$ I) @5 a. Q( r+ s0 L" e5 t) H+ [

! i" J: W0 w( F0 D& E下载OWASP规则并生成配置文件6 }8 o5 n  ~, D
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
5 G6 ^0 V. S/ S$ c; ~$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
9 W- E# c+ j- f  V$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  R; F6 t; O/ l- U7 N* Z6 H
$ cp crs-setup.conf.example  crs-setup.conf
1 w: R# h3 L  v2 u: Q配置OWASP规则
, o$ W. d7 @# z编辑crs-setup.conf文件& H4 Q: k+ p! }
3 w: D7 N8 y8 I7 d

' b- _+ Z. i% x* v$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ g* [* f- E2 ~' U$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* K5 V+ L. k& [. p& N# M
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf: d" ]0 K* o  m0 v$ P$ ^
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf: _7 [$ K7 f; z: b8 T, r& [
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。2 ]! O" ^2 ]% _3 Y7 N3 l
( H7 q0 v5 f& l3 i3 I
% L6 b: n# t5 k1 c3 m7 g
启用ModSecurity模块和CRS规则* Y9 i9 h2 A) i1 ~( [5 C
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; n) o9 M9 O$ d+ f) R5 i* K
6 p8 F  S" F" A( P& P& R2 d2 L% n; c: i$ ~2 x4 D
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
$ M  W; P& ?2 D1 O1 ~& V: v6 S8 N5 V$ ]; G2 g7 s

* Z7 T3 t. Y* |$ cd /root/modsecurity-2.9.1/9 \( u  j0 g8 |& D0 f8 G1 d. g
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  * M7 c) w# `. O1 U, J/ e: R7 s
$ cp unicode.mapping  /usr/local/nginx/conf/
3 Q. u5 k8 T1 Y4 p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
2 I3 o/ b; ~9 [" C! a2 W
# w+ z  [$ o- D% O# o3 f$ m
1 |3 c8 z, s/ }- h. N7 |$ vim /usr/local/nginx/conf/modsecurity.conf7 T4 p: E- b# e7 w. N* M: J$ H
SecRuleEngine On  G' b- D4 l) g% m0 T1 ]( E
ModSecurity中几个常用配置说明:( c$ {  x' A6 Z7 [! t4 \7 {

1 G! P: ?1 x: _3 c) v) h& E! C8 q5 N; U) i3 |
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。  l; _4 k" A5 v

1 w" f1 {* p2 _9 I+ x. {3 A" v% @2 k4 a2 t; @2 _/ ?
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
! J/ F) M. y. {- Y* z
- R& c: M3 N- Q, {. ?/ Z7 ~8 r+ m. k- T& {& k: I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。( l$ @* X& v, g+ H

% P" l! ^  t5 i6 ^4 U) h( J% I. D" P, _( b. ~, ]
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
% d* f" u; T; F% b+ |! ~1 t" k  |+ V( C  N0 E+ u8 ]% k. f

" u. a& I/ f. _/ {5 ]( [0 x' C在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& A1 d0 c# ]1 Z6 m: ]* t
# S$ @7 V  @% ~" l: M, d  [1 N  W+ q6 o" q: n* }( A) B. A' U# ^% `; q
3.x版本CRS! V8 c& J6 E5 L. V+ c8 b6 ^/ I! f
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
5 o9 m# U! R9 N: G# 生成例外排除请求的配置文件
% P* f9 [1 H6 j0 Y: t% h$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf( B( j/ _3 _8 d  R8 g
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, ]$ u! E4 H4 N' T2 T, `
$ cp rules/*.data /usr/local/nginx/conf& j. i! z" m; \% y6 k' Y
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。& q6 K5 `. M: C$ N/ p0 o

' n2 Y# X6 C$ H: `( {. Z" b- ^* N# s1 ~/ {% d
$ vim /usr/local/nginx/conf/modsec_includes.conf" c9 J: h, ~3 c- y- m

: ~: L1 s+ |2 i& Z; x
[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

% T  K- o7 N4 ~! n+ ^7 T/ v: T0 N# n) ]3 [+ G) k3 T6 `5 j
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 O8 }( S- f& c6 b) P4 G* e# j) H  Z0 v2 h/ F0 q' t5 l3 D

  a0 z9 O" F' Q( c0 L配置Nginx支持Modsecurity
* R  E/ R/ x5 m/ a启用Modsecurity
* ^& u0 o2 F" h2 d5 \9 F2 r: G使用静态模块加载的配置方法' q. k4 |: Z. m" E
在需要启用Modsecurity的主机的location下面加入下面两行即可:
! K; A7 K/ G7 p, g1 ?  r6 b
# x/ P# g( ]+ y, P" b6 X2 `  Z+ \9 e$ m) w  X1 [! ^; A
ModSecurityEnabled on;, J- `" |9 i1 M; B
ModSecurityConfig modsec_includes.conf;
  A' m* I# m; W. @" b# p: r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% K6 `' h8 U$ ]* I3 m3 q, Q8 N* E8 y! g

7 m8 F/ w) p0 H4 g; h: b$ vim /usr/local/nginx/conf/nginx.conf
- O* z; O" g& Z! ?0 m3 H: b) i" P, u, _+ |$ z

. x+ c+ C8 ]% r0 z7 _1 {: }  |server {) Z5 R0 {3 e2 D' l8 b9 ?, _
  listen       80;
2 Q5 P- r5 C7 W5 i4 W6 F+ S  P9 @: {  server_name  example.com;- C* F/ ?5 r& q/ |
6 d* A, q" m) f

9 I1 Q9 X$ @0 p* l; B9 V  location / {$ ~" Y! t1 f9 G9 }, Y" O# W
    ModSecurityEnabled on;
0 E8 n, ^* F% ?    ModSecurityConfig modsec_includes.conf;
; @. w6 U3 [$ m    root   html;0 Y5 v3 E& M$ t+ Z
    index  index.html index.htm;7 E! T4 m3 s( a6 m7 w( c6 F
  }& y) w& Z5 {/ h
}- I" r! n! V2 L; g+ v
使用动态模块加载的配置方法7 E3 |' V$ F7 f, R
在需要启用Modsecurity的主机的location下面加入下面两行即可:" M8 U, n& ]5 ~( J  [& U2 ]0 G
# i- a1 m% K: O8 G( @# H- Y& ]
1 `8 {+ Z: n. R6 D! i
modsecurity on;8 r6 O, `  n3 N% j
modsecurity_rules_file modsec_includes.conf;
- |- M, m7 B9 R8 `4 Y  b* D修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
- ?/ o7 ~' D1 o/ K6 F& V
2 b" h% J' ]) Y3 o8 t' J7 O$ [/ D$ s* [" G- x
$ vim /usr/local/nginx/conf/nginx.conf8 [0 l: z/ i: C7 Y/ J9 X# t

; Q' @3 s4 A. h+ H' E6 q) ^* p8 t) J9 ~' H& |0 w0 F- ~. }
server {
' _; P  `- g  ], E7 k7 D2 M  listen  80;& P9 z* V8 U  i( h; o
  server_name localhost mike.hi-linux.com;
' K6 R4 X/ b/ G; C! b% E( e( I  access_log /var/log/nginx/yourdomain.log;
! B8 O: z9 a5 S& x1 l
/ r' ^7 |' G! M& r- ?7 T2 Q) F7 ]6 g' s. M1 A- {" M
  location / {9 m/ q* `/ w5 b  `2 {
8 I# m- ], p% p/ w
2 l" u" C) z) S" h8 u
  modsecurity on;
* ^. x8 o' h3 ]- j: s* h5 s" {9 V  modsecurity_rules_file modsec_includes.conf;1 \# g4 }. A( d& l% P
  root   html;
" o: h, B$ b; `  f: R/ i  {  index  index.html index.htm;
. s8 [3 T, ~* p+ F' g, d1 U1 e, c+ G}+ Z+ K; t- Q8 z" _$ L
}
9 {9 e6 }. g8 U6 }1 L5 d验证Nginx配置文件
. o0 r  s* d' P; H5 C5 i$ /usr/local/nginx/sbin/nginx -t
0 I# R7 H$ l' |3 Y; {; Q: B9 Gnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- Q- A9 L( p! Q
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful0 A; g* P1 n5 a. V, O( P8 X3 y. q
启动Nginx3 w. S) Q2 A# U9 z$ R6 W6 e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf$ q0 e1 V4 p0 r5 E8 K

( F' T7 ?- U& u0 l  r' A. I" N/ m

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

4 [, m: s+ |1 L( \; {2 _

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-23 07:49 , Processed in 0.117098 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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