![]() |
|
|
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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
#define _WIN32_DCOM
#include <Windows.h>
#include <comdef.h>
#include <WbemIdl.h>
#include "detours.h"
typedef HRESULT (__stdcall *PGET) (DWORD junk, LPCWSTR, LONG, VARIANT*, CIMTYPE*, LONG*);
PGET OrigGet;
BOOL bHooked = FALSE;
HRESULT __stdcall NewGet(DWORD junk, LPCWSTR wszName, LONG lFlags, VARIANT *pVal, CIMTYPE *pvtType, LONG *plFlavor)
if(!wcscmp(wszName, L"ProcessorId")) //CPUID
{
pVal->vt = VT_BSTR;
V_BSTR(pVal) = L"PUT SPOOFED PROCESSOR ID HERE";
}
else return OrigGet(junk, wszName, lFlags, pVal, pvtType, plFlavor);
return 1;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH && !bHooked)
{
bHooked = TRUE;
OrigGet = (PGET)DetourFunction((LPBYTE)DetourFindFunction("fastprox.dll", "?Get@CWbemObject@@UAGJPBGJPAUtagVARIANT@@PAJ2@Z"), (LPBYTE)NewGet);
}
return TRUE;
}
|
|
#3
|
|||
|
|||
|
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)
{
if (dwReason == DLL_PROCESS_ATTACH && !bHooked)
{
bHooked = TRUE;
OrigGet = (PGET)DetourFindFunction("fastprox.dll", "?Get@CWbemObject@@UAGJPBGJPAUtagVARIANT@@PAJ2@Z");
DetourAttach(&(PVOID&)OrigGet, (LPBYTE)NewGet);
}
return TRUE;
}
Also I wonder how to inject this DLL into the .NET process... Could this be done using "DetourCreateProcessWithDllEx"? |
|
#4
|
|||
|
|||
|
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()
{
HANDLE hToken;
LUID sedebugnameValue;
TOKEN_PRIVILEGES tp;
if ( !OpenProcessToken(
GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | // to adjust privileges
TOKEN_QUERY, // to get old privileges setting
&hToken
) )
//
// OpenProcessToken() failed
//
return FALSE;
//
// Given a privilege's name SeDebugPrivilege, we should locate its local LUID mapping.
//
if ( !LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &sedebugnameValue ) )
{
//
// LookupPrivilegeValue() failed
//
CloseHandle( hToken );
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = sedebugnameValue;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( !AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof(tp), NULL, NULL ) )
{
//
// AdjustTokenPrivileges() failed
//
CloseHandle( hToken );
return FALSE;
}
CloseHandle( hToken );
return TRUE;
}
Code:
BOOL bInjectLibrary(HANDLE hProcess, char* szDllToInjectPath)
{
LPVOID lpRemoteAddress = VirtualAllocEx(hProcess, NULL, strlen(szDllToInjectPath), MEM_COMMIT, PAGE_READWRITE);
if(!lpRemoteAddress)
return FALSE;
if(!WriteProcessMemory(hProcess, lpRemoteAddress, (LPVOID)szDllToInjectPath, strlen(szDllToInjectPath), NULL))
return FALSE;
HANDLE hThread = NULL;
if(!(hThread = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("KERNEL32.DLL"), "LoadLibraryA"), lpRemoteAddress, NULL, NULL)))
return FALSE;
WaitForSingleObject(hThread, INFINITE);
if(!VirtualFreeEx(hProcess, lpRemoteAddress, 0, MEM_RELEASE))
return FALSE;
CloseHandle(hThread);
return TRUE;
}
|
| The Following User Gave Reputation+1 to Ember For This Useful Post: | ||
aldente (08-04-2012) | ||
|
#5
|
|||
|
|||
|
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 privilege
EnableDebugPrivilege();
// Create process
result = CreateProcess(NULL, &ExePathAndCmdLineVec[0], NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, &WorkingDirVec[0], &si, &pi);
if(!result)
{
return -1;
}
// Inject DLL
bInjectLibrary(pi.hThread, DllPath.c_str());
// Resume process execution
ResumeThread(pi.hThread);
Are 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... |
|
#6
|
|||
|
|||
|
Code:
bInjectLibrary(pi.hThread, DllPath.c_str()); // Resume process execution ResumeThread(pi.hThread); Code:
bInjectLibrary(pi.hProcess, DllPath.c_str()); // Resume process execution ResumeThread(pi.hProcess); //Close thread handle CloseHandle(pi.hThread); //Close process handle CloseHandle(pi.hProcess); |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| .NET dll hooking | Avi_RE | General Discussion | 10 | 09-28-2023 07:09 |
| API Hooking | thomasantony | General Discussion | 5 | 04-22-2005 11:44 |
| API-hooking | MaRKuS-DJM | General Discussion | 11 | 03-25-2005 13:27 |
| C++ Help (Hooking a function) | Peter[Pan] | General Discussion | 8 | 08-31-2004 20:37 |