I need to hook a particular .NET function in a target process. So I have my C# Dll ready to be injected, but I need a way to make the hook once the dll is loaded.
PROBLEM: There is no such things as dllmain in C#
RESOLUTION: Here is what I tried > C# equivalent of DllMain in C (WinAPI)
I don't have 50 reputations, so I can't comment @mheyman answer. But I did all what he suggested, compiled every thing in one Dll, which I then injected in my target process. But it blocks at the level of:
LaunchDll(dll, len, "MyNamespace.MyClass", "DllMain"); in dllmain.cpp
I can't go further, I get the error
This application has requested the Runtime to terminate it in an unusual way. Please contact the application’s support team for more information
The LaunchDll function is never executed, and if I modify it so that it immediately returns, I still get the same behavior, which makes me think the problem is at the call to LaunchDll.
Here is the output I get from the Just in time debugger of Visual Studio:
...
'target.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-processthreads-l1-1-1.dll'. Symbols loaded. 'target.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l1-2-0.dll'. Symbols loaded. 'target.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-timezone-l1-1-0.dll'. Symbols loaded. 'target.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-file-l2-1-0.dll'. Symbols loaded. 'target.exe' (Win32): Loaded 'C:\Windows\System32\api-ms-win-core-synch-l1-2-0.dll'. Symbols loaded. The thread 0xc27c has exited with code 3 (0x3). The thread 0x828c has exited with code 3 (0x3). The thread 0xb47c has exited with code 3 (0x3). The thread 0xbce0 has exited with code 3 (0x3). The thread 0xb974 has exited with code 3 (0x3). The thread 0x68d4 has exited with code 3 (0x3). The thread 0xc4e8 has exited with code 3 (0x3). The thread 0xc748 has exited with code 3 (0x3). The thread 0xc198 has exited with code 3 (0x3). The thread 0xa708 has exited with code 3 (0x3). The thread 0x8870 has exited with code 3 (0x3). The thread 0xc02c has exited with code 3 (0x3). The thread 0xc3fc has exited with code 3 (0x3). The thread 0x995c has exited with code 3 (0x3). The thread 0xb790 has exited with code 3 (0x3). The thread 0xb9b8 has exited with code 3 (0x3). The program '[47324] target.exe' has exited with code 3 (0x3).
- Of course, target.exe and Dll are in the same config (64 bits)
I compiled everything as release (who knows ? -_-) but nothing changed
Here is the source along side with the config files: https://wetransfer.com/downloads/c10aab5535121568ef185d3e1ca2193520190219015759/dd94c8 Pass:LoaderDllSO
Here is the code of the .NET Dll:
namespace MyNameSpace {
public class MyClass {
public static void DllMain() {
string text1 = "random Text from .NET Dll \n ";
System.IO.File.WriteAllText(@"D:\\TestFromCSharp.txt", text1);
}
}
}