dpkt eth.dst возвращает как ascii, так и hex в одной строке. Как получить его как просто hex? - PullRequest
0 голосов
/ 26 апреля 2020

Использование dpkt для извлечения информации из файла pcap, содержащего пакеты pcap. Когда я запускаю команды eth.dst и eth.sr c, они возвращаются в виде строк, которые объединяют ascii и hex в одну строку. Как мне остановить это, когда я пытаюсь превратить гекс в ма c адрес? В данный момент mac_addr возвращает ошибку, поскольку переданная строка не может быть преобразована.

def __init__(self,filename):
    f = open(filename,"rb")
    pcap = dpkt.pcap.Reader(f)
    for ts, buf in pcap:
        eth = dpkt.ethernet.Ethernet(buf) 
        ip = eth.data          
        tcp = eth.data.data
        print("\nNew Packet")
        print('Timestamp: ', str(datetime.datetime.utcfromtimestamp(ts)))
        print('Ethernet Frame: ',self.mac_addr(eth.dst),self.mac_addr(eth.src),eth.type)
        if not isinstance(eth.data, dpkt.ip.IP):
            print("This IP Packet Is Not Supported")
            continue

    def mac_addr(self,address):
    print(address)
    try:
        s = '{0:016x}'.format(address)
        s = ':'.join(re.findall(r'\w\w',s))
        return s
    except:
        return "Error"

This is the results that I get when i print the eth.src and eth.dst. This is the packet opened in Wireshark, as you can see on the bottom of the image on the left and middle column the hex is desplayed, on the right the characters that can be converted to ascii are displayed, however in the photo above they are being mashed together into one.

...