Ayy
Ayy

Reputation: 488

Scapy spoofing UDP packet error

AttributeError: 'bytearray' object has no attribute '__rdiv__'

I get this for the following code:

b = bytearray([0xff, 0xff])

def spoof(src_ip, src_port, dest_ip, dest_port):
    global b
    spoofed_packet = IP(src=src_ip, dst=dest_ip) / TCP(sport=src_port, dport=dest_port) / b
    send(spoofed_packet)

Found the example to spoof the packet on stackoverflow but it didn't use a bytearray, I assume I need to convert the bytearray to a string?

Also my scapy keeps opening powershell any way around that?

I fixed that error by making the bytearray into a string now I get the following error:

    os.write(1,b".")
    OSError: [Errno 9] Bad file descriptor

Upvotes: 4

Views: 1698

Answers (3)

Pierre
Pierre

Reputation: 6237

Bytearray objects cannot be converted to Packet objects (and thus, Scapy cannot send them, this explains the 'bytearray' object has no attribute '__rdiv__' error). You need to convert b, using str() (if you are using Scapy before 2.4.0, with Python 2), or raw() (with Scapy 2.4.0 and more, with either Python 2 or 3).

I strongly recommend that you upgrade to Scapy 2.4.0. This should fix the Bad file descriptor error and the Powershell windows.

For example, your code with raw() (replace with str() if you are using Scapy < 2.4.0):

b = bytearray([0xff, 0xff])

def spoof(src_ip, src_port, dest_ip, dest_port):
    global b
    spoofed_packet = IP(src=src_ip, dst=dest_ip) / TCP(sport=src_port, dport=dest_port) / raw(b)
    send(spoofed_packet)

If you don't have to use a bytearray object, you can also use a bytes/str object directly:

b = b"\xff\xff"

def spoof(src_ip, src_port, dest_ip, dest_port):
    global b
    spoofed_packet = IP(src=src_ip, dst=dest_ip) / TCP(sport=src_port, dport=dest_port) / b
    send(spoofed_packet)

Upvotes: 1

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

You can use the below code

from scapy.all import IP, UDP, L3RawSocket, conf
from scapy.all import send as scapy_send


def send(dest_ip, port, src_ip, payload, count=1):
    if dest_ip in ("127.0.0.1", "localhost"):
        conf.L3socket = L3RawSocket
    ip = IP(dst=dest_ip, src=src_ip)
    udp = UDP(dport=port)
    scapy_send(ip/udp/str(payload), count=count)


send("192.168.1.100", 9090, "192.168.33.100", "Tarun here", 2)

UDP Packet sending

Upvotes: 0

ScottMcC
ScottMcC

Reputation: 4460

Could perhaps be with the version of Python that you are using?

Looks like the __rdiv__ operator may have been depreciated in python3?

See the following SO question:

Have the `__rdiv__()` and `__idiv__` operators changed in Python 3.x?

Upvotes: 0

Related Questions