找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12624|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
6 y% h  T3 @4 M( W. R7 Y, \4 i5 W8 U4 a4 ^( y/ w
' Y4 k! _3 q+ C# q( Z
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。; ~; {3 C9 y6 G" S) Y
+ A0 j- N5 u2 K1 b5 A! k
( Z/ o; q1 k' _; @) x% l" W
什么是ModSecurity
  y; v9 F# J8 |: ^ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。+ x$ K. b* R2 ^  i# [5 V; {6 u

# M) T5 Z8 c1 \0 ~- ?4 O; i' a, `* @) {
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
% b& |; \+ E: a: Q& }9 k5 Y$ A' q( v/ F- B+ b
" w/ H$ s! m' x* o
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% ~/ i! ?" x; s, f; S7 ]4 z$ Y
4 Y' T: R; W7 Q' E/ T6 m" Z0 |

2 y" b; ?2 P5 U* M: w官网: https://www.modsecurity.org/5 k) J+ [6 j: ^" {2 h
+ G& d! M4 X( |1 Q1 M) {+ y7 l# `

5 Y* y3 u1 k+ Z% i" c什么是OWASP CRS
  f9 M3 Q' H5 F9 `& \$ COWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。* m0 @6 j! Q' G: `% _$ D3 G, D: D4 m5 m# G
- y& @$ b* O6 M, K, `
. f0 F7 i# V$ F4 M
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
) ^6 S2 z& |5 R: |+ w
/ m) j6 v! Y5 h, M4 d
! b! Y+ K. G; z6 v% M/ ]' nHTTP Protection(HTTP防御)
  w* [8 c9 J4 l" {# I3 BHTTP协议和本地定义使用的detectsviolations策略。
' F% I9 M$ r2 Y2 ~$ p7 L$ K: ?
: Q5 l) a0 k4 v7 u% _( P. H* ~0 Y7 T% B- ~1 s4 v& d. d
Real-time Blacklist Lookups(实时黑名单查询)
* W. Y& z3 D8 q. u% H( t利用第三方IP名单。) J- Z  S( e" D
: L/ ^- r4 D9 Z  _, J1 ]3 c
' @  T9 n8 ?* u2 a4 U* i' T
HTTP Denial of Service Protections(HTTP的拒绝服务保护), a% K; [; r) n6 w
防御HTTP的洪水攻击和HTTP Dos攻击。% q1 h; J% D: t

. u  ?* R* y/ D8 b/ ?$ V% d3 n3 j5 Z& N! P
Common Web Attacks Protection(常见的Web攻击防护)7 V, o# }% a2 N6 {; Y' @) C  W, _5 l
检测常见的Web应用程序的安全攻击。+ K% ~( l* R5 J! G  H( m
$ ^$ Z9 C; d- |8 K' H, P: f, l
# M7 P. c& J; b
Automation Detection(自动化检测)' B, {# ~; F# n
检测机器人,爬虫,扫描仪和其他表面恶意活动。
* k0 q0 Q# q3 n7 v$ O. I1 K9 @" t' f

$ j" o3 V/ H& [. ]3 kIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)5 y' |- d( s" l! ]" }
检测通过Web应用程序上传的恶意文件。
3 h. c2 s& [) v& u# H0 |. t- k
. V2 P9 _9 q; J4 G) t8 f9 e) t, ~( \! v* V) R. q7 ^2 A5 ?
Tracking Sensitive Data(跟踪敏感数据)
+ s- f5 J. h! ?0 I' P& o7 C信用卡通道的使用,并阻止泄漏。* P0 Z+ B0 p4 J! A1 s- D

' Z; n  V, m/ X+ Y# U* t6 m9 U0 K& S: E2 J2 v& s2 ]; I
Trojan Protection(木马防护)* m# _6 g- n2 x  e8 B
检测访问木马。0 |% w" B$ H; F4 ?4 o6 g

' D; S+ D  M1 G2 J7 k% d, t3 n" }  l  e4 B
Identification of Application Defects(应用程序缺陷的鉴定)6 Y0 {- Z+ I3 I6 D7 F) [
检测应用程序的错误配置警报。: l; f! h) n6 ~+ K# G

) m: f! \1 l  K( {2 p! o% m, }! |0 f' \3 I  @+ |) L
Error Detection and Hiding(错误检测和隐藏)1 K5 R7 Q6 N* f2 V, M5 o
检测伪装服务器发送错误消息。
9 B- o4 D$ c! R% Z2 S- _1 l5 q& P
( X, A  E; `) H& }# [/ U5 D
安装ModSecurity
- W1 _: _" p+ Z+ T7 c7 t/ G软件基础环境准备
. q' X& n9 J6 g2 u, h1 W下载对应软件包
1 p4 z4 s0 T; s* Z7 u$ cd /root$ Y( `+ M- R# X' V- k# R
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
9 Z6 ]# h" m0 r, N$ }# m; r2 h$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
( ], L7 }3 d8 ?4 u2 ?安装Nginx和ModSecurity依赖包
0 v( l& u0 K) |1 T. r/ Y9 gCentos/RHEL
7 ~; `3 `5 [) B0 ?# d  i2 j: h5 G( V7 ]
( q& G( z4 A7 D- \. C
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 }% O) U1 S4 C2 `
Ubuntu/Debian1 v, b: t7 V  j
8 g7 @& K4 O6 l
, p8 v- s3 G% `- 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 Z$ U+ b' ^  o. m编译安装ModSecurity7 Y: ^4 z" j; t4 l" B
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。, n- J% x- o# C$ ?9 l7 f$ J9 }
7 ^+ p8 u# @2 p% U, ]% `/ O: A3 R
' v' C7 P4 Z& x, [: S
方法一:编译为Nginx静态模块% c3 V# U- Z# S$ ~3 E  B) `
) b3 h$ U) |# U0 p! f, W( l: U

( g& J7 @0 ^4 w7 w2 A  P编译为独立模块(modsecurity-2.9.1)' v$ [0 `- k' \" R
$ tar xzvf modsecurity-2.9.1.tar.gz  t" k% n8 a& V6 z
$ cd modsecurity-2.9.1/
# _/ V, K/ `: J% O4 B5 Z  d+ A$ ./autogen.sh
! H4 A( `- A" k0 t$ ./configure --enable-standalone-module --disable-mlogc. q$ ]' t$ L2 [- ]
$ make$ x& B( [% ]2 x2 i. a
编译安装Nginx并添加ModSecurity模块
( h% Y5 O7 T. P+ w% q2 F% J: a$ tar xzvf nginx-1.9.2.tar.gz
1 D* S, L, ]# y# L# f6 e( a$ S$ cd nginx-1.9.2' X9 {- K7 F$ x6 J
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 g0 X) `' ?2 s
$ make && make install
* i% m. s; ^, ~" s* x% Q) J& m方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
7 v, [- ]' ~6 S4 \( ?: ~
; V- ]9 z7 q% ?8 K' F3 y2 u4 Z4 r0 z6 F6 \5 J* g+ o7 b2 x9 A' S
编译LibModSecurity(modsecurity-3.0)
- r2 Z6 y' U" ]! ^$ cd /root3 `" a4 Z( ]& ?% z$ @  o
$ git clone https://github.com/SpiderLabs/ModSecurity
. j  c% @+ L% x( q. k$ cd ModSecurity
) F7 G6 l$ k$ ?6 {( }$ git checkout -b v3/master origin/v3/master
2 w1 @0 I6 G3 j' ^/ N$ sh build.sh! y1 n+ }8 r" N: B0 @
$ git submodule init# H: U# `+ o2 [, `
$ git submodule update5 T7 k) b( T0 q1 ]
$ ./configure
. M. V% t  U8 [, O; z7 c! |1 r+ r, c$ make6 ?( X9 D) O% B2 \& [2 f
$ make install& \' |. A( p  O9 E) e4 v: x
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& {  x1 A$ O4 o. N

8 G- ~, X1 H/ U6 y
- H; S: F0 w2 [4 W: }$ ls /usr/local/modsecurity/lib
/ z1 U6 Q/ N& K4 R7 p0 Ulibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
5 B3 s& _2 }/ y7 \编译安装Nginx并添加ModSecurity-Nginx Connector模块1 I$ P6 C; }% X: @, h# \* D: x
使用ModSecurity-Nginx模块来连接LibModSecurity
, I) u0 @1 ^, _* w2 W
3 H% q2 v' x+ W/ R6 y8 B8 }4 S& y; x
$ cd /root
! R* }2 p' n( C2 [) c: L$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
% k" A$ {5 ~0 \9 k$ tar xzvf nginx-1.9.2.tar.gz4 P$ r0 m3 {0 G; Z
$ cd nginx-1.9.2
7 V7 ]5 S" V  X1 U- F" g2 d$ ./configure --add-module=/root/modsecurity-nginx
6 v- R0 N' d) g* d7 O$ make
9 o: ?( D7 q" M# Z0 i2 X+ g8 ^2 G+ h$ make && make install/ a$ o" o; T# Z# P
添加OWASP规则! ]& s. Q& ~- h
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。& V5 h/ R# W4 r( e( U, r: o8 v% j5 E

. B$ ?$ m; p" V" t
- \/ O7 V2 |( W# o$ k3 s下载OWASP规则并生成配置文件5 C# A, F! f( n6 D
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git* Q5 d; O% K; k8 X1 n# J6 m$ L( p
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
; G' z1 F+ l9 r9 L4 E. a9 X* L/ ^: k# h$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 R2 L2 H" ?% E  t: t
$ cp crs-setup.conf.example  crs-setup.conf) B: R/ O7 C! c8 C5 l- Y
配置OWASP规则, n* p0 J8 R( }- R! N9 O
编辑crs-setup.conf文件# W" E1 t4 n3 y8 s& n( t/ Y& D1 r

! i% m3 @/ I. e. v, m( s/ O0 l4 z6 ^& \) W4 \/ m
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf) N' M% R6 c3 E2 y$ v9 Q/ ~
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf% M3 b3 b5 g- O& i* q
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
+ F* A8 o2 a4 p! a( u7 u$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf+ Y( F, }/ ^8 A, A5 V' s1 {
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* \. G2 c2 q! K3 x1 ]0 r& r# _4 U+ b0 x
; z* _6 h) y& {. C2 p: q) c) R& k# j+ v' C, S1 q" q, k
启用ModSecurity模块和CRS规则
+ V4 }! g( D: Y! {复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 x4 Z, }+ v9 z' t1 r7 J* u' \( G9 d# S& h' Y% \0 h

' e- O: i+ l0 M, wmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。3 r& |0 T1 v. O! o+ l9 l
2 R6 U  G% t' m( U2 s

) R# s$ E& V. a$ cd /root/modsecurity-2.9.1/6 G( \* i* l5 o( G# x# i- B$ S
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
2 B5 a( J. t) M' t. l! T$ cp unicode.mapping  /usr/local/nginx/conf/+ ]( L) S) h% ~  n7 c' F8 r# F! `
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。2 s/ N) V4 t" k! u
5 x9 v1 S9 c- P* P# `
+ \2 a0 T) A1 b2 g; H
$ vim /usr/local/nginx/conf/modsecurity.conf' n2 [4 B4 e6 z$ F# Q* L
SecRuleEngine On1 o0 m+ z2 V$ g" p/ w
ModSecurity中几个常用配置说明:
2 P4 C8 Y. W4 Q& W' @1 B2 S# M
) I- B0 e8 m' A% M, r6 O. m- ?/ _* f! e  u2 D* K% K
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
* [% T! D( ~9 j3 b0 Q  g9 k5 a4 A4 q& _6 o6 C

1 T8 \* \5 @+ B# [, J( n) |$ O2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。5 N! o* _8 F0 g/ I3 D7 }
0 R, ]4 a* ?6 k; L4 N% s3 K
# `8 U) o: ~& Q( _
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
' k$ |4 q6 h* `& i9 r
4 [+ K8 s' U1 S2 A# U% C& {, I
& B6 S, @+ C* t3 _; F7 N) @3 X4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 f' Q' `& l3 ^4 @/ q- v! M# Q

- _7 J( H6 d' M0 O1 O3 k* r
. G0 Z1 S8 |4 p! Y在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
' j$ y/ V; ]4 Y# }3 a8 }7 X" D0 e$ Q7 H

' K6 H8 [" P; k! ^% r; d6 P3.x版本CRS
1 K" s) ?- ]; _$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: e) E4 @- D3 C% T$ ?# 生成例外排除请求的配置文件
, l7 X9 a! N- R( i4 P$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf4 q2 R7 [, L/ U7 x
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf9 I  s4 W" E1 ^" V0 M
$ cp rules/*.data /usr/local/nginx/conf5 t2 C1 d1 V  _# }0 h. v9 t6 c% b
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。- h% Q9 f) s+ z8 B. u
6 y. B8 k9 Q9 [! T# i) ]2 N
# N' S' K& {2 I7 b( t7 G- k9 t
$ vim /usr/local/nginx/conf/modsec_includes.conf
( {$ C: N6 |+ r  D& q+ [+ u5 q- m# X) Z( A; k% C( ~. E' B" z9 }
[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

# D  y$ d. \2 s" j3 a# |
0 {+ Q  d- ]1 _! m注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ X  ~3 {% b" |. R3 D8 u( s
9 `: w/ U9 n+ _+ q/ p0 j0 p

- V. k3 b" [6 `" W. G; c+ r配置Nginx支持Modsecurity
0 k* S) S  X' z启用Modsecurity, T& p. a! S7 x5 M7 X
使用静态模块加载的配置方法
4 t+ u" K" B! M5 `) b: ~8 t在需要启用Modsecurity的主机的location下面加入下面两行即可:4 `3 b4 K8 W8 g5 B. {$ h
! b+ e& D' i3 G2 \7 R

  P6 m' v  Y# u2 A4 v: P) \$ GModSecurityEnabled on;% T8 D" N9 S3 R8 n! y
ModSecurityConfig modsec_includes.conf;( R0 j3 Z  G/ h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 U3 r2 i# ]& \( a, H! G8 w2 t, e
, O$ U& R' k" Y2 `0 X, I$ U+ `' p' t  J# g* w
$ vim /usr/local/nginx/conf/nginx.conf
2 Y3 X" w9 r' E6 ?# K
, u0 g# @2 I, c) U) u$ ~2 w) `! V; y, g) r; d. e7 F
server {$ \0 ~$ q9 N' `8 f* h+ ~
  listen       80;
' }& u- W9 Y  V' j6 }  server_name  example.com;
0 Z, E1 n* |  k( U& F  T8 N4 \3 o$ t7 ~% d

. G1 `: B6 x6 t  location / {, }/ {# W: S$ Q
    ModSecurityEnabled on;  I  f, i2 j) ]% X2 m6 E- k
    ModSecurityConfig modsec_includes.conf;
# F: V  Y' j# d& ~) \6 X, A    root   html;
3 X* |% k# X8 ~    index  index.html index.htm;$ v: T, p5 N, a. h2 I  R, X  b
  }
! h& o6 R  p( E( M' A5 v1 D}* P1 x, T7 g  w0 O, b
使用动态模块加载的配置方法2 W* j" m7 R+ K0 o$ c* G
在需要启用Modsecurity的主机的location下面加入下面两行即可:& |9 `1 Y: ?' R7 @6 \; q* ]/ u

+ q+ A- y# p: k9 u& S! h1 X  w2 {2 |2 S
modsecurity on;& B2 N! s& A2 Z( i5 Q2 U1 M8 ~0 J
modsecurity_rules_file modsec_includes.conf;5 K1 M0 P7 @0 s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 i7 x6 A, m/ x- A; p# A
7 q$ e0 ~& U* ?  C; Y, Y

6 c* z3 A: X: f9 n3 d: A( X# h$ vim /usr/local/nginx/conf/nginx.conf0 \: ~1 ^  M" c! R
& Y5 I: j7 P7 g% c- n
; A3 c2 O( r5 H1 O0 T& w
server {
% X% E' F& k1 Q$ ?) d3 g: U9 H$ g  r  listen  80;
0 O4 l' ^" D  Q4 y7 |! B9 o1 [  server_name localhost mike.hi-linux.com;& `! |4 ]. l6 q1 j& t$ B! t( S
  access_log /var/log/nginx/yourdomain.log;
% C' r+ H; O( t! v/ M8 y" i5 J. j$ P6 c  P+ I8 K$ y, z
9 f6 I4 t3 y  Y' v/ s
  location / {
( a: k$ m$ W# I; U9 F* i  S3 B6 g/ C, {$ C* [/ q& f, j

$ U% s+ h8 ~4 [2 |  modsecurity on;6 j* c) l4 T2 g8 H& {: C# s: I
  modsecurity_rules_file modsec_includes.conf;
3 w/ j/ _! t4 V$ s% L8 e  root   html;1 Q9 Y& n, U* ~9 ^; {& K7 I6 r
  index  index.html index.htm;( u7 }, c( v2 Y0 V
}
9 u- I# h/ J+ V; P7 z; y. |% J}
; u0 M# q) d0 {& {; B验证Nginx配置文件
: Y* U6 \& H# b$ m7 A" b$ ]$ /usr/local/nginx/sbin/nginx -t
1 ^  R; n  T2 f4 [! l+ dnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
5 v: H6 o! R3 a6 r+ Bnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
; |) p* w3 X# Y! V& {# S0 O启动Nginx8 Y+ Y$ L2 n+ i7 I8 }1 ^- Z
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
+ t& P7 P' V# q
2 X* s* X6 u( s' y5 H5 s% t

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

; M! A! j9 R7 X. X

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-10-20 03:25 , Processed in 0.061155 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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