#!/usr/bin/env python

# disable IPv6 error message
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
logging.getLogger("scapy.runtime").setLevel(logging.WARN)
from pprint import pprint
import os
import sys

# disable payload parsing (saves two seconds runtime^^)
IP.payload_guess = []

bytes = bytearray()

#for p in PcapReader('large_flow.pcap'):
for p in PcapReader(sys.argv[1]):
    if IP in p:
        src = p[IP].src
        dst = p[IP].dst
        id = p[IP].id

        bin = "{0:016b}".format(id)
        upper = int(bin[0:8], 2)
        lower = int(bin[8:16], 2)
#        print ("Upper: %s (%d), Lower: %s (%d)" % (bin[0:8], upper, bin[8:16], lower))
#        print ("Full: %s" % (bin))
        bytes.append(upper)
        bytes.append(lower)

try:
    os.unlink('stream_encrypted')
except Exception as e:
    # ignore if file is missing
    pass
with open('stream_encrypted', 'a+') as encfile:
    encfile.write(bytes)

try:
    os.unlink('stream_decrypted')
except Exception as e:
    # ignore if file is missing
    pass
os.system('openssl enc -d -rc4 -nosalt -k nSa123 -in stream_encrypted -out stream_decrypted')
