找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12940|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。: m9 K& [" m4 O5 f; c

6 _0 h: }- s8 N3 u7 F2 l2 Y
2 M! Z2 ~$ r: i; \在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
1 N: \( f* e2 p4 V3 o4 g( Z2 q/ V+ X% X& {3 b
6 o& d2 V' N0 x* B- j: d
什么是ModSecurity( X5 i. @4 ?  V9 V
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。6 D( |; }- x9 T2 h$ F
7 |1 l# ~, }* b
1 T, h) j3 f% z; u- G
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
5 V" H; _- I: O7 S" I1 d
1 E' R" B2 R& j! w8 L$ g8 _
' o0 [: U( O1 NModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ e, R& q8 }3 w+ |: E# o/ o
6 X$ a9 n8 c# |! v% d' I# p* h! p. u" D# c) ~" x2 O
官网: https://www.modsecurity.org/) y' \- k. k$ O& K6 r+ W( @

# U+ y" |5 R2 k/ {2 W; R) p* _+ A) W/ n
什么是OWASP CRS
  K* [) L4 S! a% C2 i  L1 [( }" u: zOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。% E9 k4 @( E* _* y. r

3 Q9 U" X7 l' W0 [5 d
' D2 s6 l/ d. C6 R4 ]) _ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。% _% e% }( n* k- @* A* f8 B
8 P/ `1 N. b/ `9 Y* V( J

9 k1 p  x6 A9 l: E( MHTTP Protection(HTTP防御)
% `3 H  T# n7 W) f( EHTTP协议和本地定义使用的detectsviolations策略。! r" a' t# U8 {( C

) I% K9 q5 P; `/ e
' ^7 M& w' r3 b/ y8 {2 cReal-time Blacklist Lookups(实时黑名单查询)
. |4 i9 w% O$ r+ d+ n利用第三方IP名单。
: v; \. Y  Z2 }5 }4 v* ^$ }1 }
- P4 W/ J, J( }, L7 k) O) i) \6 @9 C! a
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
$ @! I8 N9 }9 Q) a* x% s1 }防御HTTP的洪水攻击和HTTP Dos攻击。
! U, T- ]2 `1 C4 E+ ~  {( V+ @+ H0 |  i8 f
3 D. `  ?( I; P# w4 x
Common Web Attacks Protection(常见的Web攻击防护)- a" h! M+ C/ X6 R
检测常见的Web应用程序的安全攻击。
, ]2 D# ^" R" G1 X2 w3 G) l% q/ L: e6 u8 Q& Q

4 ]+ [' h7 T0 X' N. yAutomation Detection(自动化检测)4 I. K$ ~+ r# A2 D' I$ T8 B
检测机器人,爬虫,扫描仪和其他表面恶意活动。! O; w: v5 Q$ H4 Q$ j  w. b8 r

2 ^9 B" l# Y9 l. y. `
0 m9 L+ n7 X6 |: E% Y) D% T! e: AIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)1 z) u& c: [3 K& m5 N  z/ n; v
检测通过Web应用程序上传的恶意文件。  ]. u9 e" A5 S4 N" f

* b: [5 ~! \* |4 X6 S
0 o- W4 w' r+ l) |8 Y- B+ {2 UTracking Sensitive Data(跟踪敏感数据)( D7 t; y2 {1 H) k6 A
信用卡通道的使用,并阻止泄漏。
2 o( Y3 {8 x6 S, ]' f- P
( p% e1 ]5 k# x4 M4 w& O1 l$ p7 p. j
Trojan Protection(木马防护)/ k  C5 g; y% v6 l
检测访问木马。
9 Y5 a2 H9 `, Z0 i* R6 D" Q! S5 V" G5 u" w( z. ^; z+ G. L

3 r+ ~3 f- `) O# q' x9 TIdentification of Application Defects(应用程序缺陷的鉴定); S& u1 I6 ]3 Z! L  g
检测应用程序的错误配置警报。& g& N$ D9 |$ _8 Y* |9 V  H8 ^' Y

1 L- f: D, _+ u6 x' F% I% z! B$ u+ O. }' q7 [( K7 @0 p
Error Detection and Hiding(错误检测和隐藏)
- S  H+ Z8 z) U, I* S9 E检测伪装服务器发送错误消息。
& `+ g  V! B6 \4 Z  T% p1 _2 t: l0 ~* u' q) u# E1 r! z+ B4 Q  p

, x& P1 E  `# h安装ModSecurity
* s0 y) C% r/ R) B$ s% H软件基础环境准备% L4 F! P  w' k; i
下载对应软件包
2 J, c$ E3 w5 J2 H: y6 u$ cd /root4 S1 c8 d- y$ d3 W5 k- h  J
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
/ I. ^0 q' H# ^$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz7 M; W- [+ i4 }# t# n: [3 x7 @5 b5 f
安装Nginx和ModSecurity依赖包
1 r4 l$ Q# S) B( n  a! ?# L/ KCentos/RHEL
2 ]/ M* y- H5 g( C4 V5 `1 ~
0 `8 t7 U$ [! E0 I" a3 Z9 i7 t4 e4 U. M3 ]1 x
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel! b5 H' Q% f4 Y5 D  k
Ubuntu/Debian
. V6 S( E4 F* C3 M! y
: {/ [3 D2 X9 N2 r! i  V0 V2 S$ p9 E+ D8 v8 U
$ 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
0 F5 f/ {  l/ w& Z3 e0 R. E编译安装ModSecurity) }9 z7 W" ]" f1 D9 v
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
5 f% @- f- P5 ?% s# C0 W
( O, k  M1 Y9 D' @* e6 B" e: q
方法一:编译为Nginx静态模块5 ~& H1 D; L8 b: N9 C) o
+ Y% }/ Y# X( C! o+ Y0 g. Q

. j5 ?* E% z- [( ~& P编译为独立模块(modsecurity-2.9.1)$ x# |1 y7 Y# `7 M( f7 u& B
$ tar xzvf modsecurity-2.9.1.tar.gz
6 K3 Q# C" O" g2 d$ cd modsecurity-2.9.1/( R* D4 o) h0 u1 C9 ^
$ ./autogen.sh
# S" r9 ^, V. C0 K1 A) U$ ./configure --enable-standalone-module --disable-mlogc# l3 H3 V# Y' f
$ make
' W% h4 X( K% B编译安装Nginx并添加ModSecurity模块
2 [, J% N& n5 }( X- G- {8 B$ tar xzvf nginx-1.9.2.tar.gz
7 f6 e; i3 D5 o' r& Z9 }$ cd nginx-1.9.2( a, l' }; H  L+ e/ H
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
6 ~5 J  \" V) M* m; U; o2 m, s8 G$ make && make install
3 Q/ n3 U4 y- U1 S% B7 b4 W方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
8 \& q7 h+ {- e+ G$ |2 a9 q1 D7 j- h9 P

6 I* y& V2 W. E# G  N7 \编译LibModSecurity(modsecurity-3.0); o! d. S- Y0 l9 w6 O0 _( A4 P
$ cd /root
+ i& j1 f$ F# v# M/ B$ git clone https://github.com/SpiderLabs/ModSecurity
8 |+ @- N7 v. }5 H; n0 |7 I2 X$ cd ModSecurity
4 R& G% @' {3 A4 Q$ git checkout -b v3/master origin/v3/master, A/ G* J4 A( q) G+ l8 Q
$ sh build.sh
7 y4 d9 p- E, h& }3 Z$ git submodule init+ ^/ }/ V; Z. Y8 o; [
$ git submodule update
( p# K, W9 ?' F0 b5 |" V: u$ ./configure
+ ?. r( J; R8 V2 j- r; n$ make
3 b& O' S0 F& n  [. i$ make install. |7 u( g: ^" o4 X8 }5 h/ }
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
6 ^. o" L3 Z0 A6 u6 l5 l# h  w
8 S& @& m, @0 V; H# q+ h
% P  w1 H# _- y* `1 ]; i$ ls /usr/local/modsecurity/lib  z1 ?( z# j- d: ?; s
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0' d! s. A/ }0 H+ @  h) o5 ?
编译安装Nginx并添加ModSecurity-Nginx Connector模块
. ^7 g# X% p- ?3 T: Q' o使用ModSecurity-Nginx模块来连接LibModSecurity; @- q' }2 j/ h1 |. C2 T+ v" W
5 X4 W3 u7 X3 o2 X. j5 e

) I5 u( M' K: X4 S0 D$ v: |$ cd /root3 _! o  o+ n% [# ]  s
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx6 N: |& g- I8 l. O! m, N
$ tar xzvf nginx-1.9.2.tar.gz% l1 O4 U" n5 p2 z: C; O
$ cd nginx-1.9.23 v+ d) I4 a/ V! D
$ ./configure --add-module=/root/modsecurity-nginx! S& B1 W( U' Y3 F9 F
$ make
0 {  i" B* ]& _" V$ make && make install+ p, w* S+ ^- e
添加OWASP规则
. ?/ N, d  p7 H6 _1 V8 g" J, d% G( o  qModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。( F6 _- `) c1 Z- z4 ^5 ~3 D
  m) ]% S7 t  O4 h+ b

( |5 V, K8 ?$ l下载OWASP规则并生成配置文件
& s2 V4 R8 m3 n$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
' v' ?2 ^: c4 Q# N* N$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/% Z; u  ?/ r1 C" B4 U; c
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' `9 T! [2 `3 U% Z$ cp crs-setup.conf.example  crs-setup.conf
  a7 }4 ^; G0 J配置OWASP规则8 ~& b) U7 v) X
编辑crs-setup.conf文件+ d( x2 W* ~$ L. D
, M' k: A2 G& h7 M
! }4 l- q$ q& l( r5 }
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
; I! z4 h" `1 w) i4 m/ X$ O$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
' H! A2 K+ ]6 P4 J# z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf# n, g; q" g3 v5 F0 `, x
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf8 H3 S+ Q- Y6 X( I2 @
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
0 o3 I% i( J8 B7 U) Y6 @$ s! U
# |" b% n$ V$ S# N
- S, s9 u. `, l% W启用ModSecurity模块和CRS规则
* S% I# N: V0 ^# [# M: u7 H' w复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( k/ r5 ~5 p: w- s; c" Z$ n3 q3 O7 \( }' [0 @. H6 f

4 E" I' x% _) p8 gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
/ j9 p% P2 V8 F$ z
( ~( U/ g( r2 [  a) ~6 \5 V- f2 a( Q9 ^: a4 I4 ^" L
$ cd /root/modsecurity-2.9.1/
3 ~+ C. l  b4 s8 E6 ]  O$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
! [2 d1 _, W0 [( F$ cp unicode.mapping  /usr/local/nginx/conf/
$ [, e) X% C8 ~7 U/ T7 p  X0 }5 f! |将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
4 J9 y5 p* n% C5 S* B+ T, s0 P. i1 N: x$ o3 S4 e( z+ Q* O
* S, H6 U: o$ q: V
$ vim /usr/local/nginx/conf/modsecurity.conf
+ l3 w0 f1 E, {$ {! T: wSecRuleEngine On
0 f, k' }9 A6 r9 X0 \ModSecurity中几个常用配置说明:
; t6 E: I$ e1 e% ?4 c% `3 v' b% _- k3 b$ K
7 @/ c6 P" Q2 S, H" j
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。6 l+ x: {0 D/ X6 B9 D0 |  t/ {6 W
/ ~" R! Y( K3 k+ E. L2 U; V

" `) N& C5 S! L. V* U, W( u6 ^/ `2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。7 |0 o# s+ I7 h: a( [

! n# R+ w* V2 a' c: e3 f! S2 }( {; f2 O
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
/ f! L6 @/ Y9 ?  a2 ^( k
7 ]* l4 x% L6 G7 k: N
: U5 E" W6 A$ f" h$ a4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。9 x# o; s% c# s% x3 w* r
! {; ~( \6 P4 a5 L

3 L3 Q6 j1 t% z0 m# y" `在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
0 ]2 {; j5 i2 s( }1 |+ D3 H& A: L* j# p- x. u5 C9 e) k+ I

+ M0 h1 t5 ~4 d8 H3 d+ b3.x版本CRS
  G7 ^: P" C- W3 w$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
' M/ V# q4 i* k: b2 W) K# 生成例外排除请求的配置文件
& x9 [, c+ _, X) ]5 J7 r0 D$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
; G; l6 R& W+ H( w+ ]9 z$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
& d+ j1 E; ~( n' x$ cp rules/*.data /usr/local/nginx/conf' ~( p( z) L4 [8 s" r0 y0 o1 v
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 I; ~- u; ^  o, i4 ~7 J1 Y

+ M0 I0 i6 ?  X- c
! d7 X1 r& a5 e& D' `$ vim /usr/local/nginx/conf/modsec_includes.conf% Z7 o, A5 `4 E. g5 V% A

1 \3 `3 R/ |- g$ f( ]& R; D
[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

6 e6 o  u  A: L1 U- `# A8 T8 H, W
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
7 Q; F- E4 \! k4 ]( g- u/ r& s  E1 X; A0 Y% K& D" Y5 ^" Z; I* p
0 @) W" K0 _  U$ e* N
配置Nginx支持Modsecurity
# ~$ c+ z, U6 L; b" x, u" h3 v启用Modsecurity
: E, L! f5 d8 _: c; B) N; O使用静态模块加载的配置方法+ h, R* J$ A. I1 w% w# D2 ~$ v! W
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 r- M& I$ D' ]3 ]
  k1 W: H+ @4 S' q& j

1 l  g- h; A; R0 f: `$ w5 i' ZModSecurityEnabled on;
, O. x! u) }' e: R! yModSecurityConfig modsec_includes.conf;. I# n8 M4 p1 ?: P* k
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。; l* _& V/ u& t( k  h6 u

  g- m$ w, s: I. m" C) x
  u; Y' L7 ]$ ?% A$ vim /usr/local/nginx/conf/nginx.conf, i, c2 ^5 z1 ]$ ]+ G7 y
% B' G6 e, F2 \# ?* B

8 c2 y+ z0 }( N9 ~# o6 W; |8 a1 Y: bserver {6 `0 H$ ?" I6 F. m. A
  listen       80;
) e1 D; ^  G) Y$ F: Z  \6 C# w" C  server_name  example.com;
5 r7 I# H$ ?) \, o1 J# v7 `* X
- b: D8 E1 ~$ K* k# @5 |) _5 R6 P5 F) R( K/ `
  location / {+ N) \5 _: L0 k
    ModSecurityEnabled on;
5 M, p4 r. \! _, I  T! t# v    ModSecurityConfig modsec_includes.conf;4 v& F1 F2 E- s2 J! ^) T
    root   html;
# h0 d" K6 a( c8 Y4 E    index  index.html index.htm;
7 d. D; |7 ^8 E1 d/ u, N  }9 F, {8 o- D1 g* B$ o1 J
}
, f2 [, }4 B& t! ]使用动态模块加载的配置方法
- V# K2 @3 e& d' P在需要启用Modsecurity的主机的location下面加入下面两行即可:
: U) ^4 j; P( ~) ^! t+ n: o
' j0 r1 U1 F1 T5 u2 {' r) r2 t
( Y" A3 y% p0 H9 `9 Y2 c2 Ymodsecurity on;
" C, i* ~) N) [' \4 A$ Tmodsecurity_rules_file modsec_includes.conf;0 u# |" f) p: X! G/ a
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ x9 K( ^9 B0 ~* M# [9 Z: W9 [, L3 c) k- O2 _  |+ Y
, p/ j6 i. n1 @( u
$ vim /usr/local/nginx/conf/nginx.conf& n1 T4 ?3 j! m/ A/ ^: G) C! {& k

; [, ]# |  g6 N7 s, h) h
: |# v' z+ [, V  T* Zserver {2 ?2 s6 U- o1 i- i* b4 U$ c5 d
  listen  80;0 Y+ v( e5 G2 p  V
  server_name localhost mike.hi-linux.com;
4 E6 ?6 ]6 d8 Y. @9 k5 k6 V  access_log /var/log/nginx/yourdomain.log;
- k$ E8 r3 _0 Y$ L/ X0 i8 m2 i3 E3 Y2 |' h! X8 C
7 O$ ]. Y; ]2 P, y
  location / {
  N6 e3 n. E$ t2 _# c4 Y- b- k& C1 v7 z0 K4 p
0 w3 ^& v; A+ W4 W" q! N) q. ~2 M8 ^
  modsecurity on;
( P' e/ d8 H7 W! P9 l# W% y  modsecurity_rules_file modsec_includes.conf;6 A5 R( `$ D# |4 [1 g
  root   html;
2 u  g5 p- a: f$ d# l  index  index.html index.htm;
* M( A6 I; V. Y9 I}9 P7 s4 k6 y& y- z' j
}
( S6 Z% `8 @: |7 {& B/ v1 Z验证Nginx配置文件! {0 v( _6 S  D5 ?. r& U9 _
$ /usr/local/nginx/sbin/nginx -t  A* y+ A% }, K- W; x' P8 q* {! t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: Q9 B5 b' q& K  `0 r" Znginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful/ _8 b- I1 [$ g
启动Nginx8 D) Q2 j( o" y5 g" H# l0 C$ ~
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
1 m; v& u. Z+ u( R9 O: k2 _( v6 V- H# D4 j2 Y' }

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

) _7 O9 @: w- t: `. w5 L# @

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-12-2 10:33 , Processed in 0.083094 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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