regsave_sa.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. fname='h:\\tmp.reg'
  2. import win32api, win32con, win32security, ntsecuritycon, pywintypes,os
  3. ## regsave will not overwrite a file
  4. if os.path.isfile(fname):
  5. os.remove(fname)
  6. new_privs = ((win32security.LookupPrivilegeValue('',ntsecuritycon.SE_SECURITY_NAME),win32con.SE_PRIVILEGE_ENABLED),
  7. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_TCB_NAME),win32con.SE_PRIVILEGE_ENABLED),
  8. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_BACKUP_NAME),win32con.SE_PRIVILEGE_ENABLED),
  9. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_RESTORE_NAME),win32con.SE_PRIVILEGE_ENABLED)
  10. )
  11. ph = win32api.GetCurrentProcess()
  12. th = win32security.OpenProcessToken(ph,win32security.TOKEN_ALL_ACCESS|win32con.TOKEN_ADJUST_PRIVILEGES)
  13. win32security.AdjustTokenPrivileges(th,0,new_privs)
  14. my_sid = win32security.GetTokenInformation(th,ntsecuritycon.TokenUser)[0]
  15. hklm=win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE,None,0,win32con.KEY_ALL_ACCESS)
  16. skey=win32api.RegOpenKey(hklm,'SYSTEM',0,win32con.KEY_ALL_ACCESS)
  17. sa=pywintypes.SECURITY_ATTRIBUTES()
  18. sd=pywintypes.SECURITY_DESCRIPTOR()
  19. sa.SECURITY_DESCRIPTOR=sd
  20. acl=pywintypes.ACL()
  21. pwr_sid = win32security.LookupAccountName('','Power Users')[0]
  22. acl.AddAccessAllowedAce(win32con.ACL_REVISION,win32con.GENERIC_READ|win32con.ACCESS_SYSTEM_SECURITY,my_sid)
  23. sd.SetSecurityDescriptorDacl(1,acl,0)
  24. sd.SetSecurityDescriptorOwner(pwr_sid,0)
  25. sa.bInheritHandle=1
  26. assert sa.SECURITY_DESCRIPTOR is sd
  27. win32api.RegSaveKey(skey,fname,sa)