I've been translating a powershell script into python, mostly to learn how to do it. I've gotten stuck on these lines here:
$lpTargetHandle = [IntPtr]::Zero
$CallResult = [Kernel32]::DuplicateHandle(
$ProcessInfo.hProcess, 0x4,
[Kernel32]::GetCurrentProcess(),
[ref]$lpTargetHandle, 0, $false, 0x00000002)
echo $lpTargetHandle
This is what I have in python:
lpTargetHandle = HANDLE()
CallResult = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4,
kernel32.GetCurrentProcess(),byref(lpTargetHandle), 0, False, 0x00000002)
print(lpTargetHandle)
Here is the output I am getting:
>>> lpTargetHandle = HANDLE()
>>> CallResult = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4, kernel32.GetCurrentProcess(),byref(lpTargetHandle), 0, False, 0x00000002)
>>>
>>> print(lpTargetHandle)
c_void_p(None)
>>> lpTargetHandle.value
>>> type(lpTargetHandle.value)
<type 'NoneType'>
What is supposed to happen, is the lpTargetHandle pointer should return back the Thread ID number, but I'm just getting Nones. I've seen that IntPtr's are handled in IronPython, but my goal is to learn vanilla python. My includes are:
from ctypes import *
from ctypes.wintypes import *
How do you duplicate an IntPtr in normal Python (CPython)?
Specifically, how do you write $var = [IntPtr]::Zero
in python?
I've also tried this, but it did not work:
tid = POINTER(c_int)
num = c_int(0)
addr = addressof(num)
ptr = cast(addr,tid)
CallResult = = kernel32.DuplicateHandle(ProcessInfo.hProcess, 0x4,
kernel32.GetCurrentProcess(),ptr, 0, False, 0x00000002)
Here is a pastebin of the full python code I have
Here is a pastebin of the powershell function I am working on duplicating.
Edit: Here is the relevant function that I am trying to duplicate in C
HANDLE hThread = nullptr;
DuplicateHandle(procInfo.hProcess, (HANDLE)0x4,
GetCurrentProcess(), &hThread, 0, FALSE, DUPLICATE_SAME_ACCESS);
return hThread;