找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10274|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
# P/ z; b! H: x( P; q, }3 o/ C# Y; Q2 _4 Y

# z2 J6 m  D. F0 C$ D在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。# \1 @5 ~# s4 W6 h
# J  T* u9 O5 N, ]+ Y
2 K- H" G' W5 @7 [
什么是ModSecurity
9 g+ |1 y4 t/ n% n6 ^" FModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
, O6 s1 R. Q7 |; W
$ P1 |; o8 A$ z$ x6 r- r3 t
- A$ ~" ^. l+ T% ^, f$ bModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。% Q1 u7 o- s0 x# r
$ w1 ~3 h8 q+ T' H- o

$ b, u% Z/ ?5 x' z4 r3 YModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 Q* @5 r! M: I1 o1 [2 v+ o( I5 a
8 o/ a: t- x: _% w1 W4 ?. v& G+ K& V/ R5 O  t! P, W1 W& L
官网: https://www.modsecurity.org/- N4 K) O8 W+ b
' G% P( P- A- D9 ^

! V2 m+ q. ]! {什么是OWASP CRS- {& ?, \) B2 @! T# d* |4 V9 e
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。/ F9 @% v0 Y$ p* c

0 p; f7 e( o* o+ c6 j- D+ \1 ?' [& d8 A0 F7 i5 m, i
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
) R" v+ r8 C7 [8 t2 v( }6 l+ l. k, V  e3 @3 z( g1 I) m

- t/ d$ O9 Z, |: VHTTP Protection(HTTP防御); T  m  p& I- u9 _+ r& i
HTTP协议和本地定义使用的detectsviolations策略。
6 M( f+ W5 c2 p' J6 ]% q) c. r4 Y3 E
( @# }2 D  E0 n; Q
Real-time Blacklist Lookups(实时黑名单查询)
% ~& _  i; Q0 _* i6 ?; D6 K利用第三方IP名单。. Q6 [8 K6 Y6 z' s; i) Z3 e' E
; |+ n; p. v, o
% ]2 |7 a3 [2 H5 Y/ V
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
5 @$ O+ S; p$ {8 h! B( _! i防御HTTP的洪水攻击和HTTP Dos攻击。
7 u' W6 e  L' N) c& O0 R% X
' K- n  k( n, Y8 ^! Y) F( Y4 m- s) e
Common Web Attacks Protection(常见的Web攻击防护), D+ \8 x9 G2 B9 R! p/ j; J+ C( r1 z
检测常见的Web应用程序的安全攻击。. n3 G2 P  L+ I  F9 s; k6 m

9 r: J3 Z$ ?+ L, X& B% \
4 F% X0 A$ {8 i: z3 g$ xAutomation Detection(自动化检测)
- V" N# G# k2 v: E; {, g) H3 o检测机器人,爬虫,扫描仪和其他表面恶意活动。1 y& @) n/ [! E$ q" j+ F
& {# r9 G# T) u+ p" N; C. k9 Q

3 q6 h- K" ?; Z, s! E7 U$ F/ f- nIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
' Z( P0 i: l( h# V* x检测通过Web应用程序上传的恶意文件。
0 R: L8 G; w, E& p- A  n. p' |( }# y+ {! @1 p$ S& k( T

' S- @0 T2 e" ]3 d* o" a- tTracking Sensitive Data(跟踪敏感数据); \" L1 w2 ~2 u& Q$ M, Y
信用卡通道的使用,并阻止泄漏。
* Q1 z' u8 U6 ^& E  v2 d* q
) Y' u$ R7 f( S  V
2 \1 s* Q$ l, M  kTrojan Protection(木马防护)
* r  t+ U, R: b+ {检测访问木马。
9 f, k! ?5 E' p8 u
# W# [2 q* Q) C+ w8 r% N1 l0 O: |
- C/ o0 \5 ^  \, N* ]" }Identification of Application Defects(应用程序缺陷的鉴定)
) y' y0 Q( c  Q1 e, s检测应用程序的错误配置警报。( Y4 _* m! M. v" c& k1 A

- O+ T5 g3 N9 d3 N
6 ^8 c+ N3 z  @Error Detection and Hiding(错误检测和隐藏)# i4 ?1 E, O5 r% J. g1 P) K! M
检测伪装服务器发送错误消息。) b; e) h3 F) l# m: R: f$ m% s# k
; i! G2 d0 H( h3 f
. p8 i) Z$ f% V& m  a+ A9 F
安装ModSecurity* }1 }3 M# t* V8 j, T) j
软件基础环境准备
0 F) j/ G9 ]/ k+ p* d下载对应软件包
7 V+ p' [7 y) _" Z- f/ W$ cd /root
( g. ^4 l+ R  g7 D' S; e2 B; l$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
' d& s9 }5 T4 i! Z8 _$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz: a2 @5 A2 p9 n7 }+ b& z& O8 P
安装Nginx和ModSecurity依赖包. V5 I( b6 V5 r% s; F3 @
Centos/RHEL  A6 `; A# I- B7 Y
& M8 I: g1 C3 `/ E; z
. Q/ o% T2 R' J. l: Y/ h
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel5 B+ q1 H7 H1 V$ d0 b# J9 R$ d+ e
Ubuntu/Debian& `; c4 d9 O. {/ ^

! a# W( ~5 z5 ^: W% }: \: g7 k) X5 m: M; [$ S) U8 F
$ 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$ [6 g: L! H! e0 t' i
编译安装ModSecurity
0 c0 V; W2 D% F5 T' WNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 |# v3 `: s5 \+ p- z* P

! ]2 D4 c" o8 K
8 i+ b, G# _) k8 L- e2 {( f方法一:编译为Nginx静态模块
+ h- b1 v/ x& R3 s: S4 n" x6 o
/ j; |$ x( p0 ~* o4 |0 q3 m5 o4 y; Y# q3 P" r8 e
编译为独立模块(modsecurity-2.9.1)
$ l6 y2 }4 b# Y* C$ tar xzvf modsecurity-2.9.1.tar.gz7 o0 {4 k3 I- M; Q6 M
$ cd modsecurity-2.9.1/! g: U0 g+ E, h3 D
$ ./autogen.sh
! J! \# [- e- U# J0 |$ d$ ./configure --enable-standalone-module --disable-mlogc
, o5 ], d! }4 Z2 x/ |" C$ make3 C& n  s5 E1 e- ?
编译安装Nginx并添加ModSecurity模块( a% }, M) @; B+ G$ W. L* w6 i
$ tar xzvf nginx-1.9.2.tar.gz
4 H, |) M* @2 b$ N$ cd nginx-1.9.2, j( O  n; A2 g5 V7 U" [" H
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/  e3 u% Q0 @6 [  K# j! S
$ make && make install
3 ]! d% v1 ?9 ^方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
- I" n/ ^0 U: J% b2 U( L3 |5 F. F' C7 |% R

3 Y' \7 B5 d# d0 S3 E+ U0 u编译LibModSecurity(modsecurity-3.0)
/ x2 t: i1 p$ {; \4 _, l) u$ cd /root$ J# \4 i$ y3 Q$ B8 i2 a1 V
$ git clone https://github.com/SpiderLabs/ModSecurity
. j2 [5 z4 ~7 C$ cd ModSecurity
* ]7 ~% N8 V: j5 `$ git checkout -b v3/master origin/v3/master
" B. _% ^" q" v. u$ sh build.sh- M" f8 \) f7 n" t- |  |& l5 I
$ git submodule init
: Q: y" X/ Q' L, m; P$ git submodule update
$ t3 z% R5 a" k0 x$ ./configure
& f. R6 B) P$ L, O2 B/ B6 p  U! L6 A$ make  {! `2 [6 d+ h+ a' G
$ make install+ w# x/ u5 h5 \4 G4 C! U7 C5 U
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
; F, i  P) `* c* g2 z3 I' M) f& G1 v8 x7 r8 v, u) @( T" N/ ]
0 _7 F% f0 P5 O2 K/ }- o
$ ls /usr/local/modsecurity/lib/ E3 L' u  X, f+ k
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0( T3 M) @0 j- |$ A  S
编译安装Nginx并添加ModSecurity-Nginx Connector模块; t0 w3 W2 ]$ k& a" Q4 |3 c
使用ModSecurity-Nginx模块来连接LibModSecurity5 b- ^3 v! u; w7 o7 e3 }" Z8 U

+ m8 V8 N0 T% X) l* ?6 r, c7 L" w* J; H+ E( G1 T
$ cd /root0 H3 s5 z8 T# w& Y2 H
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
: Y: j# g- {& Z0 Q. g$ tar xzvf nginx-1.9.2.tar.gz7 y8 Q% I8 A* r( R; @7 Y- d  S! {
$ cd nginx-1.9.2
0 [7 t# H  |0 ^* H; e1 @$ ./configure --add-module=/root/modsecurity-nginx; F" ?7 d- A) N( A& g! {/ Y. U
$ make% e% p2 A0 Q  v$ c: t1 }
$ make && make install
9 f/ w+ r$ y# r/ p添加OWASP规则
. z) H1 P+ c+ GModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。# M, M2 Y. a/ M

0 F# v. X' n# ~; A4 |/ c; A+ l
) E4 O( r2 X. s# K+ l下载OWASP规则并生成配置文件
# X5 s* n/ j5 {2 R4 @$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ z% c) u3 i$ v* _; e& L5 }, j$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
* e5 Z5 ]3 C" }5 Q8 q( e! M0 ^$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
0 w/ G) p0 T  f% d' G' b6 X4 X7 N$ cp crs-setup.conf.example  crs-setup.conf
9 l& b! J6 I2 @% d' x/ B! _. d, J配置OWASP规则
" o& C6 o% j% |5 t- c2 N0 P8 I* @编辑crs-setup.conf文件
% P1 A5 ~. ?/ `) r* u' z
3 M, Z3 `) S; R& |, H
1 {2 o& r  A8 U$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf6 |; f  r+ ^+ s8 ]4 ~9 v) _! Z3 H- i
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf) e& E7 A7 t9 ^8 u9 O
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf) m) P5 T$ B- X" D) [+ w
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf# P" x! T" N5 S3 Q8 a7 }
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。  B6 w+ X1 ~4 n4 l: h  b. K
3 Q2 _6 G  O* V0 z1 S+ a

) Z% C6 k+ ^& @- _9 {启用ModSecurity模块和CRS规则
3 W, H; p3 l, O1 K复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; p: U- \( s! F" T5 v9 H7 }5 p
  x" K- {, l% D

5 j% P, D# A- y2 G& J/ }modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。/ I: I+ p9 d& B) d
" m) \8 F* T: `0 I$ ]$ l

9 h5 d& i5 K" [1 c4 s8 T$ cd /root/modsecurity-2.9.1/
* n5 I5 J6 ?3 u7 h5 f$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
# e0 b  `1 Y1 g  @8 H" I2 n* @$ cp unicode.mapping  /usr/local/nginx/conf/* ^+ J; }1 Q$ C) ]
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。' j0 u7 f, a$ M  H7 E: V( `& P
) ^9 n& U% S5 H8 V- t
. S8 w# I( A  G7 s
$ vim /usr/local/nginx/conf/modsecurity.conf! i' `5 j2 [+ L- t7 x8 m
SecRuleEngine On0 a3 ^5 J* V3 V1 S% V- t% I. d8 B
ModSecurity中几个常用配置说明:8 Z* \7 ~! t8 }6 W/ x/ {

1 G) C& W+ x7 `5 |
9 m) h; _6 V% S1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, z- t3 U- z9 H% i( V0 ]; F8 a
) C8 l* d( Y: [1 P, U' M8 a( ?3 A
- [0 c, k0 T5 A; ?! o# b2 ^
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
* j; H( [8 P1 k6 |+ {( y
7 c8 x% t- y1 P0 s5 _+ m& U) _* R1 c4 R( U7 o) E1 o2 @7 \
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
$ [0 ?4 z+ J8 U2 `& J1 ~/ B# L, i( }% O# `  l
: R: ^5 ]7 B" w, D# w
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。" M) s- s% U" `* Y' _# S( ^
. c9 O% g) A' Y" ^" [# |
% R8 c0 u5 _# W
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! d( u, T1 Z; X* \" K" j

2 z$ U/ o  b& J8 z! r2 U# I* d; Z7 c: G/ C" C
3.x版本CRS
( l2 o8 _, j# t( j4 b5 a& I; [- b$ cd /usr/local/nginx/conf/owasp-modsecurity-crs7 i! B: [+ @# c1 _: W4 Q
# 生成例外排除请求的配置文件
$ B# t' x  o) ?  l0 C. o* x$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
. r& L. y8 q# \7 h( R$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
! @( R: |6 x# I$ x: R' @$ cp rules/*.data /usr/local/nginx/conf; ?" @* _& F: n- F" W
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
2 t% e8 ?/ `) [; \# e) W9 h& ?. i4 f4 L

- A7 _. s2 }4 z" Q, s% S+ `$ vim /usr/local/nginx/conf/modsec_includes.conf
3 X% d9 z6 Y  ]% i" N& u( [  }
1 I! q- P! W# r& h. h1 V9 v& g
[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

# z5 |* C& c# i1 a; J" w( V+ w* U8 ]! M% J
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
0 Y7 G1 P1 j! K  F7 m+ q/ ~% l7 k; p* r$ d
# S! T$ Z. e# r; X
配置Nginx支持Modsecurity* n8 O3 D9 t. v3 A
启用Modsecurity
3 C! q: l" l2 D* I3 h" |3 V使用静态模块加载的配置方法" U' t) Z; ^, P; k5 L. S& O
在需要启用Modsecurity的主机的location下面加入下面两行即可:2 C% F' `, F& U) o) E) A- Z/ ~

+ g) o- T; [/ g3 Y# G* O, V1 @8 W
5 N# ?% n" {8 v% jModSecurityEnabled on;8 P3 r! O& r/ n
ModSecurityConfig modsec_includes.conf;3 a+ O: S2 B' R" p+ m
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ r% ~6 r' X0 T0 E/ f
8 p/ J# G/ H+ A7 Y1 s
/ }( m1 Z0 }8 h$ vim /usr/local/nginx/conf/nginx.conf6 _0 ^- f! G1 c) U

$ D' U2 F0 Q, q
1 Z7 y9 C6 a+ B2 _7 l) Y& wserver {
# D7 h! @5 e& M6 S2 A  listen       80;
2 L* G" A8 j5 g; W. U# f  server_name  example.com;
4 @6 a" J; v5 B; }3 K
- c; D- i4 M' a3 f# t/ j! s  A+ k. C* l! T2 N
  location / {
+ r5 w6 Y; H" i( S    ModSecurityEnabled on;' Z* \4 Q0 N& D
    ModSecurityConfig modsec_includes.conf;. R% P1 I, Q0 m) P
    root   html;: X1 X0 @+ T4 e2 o1 V% Y
    index  index.html index.htm;
6 ]1 h' p" X, q9 B  j  }
: Y+ t/ C5 t9 t) g( N" H) H7 W4 E* B}
% x+ J4 b" y& A2 h& T0 f使用动态模块加载的配置方法
- T) H; L4 j8 q* \, U9 \在需要启用Modsecurity的主机的location下面加入下面两行即可:
% b; i, K* _/ c) V5 c/ u, ]
- M, P7 ^* t0 z
2 m8 e" O% F0 k( ]' s& jmodsecurity on;
) x8 B2 X6 R  J: ~modsecurity_rules_file modsec_includes.conf;4 r; Z9 x9 v+ Y
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
, c) {5 z! r' N) X% U3 w( W) x/ q- h2 B# g  h$ V

# N0 `* B6 y0 t/ B' _8 S$ vim /usr/local/nginx/conf/nginx.conf" T9 G! }& W1 R5 M2 p7 J+ c  d+ N+ s- a
, b: i" C. x% Q( p( \+ m" ]

, z) R- n: T' L2 i0 _1 Nserver {
1 l" l, u5 y( w2 ^; y5 q. T  q& w  listen  80;
8 T2 ~0 Y: e( M8 |& D$ ]  server_name localhost mike.hi-linux.com;
3 V2 K+ P$ ]$ `& u/ ~/ H" p  access_log /var/log/nginx/yourdomain.log;; Q+ C0 r" Y/ h& W8 }) u
2 B) U* ?) U& z) {8 _

' M( m3 C1 [' Q  q4 z7 [7 t; H  location / {
; C6 Y4 M- F+ g: M& r- q8 O) c# e- }; @+ @- ^6 c1 K

; H% |7 O0 P- F' |, ^  modsecurity on;
8 M5 M2 e% J$ o  modsecurity_rules_file modsec_includes.conf;
' W2 \6 E) J; Z7 d6 n2 u  root   html;7 Q, x% ~/ }, @* {3 @5 I
  index  index.html index.htm;5 K1 h* _8 t5 l0 z  |/ [
}3 u: Z$ n5 L: _* f/ A4 s3 J( `
}. X3 |) _4 f( s. r1 Y
验证Nginx配置文件
( G  J* F+ E& @2 K2 o, M4 ?  u$ /usr/local/nginx/sbin/nginx -t
* G+ E0 f4 I" S* l  C6 `nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# b4 g9 t( t1 j) S, [nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful+ _, ]/ H6 P4 i+ W1 d9 S8 P& W
启动Nginx
: `; d8 X: g$ ^  `0 T5 T# H$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf7 C: l) ?! P) ~# w
8 W9 W3 P" F# z. ^& \1 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能够战胜更多复杂/未知/混淆的攻击模式。

+ v  F( E, u) `8 a. N

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-19 03:39 , Processed in 0.078060 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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