-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddresses.py
257 lines (203 loc) · 9.46 KB
/
Addresses.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Python 3.6.5
import win32api, ctypes
import win32gui
import win32process
import win32con
from ctypes import wintypes as w
from win32com.client import GetObject
from re import findall, match
kernel32 = ctypes.windll.kernel32
psapi = ctypes.windll.psapi
class MODULEINFO(ctypes.Structure):
_fields_ = [
("lpBaseOfDll", ctypes.c_void_p),
("SizeOfImage", w.DWORD),
("EntryPoint", ctypes.c_void_p),
]
GetModuleInformation = psapi.GetModuleInformation
GetModuleInformation.restype = w.BOOL
GetModuleInformation.argtypes = [w.HANDLE, w.HMODULE, ctypes.POINTER(MODULEINFO), w.DWORD]
ReadProcessMemory = kernel32.ReadProcessMemory
ReadProcessMemory.argtypes = [w.HANDLE, w.LPCVOID, w.LPVOID, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t)]
ReadProcessMemory.restype = w.BOOL
WriteProcessMemory = kernel32.WriteProcessMemory
WriteProcessMemory.restype = w.BOOL
WriteProcessMemory.argtypes = [w.HANDLE, w.LPVOID, w.LPCVOID, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t) ]
VirtualAllocEx = kernel32.VirtualAllocEx
VirtualAllocEx.restype = w.LPVOID
VirtualAllocEx.argtypes = (w.HANDLE, w.LPVOID, w.DWORD, w.DWORD, w.DWORD)
VirtualFreeEx = kernel32.VirtualFreeEx
VirtualFreeEx.restype = w.LPVOID
VirtualFreeEx.argtypes = (w.HANDLE, w.LPVOID, w.DWORD, w.DWORD)
GetLastError = kernel32.GetLastError
GetLastError.restype = ctypes.wintypes.DWORD
GetLastError.argtypes = ()
MEM_RESERVE = 0x00002000
MEM_COMMIT = 0x00001000
MEM_DECOMMIT = 0x4000
MEM_RELEASE = 0x8000
PAGE_EXECUTE_READWRITE = 0x40
class AddressFile:
def __init__(self, path=None, data=None):
self.addr = {}
self.orig_addr = {}
self.path = path
self.moduleAddr = None
self.pointerPathFunction = None
if self.path:
self.reloadValues()
elif data:
self.readData(data)
def __getitem__(self, key):
if type(self.orig_addr[key]) == tuple and self.pointerPathFunction != None:
valueType, value, ptrPath = self.orig_addr[key]
calculatedAddress = self.moduleAddr + value if valueType == 1 else value
if len(ptrPath) > 0: #relative to main module
return self.pointerPathFunction(calculatedAddress, ptrPath)
else:
return calculatedAddress
return self.addr[key]
def readData(self, data):
self.data = data
for line in data.split("\n"):
line = line.strip()
if len(line) == 0 or line[0] == "#":
continue
name, _ = findall('^([a-zA-Z0-9\_\-]+)( *= *)', line)[0]
value = line[len(name + _):].split('#')[0]
if value.find(",") != -1: #pointer path
values = value.split(",")
moduleRelative = values[0][0] == "+"
values = [int(f, 16) for f in values]
self.addr[name] = moduleRelative, values[0], values[1:]
self.orig_addr[name] = moduleRelative, values[0], values[1:]
else:
if match('\+0(x|X)[0-9a-fA-F]+', value): #relative to module addr
value = 1, int(value, 16), []
elif match('-?0(x|X)[0-9a-fA-F]+', value):
value = int(value, 16)
elif match('-?[0-9]+', value):
value = int(value, 10)
else:
value = value.strip()
self.addr[name] = value
self.orig_addr[name] = value
def applyModuleAddress(self, addr, pointerPathFunction):
self.moduleAddr = addr
self.pointerPathFunction = pointerPathFunction
for key in self.addr:
if type(self.addr[key]) == tuple:
valueType, value, ptrPath = self.addr[key]
calculatedAddress = self.moduleAddr + value if valueType == 1 else value
if len(ptrPath) > 0: #relative to main module
try:
self.addr[key] = pointerPathFunction(calculatedAddress, ptrPath)
except:
self.addr[key] = 0
else:
self.addr[key] = calculatedAddress
def reloadValues(self):
try:
with open(self.path, "r") as f:
self.readData(f.read())
if self.moduleAddr != None:
try:
self.applyModuleAddress(self.moduleAddr)
except:
pass
except Exception as e:
print(e)
print("Could not load game_addresses.txt properly")
class GameClass:
def __init__(self, processName):
self.PID = 0
self.PROCESS = None
self.windowTitle = None
wmi = GetObject('winmgmts:')
for p in wmi.InstancesOf('win32_process'):
if p.Name == processName:
self.PID = int(p.Properties_('ProcessId'))
if self.PID == 0:
raise Exception("Couldn't find process \"%s\"" % (processName))
self.PROCESS = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, 0, self.PID)
self.handle = self.PROCESS.handle
self.getWindowTitle()
self.setModuleAddr(processName)
if self.moduleAddr == None:
self.setModuleAddr(".exe") #finds first module whose name ends in .exe
def close(self):
win32api.CloseHandle(self.handle)
def applyModuleAddress(self, addressDict):
addressDict.applyModuleAddress(self.moduleAddr, self.readPointerPath) #used for addresses relative to tekken's main module, mainly useful for tekken 7
def setModuleAddr(self, name):
module_handles = win32process.EnumProcessModules(self.PROCESS)
for m in module_handles:
filename = win32process.GetModuleFileNameEx(self.PROCESS, m)
if filename.endswith(name):
self.moduleAddr = m
module_info = MODULEINFO()
res = GetModuleInformation(self.handle, m, ctypes.byref(module_info), ctypes.sizeof(module_info))
self.moduleSize = module_info.SizeOfImage if res != 0 else None
return
self.moduleSize = None
self.moduleAddr = None
def enumWindowsProc(self, hwnd, lParam):
if win32process.GetWindowThreadProcessId(hwnd)[1] == lParam:
text = win32gui.GetWindowText(hwnd)
if text and (win32api.GetWindowLong(hwnd, win32con.GWL_STYLE) & win32con.WS_VISIBLE):
self.windowTitle = text
return
def getWindowTitle(self):
win32gui.EnumWindows(self.enumWindowsProc, self.PID)
return self.windowTitle
def readBytes(self, addr, bytes_length):
buff = ctypes.create_string_buffer(bytes_length)
bufferSize = ctypes.sizeof(buff)
bytesRead = ctypes.c_ulonglong(0)
if ReadProcessMemory(self.handle, addr, buff, bufferSize, ctypes.byref(bytesRead)) != 1:
raise Exception('Could not read memory addr "%x" (%d bytes): invalid address or process closed?' % (addr, bytes_length))
return bytes(buff)
def writeBytes(self, addr, value):
return WriteProcessMemory(self.handle, addr, bytes(value), len(value), None)
def readInt(self, addr, bytes_length=4, endian='little'):
return int.from_bytes(self.readBytes(addr, bytes_length), endian)
def writeInt(self, addr, value, bytes_length=0):
if bytes_length <= 0:
bytes_length = value.bit_length() + 7 // 8
return self.writeBytes(addr, value.to_bytes(bytes_length, 'little'))
def allocateMem(self, allocSize):
return VirtualAllocEx(self.handle, 0, allocSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE)
def freeMem(self, addr):
return VirtualFreeEx(self.handle, addr, 0, MEM_RELEASE) != None
def readPointerPath(self, currAddr, ptrlist):
for ptr in ptrlist:
old = currAddr
currAddr = self.readInt(currAddr, 8) + ptr
return currAddr
def aobScan(self, toSearch, aligned=True): #This is terribly slow and should not be used until improvements to speed.
#aobScan("48 89 91 ?? ?? 00 00")
currAddr = self.moduleAddr
moduleEnd = self.moduleAddr + self.moduleSize
toSearch = ''.join(t.upper() for t in toSearch if t.isalnum() and ord(t.upper()) <= 70)
hexNumbers = []
for i in range(0, len(toSearch), 2):
try:
hexNumbers.append(int(toSearch[i:i+2], 16))
except:
hexNumbers.append(None)
if aligned and currAddr % 4 != 0: currAddr += (4 - currAddr % 4) #4 bytes alignment
step = 1 if not aligned else 4
while currAddr < moduleEnd:
b = self.readBytes(currAddr, 4)
if b[0] == hexNumbers[0]:
found = True
for orig, comp in zip(hexNumbers, b):
if orig != comp and orig != None:
found = False
break
if found:
return currAddr
currAddr += step
return None
def bToInt(data, offset, length, endian='little'):
return int.from_bytes(data[offset:offset + length], endian)