Skip to content

Instantly share code, notes, and snippets.

@miou-gh
Last active June 14, 2017 14:36
Show Gist options
  • Select an option

  • Save miou-gh/f854cb71ec0f64245b0d0de8cd0baec1 to your computer and use it in GitHub Desktop.

Select an option

Save miou-gh/f854cb71ec0f64245b0d0de8cd0baec1 to your computer and use it in GitHub Desktop.
A WPE PAC Reader
import struct
from enum import Enum
class Packet:
def __init__(self, data, socket_id, source_addr, destination_addr, packet_size, function):
self.data = data
self.socket_id = socket_id
self.source_addr = source_addr
self.destination_addr = destination_addr
self.packet_size = packet_size
self.function = function
class SPT:
def __init__(self):
pass
class State(Enum):
HEAD = 1
NAME = 2
DATA = 3
def read_int(self, bytes, index):
return index + 4, struct.unpack('i', bytes[index:index+4])[0]
def read_str(self, bytes, index, count):
return index + count, struct.unpack(str(count) + 's', bytes[index:index+count])[0]
def read_bytes(self, bytes, index, count):
return index + count, bytearray(struct.unpack(str(count) + 's', bytes[index:index+count])[0])
def deserialize(self, input_bytes):
input_bytes = bytearray(input_bytes)
packets = []
index, capacity, = self.read_int(input_bytes, 0)
for i in range(0, capacity):
index, data_length = self.read_int(input_bytes, index)
index, data = self.read_bytes(input_bytes, index, data_length)
index, socket_id = self.read_int(input_bytes, index)
index, source_addr_length = self.read_int(input_bytes, index)
index, source_addr = self.read_str(input_bytes, index, source_addr_length)
index, destination_addr_length = self.read_int(input_bytes, index)
index, destination_addr = self.read_str(input_bytes, index, destination_addr_length)
index, packet_size_length = self.read_int(input_bytes, index)
index, packet_size = self.read_str(input_bytes, index, packet_size_length)
index, function_length = self.read_int(input_bytes, index)
index, function = self.read_str(input_bytes, index, function_length)
packets.append(Packet(data, socket_id, source_addr, destination_addr, packet_size, function))
pass
return packets
reader = SPT()
with open('x:/test.pac', 'rb') as pac:
packets = reader.deserialize(pac.read())
for packet in packets:
print('{} --> {}...'.format(packet.source_addr, packet.destination_addr))
print('[{}] Data Length: {} Packet Size: {} Socket Id: {}'.format(packet.function, len(packet.data), packet.packet_size, packet.socket_id))
print('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment