-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathutils.py
57 lines (44 loc) · 1.48 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import socket
from struct import pack
from typing import List
class RopChain:
def __init__(self, base=None, pack_str='<I', chain=b''):
self.chain = chain
self.base = base or 0
self.pack_str = pack_str
def __iadd__(self, other):
if isinstance(other, int):
self.chain += self._pack_32(self.base + other)
elif isinstance(other, bytes):
self.chain += other
else:
raise NotImplementedError
return self
def __len__(self) -> int:
return len(self.chain)
@staticmethod
def p32(address) -> bytes:
return pack('<I', address)
def _pack_32(self, address) -> bytes:
return pack(self.pack_str, address)
def append_raw(self, address):
""" just ignore the base address; useful for actual values in conjunction with pop r32 """
self.chain += pack(self.pack_str, address)
def get_connection(ip: str, port: int) -> socket.socket:
sock = None
while sock is None:
try:
sock = socket.create_connection((ip, port))
except ConnectionRefusedError:
continue
return sock
def sanity_check(byte_str: bytes, bad_chars: List[int]):
baddies = list()
for bc in bad_chars:
if bc in byte_str:
print(f"[!] bad char found: {hex(bc)}")
baddies.append(bc)
if baddies:
print(f"[=] {byte_str}")
print("[!] Remove bad characters and try again")
raise SystemExit