Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static version of EasyHook DLL #260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

tmeckel
Copy link

@tmeckel tmeckel commented Sep 9, 2018

In a project I had the requirement to link EasyHook statically.
The pull request contains alll changes to build EsayHook as a static link library instead of a DLL.
I know using the static version of EasyHook with .NET this makes no sense, but using it a native project with C or C++ it's very valuable for not having to deliver the correct EasyHook.DLL with created program.

To use the static version calll the following methods in the main() function (normally this handled by the DllMain method, when the EasyHook DLL is loaded or unloaded into a process).

EasyHookAttach();
<...>
EasyHookDetach();

My work is based on the excellent article of Jerry Wang at Codeproject

@MuffinMario
Copy link

Additionally it should be added, that this makes remote injection impossible with current implementation, since it injects the .exe instead of the EasyHook32/64 dll and probably more complicated things I can't talk about

@pepeu93
Copy link

pepeu93 commented Sep 13, 2021

@tmeckel i compiled your static branch successfully, however in my inject app by default it doesn't find the function EasyHookAttach(); and EASYHOOK_STATIC looks likes is not defined.

Then when I set #define EASYHOOK_STATIC, it throws this errors:

image

Error LNK2001 unresolved external symbol AuxUlibIsDLLSynchronizationHeld EasyHook64S.lib(barrier.obj) 1
Any idea?

@tmeckel
Copy link
Author

tmeckel commented Sep 13, 2021

Try to replace the #if with #ifdef

https://docs.microsoft.com/en-us/cpp/preprocessor/hash-ifdef-and-hash-ifndef-directives-c-cpp?view=msvc-160

And ensure that EASYHOOK_STATIC is defined when you compile your program.

@pepeu93
Copy link

pepeu93 commented Sep 13, 2021

@tmeckel I was re-reading the article you linked in Codeproject, and the guy mentioned something about Aux_ulib.lib

I added

#pragma comment( lib, "EasyHook64S.lib")
#pragma comment( lib, "Aux_ulib.lib")
#pragma comment( lib, "psapi.lib") 

Now it compiled, however, I did not understand how do you attach the dll to the target process when I call:

	NTSTATUS nt = RhInjectLibrary(
		_ttoi(processID),                   // The process to inject into
		0,                                         // ThreadId to wake up upon injection
		EASYHOOK_INJECT_DEFAULT,
		NULL,                                 // 32-bit
		dllToInject,		         // 64-bit
		&data,                             // data to send to injected DLL entry point
		sizeof(DWORD)              // size of data to send
	);

Returns: RhInjectLibrary failed with error code = -1073741595

@tmeckel
Copy link
Author

tmeckel commented Sep 14, 2021

@pepeu93 I don't know your exact solution setup, but as I described in the initial Pull Request description you have to add the two following statements to you main method (C++ or C program obviously)

EasyHookAttach();
<...>
EasyHookDetach();

After EasyHookAttach you can initialize your hooks with LhInstallHook. Ensure that the PreProcessor symbol EASYHOOK_STATIC is set and that you have the following statements somewhere at the top in your .cpp or .c files:

#ifndef _M_X64
#ifdef _DEBUG
#pragma comment(lib, "EasyHook32SD.lib")
#else
#pragma comment(lib, "EasyHook32S.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "EasyHook64SD.lib")
#else
#pragma comment(lib, "EasyHook64S.lib")
#endif
#endif

The EasyHook.lib files are the static link libraries for EasyHook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants