<jupyter_start><jupyter_text>Simple PCAP file loading and structure explotationIn this example, we will simply load a sample PCAP file and explore its contents using the tools we saw in the demo. Later in the tutorial we will look at how to query this data for the attributes we may wish to utilise for a machine learning example.<jupyter_code>import dpkt
import gzip
import os
import datetime
os.listdir('../data')
print(os.listdir('../data'))
input_zipfile = '../data/maccdc2012_00000.pcap.gz'
pcap_data = gzip.open(input_zipfile, 'rb')
pcap = dpkt.pcap.Reader(pcap_data)
top20 = list(next(pcap) for _ in range(20))
for ts, buf in top20:    
    eth = dpkt.ethernet.Ethernet(buf)

    thetime = datetime.datetime.utcfromtimestamp(float(ts))
    timestr = thetime.strftime("%H:%M:%S.%f")
    
    ip = eth.data
    tcp = ip.data
    
    srcPort = tcp.sport
    destPort = tcp.dport
    print("%s: %s --> %s" % (timestr, str(srcPort), str(destPort)))
http_traffic = [(ts, buf) for (ts, buf) in top20 if dpkt.ethernet.Ethernet(buf).data.data.dport ==80]
len(http_traffic)
print(http_traffic[-1])
ts, buf = top20[5]
eth = dpkt.ethernet.Ethernet(buf)

thetime = datetime.datetime.utcfromtimestamp(float(ts))
timestr = thetime.strftime("%H:%M:%S.%f")

ip = eth.data
tcp = ip.data

srcPort = tcp.sport
destPort = tcp.dport
eth
ip
tcp<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12950699043414277, 'fraction_numerical': 0.01692420897718911, 'mean_word_length': 4.354330708661418, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '145184', 'license': 'Apache-2.0', 'n_tokens_mistral': 509, 'n_tokens_neox': 492, 'n_words': 134, 'path': 'tutorial/pcap/PCAP File Loading.ipynb', 'repo_name': 'tleeuwenburg/AusCERT-2019', 'size': '1359'}