Python Binary Data Streaming over TCP

I’m working on gathering a data stream from CAN (python-can) and sending over TCP. I have a listener class that serializes the data using pickle.

class Sender(MessageWriter):

  def __init__(
          self,
          file: Optional[TextIO] = None,
          l = None,
  ):
    self.l = l
    super().__init__(file, l)

  def on_message_received(self, msg: Message) -> None:
    self.l.sendline(pickle.dumps(msg))

main sets up a notifier for the listener, and simply repeats the incoming data class to the listener.

from pwn import listen
def main():
  with can.Bus() as bus:
    l = listen(PORT)
    _ = l.wait_for_connection()

    echoListener = Echoer()
    sendListener = Sender()
    sendListener.l = l
    can.Notifier(bus, [echoListener, sendListener])

    input('Press enter to exit.')
    echoListener.stop()
    sendListener.stop()

The other end simply connects to the previous application and echos to stdout after deserializing what it receives.

from pwn import remote
from binascii import hexlify
from pickle import loads
from can import message

r = remote(HOST, PORT)
while True:
  m = r.recvline()
  msg = loads(m)
  print(f'{msg.channel} {msg.arbitration_id} [{len(msg.data)}] {hexlify(msg.data, " ").decode()}')

pwn was simply used because I’m more familiar with it than I am with python sockets. And maybe pickle isn’t the best choice here. It doesn’t handle chunking data. I’m not against forgoing the use of either library for something more appropriate.

Client side echoing script

./echoclient.py
[+] Opening connection to HOST on port PORT: Done
can0 1025 [8] fe 01 04 02 01 36 1a 01
...
can0 978 [4] ff ff ff 00
Traceback (most recent call last):
  File "/home/user/dev/./echoclient.py", line 11, in <module>
    msg = loads(m)
          ^^^^^^^^
_pickle.UnpicklingError: pickle data was truncated

Sender

 ./piside.py 
[+] Trying to bind to :: on port PORT: Done
[+] Waiting for connections on :::PORT: Got connection from ::ffff:HOST on port xPORT
can0 401 [8] fe 01 04 02 01 36 1a 01
...
can0 2fa [8] 00 01 00 08 10 c8 10 00
Exception in thread can.notifier for bus "socketcan channel 'can0'":
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.9/site-packages/pwnlib/tubes/sock.py", line 65, in send_raw
    self.sock.sendall(data)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/user/.local/lib/python3.9/site-packages/can/notifier.py", line 126, in _rx_thread
    handle_message(msg)
  File "/home/user/.local/lib/python3.9/site-packages/can/notifier.py", line 146, in _on_message_received
    res = callback(msg)
  File "/home/user/.local/lib/python3.9/site-packages/can/listener.py", line 43, in __call__
    self.on_message_received(msg)
  File "/home/user/dev/pican/./piside.py", line 42, in on_message_received
    self.l.sendline(pickle.dumps(msg))
  File "/home/user/.local/lib/python3.9/site-packages/pwnlib/tubes/tube.py", line 817, in sendline
    self.send(line + self.newline)
  File "/home/user/.local/lib/python3.9/site-packages/pwnlib/tubes/tube.py", line 796, in send
    self.send_raw(data)
  File "/home/user/.local/lib/python3.9/site-packages/pwnlib/tubes/sock.py", line 70, in send_raw
    raise EOFError
EOFError

So how do I either handle chunking the data for pickle, or what would be the better alternative method for streaming binary data classes? The messages that are being serialized and deserialized are classed objects. Each message should not be longer than a few bytes each. data is 8 bytes, arbitration_id is 2 bytes, a few other parameters, and the overhead data.

python-can message reference: https://python-can.readthedocs.io/en/stable/message.html

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật