Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending custom frame / packet in Python

I read many articles and found how to send custom packet based on IP using socket(AF_INET, SOCK_RAW, IPPROTO_RAW). But I want to send completely custom packet starting from Ethernet header. I can't send ARP packet if I can't form Ethernet header cause ARP don't based IP. Please, help! P.S. I am on Windows 7, not Linux :(

like image 854
Denys Malovanyi Avatar asked Oct 21 '25 04:10

Denys Malovanyi


1 Answers

In python, the easiest way is to use the cross-platform scapy library. It’s well known for that

Scapy

You can sniff, send.... lots of packets, add your own protocols, use existing ones... and it works on nearly all platforms. (On windows, it uses Npcap/Winpcap)

You can then build an ARP packet using

from scapy.all import *
pkt = ARP()
pkt.show()
sendp(Ether(dst=..., src=...)/pkt)

Which will create such packets

###[ ARP ]###
hwtype= 0x1
ptype= 0x800
hwlen= 6
plen= 4
op= who-has
hwsrc= 00:50:56:00:1e:3d
psrc= 212.83.148.19
hwdst= 00:00:00:00:00:00
pdst= 0.0.0.0

To build the packet, use the / operator

ether = Ether()
ether.src = “00:00:00:00:00:00”
ether.dst = “ff:ff:ff:ff:ff:ff”
arp = ARP()
[edit arp.psrc, arp.pdst, arp.hwsrc, arp.hwdst]
packet = ether/arp
sendp(packet) # sens packet on layer 2

Have a look at its Scapy documentation

like image 162
Cukic0d Avatar answered Oct 22 '25 18:10

Cukic0d