From 2e8e97cdb267d43a33c54555266198aa2e88eda7 Mon Sep 17 00:00:00 2001 From: adfoster-r7 Date: Wed, 21 Jun 2023 13:00:49 +0100 Subject: [PATCH] Fix python meterpreter getuid crash on windows --- python/meterpreter/README.md | 6 ++ python/meterpreter/ext_server_stdapi.py | 34 ++++--- .../tests/test_ext_server_stdapi.py | 98 ++++++++++++++----- 3 files changed, 100 insertions(+), 38 deletions(-) diff --git a/python/meterpreter/README.md b/python/meterpreter/README.md index c29c9956..2bee202a 100644 --- a/python/meterpreter/README.md +++ b/python/meterpreter/README.md @@ -22,3 +22,9 @@ python3 ./tests/test_ext_server_stdapi.py TestExtServerStdApi.test_stdapi_net_co # Or: python3 -m unittest tests.test_ext_server_stdapi.ExtServerStdApiFileSystemTest.test_stdapi_fs_stat ``` + +To debug tests, add the following code snippet to enter into an interactive debugger at the calling stack frame: + +```python +import pdb; pdb.set_trace() +``` diff --git a/python/meterpreter/ext_server_stdapi.py b/python/meterpreter/ext_server_stdapi.py index e3243851..d7abbcd8 100644 --- a/python/meterpreter/ext_server_stdapi.py +++ b/python/meterpreter/ext_server_stdapi.py @@ -899,7 +899,7 @@ def get_stat_buffer(path): st_buf += struct.pack(' mtu 1500 self.assertEqual(result, expected) - @mock.patch("subprocess.Popen") - def test_stdapi_sys_process_get_processes_via_ps(self, mock_popen): - command_result = b""" - PID PPID USER COMMAND - 1 0 root /sbin/launchd - 88 1 root /usr/sbin/syslogd - 89 1 root /usr/libexec/UserEventAgent (System) -""".lstrip() - - process_mock = mock.Mock() - attrs = { - "communicate.return_value": (command_result, b""), - "wait.return_value": ERROR_SUCCESS, - } - process_mock.configure_mock(**attrs) - mock_popen.return_value = process_mock - - request = bytes() - response = bytes() - result = self.ext_server_stdapi["stdapi_sys_process_get_processes_via_ps"]( - request, response - ) - - self.assertErrorSuccess(result) - class ExtServerStdApiFileSystemTest(ExtServerStdApiTest): def test_stdapi_fs_stat(self): @@ -273,5 +258,68 @@ class ExtServerStdApiFileSystemTest(ExtServerStdApiTest): self.assertMethodErrorSuccess("stdapi_fs_stat", request, response) +class ExtServerStdApiSysProcess(ExtServerStdApiTest): + def test_stdapi_sys_process_get_processes(self): + request = bytes() + response = bytes() + result = self.assertMethodErrorSuccess( + "stdapi_sys_process_get_processes", request, response + ) + + self.assertErrorSuccess(result) + + @mock.patch("subprocess.Popen") + def test_stdapi_sys_process_get_processes_via_ps(self, mock_popen): + command_result = b""" + PID PPID USER COMMAND + 1 0 root /sbin/launchd + 88 1 root /usr/sbin/syslogd + 89 1 root /usr/libexec/UserEventAgent (System) +""".lstrip() + + process_mock = mock.Mock() + attrs = { + "communicate.return_value": (command_result, b""), + "wait.return_value": ERROR_SUCCESS, + } + process_mock.configure_mock(**attrs) + mock_popen.return_value = process_mock + + request = bytes() + response = bytes() + result = self.ext_server_stdapi["stdapi_sys_process_get_processes_via_ps"]( + request, response + ) + + self.assertErrorSuccess(result) + + +class ExtServerStdApiSystemConfigTest(ExtServerStdApiTest): + def test_stdapi_sys_config_getuid(self): + request = bytes() + response = bytes() + _result_code, result_tlvs = self.assertMethodErrorSuccess( + "stdapi_sys_config_getuid", request, response + ) + + user_name = self.meterpreter_context["packet_get_tlv"]( + result_tlvs, self.ext_server_stdapi["TLV_TYPE_USER_NAME"] + ).get("value") + self.assertRegex(user_name, ".+") + + @unittest.skipUnless(is_windows, windows_only_test_reason) + def test_stdapi_sys_config_getsid(self): + request = bytes() + response = bytes() + _result_code, result_tlvs = self.assertMethodErrorSuccess( + "stdapi_sys_config_getsid", request, response + ) + + sid = self.meterpreter_context["packet_get_tlv"]( + result_tlvs, self.ext_server_stdapi["TLV_TYPE_SID"] + ).get("value") + self.assertRegex(sid, "S-1-5-.*") + + if __name__ == "__main__": unittest.main()