找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9970|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
7 @+ v. m  K8 u, [. B
, O$ P0 I& U" b0 }) F; |6 B( S+ y* X% e" Z3 Z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。7 L# P4 c7 d# g7 R, z

" e6 K& [( U& ^' q3 f. A
1 C$ `3 D5 X. i5 l' Y$ G+ G什么是ModSecurity, K& o  O! V6 C8 g- ^
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。" }. x7 P) j, s0 U
' p$ {% O: o3 l0 @! z8 u5 W

; J5 o4 \9 K3 W, [! zModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
3 M. {* R- B& z. e/ X
' b1 [+ F+ H8 c/ Z  \/ ^: D( m$ E3 D* t
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
  Q5 a1 b$ y% S( [4 |" @) Q3 p# |

' [- z6 b( i& A5 [% H官网: https://www.modsecurity.org/$ g, y* ^  o9 h% ~3 W4 L! v/ ~

( d+ n8 C6 k1 Q7 G: A& S! e0 o) a& [  S3 ?
什么是OWASP CRS6 L) _( y' u8 w; P2 `4 i2 |7 U/ f  h/ h
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。, A' R* V! O  x5 w, P
7 Z' ~8 f$ M1 n; }, z" }
; j, j4 N, ?1 h- b0 v# W; W
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
6 }; {8 V1 d1 s$ Y& [3 E+ M
8 T2 N$ U! g! [+ H) }7 v- S% r& s, ]+ [6 T! Q' n) v
HTTP Protection(HTTP防御)
( b  U* f. H* h. V6 w6 @, b' D% d) KHTTP协议和本地定义使用的detectsviolations策略。
4 l" T- B) f- ^* R9 u, |
& u' j+ o  E! {9 l$ c* t2 d1 |! T, ?/ W2 d- `5 l* q% J
Real-time Blacklist Lookups(实时黑名单查询)
3 z& z7 S4 K) E1 H利用第三方IP名单。! X* o9 {% \  h- }! i

* g9 i! t+ T+ A2 ^% M7 X
' A5 B+ i/ q4 b" k; _HTTP Denial of Service Protections(HTTP的拒绝服务保护)
" W# |& k; O' L" w: V0 u防御HTTP的洪水攻击和HTTP Dos攻击。
1 n/ Y: {9 a9 R6 S- f5 b4 _# s% h( B) `
1 C9 ^; r. S. n/ `- V/ p' _  Q& w  ]7 E' R& B0 s# }1 B
Common Web Attacks Protection(常见的Web攻击防护)
1 K  ?" o% g) i. X检测常见的Web应用程序的安全攻击。3 z, A( z6 ^7 @2 q3 \7 O
2 E, |* E0 Y2 v0 u, i

" d9 B" x' h& `- t$ u6 X) g1 Q) w0 ^Automation Detection(自动化检测)0 r5 _5 {* ~; v/ i' p
检测机器人,爬虫,扫描仪和其他表面恶意活动。- _& k5 I4 D1 `* B# O
8 H+ {! l! {, @! V. I: M) v* w7 h

7 ^) |1 f7 q! h% [4 SIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
5 x: z7 d3 l7 t( w6 _  O- K) @检测通过Web应用程序上传的恶意文件。
8 m! s$ a( F4 [" V4 ~' g, X& h+ p, p/ I% D. \6 x- c

( `' e; m9 t% T( B" E+ B2 u$ p8 D, ]Tracking Sensitive Data(跟踪敏感数据)
# ?5 G% N5 ~# Q# P( M9 n9 Z8 ]信用卡通道的使用,并阻止泄漏。! P0 Y+ t" A4 D2 E, l; w! [

1 f, E; C# r# ~1 L; t: z( }0 y6 q
7 W! m4 V1 w2 M8 _9 z: w! gTrojan Protection(木马防护)! x. _) I/ G' m7 E! x0 U% }
检测访问木马。
; h# N; [6 m9 H; b
$ @$ b, [. k: d4 A9 ]# z2 r0 t, ~
Identification of Application Defects(应用程序缺陷的鉴定)
9 X' @) A- N/ @9 y; J* B2 X& V检测应用程序的错误配置警报。
/ L  y# B3 h- F( x
% F8 K5 d* l; X- M
! ^5 p0 }3 D  V2 M* c- ~Error Detection and Hiding(错误检测和隐藏)
+ q# K2 \- _  }0 ^检测伪装服务器发送错误消息。+ t( I( C9 Q4 A

- h, z1 Y3 r/ V! a0 y4 I7 P: h* v! y% n1 \+ E# w" [. z" n+ @
安装ModSecurity
  I* o" C; F. Q3 ^* H: n软件基础环境准备# n! X: J. t% _
下载对应软件包% C+ s9 A6 C- q+ j- _
$ cd /root
' E( g; Z) O, `' V7 p$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'6 U! S) x7 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; Y! ]$ ]1 k* V4 ]% y
安装Nginx和ModSecurity依赖包
# P+ U3 r0 J( x! M7 b6 VCentos/RHEL
* O" w) ?5 N9 \& v# X. [% r' V8 z$ v' O$ ~4 U3 x
& k# o# W9 Q' k: J1 G4 `
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
  d, r" @  _" ~7 q  F1 [1 O/ BUbuntu/Debian
/ T/ O, s5 y+ I$ C0 `/ J
# c8 ~* z& l& F7 X$ [, \1 U0 A. Z% ?5 S! N( d
$ 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
2 j8 `! V( ~: t编译安装ModSecurity2 e/ \6 q3 m! y+ s; i
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
8 z0 z# E$ |; x2 b$ K/ j1 F
  H# P. }5 i4 g: b0 U$ V" R, [$ T: a% @# T8 `+ v& D
方法一:编译为Nginx静态模块$ M% R. }% c9 ~

% l& m- M: M$ s% i% l+ ]$ d# k' j; I" b" f( y/ l+ w8 x) o+ F4 M! w
编译为独立模块(modsecurity-2.9.1)
# K( x1 p( L) o& A' X% }  x$ tar xzvf modsecurity-2.9.1.tar.gz0 F+ h% w4 L" O% T1 e7 O
$ cd modsecurity-2.9.1/
& V' S( W2 i' y. g8 S$ ./autogen.sh* f) \# Q& h: E) h7 z3 q
$ ./configure --enable-standalone-module --disable-mlogc1 S9 i. |# s9 u. q
$ make
% `4 ], x7 J( d3 O* }编译安装Nginx并添加ModSecurity模块2 A/ H9 r' U4 {; d, V/ V4 |- {
$ tar xzvf nginx-1.9.2.tar.gz" L, Y$ K# m3 a4 @/ \: b. u5 _; q
$ cd nginx-1.9.2. e; T% [$ }& Z: {$ {) J2 |2 X$ A- c
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
# v( V/ ^( d* R# `$ make && make install" A3 b& |& `1 C! {- G. ^5 e$ b
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- f, H2 [5 }% @: D( E7 B- B
5 f5 b8 I9 `: N2 X6 x+ Q7 D9 n. C1 J
7 p4 K; C3 U' h+ p( J编译LibModSecurity(modsecurity-3.0): {4 R* r1 U8 n$ l
$ cd /root
4 @. M# K- j9 l: ]3 r$ git clone https://github.com/SpiderLabs/ModSecurity- D9 A" ~9 ~7 }0 B! U
$ cd ModSecurity
; A0 [) `8 B" n$ u0 M! X* W$ git checkout -b v3/master origin/v3/master( W/ \6 ]1 U  b$ H' \
$ sh build.sh( G8 }4 Y% o  }5 {7 \
$ git submodule init5 {% w/ K; [9 |8 J0 ]/ q
$ git submodule update+ H( F1 D3 y$ u; r
$ ./configure
3 a/ V( ^; W9 R$ make
" ^8 A% X" g. `7 O$ make install
. z' x" c- s$ j$ L% X8 U, D! nLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
" K4 Z' C4 O4 q5 I4 b8 Z
8 H& s" h% K: t. _5 E! ^8 F$ T% s) n( z) ?
$ ls /usr/local/modsecurity/lib
  {! i. @+ g- B/ K& I9 y% D- Blibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.06 K" o3 ]; \8 h/ e
编译安装Nginx并添加ModSecurity-Nginx Connector模块3 v  D8 k' E+ a2 [
使用ModSecurity-Nginx模块来连接LibModSecurity0 E, L" j; |$ U/ R
/ A* S" W0 ~' t' q

1 f& Q6 p! A  K7 z* l$ cd /root
( ~  B( A7 P$ v$ Q; b- w6 x! x$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
. j3 r5 Q: r! ?$ tar xzvf nginx-1.9.2.tar.gz
& |" m( J. L4 i$ u$ cd nginx-1.9.2
- c3 w) ^3 j  y' Y  _: }$ ./configure --add-module=/root/modsecurity-nginx
# w' x; ?8 P4 n- q: k/ N$ make& j) t6 A# _( ~1 e8 i0 e
$ make && make install4 I2 ^5 E0 [3 S" w+ T3 d
添加OWASP规则
; |% a; F  x6 x* y* AModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 u( J8 b9 |' n' m
; h2 p! e  F3 S: E% _

. A' Z! f: ?( _3 {- R% Z下载OWASP规则并生成配置文件- j# v/ Q% z5 q  @; x
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
) ~+ y$ q$ G2 b; Y1 [# C5 _4 R8 b$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
; ]9 ?" \$ a4 o+ R* B4 U$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
  H. {, M. a& \+ g$ cp crs-setup.conf.example  crs-setup.conf0 c: s% m6 Z' U1 n( P' m$ }
配置OWASP规则& u9 I  G: Q; ?" K) x4 [2 @
编辑crs-setup.conf文件
3 \. E" W! X5 v6 e5 Z! L+ i% r+ P
8 O  O' S1 v3 }$ L% e0 E8 B
* F  H% _; {0 n$ g1 {1 a$ o3 R$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf; h7 U) ^! d, f1 X
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 o4 k5 P$ U/ ?7 Y$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
6 B( |8 A! a% k, t# Z" L$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
* J* {0 f; [! \4 _默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* Y2 k1 ~2 b. c/ N
) h  F% Q. p4 U
! i5 M5 o9 E; N& _& j启用ModSecurity模块和CRS规则8 l, K5 m  @' f' R8 ^
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, k* _+ p- i3 w/ E7 _7 Q: u
: r) \* F9 g4 n  V1 n: u$ Z

3 o8 N6 _/ L( X7 z1 B; J* Wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。* a  {6 ?  l( c  C2 h- a/ N

: ~( p. e- N5 t  P' J" P  v% `* z1 `6 D  K( \* R8 t
$ cd /root/modsecurity-2.9.1/
: w5 d/ X" I, \$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
  o  }- _3 c* o/ K, c9 [" E* u, H4 V$ cp unicode.mapping  /usr/local/nginx/conf/
6 @% \; A/ v: Y* }5 p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
% z& [9 h( ]7 \7 a6 m# p! [8 t3 G$ w  r- W. q, |) z3 x# N! O

  W% f% y9 M( `+ w: J4 v6 a4 ^$ vim /usr/local/nginx/conf/modsecurity.conf
$ |! B8 k: c& H; Y: DSecRuleEngine On
) m: y" A$ u$ e; `5 L$ aModSecurity中几个常用配置说明:
& n( E! Y  K/ C, H, a: W$ i* H" K* u( }1 y$ O& z. \
: H# X5 j, K8 |, r& T, J
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. Z7 ~  D! j( I4 Y
2 Z! L" c8 h6 ~) S7 N' j5 S5 P+ ?0 }' i
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
% L4 G6 A& h: p" }/ c# m/ P5 S9 R% Y

+ N+ n- I7 d$ g# S8 O3 J2 i( u$ l. B3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。! n, b" [9 k; j& J5 L

" ~) f7 H9 S" K  n* W/ ?5 S3 d5 A/ m. E
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
: }9 ~) V* u! @6 ]" J/ `; _& v' u$ c+ E* a% i: |

/ n5 Q9 d, H/ Q+ g9 u8 N在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 k6 @$ O3 A1 J) O; j  F
9 p  F8 B* a8 S5 _" b- U
" I& H  M$ y8 W: _6 E3 m( V
3.x版本CRS, ]' Y! G' h% s" G5 V# g% `
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs  Z4 U5 q" N. ]7 j8 R! n
# 生成例外排除请求的配置文件
2 N1 J5 G( Z* E# W0 Z$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) P" `; i2 u, h  k$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 C/ O; X& w2 Q3 H
$ cp rules/*.data /usr/local/nginx/conf, [9 u: [$ G1 E7 s. M6 [9 b. s
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
6 f/ o! v( ~  x* [& g& ]0 {! K
  r3 I1 N! x2 a! C# ^, x) i! x+ S! L
( x/ {0 J6 {% f: t/ e% u" V8 f$ vim /usr/local/nginx/conf/modsec_includes.conf
, a) a0 r7 H! }& U+ W' d; C3 m9 P; }* q& E( W/ S# i5 U9 \1 v
[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
+ L0 i7 e9 q, E5 b- {7 [6 |1 U1 C

  x. e) I% o2 S5 l注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
: \# r; \" l1 {7 C- ~/ W( g
& y! [: X; a' I/ g3 }( \: V, Y
$ K6 Z9 c; O' {4 O2 m配置Nginx支持Modsecurity4 J4 v/ j+ P" {& U; d
启用Modsecurity
& D8 \1 u0 F7 L# `使用静态模块加载的配置方法
, G& |; ?: u& g" @在需要启用Modsecurity的主机的location下面加入下面两行即可:
) j* O% c* V6 l' o, e
& J3 z# E+ D( E; _$ ]! R. d% m4 @5 w5 k
ModSecurityEnabled on;9 A3 A* j! b6 y& D
ModSecurityConfig modsec_includes.conf;! E; v( _% `5 L2 |" ^
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
1 J; H2 ?4 M0 W7 t% s* Z) W; v! U% g4 c6 I& _4 e
! D4 i0 i+ Z& Z2 u# I5 b
$ vim /usr/local/nginx/conf/nginx.conf& Z; Z( @- q+ {3 R/ o% O

+ C. Y" u2 m& \- d$ l, ^, O: w5 q' U# C2 |' c/ r8 h3 K$ F
server {# \$ x4 d. D( p3 i/ d5 I% o& I
  listen       80;
8 g- a9 [) o/ k' H/ x% X4 m. e  server_name  example.com;
9 c" T' O  A4 i! M  V$ @# L8 q8 t" B4 q4 f$ z; f

; W6 |1 v  w5 b7 }! I! }9 C  location / {- Z2 a6 v) G8 `3 X
    ModSecurityEnabled on;. d- }8 e  w  M7 q
    ModSecurityConfig modsec_includes.conf;+ }9 G/ Q% _4 L6 j) x$ O8 O
    root   html;- Z' u" x* S! z, L; U. L
    index  index.html index.htm;
5 X' r4 q% Q7 e  }
7 M: \7 s$ n$ b6 L}
: V: _/ J' ^% p7 `9 z使用动态模块加载的配置方法, g9 O0 c, O' r$ ?% J. ?- L- U
在需要启用Modsecurity的主机的location下面加入下面两行即可:
' ?, m* u1 J# E' p! o! A. A
3 v) |3 S# U8 _  k0 f+ G+ d" \" A1 I& f; s0 a/ ^
modsecurity on;! z$ B& e& _" e
modsecurity_rules_file modsec_includes.conf;
; w  @4 r+ v4 h. ^" O- l  k* T/ C修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
* {, `" H( u  L1 Q' E6 f7 y# T
/ D5 y6 c$ k* E4 n
& A' u# G9 o# A# r. R4 `9 M$ vim /usr/local/nginx/conf/nginx.conf
% ?0 s) B* r9 y) M. D
6 {3 C( o  R% D$ ?- m& |9 d* ]' s
. G' ?; H1 g' d" g" ?' fserver {: c9 f+ E& e. ?
  listen  80;
8 R5 t1 z  H+ Z+ `% @/ |3 h5 Y4 |  server_name localhost mike.hi-linux.com;
& C& K2 x7 r) ?3 z9 P0 S& p' c  access_log /var/log/nginx/yourdomain.log;
; {8 k+ G) K# O4 `' U0 s0 Y: }3 m. s. z+ g1 S  v
3 ~5 p, m( s3 D- L9 ^
  location / {# Q8 A: j% {. ]! J( `/ B6 n

0 f- a) v1 i+ B7 e) B0 ]
+ u# g& f5 c8 k- i# x7 G  modsecurity on;
4 H8 B+ K1 m4 ]! {6 B  modsecurity_rules_file modsec_includes.conf;$ s2 s6 l& k$ Y$ }* ^
  root   html;
# w* p' V. V5 v5 z  index  index.html index.htm;0 g/ o3 g! \4 R) k4 t6 g) w
}# h& b9 A- z/ q' L* ?9 D9 i
}- M: i5 v) h$ O- n; Z
验证Nginx配置文件
' p7 c& [* b* N) r2 j/ v3 y! }$ /usr/local/nginx/sbin/nginx -t
" A8 A# v  _) \$ v0 cnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
$ @7 e1 c8 x7 c7 m8 G& Ynginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
6 S. ^: Q  R; J$ ^$ S1 P启动Nginx
# _/ o+ I5 q' T6 D- r$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 r& ^9 E% S( Z$ F$ `0 O5 ^6 V" U

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


: j2 q0 O, _, n; ]1 {

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-17 20:05 , Processed in 0.067553 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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