FileSecurityTest.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Contributed by Kelly Kranabetter.
  2. import os, sys
  3. import win32security, ntsecuritycon, pywintypes, winerror
  4. # get security information
  5. #name=r"c:\autoexec.bat"
  6. #name= r"g:\!workgrp\lim"
  7. name=sys.argv[0]
  8. if not os.path.exists(name):
  9. print(name, "does not exist!")
  10. sys.exit()
  11. print("On file " , name, "\n")
  12. # get owner SID
  13. print("OWNER")
  14. try:
  15. sd= win32security.GetFileSecurity(name, win32security.OWNER_SECURITY_INFORMATION)
  16. sid= sd.GetSecurityDescriptorOwner()
  17. print(" ", win32security.LookupAccountSid(None, sid))
  18. except pywintypes.error as exc:
  19. # in automation and network shares we see:
  20. # pywintypes.error: (1332, 'LookupAccountName', 'No mapping between account names and security IDs was done.')
  21. if exc.winerror != winerror.ERROR_NONE_MAPPED:
  22. raise
  23. print("No owner information is available")
  24. # get group SID
  25. try:
  26. print("GROUP")
  27. sd= win32security.GetFileSecurity(name, win32security.GROUP_SECURITY_INFORMATION)
  28. sid= sd.GetSecurityDescriptorGroup()
  29. print(" ", win32security.LookupAccountSid(None, sid))
  30. except pywintypes.error as exc:
  31. if exc.winerror != winerror.ERROR_NONE_MAPPED:
  32. raise
  33. print("No group information is available")
  34. # get ACEs
  35. sd= win32security.GetFileSecurity(name, win32security.DACL_SECURITY_INFORMATION)
  36. dacl= sd.GetSecurityDescriptorDacl()
  37. if dacl == None:
  38. print("No Discretionary ACL")
  39. else:
  40. for ace_no in range(0, dacl.GetAceCount()):
  41. ace= dacl.GetAce(ace_no)
  42. print("ACE", ace_no)
  43. print(" -Type")
  44. for i in ("ACCESS_ALLOWED_ACE_TYPE", "ACCESS_DENIED_ACE_TYPE", "SYSTEM_AUDIT_ACE_TYPE", "SYSTEM_ALARM_ACE_TYPE"):
  45. if getattr(ntsecuritycon, i) == ace[0][0]:
  46. print(" ", i)
  47. print(" -Flags", hex(ace[0][1]))
  48. for i in ("OBJECT_INHERIT_ACE", "CONTAINER_INHERIT_ACE", "NO_PROPAGATE_INHERIT_ACE", "INHERIT_ONLY_ACE", "SUCCESSFUL_ACCESS_ACE_FLAG", "FAILED_ACCESS_ACE_FLAG"):
  49. if getattr(ntsecuritycon, i) & ace[0][1] == getattr(ntsecuritycon, i):
  50. print(" ", i)
  51. print(" -mask", hex(ace[1]))
  52. # files and directories do permissions differently
  53. permissions_file= ("DELETE", "READ_CONTROL", "WRITE_DAC", "WRITE_OWNER", "SYNCHRONIZE", "FILE_GENERIC_READ", "FILE_GENERIC_WRITE", "FILE_GENERIC_EXECUTE", "FILE_DELETE_CHILD")
  54. permissions_dir= ("DELETE", "READ_CONTROL", "WRITE_DAC", "WRITE_OWNER", "SYNCHRONIZE", "FILE_ADD_SUBDIRECTORY", "FILE_ADD_FILE", "FILE_DELETE_CHILD", "FILE_LIST_DIRECTORY", "FILE_TRAVERSE", "FILE_READ_ATTRIBUTES", "FILE_WRITE_ATTRIBUTES", "FILE_READ_EA", "FILE_WRITE_EA")
  55. permissions_dir_inherit= ("DELETE", "READ_CONTROL", "WRITE_DAC", "WRITE_OWNER", "SYNCHRONIZE", "GENERIC_READ", "GENERIC_WRITE", "GENERIC_EXECUTE", "GENERIC_ALL")
  56. if os.path.isfile(name):
  57. permissions= permissions_file
  58. else:
  59. permissions= permissions_dir
  60. # directories also contain an ACE that is inherited by children (files) within them
  61. if ace[0][1] & ntsecuritycon.OBJECT_INHERIT_ACE == ntsecuritycon.OBJECT_INHERIT_ACE and ace[0][1] & ntsecuritycon.INHERIT_ONLY_ACE == ntsecuritycon.INHERIT_ONLY_ACE:
  62. permissions= permissions_dir_inherit
  63. calc_mask= 0 # calculate the mask so we can see if we are printing all of the permissions
  64. for i in permissions:
  65. if getattr(ntsecuritycon, i) & ace[1] == getattr(ntsecuritycon, i):
  66. calc_mask= calc_mask | getattr(ntsecuritycon, i)
  67. print(" ", i)
  68. print(" ", "Calculated Check Mask=", hex(calc_mask))
  69. print(" -SID\n ", win32security.LookupAccountSid(None, ace[2]))