DLL Injection
-
Hi,
ich habe mich in den letzten Tagen mit DLL Injection beschäftig und das ein oder andere Programm versucht zu schreiben. Mir ist dabei aufgefallen das ich jedes Programm töte wenn ich meine Funktion in dem vom Programm benutzen RAM lade. Ich denke ma das liegt wohl daran das ich den Arbeitspeicher einfach überschreibe(?).
Meine Frage ist nun wie ich Meine Funktion von einem anderem Prozess ausführen lassen kann ohne ihn zu crashen.Den Code den ich benutze:
(Quelle: http://mitglied.lycos.de/maiselthomas/dllinjection.php)#include <windows.h> #include <cstdio> typedef HINSTANCE (*fpLoadLibrary)(char*); typedef LPVOID (*fpGetProcAddress)(HINSTANCE, char*); typedef void (*fpFunktion)(void); struct INJECTSTRUCT { ... }; DWORD WINAPI threadstart(LPVOID addr) { HINSTANCE hDll; fpFunktion funktion; INJECTSTRUCT * is = (INJECTSTRUCT*)addr; hDll = is->loadlib(is->path); funktion = (fpFunktion)is->getprocaddress(hDll, is->func); funktion(); return 0; } bool EnableDebugPrivilege() {... } void threadend() { } int main() { HANDLE hProc; LPVOID start, thread; DWORD funcsize, written; HINSTANCE hDll; INJECTSTRUCT is; DWORD id; EnableDebugPrivilege(); hDll = LoadLibrary("KERNEL32"); is.loadlib = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA"); is.getprocaddress = (fpGetProcAddress)GetProcAddress(hDll, "GetProcAddress"); strcpy(is.path, "C:\\DLL.dll"); strcpy(is.func, "Funktion"); funcsize = (DWORD)threadend-(DWORD)threadstart; printf("ID: "); scanf("%d", &id); hProc = OpenProcess(PROCESS_ALL_ACCESS, false, id); printf("Prozess Handle: %x\n", hProc); start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_EXECUTE_READWRITE); printf("Memory: %x\n", start); WriteProcessMemory(hProc, start, (LPVOID)&is, sizeof(INJECTSTRUCT), NULL); thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT)); WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL); CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, 0); CloseHandle(hProc); Sleep(5000); return 0; }
-
Prüf mal ob "DLL.dll" eine Funktion namens "Funktion" hat.
-
Laut Depends gibt es die Funktion "Funktion".
-
Die Funktion "EnableDebugPrivilege()" sollte besser nicht zwischen "threadend()" und "threadstart()" im Code stehen.
Sonst brezelt der Remotethread in eine falsche Stelle rein:
thread = (LPVOID)((DWORD)start+sizeof(INJECTSTRUCT)); // <- die 'länge' von 'EnableRemoteThread()' würde noch fehlen ... CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, 0);
-
die 'länge' von 'EnableRemoteThread()' würde noch fehlen
EnableRemoteThread().. was soll das sein?

-
Hier ist der Code vom Tutorial etwas überarbeitet und läuft mit Unicode und ANSI:
/* ----------------------------------------------------------------------- HANDLE InjectDll(unsigned ProcessId, tstring DllFile, char *FunctionName, HWND ErrorParent, tstring errorTitle) returns the handle to the new created thread if it work returns 0 if it fails ----------------------------------------------------------------------- */ #ifdef UNICODE typedef std::wstring tstring; typedef std::wstringstream tstringstream; #define LoadLibraryGeneric "LoadLibraryW" #else typedef std::string tstring; typedef std::stringstream tstringstream; #define LoadLibraryGeneric "LoadLibraryA" #endif typedef HINSTANCE (__stdcall *fpLoadLibrary) (TCHAR*); typedef void* (__stdcall *fpGetProcAddress)(HMODULE, LPCSTR); typedef void (*fpFunktion)(void); struct INJECTSTRUCT { fpLoadLibrary LoadLibrary; fpGetProcAddress GetProcAddress; TCHAR path[255]; char func[255]; }; DWORD WINAPI threadstart(LPVOID addr) { HINSTANCE hDll; fpFunktion funktion; INJECTSTRUCT * is = (INJECTSTRUCT*)addr; hDll = is->LoadLibrary(is->path); funktion = (fpFunktion)is->GetProcAddress(hDll, is->func); funktion(); return 0; } void threadend() { } bool EnableDebugPrivilege() { TOKEN_PRIVILEGES priv; HANDLE hThis, hToken; LUID luid; hThis = GetCurrentProcess(); OpenProcessToken(hThis, TOKEN_ADJUST_PRIVILEGES, &hToken); LookupPrivilegeValue(0, TEXT("seDebugPrivilege"), &luid); priv.PrivilegeCount = 1; priv.Privileges[0].Luid = luid; priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; AdjustTokenPrivileges(hToken, false, &priv, 0, 0, 0); CloseHandle(hToken); CloseHandle(hThis); return true; } HANDLE Utilities::InjectDll(unsigned ProcessId, tstring DllFile, std::string FunctionName, HWND ErrorParent, tstring ErrorTitle) { tstringstream errorStream; if(ExistFile(DllFile) == false) { errorStream << TEXT("Can't find ") << PathFindFileName(DllFile.c_str()) << TEXT("!"); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } INJECTSTRUCT is; _tcscpy_s(is.path, DllFile.c_str()); strcpy_s(is.func, FunctionName.c_str()); unsigned funcsize = (unsigned)threadend - (unsigned)threadstart; EnableDebugPrivilege(); HINSTANCE hDll = LoadLibrary(TEXT("KERNEL32")); if(hDll == NULL) { errorStream << TEXT("LoadLibrary failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } is.LoadLibrary = (fpLoadLibrary)GetProcAddress(hDll, LoadLibraryGeneric); is.GetProcAddress = (fpGetProcAddress)GetProcAddress(hDll, "GetProcAddress"); if(is.LoadLibrary == NULL || is.GetProcAddress == NULL) { errorStream << TEXT("GetProcAddress failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId); if(hProc == NULL) { errorStream << TEXT("OpenProcess failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } void* start = VirtualAllocEx(hProc, 0, funcsize+sizeof(INJECTSTRUCT), MEM_COMMIT, PAGE_EXECUTE_READWRITE); if(start == NULL) { errorStream << TEXT("VirtualAllocEx failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } if(WriteProcessMemory(hProc, start, (LPVOID)&is, sizeof(INJECTSTRUCT), NULL) == 0) { errorStream << TEXT("WriteProcessMemory failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } void* thread = (void*)((unsigned)start + sizeof(INJECTSTRUCT)); if(WriteProcessMemory(hProc, thread, (LPVOID)threadstart, funcsize, NULL) == 0) { errorStream << TEXT("WriteProcessMemory failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } HANDLE threadID = CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)thread, start, 0, 0); if(threadID == NULL) { errorStream << TEXT("CreateRemoteThread failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } if(CloseHandle(hProc) == 0) { errorStream << TEXT("CloseHandle failed! Error Code: ") << GetLastError(); MessageBox(ErrorParent, errorStream.str().c_str(), ErrorTitle.c_str(), MB_ICONERROR); return 0; } return threadID; }Bei meinem Code ist oben bei den typedefs noch ein __stdcall, vielleicht crashed es deswegen bei dir.
-
Vielen Dank an alle. Mit __stdcall funktioniert es :).