Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python decoding UDP

code:

import socket, binascii, struct

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
while True:
    print s.recv(2048)

output:

Ek�9@@�F5��W�jq��� stackexchangecom� electronics �
h h

stackexchangecomDa�scifi ET@@�<���� stackoverflowcom���meta ,��� stackoverflowcom�A���meta ,��� stackexchangecomG��security Ee@@�+���� stackexchangecom���scifi

as you can see some of the data has been decoded/interpreted but the rest isn't not sure as to why

Can anyone help?

like image 890
Hobnob Avatar asked Nov 21 '25 02:11

Hobnob


1 Answers

You're printing raw UDP packets, which contain arbitrary binary data. Some of those bytes are in the printable range, but those that aren't in that range get converted into �.

You can get a better look at that data by printing its representation, which shows the printable bytes as normal and shows the unprintable ones as hexadecimal escape codes. To do that, change your print statement to:

    print repr(s.recv(2048))

I suspect you'd like to actually decode those packets. That's quite possible, but it's a bit technical, and you should probably study the topic a bit first. :) This article by Silver Moon, Code a network packet sniffer in python for Linux, looks quite helpful.

like image 170
PM 2Ring Avatar answered Nov 23 '25 16:11

PM 2Ring