找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11238|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。2 c6 W+ e6 [" X, g, u
$ |" ~6 n8 U3 ~9 D* C, J" ^- g

9 ?; T) `0 G0 p, L4 v4 @/ ?( U0 o! E在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
! K0 p1 c* q" K; M
- j& S- i( o+ W, P: K% o6 w9 x
( e4 Y' ^, v' [6 D3 x. @什么是ModSecurity' J; `6 b7 I6 F" J
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。3 @1 b6 j* S, }2 K2 {  P& q. ]
6 w* A, s  v2 U5 x, _
+ l" Z1 }8 B( D/ U
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。0 Y3 ~; A/ r* L- E1 N2 n' r
. i% K: ^8 A0 S2 ]( G) V

. k8 m7 W5 k( O" S: D& S7 n" y6 TModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
" Z0 @- l: T) |. k3 ]( P/ o' f
0 i6 R  @; ?( Z
: b7 L% J3 ?0 @/ \& q2 ~) B: }官网: https://www.modsecurity.org/5 w& b) r+ F# X2 X2 |3 l( Q9 }# x
- `' [+ a% u2 V& l+ k5 D& N8 R

! d, a) e/ u+ V" q) o& A什么是OWASP CRS
6 a: L' c6 t+ _& YOWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
* V8 t& S& |0 u5 v0 e
! h$ f, y! R1 |! p" {
/ v6 [0 g6 k2 E' e7 W# GModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
, X, E$ N6 y1 s$ i1 k. ?: `" ^7 K+ P, r$ C5 y! d

3 w" G/ A$ b2 o+ J; N7 S: bHTTP Protection(HTTP防御)
5 ^. G2 O* B( N& W$ M6 F7 BHTTP协议和本地定义使用的detectsviolations策略。
7 y( _% o/ ]: V/ }4 l) y
$ y3 v: {. l6 ?2 E- }
8 s  ]' p- b" S: M: ~# r4 aReal-time Blacklist Lookups(实时黑名单查询)  g% h6 K4 H, A4 W8 K1 e, ?
利用第三方IP名单。; U9 A( m& p) Z/ x1 ~" Y. W
4 p- [" {8 o1 m% d

% R4 l5 O9 E  {* ?* p7 {HTTP Denial of Service Protections(HTTP的拒绝服务保护)
; {  j& w8 ]7 R( R3 i防御HTTP的洪水攻击和HTTP Dos攻击。5 E9 s9 o" B! L0 V
- m4 T4 a9 O! G1 b, u2 T6 U8 x2 W
8 `  @8 ?! _$ y% ?6 \5 @
Common Web Attacks Protection(常见的Web攻击防护)
. f" Z3 k0 y/ V, A7 {. ^检测常见的Web应用程序的安全攻击。
, L% Y0 M5 Z% g  ^' Y+ j- x  \- W% J5 v7 H2 X/ A% Z" D" u, o

. g+ v4 j( Q8 }( R- {1 AAutomation Detection(自动化检测)( W- v7 {3 b. e) {6 r8 {$ Z0 F
检测机器人,爬虫,扫描仪和其他表面恶意活动。' p8 S( d% B! T8 D$ |' _

) M+ f4 c; g' z. t1 R9 T4 H# {' y3 ?
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)/ K% F, l+ c0 H3 ~
检测通过Web应用程序上传的恶意文件。
! b* w: \- ]; o/ t& Q) _4 }% }+ G3 m! D8 c) i. w8 G
5 J6 l- t% n1 f& L
Tracking Sensitive Data(跟踪敏感数据)& @$ a8 u7 ?  }6 T+ k. @
信用卡通道的使用,并阻止泄漏。
. z) p8 \( k$ i$ J# A% C  W% B4 n1 _
$ c  ^3 W1 ]5 F8 k0 P
+ M) G( b5 e7 d% @, KTrojan Protection(木马防护)% j2 L0 @" ^1 h, {" b
检测访问木马。
5 Q0 X4 R) f& e+ _7 _  [# p% P( c) x* M  H) ~! v4 l
* ^  D5 }$ @, b( E2 T
Identification of Application Defects(应用程序缺陷的鉴定)
: _: y$ B/ ?, o) y" t7 i& h检测应用程序的错误配置警报。% H: `% |: s9 d6 }- j& p

2 J) `6 U7 b$ O+ V0 D. T# \( w
; F8 P1 s+ ~: G+ i1 U4 }Error Detection and Hiding(错误检测和隐藏)
3 g. ~6 F6 P2 Q7 A; }# j8 I检测伪装服务器发送错误消息。5 m& I. z7 K0 E- U0 p! U# }

1 K5 C! j$ C) J% ^8 e: p3 F# V. y7 H3 M2 h0 P. @7 A0 p) T
安装ModSecurity
- J$ Z* _+ }  K软件基础环境准备
, U  j1 ?1 j0 @/ J  ~下载对应软件包# v' K3 w3 Y/ ]6 u/ a0 q. _2 F
$ cd /root
* W5 Z! c( b0 U# k2 C* T$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'% @1 J$ O6 r6 @$ A/ O
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz
: o. d" y: i% t' V. J3 {) @( j/ Q安装Nginx和ModSecurity依赖包
+ D, r5 v: H! ]Centos/RHEL7 I, B+ U3 m, s5 |/ C( d
! d% W8 C) q) I) E2 R

( R" C( G2 w) Q) @8 e, a$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel
  B9 g1 P5 [6 c4 }  T! H/ Z- @) TUbuntu/Debian$ k3 ~) N; g: s; Q* i  X! j
; |. d4 c9 q9 u
0 a9 w$ X1 A9 R4 C( S+ G8 n
$ 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( C. R* y4 K  |' C6 j  x
编译安装ModSecurity
/ B+ O3 l+ m. R% jNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
4 e1 m& p/ C! X1 Y5 D( ~5 K1 ]% @& ^
7 Z2 }% C- n  R( l3 Z( D3 D2 @+ s: \2 [' z
方法一:编译为Nginx静态模块
% `$ X/ H( X4 u2 h& L  Q( W3 a1 N. H$ R: O1 K9 p, R

- X7 P! b' E; w! O5 ]9 u编译为独立模块(modsecurity-2.9.1)
; u" n9 A" {! z" F/ p! X( X$ tar xzvf modsecurity-2.9.1.tar.gz6 }1 S# V# Z; }0 a
$ cd modsecurity-2.9.1/% e1 b$ x' ?- y
$ ./autogen.sh
6 U3 h3 l/ |( a* K3 z! G$ ./configure --enable-standalone-module --disable-mlogc
7 K1 w% L* |# s. g+ `$ make; k2 N! W$ C  I
编译安装Nginx并添加ModSecurity模块1 l( Q# l" u! o4 Y& x: B. H
$ tar xzvf nginx-1.9.2.tar.gz
( Y2 M$ p" J5 r$ cd nginx-1.9.2  ~- ~- t1 ]% v2 `" G8 {
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/' l8 N! e$ [; b
$ make && make install' O7 Q% _0 s# G# V
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块( Q6 `6 _, e' D1 U

3 V# h0 Z: h( S) n$ b& I" ~% H& Z: G$ @$ j  h& N
编译LibModSecurity(modsecurity-3.0)
  {4 p$ `& y; a5 W) c8 {6 {$ i$ cd /root
6 M6 _: L9 r" M/ t, h' K& F9 v. }$ git clone https://github.com/SpiderLabs/ModSecurity# S* ]) x3 B% j- j% J" m
$ cd ModSecurity
/ c) F/ I, J. ^- H6 `* S) ^$ git checkout -b v3/master origin/v3/master5 S) r- S  B+ t5 {8 G* T0 ]* s6 b
$ sh build.sh! Y. R3 J# a* N* g/ A. i; ]
$ git submodule init
9 P" D5 l% z" B' U$ git submodule update
& \" h# M3 p/ q& [) x+ D' Q# h$ ./configure2 R. `5 @) Y, ?$ z/ A; o/ l) O
$ make
( z. F( X  V3 E% q0 V" b$ make install- Z/ M, `7 i( ^" V% Y, l- q
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
: k4 e% w5 a1 T
( `% h7 h2 P/ q7 V# _$ I! J+ g% Q; I9 U
$ ls /usr/local/modsecurity/lib( A2 M* q1 Y( F. ?8 l" O1 V$ x
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0: P. x9 E5 b4 J+ K% t
编译安装Nginx并添加ModSecurity-Nginx Connector模块
; X2 \/ e# K& Q& v4 n使用ModSecurity-Nginx模块来连接LibModSecurity; y: g: y; ^2 u8 _

$ D8 w7 z! S3 [9 `1 B$ l/ \* a5 W: g- g! z$ ^/ f
$ cd /root
& h* U7 S" a# R2 f0 U( E8 g$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx3 O2 q, c6 N" i$ v
$ tar xzvf nginx-1.9.2.tar.gz
8 N# h% T+ E0 @# P0 o$ i7 h- C$ cd nginx-1.9.2
7 ~5 U2 y( k2 X* H$ ./configure --add-module=/root/modsecurity-nginx
  s8 T! o% G) X# y$ make& x  ?) x, Z& ^3 n2 S# P
$ make && make install" O' j9 c2 \: E8 n
添加OWASP规则' C0 h* I6 ?8 x. v
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。9 G" e- L% K# ?! x7 m* p
; M0 P8 E- U8 _' ^( }3 K0 ^

0 d4 |0 Q9 L4 e1 y) H下载OWASP规则并生成配置文件
& D3 ~$ q7 P' A4 r- L7 j  ?9 Y- S$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
; }& x4 O, k" e" C3 K$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/
, i) {  W) [$ u' R% w8 ~, ~$ cd /usr/local/nginx/conf/owasp-modsecurity-crs/ X9 M0 ^0 M7 w+ D0 m
$ cp crs-setup.conf.example  crs-setup.conf
& L% g4 a2 N# v7 f配置OWASP规则
3 @1 h/ w( F; w9 l1 a/ N! H+ K$ k' P编辑crs-setup.conf文件
# O5 t7 C2 |" ^6 u; r: J% d9 x6 m1 M% H3 K$ b
1 X# P( V0 b5 N4 N6 ?2 m0 E4 N8 Z
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf6 ^$ G) ~: ]; N! E$ C6 E7 z
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf
2 n! ?0 f9 X- Q* K: W$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
0 F8 Z2 }2 y7 n  u$ f$ ~5 t; u$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 X2 C6 A6 Z( q. l
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。3 w  s3 D, S7 @- v* w
; {1 }- \) |: n6 u1 M2 Z7 C
! z7 e. z$ w1 e, E* ?: t
启用ModSecurity模块和CRS规则9 Z7 q) \5 {; V. j5 i3 b/ h
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  K) X3 t) N1 M7 G$ g/ ~: ^) V8 i5 X3 {3 F* r
& V/ E6 v" x1 Y; G8 w  r: j
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。% O/ C+ A1 i9 i- ?( v: M
& J$ D. y7 E$ `  m7 Y
/ x+ H- t# E0 B) C' D
$ cd /root/modsecurity-2.9.1/
% [" {; B7 g  r$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  " B0 h1 `' y  u! z
$ cp unicode.mapping  /usr/local/nginx/conf/
6 ~! o  B6 L" J5 K% B+ \' _将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& T2 x8 k) Z& @% q$ d7 H9 r. Z, |* |! Q5 K( N/ U$ Q

4 P. Y$ d7 I( h9 U6 g$ vim /usr/local/nginx/conf/modsecurity.conf- A" k2 h) J4 W* ]* K2 R$ I" K
SecRuleEngine On
8 T) r! z( c& g5 w* X3 e" N# oModSecurity中几个常用配置说明:
& f! w4 E# L) _. J4 I+ l
4 v% S- Q2 \% d  a1 c
: H! Y: L! `: `# C: j* s  w: j$ v1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。) v; u/ a/ \, i( Y! M3 r8 [

7 s' H6 T# m6 f1 [* r& A9 ]+ E7 a6 ]/ q( p% c  ~' r# Y
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。# }& C/ G3 \' F* Y" Y

# \. U7 Y, M: O) F, F* ^& e8 O! o' b5 b) k2 e$ j' B
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。+ i0 w( Q3 e2 `+ k
; h7 Z9 G/ s: o! F7 l

: u8 x! n3 \, T* u! g& t4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
9 F3 x+ q, N: l- q
% _+ W0 B& Y/ v) B# q, O6 o( |! ^4 _! W7 U6 G9 [
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。8 f7 X5 q$ I$ f5 U

# w0 X) [' ?4 ^
$ U$ O  f- f* e' b3.x版本CRS$ T/ F2 D  p) x/ j. {
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs0 }4 U% Q4 h4 R7 F! ~, D. N
# 生成例外排除请求的配置文件
9 S  ~$ }" a7 _0 A$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
9 z+ \( P  P# o" Y$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
" G2 }! k" J/ A( S, y3 i8 j$ cp rules/*.data /usr/local/nginx/conf" M* P6 k9 Z0 a& x  }- L, \# S) m
为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。5 l# _! f- \6 w9 @
/ n- S, R3 k% d& G

4 b* y# d: f- \' x! |* ~/ t$ vim /usr/local/nginx/conf/modsec_includes.conf
' C' D( G- Z. p* b
6 D# }# p" S- l0 j/ E! u! t% H7 p* k
[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
& ]/ @$ ]- B) Q6 S( g8 U0 P
) H; t- S3 C/ E9 p  T! B
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
) D6 J1 p  ^0 f8 b: H
7 u8 T' y% O( z! D
  t9 I$ W4 n- p配置Nginx支持Modsecurity
0 n# i) \5 i% R8 Y启用Modsecurity" P9 k) F/ [$ u' R, x5 s; {' R
使用静态模块加载的配置方法
8 s" E/ B( r  M; _- V在需要启用Modsecurity的主机的location下面加入下面两行即可:
8 t0 u1 ~% B3 f6 A  H6 X3 t9 E- T: W+ K' U' J

9 N4 ^( F: h2 ?* j7 t& MModSecurityEnabled on;: K& |  s( s* H0 S
ModSecurityConfig modsec_includes.conf;
7 e9 X, P3 ~& J: J. J/ L0 K# r修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
/ e: n  G1 q/ w' r/ V6 p3 o0 c) y4 }4 }. q% e3 x0 K' x1 ]
. C6 d/ E9 ]: F# d" d! ~
$ vim /usr/local/nginx/conf/nginx.conf
3 k, d0 R6 Q; g, |5 d0 e" k9 k
; m& n* H6 y* m7 P: v3 ~3 v1 W# N
server {
8 o  _- A4 a$ O- {+ h  listen       80;
# y: D# w' M) x) G$ E" T$ b  server_name  example.com;
, N( i8 P3 Q8 D$ T
6 T: ?2 S+ S5 I$ W0 i9 M2 J
8 [. ^  E7 A- e1 a& \  location / {
* ]) ?/ B$ N/ R/ q; y    ModSecurityEnabled on;* I8 r' x: e& Q7 x' V
    ModSecurityConfig modsec_includes.conf;
1 p( m# X# K0 ^. p# l: I    root   html;
5 v& W: R- i* A    index  index.html index.htm;
, p0 |/ T0 m* d6 ~3 s  }
( j2 h: f) _$ t! N  r6 Y}
" I$ \/ s" H/ `8 S$ Z/ R, c使用动态模块加载的配置方法6 D9 }  `0 e0 ?0 W5 z" s2 Q" c- ^
在需要启用Modsecurity的主机的location下面加入下面两行即可:
* H. J, A/ @5 [4 f2 ?% s. @) p+ |
( K# R9 ]- U, }. C- v1 F; p0 U" h0 y, B( y0 H1 y
modsecurity on;
" g) C  M4 F1 I: _+ n) Dmodsecurity_rules_file modsec_includes.conf;
8 X: U( ?, B' m- n修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
3 R& S) J3 ?$ S8 @8 T
1 F6 @/ H. d+ U& x: L0 X& i* `1 \. S0 ^) n# o! R; ~
$ vim /usr/local/nginx/conf/nginx.conf
' J8 K  |1 K9 b+ `+ z8 L
. z/ ~) y) o5 p* K1 \$ g2 L5 |$ f1 C7 F- s; r
server {
+ j# G' H) |- @6 ?6 J  listen  80;
9 ^  h% z1 i& x% H& {  server_name localhost mike.hi-linux.com;$ k* w- c6 H# G# B- ~2 u0 s4 V5 u
  access_log /var/log/nginx/yourdomain.log;
2 i1 K' Y- y* P3 I& z/ S. Q" c  _" c8 h+ C
( {- Q" A, Z$ v7 g
  location / {. k' y# N" S5 E! Y8 Y* q) c" R6 q

) Q  t4 f4 X' b
  n8 f0 l6 T8 _6 z7 n& ^! u# U  modsecurity on;
9 e0 R7 {- t* t9 J0 C  modsecurity_rules_file modsec_includes.conf;
3 _' A( b  L) M2 j' p  root   html;
* p# c* `5 _8 h. F* c  index  index.html index.htm;. k  I4 g6 j( \) `: }* C! J
}5 E& w' h: n7 `: E& [8 z7 K; U
}. b& z; q( T0 t0 p- ?7 j: ~4 ]
验证Nginx配置文件
. W) o) W# `: m- F+ G# @3 ]& s$ /usr/local/nginx/sbin/nginx -t, L& Y! J8 B2 [3 v: O" M7 u
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
: s9 N! s: t& x6 k8 Y- ]6 O6 mnginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful, i7 C/ A; C! T) a% U. J2 X5 |8 ^& O
启动Nginx4 O* D$ t7 ]$ b. q; s/ V7 t, k- l
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2 \# h+ U0 F. I3 W$ u
8 R, [4 h( V, P1 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能够战胜更多复杂/未知/混淆的攻击模式。


# h7 V" [) v$ w+ a2 O# {8 @

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2026-5-20 18:28 , Processed in 0.057587 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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