Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegRestoreKey.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import ntsecuritycon
  3. import win32api
  4. import win32con
  5. import win32security
  6. import winnt
  7. temp_dir = win32api.GetTempPath()
  8. fname = win32api.GetTempFileName(temp_dir, "rsk")[0]
  9. print(fname)
  10. ## file can't exist
  11. os.remove(fname)
  12. ## enable backup and restore privs
  13. required_privs = (
  14. (
  15. win32security.LookupPrivilegeValue("", ntsecuritycon.SE_BACKUP_NAME),
  16. win32con.SE_PRIVILEGE_ENABLED,
  17. ),
  18. (
  19. win32security.LookupPrivilegeValue("", ntsecuritycon.SE_RESTORE_NAME),
  20. win32con.SE_PRIVILEGE_ENABLED,
  21. ),
  22. )
  23. ph = win32api.GetCurrentProcess()
  24. th = win32security.OpenProcessToken(
  25. ph, win32con.TOKEN_READ | win32con.TOKEN_ADJUST_PRIVILEGES
  26. )
  27. adjusted_privs = win32security.AdjustTokenPrivileges(th, 0, required_privs)
  28. try:
  29. sa = win32security.SECURITY_ATTRIBUTES()
  30. my_sid = win32security.GetTokenInformation(th, ntsecuritycon.TokenUser)[0]
  31. sa.SECURITY_DESCRIPTOR.SetSecurityDescriptorOwner(my_sid, 0)
  32. k, disp = win32api.RegCreateKeyEx(
  33. win32con.HKEY_CURRENT_USER,
  34. "Python test key",
  35. SecurityAttributes=sa,
  36. samDesired=win32con.KEY_ALL_ACCESS,
  37. Class="some class",
  38. Options=0,
  39. )
  40. win32api.RegSetValue(k, None, win32con.REG_SZ, "Default value for python test key")
  41. subk, disp = win32api.RegCreateKeyEx(
  42. k,
  43. "python test subkey",
  44. SecurityAttributes=sa,
  45. samDesired=win32con.KEY_ALL_ACCESS,
  46. Class="some other class",
  47. Options=0,
  48. )
  49. win32api.RegSetValue(subk, None, win32con.REG_SZ, "Default value for subkey")
  50. win32api.RegSaveKeyEx(
  51. k, fname, Flags=winnt.REG_STANDARD_FORMAT, SecurityAttributes=sa
  52. )
  53. restored_key, disp = win32api.RegCreateKeyEx(
  54. win32con.HKEY_CURRENT_USER,
  55. "Python test key(restored)",
  56. SecurityAttributes=sa,
  57. samDesired=win32con.KEY_ALL_ACCESS,
  58. Class="restored class",
  59. Options=0,
  60. )
  61. win32api.RegRestoreKey(restored_key, fname)
  62. finally:
  63. win32security.AdjustTokenPrivileges(th, 0, adjusted_privs)