From ffec0aa57efd374e705c6632f832a68a44620cd4 Mon Sep 17 00:00:00 2001 From: hakril Date: Tue, 12 May 2020 00:20:16 +0200 Subject: [PATCH] Renamed NewService class as Service + updated doc/sample for new service API --- .../_SERVICE_STATUS_PROCESS.py | 8 +++- .../samples_output/service_service_demo.txt | 19 +++++++++ docs/source/samples_output/system.txt | 40 +++++++++---------- docs/source/service.rst | 16 +++++++- windows/winobject/service.py | 22 ++++++++++ windows/winobject/system.py | 5 ++- 6 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 docs/source/samples_output/service_service_demo.txt diff --git a/ctypes_generation/extended_structs/_SERVICE_STATUS_PROCESS.py b/ctypes_generation/extended_structs/_SERVICE_STATUS_PROCESS.py index 9f92972..f46ce94 100644 --- a/ctypes_generation/extended_structs/_SERVICE_STATUS_PROCESS.py +++ b/ctypes_generation/extended_structs/_SERVICE_STATUS_PROCESS.py @@ -39,4 +39,10 @@ class _SERVICE_STATUS_PROCESS(_SERVICE_STATUS_PROCESS): state = dwCurrentState type = dwServiceType control_accepted = dwControlsAccepted - flags = dwServiceFlags \ No newline at end of file + flags = dwServiceFlags + + + def __repr__(self): + return """<{0} type={1!r} state={2!r}>""".format(type(self).__name__, + self.type, + self.state) \ No newline at end of file diff --git a/docs/source/samples_output/service_service_demo.txt b/docs/source/samples_output/service_service_demo.txt new file mode 100644 index 0000000..fb44e60 --- /dev/null +++ b/docs/source/samples_output/service_service_demo.txt @@ -0,0 +1,19 @@ +(cmd) python service\service_demo.py +Listing the first 3 services: + * + * + * + +Retriving service + + - name: 'TapiSrv' + - description: 'Telephony' + - state: SERVICE_STOPPED(0x1) + - type: 48L + - process: None + - security-description: O:SYG:SYD:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWRPLOCRRC;;;IU)(A;;CCLCSWRPLOCRRC;;;SU) +Trying to start the service +Service started ! + + - state: SERVICE_RUNNING(0x4) + - process: diff --git a/docs/source/samples_output/system.txt b/docs/source/samples_output/system.txt index c99ff59..af1c920 100644 --- a/docs/source/samples_output/system.txt +++ b/docs/source/samples_output/system.txt @@ -3,45 +3,45 @@ Basic system infos: version = (10, 0) bitness = 64 computer_name = WILLIE - product_type = VER_NT_WORKSTATION(0x1L) + product_type = 1 version_name = Windows 10 -There is 194 processes -There is 2324 threads +There is 331 processes +There is 4010 threads Dumping first logical drive: - - name = C:\ - type = DRIVE_FIXED(0x3L) - path = \Device\HarddiskVolume2 + + name = B:\ + type = 3 + path = \Device\HarddiskVolume7 Dumping first service: - + name = 1394ohci description = 1394 OHCI Compliant Host Controller - status = ServiceStatus(type=SERVICE_KERNEL_DRIVER(0x1L), state=SERVICE_STOPPED(0x1L), control_accepted=0L, flags=0L) + status = <_SERVICE_STATUS_PROCESS type=SERVICE_KERNEL_DRIVER(0x1) state=SERVICE_STOPPED(0x1)> process = None Finding a service in a user process: - + name = AppIDSvc description = Application Identity - status = ServiceStatus(type=48L, state=SERVICE_RUNNING(0x4L), control_accepted=5L, flags=0L) - process = + status = <_SERVICE_STATUS_PROCESS type=48L state=SERVICE_RUNNING(0x4)> + process = Enumerating handles: - There are 78079 handles: - First handle is: in process pid=4> + There are 208332 handles: + First handle is: in process pid=4> Enumerating handles of the current process: - There are 260 handles for this process + There are 275 handles for this process Looking for a File handle: - Handle is in process pid=10968> + Handle is in process pid=15236> Name is <\Device\ConDrv> Dumping the first system module - + ImageName = \SystemRoot\system32\ntoskrnl.exe - Base = 0xfffff803f9697000 - Size = 0x8d5000 + Base = 0xfffff80023000000 + Size = 0xab7000 Flags = 0x8804000 - LoadCount = 139 + LoadCount = 240 diff --git a/docs/source/service.rst b/docs/source/service.rst index 169d810..44a5913 100644 --- a/docs/source/service.rst +++ b/docs/source/service.rst @@ -1,7 +1,7 @@ Service ======= -The list of services is accessible via :py:attr:`windows.system.services +The services manager is accessible via :py:attr:`windows.system.services ` .. note:: @@ -10,6 +10,18 @@ The list of services is accessible via :py:attr:`windows.system.services .. module:: windows.winobject.service -.. autoclass:: Service + +ServiceManager +"""""""""""""" + +.. autoclass:: ServiceManager :show-inheritance: + :inherited-members: + :special-members: __getitem__, __iter__ + + +Service +""""""" + +.. autoclass:: Service :inherited-members: \ No newline at end of file diff --git a/windows/winobject/service.py b/windows/winobject/service.py index ee05706..737866d 100644 --- a/windows/winobject/service.py +++ b/windows/winobject/service.py @@ -39,6 +39,7 @@ from windows.pycompat import basestring class ServiceManager(utils.AutoHandle): + """An object to query, list and explore services""" def _get_handle(self): return windows.winproxy.OpenSCManagerA(dwDesiredAccess=gdef.MAXIMUM_ALLOWED) @@ -46,6 +47,10 @@ class ServiceManager(utils.AutoHandle): return windows.winproxy.OpenServiceA(self.handle, name, access) # Check service exists :) def get_service(self, key, access=gdef.MAXIMUM_ALLOWED): + """Get a service by its name/index or a list of services via a slice + + :return: :class:`Service` or [:class:`Service`] -- A :class:`Service` or list of :class:`Service` + """ if isinstance(key, int_types): return self.enumerate_services()[key] if isinstance(key, slice): @@ -59,6 +64,10 @@ class ServiceManager(utils.AutoHandle): return Service(name=key, handle=handle) __getitem__ = get_service + """Get a service by its name/index or a list of services via a slice + + :return: :class:`Service` or [:class:`Service`] -- A :class:`Service` or list of :class:`Service` + """ def get_service_display_name(self, name): # This API is strange.. @@ -70,6 +79,9 @@ class ServiceManager(utils.AutoHandle): return result.value def _enumerate_services_generator(self): + """The generator code behind __iter__. + Allow to iter over the services on the system + """ size_needed = gdef.DWORD() nb_services = gdef.DWORD() counter = gdef.DWORD() @@ -93,12 +105,17 @@ class ServiceManager(utils.AutoHandle): return __iter__ = _enumerate_services_generator + """Iter over the services on the system + + :yield: :class:`Service` + """ def enumerate_services(self): return list(self._enumerate_services_generator()) class Service(gdef.SC_HANDLE): + """Represent a service on the system""" def __init__(self, handle, name, description=None): super(Service, self).__init__(handle) self.name = name @@ -149,6 +166,10 @@ class Service(gdef.SC_HANDLE): return security.SecurityDescriptor.from_service(self.name) def start(self, args=None): + """Start the service + + :param args: a list of :class:`str` + """ nbelt = 0 if args is not None: if isinstance(args, windows.pycompat.anybuff): @@ -158,6 +179,7 @@ class Service(gdef.SC_HANDLE): return windows.winproxy.StartServiceA(self, nbelt, args) def stop(self): + """Stop the service""" status = SERVICE_STATUS() windows.winproxy.ControlService(self, gdef.SERVICE_CONTROL_STOP, status) return status diff --git a/windows/winobject/system.py b/windows/winobject/system.py index 2019844..51fc82a 100644 --- a/windows/winobject/system.py +++ b/windows/winobject/system.py @@ -64,9 +64,10 @@ class System(object): @utils.fixedpropety def services(self): - """The list of services + """An object to query, list and explore services - :type: [:class:`~windows.winobject.service.ServiceA`] -- A list of Service""" + :type: :class:`~windows.winobject.service.ServiceManager` + """ return service.ServiceManager() @property