// #include <iostream> #include <Windows.h> #include "injector.hpp" using namespace std; // // , , wstring str2wstr(const char * aIn); int main(int argc, char *argv[]) { // , if(argc != 3) { cout<<"Usage: launcher sample.exe inject.dll"<<endl; return 1; } // STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; // , // CREATE_SUSPENDED , "" if(CreateProcess(str2wstr(argv[1]).c_str(), NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi) == 0) { cout<<"Failed to create process"<<endl; return 1; } // injector injector a; // ( ) a.set_blocking(false); try { // a.inject(pi.dwProcessId, str2wstr(argv[2])); } catch(const injector_exception &e) { // - , e.show_error(); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); TerminateProcess(pi.hProcess, 1); return 1; } // ResumeThread(pi.hThread); // , CloseHandle(pi.hThread); CloseHandle(pi.hProcess); return 0; }
// #include <iostream> #include <sstream> #include <fstream> #include <Windows.h> #include "detours.h" #pragma comment(lib, "detours.lib") using namespace std; // , static unsigned int i = 0; // , int3 static const DWORD retn_offset = 0xB40E9; static const wstring dump_directory = L"dump"; static const wstring file_prefix = L"src_"; static const wstring perl_dll_name = L"p2x5142.dll"; // ANSI ? ( ) HMODULE (WINAPI * real_loadlibrary)(LPCSTR lpFileName) = LoadLibraryA; // , string wstr2str(const wchar_t * aIn); // void hook() { // , void * base_address = GetModuleHandle(perl_dll_name.c_str()); if(base_address == NULL) return; DWORD pr; // base_address = reinterpret_cast<void *>(reinterpret_cast<DWORD>(base_address) + retn_offset); // , int3, VirtualProtect(base_address, 1, PAGE_READWRITE, &pr); CopyMemory(base_address, "\xCC", 1); VirtualProtect(base_address, 1, pr, &pr); } // , void dump_data(char * buffer, unsigned int size) { DWORD pr; wstringstream ss; // ss << dump_directory << L"\\" << file_prefix << i++ << L".txt"; ofstream file(ss.str(), ofstream::binary); file.exceptions(0); if(file.is_open()) { // VirtualProtect(buffer, size, PAGE_READONLY, &pr); file.write(buffer, size); VirtualProtect(buffer, size, pr, &pr); file.close(); } } // , LoadLibraryA HMODULE WINAPI my_loadlibrary(LPCSTR lpFileName) { HMODULE h = real_loadlibrary(lpFileName); // , if ( strstr(lpFileName, wstr2str(perl_dll_name.c_str()).c_str()) && i == 0 ) { i++; hook(); } return h; } // , int3 LONG CALLBACK VEH(PEXCEPTION_POINTERS ExceptionInfo) { if ( // , Eax , Ebx ExceptionInfo->ContextRecord->Eax > 0 && ExceptionInfo->ContextRecord->Eax < 0xFFFFF && // - , "" // ExceptionInfo->ContextRecord->Ebx < 0x77000000 && ExceptionInfo->ContextRecord->Ebx > reinterpret_cast<DWORD>(GetProcessHeap()) ) dump_data(reinterpret_cast<char *>(ExceptionInfo->ContextRecord->Ebx), ExceptionInfo->ContextRecord->Eax); // Eip 4 // , , retn ExceptionInfo->ContextRecord->Eip = *reinterpret_cast<DWORD *>(ExceptionInfo->ContextRecord->Esp); ExceptionInfo->ContextRecord->Esp += sizeof(DWORD); // - return EXCEPTION_CONTINUE_EXECUTION; } BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved) { if(dwReason == DLL_PROCESS_ATTACH) { // CreateDirectory(dump_directory.c_str(), NULL); AddVectoredExceptionHandler(1, VEH); // detours DetourRestoreAfterWith(); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&reinterpret_cast<PVOID &>(real_loadlibrary), my_loadlibrary); DetourTransactionCommit(); } else if(dwReason == DLL_PROCESS_DETACH) { DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourDetach(&reinterpret_cast<PVOID &>(real_loadlibrary), my_loadlibrary); DetourTransactionCommit(); } return TRUE; }
Source: https://habr.com/ru/post/149015/
All Articles