Jump to content
Nytro

DLL Injection Framework

Recommended Posts

Posted

DLL Injection Framework

Update: I’ve left this up for posterity, but unless you have a good reason not to, you should be using Microsoft Detours for this stuff. It’s just as easy to use and far more mature.

Code injection is messy. Hooking via JMP patching is even messier. So I’ve tried to clean things up as much as possible by putting the heavy-lifting in a C++ class.

You can get a rough idea of the exposed functionality from the class’s public function list:

bool CallThreadProc(const char* thread_proc_name, void* parameter, DWORD timeout_ms, DWORD &exit_code);

static DWORD GetProcessIDFromWindow(const char* class_name, const char* window_name);

void* GetRemoteProcAddress(const char* proc_name);

void* GetRemoteProcAddress(const char* proc_name, const char* module_path);

HMODULE GetRemoteModuleHandle(HMODULE local_handle);

HMODULE InjectDLL(DWORD process_id);

HDLLHOOK InstallDLLHook(const char* existing_module_path, const char* existing_function_name, const char* hook_function_name);

HDLLHOOK InstallCodeHook(void* existing_function_address, const char* hook_function_name);

bool RemoveAllHooks();

bool RemoveHook(HDLLHOOK handle);

void RemoveDLL();

I provide no documentation beyond the brief descriptions given in the header and this following example. It should all be sufficiently self-explanatory though. This snippet injects a DLL into Windows Calculator, which is assumed to be running, then installs two hooks (one by name and another by address) from the user-supplied HookDLL.

// Create the injection object
DLLInjection injection("E:/Temp/HookDLL.dll");

// Find Calc.exe by its window
DWORD process_id = injection.GetProcessIDFromWindow("SciCalc", "Calculator");

// Inject the DLL
HMODULE remote_module = injection.InjectDLL(process_id);

// Hook a DLL function (User32!SetWindowTextW)
HDLLHOOK swtw_hook = injection.InstallDLLHook("C:/Windows/System32/User32.dll", "SetWindowTextW", "SetWindowTextHookW");

// Hook a function manually (Calc!0100F3CF)
HDLLHOOK manual_hook = injection.InstallCodeHook(reinterpret_cast<void*> (0x0100F3CF), "SomeOtherHook");

// Remove the hooks
injection.RemoveHook(swtw_hook);
injection.RemoveHook(manual_hook);

Sursa: http://www.ring3circus.com/downloads/dll-injection-framework/

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...