Get baseaddress of a process












0















I'm trying to lear about memory reading and for that I'm trying to get a base address of a process to find pointers later on. The game is called Assault Cube.



I can get the name of the process, the ID of the process, the processHandle, but I cannot find the baseaddress and I've been at this for 8 hours now.



Below is my code so you can see I'm atleast trying myself before coming here. I was trying to use getModuleHandleA(), but it doesn't seem to work and always return 0.



Picture: Cheat Engine



import os.path
import ctypes
import ctypes.wintypes

# Process Permissions
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020

MAX_PATH = 260

class winAPI():

def __init__(self):
pass

def enumerateProcesses(self):

count = 32
while True:

# Create correct size of array
ProcessIds = (ctypes.wintypes.DWORD*count)()
# Pointer to the array
lpidProcess = ctypes.byref(ProcessIds)
# The size of the processID's
sizeOfArray = ctypes.sizeof(ProcessIds)
# How many bytes there are in the array
BytesReturned = ctypes.wintypes.DWORD()

# If the function succeeds, return value is 1, if fail return value is 0
if ctypes.windll.Psapi.EnumProcesses(lpidProcess, sizeOfArray, ctypes.byref(BytesReturned)):

# If our array is large enough to contain the bytes, return
if BytesReturned.value < sizeOfArray:
# print("ProcessIds:", ProcessIds, "BytesReturned:", BytesReturned.value)


return ProcessIds, BytesReturned.value

# If our array is NOT large enough, add 32*2 bytes to the array
else:
count = count * 2
else:
# Call winapi'sGetLastError for a better explaination?
return None

def OpenProcess(self, dwProcessId):

dwDesiredAccess = (PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE)
bInheritHandle = False


hProcess = ctypes.windll.kernel32.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)
if hProcess:
return hProcess
else:
return None

def GetProcessIdByName(self, pName):

# Add .exe to name if not provided in argument
if pName.endswith('.exe'):
pass
else:
pName = pName + '.exe'

# Split our returned function into PID's and bytes
ProcessIds, BytesReturned = self.enumerateProcesses()
listPIDs = list(ProcessIds)

for i in range(len(listPIDs)):

ProcessID = ProcessIds[i]
hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)

if hProcess:

# print(hex(id(hProcess)))

ImageFileName = (ctypes.c_char*MAX_PATH)()

if ctypes.windll.psapi.GetProcessImageFileNameA(hProcess, ImageFileName, MAX_PATH) > 0:


filename = os.path.basename(ImageFileName.value).decode('utf-8')

BaseAddress = (ctypes.windll.kernel32.GetModuleHandleA(filename) )
print("Baseaddy", BaseAddress)

if filename == pName:

print("ProcessID:", ProcessID, "|", "hProcess:", hProcess)
return ProcessID, hProcess

self.HandleCloser(hProcess)

def HandleCloser(self, hProcess):

# Calls winAPI's CloseHandle function and closes handle
ctypes.windll.kernel32.CloseHandle(hProcess)

return None

if __name__ == "__main__":

api = winAPI()
pid = api.GetProcessIdByName("ac_client.exe")
processID = pid[0]
hProcess = pid[1]
print("hProcess", hProcess)









share|improve this question

























  • You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

    – Barmak Shemirani
    Dec 30 '18 at 19:10













  • Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

    – user10829235
    Dec 30 '18 at 19:24











  • You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

    – user10829235
    Dec 30 '18 at 19:36











  • It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

    – Barmak Shemirani
    Dec 30 '18 at 21:48


















0















I'm trying to lear about memory reading and for that I'm trying to get a base address of a process to find pointers later on. The game is called Assault Cube.



I can get the name of the process, the ID of the process, the processHandle, but I cannot find the baseaddress and I've been at this for 8 hours now.



Below is my code so you can see I'm atleast trying myself before coming here. I was trying to use getModuleHandleA(), but it doesn't seem to work and always return 0.



Picture: Cheat Engine



import os.path
import ctypes
import ctypes.wintypes

# Process Permissions
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020

MAX_PATH = 260

class winAPI():

def __init__(self):
pass

def enumerateProcesses(self):

count = 32
while True:

# Create correct size of array
ProcessIds = (ctypes.wintypes.DWORD*count)()
# Pointer to the array
lpidProcess = ctypes.byref(ProcessIds)
# The size of the processID's
sizeOfArray = ctypes.sizeof(ProcessIds)
# How many bytes there are in the array
BytesReturned = ctypes.wintypes.DWORD()

# If the function succeeds, return value is 1, if fail return value is 0
if ctypes.windll.Psapi.EnumProcesses(lpidProcess, sizeOfArray, ctypes.byref(BytesReturned)):

# If our array is large enough to contain the bytes, return
if BytesReturned.value < sizeOfArray:
# print("ProcessIds:", ProcessIds, "BytesReturned:", BytesReturned.value)


return ProcessIds, BytesReturned.value

# If our array is NOT large enough, add 32*2 bytes to the array
else:
count = count * 2
else:
# Call winapi'sGetLastError for a better explaination?
return None

def OpenProcess(self, dwProcessId):

dwDesiredAccess = (PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE)
bInheritHandle = False


hProcess = ctypes.windll.kernel32.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)
if hProcess:
return hProcess
else:
return None

def GetProcessIdByName(self, pName):

# Add .exe to name if not provided in argument
if pName.endswith('.exe'):
pass
else:
pName = pName + '.exe'

# Split our returned function into PID's and bytes
ProcessIds, BytesReturned = self.enumerateProcesses()
listPIDs = list(ProcessIds)

for i in range(len(listPIDs)):

ProcessID = ProcessIds[i]
hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)

if hProcess:

# print(hex(id(hProcess)))

ImageFileName = (ctypes.c_char*MAX_PATH)()

if ctypes.windll.psapi.GetProcessImageFileNameA(hProcess, ImageFileName, MAX_PATH) > 0:


filename = os.path.basename(ImageFileName.value).decode('utf-8')

BaseAddress = (ctypes.windll.kernel32.GetModuleHandleA(filename) )
print("Baseaddy", BaseAddress)

if filename == pName:

print("ProcessID:", ProcessID, "|", "hProcess:", hProcess)
return ProcessID, hProcess

self.HandleCloser(hProcess)

def HandleCloser(self, hProcess):

# Calls winAPI's CloseHandle function and closes handle
ctypes.windll.kernel32.CloseHandle(hProcess)

return None

if __name__ == "__main__":

api = winAPI()
pid = api.GetProcessIdByName("ac_client.exe")
processID = pid[0]
hProcess = pid[1]
print("hProcess", hProcess)









share|improve this question

























  • You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

    – Barmak Shemirani
    Dec 30 '18 at 19:10













  • Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

    – user10829235
    Dec 30 '18 at 19:24











  • You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

    – user10829235
    Dec 30 '18 at 19:36











  • It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

    – Barmak Shemirani
    Dec 30 '18 at 21:48
















0












0








0








I'm trying to lear about memory reading and for that I'm trying to get a base address of a process to find pointers later on. The game is called Assault Cube.



I can get the name of the process, the ID of the process, the processHandle, but I cannot find the baseaddress and I've been at this for 8 hours now.



Below is my code so you can see I'm atleast trying myself before coming here. I was trying to use getModuleHandleA(), but it doesn't seem to work and always return 0.



Picture: Cheat Engine



import os.path
import ctypes
import ctypes.wintypes

# Process Permissions
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020

MAX_PATH = 260

class winAPI():

def __init__(self):
pass

def enumerateProcesses(self):

count = 32
while True:

# Create correct size of array
ProcessIds = (ctypes.wintypes.DWORD*count)()
# Pointer to the array
lpidProcess = ctypes.byref(ProcessIds)
# The size of the processID's
sizeOfArray = ctypes.sizeof(ProcessIds)
# How many bytes there are in the array
BytesReturned = ctypes.wintypes.DWORD()

# If the function succeeds, return value is 1, if fail return value is 0
if ctypes.windll.Psapi.EnumProcesses(lpidProcess, sizeOfArray, ctypes.byref(BytesReturned)):

# If our array is large enough to contain the bytes, return
if BytesReturned.value < sizeOfArray:
# print("ProcessIds:", ProcessIds, "BytesReturned:", BytesReturned.value)


return ProcessIds, BytesReturned.value

# If our array is NOT large enough, add 32*2 bytes to the array
else:
count = count * 2
else:
# Call winapi'sGetLastError for a better explaination?
return None

def OpenProcess(self, dwProcessId):

dwDesiredAccess = (PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE)
bInheritHandle = False


hProcess = ctypes.windll.kernel32.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)
if hProcess:
return hProcess
else:
return None

def GetProcessIdByName(self, pName):

# Add .exe to name if not provided in argument
if pName.endswith('.exe'):
pass
else:
pName = pName + '.exe'

# Split our returned function into PID's and bytes
ProcessIds, BytesReturned = self.enumerateProcesses()
listPIDs = list(ProcessIds)

for i in range(len(listPIDs)):

ProcessID = ProcessIds[i]
hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)

if hProcess:

# print(hex(id(hProcess)))

ImageFileName = (ctypes.c_char*MAX_PATH)()

if ctypes.windll.psapi.GetProcessImageFileNameA(hProcess, ImageFileName, MAX_PATH) > 0:


filename = os.path.basename(ImageFileName.value).decode('utf-8')

BaseAddress = (ctypes.windll.kernel32.GetModuleHandleA(filename) )
print("Baseaddy", BaseAddress)

if filename == pName:

print("ProcessID:", ProcessID, "|", "hProcess:", hProcess)
return ProcessID, hProcess

self.HandleCloser(hProcess)

def HandleCloser(self, hProcess):

# Calls winAPI's CloseHandle function and closes handle
ctypes.windll.kernel32.CloseHandle(hProcess)

return None

if __name__ == "__main__":

api = winAPI()
pid = api.GetProcessIdByName("ac_client.exe")
processID = pid[0]
hProcess = pid[1]
print("hProcess", hProcess)









share|improve this question
















I'm trying to lear about memory reading and for that I'm trying to get a base address of a process to find pointers later on. The game is called Assault Cube.



I can get the name of the process, the ID of the process, the processHandle, but I cannot find the baseaddress and I've been at this for 8 hours now.



Below is my code so you can see I'm atleast trying myself before coming here. I was trying to use getModuleHandleA(), but it doesn't seem to work and always return 0.



Picture: Cheat Engine



import os.path
import ctypes
import ctypes.wintypes

# Process Permissions
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020

MAX_PATH = 260

class winAPI():

def __init__(self):
pass

def enumerateProcesses(self):

count = 32
while True:

# Create correct size of array
ProcessIds = (ctypes.wintypes.DWORD*count)()
# Pointer to the array
lpidProcess = ctypes.byref(ProcessIds)
# The size of the processID's
sizeOfArray = ctypes.sizeof(ProcessIds)
# How many bytes there are in the array
BytesReturned = ctypes.wintypes.DWORD()

# If the function succeeds, return value is 1, if fail return value is 0
if ctypes.windll.Psapi.EnumProcesses(lpidProcess, sizeOfArray, ctypes.byref(BytesReturned)):

# If our array is large enough to contain the bytes, return
if BytesReturned.value < sizeOfArray:
# print("ProcessIds:", ProcessIds, "BytesReturned:", BytesReturned.value)


return ProcessIds, BytesReturned.value

# If our array is NOT large enough, add 32*2 bytes to the array
else:
count = count * 2
else:
# Call winapi'sGetLastError for a better explaination?
return None

def OpenProcess(self, dwProcessId):

dwDesiredAccess = (PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION |
PROCESS_VM_READ | PROCESS_VM_WRITE)
bInheritHandle = False


hProcess = ctypes.windll.kernel32.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)
if hProcess:
return hProcess
else:
return None

def GetProcessIdByName(self, pName):

# Add .exe to name if not provided in argument
if pName.endswith('.exe'):
pass
else:
pName = pName + '.exe'

# Split our returned function into PID's and bytes
ProcessIds, BytesReturned = self.enumerateProcesses()
listPIDs = list(ProcessIds)

for i in range(len(listPIDs)):

ProcessID = ProcessIds[i]
hProcess = ctypes.windll.kernel32.OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessID)

if hProcess:

# print(hex(id(hProcess)))

ImageFileName = (ctypes.c_char*MAX_PATH)()

if ctypes.windll.psapi.GetProcessImageFileNameA(hProcess, ImageFileName, MAX_PATH) > 0:


filename = os.path.basename(ImageFileName.value).decode('utf-8')

BaseAddress = (ctypes.windll.kernel32.GetModuleHandleA(filename) )
print("Baseaddy", BaseAddress)

if filename == pName:

print("ProcessID:", ProcessID, "|", "hProcess:", hProcess)
return ProcessID, hProcess

self.HandleCloser(hProcess)

def HandleCloser(self, hProcess):

# Calls winAPI's CloseHandle function and closes handle
ctypes.windll.kernel32.CloseHandle(hProcess)

return None

if __name__ == "__main__":

api = winAPI()
pid = api.GetProcessIdByName("ac_client.exe")
processID = pid[0]
hProcess = pid[1]
print("hProcess", hProcess)






python python-3.x memory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 30 '18 at 19:36







user10829235

















asked Dec 30 '18 at 11:54









user10829235user10829235

61




61













  • You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

    – Barmak Shemirani
    Dec 30 '18 at 19:10













  • Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

    – user10829235
    Dec 30 '18 at 19:24











  • You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

    – user10829235
    Dec 30 '18 at 19:36











  • It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

    – Barmak Shemirani
    Dec 30 '18 at 21:48





















  • You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

    – Barmak Shemirani
    Dec 30 '18 at 19:10













  • Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

    – user10829235
    Dec 30 '18 at 19:24











  • You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

    – user10829235
    Dec 30 '18 at 19:36











  • It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

    – Barmak Shemirani
    Dec 30 '18 at 21:48



















You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

– Barmak Shemirani
Dec 30 '18 at 19:10







You don't have access to memory in another process. ctypes.windll.kernel32.ReadProcessMemory will fail. You have to allocate memory with VirtualAllocEx. Please also explain what you are trying to accomplish.

– Barmak Shemirani
Dec 30 '18 at 19:10















Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

– user10829235
Dec 30 '18 at 19:24





Thanks for your reply :) I want to scan for pointers, and for that I need a module's base-address. Could you by any chance show me how you would do it through VirtualAllocEx? I'm still learning about memory reading so any kind of help would be appreciated

– user10829235
Dec 30 '18 at 19:24













You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

– user10829235
Dec 30 '18 at 19:36





You said I don't have access to memory in another process, but isn't it possible with the access I have to find the base address of the memory I'm accessing? I'm accessing the processID of "ac_client.exe", so isn't it possible to retrieve it's baseaddress while being inside the memory of the process?

– user10829235
Dec 30 '18 at 19:36













It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

– Barmak Shemirani
Dec 30 '18 at 21:48







It looks like you are trying to hack a game. Windows uses address space layout randomization (beginning with Vista) to deliberately make this difficult. If you know where to look for (find an address using the cheat software...) then use VirtualAllocEx to allocate memory and ReadProcessMemory to read it. The addresses change, I think every time the program restarts. I don't know much else about this subject.

– Barmak Shemirani
Dec 30 '18 at 21:48














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977360%2fget-baseaddress-of-a-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977360%2fget-baseaddress-of-a-process%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'