找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9914|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 e$ I# n, A% m' x, T+ b9 k. w! v4 U! V

! h% n5 B7 E! j( \在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
: Z5 A. A6 R9 q7 K
2 c! \, n+ F& n3 N1 Q* F; n: |8 `% }
什么是ModSecurity4 g: J6 g. _: I7 n, L
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 `% Y+ J' Y* y! u  k3 Z) w" h
- r' M- X0 n2 g# Y# t8 i
3 e  S& e4 a% `; r' I
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
3 u) g% p& l2 k% ^4 o$ q* n  x8 p' B6 c4 q
% K! Z% l5 p7 ~/ C7 F
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。7 g8 ?$ O( y! f- k

0 a) K% d. K: N! h2 _2 B9 v% O
, \- z5 A- e7 D0 O4 G& e: [官网: https://www.modsecurity.org/
3 y- l, p. o( e# [9 X! k. L" N0 m* d" Z4 {
7 |% W6 T0 k) {+ u  s- S( v6 h
什么是OWASP CRS9 l1 ?9 b3 X: R
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
! A- k4 Z6 b8 l# w
& o% p/ `0 I1 v; {- l& {3 e/ p: N' o4 ^6 T' k% _
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。9 c$ ~# S- u/ h) N5 f% h( g8 d4 L

- j/ a2 ]9 l& w" ^8 b" T$ F; ]9 K7 X
HTTP Protection(HTTP防御)
# z8 T. X7 d+ HHTTP协议和本地定义使用的detectsviolations策略。
! Z& w! ?7 d' P9 [! x2 W% S: G9 u9 ]/ }3 `
- g; R8 l! e$ I7 R% P# v
Real-time Blacklist Lookups(实时黑名单查询)2 S7 {5 \6 }7 O3 ~: H7 l( K0 d; w
利用第三方IP名单。3 p+ k6 r) ?+ }& @1 H) v" j5 `7 {
8 ^  q4 z) y( q( o- ~

- Q! `5 g7 Q( E9 D: v0 @3 r  s& _$ H- }HTTP Denial of Service Protections(HTTP的拒绝服务保护); C* J1 q# w' _0 A$ J: y  C& d& M
防御HTTP的洪水攻击和HTTP Dos攻击。
& w$ v7 {6 H6 j+ a/ g' n5 X4 t5 v1 `/ [* ]
2 Y3 b: f+ m7 k+ N. C) ~) j, `5 h0 s6 w
Common Web Attacks Protection(常见的Web攻击防护)
& ~$ @3 z- ^8 f9 m& j/ q检测常见的Web应用程序的安全攻击。% c$ l/ n* }5 V3 {+ u7 J. W) u

5 d' h) \( ], P- }9 C. [# u7 S7 N; x9 i
Automation Detection(自动化检测)
* l9 e) z+ p$ z' ^  i检测机器人,爬虫,扫描仪和其他表面恶意活动。
% N+ Y, L) O+ K, \
* U% }& X+ ~0 {/ Z+ a  I2 K4 d
. G2 P4 A* ^- B! f' b- EIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
0 p7 T% s2 \7 V- }0 w检测通过Web应用程序上传的恶意文件。
  ^2 C5 ?) V4 C5 Z" V" L4 G5 ~5 m7 t: L" _4 e; V! }
- |- `8 W; W# \3 A& G0 y# |  k
Tracking Sensitive Data(跟踪敏感数据)
9 O4 u- {7 X$ U* S信用卡通道的使用,并阻止泄漏。9 P, K* I: b9 N9 v( O7 b
4 K' u; C" t& R7 q1 i2 T' s

5 i) a+ j6 P  U. h( U; pTrojan Protection(木马防护)
( J, _) E" w" h9 }7 Y/ O9 f" |- k检测访问木马。
+ j' J/ O3 I9 H2 [1 W" |1 Q
/ j& V' X0 ~4 p1 k' g+ a# D+ u/ G
Identification of Application Defects(应用程序缺陷的鉴定)
+ V, k: S, H: Y9 X+ b9 O# u/ E检测应用程序的错误配置警报。+ C% D0 j- Y* m2 o- Y7 J- w

4 P  ]' {* k0 ~1 c
+ U' E+ Y- u; H+ E% H% WError Detection and Hiding(错误检测和隐藏)3 ]- o6 H6 _7 B: b6 C8 o  K
检测伪装服务器发送错误消息。
4 |; n$ R, L: e' t7 s% c# d& |' ^- D9 I

6 ~$ V6 Z6 ]- P  U3 k# D7 {! m. q) G安装ModSecurity
6 A. }6 r/ j* `& R' \1 G软件基础环境准备
$ y% z. E5 J$ O& `0 ], R& I下载对应软件包5 h4 M7 i2 n) `! g7 c
$ cd /root5 N9 }& w% d( ?8 L$ U' y
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
7 l8 O# y& S: Q7 w3 K4 q9 _$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
' e  x0 a# I2 w安装Nginx和ModSecurity依赖包
3 O" a+ L) @* S0 N) R& rCentos/RHEL, r5 U/ m9 _0 W. v3 b. w+ l

/ m2 e) D* v6 C! ^- D9 t: M
6 o: D6 F" a) {+ l' `2 r$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% K$ a" ^& u9 D/ \5 c( P+ x/ r
Ubuntu/Debian
: ]6 _% L! ~! Y' j3 V! T7 b+ G9 |( O# `; ~' D/ [  a, }
- s. x5 k9 ~7 W1 o1 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
7 {! {+ ~# S" ?0 o2 b1 `+ m/ @8 @编译安装ModSecurity9 C& j& y) l9 h! g
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
1 ~# u: I) z. }
8 \8 m# C7 r. C* ]9 x# u& L, E
$ C  b9 M7 y" M( S: ~方法一:编译为Nginx静态模块  J1 \7 a4 e5 S
! k# b8 E2 }2 @, s% R& O
5 g3 ?$ c. h$ _* @. r/ c
编译为独立模块(modsecurity-2.9.1)# {- j7 w, w. ]
$ tar xzvf modsecurity-2.9.1.tar.gz
5 O6 v6 X0 v' z, ^2 M- j* |$ cd modsecurity-2.9.1/( U+ J+ N; K+ k9 U( `; G
$ ./autogen.sh
# U3 c: b; z8 N; \# H0 j/ G) ~$ ./configure --enable-standalone-module --disable-mlogc( w& f! W9 N" q
$ make- L; o0 T7 c) i8 }! T" e
编译安装Nginx并添加ModSecurity模块$ k' d) \3 {1 n: ?& |# b( G
$ tar xzvf nginx-1.9.2.tar.gz
# ^# ]5 x# c0 E; K% o& p$ cd nginx-1.9.2- {' c4 z6 i/ O: B7 Y; R! b
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
. G, {7 S) b" Q- H' q7 j$ make && make install
, @, ~! h' P" m  P! P方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, J- g) o6 t+ v% T. h6 F
2 x' W& J, t/ |5 @$ D1 s' @

6 r" J( W# j# K; X' e* p编译LibModSecurity(modsecurity-3.0)2 ]4 j9 q- |8 ], B
$ cd /root7 Y* S: U) ~! g( l; u
$ git clone https://github.com/SpiderLabs/ModSecurity
- Y0 b" x, i7 ?; j7 L% o$ cd ModSecurity
( P  I! ]; i5 V; a; ?8 i/ _8 D$ git checkout -b v3/master origin/v3/master. R/ d6 z0 Z' t9 _9 g" Y5 t
$ sh build.sh
7 Q# K$ Y0 S5 I: N& W( h$ git submodule init5 i6 k8 U1 D9 Y" z+ s# a3 `
$ git submodule update
9 f  m5 _1 D6 f9 h$ ./configure
3 y& C* }4 [3 r* O$ make
4 T7 s* u- c4 Y( j' ~5 [1 D( |$ make install' e" ~6 H5 V1 A
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
2 i5 @  N$ G- ~! l% G6 \: C% H/ }* T# \2 ?- J0 U. {

  g; r! Q" X0 [$ ls /usr/local/modsecurity/lib  X- c! d5 v7 [) L
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
, z2 N: j! }$ e: e5 w编译安装Nginx并添加ModSecurity-Nginx Connector模块, c5 w( f% Z8 u% \. I
使用ModSecurity-Nginx模块来连接LibModSecurity0 i- P2 z! f- p, B1 @# `$ `0 E) Q/ A# S
6 \( G; K2 F; C
# j# D, h6 _. M. b9 B+ K+ n8 s" C
$ cd /root
% j. h( {. z# C. f7 Q4 w$ F$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
- N; t( ^4 v% x; ~7 m$ tar xzvf nginx-1.9.2.tar.gz
* l  h. u2 v; M0 Y1 g$ cd nginx-1.9.2, a4 B; Q* q2 S  w0 ?% q
$ ./configure --add-module=/root/modsecurity-nginx( f8 r' z$ D0 ^$ V' {! X
$ make
# O8 B  x% E; x9 r; T$ make && make install
# }. u% t4 u$ e" s添加OWASP规则
+ o! a% i/ N0 \) W* JModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
) [5 ?1 Q3 o7 Z6 C8 A2 N
7 F4 k6 f% s1 o6 Q% w& W. b+ p. c0 _3 ?$ A
下载OWASP规则并生成配置文件' \' I( `' m; M
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
% O4 U; \7 m2 E# s1 J# Z9 I! W' ^$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
* ]4 J, E! m+ g9 q$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
. c3 n* I3 M+ s$ cp crs-setup.conf.example  crs-setup.conf) t" }8 b6 V$ L& A% J0 T: A
配置OWASP规则, k+ u, P! q, M- K; M
编辑crs-setup.conf文件4 r& O; C6 i' H% e, n. @
5 y6 e0 m5 b. x) f2 H

7 g+ x* a1 x" Y" D2 V$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* N& _0 k9 g* a5 H. t$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf: c# Y) k5 u1 q% e- [
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf8 l, p& S% h9 S/ c/ k
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf( E5 V0 E, O1 @6 f( @/ n- V7 u
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- d: q  \3 s$ U0 v4 `
, j# t% _1 G8 l$ E
( h& Y* b7 E2 B! G) G启用ModSecurity模块和CRS规则
* r  s( \" I  x4 j- z, N) Q( A复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, \$ J# p- G( z: c, s/ O' Z
1 n* L9 A0 S# ?9 f  _8 n
6 q$ Z: S1 |+ }3 ^
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。$ K5 q3 F, P7 f( q
2 n' L0 ?3 g# q" a) c' a* z$ X

2 {& c' X3 E, Q5 ~$ cd /root/modsecurity-2.9.1/
9 F9 [$ O  }/ W+ j$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  7 D  Y% J+ x3 a) D/ A; T% Y
$ cp unicode.mapping  /usr/local/nginx/conf/
' C: k( R3 l7 q" V. z7 N* f4 C8 X将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
1 i: r( M4 W% l( |, w8 i
" `/ @$ F4 p& w) V$ `9 C
2 x8 S. A7 V" }, D' x$ vim /usr/local/nginx/conf/modsecurity.conf3 [0 L' C, }. {2 F' L
SecRuleEngine On
' i& c" I  o4 O. E2 T* W0 ^' lModSecurity中几个常用配置说明:
! c. G  i9 W% ?- L+ K1 `( ?( Y* o; j
/ l+ I+ s7 o( n1 m, c1 o" [6 W- \* n0 E
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。3 a6 ^5 p; C% a  }7 a( |$ m

# ]! T! [( @# l. U7 d2 ]. C9 c- v& c7 x% q9 F
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
- O& R1 }3 O- M; A7 g% s
$ s$ [/ u! L/ [6 R8 Z/ Y! Z% f( ?7 `2 z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。8 q" a( _* H1 ~$ A) U* Y! W0 |" s
' `6 ]$ L" F! P0 a8 G3 n2 N; y

9 Y; {, a5 A' t6 i! |4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。0 ^7 u  b  S. R7 Y$ `) z1 n1 I

; L/ F' y5 g# O1 j& G4 r+ }9 A9 t9 W. u
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。5 y5 \. [4 s& D5 y% H9 E
9 b3 p$ r/ f* g$ y! Z
  K9 y6 S0 W9 D0 o
3.x版本CRS
1 }; x  O' o$ \2 V" V% i( z# h$ ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs$ B. W/ d; o( v2 O  V% @" T% X: T
# 生成例外排除请求的配置文件
- h% {" `/ r: W% a* y  K- b6 y* M$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) Y& @3 ?6 B& j$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
7 v  z9 U- A3 m2 P6 L$ cp rules/*.data /usr/local/nginx/conf/ V* o/ j  a) |
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
" h  m( D1 K2 l- x; {# W  u- n! p: z& E) b6 w

; @# d+ L- c4 J( V- \' N$ vim /usr/local/nginx/conf/modsec_includes.conf
4 B# x" y2 ^9 j$ s* o: a" F# M! S( y
5 W. L" W8 ]; ?8 F/ r* v: r
[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
" c9 D1 a  F$ J* w& _' {! a

+ h! B% b8 a4 W$ J注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
. H* E' V1 T, c. c
( u- l7 W! Y# C1 u5 c* D% k' ]! y0 E
配置Nginx支持Modsecurity4 d8 {  r  f/ u" R1 m3 a
启用Modsecurity
' K6 M4 e' S& O7 U8 G使用静态模块加载的配置方法
1 M; R8 w' E, P: W# }在需要启用Modsecurity的主机的location下面加入下面两行即可:
7 H# j6 q; g9 m" g5 B: `
3 J" k  s7 F$ c! R* I8 {8 n! c; u$ c% H
ModSecurityEnabled on;
: F+ ?* w5 R0 |# \. E: D' bModSecurityConfig modsec_includes.conf;: ^9 V: P0 e) Z' N/ W! F% K/ O3 D
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。. H8 _1 x, A0 t6 h& j6 _0 N: u

. Q* K6 G7 p( [  y5 w9 {$ \0 b" a8 _. J6 ?1 n( o* z9 {& ]
$ vim /usr/local/nginx/conf/nginx.conf9 k2 x! t+ ^& E& l- B/ J. z0 W; R+ d) [
4 N. _( _# d+ Q, t% B5 S; [

, ]  ?" J2 D: ^" E3 |server {
: Q. x1 x- n  |' G+ w  listen       80;
& y# s5 u$ X; K! N2 ^+ [  server_name  example.com;
+ Z, t3 s' Q9 H2 D0 P; ^! w8 n* J8 ~+ M- j+ I7 U3 g

; j2 _) z. `4 U( D; e9 d, p" V9 {9 }  location / {& A7 w7 C+ {2 x  \
    ModSecurityEnabled on;
0 ~' E1 x1 K" T+ ~    ModSecurityConfig modsec_includes.conf;- }& ]1 M. M' c; y+ f$ c
    root   html;
9 ?. \; L3 v3 p8 H- X% S% B    index  index.html index.htm;
) \* K$ Y6 K, l0 k9 K* U- n  }
; X! }. L. o- ?$ w}
. e# a6 ~6 {8 c使用动态模块加载的配置方法4 P+ J0 ]2 q! d2 c# d6 T3 l7 a
在需要启用Modsecurity的主机的location下面加入下面两行即可:+ r  G9 y( ]1 k& ?- s7 ]! S$ Z0 g

+ d/ m6 q/ T& O6 x7 x6 i! i, p3 E1 Q: j  n' U
modsecurity on;
& |( @, t3 e! f. m0 G- J# Ymodsecurity_rules_file modsec_includes.conf;# m; p7 ^$ f! g& b
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。1 c' ]6 f& {' c6 L3 l+ F0 Z4 _& A

8 w, R; r- p5 O3 _, ~9 G1 l+ `. ~4 f% D7 g
$ vim /usr/local/nginx/conf/nginx.conf
& f' J  Y0 L9 h% W- W, X9 _/ Y# S+ y
. J7 c- b4 q5 y, V
' A* s6 n6 U3 N5 vserver {
! O5 L& c8 x' j' p  listen  80;$ C: {/ |7 ~- j; A' E% r6 d
  server_name localhost mike.hi-linux.com;
6 Q8 u- C9 ?* Q+ j  access_log /var/log/nginx/yourdomain.log;6 S; r4 y( q, a- }9 m+ n1 \

3 Y& w1 f. Q4 g; v7 t; O
- r8 I9 I6 H7 a* U0 d: D* F  location / {0 P& i: a. F; a; ]
1 c: @: m6 p: u+ ~. D
. S5 R) P3 M' U" b( G4 l- B( r& x
  modsecurity on;
- [5 ?9 Q/ k+ l' N# \  modsecurity_rules_file modsec_includes.conf;
$ o% A; P* L# i6 p! U  root   html;  l# T0 Y8 y9 _! A
  index  index.html index.htm;
& G& [7 S  I3 A: E: O8 e}
, d, j# V' `+ V9 X; G6 @/ `  [2 k" R}
1 Z( h0 ]8 L# Z  {5 E+ |* f验证Nginx配置文件$ G/ b2 I( M6 h1 w& T
$ /usr/local/nginx/sbin/nginx -t
" ^3 k: v! y# d; t. pnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2 I8 P5 A/ f: s7 m4 ^4 N, ~. Znginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful9 P; d3 Z  D, y5 h8 }- t
启动Nginx
- C. I1 [! T* c5 P% D$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 ]# h% n* W9 G
9 D* Q  O$ t/ e, E' I# |* L

测试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 B3 x: V$ z  w, P' f5 }

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-10 14:36 , Processed in 0.037447 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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