How to depickle the object same as it was sent from socket in python?

Viewed 151

I am trying to serialize the object (using pickle) of type scapy.layers.l2.Ether (please refer scapy import statement of code). But while deserializing that object, it is not being retrieved properly (also shown in output). How can I fix this ? and get the object as same as it was serialized ?

import socket
import time
import pickle #serialize the object
import sys  # for command line argument
from scapy.all import * # for packet sniffing

HEADERSIZE = 10


clientsocket = 1
address = 1

#for serialization
def pkt_callback(pkt):
    pkt.show()
    msg = pickle.dumps(pkt)
    print(12)
    clientsocket.send(msg)

#check whether ip and port number provided or not
if len(sys.argv) != 3: 
    print ("Correct usage: script, IP address, port number", flush = True)
    exit(0) 

IP_address = str(sys.argv[1]) 
Port = int(sys.argv[2])

#socket creation ipv6
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.bind((IP_address, Port))
s.listen(5)


while True:
    # now our endpoint knows about the OTHER endpoint.
    clientsocket, address = s.accept()
    print(f"Connection from {address} has been established.",flush=True)

    sniff(iface="wlp3s0", prn=pkt_callback, filter="tcp", store=0)

clientsocket.close()
print('Data sent to server')

The above code serializes the object to be sent at pkt_callback Now below code is from receiving side which deserializes the object

import socket
import pickle
import sys
from scapy.all import * # for packet sniffing

HEADERSIZE = 10

#check whether address and port number given or not

if len(sys.argv) != 3: 
    print ("Correct usage: script, IP address, port number",flush = True)
    exit(0) 

IP_address = str(sys.argv[1]) 
Port = int(sys.argv[2])

#create ipv6 socket
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.connect((IP_address, Port))


while True:
    data = s.recv(4096)
    data_recved = pickle.loads(data)
    (scapy.layers.l2.Ether(data_recved)).show()

s.close()

The 1st code which serializes the object works fine and shows output as expected. The 2nd code which deserializes the object works but gives wrong output as below

###[ Raw ]### 
  load      = '\xb0\xfc6`\xe5\xe3\xe4\xa7\xc5I,\x1c\x08\x00E(\x004%\xd4@\x00h\x06s!o\xdd\x1d\xfe\xc0\xa8+#\x01\xbb\xda\xb4\xf5 \x05\xfbv\xac\xccj\x80\x10\x04\x02\xb6z\x00\x00\x01\x01\x08\n!\x84\xf3\xa3\x066\x0c\x99'

###[ Raw ]### 
  load      = "\xb0\xfc6`\xe5\xe3\xe4\xa7\xc5I,\x1c\x08\x00E(\x01o%\xd5@\x00h\x06q\xe5o\xdd\x1d\xfe\xc0\xa8+#\x01\xbb\xda\xb4\xf5 \x05\xfbv\xac\xccj\x80\x19\x04\x02B\xbf\x00\x00\x01\x01\x08\n!\x84\xf3\xfd\x066\x0c\x99\x17\x03\x03\x016\x00\x00\x00\x00\x00\x00\x00\x01n\xb3\x18\x93\xd2xT\x00\xe3Xh\xa7\xc0?|\xb3n\xf6?g\x00\xdf\xcf\x97wx\xccw\x04\x08\xbfM\xf2L1^\x84\xa1\xb0\x06\xaas\xdb\xc9\xb1\xed\x99\x8cz\x9a5P\xa3{\x04y\xbb\xc38\xcfT\\\x98f\xf1z/F\x1e\xd6tD\x90pN\x92\xe1\xf9\xee@\xbc\x1a(\x89{x\xf2\\\xd1\x14\x1d?\x02\x07\x16N\xe4\x017\x140\xc5\x81\xb4\xcd\t\x8ai\x1a\xe7PZ\x01\xbek\xba \x84[\\\xcc\xf0\xecs\xc8^\xff!\xce\x19z,\x13\xbd\xa3\x0c\x0fq\xca=\xbc\x1a834\xee\xf9hC\x0b\xb4\x85\x114'5W\xee\xed&\x93Y0\xd1\x85\xc3(\xb3\x0e0u\\\x9d\xa7\xb5g\xb0\xb4C_S\xe7\xf8\xbaH\t\x94\x84\xaf\xa9\tS\xb6\xf4XYD\tM\xf8=`\xbd\x04\xeeo\x1f\xd3\x7f*\xef\x8e\xd2\x00\x8f\xfb\x8c\xfemB\x02\xc0\x12\x8a\r\x14\xdco\\K\xfd\x97%\xbc\x15}\xb1\xe2<\xdei\x15\x17\xeec>=\xa0\xc0]\xfaa\xeal`\x10c\xe8\x86B\xff'w\xe3\xe4\xce\x9d\xbf0\xdc\xb0F\x12\x8b{;9\xa1\xce\xfdf\x1c\x8b\x8f\xc4\xc0F\xf3\\\x86\xbd\xcdu\xdb\xb8>t\xf6\xff.~h"

###[ Raw ]### 
  load      = "\xe4\xa7\xc5I,\x1c\xb0\xfc6`\xe5\xe3\x08\x00E\x00\x00SP\xcb@\x00@\x06p3\xc0\xa8+#o\xdd\x1d\xfe\xda\xb4\x01\xbbv\xac\xccj\xf5 \x077\x80\x18\x01>\x93\x10\x00\x00\x01\x01\x08\n\x066\r\xb7!\x84\xf3\xfd\x15\x03\x03\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x02Nkk'T\xcdv$\xb7e\xe00\xc9\x9f\xf2\xffw4"

###[ Raw ]### 
  load      = '\xe4\xa7\xc5I,\x1c\xb0\xfc6`\xe5\xe3\x08\x00E\x00\x004P\xcc@\x00@\x06pQ\xc0\xa8+#o\xdd\x1d\xfe\xda\xb4\x01\xbbv\xac\xcc\x89\xf5 \x077\x80\x11\x01>\xb6j\x00\x00\x01\x01\x08\n\x066\r\xb7!\x84\xf3\xfd'

but it should be like

###[ Ethernet ]### 
  dst       = e4:a7:c5:49:2c:1c
  src       = b0:fc:36:60:e5:e3
  type      = IPv6
###[ IPv6 ]### 
     version   = 6
     tc        = 0
     fl        = 630855
     plen      = 32
     nh        = TCP
     hlim      = 255
     src       = 2409:4042:2ea4:752f:cd15:9fe5:6dc1:1575
     dst       = 2404:6800:4009:810::200e
###[ TCP ]### 
        sport     = 36318
        dport     = https
        seq       = 3279176249
        ack       = 3388920021
        dataofs   = 8
        reserved  = 0
        flags     = A
        window    = 254
        chksum    = 0xf7e7
        urgptr    = 0
        options   = [('NOP', None), ('NOP', None), ('Timestamp', (427468701, 3715875499))]

How can I fix it ?

1 Answers

I have recently found the solution. Actually in second code snippet, the object show function on object was called by doing unnecessary typecasting. The line (scapy.layers.l2.Ether(data_recved)).show() should be simply like data_recved.show(). And then we can get desired output

Related