I am trying to extract TLS meta-data from a pcap using Scapy. I am able to successfully parse the packets and individual messages such as the client-hello, server-hello etc
Not sure if there is a better solution, but here's what I did to get around my problem. The accumulated frame-sizes seem to agree with what Wireshark does when it reassembles TLS frame from multiple packets.
Note: This solution assumes there are no misordered packets or duplicates. Also, the solution shown here is not complete. The code below only shows how to accumulate TLS frames when they span multiple IP packets for a single TCP stream. Please follow Janus's suggestion (mentioned in the comments below) or come up with your own solution to accumulate different the streams.
def extractDataFromPcap(pcapfile):
load_layer("tls")
try:
reader = PcapReader(pcapfile)
# tls frame accumulation related variables.
tls_accumulate = False
tls_leftover_len = 0
tls_blist = list()
for pkt in reader:
try:
if tls_accumulate and pkt.haslayer(TCP):
ip_tcp_hdr_overhead = ((pkt[IP].ihl*4) + (pkt[TCP].dataofs*4))
pkt_payload_len = (pkt[IP].len - ip_tcp_hdr_overhead)
tls_leftover_len = (tls_leftover_len - pkt_payload_len)
tls_blist.append(raw(pkt[TCP].payload))
if tls_leftover_len <= 0:
# got complete TLS frame
tls_raw_bytes = b''.join(tls_blist)
# parse accumulated frame.
tls = TLS(tls_raw_bytes)
#... process tls object
# reset accumlation variables.
tls_accumulate = False
tls_leftover_len = 0
tls_blist = list()
else:
continue
if pkt.haslayer(TCP) and pkt.haslayer(TLS):
if not tls_accumulate:
# Process new TLS frame.
# Pkt over head: ip-hdr-len + tcp-hdr-len
ip_tcp_hdr_overhead = ((pkt[IP].ihl*4) + (pkt[TCP].dataofs*4))
pkt_payload_len = (pkt[IP].len - ip_tcp_hdr_overhead)
if pkt[TLS].len > pkt_payload_len:
# partial TLS frame. Start accumulating.
tls_accumulate = True
tls_leftover_len = (pkt[TLS].len - pkt_payload_len)
tls_blist.append(raw(pkt[TCP].payload))
else:
# complete TLS frame
# ... process complete TLS Frame
except Exception as error:
pkt.show()
print("tcp-processing error: %s", error)
sys.exit(-1)
except Exception as error:
print("packet processing error: %s", error)
sys.exit(-1)