QuestionHelpIs it normal for an injector to crash if no DLL exists yet?

Posts 1–4 of 4 · Page 1 of 1
Is it normal for an injector to crash if no DLL exists yet?
I'm following Fleep Hacks' tutorials (I'm a noob in this field still, what can you expect ) and trying to write them in my own way somewhat. I'm wondering why the injector crashes during runtime... (I'm suspecting it's something to do with FARPROC loadLibraryAddr variable, but I'm not sure... I haven't actually created my DLL yet, but it still should be able to inject nothing into a process too, right?

Code:
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>

char* procName = "ac_client.exe";
char* FileToInject = "ACDLL.dll";

bool injectDLL(DWORD procID){
    HANDLE hProc;
    LPVOID paramAddr;

    HINSTANCE hDLL = LoadLibrary("KERNEL32");
    FARPROC loadLibraryAddr = GetProcAddress(hDLL, "LoadLibraryA");
    hProc = OpenProcess(PROCESS_ALL_ACCESS, false, procID);

    char* dllPath = "C:\\Users\\Kalist\\Desktop\\";
    strcat(dllPath, FileToInject);

    paramAddr = VirtualAllocEx(hProc, 0, strlen(dllPath)+1, MEM_COMMIT, PAGE_READWRITE);
    bool memoryWritten = WriteProcessMemory(hProc, paramAddr, dllPath, strlen(dllPath)+1, 0);
    CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddr, paramAddr, 0, 0);

    CloseHandle(hProc);
    return memoryWritten;
}

int main(){
    DWORD procID = 0;
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
    HANDLE hProcSnap;

    while(!procID){
        std::cout << "Searching for " << procName; Sleep(500); std::cout << "."; Sleep(500); std::cout << "."; Sleep(500); std::cout << "."; Sleep(500);

        hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, procID);

        if(Process32First(hProcSnap, &pe32)){
            do{
                if(!strcmp(pe32.szExeFile, procName)){
                    procID = pe32.th32ProcessID;
                    break;
                }

            }while(Process32Next(hProcSnap, &pe32));
        }
        if(!procID){
            system("CLS");
        }
    }
    while(!injectDLL(procID)){
        system("CLS");
        std::cout << "Injecting"; Sleep(500); std::cout << "."; Sleep(500); std::cout << "."; Sleep(500); std::cout << "."; Sleep(500);
    }
    system("CLS");
    std::cout << std::endl << "Injected successfully!";
}
no, you need to inject a dll...

loadlibrary expects one.
Why don't you try catching an exception. Or just check and make sure the all of the functions were properly executed. If one wasn't, get the error with GetLastError and check the result here. Make sure you terminate on failure.
No, no it shouldn't.
if loadlibrary can't find the dll specified it will just return FALSE (error).
This applies to most Win32 API functions. When a program crashes, something goes wrong within it's address space, e.g it tries to expand in memory without memory allocation, this causes a crash, and is a major problem in statically typed languages like C++, that have no framework and execute natively.

here is a dll injection function that I just wrote:

Code:
	
BOOL InjectDll(DWORD ProcessId, LPCSTR DllPath)
	{
		HANDLE Process;
		LPVOID Memory;
		LPVOID LoadLibraryA;
		if (!PathFileExists(ModulePath)) {
			return FALSE;
		}
		if (ProcessId == NULL) {
			return FALSE;
		}
		Process = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, ProcessId);
		LoadLibrary = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
		Memory = (LPVOID)VirtualAllocEx(Process, NULL, strlen(ModulePath) + 1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
		if (!WriteProcessMemory(Process, (LPVOID)Memory, ModulePath, strlen(ModulePath) + 1, NULL)) {
			return FALSE;
		}
		CreateRemoteThread(Process, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibrary, (LPVOID)Memory, NULL, NULL);
		CloseHandle(Process);
		VirtualFreeEx(Process, (LPVOID)Memory, 0, MEM_RELEASE);
		return TRUE;
	}
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?