gpt4 book ai didi

windows - 在 C++ 中 Hook IDispatch v 表

转载 作者:可可西里 更新时间:2023-11-01 10:01:56 32 4
gpt4 key购买 nike

我正在尝试修改系统中已存在的 IDispatch 接口(interface)的行为。为此,我的计划是在运行时 Hook 对象 v 表并修改指针,使其指向自定义 Hook 方法。

如果我能让它工作,我就可以向现有对象添加新方法和属性。不错。

首先,我尝试连接到 IUnknown 的 v 表(IDispatch 从中继承)并且工作正常。但是,尝试更改 IDispatch 中的条目根本不起作用。什么也没有发生,代码就像没有钩子(Hook)时一样工作。

这是代码,很简单,理解起来应该没有问题

#include <iostream>
#include <windows.h>
#include <Objbase.h>
#pragma comment (lib,"Ole32.lib")
using namespace std;

HRESULT __stdcall typecount(IDispatch *self,UINT*u)
{
cout << "hook" << endl;
*u=1;
return S_OK;
}


int main()
{
CoInitialize(NULL);

// Get clsid from name
CLSID clsid;
CLSIDFromProgID(L"shell.application",&clsid);

// Create instance
IDispatch *obj=NULL;
CoCreateInstance(clsid,NULL,CLSCTX_INPROC_SERVER,__uuidof(IDispatch),(void**)&obj);

// Get vtable and offset in vtable for idispatch
void* iunknown_vtable= (void*)*((unsigned int*)obj);
// There are three entries in IUnknown, therefore add 12 to go to IDispatch
void* idispatch_vtable = (void*)(((unsigned int)iunknown_vtable)+12);

// Get pointer of first emtry in IDispatch vtable (GetTypeInfoCount)
unsigned int* v1 = (unsigned int*)iunknown_vtable;

// Change memory permissions so address can be overwritten
DWORD old;
VirtualProtect(v1,4,PAGE_EXECUTE_READWRITE,&old);

// Override v-table pointer
*v1 = (unsigned int) typecount;

// Try calling GetTypeInfo count, should now be hooked. But isn't works as usual
UINT num=0;
obj->GetTypeInfoCount(&num);

/*
HRESULT hresult;
OLECHAR FAR* szMember = (OLECHAR*)L"MinimizeAll";
DISPID dispid;
DISPPARAMS dispparamsNoArgs = {NULL, NULL, 0, 0};
hresult = obj->GetIDsOfNames(IID_NULL, &szMember, 1,
LOCALE_SYSTEM_DEFAULT, &dispid) ;
hresult = obj->Invoke(dispid,IID_NULL,LOCALE_SYSTEM_DEFAULT,DISPATCH_METHOD,&dispparamsNoArgs, NULL, NULL, NULL);
*/

}

最佳答案

// Get pointer of first emtry in IDispatch vtable (GetTypeInfoCount)
unsigned int* v1 = (unsigned int*)iunknown_vtable;

这实际上 Hook 了 IUnknownQueryInterface

以下调用将路由到您的typecount

//obj->GetTypeInfoCount(&num);
LPVOID dummy;
obj->QueryInterface(IID_NULL, &dummy);

关于windows - 在 C++ 中 Hook IDispatch v 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1992092/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com