EditServiceSecurity.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """
  2. Implements a permissions editor for services.
  3. Service can be specified as plain name for local machine,
  4. or as a remote service of the form \\machinename\service
  5. """
  6. import os
  7. import win32com.server.policy
  8. import win32security, ntsecuritycon, win32con
  9. import pythoncom, win32api, win32service
  10. from win32com.authorization import authorization
  11. SERVICE_GENERIC_EXECUTE=win32service.SERVICE_START|win32service.SERVICE_STOP|win32service.SERVICE_PAUSE_CONTINUE|win32service.SERVICE_USER_DEFINED_CONTROL
  12. SERVICE_GENERIC_READ=win32service.SERVICE_QUERY_CONFIG|win32service.SERVICE_QUERY_STATUS|win32service.SERVICE_INTERROGATE|win32service.SERVICE_ENUMERATE_DEPENDENTS
  13. SERVICE_GENERIC_WRITE=win32service.SERVICE_CHANGE_CONFIG
  14. from ntsecuritycon import STANDARD_RIGHTS_READ, STANDARD_RIGHTS_WRITE, STANDARD_RIGHTS_EXECUTE, \
  15. WRITE_OWNER, WRITE_DAC, READ_CONTROL, \
  16. SI_ADVANCED, SI_EDIT_AUDITS, SI_EDIT_PROPERTIES, SI_EDIT_ALL, SI_PAGE_TITLE, SI_RESET, \
  17. SI_ACCESS_SPECIFIC, SI_ACCESS_GENERAL, SI_ACCESS_CONTAINER, SI_ACCESS_PROPERTY, \
  18. OBJECT_INHERIT_ACE, CONTAINER_INHERIT_ACE, INHERIT_ONLY_ACE, \
  19. SI_PAGE_PERM, SI_PAGE_ADVPERM, SI_PAGE_AUDIT, SI_PAGE_OWNER, PSPCB_SI_INITDIALOG, \
  20. SI_CONTAINER
  21. from win32security import OBJECT_INHERIT_ACE, CONTAINER_INHERIT_ACE, INHERIT_ONLY_ACE
  22. from win32com.shell.shellcon import PSPCB_RELEASE, PSPCB_CREATE ## Msg parameter to PropertySheetPageCallback
  23. from pythoncom import IID_NULL
  24. class ServiceSecurity(win32com.server.policy.DesignatedWrapPolicy):
  25. _com_interfaces_=[authorization.IID_ISecurityInformation]
  26. _public_methods_=['GetObjectInformation','GetSecurity','SetSecurity','GetAccessRights',
  27. 'GetInheritTypes','MapGeneric','PropertySheetPageCallback']
  28. def __init__(self, ServiceName):
  29. self.ServiceName=ServiceName
  30. self._wrap_(self)
  31. def GetObjectInformation(self):
  32. """Identifies object whose security will be modified, and determines options available
  33. to the end user"""
  34. flags=SI_ADVANCED|SI_EDIT_ALL|SI_PAGE_TITLE|SI_RESET
  35. hinstance=0 ## handle to module containing string resources
  36. servername='' ## name of authenticating server if not local machine
  37. ## service name can contain remote machine name of the form \\Server\ServiceName
  38. objectname=os.path.split(self.ServiceName)[1]
  39. pagetitle='Service Permissions for '+self.ServiceName
  40. objecttype=IID_NULL
  41. return flags, hinstance, servername, objectname, pagetitle, objecttype
  42. def GetSecurity(self, requestedinfo, bdefault):
  43. """Requests the existing permissions for object"""
  44. if bdefault:
  45. return win32security.SECURITY_DESCRIPTOR()
  46. else:
  47. return win32security.GetNamedSecurityInfo(self.ServiceName, win32security.SE_SERVICE, requestedinfo)
  48. def SetSecurity(self, requestedinfo, sd):
  49. """Applies permissions to the object"""
  50. owner=sd.GetSecurityDescriptorOwner()
  51. group=sd.GetSecurityDescriptorGroup()
  52. dacl=sd.GetSecurityDescriptorDacl()
  53. sacl=sd.GetSecurityDescriptorSacl()
  54. win32security.SetNamedSecurityInfo(self.ServiceName, win32security.SE_SERVICE, requestedinfo,
  55. owner, group, dacl, sacl)
  56. def GetAccessRights(self, objecttype, flags):
  57. """Returns a tuple of (AccessRights, DefaultAccess), where AccessRights is a sequence of tuples representing
  58. SI_ACCESS structs, containing (guid, access mask, Name, flags). DefaultAccess indicates which of the
  59. AccessRights will be used initially when a new ACE is added (zero based).
  60. Flags can contain SI_ACCESS_SPECIFIC,SI_ACCESS_GENERAL,SI_ACCESS_CONTAINER,SI_ACCESS_PROPERTY,
  61. CONTAINER_INHERIT_ACE,INHERIT_ONLY_ACE,OBJECT_INHERIT_ACE
  62. """
  63. ## input flags: SI_ADVANCED,SI_EDIT_AUDITS,SI_EDIT_PROPERTIES indicating which property sheet is requesting the rights
  64. if (objecttype is not None) and (objecttype!=IID_NULL):
  65. ## Not relevent for services
  66. raise NotImplementedError("Object type is not supported")
  67. ## ???? for some reason, the DACL for a service will not retain ACCESS_SYSTEM_SECURITY in an ACE ????
  68. ## (IID_NULL, win32con.ACCESS_SYSTEM_SECURITY, 'View/change audit settings', SI_ACCESS_SPECIFIC),
  69. accessrights=[
  70. (IID_NULL, win32service.SERVICE_ALL_ACCESS, 'Full control', SI_ACCESS_GENERAL),
  71. (IID_NULL, SERVICE_GENERIC_READ, 'Generic read', SI_ACCESS_GENERAL),
  72. (IID_NULL, SERVICE_GENERIC_WRITE, 'Generic write', SI_ACCESS_GENERAL),
  73. (IID_NULL, SERVICE_GENERIC_EXECUTE, 'Start/Stop/Pause service', SI_ACCESS_GENERAL),
  74. (IID_NULL, READ_CONTROL, 'Read Permissions', SI_ACCESS_GENERAL),
  75. (IID_NULL, WRITE_DAC, 'Change permissions', SI_ACCESS_GENERAL),
  76. (IID_NULL, WRITE_OWNER, 'Change owner', SI_ACCESS_GENERAL),
  77. (IID_NULL, win32con.DELETE, 'Delete service', SI_ACCESS_GENERAL),
  78. (IID_NULL, win32service.SERVICE_START, 'Start service', SI_ACCESS_SPECIFIC),
  79. (IID_NULL, win32service.SERVICE_STOP, 'Stop service', SI_ACCESS_SPECIFIC),
  80. (IID_NULL, win32service.SERVICE_PAUSE_CONTINUE, 'Pause/unpause service', SI_ACCESS_SPECIFIC),
  81. (IID_NULL, win32service.SERVICE_USER_DEFINED_CONTROL, 'Execute user defined operations', SI_ACCESS_SPECIFIC),
  82. (IID_NULL, win32service.SERVICE_QUERY_CONFIG, 'Read configuration', SI_ACCESS_SPECIFIC),
  83. (IID_NULL, win32service.SERVICE_CHANGE_CONFIG, 'Change configuration', SI_ACCESS_SPECIFIC),
  84. (IID_NULL, win32service.SERVICE_ENUMERATE_DEPENDENTS, 'List dependent services', SI_ACCESS_SPECIFIC),
  85. (IID_NULL, win32service.SERVICE_QUERY_STATUS, 'Query status', SI_ACCESS_SPECIFIC),
  86. (IID_NULL, win32service.SERVICE_INTERROGATE, 'Query status (immediate)', SI_ACCESS_SPECIFIC),
  87. ]
  88. return (accessrights, 0)
  89. def MapGeneric(self, guid, aceflags, mask):
  90. """ Converts generic access rights to specific rights.
  91. """
  92. return win32security.MapGenericMask(mask,
  93. (SERVICE_GENERIC_READ, SERVICE_GENERIC_WRITE, SERVICE_GENERIC_EXECUTE, win32service.SERVICE_ALL_ACCESS))
  94. def GetInheritTypes(self):
  95. """Specifies which types of ACE inheritance are supported.
  96. Services don't use any inheritance
  97. """
  98. return ((IID_NULL, 0, 'Only current object'),)
  99. def PropertySheetPageCallback(self, hwnd, msg, pagetype):
  100. """Invoked each time a property sheet page is created or destroyed."""
  101. ## page types from SI_PAGE_TYPE enum: SI_PAGE_PERM SI_PAGE_ADVPERM SI_PAGE_AUDIT SI_PAGE_OWNER
  102. ## msg: PSPCB_CREATE, PSPCB_RELEASE, PSPCB_SI_INITDIALOG
  103. return None
  104. def EditSecurity(self, owner_hwnd=0):
  105. """Creates an ACL editor dialog based on parameters returned by interface methods"""
  106. isi=pythoncom.WrapObject(self, authorization.IID_ISecurityInformation, pythoncom.IID_IUnknown)
  107. authorization.EditSecurity(owner_hwnd, isi)
  108. if __name__=='__main__':
  109. # Find the first service on local machine and edit its permissions
  110. scm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ENUMERATE_SERVICE)
  111. svcs=win32service.EnumServicesStatus(scm)
  112. win32service.CloseServiceHandle(scm)
  113. si=ServiceSecurity(svcs[0][0])
  114. si.EditSecurity()