1

我需要“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

4

1 回答 1

0

进行绑定时,应将第二个参数设置为 0: rawSocket.bind(("wlp5s0", 0))

我能够接收ARP数据包没问题

于 2022-02-08T01:46:14.837 回答