找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10224|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。, U* a. n* ^" |6 m9 }) D% b
. M% X6 [/ R0 l# A8 }& n! u0 Q4 v7 f

* r  ]0 S5 U" z7 z在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
* T% H2 @- X' ^, x+ k' V! t& g6 Q: K- f3 M4 j1 N

$ J" p1 g4 Q* ~) K什么是ModSecurity
: O5 a5 f  \$ y8 [; qModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
$ O  a% m6 _7 z3 O; \) C
/ t2 Z1 q$ Z/ m6 B9 B, o. s
' @8 P* r( ?) m. v, A' @& bModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
2 ?3 Z: d( v8 q0 y3 }7 S  Z/ G
) O) E' i' A# r! S5 x" f6 J" z* b2 J# q
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
- Z6 C4 H7 u/ c) {0 l
, E! @- t% K& e( u2 w
8 E5 v7 T) ^( |& C! g# p官网: https://www.modsecurity.org/0 K$ I; w9 k4 A' ?& d% e
5 L# \' j1 Q( z0 g* `5 H

2 h, S- d& b2 ~2 _4 b什么是OWASP CRS
1 r, z; p( M, t: W: G, GOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。+ M) P3 k: ]! b% H2 E
7 |% e7 Z7 ]5 T1 I2 d8 {

) S! G) N8 z: s) w0 IModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! Y0 i, U& P: P$ V0 `  U4 Z: U6 z6 D( l9 J

9 T- T0 K! h  ]HTTP Protection(HTTP防御)9 t* V; r6 d# [1 }
HTTP协议和本地定义使用的detectsviolations策略。
- W+ w# N# I4 a( x4 D
3 Y7 Y% H9 L6 Z1 f6 W4 p2 g+ N% `% N# p8 }2 c, S7 }
Real-time Blacklist Lookups(实时黑名单查询)6 c% }* e& E+ S: z" n# l( |( v
利用第三方IP名单。
! V: H) W2 W3 K$ Z. J8 x1 d2 n/ l7 N0 z6 t0 `0 K" A& _( D% X4 J

* l+ L  \/ s: JHTTP Denial of Service Protections(HTTP的拒绝服务保护)# S  g3 n# W' h# n7 t
防御HTTP的洪水攻击和HTTP Dos攻击。  c( L' p4 ^- W6 S# Q# _0 T  [
4 v2 j# C! T+ A/ k- z$ G& m3 ~
; o7 U+ a$ Q8 l6 k2 R
Common Web Attacks Protection(常见的Web攻击防护)
! U3 B/ U- C/ d4 E+ V. \检测常见的Web应用程序的安全攻击。! d2 U8 y" O2 Z$ d0 T, M. m

; O3 D; ^; O: o; ]+ Z# h8 ~! R6 o4 [" {* _* T7 u+ ]
Automation Detection(自动化检测)& c- G+ @. K5 F. R- K" X
检测机器人,爬虫,扫描仪和其他表面恶意活动。6 n/ \5 W5 ]/ v4 @3 M5 d6 s

; k) P  g$ \4 g9 q5 O. V: e  @; v8 ~8 a! _
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)  r: P/ |- O" i
检测通过Web应用程序上传的恶意文件。
4 p+ e( }* [, O  I# e6 i5 A* F" w! a: O( w0 p8 w8 {* P
: `9 Z2 M5 O. t8 X8 m
Tracking Sensitive Data(跟踪敏感数据), c( U, f& n; j" x
信用卡通道的使用,并阻止泄漏。
# {8 c% A9 k0 G
% ^& m( T# o' K/ m$ J: d2 J- {
  d; |7 v* E& s" F4 sTrojan Protection(木马防护)/ o  I  d* q/ A+ ~8 [2 n" u
检测访问木马。
& ?3 `+ A. F! I& @. z
( e& V9 V: ?8 k2 s7 _% g4 e0 f0 v1 v  d" L$ P
Identification of Application Defects(应用程序缺陷的鉴定)1 D- Y# N' s6 r0 F9 H) ^8 M2 L
检测应用程序的错误配置警报。( U  a" R  ~7 w+ J+ ~( }
! T6 S* k/ q0 n0 Z& X

' `  V( Y" K. u; ZError Detection and Hiding(错误检测和隐藏)
& R7 b0 B- s2 N# y, h+ x检测伪装服务器发送错误消息。  A% K  Z/ y6 {
# I3 B8 r" G2 P2 R% [
" V; S% z9 e1 N1 h5 A# ]
安装ModSecurity% r5 b; T/ n  T+ x& Q: o8 W5 Z
软件基础环境准备
  m4 Y# P1 a; H! x1 J. }下载对应软件包
6 D: Z9 [) p( X; _- j. y$ cd /root
# E% I4 Z" b0 {9 @- P. `9 ^$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'0 x: m6 E9 Q7 F* f; L( |% Z, y
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
2 ]! U/ r! S% d4 {1 O- V安装Nginx和ModSecurity依赖包4 Q( T' [/ [; u
Centos/RHEL
0 P* E) E+ _/ w, {5 T. Q/ F6 U7 s$ ^3 T( B5 S4 I
/ l) u8 h+ L2 H4 e" t* ]
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
$ L5 N* h. }# [  V' ^: JUbuntu/Debian
2 S) i. u0 L: Q6 F0 t' B( o# M4 t5 S7 ]/ L  n( K$ |5 P

, f9 ^' R. W: V, _$ 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
9 H$ p6 {* w  [' P/ q5 {编译安装ModSecurity. i& S: @, c' m: v/ y% M
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。: i( Z; O' l, ^: v, c$ [

& f8 \9 f, y8 u6 j" I
: J( H! A" W; _  k# h; C方法一:编译为Nginx静态模块( L( S1 W( u* ^( U+ Q2 a

3 `3 H# r$ G' [$ t! V6 y3 D5 X: x- W
编译为独立模块(modsecurity-2.9.1)
) J: ^* ]' U: p  ?9 N$ tar xzvf modsecurity-2.9.1.tar.gz
% R: L2 v& q1 N) p& f# x$ cd modsecurity-2.9.1/
  [( ]$ f: y7 |: O8 d% |7 T$ ./autogen.sh
% j: r8 B' H, x% Q: v0 d$ ./configure --enable-standalone-module --disable-mlogc4 Q+ v; @' F9 z7 C% D2 K! ~
$ make
9 Y# s) T) L. f) k6 a. |编译安装Nginx并添加ModSecurity模块9 Q3 v0 j& S1 G6 S7 B
$ tar xzvf nginx-1.9.2.tar.gz6 m# `( o8 j4 Y& S, j
$ cd nginx-1.9.2' ?) P2 n( q- }; G3 q7 k/ y/ o( x
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
9 T+ h* l4 x: ]4 S; s% D9 Q, y$ make && make install  i* e- K: I+ h. d, C9 ^( t
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块! |$ N8 c3 y1 _1 r' U9 {( F
) S, p6 J0 y5 o5 H2 x* B+ y

! U- P' F  A0 o) G& N编译LibModSecurity(modsecurity-3.0)* t: y9 g) ~9 ?- M
$ cd /root
6 y" ^9 H/ O0 E! H$ git clone https://github.com/SpiderLabs/ModSecurity- d4 m7 y6 S1 r1 b6 x6 e- a! b7 z) `
$ cd ModSecurity
7 Q( W" d9 e  d7 I8 m0 N2 d$ git checkout -b v3/master origin/v3/master
& O: j3 P, j+ t! b! Y$ sh build.sh5 O7 k& N* V+ Y4 X
$ git submodule init
. j1 m. C0 f. L) b! T( \9 I7 N$ git submodule update
9 ^4 M' a; o) B5 |! \* v$ ./configure' x$ I9 Y) B: W8 @' t8 X, C& H5 h# V
$ make
+ G8 A' k2 _. I" z! i$ make install' j- _3 Z; g  i, `
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 U2 r: G* z2 z8 [3 J: ?7 W' n% Z  E, \

( {6 s+ }# B% p1 o1 d& P9 S$ ls /usr/local/modsecurity/lib) l; S" R9 m# o
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
$ z  B& }- ~% n5 }6 @4 G$ E' ^/ i编译安装Nginx并添加ModSecurity-Nginx Connector模块2 l8 o* w) E7 y# G& R) s( k/ Q
使用ModSecurity-Nginx模块来连接LibModSecurity
  ?% s* k% k0 v* M" I& P/ z
; L2 x& Z& E& _
) e- h" J5 i0 j$ O$ V5 {$ cd /root
2 F. k% @8 E$ o/ ?$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
& Q. x1 p0 x. N$ tar xzvf nginx-1.9.2.tar.gz" E$ X  q- Q' Q2 u6 E$ I
$ cd nginx-1.9.2
1 s+ ^- P6 p, ]" n; b" ]$ v1 r, ^$ ./configure --add-module=/root/modsecurity-nginx5 Y) X* {2 v, j/ N- G3 r; Y
$ make
: j% `3 X6 @+ r% d  l* D" J1 q$ make && make install' |2 i3 g3 g$ ]% t- Y7 @. ?5 f, j" R
添加OWASP规则4 X& b5 M9 T1 P
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。! o- M+ d% F- |" n/ h9 @

6 D, i) i) [0 M8 U+ c. t& E+ {
+ {+ x; v) u/ ?% U下载OWASP规则并生成配置文件, }) l: U1 i' T  C6 b- K  i
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git9 h# y7 s2 X3 C3 M0 @
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/$ J9 e  h( I' V6 H+ M5 D( _; E% \4 O
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs( [# v* p1 w& J6 ~7 B+ B' r
$ cp crs-setup.conf.example  crs-setup.conf
6 o& w7 H8 _9 ]8 o3 e$ P配置OWASP规则
/ W% d9 s$ R' M' g& z编辑crs-setup.conf文件+ O* Y$ N3 o/ M! M! y4 `+ Z

( k+ o4 F* i) F) U1 x, @
* ~; h- t7 w+ K) t5 ?4 R$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
5 _6 j' @  ?) \+ @9 n2 p$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
6 R2 o, ^/ `) S+ S* M$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 V8 |1 R9 d$ L9 ^0 Q8 A( A# i
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
% L  Y9 z; K) L默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
- Z4 {7 m% q" Y0 Y2 U" c* B: m: R4 j; S7 O7 q9 \1 y9 y) V
" l3 P! y* ?, t- b
启用ModSecurity模块和CRS规则! u8 v6 F" y/ Q# u/ E! G
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& u# ]1 Q; B) n0 E/ J  F

* S& S; g  B2 t1 N! ]9 a5 F" j5 J5 q" q9 r" H$ q
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。5 }0 M8 c9 E- J3 q. t8 }

# F: q3 d$ f3 R4 \/ ?
. o( F# G; \1 Y3 |" O/ c% J$ cd /root/modsecurity-2.9.1/; @9 n6 P1 ?" l/ S; a) d  \- r
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
; B& g% e* M/ ?9 b: h0 L$ cp unicode.mapping  /usr/local/nginx/conf/
) r; J6 u+ ]6 j- l9 n6 N将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
( x: O, D6 D+ \' n0 L5 N' N+ m6 A- |2 O# I+ Q: }( ^1 L* ?
; X# Z/ M; v  |! b2 ?
$ vim /usr/local/nginx/conf/modsecurity.conf( ^1 {, O' q2 v3 A2 M
SecRuleEngine On
& j$ ?0 f7 n" f6 I  Y/ j, `0 S& ~ModSecurity中几个常用配置说明:4 b# ~/ A$ r/ `; M# W

) Y- Y0 j  s7 X  g$ L" S9 B- L3 w) I+ i
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
8 J0 F: |* @  X6 ]  a' j5 F
# l& J6 l! Z. D" h- \9 R+ j# J) U
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
1 D5 f* L$ ?# ]& X  e( h1 C) \' X4 M. Z4 b( `; ?4 ]- Q7 D. p
- r6 z2 C" Z# t' S. v& ]
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。, o  H6 {" ]% [# V! ?1 C

+ d/ p2 o, p6 p
& S5 u4 A  Z; M; ^3 p0 ?/ `4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
( E( b/ v2 w1 Y- R" M6 n/ _* T5 H  `, Q, D0 e
6 |$ }. a( |5 @8 \% K
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。, s/ z3 A8 g$ Y9 S& z5 R
1 X, S0 q8 Q8 Y

. X0 y5 u: u* T9 s/ W& r- |4 [9 z* t3.x版本CRS" D4 D7 i0 ^9 [5 s- _
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 J! S  M. p' B. c! [3 v& p+ ]
# 生成例外排除请求的配置文件
' M0 G- s! `; P! q6 i5 A* @, S$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
; A' t7 K' N) ?# l, y  N, `# T: c$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf, o* R; d9 p4 O8 v6 ?
$ cp rules/*.data /usr/local/nginx/conf
) B7 i3 L& w) q$ Q6 J  ^8 z3 P为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
' Q& R) d) H* _2 r6 C, ]' K$ t; y% R0 X% T) x3 R

% l3 h" d# `# q) ^# a7 Z) @$ vim /usr/local/nginx/conf/modsec_includes.conf1 \1 C$ p, {  M9 |
! ~. D+ W# |2 i1 `- S
[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

' S& H8 H: l. |+ w
* {9 }/ r2 r( F0 w注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
9 @. v3 }( f1 r! y1 |4 v% X& k8 M
3 a/ v' B9 W: b6 {3 Y% x) P) d7 c' m# P) Z1 u6 k
配置Nginx支持Modsecurity
7 z+ r' o( |; c1 f9 y启用Modsecurity
& z1 h) K& A) h- A6 {) s* [7 J  {使用静态模块加载的配置方法# t; x  A3 l& n/ i( A& g$ j: u
在需要启用Modsecurity的主机的location下面加入下面两行即可:
. R8 C/ N2 J: G7 T3 b/ z4 r, H. y$ o, @8 l
. u$ t2 ?' Q1 s: N6 f' ]; w
ModSecurityEnabled on;
6 q5 @& o7 O2 O* j5 wModSecurityConfig modsec_includes.conf;7 X( D+ s- ~5 I9 }, S( |: E
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 [' j/ D/ \7 X( B4 ^* v
: W1 |1 S8 k- H4 B: |* V  s$ Z2 R/ b/ l9 y
$ vim /usr/local/nginx/conf/nginx.conf' h2 M6 J  ~! S  W) x% B& p- \' s
4 ?$ H9 a9 d' k& v" P
) T( D  _! v' `# y4 I
server {
9 B6 b) Q8 h& F$ u2 x% y' H  listen       80;- \: |7 \' N2 `& \* g9 H
  server_name  example.com;
, ]; l" n1 t+ v# [# k" J2 J* I6 d$ N# _0 f
% z# w+ p9 ]! h
  location / {5 ^0 m) ]* _  r5 y7 I' ]
    ModSecurityEnabled on;
5 w3 Z8 ]: G& {0 S2 ^    ModSecurityConfig modsec_includes.conf;7 A! l: W. s8 u! m
    root   html;9 ~6 }4 v* b2 e' D' N7 I& M
    index  index.html index.htm;
: x1 ^/ B  ^; f+ {6 b& n8 m* u  }
( n. s1 ~# S* `6 }! a# c}8 X: \* S% o1 V
使用动态模块加载的配置方法; D: ]' u5 K7 o
在需要启用Modsecurity的主机的location下面加入下面两行即可:
0 z# Y! _8 L0 E
* `$ _3 W) g& q. r: d
+ ?1 ]4 d9 m- o( f2 l+ amodsecurity on;$ \! e& U- e" g9 y; ~* w' W' D
modsecurity_rules_file modsec_includes.conf;6 F* I' i9 O  a! `1 r
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
+ M( i+ l' s; w# r' ~7 J) w7 |1 N! A; c" `
2 b3 [5 z0 w0 X
$ vim /usr/local/nginx/conf/nginx.conf
6 d9 G  j+ @; u3 E& Q1 p9 s5 Y

7 z; R& q+ i' Y9 e* Y* M0 [server {
7 Q' ?- _0 f) A' O  listen  80;& J8 ]/ X0 h* {, R# c: \2 |6 N$ x9 @
  server_name localhost mike.hi-linux.com;
& T" N! z% u  }5 ^. E  access_log /var/log/nginx/yourdomain.log;, @; u% p/ M' I0 b, q2 |1 ^
) W/ G1 v  ]  k. r0 Y6 W# A% ~# [+ q( W

: E" t" U6 I- {7 s  location / {
% U. P6 c: [% R. C
/ }4 u+ K2 B9 T  v& R6 w0 M& @
' C% H0 V! }( b7 q  modsecurity on;3 g3 Q, \3 g) y0 d& I+ y$ \
  modsecurity_rules_file modsec_includes.conf;3 @' m; R7 F2 l$ Z' X& n: V
  root   html;$ X! h, B6 Y" u! V* a' E
  index  index.html index.htm;
) q; p: I* u: p" j  x- q( X}1 C  l" ^: o4 p
}
% P2 Q9 ^- T0 r; P/ ?验证Nginx配置文件
6 j  O  T% V+ w# ]* f$ /usr/local/nginx/sbin/nginx -t
5 x5 v! E) Z  G! s6 {# y9 Z6 anginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok1 O, m' p# `  f& f7 d/ L' X
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful9 g& R6 J7 }8 X9 ~+ E
启动Nginx' y* L# ]3 C; d2 ?  j* c3 ~
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
( h0 Q3 I: F! ?7 Z- V  c
$ n, p- I$ b! T4 c$ T6 X) i- z7 g: D# U

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

4 \4 K7 ?! I# a4 m6 z

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-2-13 19:11 , Processed in 0.075631 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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