Skip to content

Instantly share code, notes, and snippets.

@kanazux
Last active January 25, 2019 15:12
Show Gist options
  • Save kanazux/68fbd8290cf926855e5dfa6af7775458 to your computer and use it in GitHub Desktop.
Save kanazux/68fbd8290cf926855e5dfa6af7775458 to your computer and use it in GitHub Desktop.
Return a dict from packet in pcap file
#!/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
from collections import defaultdict
from scapy.all import IP, Raw, rdpcap
class get_dump_data(object):
def __init__(self, dump_file):
self.dump = rdpcap(dump_file)
self.pkt_list = []
def get_data(self):
for item in self.dump:
_new_dict = defaultdict(lambda: False)
_new_dict["time"] = datetime.utcfromtimestamp(item.time).strftime(
'%Y-%m-%d %H:%M:%S')
if IP in item:
_new_dict["id"] = item[IP].id
_new_dict["src"] = item[IP].src
_new_dict["dst"] = item[IP].dst
_new_dict["proto"] = item[IP].proto
if TCP in item:
_new_dict["sport"] = item[TCP].sport
_new_dict["dport"] = item[TCP].dport
if Raw in item:
_new_dict["raw"] = str(item[Raw].load)
self.pkt_list.append(_new_dict)
def run(self):
self.get_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment