From 5a29c59b3e6cfe6c1b7efb971987606ac7888741 Mon Sep 17 00:00:00 2001 From: Clement Rouault Date: Tue, 22 Dec 2015 16:17:30 +0100 Subject: [PATCH] execute_python is now safe + test --- generated_def/winfuncs.py | 10 +++++- generated_def/winstructs.py | 1 - injection.py | 43 +++++++++++++++++++++-- test/__init__.py | 3 +- test/mytest.py | 23 +++++++++--- utils/pythonutils.py | 2 +- winobject.py | 70 ++++++++++++++++++++++++++++++++++--- 7 files changed, 136 insertions(+), 16 deletions(-) diff --git a/generated_def/winfuncs.py b/generated_def/winfuncs.py index 9f21695..a5ffb64 100644 --- a/generated_def/winfuncs.py +++ b/generated_def/winfuncs.py @@ -3,7 +3,7 @@ from ctypes import * from ctypes.wintypes import * from .winstructs import * -functions = ['ExitProcess', 'TerminateProcess', 'GetLastError', 'GetCurrentProcess', 'CreateFileA', 'CreateFileW', 'NtQuerySystemInformation', 'NtQueryInformationProcess', 'NtQueryVirtualMemory', 'NtCreateThreadEx', 'NtQueryInformationThread', 'VirtualAlloc', 'VirtualAllocEx', 'VirtualFree', 'VirtualFreeEx', 'VirtualProtect', 'VirtualQuery', 'VirtualQueryEx', 'GetModuleFileNameA', 'GetModuleFileNameW', 'CreateThread', 'CreateRemoteThread', 'VirtualProtect', 'CreateProcessA', 'CreateProcessW', 'GetThreadContext', 'NtGetContextThread', 'SetThreadContext', 'OpenThread', 'OpenProcess', 'CloseHandle', 'ReadProcessMemory', 'NtWow64ReadVirtualMemory64', 'WriteProcessMemory', 'CreateToolhelp32Snapshot', 'Thread32First', 'Thread32Next', 'Process32First', 'Process32Next', 'Process32FirstW', 'Process32NextW', 'GetProcAddress', 'LoadLibraryA', 'LoadLibraryW', 'OpenProcessToken', 'LookupPrivilegeValueA', 'LookupPrivilegeValueW', 'AdjustTokenPrivileges', 'FindResourceA', 'FindResourceW', 'SizeofResource', 'LoadResource', 'LockResource', 'GetVersionExA', 'GetVersionExW', 'GetVersion', 'GetCurrentThread', 'GetCurrentThreadId', 'GetCurrentProcessorNumber', 'AllocConsole', 'FreeConsole', 'GetStdHandle', 'SetStdHandle', 'SetThreadAffinityMask', 'WriteFile', 'GetExtendedTcpTable', 'GetExtendedUdpTable', 'SetTcpEntry', 'AddVectoredContinueHandler', 'AddVectoredExceptionHandler', 'TerminateThread', 'ExitThread', 'RemoveVectoredExceptionHandler', 'ResumeThread', 'SuspendThread', 'WaitForSingleObject', 'GetThreadId', 'LoadLibraryExA', 'LoadLibraryExW', 'SymInitialize', 'SymFromName', 'SymLoadModuleEx', 'SymSetOptions', 'SymGetTypeInfo', 'DeviceIoControl', 'GetTokenInformation', 'RegOpenKeyExA', 'RegOpenKeyExW', 'RegGetValueA', 'RegGetValueW', 'RegCloseKey', 'Wow64DisableWow64FsRedirection', 'Wow64RevertWow64FsRedirection', 'Wow64EnableWow64FsRedirection', 'Wow64GetThreadContext', 'SetConsoleCtrlHandler'] +functions = ['ExitProcess', 'TerminateProcess', 'GetLastError', 'GetCurrentProcess', 'CreateFileA', 'CreateFileW', 'NtQuerySystemInformation', 'NtQueryInformationProcess', 'NtQueryVirtualMemory', 'NtCreateThreadEx', 'NtQueryInformationThread', 'GetExitCodeThread', 'GetExitCodeProcess', 'VirtualAlloc', 'VirtualAllocEx', 'VirtualFree', 'VirtualFreeEx', 'VirtualProtect', 'VirtualQuery', 'VirtualQueryEx', 'GetModuleFileNameA', 'GetModuleFileNameW', 'CreateThread', 'CreateRemoteThread', 'VirtualProtect', 'CreateProcessA', 'CreateProcessW', 'GetThreadContext', 'NtGetContextThread', 'SetThreadContext', 'OpenThread', 'OpenProcess', 'CloseHandle', 'ReadProcessMemory', 'NtWow64ReadVirtualMemory64', 'WriteProcessMemory', 'CreateToolhelp32Snapshot', 'Thread32First', 'Thread32Next', 'Process32First', 'Process32Next', 'Process32FirstW', 'Process32NextW', 'GetProcAddress', 'LoadLibraryA', 'LoadLibraryW', 'OpenProcessToken', 'LookupPrivilegeValueA', 'LookupPrivilegeValueW', 'AdjustTokenPrivileges', 'FindResourceA', 'FindResourceW', 'SizeofResource', 'LoadResource', 'LockResource', 'GetVersionExA', 'GetVersionExW', 'GetVersion', 'GetCurrentThread', 'GetCurrentThreadId', 'GetCurrentProcessorNumber', 'AllocConsole', 'FreeConsole', 'GetStdHandle', 'SetStdHandle', 'SetThreadAffinityMask', 'WriteFile', 'GetExtendedTcpTable', 'GetExtendedUdpTable', 'SetTcpEntry', 'AddVectoredContinueHandler', 'AddVectoredExceptionHandler', 'TerminateThread', 'ExitThread', 'RemoveVectoredExceptionHandler', 'ResumeThread', 'SuspendThread', 'WaitForSingleObject', 'GetThreadId', 'LoadLibraryExA', 'LoadLibraryExW', 'SymInitialize', 'SymFromName', 'SymLoadModuleEx', 'SymSetOptions', 'SymGetTypeInfo', 'DeviceIoControl', 'GetTokenInformation', 'RegOpenKeyExA', 'RegOpenKeyExW', 'RegGetValueA', 'RegGetValueW', 'RegCloseKey', 'Wow64DisableWow64FsRedirection', 'Wow64RevertWow64FsRedirection', 'Wow64EnableWow64FsRedirection', 'Wow64GetThreadContext', 'SetConsoleCtrlHandler'] # ExitProcess(uExitCode): ExitProcessPrototype = WINFUNCTYPE(VOID, UINT) @@ -49,6 +49,14 @@ NtCreateThreadExParams = ((1, 'ThreadHandle'), (1, 'DesiredAccess'), (1, 'Object NtQueryInformationThreadPrototype = WINFUNCTYPE(NTSTATUS, HANDLE, THREAD_INFORMATION_CLASS, PVOID, ULONG, PULONG) NtQueryInformationThreadParams = ((1, 'ThreadHandle'), (1, 'ThreadInformationClass'), (1, 'ThreadInformation'), (1, 'ThreadInformationLength'), (1, 'ReturnLength')) +# GetExitCodeThread(hThread, lpExitCode): +GetExitCodeThreadPrototype = WINFUNCTYPE(BOOL, HANDLE, LPDWORD) +GetExitCodeThreadParams = ((1, 'hThread'), (1, 'lpExitCode')) + +# GetExitCodeProcess(hProcess, lpExitCode): +GetExitCodeProcessPrototype = WINFUNCTYPE(BOOL, HANDLE, LPDWORD) +GetExitCodeProcessParams = ((1, 'hProcess'), (1, 'lpExitCode')) + # VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect): VirtualAllocPrototype = WINFUNCTYPE(LPVOID, LPVOID, SIZE_T, DWORD, DWORD) VirtualAllocParams = ((1, 'lpAddress'), (1, 'dwSize'), (1, 'flAllocationType'), (1, 'flProtect')) diff --git a/generated_def/winstructs.py b/generated_def/winstructs.py index 6fecad8..2c3735a 100644 --- a/generated_def/winstructs.py +++ b/generated_def/winstructs.py @@ -858,7 +858,6 @@ PM128A = POINTER(_M128A) # Struct _CONTEXT64 definitions class _CONTEXT64(Structure): - _pack_ = 8 _fields_ = [ ("P1Home", DWORD64), ("P2Home", DWORD64), diff --git a/injection.py b/injection.py index d2f2ff0..c96f589 100644 --- a/injection.py +++ b/injection.py @@ -1,3 +1,6 @@ +import struct +import ctypes + import windows import windows.utils as utils @@ -55,7 +58,7 @@ def generate_python_exec_shellcode_64(target, PYDLL_addr, PyInit, PyRun, PYCODE_ code = x64.MultipleInstr() # Do stack alignement - code += x64.Push('RAX') + code += x64.Push('RCX') # Load python27.dll code += x64.Mov('RCX', PYDLL_addr) code += x64.Mov('RAX', LoadLibraryA) @@ -89,7 +92,7 @@ def generate_python_exec_shellcode_64(target, PYDLL_addr, PyInit, PyRun, PYCODE_ code += x64.Call('RAX') code += Clean_space_for_call # Remove stack alignement - code += x64.Pop('RAX') + code += x64.Pop('RCX') code += x64.Ret() return code.get_code() @@ -130,3 +133,39 @@ def inject_python_command(process, code_injected, PYDLL="python27.dll\x00"): def execute_python_code(process, code): shellcode_remote_addr = inject_python_command(process, code) return process.create_thread(shellcode_remote_addr, 0) + +retrieve_exc = r""" +import traceback +import sys +addr = {0} +txt = "".join(traceback.format_exception(sys.last_type, sys.last_value, sys.last_traceback)) +import ctypes + +size = ctypes.c_uint.from_address(addr) +size.value = len(txt) +buff = (ctypes.c_char * len(txt)).from_address(addr + ctypes.sizeof(ctypes.c_uint)) +buff[:] = txt +""" + +def retrieve_last_exception_data(process): + # TODO : FREE THIS + mem = process.virtual_alloc(0x1000) + execute_python_code(process, retrieve_exc.format(mem)) + size = struct.unpack("