Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env python3
- import sys
- fh = open('/Users/Derek/Downloads/net.cap', 'rb')
- fh.seek(24) # Skip the header
- loop_count = 0
- final_data = dict()
- while True:
- timestamp_sec_bytes = fh.read(4)
- if not timestamp_sec_bytes:
- break
- loop_count += 1
- timestamp_ms_bytes = fh.read(4)
- packet_size_bytes = fh.read(4)
- packet_len_bytes = fh.read(4)
- packet_size = int.from_bytes(packet_size_bytes, byteorder="little")
- ethernet_frame_bytes = fh.read(packet_size)
- ip_datagram_bytes = ethernet_frame_bytes[14:]
- ip_datagram_header_length = 4*(ip_datagram_bytes[0] & 0x0F)
- #print("Header length: {}".format(ip_datagram_header_length))
- source_ip_address = ip_datagram_bytes[12:16]
- formatted_ip_source = ["{:d}".format(b)for b in source_ip_address]
- #print("Source IP: {}".format(".".join(formatted_ip_source)))
- dest_ip_address = ip_datagram_bytes[16:20]
- formatted_ip_dest = ["{:d}".format(b)for b in dest_ip_address]
- #print("Dest IP: {}".format(".".join(formatted_ip_dest)))
- if ".".join(formatted_ip_dest) == "192.30.252.154":
- continue
- tcp_payload = ip_datagram_bytes[ip_datagram_header_length:]
- seq_num = int.from_bytes(tcp_payload[4:8], byteorder="little")
- print("Sequence: {}".format(seq_num))
- tcp_data_offset = tcp_payload[12] >> 4
- print("TCP Data Offset: {}".format(tcp_data_offset))
- final_data[seq_num] = tcp_payload[tcp_data_offset*4:]
- #print(ethernet_frame_bytes[0:22])
- # print(packet_size)
- print("")
- # break
- print("Found " + str(loop_count) + " packets")
- print("There are {} items in our dictionary".format(len(final_data)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement