mihowilk
mihowilk

Reputation: 23

Python raw socket changing symbols

I have 2 programs comunicating with each other via ethernet. Sending one is using scapy to encode port, ip and payload before sending it as ethernet frame. My problem is that in payload im sending counter and when reciving that it's sometimes changed to symbol.

\x00\x00\x00\x00\x00\x00\x00\x07
\x00\x00\x00\x00\x00\x00\x00\x08 is fine but next
\x00\x00\x00\x00\x00\x00\x00\t
\x00\x00\x00\x00\x00\x00\x00\n
\x00\x00\x00\x00\x00\x00\x00\x0b its fine again

later they are changed to next asci symbols

My question is how to stop converting bytes to asci?

sender.py

import socket
from scapy.all import *

PADDING_VALUE = b'\xd1'
ETH_P_ALL = 3
DST_IP = "127.0.0.12"
IFACE = "lo"
SRC_IP = "127.0.0.11"

class FpgaMockup:
    def __init__(self, setup_iface):
        self.setup_sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
        self.setup_sock.bind((setup_iface, 0))
        self.padding = 16

    def send(self, pkt):
        self.setup_sock.send(pkt)

if __name__ == "__main__":
    testing_fpga = FpgaMockup(IFACE)
    
    for i in range(100):
        packet = IP(dst=DST_IP, src=SRC_IP)/UDP(sport=12666, dport=12666)/Raw(load=int(i).to_bytes(8, "big")+PADDING_VALUE*testing_fpga.padding)
        pkt = Ether(packet)
        testing_fpga.send(raw(pkt))
    print("Finished sending.")

reciever.py

import socket

ETH_P_ALL = 3

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind(("lo", 0))

while(True):
    pkt = s.recv(4096)
    print(pkt)

Upvotes: 1

Views: 192

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123521

These are not "changed". \x09 is exactly the same as \t, \x0a is the same as \n. These are just printed differently but nevertheless are the same:

>>> print(b'\x08\x09\x0a\x0b')
b'\x08\t\n\x0b'
>>> b'\x08\x09\x0a\x0b' == b'\x08\t\n\x0b'
True

For more information see the documentation to the syntax of String and Bytes literals.

If you don't want to have this conversation simply enforce writing as a hexadecimal sequence instead of characters:

>>> b'\x08\t\n\x0b'.hex()
'08090a0b'

Upvotes: 1

Related Questions