找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11249|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
9 ~: A+ a7 A# q
" K% b, B6 M" A/ F/ m5 A
/ J" r( s" c( M, d( z在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
' x8 D! w# p; o: F5 D1 j0 a2 ?  q) Y! t2 W6 r* A0 R' d+ p

# T4 k3 s: m0 w  J: \什么是ModSecurity& r2 [. t% R, F( ?5 i2 N
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。2 p* K' g; b) I: E5 d% T
8 E3 O' o0 m0 _/ y3 E% |
8 h" l' g# c$ L' f  S
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。3 G: t2 b2 V- L8 C) f6 s; W' E8 r7 e

9 z% {7 X8 J# f4 Y7 b' Y7 w
8 m$ ]5 v/ F  C& H9 G) h( |' lModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
+ p( ?) {# G6 T) R3 x6 j7 d
, ^( K' m* Y" i0 h1 i/ h; w& g5 y! P8 ?& ^! `
官网: https://www.modsecurity.org/; b, r) F# O# U+ T8 N( Y

, s3 ~' W2 v+ D& W5 D9 U' k3 }7 n8 o8 y" _+ F  B
什么是OWASP CRS1 [( T5 r6 W5 v5 ]  @; W2 @
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。/ \2 F) |* M* ?7 U$ D" N* ]

1 M2 Y3 [4 y4 i6 y  R2 w3 q
9 G+ p! Q9 {1 S3 ~8 rModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
0 s+ T8 u/ [; _/ }  m2 b$ a: h: D) B8 L* _8 F6 [  i$ {
0 U& l( x- |5 e! m, Y
HTTP Protection(HTTP防御)& L0 l8 m2 ^' q5 ]' x! m
HTTP协议和本地定义使用的detectsviolations策略。
7 a/ s+ k8 z$ U8 |$ y6 f4 h: S
% y0 X& R9 H' R7 P8 c
+ i; Y# e$ J# L1 g5 \# vReal-time Blacklist Lookups(实时黑名单查询)1 j5 W1 U* C( U9 ^; H5 _2 u
利用第三方IP名单。
' H  s9 }4 M& f( N# L
' h: _  o/ G0 ?' Y  H% }( `/ s- @% Y5 J: ^, B+ y# V
HTTP Denial of Service Protections(HTTP的拒绝服务保护)3 w) e0 [0 @, t/ Z" g* [
防御HTTP的洪水攻击和HTTP Dos攻击。
$ [+ m9 K) q) U8 e6 g6 t* n5 `) {0 e' z

* P6 h5 N) a: h7 }: s9 j) tCommon Web Attacks Protection(常见的Web攻击防护)
! T% p; v" Z/ e. t检测常见的Web应用程序的安全攻击。
: n& ?3 v# R% b+ a
5 g7 _5 x4 u0 p5 G5 |8 A5 _8 i2 b5 f; J1 k3 z
Automation Detection(自动化检测)) ]* O8 u# g* e
检测机器人,爬虫,扫描仪和其他表面恶意活动。& a+ f* x" e: E
6 T3 k. X- V+ f3 M
: Q, R- M1 L) u
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)" \$ S1 R6 a2 n" M& {
检测通过Web应用程序上传的恶意文件。
* H1 y: b$ [; O8 v+ e; B' k! [+ V2 Y( E2 J# z7 T

7 k3 H- ]; e, l3 lTracking Sensitive Data(跟踪敏感数据)
3 J5 }# {$ ?  P' ^5 m: R& l信用卡通道的使用,并阻止泄漏。
% D8 J4 n; n) _: d2 L$ a! S
4 n: ?' f  m, A  K
9 ^& d1 `8 O5 u' {Trojan Protection(木马防护)
" ^: B2 [' x3 s+ l/ L' F0 `$ {检测访问木马。* R+ m# P% f0 b# i- J( y6 H- y9 |6 t

- L6 {. }9 N* _: c1 `- I
. Z. j$ k, o$ m4 |  F3 XIdentification of Application Defects(应用程序缺陷的鉴定)" X$ M, Z' |6 t, }9 d/ S
检测应用程序的错误配置警报。7 n- {- J0 {- ?) Y4 S

4 d; }  u) i8 p6 }$ A
! F& n2 E  j; v( m) vError Detection and Hiding(错误检测和隐藏)
- T6 N* `/ S% T; v检测伪装服务器发送错误消息。: o( [: B& K  V3 u9 c# g' ^# k
, _9 B* f4 J- m. B, E/ B

# m6 p0 _7 r" F! ?( g7 j, k/ t安装ModSecurity
" r2 j' I: w8 e3 s8 C& I/ _% i5 H软件基础环境准备
3 z) p7 w% N1 G- s& f$ m下载对应软件包
! G/ A) j% d& t. w7 {% n$ cd /root
1 \6 E& F) O3 t, A9 G2 m$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'; M' r$ y$ \- B+ T' ]5 V1 E
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
% t- d( E1 r1 t# R安装Nginx和ModSecurity依赖包/ k, H! z' X, g3 T4 d  G
Centos/RHEL
2 Z2 y1 {7 h2 V& x- U+ j3 ^$ C" u3 u/ \$ a
/ T1 P- b# `+ d5 G3 k8 A! D# U4 N1 {
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel' e& j" O: q: n9 q! D- c
Ubuntu/Debian
- Z4 _2 I4 z# _7 W: v" G" a
. y/ A5 k# G+ r* @9 e& @/ e5 f8 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$ Z5 N/ ?- v9 w4 Q  R
编译安装ModSecurity# C+ [: Y. w' {# m9 J$ n& E2 }* w
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
- t* @8 ~( t# Z& {0 N6 @/ k9 l' w; a) U* @  h1 q2 Q

5 v1 u% t& x' d, c; n方法一:编译为Nginx静态模块
. T0 B' z! V8 ^, F
% C8 G& {8 B3 H7 Y, z( h- i3 h5 N4 a8 x* A6 ~6 s# M+ d
编译为独立模块(modsecurity-2.9.1)
; ^% f( {) ^6 R0 a7 U, n$ tar xzvf modsecurity-2.9.1.tar.gz
/ m% @& f6 f4 b" Q; r  f& Z$ cd modsecurity-2.9.1/
& H' q2 X5 C. |5 d# X( E$ ./autogen.sh: Q% ~1 _1 K3 ?/ B
$ ./configure --enable-standalone-module --disable-mlogc5 Z" x5 j4 `6 o5 S5 ]
$ make
% h/ z. I2 C. W5 B2 v) Y& F6 b- d' l编译安装Nginx并添加ModSecurity模块  q7 P9 {) @7 Q9 O. P) R
$ tar xzvf nginx-1.9.2.tar.gz
$ W& y  z! n- Q5 G. Q& l. c: g4 a$ cd nginx-1.9.25 \: d2 i0 Q$ U5 G% z+ a+ U5 J
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/1 I) ?/ d0 \' [7 f, U
$ make && make install
- E  _8 _, M+ a% H8 z; ?. Q方法二:编译通过ModSecurity-Nginx Connector加载的动态模块/ V& k8 ]  d, u$ F8 K, t7 S

) ?9 q" r8 @: ^1 m" R: X0 L
2 g" {$ R  s+ |8 T编译LibModSecurity(modsecurity-3.0)  j; _& F0 u+ D0 [  S+ r
$ cd /root
+ Z% [( Y' G$ L* I$ git clone https://github.com/SpiderLabs/ModSecurity4 |5 j6 D) O- H4 D
$ cd ModSecurity) B9 q( p( `1 ]$ Q  {& f
$ git checkout -b v3/master origin/v3/master& [/ P6 c8 V; x7 Q. o
$ sh build.sh" C( S9 ]# e& W) T0 e
$ git submodule init! S, V  I4 T3 K( j! L0 l
$ git submodule update' X3 t0 v, T* Z( r
$ ./configure
  m* L* @* k5 C/ O: A$ make
% u2 l0 f+ z) i* H& X1 n; |/ {% }$ make install3 y& C: ^2 P8 E0 H
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
1 ~8 E( o* G. `; b" _' k# C- J
- m$ j7 f; y& p1 r2 _. @/ N! E7 \* _
$ ls /usr/local/modsecurity/lib$ w8 U4 ?5 t( _5 V- z/ v
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
0 Z4 i; s( B  z6 R编译安装Nginx并添加ModSecurity-Nginx Connector模块
# B+ w& K7 f* I使用ModSecurity-Nginx模块来连接LibModSecurity8 a/ u7 A$ n3 N0 t4 d; R- s
7 X+ F7 u- }. E0 f2 `
9 K% V1 g0 O) _8 y
$ cd /root
3 o; d' U2 i5 ]& m. F$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
* A$ \# a8 e3 v2 Q$ tar xzvf nginx-1.9.2.tar.gz4 I; J; R6 W- z* z; N
$ cd nginx-1.9.2# W8 F- A0 h7 X5 ~$ o9 {  H
$ ./configure --add-module=/root/modsecurity-nginx4 l3 N/ |8 o! k! ?
$ make
3 ?% J4 r  P" F' f- a) d7 h; q  e' u$ make && make install& U/ {5 h/ B! N2 V
添加OWASP规则
; d7 |+ o5 W: J( xModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。& G# s8 \: j8 O; y

2 T8 o5 y* }+ O# g/ T  V, g3 u3 }! P$ f# O8 J$ |
下载OWASP规则并生成配置文件9 v0 z8 ~8 l, @' Z) d) ~" F* A
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
8 Q* B/ e% P& \8 _, [3 H+ n$ u$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
8 ~5 ~- g! t( p" b+ v2 {) i$ cd /usr/local/nginx/conf/owasp-modsecurity-crs- e, X+ r* i' n0 w1 |6 n+ e
$ cp crs-setup.conf.example  crs-setup.conf" O  n7 n: e3 D8 W" f5 e
配置OWASP规则" A# ]( Q- p0 R, _. H7 D2 Y# R
编辑crs-setup.conf文件0 d7 c% P. M# X% A9 S
9 A% D, m$ u* k4 p# g. A6 U/ C
/ ?% k  @( F/ P; z, q/ K/ N! S
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
: K. k* x0 y( Q# U$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
+ r8 L' p6 m; c4 d: G% f7 Z$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf% f) b$ b2 d1 B3 j& Y
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
( D8 r% x' N& {* n2 L. j7 A( `默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。0 [, V7 _  @7 F& e' Q' ^3 G. Z" S0 d

  s, i  b2 \0 M1 ]
9 f0 Z0 z8 A7 n4 b5 x启用ModSecurity模块和CRS规则
* J0 U0 r( }, v7 j复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 w) ?$ \! |# }; T+ O6 G2 K8 F- \" o* s/ [. u' e

# C% T5 C6 w8 I; K5 Q, emodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。4 @! ?/ y; [, S* `

! Z6 h- S+ h. Q( b: E+ |* ^6 ^. f6 u7 u! t6 f
$ cd /root/modsecurity-2.9.1/6 w" i% ^2 B- y% z5 B/ ^* K. F+ k
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
" W5 H1 S& p! ?$ cp unicode.mapping  /usr/local/nginx/conf/  C; w6 x: E3 b1 [/ w0 X/ x( Y
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
3 A; D% Z0 Y+ }- H$ B! j, ^) c* f  Y
5 i5 D9 K9 [0 i9 C- ?# Q* t9 a, A
2 X' j% N1 c! Y4 `# _9 j2 ?$ vim /usr/local/nginx/conf/modsecurity.conf/ J6 j- V7 j% o! z3 @& t& ?
SecRuleEngine On8 a/ e" t' E$ k; j: ~. B$ `
ModSecurity中几个常用配置说明:
: L1 [. s2 _1 Q5 ~; R- W- V+ _& J) P& s% X; m* u4 r, \! F8 Y! [

$ H) \9 J% \5 z3 |0 i* p( Y& ?+ ~1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
1 J# S* h: i8 {' }7 _( [0 X) D7 h7 S7 R/ L" @
* ?5 U3 _8 ^0 Y4 ]8 ~
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。6 m% D# h6 f" o
- t1 X, t3 d! b) J) b

" m, r% T: P! |1 q6 d3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
" h- j2 _/ u3 Y( N( I# q# f& Q* S/ i6 g. [5 q5 F! m' D
7 p  ~: z6 x% L# U9 v& g
4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
, T6 j, R' @3 p: j5 O* M; {" G/ W* V0 _! F( q* t& _8 k
& u% \* I* [4 f1 Q8 U( l1 h
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。) X3 l3 m6 p4 e, N# _5 @& x

- a% B1 \' }" O
: ~0 E0 `. k& X0 J9 i- l3.x版本CRS
0 j- Y6 O  r+ {$ ~$ cd /usr/local/nginx/conf/owasp-modsecurity-crs9 Q9 v) M$ S6 F! m
# 生成例外排除请求的配置文件2 G2 ~: P# M  m: `. J3 \
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
$ X1 h5 e4 w+ |4 A* H$ v/ W1 _$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf8 M0 ?1 M, _* N& @
$ cp rules/*.data /usr/local/nginx/conf
, r, E4 I3 ?. m$ |为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
! U- n2 X7 r( Q& P
/ k5 F& w0 u" I+ x* o( x+ v, j8 J2 N+ r, d( B
$ vim /usr/local/nginx/conf/modsec_includes.conf8 |, J" I6 S8 b( V

# v% }5 b) |0 p8 t
[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
0 x2 ?' r( T) g

3 I4 v0 D6 L0 l& c( K6 Z* ^注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。$ s5 b# g) h5 X% {# B0 a; e
- n% N+ u. j6 l3 N7 f0 `
, e; y0 P. s  ~& M9 Q
配置Nginx支持Modsecurity7 t" h) k0 {7 |
启用Modsecurity( D+ J8 y; f! x! r. y8 _/ v, C
使用静态模块加载的配置方法
6 B. r7 Q1 C) w$ v4 O在需要启用Modsecurity的主机的location下面加入下面两行即可:
: z- p5 K5 a, o1 q: e6 K+ g
1 \% A% L8 ?, Z1 m% C9 V$ w
* }+ e# p: R: I# t3 o' J. [; NModSecurityEnabled on;
9 u9 A5 m+ x1 C/ k' S" P& }1 ?ModSecurityConfig modsec_includes.conf;
9 @6 |" M4 @# Z修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。% h! e' j3 Z' s) Z) ~( [
' t1 x2 _# e. y# ]7 c; O

/ E. b9 x0 k7 s# X: l$ vim /usr/local/nginx/conf/nginx.conf
' I$ V( u: O/ ]  s( c9 r) O- y! C
5 w- ?) F# n  v# O1 K/ t) S  B+ B' {& [' X* J
server {
2 V! e2 Z( p5 }( n: q2 S  listen       80;
- ~: V3 o) J9 Q% J; l  server_name  example.com;8 k9 b% M4 ^, F. ^: \! ?- {

! e, H" C3 @' |
- a% c5 u6 I' k4 s9 o  location / {' o7 p* J7 U1 h- P" r6 i0 x6 g
    ModSecurityEnabled on;! y$ l4 n5 b( K, d/ v$ ^' M
    ModSecurityConfig modsec_includes.conf;
( p8 n. ^# x" x: m/ z    root   html;; C, F+ d8 {& x1 y" p$ s
    index  index.html index.htm;5 y3 s$ w+ ^7 h2 P6 H+ b
  }
% J  j% L5 E  b* B6 g# ]2 [}
0 o5 a" p- G8 J9 |7 ^' \使用动态模块加载的配置方法! J7 s4 y0 X! v5 Q' S1 k0 c3 N4 J" y  t
在需要启用Modsecurity的主机的location下面加入下面两行即可:
& e& a) O2 Y; B  k2 }5 u
( h% J! k9 h4 h! z/ F3 L& Q  H) r$ W2 j" F6 M
modsecurity on;
2 c+ s5 u& P# M, J6 v3 `modsecurity_rules_file modsec_includes.conf;8 `* t' U; T* y6 a1 Q9 M
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
6 A* |9 t" j- Z- u1 {, k0 ^0 e% l; |5 I+ v& R1 Z
9 G$ Q! m; y4 X& d; T0 e
$ vim /usr/local/nginx/conf/nginx.conf
! e' L+ E  @* \$ ~+ G) A6 O& X: m  I- D$ }- S$ Q

4 t) K) h5 f; h# i" O) [; s8 b4 D% Bserver {# |3 ]5 c9 m& d, H4 l
  listen  80;
" f5 l1 _% I" P' b2 v8 t  server_name localhost mike.hi-linux.com;; K5 T% q+ F& z* S4 Q
  access_log /var/log/nginx/yourdomain.log;# }# e2 k5 d' v% @
4 C( R4 O$ V; S3 `4 e% }' S: U
* O6 N2 S  o' I# V2 M) x; f, i3 d
  location / {. u; a) V# m" H& T9 R0 ^) t; g
8 q5 F! i8 q7 z+ y4 l5 R) I

1 P; M& F+ B4 ~  modsecurity on;
2 h3 g  _% p' D- O& H  modsecurity_rules_file modsec_includes.conf;
7 H" i  C; Q. K" ?+ L  root   html;
8 |0 s1 `, r8 S8 l7 v2 d  index  index.html index.htm;
4 ~$ ~- n. ?: W+ g}  J7 h! i8 S0 c* S) q0 z) B
}
5 }- J9 E) ^' F验证Nginx配置文件# P- c( t3 }! m) g6 e
$ /usr/local/nginx/sbin/nginx -t
- n5 G6 x: C. I- Enginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok- m+ ?1 K/ Y0 e0 \8 a0 |5 L
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
0 I- X6 }+ G6 p7 r启动Nginx
: D% w/ V5 x( A$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2 L3 l0 a( w, }9 D% m3 G" a, n3 P* J3 D" L% }2 x2 @4 C

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

- d" C& ~. j+ N5 @8 A1 [

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-22 09:24 , Processed in 0.066442 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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