account_rights.py 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. import win32security,win32file,win32api,ntsecuritycon,win32con
  2. from security_enums import TRUSTEE_TYPE,TRUSTEE_FORM,ACE_FLAGS,ACCESS_MODE
  3. new_privs = ((win32security.LookupPrivilegeValue('',ntsecuritycon.SE_SECURITY_NAME),win32con.SE_PRIVILEGE_ENABLED),
  4. (win32security.LookupPrivilegeValue('',ntsecuritycon.SE_CREATE_PERMANENT_NAME),win32con.SE_PRIVILEGE_ENABLED),
  5. (win32security.LookupPrivilegeValue('','SeEnableDelegationPrivilege'),win32con.SE_PRIVILEGE_ENABLED) ##doesn't seem to be in ntsecuritycon.py ?
  6. )
  7. ph = win32api.GetCurrentProcess()
  8. th = win32security.OpenProcessToken(ph,win32security.TOKEN_ALL_ACCESS) ##win32con.TOKEN_ADJUST_PRIVILEGES)
  9. win32security.AdjustTokenPrivileges(th,0,new_privs)
  10. policy_handle = win32security.GetPolicyHandle('',win32security.POLICY_ALL_ACCESS)
  11. tmp_sid = win32security.LookupAccountName('','tmp')[0]
  12. privs=[ntsecuritycon.SE_DEBUG_NAME,ntsecuritycon.SE_TCB_NAME,ntsecuritycon.SE_RESTORE_NAME,ntsecuritycon.SE_REMOTE_SHUTDOWN_NAME]
  13. win32security.LsaAddAccountRights(policy_handle,tmp_sid,privs)
  14. privlist=win32security.LsaEnumerateAccountRights(policy_handle,tmp_sid)
  15. for priv in privlist:
  16. print(priv)
  17. privs=[ntsecuritycon.SE_DEBUG_NAME,ntsecuritycon.SE_TCB_NAME]
  18. win32security.LsaRemoveAccountRights(policy_handle,tmp_sid,0,privs)
  19. privlist=win32security.LsaEnumerateAccountRights(policy_handle,tmp_sid)
  20. for priv in privlist:
  21. print(priv)
  22. win32security.LsaClose(policy_handle)