找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11375|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
1 {: \1 n& g" R- H; I: J
1 C+ ~. O& R, Z1 j8 P* }* H
# p* H3 @* J( p/ ^在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
5 W; g1 b' X0 Y6 m0 L
# u* Z( e  X: r" E! Z4 p2 z: Q
( n, R9 Z$ F' V: M1 {/ p什么是ModSecurity& J! {0 h" q8 J+ l4 q" m8 ^5 y
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。. l: k" j: y3 _

' p: ]: m7 z! p4 ?. ~* y1 O
  [( O" ]7 i  B; o, Q& aModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。/ w7 Y% O* q( N( K& \" Q
4 S+ [# S% a, B
3 L+ m1 r9 A9 y
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
; x! {- E7 O' e# {: i4 d# q
( S. x! _" n, K
, k2 ^/ s6 W, t3 t% l# U! t9 j官网: https://www.modsecurity.org/
" y+ t! V  d; Q& D$ k8 c$ q; z( x; ~2 ]7 ~) o

( O/ P% g+ h- {# t什么是OWASP CRS
+ J# t1 F8 Y/ i  qOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
( R( R* G7 W. E
' }8 j) m; d; F2 e7 q( {0 S9 n6 X' M$ Z1 n# S4 t
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 n, B2 L5 {' i# F' O7 a6 L$ j4 `3 V6 ~. k: P, n/ H; ^" J" H$ }
0 Y7 q5 }* [2 S  F3 }
HTTP Protection(HTTP防御)" s4 R: `% O/ s0 E) I3 M
HTTP协议和本地定义使用的detectsviolations策略。
: p$ g8 ]6 T) o' a4 Y; ^/ w( f' a1 Z. b' F8 E  F

2 r/ r$ v1 m; J, a0 C3 E, VReal-time Blacklist Lookups(实时黑名单查询)
! u9 Q  F: H" c% ]! V利用第三方IP名单。
4 G$ f/ X; _8 J, E8 d) C1 T8 J% g9 ?& O3 \/ n) z) j

' B7 o; p% D( g  o; uHTTP Denial of Service Protections(HTTP的拒绝服务保护)* N( r) _9 M; q
防御HTTP的洪水攻击和HTTP Dos攻击。4 ~! m0 o$ h) E8 l
) z1 n: ?5 V+ \) Z

. P0 e' C4 R5 n* e8 h3 QCommon Web Attacks Protection(常见的Web攻击防护)2 _: Q, d. J0 b2 R% N
检测常见的Web应用程序的安全攻击。/ {' k  R' p: K+ Q6 w& C6 s

7 v% x3 D. W3 E8 `2 H0 _
- _5 I( k& U9 {6 N7 k; FAutomation Detection(自动化检测)
+ B/ ^- v; m& b0 |: U检测机器人,爬虫,扫描仪和其他表面恶意活动。
$ \+ i  e& M# [# }8 u3 p6 S, c
1 u$ D1 O  S6 Y) D, |  ^$ U7 ]; v& m  x9 x& J
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)2 s! h0 B2 Q  @
检测通过Web应用程序上传的恶意文件。
5 o* n6 f) b0 @) E1 q; A* g$ z6 |# T7 i
2 X5 f& F' `( w7 H4 E
Tracking Sensitive Data(跟踪敏感数据)* F/ \7 c& ^; ~; R
信用卡通道的使用,并阻止泄漏。
3 G9 L" C' a% j' {( S0 u. _  d9 Z9 Y* X" V) Z4 e6 g; L

8 w: I% F- |7 U9 ?! F& N  A$ kTrojan Protection(木马防护)
7 M3 e/ l9 A( @6 K$ X检测访问木马。$ y: S& I" v2 R+ B* H

) z8 H- G5 k9 _& K5 x" A* N3 @0 c' @' @9 ?' B; u/ `
Identification of Application Defects(应用程序缺陷的鉴定)( \2 u: @  U9 }) E& k
检测应用程序的错误配置警报。0 o: l% T7 j- l0 w$ w& y
) ?4 w3 k! o( G. W

, _/ y% E# i, J; W8 Y+ fError Detection and Hiding(错误检测和隐藏)
$ X3 f7 e  ~+ I/ B! M$ ~: S+ b检测伪装服务器发送错误消息。
& Z  B1 O; [, Y+ q! K0 R" x* x5 f: f4 o; x# S9 n* |
& ^2 ^* u2 d) |. o# D
安装ModSecurity- @  G1 z: t- U3 ^2 K- R
软件基础环境准备% L3 G! ]& R: @
下载对应软件包$ S3 k, t5 L( f: u
$ cd /root5 f; p4 @5 u$ H! l0 `$ g" j
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'7 T5 ?( i6 e( J
$ 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. h; G1 }9 l8 g3 O6 U安装Nginx和ModSecurity依赖包
( g( R" J) d: T/ i/ I  l1 bCentos/RHEL' R/ k1 X# L8 _

3 Z1 I$ v4 L: N5 B% k2 h8 }7 X$ E. n  J4 l6 L0 @7 q* \
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
) n5 q& o2 G* ~4 zUbuntu/Debian4 I- y' d' q& S4 f/ `7 I

3 D" k% N- l; ^' \* u
( K! E* X( q5 s6 ~$ 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" T) ~, ?9 C' K9 h" x/ _+ Q' K' g
编译安装ModSecurity
) _$ B7 D: K; Y6 I  K5 \# x. ENginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。  Y, E8 [6 @3 L& c) U) Y7 P
) z: x% [/ I6 [1 w( A/ T8 {

  h% M: T6 M& L" Z方法一:编译为Nginx静态模块
* m/ @- z: }7 L2 B! I; [( k7 n0 G; F+ l; ^$ x# U  \: }$ H

* v! _) g" ^, D2 P/ o9 n1 r8 T, Q编译为独立模块(modsecurity-2.9.1)  n8 `& w8 |& z% h7 q1 U  g+ T9 T
$ tar xzvf modsecurity-2.9.1.tar.gz
+ M! |2 S8 f, I0 L: Y8 ~! N$ cd modsecurity-2.9.1/) j% M1 L; U* k9 U& {& B! c7 m
$ ./autogen.sh5 n- S, I3 H  `! y# e0 e  R
$ ./configure --enable-standalone-module --disable-mlogc+ Z! E3 M" u$ d5 T4 K( y" e
$ make; h$ l2 L, y7 X
编译安装Nginx并添加ModSecurity模块
0 a  `0 D! O9 W; J. A$ tar xzvf nginx-1.9.2.tar.gz" h3 t! r5 x+ V# ]5 E  H7 l
$ cd nginx-1.9.2: F+ g# Z" h" T& f: V4 E0 g
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 [) I, e: D# X7 u+ E( `
$ make && make install* l" T; x$ \2 s3 M4 l
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块4 n6 y/ `" F* F' d% S
2 b0 k9 R3 p0 x9 ]* u. U0 z

9 x  u4 L& _' P+ j+ Q; N编译LibModSecurity(modsecurity-3.0)
' ]6 r& A  R3 `. }  U) t/ R: u9 `$ cd /root, s' h! `9 ]9 F9 g- f; G8 r' L
$ git clone https://github.com/SpiderLabs/ModSecurity4 x, i, B) T& n3 n& F3 x8 }
$ cd ModSecurity% Q+ x4 V$ j" ^/ B- _; M
$ git checkout -b v3/master origin/v3/master
. K9 \$ c, b# J+ D3 Z. n$ sh build.sh
0 B& u# A3 [  O  `7 N$ git submodule init
7 z8 N. }8 `! h6 g  W; v1 a$ git submodule update/ Q+ v; N% ?9 ~! L, N" {
$ ./configure% s2 }0 Y- k8 d9 F# B7 u
$ make
/ i( q6 _: \8 W* I9 x$ make install8 v& E2 J2 n  ~5 ?+ U( @
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。0 N$ I7 e' U" g; F& i$ `

  u8 o, \& u! k/ W/ n- z" w, @
" @6 X/ z/ Y* F/ s$ ls /usr/local/modsecurity/lib
) \$ a! g; d3 M% u# I% l; C3 jlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
- z" i6 d4 z" w' M% R+ h3 m9 F编译安装Nginx并添加ModSecurity-Nginx Connector模块
; T& N* c9 y8 y; N- l) G3 l/ y- A# o使用ModSecurity-Nginx模块来连接LibModSecurity8 V5 r, L* }+ P2 d
# [0 l8 S2 |( ~( y
0 T& V5 d5 @6 \! d
$ cd /root
6 v: t  ~1 ^8 [6 E1 s: f7 }# S9 q$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx; {: ~. E2 X* Y& Z1 f# [
$ tar xzvf nginx-1.9.2.tar.gz
. A6 I8 r) B8 `6 O- I$ cd nginx-1.9.20 H' w8 u. S: m% f
$ ./configure --add-module=/root/modsecurity-nginx4 u' f' \: f: T9 N0 f
$ make# M- d: @' L. o' x$ T3 u3 S' m( J5 k
$ make && make install
7 U# r) _  Y9 ?& J* b添加OWASP规则
$ _0 V. I. r* Y) t" G" LModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
1 t8 Q& v* p8 c% q, S
+ E$ l# n4 z8 H+ r/ Y. A+ G9 S& h' Z' H' ~1 d/ S8 C
下载OWASP规则并生成配置文件# O! z" |0 \' p
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
/ `: Z' ~! x* B3 O$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/. O: i* Y) s  R- o7 q0 V
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
: |! r9 }& A' p& L$ d) e4 T3 P$ cp crs-setup.conf.example  crs-setup.conf" C" I2 z6 D( x" \. N. E( ~4 F3 q1 \: l
配置OWASP规则
! w( t: H  c' Z' Y7 i# N) M& t编辑crs-setup.conf文件9 ]$ u, H7 Y. Q+ P

: i8 Q% k" @$ n$ v6 F
. ?/ P( a! x9 E& [9 l5 k$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf4 y0 s4 e2 o5 `+ g" @
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
( b1 n6 r. C: k& }* s$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- @' u3 T, ~) A* ^1 i& ~5 w
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
8 y" J. U% N) U6 u& q# o2 L默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。" [* V* e1 F* O5 `

# f5 P3 e& Z, i6 ?+ Z" `) r( H9 ~/ \6 n, {$ r2 s5 K
启用ModSecurity模块和CRS规则
( ^$ Q( b& _' c复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。1 l1 X0 w2 W  @" g( A7 `
( t8 E6 n, E8 W; `# O/ v

; K3 W9 X* o& ~; K/ B; o; ~; tmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 K; c! g0 u6 {7 J- E" W7 Z8 W! y
5 b+ ?* O! D3 b' C$ ~' O; d; l* _3 v/ W/ H6 }8 o
$ cd /root/modsecurity-2.9.1/
; ~. w( s- I: l8 }3 L$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
% J; p! @3 W$ O* S3 e7 T2 w$ cp unicode.mapping  /usr/local/nginx/conf/6 }( Y, ?+ o" P, h0 X8 d) K
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
! `4 V5 _1 D$ l9 I) s$ T! _0 M. f. u3 K# w$ ?  q# U) Y
  k* w2 h4 G# u" u6 b8 C
$ vim /usr/local/nginx/conf/modsecurity.conf
& B- b9 j" w6 u) B7 _- b7 C6 A8 }SecRuleEngine On' \, n, ^$ t. p2 U5 A
ModSecurity中几个常用配置说明:0 z& a, e8 B0 c1 F: g4 Z: O

5 Y- Z) h2 v7 @$ K$ E; U# E% t) z7 a& ]2 v2 T9 c) Q" q
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
! p( N4 [' g2 t) X: F* H- _+ _4 c+ n) g7 r
: t/ j& S$ D; V' g- g1 M2 G; a
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
9 j" Y, {9 ?8 I/ O& ]( m; ^' W) `! B7 S

- m2 [; D. ^$ {' B' ?7 p3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
+ a+ [  r( l; M* J/ Q- Z' `; L
+ Q! F8 o; W2 I5 v$ a' o9 q6 P* U3 ?) ?
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。% i0 S# e- q  i% y1 ?
( o  X, v/ r7 v  i% y. C% f

, b: i  z2 U: g8 b. n在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 p! u- i* B- g; _

+ x% b- w! H. u; m1 p% d$ ^6 S+ ~" A: ?0 R
3.x版本CRS8 |+ c& u6 ~, h+ O# p- E$ J+ d7 h. a
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
; Q9 E$ O5 \0 y( Z# 生成例外排除请求的配置文件; z$ h$ o0 v  x2 o, B: N1 _
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
3 g# D8 S0 I! t4 l$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf( B, @- w' m; P
$ cp rules/*.data /usr/local/nginx/conf
" A! C" e9 [& [& M为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。7 d' q, _" v- K; x$ z
& Q& Q' l6 P0 O

; F, _0 z4 V8 `8 X$ vim /usr/local/nginx/conf/modsec_includes.conf
# F; t3 l2 U' M# e+ |" ~
; y( u& V2 x6 f$ J7 w# z( i
[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
  ^2 L' X+ n( Q) c( p& l( g7 ^3 G
4 L" Z7 q6 @" t! }0 W9 m: J  Y
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
1 B3 ^$ d8 C. G* T
$ V6 r/ P( J0 ]: k8 d, I, E* ^- p, A  q4 p# m: F- i) r
配置Nginx支持Modsecurity4 B2 R* |# G8 c, K+ u6 V7 _% J. o
启用Modsecurity, s+ @. p/ q: E& c7 n; e; [
使用静态模块加载的配置方法
# F& A0 Z3 y' u- Z7 ]1 }9 R在需要启用Modsecurity的主机的location下面加入下面两行即可:
, C/ \4 [  L: C& H! o7 W
2 l% L6 [" j3 J5 B2 O
) b$ Y( m2 f' r. s2 _2 E0 |ModSecurityEnabled on;: H, i3 J- Y) c- @& k6 ~
ModSecurityConfig modsec_includes.conf;' _% Z# ^4 F0 B) D2 R4 A- j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 }" Y: k3 h, q  L* K
0 w5 u. E7 z, d# H) C9 B: d1 D% m
0 V6 L+ P/ ^( x/ C
$ vim /usr/local/nginx/conf/nginx.conf$ K! v' B/ A8 Q3 c: Q8 X6 p

2 |& K' R7 {& U4 |/ {# U- f, X. \! O9 V! A- Z7 T2 I
server {; z5 W+ E# `& ], b' U: m2 T
  listen       80;
' Y) ?# m" h$ L1 p; u  server_name  example.com;
( f$ J; Z0 f. d! N( F
6 u+ E" P* j) E7 u" v( f' _
1 O3 m  v7 o+ V! r6 W& Y% ?8 C6 b% S  location / {. {1 e, j3 I- o: }; w( |
    ModSecurityEnabled on;
2 i9 _% a& A2 w/ Y. Y    ModSecurityConfig modsec_includes.conf;
- r( l8 S1 Z' Z& Y" a! d    root   html;4 n8 |8 u) v* }6 \8 R; _
    index  index.html index.htm;
" N( E& Q( A, p( a4 V6 M9 ]  }+ U  V6 e% \( T* s, _3 E9 d, W( a
}
2 ?3 k  j. f9 h, D. [0 s" _# ^使用动态模块加载的配置方法: {  L, N. D) P5 x% }# [
在需要启用Modsecurity的主机的location下面加入下面两行即可:
' t, V0 J9 \4 z4 H- {; Y) J' ?3 [" T/ \: q
; r5 d  Q* O  l: |! C
modsecurity on;- ^+ ^: s3 h2 o  i
modsecurity_rules_file modsec_includes.conf;
+ x$ O  M) S- o- D修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。3 T: }' L. J# H' `, O

: K+ b6 Y* F  k4 U, F- i3 f! C; D2 l7 N% H/ ?
$ vim /usr/local/nginx/conf/nginx.conf
+ [+ M. U! W" n" H, Q% G& B# y
& U+ g3 V* V1 J
! I, D( u. U# T5 |- g" n+ s+ eserver {
* T+ j* e' L! E0 D  listen  80;
9 n; a' `) Q' }  server_name localhost mike.hi-linux.com;7 N% E* u. B$ U) a  X% s
  access_log /var/log/nginx/yourdomain.log;
" W) J% v) J) x4 F, `
% `2 ^3 |# W; U* {: ]
, X& E  i# V7 b! C0 w# }  location / {5 q  m! x3 E+ a9 s
( T, |/ ~1 c9 ]1 Z% x6 f
4 m$ r: N  N) C$ H+ u9 X) x" m# n
  modsecurity on;
2 U: T1 R, A6 b! a( P  modsecurity_rules_file modsec_includes.conf;
: l7 I  `1 X- _" ~  root   html;# V- q3 d( t; H: y3 T6 d" A7 C
  index  index.html index.htm;. Q; a" ?* p4 l5 Q
}+ X7 J* J9 W- h' G4 A
}
' U5 p% o* T  h6 n验证Nginx配置文件
6 `/ U+ K& Y, e( k5 P3 [$ /usr/local/nginx/sbin/nginx -t
: r6 f. S% r6 Xnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok% s2 T$ `8 Y! l- E: S
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful( A* j" y5 W6 M) B7 V1 |
启动Nginx+ a  h/ X' S6 U  T% Q
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 m7 K. \7 v  y
1 Y' ~- J  p' n/ n! _4 Q5 A  W) v

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

! N7 `8 G. }2 e4 V

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-5 13:11 , Processed in 0.088872 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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