Last active
March 25, 2024 21:59
-
-
Save MarkBaggett/d8933453f431c111169158ce7f4e2222 to your computer and use it in GitHub Desktop.
Python - SCAPY - Full Packet Session Reassembly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#From here https://pen-testing.sans.org/blog/2017/10/13/scapy-full-duplex-stream-reassembly | |
def full_duplex(p): | |
sess = "Other" | |
if 'Ether' in p: | |
if 'IP' in p: | |
if 'TCP' in p: | |
sess = str(sorted(["TCP", p[IP].src, p[TCP].sport, p[IP].dst, p[TCP].dport],key=str)) | |
elif 'UDP' in p: | |
sess = str(sorted(["UDP", p[IP].src, p[UDP].sport, p[IP].dst, p[UDP].dport] ,key=str)) | |
elif 'ICMP' in p: | |
sess = str(sorted(["ICMP", p[IP].src, p[IP].dst, p[ICMP].code, p[ICMP].type, p[ICMP].id] ,key=str)) | |
else: | |
sess = str(sorted(["IP", p[IP].src, p[IP].dst, p[IP].proto] ,key=str)) | |
elif 'ARP' in p: | |
sess = str(sorted(["ARP", p[ARP].psrc, p[ARP].pdst],key=str)) | |
else: | |
sess = p.sprintf("Ethernet type=%04xr,Ether.type%") | |
return sess |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wanted to share my improved version. It's more compact, more general (handles all Ethernet packages), puts more information into the key string, and makes it easy to extract that information from the key string later.
Extract the info with a regex like this, here additionally using the
ipaddress
library, but that's optional of course.@MarkBaggett Thanks for the great blog post!
@jvmk Yes you can add any information contained within the packet object
p
. For example, my function adds more information on layers of the packet. Just note that some values may throw off grouping, for example, if you addedp[TCP].seq
, grouping wouldn't work anymore...