Getting the TCP packet payload from Python and impacket

I managed to find the packages of interest using code based on this example:

How to filter pcap file by specific protocol using python?

The next child from the TCP packet is the actual data:

   if isinstance(child1, TCP):
        if child1.get_th_dport() == 80:
           x = child1.child()
           print x

It prints package data, such as a wired shark, and displays hex and ascii versions. However, I have not yet been able to find a way to simply get the hex content. I know that I can manipulate the print conclusions, but I decided that there should be a way to get the data in hexadecimal ...

I looked through the samples, but no one seems to be doing this. Does anyone know the right way?

+3
source share
1 answer

packet.get_data_as_string() , , . "hex column", print child. ASCII:

def display_hex(pkt, cols=8):
    size = cols * 4
    data = ''.join('%02x' % ord(b) for b in pkt.get_data_as_string())
    for i in range(0, len(data), size):
        for j in range(0, size, 4):
            print data[i+j:i+j+4],
        print

if isinstance(child, TCP):
    display_hex(child)

:

1703 0103 b0b1 9387 be4e fe00 9230 6192
e3bb 217e c1cb 8511 556f f986 4f31 542d
15c6 f42e f3bb 93d5 cf33 f126 c174 dbc4
... snip ...
8b1d 8707 96d6 7a18 2aab fd0b 48ee c4eb
b7d8 a67f 8bc0 597d 1044 a076 1a9e 24ba
959b fda3 1adb 2384 669c e6c8 c3b5 bef4
1189 eda8 3e  
+2

All Articles