找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9738|回复: 0

利用ModSecurity在Nginx上构建WAF

[复制链接]
发表于 2017-10-19 17:34:51 | 显示全部楼层 |阅读模式
ModSecurity原本是Apache上的一款开源WAF模块,可以有效的增强Web安全性。目前已经支持Nginx和IIS,配合Nginx的灵活和高效可以打造成生产级的WAF,是保护和审核Web安全的利器。; G% I0 _" c, t4 Q: c+ u# w) m
- Q: E! D# d( j# f

' H7 m# I% n! n0 {( l* B在这篇文章中,我们将学习配置ModSecurity与OWASP的核心规则集。
9 g/ J+ L5 A; k+ }% f9 A* B2 ~5 A2 k# ?9 E

8 q. w3 H) ]  O; H8 H* [+ H4 s什么是ModSecurity; C; i4 B- F* X8 y& D  H
ModSecurity是一个入侵侦测与防护引擎,它主要是用于Web应用程序,所以也被称为Web应用程序防火墙(WAF)。它可以作为Web服务器的模块或是单独的应用程序来运作。ModSecurity的功能是增强Web Application 的安全性和保护Web application以避免遭受来自已知与未知的攻击。
% Y$ X3 u/ |2 W4 M% o* x& k) ~1 z1 Z5 {" N

9 g* T3 [, ~  A2 zModSecurity计划是从2002年开始,后来由Breach Security Inc.收购,但Breach Security Inc.允诺ModSecurity仍旧为Open Source,并开放源代码给大家使用。最新版的ModSecurity开始支持核心规则集(Core Rule Set),CRS可用于定义旨在保护Web应用免受0day及其它安全攻击的规则。
; l! ^( @+ J# o1 ?
% _# S9 ?1 A, I( @4 g; o7 c6 q4 d, c: G& x. p! V# U) W9 z8 ]
ModSecurity还包含了其他一些特性,如并行文本匹配、Geo IP解析和信用卡号检测等,同时还支持内容注入、自动化的规则更新和脚本等内容。此外,它还提供了一个面向Lua语言的新的API,为开发者提供一个脚本平台以实现用于保护Web应用的复杂逻辑。5 \+ S& o- G8 [# U3 C: p) y
: U- k+ {7 K5 p# S6 Q

+ G* u9 p# V  v+ |官网: https://www.modsecurity.org/
! }* c# j. s' X* ]$ y* Z/ y% z& I  E7 E' `- k: G4 p

3 A$ o9 I/ w( s$ ?7 _/ h- D9 p什么是OWASP CRS# _9 R* L7 K! U5 K7 y
OWASP是一个安全社区,开发和维护着一套免费的应用程序保护规则,这就是所谓OWASP的ModSecurity的核心规则集(即CRS)。ModSecurity之所以强大就在于OWASP提供的规则,我们可以根据自己的需求选择不同的规则,也可以通过ModSecurity手工创建安全过滤器、定义攻击并实现主动的安全输入验证。- c8 u8 u/ Z( Z; y# H
- O( z. C' v' D3 V, \

$ x/ f3 D* A! R1 g1 q8 h" ]: }ModSecurity核心规则集(CRS)提供以下类别的保护来防止攻击。
! E! \; g* I, n7 o2 B2 @: K' k8 z( ^& [: e
$ r  I9 E  Y( ~+ ]; T: m( Q
HTTP Protection(HTTP防御)
2 U8 ^) O* z# [/ H* W! H) @HTTP协议和本地定义使用的detectsviolations策略。; ?. C$ K- c6 w) p
3 I3 X7 B: \: W
3 F1 j" I: y- z, i1 @1 P
Real-time Blacklist Lookups(实时黑名单查询)( e8 C0 Y6 P" r6 U2 |$ w" C' p2 T
利用第三方IP名单。" F9 ]5 C7 u0 ?+ F- m
- M) f+ B( r( U! c+ m4 o4 m
$ U) q' W" `6 M. S8 y. q
HTTP Denial of Service Protections(HTTP的拒绝服务保护)
6 _6 U( L' {/ X9 f防御HTTP的洪水攻击和HTTP Dos攻击。
# L7 r0 p; \5 c9 ?3 i! N4 \/ }+ r
4 P7 @% S" X5 u. T# _9 U" i) _3 W
Common Web Attacks Protection(常见的Web攻击防护)
3 I. O2 d2 C4 }; N0 c检测常见的Web应用程序的安全攻击。
* K  E1 ?7 ?" e
; y" q0 P/ G0 e9 y) p2 R( o$ c0 F
) i( Y/ j; |& k5 D/ y$ R+ b# QAutomation Detection(自动化检测)
' [& ^! e2 ~; `: U- p) B* X; J5 T检测机器人,爬虫,扫描仪和其他表面恶意活动。
' I3 F* V3 P$ r3 u; B0 H
! F+ F# C5 S3 X5 j8 x% B! ^! }2 Y
! M1 q, V, G& S- H: X" P  F+ d8 s8 J: t. jIntegration with AV Scanning for File Uploads(文件上传防病毒扫描). h, n( C7 Z' V( `& y
检测通过Web应用程序上传的恶意文件。+ m5 m6 _8 x* d+ X+ h
4 Q* j+ o0 _- r& a! [6 H
3 K: ]$ u; m) Y$ Q1 ~
Tracking Sensitive Data(跟踪敏感数据)
% r' b" Z. ^1 ^& Z信用卡通道的使用,并阻止泄漏。
0 t# |% Z. n. H, B  k8 I0 O- V7 R! ]3 @
& h# |9 Z1 L/ {% o2 S
Trojan Protection(木马防护)
1 h& `$ z! `/ J9 g" s) Q' L5 I检测访问木马。, V- t4 I5 Q( o& j; Z3 F! \

. m/ ~1 V# Y, ~& _
7 B0 z% @' b' ^1 G; _Identification of Application Defects(应用程序缺陷的鉴定)
# ~0 p0 q; T/ H( ^检测应用程序的错误配置警报。
" m- ]6 p/ D- I' K9 V# e( F
/ L/ }, p& A, h4 Q& t
; p( b6 g* S. |Error Detection and Hiding(错误检测和隐藏)5 J4 F& Z& v! M+ g  L0 T
检测伪装服务器发送错误消息。9 l! Q$ D# W: X: u: m, X/ j
! W4 T) g4 G9 _/ a' V2 S, t
: z- G( t9 X  K  _
安装ModSecurity
+ r$ Y+ j# [% \软件基础环境准备6 |6 e; `. ?" Q1 |/ N4 f
下载对应软件包
8 q9 v: d5 n$ Y/ m$ cd /root2 C1 \8 p! G, L5 W7 v
$ wget 'http://nginx.org/download/nginx-1.9.2.tar.gz'$ S8 b) |7 R! \/ d: [3 z0 a0 I
$ wget -O modsecurity-2.9.1.tar.gz https://github.com/SpiderLabs/ModSecurity/releases/download/v2.9.1/modsecurity-2.9.1.tar.gz9 w# Z$ u1 e8 J' Z
安装Nginx和ModSecurity依赖包+ ?3 F0 e% I. `" O6 r$ @0 Z
Centos/RHEL
2 n4 g0 g1 N7 c. s% ]% m% d! S% e# Z. ?/ q3 @7 R% c" z6 y
/ Q) q) ^+ w9 q/ r7 Z
$ yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel zlib zlib-devel openssl openssl-devel$ [) {) C& z$ j6 ~; V: b$ f9 ^
Ubuntu/Debian8 j# O& s. B( s8 B+ W: ]+ A# r
' l8 O9 }8 n7 Q6 {

: P7 z* _. m4 B6 M$ 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
8 ?( x+ X! g9 M, Y. l! N编译安装ModSecurity
" ?% ?$ ]" g% ?( d* a; K/ MNginx加载ModSecurity模块有两种方式:一种是编译为Nginx静态模块,一种是通过ModSecurity-Nginx Connector加载动态模块。
9 |/ ]' y2 P. l5 v$ Y
' U; l7 [; }+ ?0 y) s! y7 w: b3 q* }4 @
方法一:编译为Nginx静态模块
" x5 b2 R: W$ p$ f. M+ i8 k
+ L- F  L7 r6 I' g1 k+ |, k- p5 a  u
编译为独立模块(modsecurity-2.9.1)
1 p8 l; P5 I3 H5 e% ]: {' L$ tar xzvf modsecurity-2.9.1.tar.gz5 Z4 ?+ s5 ?  l6 \4 w
$ cd modsecurity-2.9.1/' u" F, t$ Q4 K1 U
$ ./autogen.sh
5 k# T4 W' p  M& i# A- ]) T" O+ b5 B/ W$ O$ ./configure --enable-standalone-module --disable-mlogc
! q. _: ]9 c5 S1 n$ make
6 D* ~$ e. m( I' M) P编译安装Nginx并添加ModSecurity模块
2 x( R) Y2 v5 N$ n- q$ {$ tar xzvf nginx-1.9.2.tar.gz
0 E3 t, ~8 R, Y2 X, |% x$ cd nginx-1.9.2
8 K- m" `: M' \" J  \0 ?2 m. ~$ ./configure --add-module=/root/modsecurity-2.9.1/nginx/modsecurity/
: i7 j$ o  `3 @$ x$ make && make install  t) f+ L6 S+ ~2 `
方法二:编译通过ModSecurity-Nginx Connector加载的动态模块) h1 z, u) \& l* W" Q

. V8 i7 d0 L; E" P, _! u: R: N/ m, l: e( ^: o1 T- I
编译LibModSecurity(modsecurity-3.0)  ^# ~0 k1 ]$ [4 `7 i" {2 D+ q
$ cd /root
! H# R) O4 |& B0 z, s4 g; G* c" n$ git clone https://github.com/SpiderLabs/ModSecurity! R0 O, l& Z2 c. i2 P; h, H
$ cd ModSecurity
/ H. R$ U  M4 l$ git checkout -b v3/master origin/v3/master7 V: W' [6 c7 n& w7 V& {
$ sh build.sh, \% S) q) C! i! r" w0 N; D/ z
$ git submodule init
: g$ i0 t) h6 }4 u. _2 _3 a$ git submodule update5 ]2 R4 L* U% D% b$ }% l! k; r
$ ./configure
& r, p: R, e, }0 h$ make5 C5 O- J% s; W  Z
$ make install7 S) l7 d! J3 V6 d( F
LibModSecurity会安装在 /usr/local/modsecurity/lib 目录下。
8 T& f8 x0 S5 j3 p* t  E4 d' C, L8 \! d0 c
4 I+ `! F$ L" J4 S8 v' s% f/ V7 G$ x
$ ls /usr/local/modsecurity/lib( X0 U! X# R6 G0 r; g/ f( }8 o
libmodsecurity.a  libmodsecurity.la  libmodsecurity.so  libmodsecurity.so.3  libmodsecurity.so.3.0.0
" v' m2 Y+ h! G编译安装Nginx并添加ModSecurity-Nginx Connector模块5 p5 i. }% S" f$ U; y
使用ModSecurity-Nginx模块来连接LibModSecurity
6 \+ W& I: }& z. T" ~" C6 i3 \6 Y. t: j2 w# h# c8 G% y
1 b* M9 ]; i+ O$ w# \& Q
$ cd /root" ]" Z* t/ Z5 X& E
$ git clone https://github.com/SpiderLabs/ModSecurity-nginx.git modsecurity-nginx: l* y) J# w- z6 @* {  x2 S  [
$ tar xzvf nginx-1.9.2.tar.gz
0 ]- ^5 ?) h7 \4 M- h2 K' |$ cd nginx-1.9.2
6 y5 W- q: w! P$ W  J8 P$ ./configure --add-module=/root/modsecurity-nginx' \9 Z4 K4 F) W7 W" j. c
$ make' r  i! s# y& {& s* @" L
$ make && make install
) ?% ~2 I" D% _3 N添加OWASP规则
/ e" E1 _# x  i7 G4 k( Y. cModSecurity倾向于过滤和阻止Web危险,之所以强大就在于规则。OWASP提供的规则是社区志愿者维护的被称为核心规则CRS,规则可靠强大,当然也可以自定义规则来满足各种需求。
7 C& G0 Q9 i; ]2 L' _' c( T6 g1 c5 W( [% ]6 k
& G* x7 T$ ~: j, k+ t
下载OWASP规则并生成配置文件, C; M" O" S8 ^
$ git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
$ `; D" s% D' ?2 }/ c$ o$ cp -rf owasp-modsecurity-crs  /usr/local/nginx/conf/9 q! j: o6 a6 b
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs
) {' h9 `# X% U% L3 Y$ cp crs-setup.conf.example  crs-setup.conf
! H+ v1 l$ z  S8 w9 _: W配置OWASP规则
3 b) t' y5 f- Q5 j' Y编辑crs-setup.conf文件
! O! m" `# {, o3 Y2 Z
3 M4 `; ?8 z* _7 y$ S4 X, B8 [/ t7 K# V* S' p/ {( s
$ sed -ie 's/SecDefaultAction "phase:1,log,auditlog,pass"/#SecDefaultAction "phase:1,log,auditlog,pass"/g' crs-setup.conf
8 K$ W& C6 n1 Q& F2 I: B" Z7 u% s$ sed -ie 's/SecDefaultAction "phase:2,log,auditlog,pass"/#SecDefaultAction "phase:2,log,auditlog,pass"/g' crs-setup.conf/ C" |; H/ N  }
$ sed -ie 's/#.*SecDefaultAction "phase:1,log,auditlog,deny,status:403"/SecDefaultAction "phase:1,log,auditlog,deny,status:403"/g' crs-setup.conf- n, ^2 v/ D$ v4 C  v; w
$ sed -ie 's/# SecDefaultAction "phase:2,log,auditlog,deny,status:403"/SecDefaultAction "phase:2,log,auditlog,deny,status:403"/g' crs-setup.conf1 K. [# m! Y2 T( R# ?( T8 N; s7 n
默认ModSecurity不会阻挡恶意连接,只会记录在Log里。修改SecDefaultAction选项,默认开启阻挡。+ ^0 @" ]+ U& h, {* x
2 y% v: K3 ]( V

) J0 J7 V: S) q! P: M启用ModSecurity模块和CRS规则4 y6 e$ {$ F) H- w1 }* u/ p! n
复制ModSecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到Nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; ~8 o& N% e' m: c) \
1 _/ r: I0 W! G" u. U

1 ^' x8 R5 j6 k; `  Z2 Qmodsecurity.conf-recommended是ModSecurity工作的主配置文件。默认情况下,它带有.recommended扩展名。要初始化ModSecurity,我们就要重命名此文件。) F; s1 k9 _# f$ O: J
% ?2 }* b4 _+ |) _* n
& R2 A5 ^# u! p" x
$ cd /root/modsecurity-2.9.1/" t, l# Z0 n4 ]$ Q
$ cp modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.conf  & a# Z/ W: Z9 e  l  G( f
$ cp unicode.mapping  /usr/local/nginx/conf/6 N* Q, d! p  a: `0 G  c
将SecRuleEngine设置为On,默认值为DetectOnly即为观察模式,建议大家在安装时先默认使用这个模式,规则测试完成后在设置为On,避免出现对网站、服务器某些不可知的影响。
7 k, m" m4 o- x' Q8 Q$ ]$ _$ i
! Z9 [6 z+ D9 U7 z& o2 Z/ g
  l' U1 s2 t* M' V  ~/ H. J1 H$ vim /usr/local/nginx/conf/modsecurity.conf, x8 d/ D* C6 O& k( n9 l
SecRuleEngine On
. o/ L% j  |* m/ ~0 c, lModSecurity中几个常用配置说明:
  ]" y! V% `3 M" O3 m* n+ b" [5 p* l1 L; p1 @* s- X+ b
3 x+ i4 X- m9 V' q0 _' C* B
1.SecRuleEngine:是否接受来自ModSecurity-CRS目录下的所有规则的安全规则引擎。因此,我们可以根据需求设置不同的规则。要设置不同的规则有以下几种。SecRuleEngine On:将在服务器上激活ModSecurity防火墙,它会检测并阻止该服务器上的任何恶意攻击。SecRuleEngine Detection Only:如果设置这个规则它只会检测到所有的攻击,并根据攻击产生错误,但它不会在服务器上阻止任何东西。SecRuleEngine Off:这将在服务器上上停用ModSecurity的防火墙。
- F0 i% ^1 |# ~9 K0 z- X2 D# m9 {7 w6 P2 a9 T9 \% I' g( F; G) c# E
$ m# W! B! q4 j1 E) l! p3 [* B
2.SecRequestBodyAccess:它会告诉ModSecurity是否会检查请求,它起着非常重要的作用。它只有两个参数ON或OFF。$ q( P. y7 ^7 q8 Y! m$ a% w, ^
; K6 g# m( M# N( n6 V, I5 I% _" U
5 g! p( G$ e1 n
3.SecResponseBodyAccess:如果此参数设置为ON,然后ModeSecurity可以分析服务器响应,并做适当处理。它也有只有两个参数ON和Off,我们可以根据求要进行设置。
4 O+ I/ ?# `2 H) G) |: G
7 n% l" z: I; k
0 u" G1 ?* o: p, ^! h2 C% u' [$ T5 s4.SecDataDir:定义ModSecurity的工作目录,该目录将作为ModSecurity的临时目录使用。6 \5 S) z" V' N% J4 B

2 c! L- k4 M  m0 W" M- @
* b  t, o7 M0 i3 X$ P在 owasp-modsecurity-crs/rules 下有很多定义好的规则,将需要启用的规则用Include指令添加进来就可以了。. h0 Y5 o, M$ j8 R! J
; t. O" Y2 W# E5 D7 H- O/ h) S
- [: x) e2 P! `# H. c' N7 f. Y
3.x版本CRS* B* o- I4 ^  s! E6 F
$ cd /usr/local/nginx/conf/owasp-modsecurity-crs! _; i: O1 h8 E0 D  N& B5 j
# 生成例外排除请求的配置文件
7 F* s. f% `6 m+ _$ cp rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf6 a3 S& @9 H& f; z' Z6 ]
$ cp rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example rules/RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
& @2 f2 `' h6 c3 |$ d$ cp rules/*.data /usr/local/nginx/conf
9 s! K4 ?' W. [( p为了保持modsecurity.conf简洁,这里新建一个modsec_includes.conf文件,内容为需要启用的规则。
3 c& N5 q: d' Z3 G
2 r4 c$ o1 a% q4 h/ \) i/ N: [4 U9 R. h1 j, z2 a7 N
$ vim /usr/local/nginx/conf/modsec_includes.conf  R, T7 i8 F! [, j
% c0 U$ E$ b8 @; I+ A
[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
: G+ T* ~3 t) h( X6 x5 k

8 V" [, y: Z: V/ B( S注:考虑到可能对主机性能上的损耗,可以根据实际需求加入对应的漏洞的防护规则即可。
- _! _( [  @, m! q" p: m8 W3 o; [* @) H1 B# ~: [% y
1 ~) y8 ]: r9 v  P/ P9 ~5 b* W' p
配置Nginx支持Modsecurity
3 Q1 Z  t+ b9 C# X( W3 X启用Modsecurity
. a2 n, _0 n, b+ g* V使用静态模块加载的配置方法
5 W( @8 f0 A8 e在需要启用Modsecurity的主机的location下面加入下面两行即可:: z( x+ Q) u/ m$ m/ |
2 P! D. Y; X1 M- H4 p+ h9 o: f
; e% }  s% {. G, s
ModSecurityEnabled on;9 i! F" b: \; o4 A
ModSecurityConfig modsec_includes.conf;6 {  \% N* j# E! T
修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。
$ V% m! o" Z/ r5 @2 X% \* g' y- ^; B9 n

' Q0 R  Q2 v4 P- V7 G4 T, R$ vim /usr/local/nginx/conf/nginx.conf
' g: a2 c, z4 D# X. _  s
2 p$ p6 q0 l5 t! k7 d
+ }( ^/ _4 D+ l% L% E& x% Wserver {6 @+ j2 B! @$ [4 T# i
  listen       80;8 x, Z" A- Y/ t- c% ]2 @
  server_name  example.com;
) v' p& s8 ]# n' q" }6 w3 i2 i9 [
6 [) x' b+ \: _7 m
  location / {" `$ n. F$ W& l
    ModSecurityEnabled on;+ ?, l0 g9 W- ?) w3 l7 }
    ModSecurityConfig modsec_includes.conf;
9 G/ t* S/ Q. F" g& p& q. F1 e, }    root   html;, o/ O! Z8 z" R. v5 N
    index  index.html index.htm;2 {% w# e+ a; C
  }+ V* \. T- W$ ^* }# M! z* c0 y1 A. D; x
}) f6 ~. W1 A, H4 c( C
使用动态模块加载的配置方法2 G: ~' y& }2 @( Z. K% l8 ?
在需要启用Modsecurity的主机的location下面加入下面两行即可:) O4 ~; s- H( \# T9 w

7 j0 [. c; P* |4 M6 D8 W6 z% G
' E3 U- q( j% wmodsecurity on;/ G. t7 ?# D1 A) P) n$ @
modsecurity_rules_file modsec_includes.conf;
9 i7 c/ ^5 z" F: Y1 K修改Nginx配置文件,在需要启用Modsecurity的location开启Modsecurity。6 l, ]! P4 ~4 c/ S! ^# @) C( n3 ~/ Z

; U; R- A+ ?2 g6 [6 R3 j, v0 S5 t9 P5 a2 f0 z, X
$ vim /usr/local/nginx/conf/nginx.conf
9 k' {1 G9 y# d- S9 ?( x! j) o, c# L& `2 {1 R

; Y& d# b. E( Xserver {
6 K: e/ _% g+ p& T5 T  listen  80;. y1 P; U0 |# a; ~& w
  server_name localhost mike.hi-linux.com;6 G! r) \; y/ M( @4 O: t
  access_log /var/log/nginx/yourdomain.log;
) W$ M" c+ ^& m# b7 R* c0 [3 {1 `
) A7 I( a# F, X( _" f
/ E8 ~, s3 e7 E7 I  location / {
4 r0 m; i. R/ l2 m9 f% \6 q. x
! w' T" N# d1 K$ l
- H8 X, ^( v$ t  modsecurity on;, R, F+ ^$ _+ ~. R  ~
  modsecurity_rules_file modsec_includes.conf;
1 R- {% ~3 m" u1 \  root   html;
1 S. `( z& n; k' o1 O" }: l  index  index.html index.htm;
: T% Z( D5 ~. Q& f$ n# n) a}
, q) N/ l' L0 c) d: E; ^5 j7 I8 s) s}6 p, l4 L( ^% A4 W
验证Nginx配置文件( T0 q0 T9 m4 g2 Z) N
$ /usr/local/nginx/sbin/nginx -t
1 y2 R$ N/ R) S1 tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok6 ?% s0 q0 Z2 k' A4 _
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful& j7 L5 v. o/ t& k# |
启动Nginx
2 |6 O6 N. o, P  j# W# ]( j$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf0 I2 i1 n, v% r7 [$ R

/ D+ L9 ^9 m! w/ `. A

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


3 ?8 Q) `# l  z6 V- N

本帖子中包含更多资源

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

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

本版积分规则

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

GMT+8, 2025-12-15 01:12 , Processed in 0.074515 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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