![]() |
Hooking WMI (.NET Application)
Hello!
I have a .NET application which uses WMI to query the "Win32_Processor" class: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394373(v=vs.85).aspx I want to hook the function call and modify the string returned in "ProcessorId". How would you do that, when working with a .NET application? I would prefer a "high level" general solution (e.g. a Loader) which also works for future versions of the software, instead of modifying the application directly. |
This is easy. You can do it very easily by using Microsoft Detours and injecting a DLL into the target process.
Code:
#define WIN32_LEAN_AND_MEAN |
Thanks for your answer!
But: I'm new to Detours, and I'm having trouble to get your DLL to work... The current version of Detours deos not support "DetourFunction", I had to change the DLLMain function: Code:
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)Also I wonder how to inject this DLL into the .NET process... Could this be done using "DetourCreateProcessWithDllEx"? |
1 Attachment(s)
Please see attached for my copy of detours.h and detours.lib
You can write a simple loader that could start up the app with CreateProcess(), using information from the lpProcessInformation param to get the process' handle. Note: you may need to enable SeDebugPrivilege first before doing any of this. Code borrowed from online. Code:
BOOL EnableDebugPrivilege() Code:
BOOL bInjectLibrary(HANDLE hProcess, char* szDllToInjectPath) |
1 Attachment(s)
Ok, thanks for your version of the Detours library, with that one I could build the DLL.
However, injection/hooking does not work properly, yet. This is basically what my loader looks like: Code:
// Get debug privilegeAre you sure this kind of injection works for .NET applications? I attached a sample executable... It is not the target, but it works just like the target. I couldn't manage to manipulate the "ProcessorId"-Entry using your DLL... |
Code:
bInjectLibrary(pi.hThread, DllPath.c_str());Code:
bInjectLibrary(pi.hProcess, DllPath.c_str()); |
ResumeThread() has to be called with pi.hThread, that was right. The process will stay suspended, if you call it with pi.hProcess.
But apart from that, I tried your suggestions. I also tried to start the process non-suspended, which doesn't work either... This injection stuff is a debugging nightmare, you can't debug it in the development environment, and debugging in IDA doesn't work neither because of all this .NET stuff... :-( Don't you have somer loader, which is known working for .NET applications? |
Quote:
I have successfully got the DLL working in your test app, spoofing processor ID with my hook + this loader: http://code.google.com/p/injector/ Command line: newloaderv4.1.exe --lib "E:\Downloads\WmiSpoof.dll" --launch Test.exe http://i.imgur.com/2rqsj.png |
1 Attachment(s)
Yes, I am on a x64 system...
I just tried the loader you mentioned in a virtual machine (x86), but without any success either... Code:
newloaderv4.1.exe --lib WmiSpoof_x86_StaticRuntime.dll --launch Test.exeWhen I close Test.exe, the loader outputs: Code:
Error: [@InjectLibraryW] Unknown Error (LoadLibraryW).I built it Non-Unicode with static runtime ('release' mode) with MSVC 2010. I tried other options as well, neither worked (dynamic runtime, Unicode). Does my attached DLL work for you? (Thanks for your patience, by the way!) |
Yes, it works when I run the injector from administrative cmd line and modify the exe to have 32bit required in the .NET metadata header.
|
So why doesn't it work for me?
I tried to run it on Windows XP x86, as the Administrator user... No luck! What's your version of the .NET Framework? Maybe that's the difference... |
You are hooking a non-framework native module. I do not know what to tell you if it does not work on XP (unless you are lacking runtimes for VS2010 or something, even though it does not import it). I am on Windows 7 Ultimate x64 SP1 with all updates and it works fine, and it worked when I was on XP SP3 as well.
When I compile modules like that I link against the latest WDK libs and headers to cut down on size and importing anything from the new runtimes. It will link against msvcrt.dll instead. Perhaps you can PM me and tell me what you want to spoof your processor ID to and I can compile one in my environment and you can try it. |
I tested the exact same binaries on a third machine, and these are the results:
Win7 SP1 x64: Does not work WinXP SP3 x86: Does not work Win7 SP1 x86: Works Strange, but now I have at least one working system. I'll do some testing to find out why it doesn't work on the other ones. Thanks for all your help! |
| All times are GMT +8. The time now is 10:41. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX