找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11923|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
3 l0 ~; W' x6 Q; `9 q3 x/ f5 g$ a2 }5 J% \

" E9 Z# D& a, y. {9 X在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
' f+ n; O8 {( U) _! t: e' E& m) T8 l
, Y6 o; n2 P4 r; S; j. L# E% Q
什么是ModSecurity
6 G! l% f3 f7 v) NModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
& t# C4 f9 c0 a2 Z$ L/ ?# E; b( m- {  o: x' o, v) R% z/ ?

( S$ O/ D0 ~( n/ M4 q4 j4 wModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
( ^% x9 f  R/ V0 p: b# ^0 G3 b" C  w' M

! f6 M# e$ [, J# `. I& u. s. rModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。( r- B& \8 i( |: n
; k8 u+ F# r1 d  O
6 ~3 P3 G% ~) f/ s# K% C7 t5 [+ {- v
官网: https://www.modsecurity.org/
0 I2 ]0 r9 Q  @, e! ?" \4 }) a" G& d% c, X; H5 [' M

3 |3 a1 V* d0 ~& z什么是OWASP CRS4 P; ^6 U% ?& j4 o0 U+ V
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
  i+ R: @5 k2 V# B' A) E( ^& \) K! |9 u1 p: [
+ c" W& ]' c, [% s7 R. B: }7 G
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
* @) _+ {# _! m; v) s
# _6 U+ X9 D# j# E3 _
( z( W! E4 g$ t" D$ wHTTP Protection(HTTP防御)3 ?' B' d2 I% D; y8 U5 c
HTTP协议和本地定义使用的detectsviolations策略。
, I9 H" J3 X, Y+ U4 m3 c; ?  s/ p3 ^; H( f) @/ D/ X

3 k9 H' I6 Z* `5 y1 k$ iReal-time Blacklist Lookups(实时黑名单查询), r9 k  l, ?7 _! S+ \! Y
利用第三方IP名单。
% Y/ g) P/ |/ n6 \" u! }7 P
% O, n6 ^6 Z2 Z; ?+ e) ]+ V, _# }
) R6 u4 b& I' m  o% EHTTP Denial of Service Protections(HTTP的拒绝服务保护)6 i5 ^& q' i# W  f( i9 R) S5 E9 ]
防御HTTP的洪水攻击和HTTP Dos攻击。
# _; k; S' Q0 ?8 \
/ O" ?) D  _4 Q) `9 y" }- d
/ R5 y; x0 a9 q) K) u' |8 ]Common Web Attacks Protection(常见的Web攻击防护)4 u- f  ~: q- ?8 u& T1 i
检测常见的Web应用程序的安全攻击。
+ r) m& c* h- L+ c% e8 q& B7 p+ C, V8 g  h: S$ u/ E+ n
( @, s' z; Q9 A: p( L& X; @" B
Automation Detection(自动化检测)/ ^: g  S; z6 ?- U5 b, c; Z* m
检测机器人,爬虫,扫描仪和其他表面恶意活动。
6 U9 q! p& }& h5 i
+ T4 C6 f' Q  E
/ c* x; T& m9 D  b4 V+ y- \) ?. I% ]Integration with AV Scanning for File Uploads(文件上传防病毒扫描)7 r3 a9 O8 C8 S1 l9 t  s; m
检测通过Web应用程序上传的恶意文件。
& e/ N) I/ c- _% b8 q8 j& h3 x* U) `" h

6 u% c8 Z0 |% _* _/ @Tracking Sensitive Data(跟踪敏感数据)+ X+ S% ]9 S5 k6 t( B3 c* D
信用卡通道的使用,并阻止泄漏。
$ `0 w! Z+ G; Z8 b4 D( q* I
: {% N/ V( d# W. k/ N
8 H: Y2 Z1 o( c" b& X6 j) y8 D# |Trojan Protection(木马防护)
/ P8 p1 y* B* w+ `! R检测访问木马。
5 M& T% ]7 n+ ~+ u: F5 N( ]5 e* X+ d/ M! q. v. v
5 \1 J) L% a7 j. i0 d9 q# g
Identification of Application Defects(应用程序缺陷的鉴定)
3 s4 V/ ?- }1 _2 J  v% S检测应用程序的错误配置警报。6 H* b! d6 i5 S/ E: j6 ]) n2 |. G1 V

0 i2 T! ~: O* }) K
- \4 g1 P6 k' A2 n/ g1 ^Error Detection and Hiding(错误检测和隐藏)/ }* M  K( W& o) C4 K0 J
检测伪装服务器发送错误消息。
8 d: O) z! Z4 q! q, {$ p. v
2 U! G$ I8 H: p, g( t; g1 g9 f- S& j2 ?4 P
安装ModSecurity2 e2 [" V! R- n* @: _
软件基础环境准备
. P- {. D  \- |" R9 \8 C下载对应软件包: J* M; t- ^# Z" y7 r: d( Q. d
$ cd /root" ?- m1 r- E: i0 u' [1 x
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'- W( d* f( H2 }, Z5 ^* l
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
9 z# K* a0 U3 u' w! i% t% W0 ?安装Nginx和ModSecurity依赖包5 w6 b5 Q1 b/ E; q
Centos/RHEL9 Z- T; t% f, n/ o; C

5 S0 t( X& \9 Y& S# J. r! Y" e) W% @/ B
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel/ A$ r3 y1 c8 O* s, `( T5 \
Ubuntu/Debian
+ Q* N' J! g5 S6 [& Z9 x
: p; N" N: {9 \4 t0 t, E& t
$ B# v% r8 S4 N# h/ I7 G* n- P9 a, e% C$ 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  y6 P) n$ J0 D6 ~5 l9 h
编译安装ModSecurity% B- o8 L- ^+ H6 Q
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。0 G, e' |- e5 t3 [( ~( B6 H

' y+ i1 Q. z  l2 s) n+ H
5 V# L* h$ w6 s# c7 t方法一:编译为Nginx静态模块8 z$ Z- o# H  w; i( ]! X9 e6 A

, k4 E6 G) \/ _8 r$ Q% s( Q# Z0 Z- f' z% C# p
编译为独立模块(modsecurity-2.9.1), T" C; c6 s+ H* d
$ tar xzvf modsecurity-2.9.1.tar.gz$ q9 w) }* H) S8 X1 Z7 w
$ cd modsecurity-2.9.1/2 b4 X7 ^+ w' K1 K7 N
$ ./autogen.sh6 T  N: x+ r# a/ |) {
$ ./configure --enable-standalone-module --disable-mlogc
' {# b, J" D4 i6 x# w& p5 s4 E* C# w$ make
2 `. u7 c# \3 ~  o% O编译安装Nginx并添加ModSecurity模块
$ W* ~1 P# i% q, Q8 `$ tar xzvf nginx-1.9.2.tar.gz7 d. `; E8 e2 q! H6 Y7 Z2 H! t& c9 J1 A8 u
$ cd nginx-1.9.2. n2 d) [. u4 M& j
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/: m0 d* |2 @7 g( W3 U) w& C
$ make && make install
0 ?7 E! Y( t- n方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, L" B  r5 g' X$ o- e$ z. X9 N  R

2 I1 r( J- L0 L4 U' i% e4 J% H
0 x! o+ J6 f+ h' Z& [) G编译LibModSecurity(modsecurity-3.0)
8 m$ I' ~( T) w8 Y' l) x$ cd /root' J' Q% ~8 A3 v: s( q, n3 j; j
$ git clone https://github.com/SpiderLabs/ModSecurity( X+ S6 l4 q$ f' ?/ u' w+ V
$ cd ModSecurity+ ?5 @1 M' c* Z+ u
$ git checkout -b v3/master origin/v3/master
8 ^' y/ B9 Q  k! r2 g; |3 \3 A( S$ sh build.sh
; U  h: N9 p. M  P; ?$ git submodule init7 R. g/ @# W" P/ Z
$ git submodule update
# s; o' M3 }; k, |; a* O# y$ ./configure% Q! {* i* J4 |0 m
$ make
) K( I% o: [) k3 c- d$ make install& l' c' [3 F# ~$ t+ |4 I
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: X3 p. [9 M; e& g. j1 K+ D1 x
6 s% l+ p0 k& F" {2 a
+ |3 k0 s! |. _' x- n$ ls /usr/local/modsecurity/lib- h! Z& O; v9 l: |
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0# h, u" a+ j8 ?2 Z, c3 z; }
编译安装Nginx并添加ModSecurity-Nginx Connector模块
8 p. }: G# D/ A0 p+ v使用ModSecurity-Nginx模块来连接LibModSecurity$ T2 f' Y# x) d. ^+ e* R% g- W& f
; w/ D- ^: B1 X- z
! t  }" L( y, [0 E' E2 R$ R
$ cd /root
4 ~3 H5 L" G$ {7 C. s$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* Q% B1 h$ M* c$ p. p$ tar xzvf nginx-1.9.2.tar.gz- t" ^, r% V* D4 ~5 Y
$ cd nginx-1.9.2
1 Y' `6 K% O. a0 Y* r  h0 }4 j6 t$ ./configure --add-module=/root/modsecurity-nginx- r1 T! V. j2 ~- @4 W2 ]
$ make
, z8 R: a; L, J, a$ make && make install$ Q5 A" D+ _9 z! x2 U, }
添加OWASP规则- i4 @( q! r" {+ S/ ~9 u& g" E$ G
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。3 {4 ^# ^8 ^9 k( u5 A- _) s' F3 Q' r

' q- k! p$ U  E
2 I) T, B3 u) @' k; F下载OWASP规则并生成配置文件% Q$ g! v, c6 k3 o. r" d
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
& ?3 f! J( v4 ?. P8 Q8 k$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/9 o3 x8 c2 k  s8 @
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! f; q' O  Z. q; s
$ cp crs-setup.conf.example  crs-setup.conf
8 R& T% a5 K  H/ f$ O# s4 D配置OWASP规则- }) G% f* M, q
编辑crs-setup.conf文件# M( I+ |7 N; R! ^! d6 z; }. G+ y

' `/ C# j1 Q* ~! e) b& t" k( P% R- @: ^  N) c% S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
+ @" k) ]8 k1 I  M$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
7 v$ l( l$ f3 c( O! J- t% b3 G! u, s$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
& _1 i$ R5 l' Q4 V& t5 |+ @0 J$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf9 l& M- i" d! d; i' z7 O6 `) t
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
. u1 I, k! H2 t) R. r7 ]% S& K# F* \5 r& y- ~

! b+ Q  f5 f, f5 z% A启用ModSecurity模块和CRS规则
6 p' b* V9 p! B7 m- Z; Y  }复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& Q, p5 q7 w. |* o' e
+ s$ m+ R/ I6 T

5 M  I1 X! [4 c# L7 jmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。- f3 m% O. i  V. |- e, K' J

  e  X: @; x. a* t' I- w4 u9 W8 B, R6 n+ k/ F# i
$ cd /root/modsecurity-2.9.1/
' j; u8 [1 x; P$ s% g- V% A( a! K$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  " b& S; Q+ T0 d& F. t4 @2 z
$ cp unicode.mapping  /usr/local/nginx/conf/& n& D- Y) W2 N2 L8 k* ^. N
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。3 x* H' R0 p/ _3 C% V
8 v# \! o4 E% w* b5 E8 O, \: q+ d
- |; V' \) p0 f( D0 u/ l# }
$ vim /usr/local/nginx/conf/modsecurity.conf" ~& l3 b7 {1 v# W  _
SecRuleEngine On+ M5 ^' Q: `  X3 b
ModSecurity中几个常用配置说明:* S6 U/ x# ~3 T; U" E- d5 `  K
' o" l1 b4 n5 f

. q1 Q# d! F  f  G! b1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。6 t! u- \1 I, [* W7 C/ V+ d4 I: F

/ C' n8 g/ k, ~; u% u( I2 V  y" P3 Z  i1 x- D3 k0 ^# k) O
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。2 f$ u! M# s% L( ~  ~" B/ d2 I

: S6 r5 M) {0 ]6 f: r6 k1 y0 ^& Z, y3 S7 I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. B7 Z& Q% a% S7 ^7 j. {
9 }6 T( A2 C5 G" r8 K2 }9 A# U( U  p! Z# D/ x3 g
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。2 q- p- P1 w5 Y( J

( t+ d; A1 t; H; o& \! V% m  L  e; w
: h& j( I6 y/ A! F/ h  z/ p% W, i在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
! v. p$ A0 [; d$ [; ?
4 c& d( K; Q7 }$ ]
* z1 G& f' J7 k. D3.x版本CRS' W0 M0 o; ]* u! y9 {: [' g
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs% H# B' r1 T3 h; n! }4 ?. q
# 生成例外排除请求的配置文件
9 y. D7 ]6 R3 u! l; p$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf! Y; L7 {" d# a& ]# G  g
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf4 p; g4 O- g7 w
$ cp rules/*.data /usr/local/nginx/conf
; T4 a% T& M- z为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
1 ]0 G! L! b3 y# `0 ?' s
  C: Y+ {. y3 T" j# @; z$ d2 w5 P1 v4 S# @. e/ M, w/ E( z( `
$ vim /usr/local/nginx/conf/modsec_includes.conf
; c, A+ g- t1 A9 L1 x/ I! c
) A  o+ |/ e+ e( R
[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

: v- m0 X8 l/ U& V2 e: d1 z5 F; j+ O, w
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。) c" ?8 u7 ?; ]7 I! f
2 ~/ `3 }( {/ w+ `: T, M* o; M% C

: e' L7 N" V# d9 I2 I( r配置Nginx支持Modsecurity
7 s: s& L+ N# R, i: N* F9 j/ W启用Modsecurity: C  X2 g; N  L. O! @- _9 F
使用静态模块加载的配置方法/ _8 R/ v0 L! {4 E+ [4 Y
在需要启用Modsecurity的主机的location下面加入下面两行即可:
2 s; \# L* Z0 a1 j4 x' y) P$ D3 s; y$ `2 z; R
8 P) I+ w1 ?! V1 S2 h' a
ModSecurityEnabled on;3 W4 I+ y5 S9 m! f" r
ModSecurityConfig modsec_includes.conf;4 {- B' e# M5 X8 P2 o$ Q. `7 y" L
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ \  E& n0 |% u) o! |5 `! Z5 T* m: [$ Z6 ?9 o: X1 T0 |
* @" |  Q2 G5 ]* U4 O( X
$ vim /usr/local/nginx/conf/nginx.conf8 g- V; a- E: n8 I6 s7 w, n6 g
2 U+ u. \2 K" R) ]0 y2 G

: j# A' A2 v! {% H. qserver {
: t7 f1 M! i# Z2 y6 \! N2 w  listen       80;
( y" x& h' G' e  server_name  example.com;5 ~4 j% H, |5 Q$ l. R- I; I1 D
  O7 a9 F5 I- w4 P9 I( |

# @3 |% y# g/ t: k% P$ X( Q  location / {
6 @4 Y' g# K4 B0 `5 C5 v/ ?    ModSecurityEnabled on;
9 m1 A+ f% c) ]- k  [    ModSecurityConfig modsec_includes.conf;
* a! `. q; \7 o( S3 l. C5 Y5 G    root   html;$ g' F& U' g5 x7 _. Q
    index  index.html index.htm;, h$ e0 Y+ X2 u# G& D8 N. H
  }% D  S& b) G* I* I
}" Y% D* Y) c- X0 J! W
使用动态模块加载的配置方法" p/ r2 B: m' ^) Y/ Q
在需要启用Modsecurity的主机的location下面加入下面两行即可:
# k% h, q  H; n( v$ ~3 ^
, O$ R0 I. b2 Y: R, U- d$ ~% @7 S: g
modsecurity on;! U( @! V0 G' e; O3 O' f2 M) B
modsecurity_rules_file modsec_includes.conf;
5 t2 [# l" T$ G: S' R" \& [- r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 S7 }) S. I8 O9 \6 S3 L
* J- i4 v& N% l& M8 K2 @# v: a& N0 }2 W; a: p3 W& l! C
$ vim /usr/local/nginx/conf/nginx.conf
$ f- q1 J# L0 ~/ m# m2 h( u
2 [* a) h" o( d5 N5 ^* N3 B' u1 L, v
server {
% s3 k7 H( Z0 @! G( R2 H  listen  80;  w& u; \/ r- m" _
  server_name localhost mike.hi-linux.com;
$ A9 b0 O8 d1 A: A: t* \, M' B  g  access_log /var/log/nginx/yourdomain.log;5 W5 I: R) M- C0 K
% m; q7 Y9 o4 ]/ A! g. `

! q0 D/ V/ `5 y- j$ a  location / {
  C) d0 |# C3 K6 I
  J3 Y2 Z+ k- C4 z: a1 T4 ?  O1 L9 ^1 L3 ~: O  D
  modsecurity on;
! t6 q! k+ l$ n6 f  modsecurity_rules_file modsec_includes.conf;  r  I1 y: k; ~1 Q7 {; g* m
  root   html;4 I* S1 M# n( ^( x2 _& l
  index  index.html index.htm;- e$ }7 i3 K. r+ [) G) ?
}4 n6 P1 d* l& B9 U& Y
}
; B& n8 G6 v+ j, z: [验证Nginx配置文件: f- I- F# N$ q& s* ~7 P8 ]8 ^
$ /usr/local/nginx/sbin/nginx -t
3 C. P; h) v; l6 o  K; a1 a6 C( G* Hnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok! \8 F7 X) c3 r/ X" Y, b
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7 Y0 r7 o* D$ u4 P. ^( V* Y
启动Nginx
. K1 P9 d7 R& |$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
) Y7 w. {( F  Z9 b6 J: |5 R: G& }; F2 M8 O6 x- 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. H1 v3 I: {, l; g

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-7-30 21:35 , Processed in 0.077529 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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