Pha3drus
Pha3drus

Reputation: 131

Setting source port for UDP packet with Scapy?

I am trying to spoof UDP packets for a school project, and I am using Scapy to do this. For my purposes, I need to be able to set the source port to a specific port. However, when I do this with Scapy, it doesn't seem to have the correct source port when received on the other end. For instance, if I listen with Wireshark, all of the packet contents are correct except for the source port. For reference, I am doing the following in Scapy (super simple):

a=IP()
set a.src and a.dst
b=UDP()
set b.dport and b.sport
payload="HELLO"
packet=a/b/payload
send(packet)

Any ideas as to why the port number is not coming out correctly when the packet is sent? Thanks!

Upvotes: 1

Views: 8795

Answers (2)

stackprotector
stackprotector

Reputation: 13588

If somone is looking for how to use a random source port:

from scapy.all import *
send(IP(src="1.3.3.7",dst="7.3.3.1")/UDP(sport=RandShort(),dport=13337)/Raw(load="hello"))

Upvotes: 1

Saviour
Saviour

Reputation: 322

I didn't quite understand your code but this should do it:

myPack = IP() / UDP(sport=2000) / Raw('hello')
send(myPack)

Upvotes: 3

Related Questions