|
| 1 | +import pytest |
| 2 | +import threading |
| 3 | +import time |
| 4 | + |
| 5 | +import windows.alpc |
| 6 | +import windows.generated_def as gdef |
| 7 | + |
| 8 | +from pfwtest import * |
| 9 | + |
| 10 | + |
| 11 | +def generate_client_server_test(client_function, server_function): |
| 12 | + def generated_test(): |
| 13 | + th = threading.Thread(target=server_function, args=()) |
| 14 | + th.start() |
| 15 | + time.sleep(0.5) |
| 16 | + client_function() |
| 17 | + th.join() |
| 18 | + return True |
| 19 | + return generated_test |
| 20 | + |
| 21 | +PORT_NAME = r"\RPC Control\PythonForWindowsTestPort" |
| 22 | +CLIENT_MESSAGE = "Message 1\x00\xffABCD" |
| 23 | +SERVER_MESSAGE = "Message 2-" + "".join(chr(i) for i in range(256)) |
| 24 | + |
| 25 | +def alpc_simple_test_server(): |
| 26 | + server = windows.alpc.AlpcServer(PORT_NAME) |
| 27 | + msg = server.recv() |
| 28 | + assert msg.type & 0xfff == gdef.LPC_CONNECTION_REQUEST |
| 29 | + server.accept_connection(msg) |
| 30 | + msg = server.recv() |
| 31 | + assert msg.type & 0xfff == gdef.LPC_REQUEST |
| 32 | + assert msg.data == CLIENT_MESSAGE |
| 33 | + msg.data = SERVER_MESSAGE |
| 34 | + server.send(msg) |
| 35 | + |
| 36 | +def alpc_simple_test_client(): |
| 37 | + client = windows.alpc.AlpcClient(PORT_NAME) |
| 38 | + response = client.send_receive(CLIENT_MESSAGE) |
| 39 | + assert response.data == SERVER_MESSAGE |
| 40 | + |
| 41 | +test_simple_alpc = generate_client_server_test(alpc_simple_test_client, alpc_simple_test_server) |
| 42 | + |
| 43 | + |
| 44 | +def send_message_with_view(client, message_data, view_data): |
| 45 | + # Create View |
| 46 | + section = client.create_port_section(0, 0, 0x4000) |
| 47 | + view = client.map_section(section[0], 0x4000) |
| 48 | + |
| 49 | + # New message with a View |
| 50 | + msg = windows.alpc.AlpcMessage(0x2000) |
| 51 | + msg.attributes.ValidAttributes |= gdef.ALPC_MESSAGE_VIEW_ATTRIBUTE |
| 52 | + msg.view_attribute.Flags = 0 |
| 53 | + msg.view_attribute.ViewBase = view.ViewBase |
| 54 | + msg.view_attribute.SectionHandle = view.SectionHandle |
| 55 | + msg.view_attribute.ViewSize = 0x4000 |
| 56 | + msg.data = message_data |
| 57 | + windows.current_process.write_memory(view.ViewBase, view_data) |
| 58 | + return client.send_receive(msg) |
| 59 | + |
| 60 | + |
| 61 | +CLIENT_VIEW_MESSAGE = "Message 1\x00\xffABCD" |
| 62 | +CLIENT_VIEW_DATA = "Message Data-view" + "".join(chr(i) for i in range(256)) |
| 63 | + |
| 64 | + |
| 65 | +def alpc_view_test_server(): |
| 66 | + server = windows.alpc.AlpcServer(PORT_NAME) |
| 67 | + msg = server.recv() |
| 68 | + assert msg.type & 0xfff == gdef.LPC_CONNECTION_REQUEST |
| 69 | + server.accept_connection(msg) |
| 70 | + msg = server.recv() |
| 71 | + assert msg.type & 0xfff == gdef.LPC_REQUEST |
| 72 | + assert msg.view_is_valid |
| 73 | + view_data = windows.current_process.read_memory(msg.view_attribute.ViewBase, len(CLIENT_VIEW_DATA)) |
| 74 | + assert view_data == CLIENT_VIEW_DATA |
| 75 | + assert msg.data == CLIENT_VIEW_MESSAGE |
| 76 | + msg.data = SERVER_MESSAGE |
| 77 | + server.send(msg) |
| 78 | + |
| 79 | +def alpc_view_test_client(): |
| 80 | + client = windows.alpc.AlpcClient(PORT_NAME) |
| 81 | + response = send_message_with_view(client, CLIENT_VIEW_MESSAGE, CLIENT_VIEW_DATA) |
| 82 | + assert response.data == SERVER_MESSAGE |
| 83 | + |
| 84 | +test_view_alpc = generate_client_server_test(alpc_view_test_client, alpc_view_test_server) |
0 commit comments