找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11798|回复: 0

利用ModSecurity在Nginx上构建WAF

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

9 y- N9 O( E7 B, b$ Y
4 F% }' z0 B: ?+ A' o4 K" Q在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
% z+ R3 Y, U! f  x0 K0 [+ P5 Y! R4 Y3 B9 n
6 Q1 U. ]& n! Z9 @; v- G, S  V1 @' T
什么是ModSecurity
" o" `7 v, ]4 P/ e# ]" s( @( L+ yModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。/ A8 N2 x4 V* i3 D
* W. n; o* z( w* E
: E) u! k5 b% u- ?* H
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。5 o% s3 P" a9 Y

) f6 R6 r3 Q: }) u" V( [5 Q5 m7 I. m' d
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
6 ]! O9 G. ?; \% D/ o' ^; i% \; s( Z, R  X( @
" c  F/ j: G! X
官网: https://www.modsecurity.org/
( O) d( d& I0 q4 ?% K' u% V* z, _' Y# c( `3 N/ `5 G/ V
+ E: @& @% ]. [% {; f6 h7 d  Y
什么是OWASP CRS5 g$ U3 b" F( l7 q" ]& f
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
$ d  R! C( l  e" N
$ @( F$ z& R7 @) Y8 s' H5 S. R& Z* D0 Y
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
) {! B/ [2 G- U. @
; M0 S0 s3 Z: d7 J) u$ b1 j4 `! ~7 v: g: b" W! U3 R' g
HTTP Protection(HTTP防御)# c0 `$ q0 @7 M' {# j/ }7 r/ S( f& V
HTTP协议和本地定义使用的detectsviolations策略。
6 j& S! ^1 c9 s% G9 n0 l
0 c4 h: {2 u4 j4 D2 q/ ?4 D7 t$ r3 O7 d6 K
Real-time Blacklist Lookups(实时黑名单查询)$ ], I/ N5 M0 {& z+ d& M) F0 F
利用第三方IP名单。
4 B3 V* ^1 n0 f; {$ j$ E  L1 _' i: b. @( }6 \: j9 L# k2 q

, a; J, g; }& T# v/ eHTTP Denial of Service Protections(HTTP的拒绝服务保护)
2 k" d# M  O" t防御HTTP的洪水攻击和HTTP Dos攻击。
9 X! W- l) C' o. z% J: G
) o7 T1 Z/ b7 N- v& f# O5 e9 P) v* q# ]' m9 }6 A9 f6 K# T9 y
Common Web Attacks Protection(常见的Web攻击防护). q; L8 v$ E6 l% m. ^
检测常见的Web应用程序的安全攻击。
3 M- T" A- `/ d3 ]/ h1 Y2 K; e' A
3 ?" i/ a0 c  |; f
: _/ y1 o* ?) s/ y8 e1 aAutomation Detection(自动化检测)& Z9 P% }8 R/ E: @
检测机器人,爬虫,扫描仪和其他表面恶意活动。$ A: y  n& h# I. b( a0 n
: z- m9 G# g) s/ W. ~

, C% I! n1 ]! y( d' iIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)
% A" K' ~. ?1 I! g# k, V2 }6 T$ {检测通过Web应用程序上传的恶意文件。
5 l; M. e% H5 i. F% Z, V7 X5 W: T  f  r7 L0 s5 S

. `( ~& X, L0 A1 u* H( d7 ATracking Sensitive Data(跟踪敏感数据)' I* ]. K: i) I' e  c4 ^( r
信用卡通道的使用,并阻止泄漏。
( Y) v" k# o# l* J+ S0 `2 v- u7 T" A! [  T
' C# G2 G8 J4 g9 ~6 O6 ~. x1 l
Trojan Protection(木马防护)- B  u) G+ h8 }$ u
检测访问木马。
) |9 J. a) |% A2 e, g; Y. h
+ M0 D3 a" _: J( X6 U0 i
# X/ I/ [6 E; F- U* N! rIdentification of Application Defects(应用程序缺陷的鉴定)
2 l# G2 E! z) v7 ^检测应用程序的错误配置警报。
+ g9 R* q. n! i6 d' i7 Y2 W2 q* R+ d" ?# `* i. i  d( B1 {
8 P0 V9 A& W9 V  `5 s# |
Error Detection and Hiding(错误检测和隐藏); {, o# P% C, h1 P0 [
检测伪装服务器发送错误消息。' c, T! R# T( w& I3 j8 p
: s  Y9 k% ^6 ?4 U" ?# L

1 @* h! T- {& ?2 {安装ModSecurity
7 j8 |5 f' k, v$ @  o, t5 M% y. O6 U" p8 K+ S软件基础环境准备
5 F2 x; Q& M- j5 b下载对应软件包; B- V+ V  {; y( J* Y
$ cd /root
# e. f; _1 _) S& ~$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
( l+ e' B# A$ L: r( }$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
; a/ m; L  @% _5 G4 [7 i安装Nginx和ModSecurity依赖包
. g  Z; L' f/ j( o) f) U# S9 jCentos/RHEL+ r; J; `. w) o3 U. [
' A" t" F: M4 X3 d, a

& f$ G/ m" V7 h* F2 o5 L( w8 L$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel" m1 ~9 C4 c0 R& ~/ b8 ~5 P
Ubuntu/Debian  w( [( {. e; ]9 m3 }
- A5 V3 u0 {9 f6 m& y) o' v

/ O! z  l8 A  B$ 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' {% Q7 j6 W5 e! e3 z
编译安装ModSecurity
3 J  ~- p0 R. x0 pNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
# z( }: W% w; v9 g% o
3 I5 H& l5 w( B+ Z% E/ [& t: v* n
* W. N7 Z; l1 r方法一:编译为Nginx静态模块
( Z8 m/ v: A7 r& o7 x) l
+ o" N2 l1 J! w- Z- c: q# Z
8 m9 w3 K5 ~0 P' }+ t# D编译为独立模块(modsecurity-2.9.1)
4 T7 ?7 }/ S9 P& j3 U. k$ tar xzvf modsecurity-2.9.1.tar.gz! ^, O, k" M' D7 u
$ cd modsecurity-2.9.1/
6 V( L( Y7 |" K$ ./autogen.sh
( W  s2 B$ a" @/ r8 w$ ./configure --enable-standalone-module --disable-mlogc
: ]+ n! ?- z, D- c+ |; \+ q8 s$ make8 z9 O- [8 W9 _; w; k
编译安装Nginx并添加ModSecurity模块
9 ?. w* T  a( I) r; W$ tar xzvf nginx-1.9.2.tar.gz& s  l4 `- |( u! U4 t6 v1 o
$ cd nginx-1.9.2
. W  p% e0 c" i* _$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
& V, D1 d! ^: U8 w6 q$ make && make install
4 }( V9 d0 H: b! o- B3 ^! j方法二:编译通过ModSecurity-Nginx Connector加载的动态模块7 ^3 Q/ [/ m, V7 A8 i  p# T
' w% X4 t2 J% S& Y4 \4 @9 g

2 H! U. k7 F5 s8 e9 h3 C: a编译LibModSecurity(modsecurity-3.0)
1 g3 x/ c: H* e0 s, X$ cd /root
! A( {4 |. d+ t  F) Z' V$ git clone https://github.com/SpiderLabs/ModSecurity
0 X' g- u3 w% S# o# U, \8 `$ cd ModSecurity( ?2 b. W$ A* y: B1 s7 s  {$ I; d
$ git checkout -b v3/master origin/v3/master
( f- P- s- g5 t8 ~! L% e2 y$ sh build.sh
- H  E5 E: A# @. K$ git submodule init
7 i( i( u2 Y4 Q6 h$ git submodule update
* ?+ B7 r" t8 O9 m9 q3 H$ ./configure
( P1 Z0 s/ j3 s8 `+ P$ make
6 A, f. E- D+ j0 N) S7 A* D" `3 \$ make install
+ f% \5 U! c" F% tLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
$ T0 I7 L; V8 x  p& g! P
7 h/ r- M$ t7 U% _6 V; b1 ?% V% a2 G- e
- @( g& ~2 R. ]# F( u1 ?$ ls /usr/local/modsecurity/lib
% t( D7 H) O6 B( p. B8 C0 h, ilibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
& O- U* V3 I5 T2 `: A7 v/ F编译安装Nginx并添加ModSecurity-Nginx Connector模块- k# @% x, R6 _: M! @- k
使用ModSecurity-Nginx模块来连接LibModSecurity
& j8 i! u, s5 R! h( }
; n  A3 z' t; J+ D$ H
+ e7 B3 R5 K5 e: e- M# ~% ?$ cd /root9 Z3 }+ Z+ w4 ?! i  x
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
3 ~. y) d; f. Z3 N8 `% f$ tar xzvf nginx-1.9.2.tar.gz/ C. p  L; r% z6 L' k
$ cd nginx-1.9.2
* g3 s' u" G& A# e$ ./configure --add-module=/root/modsecurity-nginx) f- ^8 x* h; z6 ?& f  B( _
$ make
3 ]9 ~7 h3 ~/ \, n( u. A, x% x$ make && make install
7 l) U& V2 k/ w) q7 N: w8 Q添加OWASP规则& p+ V1 Q& H: h4 z$ E# _3 b& a1 D
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
6 i3 M3 I1 Y8 E1 E; V
! I) S; W6 M* p( y% B/ \" d4 O' U1 f" e: y
下载OWASP规则并生成配置文件- v- Z2 H; F. U( D7 B) s
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git% R. t% ~* R) `' L  b0 e; u
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
4 U0 I% j; B% f, ^! C" G  y0 D9 S$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
/ P' ?$ m1 y) Z" `. r/ ]) R. `$ ~" W$ cp crs-setup.conf.example  crs-setup.conf# k) q( E; ?3 z: T
配置OWASP规则
# N6 e) P5 \! O2 L6 l编辑crs-setup.conf文件
$ N/ O: l( S) F1 J: B: h5 ?% p; P- ]4 ^+ P2 G3 W' e

% P9 }4 n6 M- i4 o4 j7 K0 u$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
* _7 d8 X% X! L5 Q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
  M9 _3 d. S& r2 n8 C6 H$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf; ?* K$ N, }6 j$ H
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf* P! C; i3 O) A1 c0 Y8 N/ L6 a& G# |" F( Z
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
2 P/ U# l! H8 R% |' u: V: X2 d2 O  U: @

' y1 k: H. ]  F+ e9 ?8 t6 N启用ModSecurity模块和CRS规则
) v5 ~! }' j. r* f1 @复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: l. R. W7 u/ m9 d! g. H* d

% A$ K( s% q7 D% C, Y: n2 L9 {' I% R6 H
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。2 Z/ C2 q4 R5 ]$ p7 H
* L/ p( i0 ~; v  t( w
7 W5 F, V' _- F5 w/ m: Q6 K/ M
$ cd /root/modsecurity-2.9.1/
& p7 B* u: O- n4 e7 R4 Q/ ]$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  & a/ Q' ]2 U- \7 m% q3 z
$ cp unicode.mapping  /usr/local/nginx/conf/. Z4 z0 ]4 C8 {5 J
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 j" L2 v( n# K4 ^) }% ~3 Z
# k2 V( j4 S( [  W6 `

% n; h1 u3 ^+ z$ vim /usr/local/nginx/conf/modsecurity.conf. e! O  R$ C0 V1 a9 S
SecRuleEngine On
8 L6 g& x' [6 A( G4 jModSecurity中几个常用配置说明:
8 Y) S; Z& H+ x1 M* s. K0 U# z6 m
$ \# {! R* W- K: D& y
9 b3 j2 S5 ]8 X2 n- T0 j1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
. @& C0 p5 L: O+ L. y3 L8 F; R
  }. m) b5 N- g: z: F
2 i$ b$ W# m2 w( i! c4 p- w2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。3 j' K; }- [7 Z# U) F" z8 G! V
$ F7 H; O, o) h  U/ ?
( \" w- u' a) h+ }9 M! m
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。) y, y: `- o/ w9 f
- U1 ]8 W$ G! x5 ~0 j$ R

: e% x# B5 I. d! V( E( L$ \4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。6 s2 A6 A) O: j2 b- l5 J- E
' m9 F! o) U- [
) \$ w8 X; w* E5 }6 N7 m9 X
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。0 J7 [- ]& u3 T0 N& n# r, c
+ v# g5 _, D8 k  l
2 j2 F; {3 r* k+ {/ _# e; c
3.x版本CRS
: ]1 L& P" R' ]$ cd /usr/local/nginx/conf/owasp-modsecurity-crs2 e/ ~* {. `5 G& O( o# M6 O  Z* Q( a7 B
# 生成例外排除请求的配置文件
4 D- @$ d% I# Q! H2 \! V) K$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
- D( t# Z# a- @4 \0 P9 o$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
2 r" o- N0 V. r" r8 Z; {$ cp rules/*.data /usr/local/nginx/conf
; Z7 ^- n- h" m) w6 R3 b( o2 u1 x为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。' o6 ^) l5 F: B

, z6 r, A# _+ m/ O$ @& {1 G! k6 n0 o4 e0 W# m9 @: I( G+ V
$ vim /usr/local/nginx/conf/modsec_includes.conf
0 L( G/ Y. {7 u! l! R* u/ H7 _: [. r& T- J4 D4 q& U
[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
: h' W" Z% j8 Z0 d: r, L; O- J0 k

' m: C- C: |) D2 ~( E+ p. `注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
0 Q# P6 U8 }. ^, V: o0 m. g
* c0 @4 N; O% V  b8 F$ I, N: {7 @7 A7 X# u. w
配置Nginx支持Modsecurity
& _; ~. G" B- z% U. z启用Modsecurity  Y% k. U4 I: J  z- O( [& c+ A
使用静态模块加载的配置方法
# s% }( m+ j8 |, S4 `在需要启用Modsecurity的主机的location下面加入下面两行即可:
* \. T5 g+ Q/ |
# ?# r$ L1 A3 I0 b. \" e! {
* c5 T+ \0 \7 q7 }+ hModSecurityEnabled on;/ _) H( h3 b+ {# N* h9 z7 W
ModSecurityConfig modsec_includes.conf;
, a1 e& l" j2 c( J( j+ M/ Z9 F/ Y$ a修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。! P" U6 {) T' P# ?! e" H4 \
. ?! e0 u/ V7 \) _+ l5 X# V" |7 ]

& ]4 |# q7 Q+ L" B$ vim /usr/local/nginx/conf/nginx.conf9 u+ r# j0 m  A* Y! l
$ i' D9 j* G2 {6 ~0 [
! l# `5 q/ e% T. ?! F6 ~
server {# `# |& C6 r9 [$ U' _; B
  listen       80;
( v# O4 W% {: c( c$ i0 B0 g  server_name  example.com;2 x' W  g& L; w& p
4 g; F& ]1 R9 M8 \7 j3 Y% u8 G
: R6 B0 Q* i( `7 h( ~; X+ F
  location / {
$ k1 l6 }" I. v3 j( n5 f    ModSecurityEnabled on;
" \* h9 T3 z/ I2 k    ModSecurityConfig modsec_includes.conf;% A, Y1 C3 ]& L6 l7 e
    root   html;
- q" E+ e, q7 n3 m. T- I    index  index.html index.htm;
# V$ c1 T) N# d) p' X7 O4 W; g  }1 S( m) w# _2 j: t* j
}( E- @' V- N) |9 d8 l9 G
使用动态模块加载的配置方法* v5 T1 m4 J( J3 @
在需要启用Modsecurity的主机的location下面加入下面两行即可:
/ P" G! A  w2 a) T8 P- S5 }6 R- W& }2 U

- n7 p* O+ g2 J' Rmodsecurity on;+ R! k2 t) v8 E, d' w; v6 p
modsecurity_rules_file modsec_includes.conf;
7 y, P, ]; \" M0 Y  A0 U% h修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。$ N+ R9 {2 W" w, q/ W/ r# e% X

- j8 V# K) B" W# g  ?
9 Q' Z* z  S1 s0 M" s# Q$ vim /usr/local/nginx/conf/nginx.conf
6 I2 [4 {: L: ~2 ~/ O' z5 @! {
. |8 o# P) j) Z/ X
7 _& u2 `6 n0 j% U0 u9 n  i; Fserver {
. _3 u" i/ I7 V  listen  80;
. j8 t" Y; M/ o! e+ I  server_name localhost mike.hi-linux.com;
& G4 @- }/ U6 \& o9 z  access_log /var/log/nginx/yourdomain.log;8 E' f2 ~5 v0 A

5 f. P4 v" s9 v7 F3 O
4 K0 Q5 c2 o  n1 V  location / {' Q) A2 u% N' H! }1 b" p! ^

% C+ J: N9 O0 ^4 W6 z' o# o1 v/ [/ E( q3 a
  modsecurity on;) [7 b% H0 u# p0 k* u3 Q+ T
  modsecurity_rules_file modsec_includes.conf;$ B/ r4 n) k4 v  r$ a
  root   html;. K! L, K3 D& c. j
  index  index.html index.htm;
* u& J1 ?& z" W+ A. ?1 A. i}! Q% B6 N2 c( y3 Z& B+ b
}
& O. M9 \* }6 a2 G& Z验证Nginx配置文件
  a, L+ b8 c  H; Y: e+ }3 s$ /usr/local/nginx/sbin/nginx -t
; p0 c+ ^: {: t  q( j, l+ D8 E; F+ Hnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok0 a& {" }* x7 P( n
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
$ P1 ?) }# g' \9 d0 f  Z! `启动Nginx' a; @4 U" ?. r
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
3 C8 C; m2 w4 j$ W) P2 h" w, m$ y8 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能够战胜更多复杂/未知/混淆的攻击模式。


6 \/ u: W* q7 T2 a7 p' e- b" I4 l/ w

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-14 04:45 , Processed in 0.074075 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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