qo0-csgo icon indicating copy to clipboard operation
qo0-csgo copied to clipboard

Implement DllMain-safe (non-deadlocking, FreeLibrary-able) loading mechanism

Open bmbkr opened this issue 2 years ago • 3 comments

I haven't tested this code because I don't have enough bandwidth to download Visual Studio again, at least it's syntactically correct.

This PR adds compatibility with more LoadLibrary DLL injectors. These injectors work by just calling LoadLibrary(pszDll) in the target process. Some also support uninjecting by calling FreeLibrary(hDll) in the target process. The current implementation of DllMain is incompatible with this function call, as there is no handler for DLL_PROCESS_DETACH.

Changes include:

  • Removing all DllMain-unsafe code (anything non-Win32, as well as MessageBox calls, these are NOT intended to be used inside of DllMain)
  • Combining OnDllAttach and OnDllDetach threads into one. Now at the end of initialization, the thread waits for either the panic key to be pressed, or for DllMain to signal that the thread should exit.

bmbkr avatar Aug 18 '22 19:08 bmbkr

@nieatty Seems a good PR, what about DisableThreadLibraryCalls?

ghost avatar Aug 19 '22 06:08 ghost

i'm appreciate the work you did, but due to v2 WIP, i already made changes to it, like:

  • moved to static CRT
  • instead of doing the whole code in two separate threads, i've moved initialization out of thread and leave only 1 thread to keep unload spinlock
  • moved all of detach code it used to DLL_DETACH reason
  • also had a lot of things to do that will help to completely avoid the CRT and some of winapi calls/imports, ofc i leave the CRT initialization itself otherwise base will not be suitable for beginners but still makes it easy to rework for ppl that know what they do

rollraw avatar Aug 19 '22 12:08 rollraw

@nieatty Seems a good PR, what about DisableThreadLibraryCalls?

I’m not gonna lie, I did totally forget to add that back. I also haven’t really done any research into it either…

i'm appreciate the work you did, but due to v2 WIP, i already made changes to it, like:

  • moved to static CRT
  • instead of doing the whole code in two separate threads, i've moved initialization out of thread and leave only 1 thread to keep unload spinlock
  • moved all of detach code it used to DLL_DETACH reason
  • also had a lot of things to do that will help to completely avoid the CRT and some of winapi calls/imports, ofc i leave the CRT initialization itself otherwise base will not be suitable for beginners but still makes it easy to rework for ppl that know what they do

Well then, I guess that makes this PR useless haha. Feel free to close this when V2 drops (mostly as a notification for me lol, I like this repo)

bmbkr avatar Aug 19 '22 14:08 bmbkr