RegUnloadkey access denied

F

future_retro

In the following script I can't get the reg key to unload. Anyone got
any ideas? I can load and save the key without any problems. I can
unload the key from regedit and have checked security on the key. The
docs don't mention any special privileges are needed
(SeRestorePrivilege should do it). I'm stumped. Cheers peeps, MW.

import ntsecuritycon
import win32security
import win32api
import _winreg
import win32con

flags = ntsecuritycon.TOKEN_ADJUST_PRIVILEGES |
ntsecuritycon.TOKEN_QUERY
htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(),
flags)
Loadid = win32security.LookupPrivilegeValue(None, 'SeRestorePrivilege')
#Saveid = win32security.LookupPrivilegeValue(None, 'SeBackupPrivilege')
LoadPrivilege = [(Loadid, ntsecuritycon.SE_PRIVILEGE_ENABLED)]
#SavePrivilege = [(Saveid, ntsecuritycon.SE_PRIVILEGE_ENABLED)]
win32security.AdjustTokenPrivileges(htoken, 0, LoadPrivilege)
#win32security.AdjustTokenPrivileges(htoken, 0, SavePrivilege)

key = _winreg.HKEY_USERS
hkey = win32api.RegLoadKey(key,'Marc','c:\\ntuser.dat')
okey =
win32api.RegOpenKeyEx(key,'Marc\\test',0,win32con.KEY_ALL_ACCESS)
win32api.RegSetValueEx(okey,'test',0,_winreg.REG_DWORD,0)
#win32api.RegSaveKey(okey,'c:\\ntuser2.dat',None)
#win32api.RegFlushKey(_winreg.HKEY_USERS)
win32api.RegUnLoadKey(key,'Marc')
 
F

future_retro

I've worked out that I can open and close the key fine if I don't open
a subkey. I've tried adding flush and close statements after opening
the key and still get access denied when trying to unload.
 
F

future_retro

solved it!

I need to close the key first

import ntsecuritycon
import win32security
import win32api
import _winreg
import win32con

flags = ntsecuritycon.TOKEN_ADJUST_PRIVILEGES |
ntsecuritycon.TOKEN_QUERY
htoken = win32security.OpenProcessToken(win32api.GetCurrentProcess(),
flags)
Loadid = win32security.LookupPrivilegeValue(None, 'SeRestorePrivilege')
Saveid = win32security.LookupPrivilegeValue(None, 'SeBackupPrivilege')
LoadPrivilege = [(Loadid, ntsecuritycon.SE_PRIVILEGE_ENABLED)]
SavePrivilege = [(Saveid, ntsecuritycon.SE_PRIVILEGE_ENABLED)]
win32security.AdjustTokenPrivileges(htoken, 0, LoadPrivilege)
win32security.AdjustTokenPrivileges(htoken, 0, SavePrivilege)

key = _winreg.HKEY_USERS
hkey = win32api.RegLoadKey(key,'Marc','c:\\ntuser.dat')
okey = win32api.RegOpenKeyEx(key,'Marc\\test',0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(okey,'test',0,_winreg.REG_DWORD,0)
win32api.RegCloseKey(okey)
win32api.RegUnLoadKey(key,'Marc')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top