Advertisement
AceScottie

perms.py

Aug 29th, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. import ntsecuritycon as con
  2. import win32security
  3. import os
  4. class perms:
  5.     def __init__(self, file, users):
  6.         self.file = file
  7.         self.users = users
  8.         self.grants = []
  9.     def grant(self):
  10.         entries = [{'AccessMode': win32security.GRANT_ACCESS,
  11.                 'AccessPermissions': 0,
  12.                 'Inheritance': win32security.CONTAINER_INHERIT_ACE |
  13.                                win32security.OBJECT_INHERIT_ACE,
  14.                 'Trustee': {'TrusteeType': win32security.TRUSTEE_IS_USER,
  15.                             'TrusteeForm': win32security.TRUSTEE_IS_NAME,
  16.                             'Identifier': ''}
  17.                             }
  18.                 for i in range(len(self.users))
  19.                 ]
  20.         for i in range(len(self.users)):
  21.             entries[i]['AccessPermissions'] = (con.GENERIC_ALL | con.GENERIC_WRITE)
  22.             entries[i]['Trustee']['Identifier'] = self.users[i]
  23.         self.run(entries)
  24.     def get_access_mask_str(self, mask):
  25.         ACCESS_MASKS = ['FILE_READ_DATA', 'FILE_LIST_DIRECTORY', 'FILE_WRITE_DATA', 'FILE_ADD_FILE',
  26.                         'FILE_APPEND_DATA', 'FILE_ADD_SUBDIRECTORY', 'FILE_CREATE_PIPE_INSTANCE', 'FILE_READ_EA',
  27.                         'FILE_WRITE_EA', 'FILE_EXECUTE', 'FILE_TRAVERSE', 'FILE_DELETE_CHILD',
  28.                         'FILE_READ_ATTRIBUTES', 'FILE_WRITE_ATTRIBUTES', 'FILE_ALL_ACCESS', 'FILE_GENERIC_READ',
  29.                         'FILE_GENERIC_WRITE', 'FILE_GENERIC_EXECUTE']
  30.         for t in ACCESS_MASKS:
  31.             attr = getattr(con, t)
  32.             if (attr & mask) == attr:
  33.                 yield t
  34.     def check_perm(self):
  35.         dacl = win32security.GetNamedSecurityInfo(self.file, win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION).GetSecurityDescriptorDacl()
  36.         CONVENTIONAL_ACES = {
  37.             win32security.ACCESS_ALLOWED_ACE_TYPE : "ALLOW",
  38.             win32security.ACCESS_DENIED_ACE_TYPE : "DENY"
  39.         }
  40.         for n_ace in range(dacl.GetAceCount()):
  41.             ace = dacl.GetAce(n_ace)
  42.             (ace_type, ace_flags) = ace[0]
  43.             if ace_type in CONVENTIONAL_ACES:
  44.                 mask, sid = ace[1:]
  45.             else:
  46.                 mask, object_type, inherited_object_type, sid = ace[1:]
  47.             name, domain, type = win32security.LookupAccountSid(None, sid)
  48.             if name in self.users:
  49.                 perms = (','.join(self.get_access_mask_str(mask)).split(","))
  50.                 if not "FILE_GENERIC_WRITE" in perms:
  51.                     print("%s does not have write access. Enableding Write access for user" %name)
  52.                     self.grants.append(name)
  53.                 else:
  54.                     print("%s has write permissions" %name)
  55.                     self.users.remove(name)
  56.         for i in self.users:
  57.             if not i in self.grants:
  58.                 self.grants.append(i)
  59.         print(self.grants)
  60.         if len(self.grants) > 0:
  61.             self.users = self.grants
  62.             self.grant()
  63.     def deny(self):
  64.         if not os.path.exists(self.file):
  65.             raise WindowsError('Path %s could not be found.' % self.file)
  66.         total = 0
  67.         for x in self.users:
  68.             userx, domain, utype = win32security.LookupAccountName("", x)
  69.             sd = win32security.GetFileSecurity(self.file, win32security.DACL_SECURITY_INFORMATION)
  70.             dacl = sd.GetSecurityDescriptorDacl()
  71.             num_delete = 0
  72.             for index in range(0, dacl.GetAceCount()):
  73.                 ace = dacl.GetAce(index - num_delete)
  74.                 if userx == ace[2]:
  75.                     dacl.DeleteAce(index - num_delete)
  76.                     num_delete += 1
  77.                     total += 1
  78.             if num_delete > 0:
  79.                 sd.SetSecurityDescriptorDacl(1, dacl, 0)
  80.                 win32security.SetFileSecurity(self.file, win32security.DACL_SECURITY_INFORMATION, sd)
  81.         if total > 0:
  82.             return True
  83.     def run(self, entries):
  84.         sd = win32security.GetNamedSecurityInfo(self.file, win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION)
  85.         dacl = sd.GetSecurityDescriptorDacl()
  86.         dacl.SetEntriesInAcl(entries)
  87.         win32security.SetNamedSecurityInfo(self.file, win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION | win32security.UNPROTECTED_DACL_SECURITY_INFORMATION, None, None, dacl, None)
  88.  
  89. if __name__ == "__main__":
  90.     file = "C:\\Program Files (x86)\\MyApp"
  91.     users = ["Users", "Everyone"]
  92.     perms(file, users).check_perm()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement