找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10413|回复: 0

利用ModSecurity在Nginx上构建WAF

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

& X3 U, o! e( ^. y, X2 `# `/ ~3 S
+ F7 O% v9 V* E, n在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
; g  t4 Z" e' j0 ~6 z0 Y& L
# o+ I  R( T, W7 A9 R  ]' s( E
7 B' c) M- a& d6 n什么是ModSecurity
! U3 p' n. G. a& z5 R' DModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。% n: A; O) E9 ]/ j! F9 {9 L* q

" A3 U0 O4 q6 l- @( ]8 K6 K: u+ r
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。, \* x0 w5 }% @! g! `3 |0 R

$ f3 z) q- T4 j( h0 E) J2 l1 a2 W4 U- P& C! ?) M
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。/ m9 ]: U. t( p+ V  H3 ~
6 f1 m4 p! i0 a( l; w

8 U! N! C7 \' z2 O6 o! _官网: https://www.modsecurity.org/0 K4 T3 ^. ?1 Q9 |  T5 c6 H' x
/ g5 d, t* E* G7 h% Y

0 Q8 y) S5 l5 x: q) [) t3 t, ]什么是OWASP CRS
/ m8 `! R0 ]2 B0 X; U) \7 XOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% J  x1 [! ]4 v( M- _5 A$ h$ J! ~$ N9 ^
, b9 w, s& ]/ d
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 O+ p/ Q, ]6 L) M0 a% x6 Q# M

1 t3 r( k$ q7 j: l: G4 D! m4 p
HTTP Protection(HTTP防御)1 ^8 s" I' w  K2 @( I$ k5 _& H7 w
HTTP协议和本地定义使用的detectsviolations策略。# c6 h2 N2 ]4 @1 C4 Z3 s0 {

; f& g% o* K' G. q0 _
7 f4 k; p4 @2 R- x, ]Real-time Blacklist Lookups(实时黑名单查询)
  @5 Z# ]4 g+ d利用第三方IP名单。  n$ m* c6 F4 k" t6 X3 [' f5 c" Z' q

. X" _  f! X( c/ L! P, |" U: _( \3 F: r, b2 x9 F4 K& j
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
, ^: W' N! L! f$ i3 v防御HTTP的洪水攻击和HTTP Dos攻击。
4 l* Z, g1 M8 I  J5 M3 G; n% T; X4 B5 N$ ~. _3 f' q0 [

# K# ]* q- a& kCommon Web Attacks Protection(常见的Web攻击防护)2 ~: t/ \0 u2 o; a& T( ?6 r
检测常见的Web应用程序的安全攻击。  d$ S1 M! s: @( Q5 f

0 P. ^8 ^/ y( t7 v9 f: D7 q1 C8 t4 ~
Automation Detection(自动化检测)
; ^* B0 s$ [0 m. ~检测机器人,爬虫,扫描仪和其他表面恶意活动。
1 k* J& `. D; T& [4 y$ j( U$ I
: N8 m6 S+ b- i$ f2 e; Q) M! h1 O( j# W. d" S8 W9 ^- ^" A
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
' l2 V  D" ^  h' ?检测通过Web应用程序上传的恶意文件。
% K, d' J. l5 a; z- P  F6 G# r) C) l$ [: Q

) B6 T- q) F" ~! N8 F7 w& \. GTracking Sensitive Data(跟踪敏感数据)* w2 A1 W2 r& I5 g+ |; p- u# x
信用卡通道的使用,并阻止泄漏。5 G9 q- y% Y1 J9 a

; D+ `& c, v- W+ I( X9 H. |
7 C: Q/ z' S2 V1 U' E' ~6 jTrojan Protection(木马防护); Z, K  w8 I1 \9 M9 q
检测访问木马。- C: C/ _' f# M' n9 X
& ~3 p/ l1 s. H7 v! @( q

: I+ d7 D% X# x1 O& g* L% f# b* cIdentification of Application Defects(应用程序缺陷的鉴定)* R. h# h  q) V/ s$ [$ q! W
检测应用程序的错误配置警报。
; A8 M( E- g5 {; \  O9 i: m9 s. ]- T, ?# h1 C

3 X0 }+ D) l2 {% c- l; UError Detection and Hiding(错误检测和隐藏)1 G) l+ |/ j8 T+ j1 I: x$ Y
检测伪装服务器发送错误消息。' A- m- `' u2 P% j' w7 C5 B

' q8 e. X# v0 f
" [9 H3 c: T5 @: D8 j7 p& J( _安装ModSecurity
  e. s3 d7 F9 F% W8 Q( H* D; ?软件基础环境准备6 S7 V& i& N# U1 s( ^. c
下载对应软件包
! w( I2 b; k8 b9 r) W$ cd /root" F( q) f9 G0 h2 l' Y+ A- D1 T, u
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'/ t. M: ^- H; ]% a' p9 n1 S1 M7 L
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz1 |- {: w* w- k/ h/ C
安装Nginx和ModSecurity依赖包
4 E' ]0 q4 s$ \$ Z# U( r0 `Centos/RHEL
0 q$ k4 i& u6 }  A" l# b3 S, ?) q( U. r- T; V$ l5 Y
7 m9 [& N  Q( i( B2 Q
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel3 g! {/ P# e; x
Ubuntu/Debian
, q& l  ~5 C" Q' E8 p- }+ C; C9 `, l9 u* L

' V0 [' }: K, ]& e  ]$ 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  H5 @2 m& |# Y( G
编译安装ModSecurity
" N: V0 U# S( T2 UNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。5 y" }+ U0 A- C0 L- H- S  g  a
# S  O  }; l/ M! J; B1 |& H
; @" x+ b4 \  q& I" ~
方法一:编译为Nginx静态模块
$ h+ |1 I/ F4 S. w9 O. P: L* H, I* H. o7 q3 F5 n- C) }* {& x+ P
2 P8 V8 t1 F$ Q8 [
编译为独立模块(modsecurity-2.9.1)
# _$ Y( _9 Y* O8 c6 H! l$ tar xzvf modsecurity-2.9.1.tar.gz
& t' X* S, Q% J+ Q$ cd modsecurity-2.9.1/" P7 p- p! @2 L( X
$ ./autogen.sh3 c6 i1 w" H+ C% V0 f" J9 X' n
$ ./configure --enable-standalone-module --disable-mlogc
. j9 A/ x9 ^7 g- L7 P6 @' D2 S4 b$ make
5 n, n! |$ e# r8 |$ Y" Z编译安装Nginx并添加ModSecurity模块! p6 |0 H  Z  z* J
$ tar xzvf nginx-1.9.2.tar.gz
9 ~0 A4 d  h/ M# k- g8 N9 G$ cd nginx-1.9.27 }! f5 C" e- k8 s7 E
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/. @- _( m* x% \/ s  P' B
$ make && make install
  E+ Y; w' _) [! ]; U3 J/ h方法二:编译通过ModSecurity-Nginx Connector加载的动态模块6 X- h! [& m; _
" V0 ^5 J; S5 q* c% o+ z
2 K( L* v. S+ C# b0 f" `# W
编译LibModSecurity(modsecurity-3.0)9 v0 c. d% z9 W  ^1 X0 c7 Y
$ cd /root& c1 x0 q' ^& _  E
$ git clone https://github.com/SpiderLabs/ModSecurity" |/ G. x7 O7 V3 T* x2 V
$ cd ModSecurity2 Y, Z6 ]: Z$ T9 Y! B
$ git checkout -b v3/master origin/v3/master7 ]3 M8 P+ A6 D& N8 Q
$ sh build.sh2 K6 r& N/ W9 u. f
$ git submodule init
4 \' \. ?! b$ E/ E: o: ]$ git submodule update
3 u) f+ \' |0 _6 |4 F$ K9 Q$ ./configure7 q4 C3 @5 p( K2 b7 X. f& h
$ make' d7 [& K& z/ ?8 x
$ make install
, Y! {& \, \3 [; L9 W7 y" iLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。& B0 T9 |0 E: u

( V1 n" j& _( I2 E7 K6 T4 ^' M: n- Z# h' ^8 L7 A
$ ls /usr/local/modsecurity/lib
2 v8 Z/ O! q9 S0 R% e3 @6 Blibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.02 I! F$ W% U$ G( D3 N2 }
编译安装Nginx并添加ModSecurity-Nginx Connector模块
" K- O' |5 l! [  R2 t: ~使用ModSecurity-Nginx模块来连接LibModSecurity% i9 t1 S9 G1 v4 R" k1 n
! S: Y# r: k- E, ?3 _; T$ c, @
4 h( `0 W1 F+ ~" r' |) L+ R7 O
$ cd /root
( v) L3 z. q/ v" o" Q  X0 S! e$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
+ [9 L6 ]) m+ l% N6 j- }$ tar xzvf nginx-1.9.2.tar.gz! w7 f+ V5 c7 F" b7 ~! A
$ cd nginx-1.9.2
9 m4 @5 P( c  e( u1 k- J$ ./configure --add-module=/root/modsecurity-nginx
! r. p: v+ i% b/ ]% B" ^/ h$ make+ x; j' y7 Y. ?% i9 o" F1 Q) Y8 ?
$ make && make install
/ }7 O0 E; e" ~2 Z0 c- ?8 S) g添加OWASP规则
( h9 A3 @' U4 L) C" n/ ?ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
& y0 E* }9 [: ]% I* x8 J* K2 @" J, c# _( n: x
* G: d8 `( O$ I4 |, G, y- Q
下载OWASP规则并生成配置文件7 k5 ]/ O; T! N# c- n3 Z
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
6 L$ `, f8 A$ A& _. J; ]6 |8 e$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/5 z9 r6 r  `' k) x) }$ c$ `1 K
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 Z0 r. }0 `. c$ x3 M
$ cp crs-setup.conf.example  crs-setup.conf& ?$ y$ @# n5 n4 D& P6 ^4 s" k% f4 R
配置OWASP规则
. \6 q8 f; Z4 w( U3 K! j编辑crs-setup.conf文件
7 ^  C4 K+ w0 Q* X# C$ {' T
$ Y8 \& }* `9 C( K9 v: ]0 y( ]3 j9 e& j0 f& H6 R: R( d
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf3 C! M* S0 Z) V7 r
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
0 h3 \9 B2 i2 K$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf6 r' V7 ^/ v6 A
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf) u3 D, p! J( i! s! p
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
* A+ }5 O2 n& q# p0 r1 L  r6 x' E0 t" ]; L' g3 Y: G! H' v
5 A  F) h) r/ c5 {7 D4 r
启用ModSecurity模块和CRS规则& ^! L4 w( F0 @5 H9 m7 e7 b4 |  m
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' q& I  @: ^; G+ O' d- }  z9 s- ~1 n' i, [& {

  M' {" n# F" V0 @- ~modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。& k$ ]. _) P. S! _8 a: E" X

1 [& ~9 c, O4 l! B! u8 n  q$ M- V& h5 t$ j, ^% U. V# H" x
$ cd /root/modsecurity-2.9.1/
0 c" U  k. @' w$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
; Y" c/ m  I( L. m1 ^7 h; v$ cp unicode.mapping  /usr/local/nginx/conf/
7 e9 k0 a" ?. d& u0 B5 e" N; p将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 s& ]- P/ T4 a4 f

) R. U# N* b8 t1 r0 [; o% r" {* |* ]; {8 y0 x
$ vim /usr/local/nginx/conf/modsecurity.conf
/ p6 r# o% i) {1 gSecRuleEngine On$ q  J4 U0 H) [5 D$ t/ q
ModSecurity中几个常用配置说明:
! w* a1 F+ l/ d" l; b; G1 w& r  q3 [5 W: v% k

5 @- U; F# h! Z- ^6 w1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
+ ~2 i; x' i' k1 n$ ^# l4 F( M: y. C! W" d( K

' ], o% Q* Q! K# C& Q. E2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
) B1 n! s+ i: V3 g! o
" [8 ~9 R: v! l9 L" j
( b: a- |9 E3 D6 p3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。, ~: g5 m4 ^+ p, f; H4 ~
2 u8 T" m4 j/ V# G5 k
6 Z4 {# {3 C( l! e* `: `( Q6 q
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* `% `7 e' R" ~7 c8 M) @: W- x- t  {- W

  H4 X. G. v1 G; n5 L# a) L在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。  z! X8 W6 w* K( h( r) t/ @4 ^

( l# s( B: h0 Y" f) ^
7 v* A5 v' S% [$ Y1 Y0 `3.x版本CRS- ?4 C) j2 o) T; u0 B' `- w2 G
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 z* A" E( f" t) z% `8 F+ b- K* w
# 生成例外排除请求的配置文件; ]$ d( |" \4 t! Y+ ^
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
5 {3 U: A4 S4 c& z0 ^0 H5 a) p$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
  y/ s; N1 }! V/ ]0 E6 ^, p$ cp rules/*.data /usr/local/nginx/conf
% G" A; ~% g& y6 c+ F为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。; X& ^* L5 }# n* R4 X+ ^
2 t) Z* G, G: O9 `7 `
( \2 U9 D% Q, R+ Z; d5 Z/ P
$ vim /usr/local/nginx/conf/modsec_includes.conf
% P' p( L8 H" F$ l, ~6 d& Y' A8 K) o, Y7 q% N/ l6 Y2 g; N2 l7 x& |, v/ m  ]6 _2 O
[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
; W6 O. u4 Z5 p. F

9 d# U- \: @! K3 @注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
- W% T0 _  M3 R. u$ n( B3 L( L7 |* d$ T9 n& w) U7 S5 ]  i6 S3 K& f9 x
1 e8 V$ {& t8 ~
配置Nginx支持Modsecurity
: Z/ w, q; Z. x) o; r" @启用Modsecurity
! h% ]. T; S; M# z1 w! D; i使用静态模块加载的配置方法0 y  i1 w) x& b
在需要启用Modsecurity的主机的location下面加入下面两行即可:0 S; x+ u( ?% @) {

+ |! h+ K2 x, o% k' O8 b6 t! |
* c8 ^+ T& m' Q/ e1 |ModSecurityEnabled on;
; p0 P# Z- h7 H2 WModSecurityConfig modsec_includes.conf;2 ^0 L; o; x/ z
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
' k' k2 I" m7 \- {2 j4 E7 a
  s8 w$ i( v! _6 a$ J
! y9 C5 G- q# A) d; [. \2 X3 z$ vim /usr/local/nginx/conf/nginx.conf
! d. A+ K7 k, j. d8 m& B, Q, k1 n3 E

1 Z* m% t, C! y; L2 L  r! C  `7 A5 zserver {6 ]2 p8 T' A6 n7 j& N( B4 L) Q6 N
  listen       80;
, s' ?. _0 Z6 \; ^( T  server_name  example.com;
6 x4 j6 k, ]5 V
8 o! J. E9 W; H- p/ ^! p- B; _- O) a* @2 x( c# R$ h" T1 W1 ^0 t
  location / {
7 i0 b% K8 [1 m/ w7 J/ \    ModSecurityEnabled on;
7 I6 R( I( m- x1 U! b" _4 }    ModSecurityConfig modsec_includes.conf;
1 `% E: u3 P  W+ W: @' n    root   html;* J) Z/ Y7 c. Y0 t" p; t0 x
    index  index.html index.htm;9 ]5 o: R% |# k. q% F
  }) ~1 A1 C' v. z# P2 M2 p
}
, l# L9 y7 }) `# L( @. q% i2 O使用动态模块加载的配置方法
% {1 e; U8 u9 m: K* k5 _$ A8 i在需要启用Modsecurity的主机的location下面加入下面两行即可:
; H# j6 y( F8 ^0 p
* m$ Y( S9 f/ C7 s* w4 j7 w; \, o* h
0 M) z0 p( K; O: r) N0 f8 Bmodsecurity on;( m% l/ u2 W: Z1 [3 f
modsecurity_rules_file modsec_includes.conf;2 J/ P" T- Y1 R' q; |
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。) }, M& X) ~" U" a7 r: _3 r2 @; J
8 E1 m( J; v9 N# M" E7 `4 c
# p8 e  T: h$ D- Q
$ vim /usr/local/nginx/conf/nginx.conf% `, f/ \' u% ?6 {

, W8 r  f% H" ]& E7 s* n; Q" i6 q$ f% r6 T
server {6 ], [5 O( V9 m% n- ^/ z. x- R! h
  listen  80;3 J" h: J1 [1 s' {% q
  server_name localhost mike.hi-linux.com;; I- t. R' I4 |9 n8 F5 Q
  access_log /var/log/nginx/yourdomain.log;. U+ G$ X6 Z' r' r$ ^! Z/ r7 P/ a
7 n* v3 T6 p3 H- ~5 z8 {8 t
( D1 I" \* F( K# ?- X
  location / {
2 B" Y/ g- w% Q* b% Q
: a% ~2 w5 x8 c' F. W' X* r# f7 ^& j( B0 B" y9 \. I4 Q8 Z( G5 A
  modsecurity on;9 W8 y0 ^0 }; K+ U9 L* g$ }
  modsecurity_rules_file modsec_includes.conf;8 _( N. f2 T  h
  root   html;8 U! T" G/ Q  U# a/ ]/ T
  index  index.html index.htm;
9 v7 e/ A0 ^  c: G4 u}
. e! g) |2 E, ^4 ^$ a1 ~}( _3 m0 m( @' i' O. Y0 Q
验证Nginx配置文件" l7 R5 o/ _: C
$ /usr/local/nginx/sbin/nginx -t
9 R$ I, e8 x0 n1 @4 f, pnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
% }: x, k6 N( qnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful" j( L5 L+ ]  E0 l# a5 s
启动Nginx' Q" s! D1 F+ U# h; r' n
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 g4 m! |. i; ?
+ J8 P/ A8 H* A( p; k+ L

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

5 U. R4 l/ G6 X7 a& Q2 r5 h

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-3-3 19:29 , Processed in 0.096729 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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