找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12652|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ k0 Z* \# m, k! J! Q
6 O8 Z  ~) @5 i% J
$ _$ U  S; f0 U6 P' E$ G1 `' h# |在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 \' ^. S7 o  o4 k1 H; i6 T; x

4 o8 q* }; @5 _
, F1 A; i* w# V什么是ModSecurity
5 z! W0 P& Q( o& zModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 y' m: @. t0 G1 _& N* g$ m
: O2 P7 ?. n  O1 b- x: h
7 i6 e$ Q- B0 o
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。* B8 v) K# T9 L6 T3 ?. }5 u2 I
" T3 u; k7 j6 e

' e# M5 I7 d/ I7 h" BModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
+ s" m$ t# ^& ~" l9 G9 f& e, l* g) F

8 X) x4 M( t5 n官网: https://www.modsecurity.org/4 a+ P9 \! E8 ?( P* K
+ L( {: n4 p2 J; K
4 ^& k1 ?0 {7 v( B1 }5 R
什么是OWASP CRS
$ v4 U% I. N  X' g8 R  C) NOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
: S, R% j8 {! S) X3 L& g
6 v  f0 d- W6 U* Z9 [! k; T1 ]/ u" J0 E6 }; l7 p4 q% C* F9 `
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。( }4 E: b: {( w. m! C

1 ?* H' f  M: P3 v& |7 _/ T  _" L2 ^, G# j1 M# @
HTTP Protection(HTTP防御)5 K  Z, H' h, C+ `$ V% d
HTTP协议和本地定义使用的detectsviolations策略。
! G  U! r+ A9 `: r0 U  L0 U4 i2 Z
! w9 W& G! U; F* L2 V& |
; _- [" ~0 Y$ E2 m# B0 ]7 UReal-time Blacklist Lookups(实时黑名单查询)! A. O3 U1 [% h; F# f
利用第三方IP名单。
( h0 R3 `! K" F- W/ ^7 G% t1 G& V" f

' i) ?. t7 D& |HTTP Denial of Service Protections(HTTP的拒绝服务保护)& H  m' X1 `, U9 }" k: a% {" }' f
防御HTTP的洪水攻击和HTTP Dos攻击。4 ^% H3 p) i# O% J# u) }  F
; R) t3 M& s, l' @1 Z: @

7 M  l0 o, C+ p, q0 B: UCommon Web Attacks Protection(常见的Web攻击防护)
$ a4 v/ Z& u' U2 Y0 \& U) @: n检测常见的Web应用程序的安全攻击。- P. O2 v, {* u
, P  U* Y3 @( d# b' v
" H' {: {/ u5 b
Automation Detection(自动化检测)
) Z! U6 d9 z& x7 V+ I; q1 P; j检测机器人,爬虫,扫描仪和其他表面恶意活动。
  a4 a) {  d* m0 e
' u5 k/ D8 a1 w* X: y3 T" n$ g
" l$ X" \) O, Z' m1 OIntegration with AV Scanning for File Uploads(文件上传防病毒扫描). O$ b1 W- L" Z+ V3 Z! u9 v7 T: a: H/ P
检测通过Web应用程序上传的恶意文件。' R: b( X" q2 l

6 A- s+ p, c+ h' C. x7 A  R' {/ f' p/ ]' H6 B- e# e
Tracking Sensitive Data(跟踪敏感数据)0 y6 p! o& @9 @1 {. r+ N
信用卡通道的使用,并阻止泄漏。3 t0 P0 c9 K! a

7 Y' o' l; y- n: W" y9 _0 H, J+ @' J# x( _8 R+ y9 P
Trojan Protection(木马防护)* V3 Q8 E: [( c  P
检测访问木马。, Y; C: P# ~" l4 q) t) G

( Z4 \& [# D+ Q" v1 p
6 q. m. O! W! B6 w: |  pIdentification of Application Defects(应用程序缺陷的鉴定)/ }( Q3 F- L1 T4 z, t# T) _" C( ~; S
检测应用程序的错误配置警报。: a$ P  {) M- m5 e  x8 h
2 r- b' J$ X) V  g, Y( \& S

0 K! K- U* T" \2 Y, Y/ `  HError Detection and Hiding(错误检测和隐藏)6 V* d7 Y6 ]( E9 T, V7 u8 d. c8 J; u
检测伪装服务器发送错误消息。
8 w) `0 o# \3 Z4 k* I1 j
" t! u. \9 w& a
: n* P5 G0 Y; m安装ModSecurity
& y) q2 w1 C4 a% c* g  N/ ^# a软件基础环境准备6 g. B$ J1 `2 @  k: _+ F* M. y0 J+ T
下载对应软件包
; B1 Z! G# }$ a$ cd /root
# h. L1 o9 {( v7 V9 k* w" Q6 X$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'. l3 D& n$ a0 o6 F! D) U
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
2 `  w2 L  H& p7 c" v; Z安装Nginx和ModSecurity依赖包
* U! `7 D$ M% {# V9 {Centos/RHEL
) h6 ^, z' `3 A0 m+ \/ \4 f8 o# A& M6 q
  u, R+ t0 {9 m9 H5 ], @$ N* ^2 O: y/ W
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel! X) n& e" L  v8 Z' l
Ubuntu/Debian
4 Y: e$ W) l9 q8 |3 c: G5 g; l) b0 n* j9 x) a3 \! c# m

/ n. e$ u' B1 K# L9 z3 U. R5 e$ 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++-dev2 s+ \9 s/ \7 n( X
编译安装ModSecurity& ^2 c# G& f9 m
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 F; k( U' A4 l! Z& E! z9 Q: ^, X# @9 t

4 }/ r9 l. z# K; q# j* y
2 A9 \/ ]& ?3 r方法一:编译为Nginx静态模块9 v/ f- A6 S; }1 ~1 u' S( a
' M  w  d4 Y0 a* {

1 a, z$ m% J1 a- N. P2 j& @) L7 P编译为独立模块(modsecurity-2.9.1)7 i) o, t- T! L) U9 z' d2 P) v
$ tar xzvf modsecurity-2.9.1.tar.gz
" v: n" z3 b* ~" ?$ cd modsecurity-2.9.1/8 l6 a- D9 S# L5 ~6 m: |
$ ./autogen.sh
. [& F) W5 Z/ z! l* L" g$ ./configure --enable-standalone-module --disable-mlogc
+ t: t' o4 L' Y$ make7 b6 F. f6 F" W$ c5 f! j
编译安装Nginx并添加ModSecurity模块9 E& ?/ [! |( N+ W: I5 a6 ?
$ tar xzvf nginx-1.9.2.tar.gz
- x% G, L1 u- ^  x" A, \; ~$ cd nginx-1.9.26 l9 p/ z7 l/ {6 o$ p3 T. D
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/$ E- d. g4 ^& T+ A' `1 v# {. F
$ make && make install" m# d( A/ |, o* F- _4 {% i
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
( b, i& j; I4 j' c. C
0 ^; T4 R1 q0 h7 c: n( q) l2 T  i7 J$ B; y
编译LibModSecurity(modsecurity-3.0)) [/ f' n4 x5 f' `' x% a2 M8 N* I
$ cd /root
& N! E) i' ?# x$ git clone https://github.com/SpiderLabs/ModSecurity" ?0 j+ v* H' B5 K: j" ]5 ?: W
$ cd ModSecurity
; n: R7 B2 k# q, y8 C$ git checkout -b v3/master origin/v3/master
1 x! q7 H4 [, U3 J3 Z$ sh build.sh7 e+ k& M, U4 t) c: ]- [
$ git submodule init
3 y7 ]8 b0 ?* r' i% N8 H$ git submodule update- |7 l! ]' t4 S9 j; C3 e; K0 k/ E0 ]' Y
$ ./configure) o8 I0 @( ]7 p5 w' N' B2 u. Y
$ make
4 X7 T# {  B7 n4 x) O( v6 |9 v' A$ make install9 R0 k8 Z" {$ G0 Y2 ~
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 Q8 x/ {& E" o6 x
" C  w- ]; W0 T1 @1 H, H' Y9 \1 O1 u+ }( l1 |7 d4 H
$ ls /usr/local/modsecurity/lib3 q7 f3 M" N; ^9 s( f
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0( b: C4 Y# m! @; Z/ }) c
编译安装Nginx并添加ModSecurity-Nginx Connector模块
: C& B3 h" |0 |7 V2 |5 i使用ModSecurity-Nginx模块来连接LibModSecurity
) H; c7 c. K% b! Q  x/ e  d- G7 u( e" j  U

/ u& h0 g" M2 {: Y$ cd /root% O- B% a" \& `' y% R7 i
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ `$ ^& }# w' h, B
$ tar xzvf nginx-1.9.2.tar.gz
- s) \6 g" P% c% Q7 t  H$ cd nginx-1.9.2+ f. J, N6 {+ x
$ ./configure --add-module=/root/modsecurity-nginx
- n8 h# X/ V5 N$ b. \$ make
0 v; n4 |9 M* ^8 y$ make && make install
  ?' q7 p/ H/ K- l$ G  w5 X- J添加OWASP规则$ h+ w% f( \- a; Q* |  ]) `/ M& E1 p
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
. U  C3 q1 w3 d4 U5 m3 C# _
7 D! \( a4 R0 X& ?* ?$ j/ s3 ?. w
4 [. o9 M1 d4 d. C下载OWASP规则并生成配置文件
- c, M) }7 ?$ d  R9 ]6 j% o$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git' w( P) s4 Q% @$ @+ ~
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/4 A$ h" H4 ^8 c, J
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs+ P) i" Q/ A9 N: R
$ cp crs-setup.conf.example  crs-setup.conf
- I5 |/ v1 V( R8 e  ]9 H3 `  |- S+ }配置OWASP规则! E  w3 x0 J. a* W5 E* o6 W5 E( g
编辑crs-setup.conf文件2 T2 m+ t6 Q- J( z% h9 x; U% F( a2 c

) F* s( Z/ f3 t& X8 r: r
# W0 O9 |0 a5 \0 w# \6 c* ^$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf5 q. Q0 O; F6 R
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 @* A* q0 _* W0 A: z; B- G$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
) j8 B# }; @8 K- X7 Q( Z$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf  ~1 W0 b- V1 [) H0 l7 g
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。6 M% K7 g3 T# w9 i1 z
' z% J7 g/ m4 J' H& N, i" W
2 E9 ~& @) N# U3 W% B1 {& b
启用ModSecurity模块和CRS规则& G6 F9 H& I: }/ _& R( _  Q
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ n. c3 L  f; c; W# Z

; x, T$ M. M" H6 h) t7 z
  i+ R$ {# s6 j+ o4 D- Imodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
# G2 O: B" A, ?
* q+ z; t: y( S# m* e, J& E
7 `: j5 b" P1 c0 s$ cd /root/modsecurity-2.9.1/. G% ^) y( s! l, U' a* g4 z
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  - u! b- g, {  k7 @! ~
$ cp unicode.mapping  /usr/local/nginx/conf/0 s0 p- N, g6 ?5 F- o1 S
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
' c: G! u. P' D) x# D: R
6 @: u4 z" D2 i7 o# c& s" A
0 g  |0 a4 X$ k+ F: `  r0 B$ vim /usr/local/nginx/conf/modsecurity.conf0 j" f8 G& t0 D5 `7 a
SecRuleEngine On
, H! {3 U+ D) k9 \1 Z, w  l# OModSecurity中几个常用配置说明:' l7 w0 a2 r/ o: M9 d

) Q6 b) q: z1 e4 J# S- ^3 F
. N& Y( W$ A3 U0 T( T7 V1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ h- y; `2 j+ {
2 i) w8 W+ g, X0 t+ f0 O+ B& e  S2 |  W9 }0 q/ N/ l
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
4 c) M0 ^" e% T
5 N! k5 X/ H% C% P2 v' ?: {; X& O2 {# v& B4 A, U
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。+ [, y1 E' }! v/ n$ ?
' W+ {0 Y5 H5 R; S. p
: ^$ b; S, ]& x- m
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
  {& C! t" [  P3 }
$ n( O# |4 [4 k  H# N
9 K; S, c; Z1 X! U在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。+ e9 g7 h. E. L" f$ J# R1 V

' F8 F: j: y. b9 W# [4 L
3 @7 w# V6 D. M" h" ]# F  l4 E3.x版本CRS; k( \! E# s2 n' e1 d3 u6 p
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 _' B9 J& F, e" b1 u( D# 生成例外排除请求的配置文件
! c& E' }, _2 E; t$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf! Q! O/ J6 T+ |. k7 o
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf5 L8 {$ s8 X& ~2 x3 @
$ cp rules/*.data /usr/local/nginx/conf# }% U+ e7 K( s; b8 m
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
+ W9 Z) I; e0 J
' @3 K4 ~0 g+ m' t
2 ]5 R- N, U2 H9 i$ vim /usr/local/nginx/conf/modsec_includes.conf
0 u: @: b  ]9 G3 g( k( h3 G" a! {
1 w- U) e" q) c  K# |
[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

  u7 T2 a& y. q6 |. q+ k3 k9 q+ `+ z- V! H. {4 o
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' p: P% [2 N' w. u' y0 b$ u$ d! x5 @8 G# f& `( w

* c$ |8 n, }2 m: a7 ^& M8 T配置Nginx支持Modsecurity6 F, ~1 P2 I# Y; r. w$ h0 m
启用Modsecurity; X& _/ v4 X  ^# r
使用静态模块加载的配置方法: D3 f; T4 P! A3 b+ `5 O6 o6 D5 W
在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 R" g; z! h- g* T- K- Z, M, |3 d& u+ V
9 R# S& ]% \' Z
ModSecurityEnabled on;, P( Y" E1 o/ f" W+ _
ModSecurityConfig modsec_includes.conf;, h- ?0 H2 `9 A: a! `# B
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。/ @: ?9 f, @7 t6 x" B, Z

* w; F& y9 ^) s0 N" @: _4 `3 \4 U2 Q5 M( y: I
$ vim /usr/local/nginx/conf/nginx.conf
. m/ K+ D# v& I/ {5 l' w4 G, f, S# O& A) F- k: w+ w! o5 T# G5 E

" o9 K, r" L0 z7 Mserver {
) A. x; q0 m' }+ A5 F  listen       80;* R# n7 U& ]; B. ^
  server_name  example.com;
6 J& q" @+ B# t$ H) v) ?: @$ b7 o: h6 j+ L

9 |) ~* }( i1 W+ W$ H  location / {' f" ]" v- x4 [( q/ [3 W
    ModSecurityEnabled on;0 B& c0 l/ [6 r3 c6 \/ J2 i1 V
    ModSecurityConfig modsec_includes.conf;" b( |$ w+ l! }4 Y6 ?! ^
    root   html;% I+ s; b  `2 S' p
    index  index.html index.htm;
$ m5 C/ H4 `& W' e. u  }1 w' a0 ^5 i: v+ q( V/ O3 G* ~% z2 }
}
9 x& J' w" @9 |) i使用动态模块加载的配置方法9 C; R3 o% }; R+ d1 M
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 Q, N+ v3 {3 S  Q* ]# k0 \% r

1 g1 l2 T( |- [2 k; z- M
# f4 X# E7 r- }7 @$ jmodsecurity on;
+ y, c  \6 |0 G/ k/ w+ v: ]- ]5 v6 Kmodsecurity_rules_file modsec_includes.conf;: F; I! h6 r/ q# q, k# O$ h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
! k. {' n, d2 Z/ v; m0 ?) [' U. j8 T3 T
. X" `0 Z' x: j
$ vim /usr/local/nginx/conf/nginx.conf
# f8 T# J! h. Y! \' P. ]' ]: t
" i. U# E; F: W5 |5 B5 ]( e: s* }- t% h7 e
server {
, ]$ r! |2 u; s  listen  80;
- [6 W8 ]9 _% q2 x) F, x+ j" i  server_name localhost mike.hi-linux.com;
- d8 w% \2 T3 D) Z. G3 L  access_log /var/log/nginx/yourdomain.log;7 I& m, T9 m9 B: Q  U2 b' G+ n

1 U/ G( L6 B& G/ e
6 K: ~( B! H, y* I& R  location / {5 ~" |; G) H. U/ k" a4 E3 \
4 c! g, E, x: k- b' e- t
. n2 C/ }. i: J  w& t, v% T
  modsecurity on;) A  x9 p3 O1 d) [! O
  modsecurity_rules_file modsec_includes.conf;
6 B- M# _; s% Q2 Y, k# Q; f$ K' Y  root   html;5 O0 H9 a5 [9 E  D
  index  index.html index.htm;
  `% H, G, p0 p- }% ?' b3 o}
  |& l$ [0 x: ~}* ?4 _/ a0 [1 }9 e; G& ?
验证Nginx配置文件
' D2 j+ M- D7 X7 j9 B8 e$ /usr/local/nginx/sbin/nginx -t* l- a& m9 I* P$ P
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 B, ]  j/ Y. r* O# g0 ?* i8 m  p
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful, A5 T" s# b9 @: d4 K
启动Nginx# t5 ?" [6 N% L9 M+ e
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf% ~- [/ k0 b. l0 u2 @
$ x+ e. B: ~. Q1 f8 e. J

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

$ Z( k, }/ w  a8 Z

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-24 16:56 , Processed in 0.076701 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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