explicit_entries.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import os
  2. import win32security,win32file,win32api,ntsecuritycon,win32con
  3. from security_enums import TRUSTEE_TYPE,TRUSTEE_FORM,ACE_FLAGS,ACCESS_MODE
  4. fname = os.path.join(win32api.GetTempPath(), "win32security_test.txt")
  5. f=open(fname, "w")
  6. f.write("Hello from Python\n");
  7. f.close()
  8. print("Testing on file", fname)
  9. new_privs = ((win32security.LookupPrivilegeValue('',ntsecuritycon.SE_SECURITY_NAME),win32con.SE_PRIVILEGE_ENABLED),
  10. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_SHUTDOWN_NAME),win32con.SE_PRIVILEGE_ENABLED),
  11. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_RESTORE_NAME),win32con.SE_PRIVILEGE_ENABLED),
  12. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_TAKE_OWNERSHIP_NAME),win32con.SE_PRIVILEGE_ENABLED),
  13. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_CREATE_PERMANENT_NAME),win32con.SE_PRIVILEGE_ENABLED),
  14. (win32security.LookupPrivilegeValue('','SeEnableDelegationPrivilege'),win32con.SE_PRIVILEGE_ENABLED) ##doesn't seem to be in ntsecuritycon.py ?
  15. )
  16. ph = win32api.GetCurrentProcess()
  17. th = win32security.OpenProcessToken(ph,win32security.TOKEN_ALL_ACCESS) ##win32con.TOKEN_ADJUST_PRIVILEGES)
  18. win32security.AdjustTokenPrivileges(th,0,new_privs)
  19. all_security_info = \
  20. win32security.OWNER_SECURITY_INFORMATION|win32security.GROUP_SECURITY_INFORMATION| \
  21. win32security.DACL_SECURITY_INFORMATION|win32security.SACL_SECURITY_INFORMATION
  22. sd=win32security.GetFileSecurity(fname,all_security_info)
  23. old_sacl=sd.GetSecurityDescriptorSacl()
  24. if old_sacl==None:
  25. old_sacl=win32security.ACL()
  26. old_dacl=sd.GetSecurityDescriptorDacl()
  27. if old_dacl==None:
  28. old_dacl=win32security.ACL()
  29. my_sid = win32security.GetTokenInformation(th,ntsecuritycon.TokenUser)[0]
  30. tmp_sid = win32security.LookupAccountName('','tmp')[0]
  31. pwr_sid = win32security.LookupAccountName('','Power Users')[0]
  32. ## MultipleTrustee,MultipleTrusteeOperation,TrusteeForm,TrusteeType,Identifier
  33. ## first two are ignored
  34. my_trustee = {}
  35. my_trustee['MultipleTrustee']=None
  36. my_trustee['MultipleTrusteeOperation']=0
  37. my_trustee['TrusteeForm']=TRUSTEE_FORM.TRUSTEE_IS_SID
  38. my_trustee['TrusteeType']=TRUSTEE_TYPE.TRUSTEE_IS_USER
  39. my_trustee['Identifier']=my_sid
  40. tmp_trustee = {}
  41. tmp_trustee['MultipleTrustee']=None
  42. tmp_trustee['MultipleTrusteeOperation']=0
  43. tmp_trustee['TrusteeForm']=TRUSTEE_FORM.TRUSTEE_IS_NAME
  44. tmp_trustee['TrusteeType']=TRUSTEE_TYPE.TRUSTEE_IS_USER
  45. tmp_trustee['Identifier']='rupole\\tmp'
  46. pwr_trustee = {}
  47. pwr_trustee['MultipleTrustee']=None
  48. pwr_trustee['MultipleTrusteeOperation']=0
  49. pwr_trustee['TrusteeForm']=TRUSTEE_FORM.TRUSTEE_IS_SID
  50. pwr_trustee['TrusteeType']=TRUSTEE_TYPE.TRUSTEE_IS_USER
  51. pwr_trustee['Identifier']=pwr_sid
  52. expl_list=[]
  53. expl_list.append(
  54. {
  55. 'Trustee':my_trustee,
  56. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  57. 'AccessMode':ACCESS_MODE.SET_AUDIT_SUCCESS, ##|ACCESS_MODE.SET_AUDIT_FAILURE,
  58. 'AccessPermissions':win32con.GENERIC_ALL
  59. }
  60. )
  61. expl_list.append(
  62. {
  63. 'Trustee':my_trustee,
  64. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  65. 'AccessMode':ACCESS_MODE.SET_AUDIT_FAILURE,
  66. 'AccessPermissions':win32con.GENERIC_ALL
  67. }
  68. )
  69. expl_list.append(
  70. {
  71. 'Trustee':tmp_trustee,
  72. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  73. 'AccessMode':ACCESS_MODE.SET_AUDIT_SUCCESS,
  74. 'AccessPermissions':win32con.GENERIC_ALL
  75. }
  76. )
  77. expl_list.append(
  78. {
  79. 'Trustee':tmp_trustee,
  80. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  81. 'AccessMode':ACCESS_MODE.SET_AUDIT_FAILURE,
  82. 'AccessPermissions':win32con.GENERIC_ALL
  83. }
  84. )
  85. old_sacl.SetEntriesInAcl(expl_list)
  86. expl_list=[]
  87. expl_list.append(
  88. {
  89. 'Trustee':tmp_trustee,
  90. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  91. 'AccessMode':ACCESS_MODE.DENY_ACCESS,
  92. 'AccessPermissions':win32con.DELETE
  93. }
  94. )
  95. expl_list.append(
  96. {
  97. 'Trustee':tmp_trustee,
  98. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  99. 'AccessMode':ACCESS_MODE.GRANT_ACCESS,
  100. 'AccessPermissions':win32con.WRITE_OWNER
  101. }
  102. )
  103. expl_list.append(
  104. {
  105. 'Trustee':pwr_trustee,
  106. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  107. 'AccessMode':ACCESS_MODE.GRANT_ACCESS,
  108. 'AccessPermissions':win32con.GENERIC_READ
  109. }
  110. )
  111. expl_list.append(
  112. {
  113. 'Trustee':my_trustee,
  114. 'Inheritance':ACE_FLAGS.NO_INHERITANCE,
  115. 'AccessMode':ACCESS_MODE.GRANT_ACCESS,
  116. 'AccessPermissions':win32con.GENERIC_ALL
  117. }
  118. )
  119. old_dacl.SetEntriesInAcl(expl_list)
  120. sd.SetSecurityDescriptorSacl(1,old_sacl,1)
  121. sd.SetSecurityDescriptorDacl(1,old_dacl,1)
  122. sd.SetSecurityDescriptorOwner(pwr_sid,1)
  123. win32security.SetFileSecurity(fname,
  124. all_security_info,
  125. sd)