Add alpc[Client|Server] __del__ to close handles + NtAlpcDisconnectPort + test

This commit is contained in:
Clement Rouault
2017-09-15 17:54:19 +02:00
parent b9a00b2ed7
commit 7947ace02d
6 changed files with 44 additions and 4 deletions
@@ -1054,6 +1054,11 @@ NTSTATUS WINAPI NtAlpcSendWaitReceivePort(
_In_opt_ PLARGE_INTEGER Timeout
);
NTSTATUS WINAPI NtAlpcDisconnectPort(
_In_ HANDLE PortHandle,
_In_ ULONG Flags
);
NTSTATUS WINAPI NtAlpcCreatePortSection(
_In_ HANDLE PortHandle,
_In_ ULONG Flags,
+4 -1
View File
@@ -174,7 +174,10 @@ def pytest_terminal_summary(terminalreporter, exitstatus):
for type, items in leak_report.LEAK.items():
terminalreporter.write_line("Leaked handles of type <{0}>".format(type) , Purple=True, bold=True)
for item in items:
descr = item.description()
try:
descr = item.description()
except WindowsError as e:
descr = None
if descr is None:
try:
descr = item.name
+26 -1
View File
@@ -1,3 +1,4 @@
import sys
import ctypes
from collections import namedtuple
@@ -337,6 +338,10 @@ class AlpcTransportBase(object):
winproxy.NtAlpcSendWaitReceivePort(self.handle, flags, None, None, receive_msg.port_message, receive_size, receive_msg.attributes, None)
return receive_msg
def _close_port(self, port_handle):
windows.winproxy.NtAlpcDisconnectPort(port_handle, 0)
windows.winproxy.CloseHandle(port_handle)
class AlpcClient(AlpcTransportBase):
@@ -428,6 +433,13 @@ class AlpcClient(AlpcTransportBase):
r = winproxy.NtAlpcCreateSectionView(self.handle, flags, view_attributes)
return view_attributes
def disconnect(self):
self._close_port(self.handle)
def __del__(self):
if sys.path is not None:
self.disconnect()
class AlpcServer(AlpcTransportBase):
"""An ALPC server able to create a port, accept connections and send/receive messages"""
@@ -435,6 +447,7 @@ class AlpcServer(AlpcTransportBase):
def __init__(self, port_name=None):
self.port_name = None
self.communication_port_list = []
if port_name is not None:
self.create_port(port_name)
@@ -511,4 +524,16 @@ class AlpcServer(AlpcTransportBase):
port_attr.DupObjectTypes = 0xffffffff
# windows.utils.print_ctypes_struct(port_attr, " - CONN_PORT_ATTR", hexa=True)
winproxy.NtAlpcAcceptConnectPort(rhandle, self.handle, 0, None, port_attr, port_context, msg.port_message, None, True)
return rhandle.value, msg
self.communication_port_list.append(rhandle.value)
return msg
def disconnect(self):
self._close_port(self.handle)
for com_port_handle in self.communication_port_list:
self._close_port(com_port_handle)
# TODO: add an API to close a communication port ?
def __del__(self):
if sys.path is not None:
self.disconnect()
File diff suppressed because one or more lines are too long
-1
View File
@@ -436,7 +436,6 @@ class Process(AutoHandle):
if size & 0x0fff:
size = ((size >> 12) + 1) << 12
#ssize = ULONG(size)
#import pdb;pdb.set_trace()
old_protect = ctypes.addressof(old_protect)
xaddr = ULONG64(addr)
addr = ctypes.addressof(xaddr)
+3
View File
@@ -869,6 +869,9 @@ def NtAlpcAcceptConnectPort(PortHandle, ConnectionPortHandle, Flags, ObjectAttri
def NtAlpcQueryInformation(PortHandle, PortInformationClass, PortInformation, Length, ReturnLength):
return NtAlpcQueryInformation.ctypes_function(PortHandle, PortInformationClass, PortInformation, Length, ReturnLength)
@NtdllProxy("NtAlpcDisconnectPort", error_ntstatus)
def NtAlpcDisconnectPort(PortHandle, Flags):
return NtAlpcDisconnectPort.ctypes_function(PortHandle, Flags)
@NtdllProxy("NtAlpcSendWaitReceivePort", error_ntstatus)
def NtAlpcSendWaitReceivePort(PortHandle, Flags, SendMessage, SendMessageAttributes, ReceiveMessage, BufferLength, ReceiveMessageAttributes, Timeout):