找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11518|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
; I" x# N$ H# S& R$ l) ~* V
3 l" }8 t# A  z- B( H$ d9 J' M/ s; S4 Z
$ w: w( f- B' @在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。3 o) Q4 `( B* l" |6 Q( S1 h, o- m

: u* K; ^. M/ K; z5 n
9 r* T/ u" _# i什么是ModSecurity8 ?! E; B* A- j. d
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。$ g2 m* o1 Y. p2 C; H
/ X, F) g2 |7 `0 o9 {* x

7 s- b: W- N  W% K( pModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
6 A  a) @! O2 f- H& f( Q. @
4 G% p* [% ^% U! A- _
0 M0 A: c7 Q/ ^3 IModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
& T- p4 o7 s( L" r. u+ T' b! V0 _2 a# z! \. y4 B" g' p
& m9 ]9 y/ i' w* B/ L1 P* a+ L' L
官网: https://www.modsecurity.org/( s) w( K! \) B/ r6 s# i, Z
( \3 u1 O7 y" M8 q
$ t+ {* ~! Z' @+ _) M: x$ T' n. L
什么是OWASP CRS
/ b$ A6 c  w. z) B: W9 b0 {OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
' r/ p  ~; _- B4 v. h# A
; ^7 E3 [& E" }6 v$ @
$ a' Z1 W; P2 \0 QModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。8 p4 G# V0 D: u
  W/ Q- \6 H7 `6 C' d

/ J5 U# F: \3 e4 t) r  WHTTP Protection(HTTP防御)9 S8 y0 @; O; E- p1 k' H
HTTP协议和本地定义使用的detectsviolations策略。& H5 i- ?1 f+ e% v# _  c: X
6 ^) X8 Q* O* j! }: d

7 E& C# S0 }1 B; GReal-time Blacklist Lookups(实时黑名单查询)& x$ p$ @/ |! T* l/ J& q0 G
利用第三方IP名单。+ L5 X# S0 d( @

. `! C8 E+ z0 D
' V+ S0 d- T1 Q8 I# aHTTP Denial of Service Protections(HTTP的拒绝服务保护)' ~3 [6 J5 p6 N  z* m
防御HTTP的洪水攻击和HTTP Dos攻击。' p2 a& `# V$ z/ _* v
" d  x  S, z# v  s6 L" u# B5 g. r

0 K$ m$ o5 y1 A( Q  ?Common Web Attacks Protection(常见的Web攻击防护)0 v/ e1 x4 ~+ i% j# p
检测常见的Web应用程序的安全攻击。) [4 M" q7 B! J' O! J
( _- L0 f" W# P" x
+ ~: w- s" \5 S* p8 X
Automation Detection(自动化检测); b  C8 b: I) f
检测机器人,爬虫,扫描仪和其他表面恶意活动。
4 H5 e. K) {5 M  I: ]+ J) \7 F7 o1 |; H/ D& Q

0 A& L. D5 I, r! c& \5 H% z  oIntegration with AV Scanning for File Uploads(文件上传防病毒扫描)5 i0 x4 K5 q, I+ X* f9 e: J
检测通过Web应用程序上传的恶意文件。7 d/ O) v: {+ }8 k6 i5 [1 u" W* C
# C4 A4 e1 g3 p- ^1 o" |% ]6 x0 c

1 y9 P2 P/ v& w$ e$ WTracking Sensitive Data(跟踪敏感数据)0 b! H+ @; `$ Q/ U. H( t
信用卡通道的使用,并阻止泄漏。, e- i" x6 M8 D  U* U
6 B6 P' }! H: E; e: u3 I5 k
8 i$ K" r  w- g5 e
Trojan Protection(木马防护)' }# b( N# {7 m- K: W  u. _% C
检测访问木马。3 v; Z9 U7 d0 A0 U) Z/ n+ D" g
/ Z# Q7 E, o% C. j4 @( a6 a! Q. T
# S; X2 f- V! ?
Identification of Application Defects(应用程序缺陷的鉴定)
% k5 \" _0 L/ i$ }检测应用程序的错误配置警报。# w4 H) v, q, e, L8 P

! a( [' ]) b/ N
$ ~& ?) a6 A$ G$ Z5 R1 B. \* N2 VError Detection and Hiding(错误检测和隐藏)
$ q) }& h% d: O3 r3 |8 F检测伪装服务器发送错误消息。
' Y  U3 U5 F8 l7 U" j" y  v2 h
: d, _! P/ {, S$ i. k" h. @+ f1 t
安装ModSecurity
8 u; u; l0 I0 |0 \; G% E6 b/ @软件基础环境准备
; z, R- c+ f* H' }/ G$ e- l2 a" b/ u下载对应软件包
# O2 a$ k6 Z4 D. [$ cd /root
9 Q' K6 D2 Y  o$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'9 B- O# Z4 _0 N, t7 Z  O0 t" G
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
; S5 o$ _- d( T5 B. t安装Nginx和ModSecurity依赖包
" A5 ?  j4 n9 |0 `Centos/RHEL$ j% p# o' Z9 ~0 M
+ {/ ?  l1 T5 o/ G- Z

' P8 O7 O  @8 E9 f$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel. L( U2 X5 M7 \1 D& V
Ubuntu/Debian  G- p2 w4 z% ^$ o" a4 W
( h8 o$ I  t- t" n
2 b7 W) |7 S! T1 Z# i/ q; }0 r; x+ B
$ 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
' o) Z, ]$ V9 @& Z# t8 {  ?. F编译安装ModSecurity2 [0 `! O& t) \4 b/ A
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。% Y" b% l8 {( o! V& x4 g  A

& l6 R: J1 O( Y4 m& v$ o  F
7 T+ j6 Z' m6 V" }3 U7 w. [方法一:编译为Nginx静态模块/ ^6 Z& [$ N5 W8 c5 x( L
; u' {( ]* u8 [7 I4 E) w  d  }
- K$ Q+ t; s* J1 e7 a$ t+ n2 O3 N
编译为独立模块(modsecurity-2.9.1)2 l, ]- D! n, R" |) z4 A& M
$ tar xzvf modsecurity-2.9.1.tar.gz0 r% a' N7 v" V6 w
$ cd modsecurity-2.9.1/& i. i; t) ?% K  G
$ ./autogen.sh
; u+ j. @& X' ^1 e6 K+ O$ ./configure --enable-standalone-module --disable-mlogc( Q' o% ?' y+ b9 n* A6 h2 K
$ make
6 B% ]: c  n; R编译安装Nginx并添加ModSecurity模块
8 @# d8 _7 m/ l7 @$ tar xzvf nginx-1.9.2.tar.gz
7 Z! K+ E$ i8 o$ s4 _$ cd nginx-1.9.2
8 v+ r: c' g8 f) v1 A$ @$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
; K& @8 b! Q! I+ J$ make && make install$ n) q5 r# [" ~+ _5 i5 S
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块
, @. O- E( c! V$ S2 v2 \
# ^3 C2 a+ y% w5 C) j) D0 H5 C
6 L' f# }  C: c( B编译LibModSecurity(modsecurity-3.0)1 k" T$ t! S4 J: C
$ cd /root
" O; b" |7 l4 r( X/ H3 }$ git clone https://github.com/SpiderLabs/ModSecurity2 d  M3 O" `* w4 Q/ O  e4 p
$ cd ModSecurity
& k) k+ g6 L9 x! ?$ git checkout -b v3/master origin/v3/master4 G5 k$ N" O. j( {- H
$ sh build.sh
1 q+ d5 g, {4 ~* @4 u" q# ]$ git submodule init" \/ h) p6 j- T: G6 \$ o  ]
$ git submodule update9 @/ e* J6 V" P' W- q
$ ./configure" [9 U( X9 `4 {8 d$ N' @5 h! ]6 S3 |
$ make
. j. A  D( r$ a$ make install
* v4 m! @. S) D% m  s8 ~LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
+ G- O8 W0 b2 ?( E5 ?3 ^3 ~6 x+ X/ }+ k- M5 j# f- @( }- v8 n
  J; n% q/ V! V: ~: [$ p0 `0 v
$ ls /usr/local/modsecurity/lib) p. H# Z( @9 x; R2 C$ z
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0! e  i( l+ S: Q: B2 b) C  x
编译安装Nginx并添加ModSecurity-Nginx Connector模块# ~6 ~( P0 v/ v/ D5 U4 |
使用ModSecurity-Nginx模块来连接LibModSecurity
, d8 G. Y  _9 g; Q- ~
: \" J5 K  c0 I! t: t1 v  _" c3 t( [3 u6 e" M. G
$ cd /root# Y1 {3 U. W8 {3 N& l/ D
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx
$ h8 D4 @8 Q6 w* l7 U$ tar xzvf nginx-1.9.2.tar.gz
, H6 N' G, s* _: M* U$ cd nginx-1.9.2
/ N4 N+ a8 {* g% g# y* `; ^4 z1 \1 r2 {$ ./configure --add-module=/root/modsecurity-nginx
& Z8 E; ]" g  c, C" p$ make
* }  c/ Q% a' n3 E+ R$ make && make install
8 a- s/ }) a5 s* C; q. n添加OWASP规则6 V, m2 _8 ~8 `6 K2 C6 V. o$ n4 `
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
% Y% P7 n) H; v0 d7 e! r! r1 \( ^$ |

! ~: e! ^3 @. e& n( ?: O* U下载OWASP规则并生成配置文件9 G9 {# n1 m+ [. ^
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
' W6 F: C7 W: `! A- u6 {$ y' l$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/* ~' V4 o, W) i- E% M" y
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs) P4 i5 L( Y: v3 J$ _
$ cp crs-setup.conf.example  crs-setup.conf& H' T) j) U. O
配置OWASP规则
) e% p# a) [- g( x编辑crs-setup.conf文件
0 {/ x4 L) ~+ h* g  d! h3 H
5 @0 G0 K& H1 x2 v! g7 [$ J4 P1 o1 T9 Z  W& w& x+ r0 Q9 v' s
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
  F) r/ G. |! f$ D0 O$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf* r# Q3 ^) z: O1 |
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
7 p( h, a" o9 O$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf' N4 \- D0 J2 ~
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。
5 I9 V2 t$ R. e3 q% Y+ o6 i$ Z0 H
$ m% N9 L" p' z2 F
启用ModSecurity模块和CRS规则3 @( s- H1 f0 `. u1 L0 a9 T
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  M3 Z7 }) y! j- ~7 N/ N0 U& H0 X& e
3 v1 m4 ~0 h8 M* n. I9 `! V5 d4 f; X
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
5 k- a; h" G; z, y* l8 y5 O3 C, y: i1 J: @9 j5 H
3 r" N' M9 \& }9 s& X( V
$ cd /root/modsecurity-2.9.1/
/ F# H; ?3 T1 m4 j8 h$ j$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf    ?+ N  Y( E. D' R
$ cp unicode.mapping  /usr/local/nginx/conf/
; V8 S2 X! W! ~5 F4 ^( P, E3 i0 A将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
* W/ t# J$ B  A" b3 |6 s( m
; r; ]6 T3 u1 Z( C) c- H4 ^0 T7 q* g3 r& F3 w
$ vim /usr/local/nginx/conf/modsecurity.conf
7 }  e$ n; p7 R0 Y4 GSecRuleEngine On/ `& ~& l8 g4 b7 c, E
ModSecurity中几个常用配置说明:
$ ?, X: u# Y4 J6 Z  d/ ]6 W5 o! X0 y5 k3 D" a

% F- H/ \0 `' k, m$ v. m9 r1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
, N3 M9 |5 k/ q% J+ n
. z* Z. u' s! Q' Z) _3 N7 X
$ @' [: a8 X/ J- g. `  `2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# ^1 W3 O  g# q% T* [6 p

  S5 e6 v5 V) B- Y5 f9 p6 Z1 M3 l- F" O( I, K) I
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
. s0 a" e' B/ b: p* _- K7 c( z6 g5 l& t( d, R

% d* y5 `6 ]0 @3 b0 h4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
" E, L2 G( s. G+ @0 Y2 N& ^1 ], S8 I8 l/ J( g& r$ s) ~

: x$ C  q2 R( D$ i) V. L在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。/ @% a0 s2 q3 I! h) Z$ Y: o0 \' {
$ @$ p5 \0 U2 m6 ?

# e3 |: C$ J( L3.x版本CRS
0 _* I5 ^" t( E3 c5 J$ cd /usr/local/nginx/conf/owasp-modsecurity-crs3 ?; s: }1 [! v, a5 K
# 生成例外排除请求的配置文件5 R  v7 N  w+ `! I7 h
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
) }) T- S; D$ F3 w( Y7 {! }$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf" z- l7 h8 b7 ?1 d3 c
$ cp rules/*.data /usr/local/nginx/conf- @8 F$ o1 U6 A0 g4 u; F( v
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
  H; {  |' o5 E; i8 v& G) _
# p( h4 e. O' v* J1 \' }5 w  P4 _6 v" y' |3 \6 W# N
$ vim /usr/local/nginx/conf/modsec_includes.conf
. r8 b5 T" Y8 t# Z& w9 i4 a! z$ T+ ?# ?, N. A. w2 i
[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
8 m, K$ G  h0 D
" t. H  P+ i5 b6 i& A  T" c( s
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。% w" E, W: l% N8 i
1 T- j- c; M1 b2 ]; c# w* x/ S7 _0 N

$ ?9 C  F$ o0 J5 k4 R) h$ h配置Nginx支持Modsecurity8 b+ y) q/ u/ ~) h# r9 T
启用Modsecurity) n( p: t7 t  U* {5 J" x
使用静态模块加载的配置方法
; m& x# _: s; {+ u& I2 F3 m在需要启用Modsecurity的主机的location下面加入下面两行即可:. X* A/ L. R/ {6 n" q! y

& o( p+ h2 a0 h3 M: f* ^0 u9 v  W5 |3 a9 r
ModSecurityEnabled on;; [# o" z$ Y/ k
ModSecurityConfig modsec_includes.conf;+ I: C- d2 G5 @- R' s
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
8 B1 Z4 t- g% \; v) l' X0 k) B# V! O" j7 d" C: r0 ~7 i1 M8 K

( H; u' E' e. T  J& t+ l$ vim /usr/local/nginx/conf/nginx.conf
0 g0 r. i6 s9 {/ }1 {$ W
+ S) c: L! R% k8 D7 D$ F1 L6 w
- T' y; K6 t! mserver {  H, F, K5 ?) w2 _2 ?' c) ?3 y9 f
  listen       80;( Z, l9 W/ C1 |, w
  server_name  example.com;
9 c. |/ w8 v+ `9 k1 ^) d5 r- o% g  e+ q* I& m. m
3 V0 B6 b, m/ C' d3 G2 R
  location / {! {9 n! r; i3 g
    ModSecurityEnabled on;8 S  W" @+ Q5 \) U" O: g* S- F8 [
    ModSecurityConfig modsec_includes.conf;! @& i$ b8 }3 M
    root   html;7 a& \' [/ C% G6 L# Z4 l; U# c
    index  index.html index.htm;0 M" I! c% y3 d* T% q" S
  }
) ?! O, R3 l+ ~4 m}/ Z' A( Z* d  e9 h8 ~
使用动态模块加载的配置方法9 V4 z6 o; ]" y1 F7 A" D: j: y- f
在需要启用Modsecurity的主机的location下面加入下面两行即可:) C' D; t/ c9 I6 t5 z9 L- u; I

, d& _3 X( U8 Y3 j/ E- T0 O: D0 c# k5 F: J  @4 N; E  F
modsecurity on;
: W" ]9 F, L% |+ S# vmodsecurity_rules_file modsec_includes.conf;
8 `7 j4 r. _/ T* A8 V5 k修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 p. y7 Z* Y, b' O+ g) X& h# j- A. M' K/ j3 {7 b
+ i/ `/ A( S. F* ?/ n5 X% s- r  ?
$ vim /usr/local/nginx/conf/nginx.conf
$ D& z/ g" @0 F. v. D  u/ a3 a; n$ u! O, n5 m. M

5 }1 K2 A' V% f& m8 a7 p$ l6 i) [server {
& v' E' _. m& _7 b) K  listen  80;/ q  d( R" q0 o) x- F
  server_name localhost mike.hi-linux.com;4 N6 o* G+ u1 p: G
  access_log /var/log/nginx/yourdomain.log;
$ u4 i0 F8 p" _. N
, L' {3 o/ f1 _  p
  G5 x3 M  z4 a2 z0 q& v  location / {
+ ^) w: W. E% Z7 W' v
! e/ S! v. n: x: w2 P$ T* o7 Y, w6 d3 y8 K
  modsecurity on;: `- t! `5 ^$ s& U
  modsecurity_rules_file modsec_includes.conf;. v  d1 Y* ^, a
  root   html;! o. U4 C& H9 u3 S: }7 N
  index  index.html index.htm;
* ~1 x* R' l* |! \7 y3 ~. x/ Z}, i1 u" r, K: p& Y3 f# K' G
}. J$ R+ C4 K; O2 M6 Z# ]5 [
验证Nginx配置文件. g( `  Y" E) z5 E7 ^$ }( o
$ /usr/local/nginx/sbin/nginx -t" |( N* O7 }( E# L* y5 @
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok6 S. ^& H# \- Y9 R
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  ]9 p2 D1 e. T" D# Y& B启动Nginx6 E' [$ n9 N; a2 o% n$ J  n2 I
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf9 b* U' \/ V3 o( D" q9 h! e. \8 Y
6 u0 t* Z% ?  g& g0 G# o

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


- r) z3 S5 c1 C4 y3 ~. x, K9 W' d

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-6-20 05:41 , Processed in 0.061412 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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