找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9980|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。( t* v: a* _+ T$ I* r% C
4 x4 {- }# V, _9 y1 [7 W
# _( v' x# g" A8 G5 _# a
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
7 e' r9 V$ j0 [7 S) \+ ~- o# Y7 e7 _. t+ o9 C+ l+ H
9 p8 z' p: k  t  r5 ]( }/ Q0 ]
什么是ModSecurity* @0 o/ C: S6 V0 G+ S' _
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
8 Z5 N$ r0 \+ z6 T" H9 F
% g: A! M# I1 E8 q  W1 N8 Q# \" ?+ d6 k9 s3 H
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。) h; ^( V7 e0 m2 ^0 Y( U

8 l6 O) r2 O2 Z' ], T% p, \9 V7 d2 i, b
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
$ d4 l. R  D/ X/ x; J+ G  D, g9 l: _: h# a9 E3 `# j& _  N

; @; I( S  y4 }* `官网: https://www.modsecurity.org/
& v, p5 {6 T" \9 K6 h1 d
7 K7 m' `  m8 C" m3 p: E
5 T% L: Z$ [8 P% ]. p$ ~0 ~& d8 p% _" V什么是OWASP CRS
1 e: k  B- y" p; h$ FOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
% F9 o6 L1 [, G# c5 c1 s" E; G7 d& d3 n- g/ \5 ]
# @6 O- f4 e" ?% l
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。$ m, m3 |) _5 C' r7 l
5 _$ m. [$ W$ m6 f5 f
4 u1 H5 b  x* `# N/ S% M
HTTP Protection(HTTP防御)
' @3 O8 b: i: {0 w% jHTTP协议和本地定义使用的detectsviolations策略。
! Y6 O0 s0 @* c+ ]; x, E, p3 c/ h9 z6 Z$ C# [  ]
. C( v6 U/ V5 d" i8 @
Real-time Blacklist Lookups(实时黑名单查询)
, H, s: [4 h5 U' t5 I- h利用第三方IP名单。
6 B7 W) r- l% D5 U; r& B3 j* R5 r; ~) e, S
4 D2 S# m5 {' u0 X4 b
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
' n! a4 U: J7 Y$ U* C防御HTTP的洪水攻击和HTTP Dos攻击。
# W: j+ z. k% [3 E/ }; J) Y1 X8 h" v/ {( Q7 K
& U, t4 w! @1 ^" F
Common Web Attacks Protection(常见的Web攻击防护)) b$ {# L/ @* ~& g3 F9 G4 O
检测常见的Web应用程序的安全攻击。( A, h; r% f. M  [: a: [

  P5 Q) U' f* z0 v# n6 s; k
: B2 D- |* ?3 B  S+ g% vAutomation Detection(自动化检测)
9 k" Z- h) L" l" I' L. A检测机器人,爬虫,扫描仪和其他表面恶意活动。* V( D8 ~* ?: K+ o* @6 j) j
" K8 z0 B* }1 h& O; {+ X
! `' @4 N  g3 n/ }8 i' ~
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
9 B5 J+ v7 R6 M0 Z5 l检测通过Web应用程序上传的恶意文件。
9 G3 E! Q8 k% z  N( {, A/ [7 b8 D9 X4 {. ^; f

& N6 Z2 u* J& V7 p2 g9 nTracking Sensitive Data(跟踪敏感数据)  Z+ C) D' L. j" I
信用卡通道的使用,并阻止泄漏。
/ v; e/ @/ m/ t* E8 b1 y0 g6 v) M! H/ k: g5 r

2 x2 V7 ?$ T* h1 x) t; S! a4 g4 PTrojan Protection(木马防护)
# S# s% f. k# ]; v: C8 T检测访问木马。
  r1 g  n' P+ I# ]8 i( e- m  X0 T3 c

: q* a8 C7 e( t: H9 r# xIdentification of Application Defects(应用程序缺陷的鉴定)
8 Y" t! w& A: F- J. F4 m* I0 o检测应用程序的错误配置警报。6 ~' k5 D# T7 B# z  h! ^% f

+ s- P6 m* K' u- V
9 X2 Q' T5 K# f+ u4 ]  |- oError Detection and Hiding(错误检测和隐藏); k. L1 F$ v% C( J
检测伪装服务器发送错误消息。
9 X. H8 }- }! c& ]$ D' M3 M: u6 ]
, E/ {$ b/ R7 t2 a* z* o0 ^" D* K5 F4 R. j
安装ModSecurity
, s! z5 \3 c2 t软件基础环境准备
4 F7 Z% s3 t& l* h) P5 t下载对应软件包! E) f8 a; e8 ?. I$ r. f, b. v( i
$ cd /root
  w+ u' [4 T' B+ s) H5 {% S$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
4 o1 o& R+ h# a' J- z) I6 d$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
/ ]" q9 W' i* H% z! X, o0 \安装Nginx和ModSecurity依赖包% l! G" o  S& N  s
Centos/RHEL1 Z! g. |1 A9 {+ }
; a) S5 Y4 r" A0 f" D7 \

" {) F, O/ T! V0 f1 o% q$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel% h1 Z" |  I! P
Ubuntu/Debian' k$ s' D9 I7 m. K4 s6 ~. t" T3 u
8 v' ]8 q3 A( M/ M1 Z
8 Q: y; O4 y8 @7 J# z2 n+ l
$ 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
5 E, q3 d9 m2 r1 V8 B- n编译安装ModSecurity
; B1 ~, e- n! K; jNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。) b; P9 c5 k0 O* G

) G2 u/ W0 b2 p& S8 x
1 g2 T2 S# A. D; }) j8 s9 x方法一:编译为Nginx静态模块6 ^7 y: H" @  _& |, n
3 Q/ g; Y: [% V3 D: M$ u8 E
( z- f% Y( z9 z) A
编译为独立模块(modsecurity-2.9.1)1 C/ Y  D# T- q* l5 N
$ tar xzvf modsecurity-2.9.1.tar.gz
) I3 k' ~5 e8 V9 `$ cd modsecurity-2.9.1/3 h) q5 o6 {1 T9 \5 ?; K
$ ./autogen.sh/ h3 ~. w- f* n; H/ S5 f: z
$ ./configure --enable-standalone-module --disable-mlogc. d5 n) O/ @- V9 l! w" ?1 d( X- _
$ make# |8 c+ |# J. ]
编译安装Nginx并添加ModSecurity模块& w, C7 N8 a# ^. s, Y/ O9 N$ d
$ tar xzvf nginx-1.9.2.tar.gz/ C" P! _6 @# K. ]; u' I
$ cd nginx-1.9.2) ~! M) G( w6 D7 _& x, C8 R
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
4 v8 Z% g5 B4 k; j% I+ `, X0 \9 [$ make && make install
! o9 @& @2 G& V7 ^# D方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
. t3 W1 p  L" V5 i4 s! g: B
+ g( o2 r2 {6 m+ q2 @* g0 h; C2 b
* N4 ?' E+ u  V3 W编译LibModSecurity(modsecurity-3.0)
! [- \) [6 a" A6 ^* r- N+ j$ cd /root6 w$ H% i; V' k# W. I
$ git clone https://github.com/SpiderLabs/ModSecurity
+ e, p4 o) l" u/ [/ H! W3 b- m  c$ cd ModSecurity
) Y4 l( e; t9 t( o' F2 \$ git checkout -b v3/master origin/v3/master
! b' {) @/ @) a5 C7 j1 f$ sh build.sh) e# m' k4 Z  B8 W
$ git submodule init
1 V, T" r2 s1 U; u$ git submodule update- L! k$ U  E* v, K0 x) ~
$ ./configure
. \) d% ^& w1 ?( R, @$ make3 M2 Z9 t  j  m6 ~
$ make install
+ G4 |' e/ k" `& lLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
- g9 v# ?* P& r2 C/ X
$ ^2 K5 O, ?5 E8 b$ _$ V& D* j" h+ m+ U% m. A
$ ls /usr/local/modsecurity/lib
9 j, q) U% z" z9 V. w9 w7 ?- tlibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0- j( R+ D, o% Z6 T& M
编译安装Nginx并添加ModSecurity-Nginx Connector模块
# a! `1 e$ I& [6 Z3 l* A使用ModSecurity-Nginx模块来连接LibModSecurity
" w$ F$ G7 h* ~. @: F/ [9 C
! |5 J' p1 ^4 Q5 V2 z. o: E( g
, x3 I9 @; n" J6 s$ O/ X- T$ cd /root
! ~" F/ D# f( m$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx# A2 r7 i% j% _+ X8 C& E' I$ `# p# k9 q
$ tar xzvf nginx-1.9.2.tar.gz; k" h' X; K# {- J8 B6 h3 P! c- ]
$ cd nginx-1.9.2
# a* c. j( X$ ~+ i5 R$ ./configure --add-module=/root/modsecurity-nginx1 {9 D8 b" G/ V. ^. o: f. Z& V
$ make" H( k  O- T( U) y
$ make && make install1 u5 i4 j% O7 m% r" N& d% t: k
添加OWASP规则: |: X" F* ?, g- F
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 }' j% b/ `7 {. T) J  k1 D% l& W+ c# W, E* v
- a! a2 z% f6 u. i7 `- r
下载OWASP规则并生成配置文件
9 c. ^% F  r1 Q0 Q8 [( p/ y* q$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git( w5 N1 t8 |4 G* T6 T
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
8 ]" I# S+ Y- r, E$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
- V% Y- V; K4 Q( x: m% i$ U, Q8 p  @4 S$ cp crs-setup.conf.example  crs-setup.conf  V6 }7 t# b" n2 F7 s
配置OWASP规则
3 b! u/ `' n5 D' B编辑crs-setup.conf文件
+ |% o5 F+ e2 h2 z2 e
  g. b8 |7 S1 C. X$ H6 H8 W2 t# W) M
* h# R9 y( a8 c. Y( D2 A$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf! U: y( M1 a3 Z  p& o! L7 H) k" n- h
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
/ a8 Z  N& F7 i4 L( K: R' g" [$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf% D( F4 S) x: @9 ~' d% n# l! _# c
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
) p! ^2 D$ X; n. v" W4 j: h默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。  w, |" ~1 G" z, m( o
& S3 t+ r/ J+ A6 w

$ L, B9 [; B, _6 \& W: N' L启用ModSecurity模块和CRS规则5 ~3 Z1 U; ~/ E# P; X; r
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( v6 ?5 ~* X5 e0 J- r( A
/ D( R  E7 k! O/ M  {& s

5 ~3 l* S3 R; O0 m+ X! I2 gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。' |0 c) s8 @* C5 f! B$ q  d
+ ~% \4 Y$ m! y1 l' x, c( F
+ T' [" _+ e3 `- g- }, \) r* D
$ cd /root/modsecurity-2.9.1/- L6 }# J) B1 m4 I6 r
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
) V0 G, x' X. F5 R. y4 \) J$ cp unicode.mapping  /usr/local/nginx/conf/9 s9 Z) _0 k/ j9 c
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
; {4 W- D$ e5 o9 C1 e6 B. p  G1 f& V1 g- J
. x: K3 y. @( F4 w5 m! Z( S8 V
$ vim /usr/local/nginx/conf/modsecurity.conf
3 n% u" U# `4 K7 G! C0 JSecRuleEngine On1 C. i$ d8 }4 q9 Z/ u  s. k& h
ModSecurity中几个常用配置说明:+ Q% ]5 _+ {- y. H% }
& T+ T: ?( U; p8 b$ n/ K* H0 D
* f1 z# {5 B& k
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
8 P7 h. t/ Y  F- @% o; L0 ^- j/ }& J1 W) K6 a7 N% O, C
& z. e( _$ S# p
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。  Y1 j3 C6 `& [- J1 n& [

5 y0 m0 H/ j. [& N5 G% f" z# P; g' @' q' [8 o  z
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
6 o, Q, @' W0 }4 p, `  D' d% r5 Y; a! C: P& Q: V

* @2 i9 ^5 m( [5 w. `& a8 C( y0 l4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。; y1 L+ x- ?8 l+ `% d! S

" y, x& v" i1 f3 M/ `5 d1 m0 \$ E
9 M- N3 R0 f8 g. v0 n, y# X2 f在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。9 o. r/ n3 j: {9 t: U8 X2 K
+ ]5 {; m) J9 P

* |; Q, ^" F+ S% p# k% {8 Y3.x版本CRS  l6 ]( t# b" `: E
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
$ A4 K! h3 v1 j9 R# D8 l$ n  Z# 生成例外排除请求的配置文件) Q3 D1 Q; \. x* l
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
; D/ N# n0 H; n$ j# \5 b  S7 T( c$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf. ]8 y+ ^  X9 J
$ cp rules/*.data /usr/local/nginx/conf
$ y7 L# d* x* {( x- N( a# e2 v为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。  z6 n$ ?$ p& C" T

" H" Q# @) N  X6 y$ R2 t6 e$ k; I4 _$ R" c: c: P7 e
$ vim /usr/local/nginx/conf/modsec_includes.conf/ b$ Y/ Y* l% B* k0 |; ?$ P0 l
& A1 `9 J, w( Y% S# ^' M- n6 J
[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
" d1 W7 d& A8 v% ^, b) ^  b

+ t* e6 S, ^5 u( J  I, s注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。4 D1 t0 ]* i, C" r0 K; ]0 i

/ [* Q: G/ f! S/ l+ t. q9 ]6 Q& I$ ~  [' p
配置Nginx支持Modsecurity' f7 `: E. a4 g3 n7 A7 T5 s$ f
启用Modsecurity
# s. P8 v" Y% z, {" i- z# l8 ?使用静态模块加载的配置方法1 r; A: @" f. G2 k; l3 l
在需要启用Modsecurity的主机的location下面加入下面两行即可:* s/ d; Z9 Y3 G* f7 z3 N1 U
2 a! U' @9 ^0 y5 m* c# k' \0 w- f, Z
% g, ]1 _3 ~! m! c* x; y
ModSecurityEnabled on;& E8 W% G( J: c) E
ModSecurityConfig modsec_includes.conf;+ I" |! N9 W) O* K
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
% e+ A% z) H! |# G: S) n- H7 f
  @/ Z! j1 Y9 {4 I! q# s' x1 T
$ vim /usr/local/nginx/conf/nginx.conf
6 J1 G- Q2 t# a3 k: r- n% ^2 A5 m2 R9 _! g

) d" Q0 v- r2 x- ]server {6 {: o- A. g5 f3 m" y8 \/ M5 i
  listen       80;
, a" c) U6 b4 s  server_name  example.com;. q: Y# X1 f: P- a) e$ e( }1 c
# m. Q! Q& T/ V7 D& R  [; X; C

0 g& }  s! Q: @( G$ F; M  location / {
* T8 @4 C* ]. L3 d9 F6 R) a# E; d    ModSecurityEnabled on;* o! U  m. S7 H& ?0 p3 C: V
    ModSecurityConfig modsec_includes.conf;' {9 m# v" w6 W" o. G1 `3 V
    root   html;
0 o' ]6 z) [$ I: r    index  index.html index.htm;1 g/ y1 I. W3 `$ c# x3 ?
  }
# c3 W$ u( H" n: x}) q1 Z* B" ]7 s( n5 x: A
使用动态模块加载的配置方法
, q. P' \1 k" A5 c在需要启用Modsecurity的主机的location下面加入下面两行即可:, m! e) c1 ~1 R& q  |! X6 m/ C
: u* p# {' Y, ~3 d: s) ^& w* i

7 e7 O6 }% Y& O5 x, x7 jmodsecurity on;5 b- b; m+ q2 {
modsecurity_rules_file modsec_includes.conf;  K9 b5 y9 t! U1 d% ~; U: h
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。( n0 T9 W/ [  d' v% \+ d! q
8 F4 j! [7 ^* W0 d* P) C/ H# h

7 n# i" B- I9 F& `* G  s7 u5 z$ vim /usr/local/nginx/conf/nginx.conf- k/ D2 H0 P% q) L6 J
8 O9 R/ h, X) a! U

5 s/ W* l9 k! Pserver {
. B& z) H, D2 U: ~8 ^% Q, e  listen  80;
; f. R+ L2 ^& Q+ W* f  server_name localhost mike.hi-linux.com;
" h3 g" o- A5 n- T) f+ f0 @  access_log /var/log/nginx/yourdomain.log;
4 K7 T' X; p5 ^6 l$ C" B+ i& C3 j! P( g$ T+ K# g" o
' u: z* o- b4 h0 N# e; V3 J1 U) i+ \- r
  location / {
5 \* Z) c- \" F. z8 d) h5 o7 `1 a- m! `# P0 }, n; y( H- S

6 D% v# D; J2 `& T3 T* P; a  modsecurity on;$ o; _) t! q) f- Y2 W/ H$ t
  modsecurity_rules_file modsec_includes.conf;
3 d  q# P' T0 m% p  root   html;, A; b7 u. {5 ^8 N: L
  index  index.html index.htm;! G: Z8 h: B8 N7 s2 U5 Y) ]& ~4 \8 M
}" z+ ]! q5 G# E4 h3 Q
}
! x$ Q9 d. A5 S! o$ d  |1 @验证Nginx配置文件
+ N, B+ g5 Z( F' h) P1 d9 _: ^+ s$ /usr/local/nginx/sbin/nginx -t
0 ]- p3 `9 k; G: \# }, O% A3 ~nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok" Q; |8 X) {- u3 V
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- d3 \7 ^2 x+ R& Z( W启动Nginx
/ b; K, J, w1 R* g5 J# h& ^( h$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6 U4 B) E  s6 \  I* Y; `# m5 i! V; f/ |

测试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 R" E* p3 o- u& T8 m- K

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-1-19 14:58 , Processed in 0.047713 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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