找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9685|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。
$ V2 f2 C# ?; ?; W/ I% h& B) I7 c8 p( B$ Z9 ]) B
1 b5 |. u  w$ f/ |$ |
在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。5 X. M- ?* b" v2 o- i% `0 W! k( P

- k9 X6 g, f# l7 C/ p4 R) S
) K& y! G6 G$ c什么是ModSecurity5 l- N9 e5 b' C) t  D- N3 D
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
" y$ S; t& p2 d5 G+ t( F6 C8 R2 G5 ]7 r# G! c
9 ^7 t  l& Y6 n( h2 ?: Z8 K
ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
7 w% o4 q) i4 t4 M4 V, `, ^) n9 v$ f" G; o3 a4 n3 m/ o: a

; x. c6 H: r) r# m  w+ FModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。
3 n# `% X; i7 w- R6 T+ `7 h3 g4 i2 _9 y6 m6 r7 n
4 o$ \1 d6 d! t0 Q# ^* c
官网: https://www.modsecurity.org/( U: B8 o% X" n( P. e. Y7 R; h

/ Z. a6 u/ a0 I& ~% `% P) P
$ @  U$ y2 r# C& P5 }' w" L$ N什么是OWASP CRS
, u9 P: y# r1 ]OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。
/ b  F; `3 I5 u% u3 ?  e: d' e% U5 {; }  `5 h0 Z" i

* z3 B) R0 G2 w3 oModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。' H# x/ m: ^- N. L9 V( ?

4 V6 ^7 B9 X* i2 ~1 z2 N: U; q8 p/ z2 V1 l
HTTP Protection(HTTP防御)
  Z1 x! L! e: k% H6 V3 qHTTP协议和本地定义使用的detectsviolations策略。
4 c; }% z) p( v5 ]  t* e& j7 w. r- O6 V' |* T, \5 t# h

) s9 }$ |5 I6 d# jReal-time Blacklist Lookups(实时黑名单查询)
2 x) \8 V/ i; ^0 ^% y3 M# j4 u利用第三方IP名单。
8 @' _% z. {# F- d3 D) _1 F! U1 C; d  i9 e- M- S

9 G- f& V- \3 e7 ]HTTP Denial of Service Protections(HTTP的拒绝服务保护)$ Q3 r$ ]' Y1 z0 ^
防御HTTP的洪水攻击和HTTP Dos攻击。
6 {8 Q, o% ]9 ^
/ Y" \$ o! F4 a! a
8 k! T# r! I! `6 zCommon Web Attacks Protection(常见的Web攻击防护)
3 L& A: \, Y" B  x9 }检测常见的Web应用程序的安全攻击。/ `: |$ H/ ~4 R4 L0 X/ d# R. `
2 |8 ]! F' [# A6 o& r

7 B# F8 z5 c9 vAutomation Detection(自动化检测)$ j0 v3 a$ V, ~. n, v0 [* S
检测机器人,爬虫,扫描仪和其他表面恶意活动。; r  e8 ?7 h8 n9 P2 h6 I- _( A2 b% ^

8 C+ _1 m% D& u8 H7 w* ~7 \9 \: `" M
Integration with AV Scanning for File Uploads(文件上传防病毒扫描)
* o! e8 J! c! a7 R: @! n检测通过Web应用程序上传的恶意文件。! P: Q+ n+ n2 S* g
8 p1 q% w, J3 O: v7 ~- B# x

, f8 N# ?3 O! _9 ITracking Sensitive Data(跟踪敏感数据)
" d% `( W. |6 g' Z信用卡通道的使用,并阻止泄漏。- h% }& @+ M! y# U- K2 P

0 w5 D' o; h$ t
; i2 ~+ c, U3 K1 W2 m9 T3 E- sTrojan Protection(木马防护)' X0 u( d. l( Q8 @2 o8 C
检测访问木马。
/ S0 r5 @6 ?! Z6 r+ }- L) y' H
) w" x/ X& S# v- Y, `" B6 \9 B! A6 g' U: k9 F6 l* ^
Identification of Application Defects(应用程序缺陷的鉴定)1 _9 c+ B: @8 ^" |
检测应用程序的错误配置警报。
# B/ q( E7 C# @% F0 B0 g# g
3 _6 \: s5 ^, c" t
" b+ f- ]7 c2 ]4 l! tError Detection and Hiding(错误检测和隐藏), @9 A' \0 S4 J$ C7 ?9 J" y& G
检测伪装服务器发送错误消息。
; e# k' ]% H) I0 j2 {
8 ^" a/ {+ o/ ^" Q' ~3 m! a$ o2 H  ?
安装ModSecurity7 z8 Y9 q$ L* ~$ m9 J/ [; y' a2 ^/ m
软件基础环境准备# [$ _, F0 m) R4 `8 U- j+ V! E/ h
下载对应软件包
, W' J- M/ {. w  P* Z4 w$ cd /root; ^) X1 {  L6 ^
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'
$ n, V' c3 l) l' x$ w$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz' j" i' n. T7 x/ @" O3 n
安装Nginx和ModSecurity依赖包$ a* f# ]/ @/ q/ E6 n4 x
Centos/RHEL
/ S! i! C) \( [8 |+ y) ^, s3 Z  N
9 Q. ~* Q7 Y/ R' g6 d2 z& V6 n5 H! D0 Q& q' `
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel" P, T6 i- M9 i1 V7 \4 u( l% i
Ubuntu/Debian% ~( U2 P0 h' V$ q5 q( X& [# M
: ~! x# E' M/ p$ G' K. x

9 d# H! w5 ]0 k0 {: h$ 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& j* @" x! C8 ?& [
编译安装ModSecurity
, }" z& y* E0 N5 wNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 T: K1 E' {+ a# r) L
( y5 M' O' d( Z& ^% r" Z0 g% q- l" i/ @2 t# ]
方法一:编译为Nginx静态模块: E% f9 k" ]1 S& y9 w% X
5 w/ ^% G6 m# x' n* C5 U
2 \( J( e6 O2 w8 I# D: b
编译为独立模块(modsecurity-2.9.1)
' ~/ |! V; P9 k' P! l- N$ tar xzvf modsecurity-2.9.1.tar.gz# A. N7 d) \2 h1 D& N( j7 a2 Y6 j
$ cd modsecurity-2.9.1/- a' O2 F2 P2 O* ?/ t
$ ./autogen.sh
; u* E4 O! Q' N# Q4 {$ b" J$ ./configure --enable-standalone-module --disable-mlogc! T3 N0 E! A" l7 g5 b& g. [
$ make8 s% b9 Y2 [* Y0 E% f
编译安装Nginx并添加ModSecurity模块8 m. a, P, T; k; Q& X& i: F8 ?
$ tar xzvf nginx-1.9.2.tar.gz8 L" [0 h0 q( _! g% I
$ cd nginx-1.9.27 w# e3 a2 Y0 H$ m6 ^* o5 ]
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
. G8 r/ H9 c6 M$ make && make install/ @, a5 }5 a/ O) U& S
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块$ |- s' @* P$ ]( r% @

0 f% O4 p3 a6 ]. o. j/ w/ ^) h( H9 p: h( `
编译LibModSecurity(modsecurity-3.0): G1 }, s' A- f9 r
$ cd /root
7 c# G( S& V4 }# F9 ~4 e$ git clone https://github.com/SpiderLabs/ModSecurity7 o5 [) `/ P  F
$ cd ModSecurity! X; _- X- F, M* w  l
$ git checkout -b v3/master origin/v3/master# X) E% D$ K* \( a* W" t$ n! V- M
$ sh build.sh5 M8 n) Z8 Q. C$ O
$ git submodule init3 j: F1 M7 @( z) c( u
$ git submodule update9 j0 n& l; N8 b! ]
$ ./configure3 o) C+ L. v& R
$ make; x& ^4 X3 Y0 i2 D' o' `
$ make install
7 o& ~$ }$ P& r& D0 ^7 [/ xLibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
) I: B8 c8 I+ Y7 v( y) I" u: @8 B1 d5 X

& R) `8 I, |8 q% q7 R4 S1 W$ ls /usr/local/modsecurity/lib
8 G. o$ M" e6 m2 q& ?, ~' A3 Alibmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
7 B$ p: j7 q8 O; v编译安装Nginx并添加ModSecurity-Nginx Connector模块1 i+ l7 W3 T$ C; z2 e+ a
使用ModSecurity-Nginx模块来连接LibModSecurity
4 Z0 ~# Z) q" m: y3 W
7 ]/ g$ J4 N* I4 }! B  ~1 q+ F2 W' P/ J2 t7 t
$ cd /root
6 a/ I1 x+ v1 ~; l( E# {$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ y# u3 d! b9 Q2 ^5 j, o9 D
$ tar xzvf nginx-1.9.2.tar.gz
1 p2 R& b+ q2 x$ cd nginx-1.9.2
0 ^6 ]  y5 Q  @$ ./configure --add-module=/root/modsecurity-nginx! _% O1 {, C1 a8 Y: X/ X4 K4 }  f
$ make  a/ C& L% u1 V8 H* N% \; T3 z
$ make && make install
4 [$ _( E& D' A/ p1 ^0 ?. V4 v+ W添加OWASP规则' M# q! e: f1 x. m; y& g
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。0 X* u9 |* s% L) F  n& _, j

8 f& T7 W3 C" i/ F- m7 C5 ~. y) \7 k1 e- s
下载OWASP规则并生成配置文件
3 I( x# J% N/ V3 j# F2 ^8 z$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git8 `7 U' V2 C1 G, E, P$ d
$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/& j" y4 _7 g$ _
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
& u- b6 B$ h+ T; V$ cp crs-setup.conf.example  crs-setup.conf
  B( N$ ^+ K7 q! x6 ^* @配置OWASP规则
- v  K/ q# J: m' F. z编辑crs-setup.conf文件
1 X. D( a  {8 ~* Z* d8 ?8 P! f
# i, P. [/ r) ^! F9 ?
9 b/ z$ A# G. W, r, ]$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
8 J! E! T' N0 O% a/ q$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf; l9 f- ~4 ^: b) r/ V
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf
' n% Y) q) P6 ^( K$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf
" E) h  \- s6 H$ s默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。, [2 l% Y4 J( P% K9 n
* A8 o; _* n* Z) B/ f

' j1 j+ l( B' K3 [' Z* p) N* \启用ModSecurity模块和CRS规则' E5 {) e* V6 ]0 m2 Z9 s+ o) M2 e' A' B
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( u/ e4 f! N+ K, I6 ]
3 Q9 o5 F  I3 C

2 R$ H* U  b0 `* i! gmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。
7 l, n" Y+ Y; Z$ e1 n% I* X! \- W- l' i& B$ w

7 n; ?( l  m/ I$ cd /root/modsecurity-2.9.1/
  V7 f) G0 _1 C4 O1 f. W$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
' M+ H  Z: X( I: D! X0 H$ cp unicode.mapping  /usr/local/nginx/conf/
' R% m, p0 I' u将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& _: u" i3 W, Z  E+ |' t4 G
% J9 J1 f! ?' A6 M: k/ y+ s" v: i& v0 D; L/ r: S8 n
$ vim /usr/local/nginx/conf/modsecurity.conf
. E. F/ }6 u3 n5 i7 fSecRuleEngine On
. Q& U- f) ^+ D0 L5 N5 q5 P3 EModSecurity中几个常用配置说明:
  z- L, B4 s& P: b* e" g
# }. T! f* h, a& P; z( F9 r3 [# m7 r6 Y; t& A. ?8 F7 V
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
% n9 C8 j% O0 Y2 C3 T  s+ L" {# K! T* K9 r" Q
6 H! J# p- k+ M- o3 T( ?
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。. ?+ {6 Y7 V0 P% z
( i" d3 D# ~" @
' s) A. G) n* h+ E% s) ]7 g
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
: l3 b9 Y1 K8 z3 a" E9 t5 ]: h
  t/ B' G. l  U# W
7 y0 V% }3 M" Q. g- i4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。
* _, }+ \. L- @' [9 z  G4 K% G
, ]2 |1 ^" L- D+ \
在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。
& Q1 ?# i' Q; e. h
4 q/ {3 X& I0 j6 i% }  a
- Q; c8 [& ^; P( ^1 I0 R8 [# e3.x版本CRS
$ U! N3 S- z( i0 H* S0 D$ |$ cd /usr/local/nginx/conf/owasp-modsecurity-crs4 x& W, G! X' C# Y" u# {6 m
# 生成例外排除请求的配置文件+ ~( R5 N3 z1 p: u+ m- X* N. `# z
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf& K5 {7 S1 C$ E$ n; S1 G
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf3 Z; b  U" {: \# s% [# D
$ cp rules/*.data /usr/local/nginx/conf
1 E: p: N2 C2 V4 d% M为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。9 D  D! N1 e3 X, _, j4 \0 q
) I0 e; K+ ~2 c/ `

. i3 Z1 G3 Y* o/ R! B$ vim /usr/local/nginx/conf/modsec_includes.conf
1 [( W/ m5 f. b) N) [
5 G/ s$ C% M; b" e7 J. q
[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
! R) m5 T6 V% K5 P
' n4 ?" O0 o- G% s) e
注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。  t' r3 N1 h) x
% l8 D3 f5 C" m- k0 ?  q; ]
# h) C$ @9 B6 _7 V
配置Nginx支持Modsecurity
6 C# A2 o" D0 l0 _5 }. U4 B% |启用Modsecurity
3 r/ C) d& W9 \! F1 @! W; o使用静态模块加载的配置方法
% E, H" _* c+ q. C) T# A在需要启用Modsecurity的主机的location下面加入下面两行即可:
( S0 Q& }# c5 H: |
" T1 W) R. ]  P5 n: x4 d- j: h3 O9 Y0 G; h9 D8 @! T5 I& J7 O
ModSecurityEnabled on;
2 o% u# [# ~/ a& G6 O1 L! l0 gModSecurityConfig modsec_includes.conf;. [( s$ l% H9 P% u
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。* m) ?/ e7 ?* |9 |. f9 ?
: X: k9 x/ S8 z3 Q

% ^& I+ Z9 q; A! d5 X6 E5 o0 v: u$ vim /usr/local/nginx/conf/nginx.conf; V( t8 v3 f8 a- r1 r* g
) l' l2 G" L8 T# _1 K( G9 V4 ]+ N$ @/ ^2 C
7 C4 R3 N2 Q# k4 Q: Z6 T5 \
server {( C2 [. b: d! C
  listen       80;. x$ x( B2 N9 e5 f8 J$ u
  server_name  example.com;
2 o) u9 }$ L1 B+ i) i# i* _) q5 u! k2 ^% Y
+ g1 b5 b( f  r$ g3 U/ ?$ M: V
  location / {
- u& B8 J! _4 Y3 L    ModSecurityEnabled on;
8 e7 t1 w3 }4 z    ModSecurityConfig modsec_includes.conf;1 ^9 J; h8 _, f* Y1 O
    root   html;! r  V- G9 V+ U2 q
    index  index.html index.htm;( O+ m, z) E1 \' T
  }$ |! O  |" p$ F) q1 C: p9 `* g
}
; B( Z, K& r8 h3 s" u( P9 B) m+ c使用动态模块加载的配置方法
4 p  N8 {1 S- `3 B: S! z8 @在需要启用Modsecurity的主机的location下面加入下面两行即可:
1 t' Q, [& E: W8 X. ^# w' e* Y% p) W0 @! M& i/ f1 Y* K

& y5 m4 A" t! e& J3 Pmodsecurity on;  N& ?' S, m1 p4 V. q2 s3 s
modsecurity_rules_file modsec_includes.conf;! v/ }& [0 Y8 V! Z) j
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
. F8 }, U5 L, D) V( g+ l
& w6 d: ]5 h+ X- Z% B! x
, }) j4 m! L) C$ vim /usr/local/nginx/conf/nginx.conf0 j0 u. v: B3 ?9 L# K# Z

& i4 h9 r2 t  t# `) J
* |; v) q- M$ |$ @0 ?0 dserver {
( O7 T/ g/ b+ J5 u2 ?  listen  80;. H) L5 `9 R  w6 _1 `
  server_name localhost mike.hi-linux.com;
) r5 |5 v' m* U. q5 C- z$ c  access_log /var/log/nginx/yourdomain.log;
0 o2 Z0 x3 K2 S) w2 S
9 n' s9 f1 w( H7 p
" Y+ M0 h! i2 K' h4 n4 A& W  location / {
& {4 n- e& ?* d7 P  L7 p: _2 V1 F# Y& }  N; y. H

& t7 Z3 E  ?& D5 e  modsecurity on;/ P: R, h0 [9 e  G6 H
  modsecurity_rules_file modsec_includes.conf;
% _* S) l+ W0 [& ~# G1 A  root   html;
0 x4 n  R+ c& v3 Y2 v2 @* `7 S  index  index.html index.htm;$ w  ~, ~2 ?; S
}/ C- z5 w+ \5 [. K3 @  C
}
# ?2 {7 ~$ V! l- b* g验证Nginx配置文件% J% K% _2 s- h
$ /usr/local/nginx/sbin/nginx -t* \0 M7 f! s& {; i6 c% z
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok' j3 z! N% I" L9 l4 {& z# w
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
) d1 j% j; P; ?9 W8 |2 `$ ]$ b启动Nginx8 L4 u# [+ @) E) ?& t3 @
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
+ T' K( Y& {0 n, Q8 e' p+ y/ X' x1 E1 y" T% g; z+ \2 J& W9 u" p

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


% I2 M7 A- i; d

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-7 20:43 , Processed in 0.083050 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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