我需要“在一个程序中发送和接收ARP
数据包”。我知道scapy
数据包已经很好地使其安静了,您只需要传递一些参数即可。但是,除了传递参数外,我还想了解更多。因此,如果您推荐scapy
作为答案,则可能无济于事。
python
import struct
import socket
import binascii
rawSocket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW,
socket.htons(0x0003))
rawSocket.bind(("wlp5s0", socket.htons(0x0003)))
source_mac = binascii.unhexlify('00:A0:C9:14:C8:29'.replace(':', ''))
#b'\x00\x00\x00\x00\x00\x00' sender mac address
dest_mac = binascii.unhexlify('ff:ff:ff:ff:ff:ff'.replace(':', ''))
# b'\xff\xff\xff\xff\xff\xff' target mac address
source_ip = "192.168.100.3" # sender ip address
dest_ip = "192.168.100.1" # target ip address
# Ethernet Header
protocol = 0x0806 # 0x0806 for ARP
eth_hdr = struct.pack("!6s6sH", dest_mac, source_mac, protocol)
# ARP header
htype = 1 # Hardware_type ethernet
ptype = 0x0800 # Protocol type TCP
hlen = 6 # Hardware address Len
plen = 4 # Protocol addr. len
operation = 1 # 1=request/2=reply
src_ip = socket.inet_aton(source_ip)
dst_ip = socket.inet_aton(dest_ip)
arp_hdr = struct.pack("!HHBBH6s4s6s4s", htype, ptype, hlen, plen, operation,
source_mac, src_ip, dest_mac, dst_ip)
packet = eth_hdr + arp_hdr
rawSocket.send(packet)
rawSocket.recvfrom(65535)
```
By the way I can clear see the packet already send and reply but `Wireshark`
操作系统:Linux Ubuntn 16.04
解释器:Python 3.5.2