import socket
import struct
import binascii
def formatMAC(mac):
return mac.lower().replace(':', '')
def sendPacket(packet):
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind(("wlan0", 0))
return s.send(packet)
eth_src = formatMAC('A0:88:B4:0A:A5:A8')
eth_dst = formatMAC("18:A6:F7:CF:51:B6")
eth_prt = '0806'
arp_hw_type = '0001'
arp_prt_type = '0800'
arp_hw_size = '0006'
arp_prt_size = "0004"
arp_opcode = '0002'
arp_mac_src = formatMAC('A0:88:B4:0A:A5:A8')
arp_ip_src = '192.168.0.134'
arp_mac_dst = formatMAC('18:A6:F7:CF:51:B6')
arp_ip_dst = '192.168.0.1'
eth_pack = struct.pack("!6s6s2s", binascii.unhexlify(eth_dst), binascii.unhexlify(eth_src), binascii.unhexlify(eth_prt))
arp_pack = struct.pack("2s2s1s1s2s6s4s6s4s",
binascii.unhexlify(arp_hw_type),
binascii.unhexlify(arp_prt_type),
binascii.unhexlify(arp_hw_size),
binascii.unhexlify(arp_prt_size),
binascii.unhexlify(arp_opcode),
binascii.unhexlify(arp_mac_src),
socket.inet_aton(arp_ip_src),
binascii.unhexlify(arp_mac_dst),
socket.inet_aton(arp_ip_dst)
)
packet = eth_pack + arp_pack
print(sendPacket(packet))
感谢。
答案 0 :(得分:0)
如果您将Wireshark hexdump与您尝试发送的内容相匹配,您将看到"硬件地址大小"和#34;协议地址大小"实际发送的数据包的字段都是00。
这是因为您将两个字节传递给struct.pack
(0006和0004),但是您告诉它(使用1s
)只格式化1个字节,因此它只输出第一个字节。
如果你看一下hexdump,你会发现地址实际上已经被发送了,Wireshark只是忽略了它们,因为它的大小应该是0。
只需将0006和0004更改为06和04,因为这些是1字节字段。