0

I am trying to send a custom packet (with a custom layer) using Scapy in python socket.

Here's the code of the client

#!/usr/bin/env python 

import socket
from scapy.all import *


TCP_PORT = 5000
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.240.1', TCP_PORT))


class Reservation(Packet):
    name = "ReservationPacket"
    fields_desc=[ ByteField("id", 0),
         BitField("type",None, 0),
         X3BytesField("update", 0),
         ByteField("rssiap", 0)]

k = IP(dst="192.168.240.1")/Reservation()
k.show()
send(k)

print "Reservation Message Sent"

s.close()

and the packet k appears to be successfully created and sent.

Here's the server which is responsible to receive the packet:

#!/usr/bin/python

import socket
from scapy.all import *


class Reservation(Packet):
    name = "ReservationPacket"
    fields_desc=[ ByteField("id", 0),
        BitField("type",None, 0),
        X3BytesField("update", 0),
        ByteField("rssiap", 0)]


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('192.168.240.1', 5000))
s.listen(1)

while True :

    conn, addr = s.accept()

    print 'Connection address:', addr

    print ''

    data = conn.recv(1024)
    data.show()
    conn.close

s.close()

and this is the output I get from the server:

Connection address: ('192.168.240.5', 58454)

Traceback (most recent call last):
File "server.py", line 36, in <module>
  data.show()
AttributeError: 'str' object has no attribute 'show'

How can I receive my packet and decode it to read its custom layer?

rebrid
  • 430
  • 8
  • 27

1 Answers1

0

Data became a string in this line:

data = conn.recv(1024)

Checking the doc https://docs.python.org/2/library/socket.html

socket.recv(bufsize[, flags]) Receive data from the socket. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by bufsize. See the Unix manual page recv(2) for the meaning of the optional argument flags; it defaults to zero.

What are you trying to do with data.show(), you just could print the data with:

print data

Also follow this link to decode the string: Python - converting sock.recv to string

stringdata = data.decode('utf-8')
Community
  • 1
  • 1
lapinkoira
  • 8,320
  • 9
  • 51
  • 94
  • data.show() should show the fields of my packet. If I try "print data" I don't get anything. I would like also to access the fields of my packet, like data.type but it's not working: Traceback (most recent call last): File "server.py", line 36, in print data.id AttributeError: 'str' object has no attribute 'id' – rebrid Nov 11 '15 at 10:34
  • You didnt read the documentation I pasted there. This data is a string, it hasnt id or show attributes, if you print it and its None it means it's empty. – lapinkoira Nov 11 '15 at 10:51
  • Thanks but I still don't know how to do, how can I receive it and then use the packet fields? – rebrid Nov 11 '15 at 14:15