Python Forum
Windows - Get Handle to Pass as Parameter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Windows - Get Handle to Pass as Parameter
#1
I'm not sure about how to proceed when dealing with Windows (W10 1803, if that helps). Here's the code I'm trying to work with:
token = ctypes.create_unicode_buffer(1024)
handle = ctypes.windll.Kernel32.GetModuleHandleW()
print(ctypes.windll.Advapi32.OpenProcessToken(handle, 'WRITE_OWNER', token))
print(ctypes.windll.Kernel32.GetLastError())
sys.exit()
The error I get is 6 - unknown handle. So, my question is: how do I get the handle. Eventually, what I want to do is enable the SE_TAKE_OWNERSHIP_NAME privilege and open a registry key with WRITE_OWNER access. Microsoft makes you go through 10 steps to do one simple thing. So, I'm trying to change the privileges of the access token of the script (Python.exe, I guess) so that I can access an administrator level registry key.
Reply
#2
Well, I slept on it, woke up and started Googling. Here's what I have:
handle = ctypes.windll.Kernel32.GetCurrentProcess()
print(ctypes.windll.Advapi32.OpenProcessToken(handle, 'TOKEN_QUERY', token))
print(ctypes.windll.Kernel32.GetLastError())
I needed the GetCurrentProcess() function to get the handle. But now I get an ErrorCode 5 - Acess Denied return. I'm an administrator account. The only account on this computer. I would really appreciate a Windows guru or two here. How can I access the token? I've read I can impersonate the LOCAL_SYSTEM account...

By the way: sorry. I know this isn't specifically a Python issue; I just figured there are some experienced programmers here. Where else could I go for help?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I handle escape character in parameter arguments in Python? JKR 6 1,148 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  How to pass encrypted pass to pyodbc script tester_V 0 857 Jul-27-2023, 12:40 AM
Last Post: tester_V
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 11,023 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,557 Sep-07-2020, 08:02 AM
Last Post: perfringo
  script that handle windows notifications. darktitan 1 2,310 Apr-10-2020, 12:47 PM
Last Post: Mateusz
  Pass by reference vs Pass by value leodavinci1990 1 2,211 Nov-20-2019, 02:05 AM
Last Post: jefsummers
  The derivate class dosn't behave like the base when i pass parameter to them drudox 4 3,174 Aug-05-2018, 06:42 PM
Last Post: drudox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020