依据开源hook库 minhook例子 想hook一个com IFileOperation 一直不成功,执行结果直接返回啥都没做,程序如下,请大神指教
根据开源hook库 minhook例子 想hook一个com IFileOperation 一直不成功,执行结果直接返回啥都没做,程序如下,请大神指教
#include "stdafx.h"
#include <windows.h>
#include "MinHook.h"
#include <Winternl.h>
#include <stdio.h>
#include <shlwapi.h>
#include <tchar.h>
#include <string.h>
#include <psapi.h>
#include <strsafe.h>
#include <Shobjidl.h>
#include <Shellapi.h>
#include <Shlguid.h>
#include <Shlobj.h>
#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
bool __is32bitMachine = false ;
#elif defined _M_IX86
#pragma comment(lib, "libMinHook-x86-v100-mdd.lib")
bool __is32bitMachine = true;
#endif
PVOID GetInterfaceMethod(PVOID intf, DWORD methodIndex)
{
if (__is32bitMachine)
return *(PVOID*)(*(DWORD_PTR*)intf + methodIndex * 4);
else
return *(PVOID*)(*(DWORD_PTR*)intf + methodIndex * 8);
}
typedef HRESULT (WINAPI *CopyItemsNext)(IFileOperation * pThis, IUnknown *punkItems,IShellItem *psiDestinationFolder);
CopyItemsNext Real_CopyItems = NULL;
CopyItemsNext Actual_CopyItems;
HRESULT WINAPI CopyItemsCallback(IFileOperation * pThis, IUnknown *punkItems,IShellItem *psiDestinationFolder)
{
MessageBoxW(NULL,L"CopyItems Function Called", L"HookedCopyItemS", MB_OK);
return Real_CopyItems(pThis, punkItems, psiDestinationFolder);
//return 0;
}
typedef HRESULT (WINAPI *COCREATEINSTANCE)(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID*);
COCREATEINSTANCE Real_CoCreateInstance = NULL;
HRESULT WINAPI CoCreateInstanceCallback(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID *ppv)
{
const char *IFileOperation_GUID = "{3ad05575-8857-4850-9277-11b85bdb8e09}";
char GUIDString[64];
HRESULT HR = Real_CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv);
sprintf_s(GUIDString, 64, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\0",
rclsid.Data1, rclsid.Data2, rclsid.Data3,
rclsid.Data4[0], rclsid.Data4[1],
rclsid.Data4[2], rclsid.Data4[3],
rclsid.Data4[4], rclsid.Data4[5],
rclsid.Data4[6], rclsid.Data4[7]);
if (strcmp(GUIDString, IFileOperation_GUID) == 0)
{
MessageBoxA(NULL, "IFileOperation_GUID Found", GUIDString, MB_OK);
if(Real_CopyItems == NULL)
{
Actual_CopyItems = (CopyItemsNext)GetInterfaceMethod(*ppv, 17);
MessageBoxA(NULL,"AFTER GetInterfaceMethod", "TEST", MB_OK);
if (MH_CreateHook(Actual_CopyItems, &CopyItemsCallback, reinterpret_cast<void**>(&Real_CopyItems)) != MH_OK)
{
MessageBoxW(NULL, L"Failed CreateHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_EnableHook(Actual_CopyItems) != MH_OK)
{
MessageBoxW(NULL, L"Failed EnableHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
}
}
}
return HR;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (MH_Initialize() != MH_OK)
{
MessageBoxW(NULL, L"Failed Initialize", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_CreateHook(&CoCreateInstance, &CoCreateInstanceCallback, reinterpret_cast<void**>(&Real_CoCreateInstance)) != MH_OK)
{
MessageBoxW(NULL, L"Failed MH_CreateHook CoCreateInstance", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_EnableHook(&CoCreateInstance) != MH_OK)
{
MessageBoxW(NULL, L"Failed MH_EnableHook CoCreateInstance", L"Info!", MB_ICONWARNING|MB_OK);
}
return 0;
}
回答满意可以加分 在线等
------解决思路----------------------
DLL注入有好几种可行的方案,其中一种是远程线程注入技术
代码如下
#include "stdafx.h"
#include <windows.h>
#include "MinHook.h"
#include <Winternl.h>
#include <stdio.h>
#include <shlwapi.h>
#include <tchar.h>
#include <string.h>
#include <psapi.h>
#include <strsafe.h>
#include <Shobjidl.h>
#include <Shellapi.h>
#include <Shlguid.h>
#include <Shlobj.h>
#if defined _M_X64
#pragma comment(lib, "libMinHook.x64.lib")
bool __is32bitMachine = false ;
#elif defined _M_IX86
#pragma comment(lib, "libMinHook-x86-v100-mdd.lib")
bool __is32bitMachine = true;
#endif
PVOID GetInterfaceMethod(PVOID intf, DWORD methodIndex)
{
if (__is32bitMachine)
return *(PVOID*)(*(DWORD_PTR*)intf + methodIndex * 4);
else
return *(PVOID*)(*(DWORD_PTR*)intf + methodIndex * 8);
}
typedef HRESULT (WINAPI *CopyItemsNext)(IFileOperation * pThis, IUnknown *punkItems,IShellItem *psiDestinationFolder);
CopyItemsNext Real_CopyItems = NULL;
CopyItemsNext Actual_CopyItems;
HRESULT WINAPI CopyItemsCallback(IFileOperation * pThis, IUnknown *punkItems,IShellItem *psiDestinationFolder)
{
MessageBoxW(NULL,L"CopyItems Function Called", L"HookedCopyItemS", MB_OK);
return Real_CopyItems(pThis, punkItems, psiDestinationFolder);
//return 0;
}
typedef HRESULT (WINAPI *COCREATEINSTANCE)(REFCLSID, LPUNKNOWN, DWORD, REFIID, LPVOID*);
COCREATEINSTANCE Real_CoCreateInstance = NULL;
HRESULT WINAPI CoCreateInstanceCallback(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID *ppv)
{
const char *IFileOperation_GUID = "{3ad05575-8857-4850-9277-11b85bdb8e09}";
char GUIDString[64];
HRESULT HR = Real_CoCreateInstance(rclsid, pUnkOuter, dwClsContext, riid, ppv);
sprintf_s(GUIDString, 64, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}\0",
rclsid.Data1, rclsid.Data2, rclsid.Data3,
rclsid.Data4[0], rclsid.Data4[1],
rclsid.Data4[2], rclsid.Data4[3],
rclsid.Data4[4], rclsid.Data4[5],
rclsid.Data4[6], rclsid.Data4[7]);
if (strcmp(GUIDString, IFileOperation_GUID) == 0)
{
MessageBoxA(NULL, "IFileOperation_GUID Found", GUIDString, MB_OK);
if(Real_CopyItems == NULL)
{
Actual_CopyItems = (CopyItemsNext)GetInterfaceMethod(*ppv, 17);
MessageBoxA(NULL,"AFTER GetInterfaceMethod", "TEST", MB_OK);
if (MH_CreateHook(Actual_CopyItems, &CopyItemsCallback, reinterpret_cast<void**>(&Real_CopyItems)) != MH_OK)
{
MessageBoxW(NULL, L"Failed CreateHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_EnableHook(Actual_CopyItems) != MH_OK)
{
MessageBoxW(NULL, L"Failed EnableHook Real_CopyItem", L"Info!", MB_ICONWARNING|MB_OK);
}
}
}
return HR;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (MH_Initialize() != MH_OK)
{
MessageBoxW(NULL, L"Failed Initialize", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_CreateHook(&CoCreateInstance, &CoCreateInstanceCallback, reinterpret_cast<void**>(&Real_CoCreateInstance)) != MH_OK)
{
MessageBoxW(NULL, L"Failed MH_CreateHook CoCreateInstance", L"Info!", MB_ICONWARNING|MB_OK);
}
if (MH_EnableHook(&CoCreateInstance) != MH_OK)
{
MessageBoxW(NULL, L"Failed MH_EnableHook CoCreateInstance", L"Info!", MB_ICONWARNING|MB_OK);
}
return 0;
}
回答满意可以加分 在线等
------解决思路----------------------
DLL注入有好几种可行的方案,其中一种是远程线程注入技术
代码如下
//提升进程权限
bool EnableDebugPrivilege(const LPTSTR name)
{
HANDLE token;
TOKEN_PRIVILEGES tp;
//打开进程令牌环
if(!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES
------解决思路----------------------
TOKEN_QUERY,&token))
{
return false;
}
//获得进程本地唯一ID
LUID luid;
if(!LookupPrivilegeValue(NULL,name,&luid))
{
return false;
}
tp.PrivilegeCount=1;
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
tp.Privileges[0].Luid=luid;
//调整进程权限
if(!AdjustTokenPrivileges(token,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL))
{
return false;
}
return true;
}
bool LoadDllToProcess(TCHAR *Path,DWORD Id)
{
EnableDebugPrivilege(SE_DEBUG_NAME);
HANDLE Process=OpenProcess(PROCESS_ALL_ACCESS,FALSE,Id);
if(Process==NULL)
{
return false;
}
else
{
size_t Size=wcslen(Path)*sizeof(TCHAR)+1;
BYTE *Param=(BYTE *)VirtualAllocEx(Process,NULL,Size,MEM_COMMIT,PAGE_READWRITE);
if(Param)
{
if(WriteProcessMemory(Process,Param,Path,Size,NULL))
{
VirtualProtectEx(Process,Param,Size,PAGE_READONLY,NULL);
if(CreateRemoteThread(Process,NULL,0,(LPTHREAD_START_ROUTINE)LoadLibrary,
Param,NULL,NULL)==NULL)
{
return false;
}
}
else
{
CloseHandle(Process);
return false;
}
}
else
{
return false;
}
}
return true;
}