找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11749|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 I1 z* M: ]/ y$ Z: l# \4 n4 A- |& Y5 a5 `; w. x- r* L( b! d
9 z" Q8 X/ t0 n; T
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。" ^  F8 `" n2 j

& z# T9 i: K- B- a* f3 c1 X
5 t3 F  s" }5 s2 p5 a4 a* A0 a什么是ModSecurity
2 {! T+ W- C! j' m- E6 UModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
1 j6 p& ?& W" U4 q3 B' s
7 V1 W( |; ~: j" n6 Y1 _9 ?6 F" J: w  b; e
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。8 G3 [% m$ K) q

& J# J0 I* e7 `1 r) ^" _) b0 x0 h
% o; F  Y/ [* K; ?ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。& c9 Q. [3 d) L/ j! Y  o) g
5 I" t1 j0 P+ z: o4 }" O

( n5 W( E  }$ j( f5 g! ?官网: https://www.modsecurity.org/
, [& O2 W5 W: y# ^. i8 }  G7 |0 J8 T9 j: F. m2 Z4 J# q
8 X$ S4 v+ y$ B( k6 N, j& t
什么是OWASP CRS9 [6 U% P% b: }. ^5 ]2 d: Y2 v% |
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。. t8 g( Y7 m. V. Z+ G8 d

* D9 g2 ?* m* o. {  j* J7 h  ?0 g6 x% K4 p, c  a. \$ i
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
) L3 _1 r2 f3 P% @  k
- ~' U6 J) x4 |, y; H" s5 @5 i. e1 O
: l. \) ^. }; G3 @" u5 `1 \$ qHTTP Protection(HTTP防御)7 L; j% w) I4 \2 A2 g3 X0 B# [; p
HTTP协议和本地定义使用的detectsviolations策略。
- L) G, S% [# I4 ?4 {7 X
6 Y) o; Q) R% P# i9 b0 r1 ]+ c2 l  V0 v& h8 U
Real-time Blacklist Lookups(实时黑名单查询)
3 N* \& Y$ A# `8 f+ a利用第三方IP名单。
0 ~8 K! d! P# Z& L6 v. U( S# n
. X! P; f7 q1 w) k' W2 r% U
7 k8 e- U3 C2 |3 EHTTP Denial of Service Protections(HTTP的拒绝服务保护)
7 T! D- t. a- J* C: a防御HTTP的洪水攻击和HTTP Dos攻击。
# {. ?" U* N+ ~  s
4 L/ {9 J( c5 d7 t. j" m2 ?4 Z; n* M/ s: V4 a/ x" F- }$ W
Common Web Attacks Protection(常见的Web攻击防护)
& ~) k) N" e5 I4 j% L检测常见的Web应用程序的安全攻击。
5 f: g- h& S- s! G/ I+ A. {1 ~6 ?7 Z) l6 V+ p" L7 c) \) D

1 A: e# s! Y% O  [" zAutomation Detection(自动化检测)
8 }6 k0 b0 R/ S7 [9 a( b: |6 N* Z9 z检测机器人,爬虫,扫描仪和其他表面恶意活动。
2 h" [& X. t2 R; G# `0 n/ m: s# J* X/ V9 D3 q

+ s( w) N+ B, I& LIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
8 g* j1 T2 P9 L* [" y检测通过Web应用程序上传的恶意文件。, H; b; h- C# L! u/ @6 d
- w7 D) o/ P- N2 v# T

* j+ v* p5 c# J  o3 a- i& wTracking Sensitive Data(跟踪敏感数据)
* j% K# y% [, ~6 e6 \9 u2 g信用卡通道的使用,并阻止泄漏。4 y; Z  I  ~7 m
; S( r: |+ K5 c) p
, a! k1 P' E& _3 M
Trojan Protection(木马防护)7 t" q  t' P/ f0 Z/ n
检测访问木马。
! u9 c6 v" W6 o7 b9 k! y0 E  a* s$ `9 R* F! z

. G! r( {% l( N3 cIdentification of Application Defects(应用程序缺陷的鉴定)
2 r, V' c7 D  R- I  y) P! t检测应用程序的错误配置警报。
& V5 A& q7 H3 h  w3 N7 `
1 G; ]! @! A9 w! v3 ]2 O: u
9 y- h. ]/ |2 Z; M$ ]( jError Detection and Hiding(错误检测和隐藏)
- G4 c" ~) @  C  H; Y/ l3 O& ?检测伪装服务器发送错误消息。  m8 J2 u6 k; d7 J5 l( z  I

8 ]  w  v' _& D% r8 k% _" Z+ g* h/ p9 |3 `" y0 u+ |
安装ModSecurity% i0 ~- E- U' |) ^& R
软件基础环境准备
! ?  n2 X/ \, W: _' i4 {2 E下载对应软件包
) C# h6 e$ u* w" t, v$ cd /root
% }9 a0 Z3 M2 h( f, V5 }$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'3 B4 u- e/ u8 j
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 d# H; e- V, L1 K% V) x9 u$ s' x2 v' H
安装Nginx和ModSecurity依赖包# F6 d8 G" W( m7 c
Centos/RHEL
( G4 \9 L, Q6 m, f0 s
. Q1 C. e( V: S8 {! M! j* i1 l% y  y+ S
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
5 w1 x1 _3 U5 |' C2 B0 e$ t  EUbuntu/Debian
* f5 W- A( @" P1 G
' Z4 ?+ [( {# T7 J& X3 s! H5 Q1 c* P3 U2 \5 [( b! U8 \5 r5 q2 v
$ 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++-dev5 j5 P7 ^& \! f3 h( N9 J
编译安装ModSecurity& S) ~" _  `) }: [
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。4 c6 d+ }' T! T6 J0 `1 F

, V' h2 r3 \9 |0 [
" p- e* @" R  O/ ~3 W% n方法一:编译为Nginx静态模块" w1 P7 f0 ~+ ~  N' N% K2 H: j; R( Q

- F* f+ ~3 ]  Z9 T5 G7 S2 W& \( \9 t% g- @1 l6 `
编译为独立模块(modsecurity-2.9.1)9 P% X; {; S" R* s
$ tar xzvf modsecurity-2.9.1.tar.gz
3 L- k6 G; x9 J; w: |! c* \$ cd modsecurity-2.9.1/  ^6 T- H9 X7 X' u
$ ./autogen.sh
1 S; c3 A7 q9 U9 g$ ./configure --enable-standalone-module --disable-mlogc
- H6 g* u4 h; a, r+ r! I$ make+ O+ M$ J: k9 x, t1 ^3 G
编译安装Nginx并添加ModSecurity模块
. c: u, }- d3 A4 E/ V2 }& X% Y$ tar xzvf nginx-1.9.2.tar.gz
, X$ E" S9 e9 ~7 t6 S1 x$ cd nginx-1.9.2- U. Y" s! j  c" t% \
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/4 b2 z" J/ [+ ]
$ make && make install
/ h4 E6 z! b/ a+ L& ?方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
& U. V/ r. l" R  L  _- i: O- W5 e/ ]# X" Q6 @# y, n! I/ ]: I2 ^1 b& @
0 s, q8 M; `4 z
编译LibModSecurity(modsecurity-3.0)
+ d' f  w2 H; w3 n  G2 d$ cd /root3 ?* ^; I6 n( c' T# f* r
$ git clone https://github.com/SpiderLabs/ModSecurity+ N/ Q9 z$ h9 v. Y) Y' f$ W
$ cd ModSecurity# k( r. R+ s, \% i0 T' y* T
$ git checkout -b v3/master origin/v3/master
! O1 |- f  r4 |6 ~- P( h( B2 u" ]$ sh build.sh
0 }0 n! m) ]) g" e, S% l$ git submodule init
2 s% \) _3 W  l& W$ git submodule update
: p* G( z# O% N3 ?% ]- j$ ./configure& ?5 M8 X6 ^" R7 i" b2 x
$ make
5 j/ h; r7 j# u8 F8 ?$ make install
/ `- I8 u8 Q* @5 q) M7 R0 _LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。  s# a2 \0 Z: r$ F# ~$ V) K) y
, l' p5 e: `6 D0 }7 w

& Q2 w; z6 y/ P: v2 d7 ]( S$ ls /usr/local/modsecurity/lib
$ _  J4 ]0 X( l& r% n2 glibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
% Z  v9 k, l: {* k  `5 R编译安装Nginx并添加ModSecurity-Nginx Connector模块8 o! H7 @$ `8 h5 P
使用ModSecurity-Nginx模块来连接LibModSecurity8 U8 r% u. @2 ^* l, l
4 t' ?4 w- x* h6 B* _1 n: r$ j

1 a: b, o9 r8 k# X4 X& `0 d/ J$ cd /root7 h( K8 Y$ ]6 T2 I6 j0 I
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
4 ~5 v, d2 g$ y5 K$ tar xzvf nginx-1.9.2.tar.gz
* w; D1 J4 G; ^# K2 I$ K! k6 w$ cd nginx-1.9.2
2 I- }- V7 R, R7 J2 U- m$ ./configure --add-module=/root/modsecurity-nginx
4 U( V  b- v( `7 K( `/ a6 \" Q$ make9 ^4 u4 P. E  B* v: Y2 @
$ make && make install- O  k4 l4 `! f6 H) ?
添加OWASP规则; b4 E7 v% u9 F! r& S5 j& g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
/ r+ f" B( x& ~, G; `/ a4 _" g' O' M$ H9 b
: v( o2 H/ p' ]3 }4 x( f7 H
下载OWASP规则并生成配置文件5 {& B" z8 J4 n5 H2 c9 F3 X9 Z0 Z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git5 R( H9 ]7 F2 S+ [
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/8 A; C9 W4 \8 a; s
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  `( y) V& G: {# l  ~# C5 u$ cp crs-setup.conf.example  crs-setup.conf
$ [% f0 I7 k( G! j1 M5 }" Y配置OWASP规则
* n0 T) [4 e6 Y1 G1 M7 u( U; y编辑crs-setup.conf文件
& ]6 m6 x# [0 t* E( G, n$ M0 P# C6 g) a5 H8 B
4 Z& y. M3 H% _, `7 }& z
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf. \! M4 _. Y: `: |; K3 F0 K
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
; Q2 P8 a2 u! J; y# |: F, f$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
* q+ c, M  S' k. K2 j* |9 k$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf/ V1 {  X7 B- U" X; m
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- E% U( o2 V7 U; ~) z$ z5 c: r, q
) X7 K/ [4 H8 Q& r# A; M; d
" |; b0 I4 z# p: v启用ModSecurity模块和CRS规则
& A) r3 D3 i. Z复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 T% a% O5 N: H8 `2 i
5 _  S7 H& B: e+ \. O. {
; L2 s! S. a( S: [
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
# z" j$ B7 `9 J: @/ R: O% G" T
  f* S# {' R4 E! i1 f5 h6 T( I, {5 g% M( V! ^( z- p- q
$ cd /root/modsecurity-2.9.1/% u2 V% z" [) b. j, V
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  8 [6 W& o$ ?6 \
$ cp unicode.mapping  /usr/local/nginx/conf/8 x4 w( A1 {& R7 P- Z6 i
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。& _  Q* t# z% k- o8 g0 S2 q5 ~0 f

: s: H5 e9 J# t2 ]- U6 s! }% E
2 |/ r/ X, N* X# @, V$ v' q$ vim /usr/local/nginx/conf/modsecurity.conf
$ y8 o! n: v9 s& @. _SecRuleEngine On/ G/ a, |% x* [* e1 }7 a9 E
ModSecurity中几个常用配置说明:7 E, }6 }7 g2 u, q' |
$ c" X* l) O0 B8 _$ m) A

% ], V1 {% |. @" e1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。9 z. g0 h; h4 F# m
0 E% ?* Y7 b# E

2 a. U# D8 g2 O# ], _, w2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。! H+ R" |7 p; i1 p. s

" q. u5 f% q2 C! h' ?/ b' o+ F7 X7 y7 `# @3 Y2 T7 X
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
! R. W$ z$ s- \% p* i0 V/ l) _0 _) X9 p0 E
, ?; f& x4 j" p& I+ [. U2 U
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
9 d6 O# t# r3 T; U7 `1 }* h) [! E6 n) h# S7 ]! g3 M  C# }' |
; f1 N0 ^& M  m: E/ K  I
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
, K. A- f+ I+ |  V" S2 z% ~* }2 H3 T: {

4 K  M7 Z% P1 P; c0 ~3.x版本CRS' U' ^, T! r# t: J! {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- P$ f! a8 i9 ]. s# 生成例外排除请求的配置文件+ E# ^! d% e9 o  ^7 _& v
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
& C: }8 _  B! F$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
* S1 ~0 k: b; C0 d9 K' X2 s$ cp rules/*.data /usr/local/nginx/conf
5 O  i8 \8 h" {7 s: P2 x" E  G1 l为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。9 B' ~) ?* F5 j0 c6 P
' z/ w2 z- ~5 A

! D, \8 x$ _. ^/ o$ vim /usr/local/nginx/conf/modsec_includes.conf: F1 f7 B: e, K6 b% P: ?+ y  q

( X# W0 F* Y# d! ^% z% Q0 w
[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

- q1 b! b5 L# {# ]6 E% }+ [8 V9 c
: P" f, b0 y, p1 g注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。7 ^( j) Y! c) S" ]) a
! i% o/ t& l2 e4 D# r7 i

9 A) \3 \0 ~& Z& X( e$ q2 n配置Nginx支持Modsecurity$ P' t0 D( o. ]6 |8 C; @
启用Modsecurity( c5 j% z  z6 D" ?+ X' S* @
使用静态模块加载的配置方法. N6 {9 u( O$ u' t
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 {, c+ H5 J( M7 Q1 `
5 E! D' V/ _( C/ y6 D6 u& L0 ^
5 S- H, s- \4 E0 q& T$ p: x" X" _' c
ModSecurityEnabled on;! ?' t& j: O) P, _* f
ModSecurityConfig modsec_includes.conf;
) f: p4 T- ^; [: H8 Q2 f修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。& D! ]$ a* P; W4 E

% J" k; D' R8 g/ F. S! I
2 A) C7 J1 u# |' F% q! \$ g$ vim /usr/local/nginx/conf/nginx.conf4 Q  Z% E# u9 ~7 m7 M8 R, [+ g

# \" c; i/ W/ |' ]3 J0 e9 }1 N$ j& Y* V5 |6 d9 n8 s; [
server {% `! R7 i& \- z: g( W* }, [* l) @
  listen       80;
: L: ^- r) _9 {! }1 S- J  server_name  example.com;
# o4 S! s/ p0 K7 ^* G9 V% |+ J+ J2 [* `. g# \* }

0 R9 m: a' S, G/ q% _3 T  location / {
3 c' @' R! c* Q6 P; M. X    ModSecurityEnabled on;
2 J+ }2 ]' A0 ]3 q7 Q    ModSecurityConfig modsec_includes.conf;( g2 g! T2 h1 @3 j' f) X$ F$ j
    root   html;& p7 B( p, y) D2 a: X/ _% H
    index  index.html index.htm;& c+ E( {! ^- A! k
  }
( z2 q: k# o# R% Z4 H! m+ m}
& H: q! O, j( F# N' x) D4 E) p0 W3 W使用动态模块加载的配置方法# a0 I! g: z* ^: D) x5 ?  e$ a% I4 X7 r
在需要启用Modsecurity的主机的location下面加入下面两行即可:" d$ o4 s. V2 D0 P9 B
5 f$ d* U' e# W3 t: L' t. w3 e

: w4 m% ?: v; o% zmodsecurity on;
& Y7 K5 U  e! Z- h: ~7 E. `modsecurity_rules_file modsec_includes.conf;
) h9 j; P+ C$ Q2 z( n修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。  p% u& I( i2 V& w. z% h) B
2 D5 _+ E& q: j4 \& _
3 Z/ X$ U$ S$ `8 J+ |
$ vim /usr/local/nginx/conf/nginx.conf
" h+ C$ r/ l' N+ d  J
  }" I1 X8 q7 U- i$ n6 ~' B4 U2 V, d; {/ v3 r2 H
server {9 L5 }4 j  e& O+ S7 ?- w
  listen  80;
* I, _1 t, Q* O2 ~) v! i* u7 w! S  server_name localhost mike.hi-linux.com;
+ \) a: I; G0 D! w  J0 T; t  access_log /var/log/nginx/yourdomain.log;
( n6 n" ]* w. J5 O0 O0 `: W$ F7 H- E# V+ H( G, G! S( |. H/ R

& t7 q/ N  |4 L# k  location / {$ L9 q6 U$ @8 b1 T+ r. Q1 @" w
0 B. V& h6 R1 ?2 h' N

  E* H! j- [2 O/ B% `! y4 ]3 [  modsecurity on;* p6 u6 N* _8 i# ~
  modsecurity_rules_file modsec_includes.conf;
  a) O: H% c4 L  root   html;1 d& n$ G# @. @- f/ [" W; w
  index  index.html index.htm;
: U" V+ R% d; M& Y8 m1 u}
3 @# C" y1 M: [8 t0 p% m" d) C}
; D8 v1 e3 w4 K/ \& _9 I: m验证Nginx配置文件; f& ?& d9 A# E# ^2 H& p
$ /usr/local/nginx/sbin/nginx -t
- l- V5 I) p% hnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ O6 P9 o& o  Onginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful; |% E: U" u) u* g
启动Nginx' V: S( ~* Z+ M" S1 S
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf, ^3 |8 u0 O; o& L
4 T# N8 p: K5 `6 N$ U' U: g7 a

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

# f$ h. a, `# x  X5 x

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-9 21:00 , Processed in 0.087907 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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