GetTokenInformation.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """ Lists various types of information about current user's access token,
  2. including UAC status on Vista
  3. """
  4. import pywintypes, win32api, win32security
  5. import win32con, winerror
  6. from security_enums import TOKEN_GROUP_ATTRIBUTES, TOKEN_PRIVILEGE_ATTRIBUTES, \
  7. SECURITY_IMPERSONATION_LEVEL, TOKEN_TYPE, TOKEN_ELEVATION_TYPE
  8. def dump_token(th):
  9. token_type=win32security.GetTokenInformation(th, win32security.TokenType)
  10. print('TokenType:', token_type, TOKEN_TYPE.lookup_name(token_type))
  11. if token_type==win32security.TokenImpersonation:
  12. imp_lvl=win32security.GetTokenInformation(th, win32security.TokenImpersonationLevel)
  13. print('TokenImpersonationLevel:', imp_lvl, SECURITY_IMPERSONATION_LEVEL.lookup_name(imp_lvl))
  14. print('TokenSessionId:', win32security.GetTokenInformation(th, win32security.TokenSessionId))
  15. privs=win32security.GetTokenInformation(th,win32security.TokenPrivileges)
  16. print('TokenPrivileges:')
  17. for priv_luid, priv_flags in privs:
  18. flag_names, unk=TOKEN_PRIVILEGE_ATTRIBUTES.lookup_flags(priv_flags)
  19. flag_desc = ' '.join(flag_names)
  20. if (unk):
  21. flag_desc += '(' + str(unk) + ')'
  22. priv_name=win32security.LookupPrivilegeName('',priv_luid)
  23. priv_desc=win32security.LookupPrivilegeDisplayName('',priv_name)
  24. print('\t', priv_name, priv_desc, priv_flags, flag_desc)
  25. print('TokenGroups:')
  26. groups=win32security.GetTokenInformation(th,win32security.TokenGroups)
  27. for group_sid, group_attr in groups:
  28. flag_names, unk=TOKEN_GROUP_ATTRIBUTES.lookup_flags(group_attr)
  29. flag_desc = ' '.join(flag_names)
  30. if (unk):
  31. flag_desc += '(' + str(unk) + ')'
  32. if group_attr & TOKEN_GROUP_ATTRIBUTES.SE_GROUP_LOGON_ID:
  33. sid_desc = 'Logon sid'
  34. else:
  35. sid_desc=win32security.LookupAccountSid('',group_sid)
  36. print('\t',group_sid, sid_desc, group_attr, flag_desc)
  37. ## Vista token information types, will throw (87, 'GetTokenInformation', 'The parameter is incorrect.') on earier OS
  38. try:
  39. is_elevated=win32security.GetTokenInformation(th, win32security.TokenElevation)
  40. print('TokenElevation:', is_elevated)
  41. except pywintypes.error as details:
  42. if details.winerror != winerror.ERROR_INVALID_PARAMETER:
  43. raise
  44. return None
  45. print('TokenHasRestrictions:', win32security.GetTokenInformation(th, win32security.TokenHasRestrictions))
  46. print('TokenMandatoryPolicy', win32security.GetTokenInformation(th, win32security.TokenMandatoryPolicy))
  47. print('TokenVirtualizationAllowed:', win32security.GetTokenInformation(th, win32security.TokenVirtualizationAllowed))
  48. print('TokenVirtualizationEnabled:', win32security.GetTokenInformation(th, win32security.TokenVirtualizationEnabled))
  49. elevation_type = win32security.GetTokenInformation(th, win32security.TokenElevationType)
  50. print('TokenElevationType:', elevation_type, TOKEN_ELEVATION_TYPE.lookup_name(elevation_type))
  51. if elevation_type!=win32security.TokenElevationTypeDefault:
  52. lt=win32security.GetTokenInformation(th, win32security.TokenLinkedToken)
  53. print('TokenLinkedToken:', lt)
  54. else:
  55. lt=None
  56. return lt
  57. ph = win32api.GetCurrentProcess()
  58. th = win32security.OpenProcessToken(ph,win32con.MAXIMUM_ALLOWED)
  59. lt = dump_token(th)
  60. if lt:
  61. print('\n\nlinked token info:')
  62. dump_token(lt)