From d1cadf062a8628c76e29ab43fef4505e58655efb Mon Sep 17 00:00:00 2001 From: Clement Rouault Date: Fri, 4 Aug 2017 17:18:41 +0200 Subject: [PATCH] working on windows.alpc + fix in current_process.threads --- samples/alpc_client_serveur.py | 17 ++++++++++------- windows/alpc.py | 1 + windows/test/mytest.py | 4 ++++ windows/winobject/network.py | 2 +- windows/winobject/process.py | 2 ++ 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/samples/alpc_client_serveur.py b/samples/alpc_client_serveur.py index a06b910..f9627b7 100644 --- a/samples/alpc_client_serveur.py +++ b/samples/alpc_client_serveur.py @@ -8,20 +8,23 @@ PORT_NAME = r"\RPC Control\YOLOPORT" def alpc_server(): server = windows.alpc.AlpcServer(PORT_NAME) # NtAlpcCreatePort print("[SERV] PORT CREATED") - attrs, msg = server.wait_data() # NtAlpcSendWaitReceivePort (send_msg = None) + msg = server.recv() # NtAlpcSendWaitReceivePort (send_msg = None) print("[SERV] Message type = {0:#x}".format(msg.u2.s2.Type)) print("[SERV] Received data: <{0}>".format(msg.data)) - if msg.u2.s2.Type & LPC_CONNECTION_REQUEST: + if msg.type & 0xfff & LPC_CONNECTION_REQUEST: print("[SERV] Connection request") msg.data = "WOKAY" server.accept_connection(msg) # NtAlpcAcceptConnectPort - attrs, msg = server.wait_data() # NtAlpcSendWaitReceivePort (send_msg = None) + msg = server.recv() # NtAlpcSendWaitReceivePort (send_msg = None) print("[SERV] Received message") print("[SERV] Message type = {0:#x}".format(msg.u2.s2.Type)) - if msg.u2.s2.Type & LPC_REQUEST: + if msg.type & 0xfff & LPC_REQUEST: print("[SERV] ALPC request: <{0}>".format(msg.data)) - # Copy MessageId + NtAlpcSendWaitReceivePort - server.reply(msg, "REQUEST '{0}' DONE".format(msg.data)) + # We can reply by to way: + # - Send the same message with modified data + # - Recreate a Message and copy the MessageId + msg.data = "REQUEST '{0}' DONE".format(msg.data) + server.send(msg) def alpc_client(): @@ -29,7 +32,7 @@ def alpc_client(): connect_response = client.connect_to_port(PORT_NAME, "COUCOU") # NtAlpcConnectPort print("[CLIENT] Connected: {0}".format(connect_response.data)) print("[CLIENT] Send Message ") - attr, response = client.send_receive("POUET") # NtAlpcSendWaitReceivePort + response = client.send_receive("POUET") # NtAlpcSendWaitReceivePort print("[CLIENT] Server response: <{0}>".format(response.data)) diff --git a/windows/alpc.py b/windows/alpc.py index 127c8a8..5635dab 100644 --- a/windows/alpc.py +++ b/windows/alpc.py @@ -314,6 +314,7 @@ class AlpcClient(AlpcTransportBase): else: raise ValueError("Don't know how to send <{0!r}> as connect message".format(connect_message)) + windows.utils.print_ctypes_struct(port_attr, "port_attr_connect", hexa=True) receive_attr = MessageAttribute.with_all_attributes() winproxy.NtAlpcConnectPort(handle, port_name_unicode, obj_attr, port_attr, flags, None, send_msg, buffersize, send_msg_attr, receive_attr, timeout) # If send_msg is not None, it contains the ClientId.UniqueProcess : PID of the server :) diff --git a/windows/test/mytest.py b/windows/test/mytest.py index 695a174..1dfebb5 100644 --- a/windows/test/mytest.py +++ b/windows/test/mytest.py @@ -75,6 +75,10 @@ class WindowsTestCase(unittest.TestCase): # Re-set the handle to be able to kill it calc._handle = save_handle + @check_for_gc_garbage + def test_current_process_threads(self): + # Had a bug with WinThread and CurrentProcess.name (which was non-existant) + self.assertTrue([repr(t) for t in windows.current_process.threads]) @check_for_gc_garbage def test_pop_calc_32(self): diff --git a/windows/winobject/network.py b/windows/winobject/network.py index 71dc77f..1a2a15e 100644 --- a/windows/winobject/network.py +++ b/windows/winobject/network.py @@ -394,7 +394,7 @@ class FirewallRule(cominterfaces.INetFwRule): return icmp_type_and_code.value def __repr__(self): - return '<{0} "{1}">'.format(type(self).__name__, self.name) + return u'<{0} "{1}">'.format(type(self).__name__, self.name) class Network(object): NetFwPolicy2 = windows.com.IID.from_string("E2B3C97F-6AE1-41AC-817A-F6F92166D7DD") diff --git a/windows/winobject/process.py b/windows/winobject/process.py index a5886d2..41831f4 100644 --- a/windows/winobject/process.py +++ b/windows/winobject/process.py @@ -761,6 +761,8 @@ class CurrentProcess(Process): allocator = native_exec.native_function.allocator + name = "CurrentProcess" # Used by Winthread for __repr__ + # Use RtlGetCurrentPeb ? def get_peb_builtin(self): if self.get_peb is not None: