Create a gist now

Instantly share code, notes, and snippets.

import struct
class PayloadGenerator:
def __init__(self, payload):
self.payload = payload
def get_length(self, payload):
return sum(map(lambda x: len(x), self.payload))
def generate(self, base=0):
data = dict()
payload = list(self.payload)
raw_length = self.get_length(payload)
append_length = 0
i = 0
while i < len(payload):
item = payload[i]
if isinstance(item, str):
pass
elif isinstance(item.value, list):
tup = tuple(item.value)
if tuple(item.value) not in data:
payload += tup
data[tup] = item.pack(base + raw_length + append_length)
append_length += self.get_length(tup)
payload[i] = data[tup]
elif isinstance(item.value, str):
if item.value not in data:
payload.append(item.value)
data[item.value] = item.pack(base + raw_length + append_length)
append_length += len(item.value)
payload[i] = data[item.value]
else:
raise TypeError
i += 1
return ''.join(payload)
class Ptr32:
def __init__(self, val):
if not (isinstance(val, str)
or isinstance(val, Ptr32)
or isinstance(val, list)):
raise TypeError
self.value = val
def pack(self, addr):
return struct.pack('<I', addr)
def __len__(self):
return 4
class Ptr64:
def __init__(self, val):
if not (isinstance(val, str)
or isinstance(val, Ptr64)
or isinstance(val, list)):
raise TypeError
self.value = val
def pack(self, addr):
return struct.pack('<Q', addr)
def __len__(self):
return 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment