找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9406|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。2 b" M/ H4 n( J# R9 v7 e& X
( z3 O, R( {; G

  l- U7 l0 ~& p+ a$ ^0 N2 @在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。& u/ X; A: O/ X3 F% f

- ]7 C2 c0 Y- s2 B4 ~$ X/ |6 a5 E1 T# u: T& e; `5 h
什么是ModSecurity
# H% I( t0 O" D( h; G# X) AModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。) C1 c0 P, {! K( m  L

5 {3 d* w+ i! `; Z' t( Q/ R
9 J0 a2 V+ Q8 ]ModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
7 X: ?; v: y  d. M9 u4 V1 \; j
9 C& F3 z: T4 \& `* f7 l) r6 g+ A- l2 K" N* s
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。% v; w  X3 G4 Y; Y5 P) V
& I, X( v. `. U1 K" @8 r/ q3 r
4 P$ F+ Q4 W" Z, @6 `2 B' g
官网: https://www.modsecurity.org// I* Y9 C% b: T2 D
, g* h9 p# S7 y  n: y

3 E, w& [1 j2 J! m: ~8 e' Y, |什么是OWASP CRS' T: d7 u+ O# n4 f0 B
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。' u4 F: J: Y' f* B% [$ H
7 k2 z' d- C& g- e) W/ C* `; h* Z8 n
4 Q# ?& R6 _; i) E9 l6 z
ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
5 d% I. a2 G" W1 A8 w$ N  m: X, Q5 \+ ^0 P2 |: W3 y4 W6 f

$ ^1 O8 [4 C" o" j! q- E3 hHTTP Protection(HTTP防御)
2 T+ {5 X* @9 K1 ?- xHTTP协议和本地定义使用的detectsviolations策略。
, q" k/ G' M  `0 W9 s( G; h' ?+ b# r' g
4 u% H  W( V' j& e$ z3 J
Real-time Blacklist Lookups(实时黑名单查询)
" X  `/ u; Z! M# D利用第三方IP名单。
2 R" w7 r9 w. m9 E. Z% M! V& \" @6 d+ d) S$ V+ i/ G0 |! q

* L* _9 z1 z; THTTP Denial of Service Protections(HTTP的拒绝服务保护)
# e/ X& g  b0 t2 B$ @  G  {, C防御HTTP的洪水攻击和HTTP Dos攻击。$ c/ x* J4 K  v7 n" x
* f) ]7 z+ z$ C2 j0 l! w/ F# Q+ [6 k
1 ], m' z, o" X/ ~# J# {3 \' q
Common Web Attacks Protection(常见的Web攻击防护)
, I1 u' a6 u& c9 b% x检测常见的Web应用程序的安全攻击。
3 u% }$ o, Z0 h# G1 t3 \% R: o! C; c4 v5 s  U) v
+ t, ?( @. \3 M7 D
Automation Detection(自动化检测)+ t& w5 f+ d7 V- N
检测机器人,爬虫,扫描仪和其他表面恶意活动。
3 o- C6 C: f1 W" l- ?5 ]% G" }- }
, i+ y1 H: X3 e) T  V0 a# [1 C
3 m: N  F$ J# k7 VIntegration with AV Scanning for File Uploads(文件上传防病毒扫描), W  A" Q' F6 B) s! R1 c- V# _8 k
检测通过Web应用程序上传的恶意文件。
* {" k3 ]' l/ T; P
! ?) l4 O3 O: W: L0 `- ]1 E4 v; [' q. O. f
Tracking Sensitive Data(跟踪敏感数据)
$ L  t1 D1 x9 b  b* Y信用卡通道的使用,并阻止泄漏。
8 X3 [. m- h4 d* l$ N0 H
! K& f! ?: u  ]& i, u& A$ {- H0 v7 K5 D
Trojan Protection(木马防护)3 @0 v* R4 l! K# F$ M& e
检测访问木马。
$ ~: G; L! `0 Y$ j) @" d9 I
) M# _  \# b# E7 t% s1 ^- n% O
8 [+ I1 K3 U) u0 j+ ?% AIdentification of Application Defects(应用程序缺陷的鉴定)- @; \9 M* ]8 J% M0 |4 l" N  S
检测应用程序的错误配置警报。
4 b7 R$ W' L+ X
+ M1 n6 Y& I. _
% [3 M. q; Y  ?  H6 lError Detection and Hiding(错误检测和隐藏)/ ]$ x6 i0 r4 e7 d4 `/ ?
检测伪装服务器发送错误消息。
, ?  b" f3 P) S. s  m# [. {
% Z0 X, K5 e4 R
$ o# S7 R) @  b安装ModSecurity& r  m( i' S' v) t
软件基础环境准备
' b8 e' ]' Q3 U! X( W7 X# b9 \! F下载对应软件包& T' u; k, p" b+ s- a: J1 E
$ cd /root
3 y+ _4 u0 t% P7 e: j$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz', J1 ^& Q" Z' X; x9 b9 J+ ^
$ 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  Y& j, U0 S+ C3 b
安装Nginx和ModSecurity依赖包' H9 m8 A& K( N1 O, C; _
Centos/RHEL
$ ?2 W& s  ^, ^2 S
: U) W# _6 @' ~3 T: ~9 Y
- v! @4 Q9 K4 z, b& K8 `$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel, z  p) ?" q$ B4 }/ I, O7 Q
Ubuntu/Debian( I' K- n; o. \# \# `

; x% ]0 O" N' V% s% @- X- D
) G: @6 v" d, A: E, ?$ 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 Q% L! L8 j5 K8 S编译安装ModSecurity5 d- O9 e+ b6 A, x5 u' W
Nginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
- d' H0 m: B: o& }
& H% I4 V6 V: [# I& t
0 t/ [0 l6 a( m0 t' q9 P方法一:编译为Nginx静态模块
1 v& i6 l& `: s" D* q/ i4 u2 e2 t3 p) F" ~; C2 U
+ |& \4 j" b# `& U
编译为独立模块(modsecurity-2.9.1)
! Y+ a/ d3 |7 V& Z, \! P2 x$ tar xzvf modsecurity-2.9.1.tar.gz& D- ^$ Y, V" q; u, e
$ cd modsecurity-2.9.1/5 v/ F* t0 y5 e$ J
$ ./autogen.sh
* t, a# N+ \: R6 e' K! I+ J0 a3 Z$ ./configure --enable-standalone-module --disable-mlogc
6 t% p$ S, ]/ p$ make
  ^0 B0 e# ^/ q3 W4 G! Z: X/ e4 K$ x/ _编译安装Nginx并添加ModSecurity模块$ |& J+ E& b8 H* Y
$ tar xzvf nginx-1.9.2.tar.gz
9 r! J  F" m4 }1 T+ f$ cd nginx-1.9.23 Y7 e% g- k# N6 c7 X3 v+ o7 s# A, g
$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
( Z7 H' J% P! V+ G7 u6 ^. @$ make && make install' m9 w& u# `' |' h9 \% k
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块, n% H) K* N+ D) a6 ?8 X
5 f9 W0 i2 W7 m# ]1 O4 m4 d5 \

4 P5 F+ p  W5 }) k9 C编译LibModSecurity(modsecurity-3.0)
3 r1 M2 H% \+ f0 Q; d; r# Y6 [3 l$ cd /root7 ~; ], @8 f7 f6 p, a8 r+ T
$ git clone https://github.com/SpiderLabs/ModSecurity
1 G9 K' ]# k% D$ f0 E0 ]$ cd ModSecurity
6 d; Z+ x6 Z+ a- ]$ C% o$ git checkout -b v3/master origin/v3/master
2 A& G0 \/ ?# _9 [$ sh build.sh
1 g+ b. G' Q9 B% [" t  W. ]- S$ git submodule init
, N6 ]/ |! g6 V% S$ git submodule update1 q" w; d6 O+ g" |. [% W
$ ./configure
( U. R/ j# b8 ~% A9 m$ make
/ ]( D/ {) }/ Y# h$ make install  }: p" u1 u- w, \0 f& x
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。+ V; J0 `6 X" \! o  g  ^. U& W
4 _- D1 |; l; p) ?8 y  A5 h

3 }/ R7 m1 e3 s. W9 f$ ls /usr/local/modsecurity/lib6 I0 X4 B* d0 x7 t, G3 o9 Z$ w0 }8 M
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0- H) G0 J* B7 A8 Z- l9 \
编译安装Nginx并添加ModSecurity-Nginx Connector模块- P, V$ L) f/ b) F, e% {
使用ModSecurity-Nginx模块来连接LibModSecurity2 ?+ x! ^: Q  w# a# _

; P: \  o8 j7 q* g: C5 R# {2 u) x) Y% H7 {) J
$ cd /root2 b6 C! r0 h  g8 S
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx$ ^. a+ v8 i/ |- i
$ tar xzvf nginx-1.9.2.tar.gz
1 ]& Q5 L3 M7 e$ cd nginx-1.9.2
9 C7 \- D$ d/ o% M4 Y+ f- v$ ./configure --add-module=/root/modsecurity-nginx
' X$ B/ O+ k, G5 q$ make8 m. A% k2 @. I2 R$ F$ F+ A
$ make && make install0 s2 @+ d. l; i3 I7 k3 K
添加OWASP规则* N) {& E/ q* r2 G$ u! k
ModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。2 B- H7 O0 Y. ^

; `$ |+ P6 P" E0 ~( Y3 t; V: `8 r8 I# E: u" Q0 b* N7 ]
下载OWASP规则并生成配置文件
% {, [. z& ^. S$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
0 @1 ?& V) c, @  _% z$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/3 [3 P. f; _: t$ G2 d
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! b1 U9 }1 {0 Z1 s
$ cp crs-setup.conf.example  crs-setup.conf
( T7 U' v! T6 k- s5 B4 X配置OWASP规则
1 w4 h) E- s3 c  `2 Z6 e" o/ s( m8 Z编辑crs-setup.conf文件
* s. m: q3 Z+ r& p
! C+ i' [8 w! u/ m) p4 E" o% X$ `5 L* u. u6 }3 m2 G) N
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf  l2 S* s' |7 w. X
$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf- t! E* G4 t1 x- w, {: B8 P/ v* P
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf2 H5 D: W* y4 R( X, _/ e$ ]  A
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf& |+ U8 I$ s5 j' D6 A! c
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。0 Z  a( ]- \/ p1 t! K

5 [4 F; P- m9 c& M. ~* X. _- l: |5 p- Q2 A6 }
启用ModSecurity模块和CRS规则
+ R  U7 p2 G0 `复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' q1 _+ {7 f+ s' W: C' n
5 b% F* _' a7 ~: V( I" q- M4 Z# _, k. v# R$ ^$ }
modsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) T) }  `5 ?8 H3 J0 J5 s/ T& d" n5 M
* x$ h" T8 Y( E- |' p$ l" j$ C

% V0 s" O4 G$ l4 b3 E  z. n$ cd /root/modsecurity-2.9.1/$ x$ P% r# q3 X1 y0 K
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  
. z- ^; R4 o( s7 V3 |$ cp unicode.mapping  /usr/local/nginx/conf/6 S0 p7 m. E9 ~) w# C
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
& ?) a# w. D6 H9 Y3 Y2 d/ J! Y; y  a5 n+ _5 W
8 o, T, E3 J3 c% |3 j
$ vim /usr/local/nginx/conf/modsecurity.conf
: y0 S$ _$ R8 a8 v) l7 A. A( C6 g4 dSecRuleEngine On
9 K0 u& r. B* Y% P4 iModSecurity中几个常用配置说明:% [& F3 Q! x- g2 v1 N1 j4 `* q+ J
; s( J! L% T5 M

# O0 \1 k4 z, T3 ~1 b( l1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。, z# \+ P; n0 d7 h
+ ~, A+ D2 [! C  i
! y. T1 ]# b( O$ ]1 i7 W
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。
+ o$ F+ B" y. P8 G0 ^) c
) J) O7 x! C0 g, C" q
5 B) |- \: \& v/ G) M2 Q3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
0 v7 c  V6 l0 g8 Q% f4 Y5 v) b9 L; g: R' T

3 h2 Q8 n7 S; ?3 f4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。$ K3 W& Z! |5 U
4 w1 n+ u( B$ P7 ]4 p

( `: R( l8 i$ w. p+ y$ Y: H在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。! k, Y6 X0 x1 N1 w3 d; z

0 A: R- W* j7 n8 ^) K0 D  [: z6 J
* q8 {* |: ~% ^3 T; @3.x版本CRS
) B1 u& {3 C+ c0 B; X$ cd /usr/local/nginx/conf/owasp-modsecurity-crs" K; D+ \9 z* e
# 生成例外排除请求的配置文件; x( B' }8 S6 n; R
$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf$ w: d5 R+ `; F8 K
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
9 J& i  |7 p9 r" K1 ^# r5 S6 x$ cp rules/*.data /usr/local/nginx/conf
! r$ u  k" _4 a  `为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。% y: o/ }9 \9 F9 \- N

  |, l, e/ ?- L( q$ y2 L( a3 I" Z. e3 s! M3 q
$ vim /usr/local/nginx/conf/modsec_includes.conf1 L1 Y. x; x* y* Q: M) w. m3 ^4 U
3 r7 h. ?; r; L9 P3 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

: ~0 o" i4 }) r/ m6 ~
, I. Q  t5 y& l$ _0 s注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
' V6 u! y/ i' _# {
. ?: J3 m) e- j; Z
6 ~3 t2 L7 I7 F( m配置Nginx支持Modsecurity
) x% n4 @6 F. h2 L$ H$ [启用Modsecurity9 |* k9 Q) q4 N
使用静态模块加载的配置方法
4 \0 c! z6 X; R1 E5 C3 a在需要启用Modsecurity的主机的location下面加入下面两行即可:: N8 p. i  K- ?# {* Q/ s2 U

+ i( K5 k3 y- _) Y: F2 |) ~7 B8 z2 b7 Z! y$ h
ModSecurityEnabled on;, o/ U# m) y7 i0 S
ModSecurityConfig modsec_includes.conf;  F; {9 d: Z1 l% i# c9 B
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 b! j) N  ^( ~, Z/ _
7 t9 `8 u+ V9 p

( S, K4 i; H% w( l# V; a" D1 K$ vim /usr/local/nginx/conf/nginx.conf
, _6 a# H* F& }% h/ m- M7 w/ F: h3 c" S: |# R, P- o5 o0 M3 L' T

% ^3 J, a$ w  l. z# C, O% L2 Mserver {
0 q! |" \  {8 D8 z; p  listen       80;
1 z8 U' [" q2 N  l* V! ?  server_name  example.com;" c' h- Q( K6 E1 p' Y

1 O' M6 a/ E0 J: m+ q' y- }2 g- j# ~* ^' [+ E: n: r. _2 ?+ j; u
  location / {! ^1 @- t6 G0 }, _2 H7 r
    ModSecurityEnabled on;1 n6 e6 |0 a4 ^% g! b; p+ V/ r
    ModSecurityConfig modsec_includes.conf;5 O$ k/ [9 y/ t* H) {
    root   html;
4 U2 ]2 f3 F7 X    index  index.html index.htm;
  ]" `+ ^, R$ ]  }% t6 l9 i8 X8 c2 U
}
2 B, \2 E3 a' d! b使用动态模块加载的配置方法
2 D& T2 v# {% x- T- A, H) C在需要启用Modsecurity的主机的location下面加入下面两行即可:
& z! K2 ]  ^8 l- C8 H, w- \' p6 u
% d1 Q" T* K  q+ _* \' P4 S: e" X
' |7 u' E, f. w3 k. Hmodsecurity on;
0 ?1 \# O& q) G* {6 V) ~/ L9 Z: Amodsecurity_rules_file modsec_includes.conf;
5 V6 _( i% ^, J* B修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。9 M* l$ n$ c, F( y$ R; w& h" H4 E
5 R) ~) t$ ?8 C3 S

# D' h  t* E: t0 a' F' t9 t4 {' |5 A0 q$ vim /usr/local/nginx/conf/nginx.conf3 H6 {/ r9 i- D+ K& z  m1 _4 _. d
! R" t+ m4 f$ N4 q% n  r4 X

0 n: y8 _4 Y/ s& Q7 j2 Vserver {* H8 r. s. u9 S  V( S
  listen  80;
. Q# t; _6 K2 R3 W% x( N0 C. r+ U  server_name localhost mike.hi-linux.com;2 c% P4 o. V) x+ d# D
  access_log /var/log/nginx/yourdomain.log;
2 z0 y1 X" K4 J8 Z
2 p, ]7 P! D, k1 j/ G
; x& _( ^4 B$ @' |0 {  location / {" M+ a* A" m  Y' d4 _) I

9 ?& h8 b( l$ m/ @( ]
9 S4 M! ^7 b3 w. q3 v  modsecurity on;
1 ~% ?; _; Q. O. x2 ?; l( |+ O  modsecurity_rules_file modsec_includes.conf;  ^  D+ [# T& I6 Q: X* Y: h
  root   html;6 Z. j4 {: b; ]0 T* t
  index  index.html index.htm;% p8 y4 j% X/ u# x
}
7 z; q3 @) k- F+ }' X( O, G}
& H+ {5 n2 d# l  D0 ^4 I: w验证Nginx配置文件
& n/ R' N$ O/ L) m/ G7 ?$ /usr/local/nginx/sbin/nginx -t8 M4 p. |) [, v8 \
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok3 L! J$ c7 G5 }0 W) K
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful/ b, [) E9 p: X* q
启动Nginx+ Y/ y0 e: |. [; D2 R
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf. H7 E9 p+ P: y# w  L+ C4 m1 u

! [- b: ?& G" j

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

2 |& f& r5 X; a7 `

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-10-28 22:20 , Processed in 0.074829 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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