Hooking a DirectX/COM Interface

Windows运行Switch游戏的技术原理与实操指南 游戏模拟器本质是跨架构的动态二进制翻译与系统调用重定向技术,其核心在于将ARM64指令实时编译为x86-64代码,并劫持原生OS服务(如hid、nvservices)映射至Windows API。Cemu并非传统仿真器,而是依赖JIT编译与Syscall Hooking实现高性能兼容;输入层则需vJoy虚拟HID设备与mouse2joystick精准建模采样率、加速度曲线及语义映射,解决体感延迟与坐标失真问题。该技术路径广泛应用于任天堂游戏PC化、嵌入式逆向及跨平台运行时开发等工程场景。 阅读详情

By Martin Mueller

This article features a description on, how to hook a DirectX/COM Interface. I used the DirectInput interface as an example of how to hook an interface function. 

     Download source files - 45 Kb

Introduction

After all the helpful articles I read here, I am glad that I can contribute a subject which has not yet been covered.

This article features a description on how to hook a DirectX/COM Interface. I used the DirectInput interface as an example of how to hook an interface function.

For the basic windows hook, I refer to an article by Wade Brainerd, which describes the API hooking process

 

Task:

 

To intercept a method of a COM Interface requires an extended approach compared to hooking an API call. If the desired dll is examined, only the create Interface function is actually exported by the dll. So, how can you hook your desired function?

 

 

 

DInput.dll

 

A COM interface is basically a list of virtual function pointers, which are linked together. You merely have to follow the links and modify every node till you finally reach the pointer of the function, which you would like to replace.



Step 1:

As you can see, only the create interface COM functions are visible, so you have to start your hooking chain at the DirectInputCreate function which returns a COM interface.

Here, you have to inject your Dll into the import address table ( IAT) of the calling program.

 

Step 2:

If the calling program invokes a DirectInputCreate, your function is called, you receive a pointer to a pointer of a pointer to a virtual function table, which is the interface of direct input


DECLARE_INTERFACE_(IDirectInputW, IUnknown)
{
    /*** IUnknown methods ***/
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj) PURE;
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
    STDMETHOD_(ULONG,Release)(THIS) PURE;

    /*** IDirectInputW methods ***/
    STDMETHOD(CreateDevice)(THIS_ REFGUID,LPDIRECTINPUTDEVICEW *,LPUNKNOWN) PURE;
    STDMETHOD(EnumDevices)(THIS_ DWORD,LPDIENUMDEVICESCALLBACKW,LPVOID,DWORD) PURE;
    STDMETHOD(GetDeviceStatus)(THIS_ REFGUID) PURE;
    STDMETHOD(RunControlPanel)(THIS_ HWND,DWORD) PURE;
    STDMETHOD(Initialize)(THIS_ HINSTANCE,DWORD) PURE;
};

Step 3:

Now you can create your device with CreateDevice. You will again receive an address to a different virtual function pointer table, which represents the Device.

 

 

 

 

Pick the method you would like to replace and change the virtual function pointer table in the according place to inject your function.

 

Step 4:

Do the actual data manipulation.

  

 

 

Implementation:

 

Step 1:

To hook yourself into an API function, you can simply use the windows API call SetWindowsHookEx. Here you create a system hook to monitor starting processes and match it to your desired program. After you identified your program you have to compare the import module names with the dll you wish to replace. Since this hook is written for direct input, the entry we are looking for is

DINPUT8.DLL. To find this entry you have to loop through the descriptors till you find your dll.

 


// Iterate through each import descriptor, and redirect if appropriate
  while ( pImportDesc->FirstThunk )
  {
    PSTR pszImportModuleName = MakePtr( PSTR, hModEXE, pImportDesc->Name);

    if ( lstrcmpi( pszImportModuleName, Hook->Name ) == 0 )
    {
      sprintf(dbBuffer,"Dll Found in module  %s replace it/n", Hook->Name );
      WriteToLog(dbBuffer);
      RedirectIAT( Hook, pImportDesc, (PVOID)hModEXE );
    }
    
    pImportDesc++;  // Advance to next import descriptor
  }


After you found your entry, you have to remove the write protection from the IAT with


VirtualQuery( pIAT, &mbi, sizeof(mbi) );

to be able to write into the memory. After the memory is open, you have to find your entry by iterate through the IAT.

 


  while ( pIteratingIAT->u1.Function )
  {
    void* HookFn = 0;  // Set to either the SFunctionHook or pStubs.

    if ( !IMAGE_SNAP_BY_ORDINAL( pINT->u1.Ordinal ) )  // import by name
    {
      PIMAGE_IMPORT_BY_NAME pImportName = MakePtr( PIMAGE_IMPORT_BY_NAME, pBaseLoadAddr, pINT->u1.AddressOfData );

      // Iterate through the hook functions, searching for this import.
      SFunctionHook* FHook = DLLHook->Functions;
      while ( FHook->Name )
      {
        if ( lstrcmpi( FHook->Name, (char*)pImportName->Name ) == 0 )
        {
          sprintf(dbBuffer,"Hooked function: %s/n",(char*)pImportName->Name );
          WriteToLog(dbBuffer);
          // Save the old function in the SFunctionHook structure and get the new one.
          FHook->OrigFn = (unsigned long*)pIteratingIAT->u1.Function;
          HookFn = FHook->HookFn;
          break;
         }

          FHook++;
        }

      }


Now, you can now replace it with your own one.


  // Replace the IAT function pointer if we have a hook.
  if ( HookFn )
  {
    // Cheez-o hack to see if what we're importing is code or data.
    // If it's code, we shouldn't be able to write to it
    if ( IsBadWritePtr( (PVOID)pIteratingIAT->u1.Function, 1 ) )
    {
      pIteratingIAT->u1.Function = (DWORD)HookFn;
    }
    else if ( osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
    {
      // Special hack for Win9X, which builds stubs for imported
      // functions in system DLLs (Loaded above 2GB).  These stubs are
      // writeable, so we have to explicitly check for this case
      if ( pIteratingIAT->u1.Function > (DWORD)0x80000000 )
          pIteratingIAT->u1.Function = (DWORD)HookFn;
    }
  }

The only thing remaining is to restore the memory attributes, as nothing ever happened.


VirtualProtect( pIAT, sizeof(PVOID) * cFuncs, flOldProtect, &flDontCare);

Step 2:

Inside of the CreateInterface method, we start hooking into the COM interface by injecting our own CreateDevice function pointer into the Virtual function table (Vtbl), which it is returned in the ppvOut pointer of the original call.

 


  DirectInput8Create_Type OldFn = 
      (DirectInput8Create_Type)D3DHook.Functions[D3DFN_DirectInput8Create].OrigFn;
    HRESULT hr = OldFn( hinst, dwVersion, riidltf, ppvOut, punkOuter );

Resolve the pointer until you get the pointer to the Vtbl of the interface.

 

With this address, you have to again remove the memory protection before you can inject your function into the table and save the old function pointer for later use.

Inject your function pointer into the offset of the CreateDevice function pointer inside of the interface Vtbl and restore the memory protection.

 

As you can see, the CreateDevice is the 4th method of the DirectInput interface, which means the offset inside of the Vtbl is 0x0C (pointer(DWORD)*index 3).


typedef struct IDirectInput *LPDIRECTINPUT;

#if !defined(__cplusplus) || defined(CINTERFACE)
#define IDirectInput_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b)
#define IDirectInput_AddRef(p) (p)->lpVtbl->AddRef(p)
#define IDirectInput_Release(p) (p)->lpVtbl->Release(p)
#define IDirectInput_CreateDevice(p,a,b,c) (p)->lpVtbl->CreateDevice(p,a,b,c)
#define IDirectInput_EnumDevices(p,a,b,c,d) (p)->lpVtbl->EnumDevices(p,a,b,c,d)
#define IDirectInput_GetDeviceStatus(p,a) (p)->lpVtbl->GetDeviceStatus(p,a)
#define IDirectInput_RunControlPanel(p,a,b) (p)->lpVtbl->RunControlPanel(p,a,b)
#define IDirectInput_Initialize(p,a,b) (p)->lpVtbl->Initialize(p,a,b)
#else
#define IDirectInput_QueryInterface(p,a,b) (p)->QueryInterface(a,b)
#define IDirectInput_AddRef(p) (p)->AddRef()
#define IDirectInput_Release(p) (p)->Release()
#define IDirectInput_CreateDevice(p,a,b,c) (p)->CreateDevice(a,b,c)
#define IDirectInput_EnumDevices(p,a,b,c,d) (p)->EnumDevices(a,b,c,d)
#define IDirectInput_GetDeviceStatus(p,a) (p)->GetDeviceStatus(a)
#define IDirectInput_RunControlPanel(p,a,b) (p)->RunControlPanel(a,b)
#define IDirectInput_Initialize(p,a,b) (p)->Initialize(a,b)
#endif


After we know where to inject it, we can start thinking about the implementation. When you look at the declaration of CreateDevice in the dinput.h, it does not match up with the declaration you see in the DirectX Help.


HRESULT CreateDevice(      

    REFGUID rguid,
    LPDIRECTINPUTDEVICE *lplpDirectInputDevice,
    LPUNKNOWN pUnkOuter
);


As you can see in the definition inside the dinput.h, you have to add a 4th parameter, which is the interface pointer. This ends up in the following function declaration.


HRESULT __stdcall   PASCAL MyCreateDevice(LPVOID *ppvOut,REFGUID rguid,
    LPDIRECTINPUTDEVICE *lplpDirectInputDevice,
    LPUNKNOWN pUnkOuter

This is important. You have to make sure, to use the __stdcall calling convention in your declaration, since if you refer to the MSDN.

 

The __stdcall calling convention is used to call Win32 API functions. The callee cleans the stack. __cdecl is the default calling convention for C and C++ programs. The stack has to be cleaned up by the caller, which is not the case with our function.

When you look at the disassembly of this call, you can see the stack pointer verification function _RTC_CheckEsp is called after the call to the interface function.


if (lpdi->CreateDevice(GUID_SysKeyboard, &lpdikey, NULL)!=DI_OK)
00401365  mov         esi,esp 
00401367  push        0    
00401369  push        offset lpdikey (4552C8h) 
0040136E  push        offset _GUID_SysKeyboard (44643Ch) 
00401373  mov         eax,dword ptr [lpdi (4552C4h)] 
00401378  mov         ecx,dword ptr [eax] 
0040137A  mov         edx,dword ptr [lpdi (4552C4h)] 
00401380  push        edx  
00401381  mov         eax,dword ptr [ecx+0Ch] 
00401384  call        eax  
00401386  cmp         esi,esp 
00401388  call        _RTC_CheckEsp (4026A0h) 
0040138D  test        eax,eax 
0040138F  je          Game_Init+78h (401398h) 
   return(0);
00401391  xor         eax,eax 
00401393  jmp         Game_Init+107h (401427h) 

// set cooperation level
if (lpdikey->SetCooperativeLevel(main_window_handle,

If you forget to declare your function with __stdcall your function will process fine, but you will fail the esp pointer test of this function which sets the eax and test it after the function call.

 

Step 3:

When you now create the device, the call is re-routed to your CreateDevice function.

After you call the original function, you receive a new pointer in lplpDirectInputDevice,

which will direct you to the Vtbl of the device.

 

HRESULT hr = OldCreateDev(ppvOut,rguid,lplpDirectInputDevice,pUnkOuter);

In my sample, I replaced the GetDeviceState, since I can add additional input in the return data of the calling function. To get to the offset, you have to look at the definition inside the DInput.dll. You see that the GetDeviceState function is the 10th method inside of the Device, which leads to an offset of 0x24.

After you know the offset, we can proceed with the instructions in step 2 to remove the protection, store the old pointer, inject your own function, and restore the memory protection.

 

Step 4:

When the GetDeviceState function is called by the target program, the injected function is called and you can manipulate the data as you wish.

About Martin Mueller

DXInput来做的键盘HOOK的软件源码 DXInput来做的键盘HOOKVB很实用的源码,精心挑选的精品源码 立即下载

相关推荐

国外大牛写的HOOK

国外大牛写的HOOK技术的程序,可以完成HOOK功能

Hook DirectInput->CreateDevice->GetDeviceData解决方案

已解决,来人散分了,Hook DirectInput->CreateDevice->GetDeviceData 在一款使用DirectInput的3D游戏里面,通过Hook DirectInput8Create函数,CreateDevice方法,取得了GetDeviceData函数指针,在GetDeviceData里面已经可以取得键盘输入的数据(程序未使用GetDeviceSta...

xuq09的专栏 3229

DirectInput & Xinput - 遊戲手把控制器範例

DirectInput & Xinput - 遊戲手把控制器範例

对DirectX/COM接口的挂钩

 对DirectX/COM接口的挂钩           一般的挂钩(Hook)都是针对Windows API或消息的,而本文要讲的是如何挂钩一个DirectX/COM接口,有意思吧,请往下看,文中以DirectInput作为范例进行讲解。           目标         相比挂钩一个API调用,拦截一个COM接口的方法需要做更多的工作,如果我们要拦截的D

谢启东的专栏 7919

DirectInput使用例程

一个使用dxsdk下的DirectInput实例,vc6.0+dxsdk8.0编译通过,欢迎大家下载学习交流!

python模拟鼠标点击和键盘输入的操作

所有代码都是网上百度出来的,通过个人实践找到适合自己的。 采用的python 库是 pymouse、pykeyboard 安装时直接pip安装的,pip install PyUserInput 安装不成功的可以参照http://www.lfd.uci.edu/~gohlke/pythonlibs/这个地址,内容很全 实现了一个最简单的输入密码,enter进入的登录过程 如果想遍历,可以使用itertools 包,很有用,具体用法这里不介绍了。 · 鼠标点击 有m.click(x, y, button, n)  — 点击,想x,y坐标,button:-1表示左键,-2表示右键,n:默认1次,2

Direct3DHook 源码深度剖析2:GetVTblAddresses 如何定位 COM 接口函数指针

Direct3DHook 是一个基于 Direct3D API 钩子实现游戏截图与实时叠加层(Overlay)的开源工具,支持 DirectX 9/10/11。上一篇文章我们讲了整体架构,本篇聚焦最核心的一个函数——`GetVTblAddresses`:它只有短短几行代码,却回答了"Direct3DHook 如何精准找到 Present、EndScene 等接口函数并替换它们"这个关键问题。读懂它

gitblog_00689的博客 390

Hooking_DirectX_COM_demo_HCK_ko_DEMO_

invalid description

ScriptHookV:打造专属GTA V模组世界的开源钩子框架

ScriptHookV是一款专为《侠盗猎车手V》(GTA V)设计的开源钩子框架,它允许开发者在离线环境下加载和运行自定义模组,为游戏注入无限创意。通过深入游戏引擎底层,ScriptHookV提供了强大的脚本注入能力和原生函数调用接口,让技术爱好者和创意开发者能够完全掌控游戏世界,实现从简单参数调整到复杂游戏机制改造的全方位定制。 ## 核心理念与价值主张 ScriptHookV的核心设计理念

gitblog_07817的博客 458

SAP 财务快速记账之—G/L Item Fast Entry

SAP FICO 运维解决方案

lijinbao171022的博客 1652

做量化没有实时数据怎么行?我找到一个超级好用的Python库,速度还贼快!

今天咱们介绍了 pytdx 这个神奇的库,重点包括:✅ 安装 pytdx✅ 连接 TDX 服务器✅ 获取实时行情数据✅ 获取 K 线数据✅ 获取分笔成交数据总之,pytdx 是一个速度快、免费、易用的量化利器,适合 A 股量化交易的同学们使用。如果你还在为数据发愁,赶紧试试吧!🚀顺手再看就是对花姐最大的支持,期待你的关注。

奔跑的蜗牛 5762

ScriptHookV深度解析:构建GTA V高级游戏模组的开源脚本注入引擎

ScriptHookV是一款专为《侠盗猎车手V》(GTA V)设计的开源脚本注入引擎,它为技术开发者和高级用户提供了在不修改游戏原始文件的前提下,扩展游戏功能、创建自定义模组的强大能力。作为一款成熟的游戏模组框架,ScriptHookV通过创新的脚本注入技术和模块化架构,让开发者能够安全、高效地构建独立的脚本模组,为GTA V社区带来了无限的游戏玩法创新可能。 ## 核心机制解析:脚本注入引擎的

gitblog_00404的博客 736

用Python实现图片风格迁移,让你的图片更加的高逼格!

先来看下效果: 上图是老王在甘南合作的米拉日巴佛阁外面拍下的一张照片,采用风格迁移技术后的效果为: 一些其它效果图: 下面进入正题。 近年来,由深度学习所引领的人工智能(AI)技术浪潮,开始越来越广泛地应用到生活各个领域。这其中,人工智能与艺术的交叉碰撞,在相关技术领域和艺术领域引起了高度关注。就在上个月,由电脑生成的艺术品在佳士得(Christie’s)的拍卖价竟高达43.25万美元,...

Python追梦 6190

深度解析Dalamud:FFXIV插件框架的架构设计与最佳实践

Dalamud作为《最终幻想14》(FFXIV)的插件框架和API,为游戏开发者提供了强大的扩展能力。这个开源项目通过模块化设计、安全的内存管理和高效的UI渲染系统,解决了传统游戏插件开发中的诸多痛点。本文将深入分析Dalamud的架构设计思想、核心模块实现原理,并提供实际应用场景和性能优化建议,帮助开发者充分掌握这一专业级插件框架。 **核心关键词:** FFXIV插件框架、内存安全机制、Im

gitblog_00248的博客 283

台积电技术研发实力

台积电技术研发实力 台积电在半导体行业的地位毋庸置疑。他们究竟有多强大,大部分读者了解得可能非常片面。从最新公布的2019年财报里,看看台积电的真正实力。 一、台积电在市场上的地位 根据公司财报,台积公司在先进制程技术、特殊制程技术,以及先进封装技术的发展上持续领先全球集成电路制造服务领域,2019年的市场占有率为52%。台积公司总体营收以地区划分(主要依据客户营运总部所在地),来自北美市场的营收占台积公司总体营收的60%、日本与中国大陆以外的亚太市场占9%、中国大陆市场占20%、欧洲、中东及非洲市场占6

吴建明wujianming_110117 1917

SPI为什么不需要加上拉电阻

大家好,我是记得诚。I2C因为其开漏的结构,无法输出高电平,所以外部加上拉电阻。我们在使用SPI的时候,发现是不需要加上拉电阻的。主要因为SPI多为推挽的结构,推挽电路,高低电平都可以输出,所以外部不需要加上拉电阻。推挽电路可以用2个三极管或者2个MOS管搭建而成。

记得诚 2644

Dalamud终极指南:重新定义FFXIV插件开发体验

还在为《最终幻想XIV》插件开发的复杂性而头疼吗?想象一下,当你试图为游戏添加一个简单的功能增强时,却需要处理内存注入、游戏版本适配、UI渲染等一系列底层技术细节。Dalamud框架正是为了解决这些痛点而生,它为FFXIV插件开发提供了一个完整的解决方案,让开发者能够专注于功能创新而非底层技术。 ## 从挣扎到优雅:插件开发者的进化之路 **传统开发模式**:每个插件开发者都需要重复造轮子,处

gitblog_01006的博客 713

R语言生存分析之竞争风险模型

R语言生存分析之竞争风险模型 竞争风险模型(Competing Risk Model)是一种处理多种潜在结局生存数据的分析方法,早在1999年Fine和Gray就提出了部分分布的半参数比例风险模型,通常使用的终点指标是累积发生率函数(cumulative incidence function,CIF)。 研究中结局事件可能有多个,某些结局将阻止感兴趣事件的出现或影响其发生的概率,各结局事件形成“竞争”关系,互为竞争风险事件。 竞争风险模型( Competing Risk Model ) : 指

data+scenario+science+insight 5251

DouK-Downloader 上手指南:3 步把抖音视频批量存到本地

DouK-Downloader(仓库 TikTokDownloader)是一款完全开源、基于 HTTPX 的抖音 / TikTok 作品下载与数据采集工具:把链接或账号主页粘进去,视频、图集、音乐就按你定好的命名规则落到本地,作品字段还能同步导出成表格。它支持终端交互、后台监听、Web API 三种运行形态,采集数据可写入 CSV / XLSX / SQLite。 ## 先把一个链接里的视频存下

gitblog_01121的博客 240

RK3588开发笔记-MIPI-CSI接口视频解码芯片XS9922B调试

Rockchip RK3588 是一款高性能的处理器,支持多种摄像头接口,包括 MIPI(Mobile Industry Processor Interface)接口。MIPI是一种用于移动设备和嵌入式系统中的摄像头、显示器和其他传感器设备的串行接口标准。MIPI是MIPI联盟发起的为移动应用处理器制定的开放标准。CSI-2(Camera Serial Interface 2)是MIPI 联盟定义的另一种高速接口,主要用于摄像模组和处理器之间的连接。

flypig has a dream! 6870

yolov26改进 | Neck篇 | 利用Gold-YOLO改进yolov26对小目标检测能力(全网独家首发)

本文介绍利用Gold-YOLO的Neck改进YOLOv26,通过信息聚集—分发机制GD,先全局汇聚不同层级特征,再将融合后的全局信息重新注入各尺度分支,增强浅层细节与深层语义之间的交互,减少传统Neck逐层传递造成的信息损失。该结构虽然融合路径更长,但能够在不过度增加延迟的前提下提升多尺度目标检测能力,尤其适合大小目标并存、尺度变化明显和复杂背景场景。本文将讲解GD机制、Neck替换、模块注册与配置方法,并提供完整代码、配置文件及Codex修改思路,方便直接复现、实验对比和二次创新。

Snu77的博客 302

DirectX Input 键盘实现

// DInputKeyboard.h: interface for the CDInputKeyboard class.////////////////////////////////////////////////////////////////////////#pragma once#define DIRECTINPUT_VERSION 0x800#include

zgl7903的专栏 4205

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Windows编程 DirectInput 鼠标和键盘的输入

版本:VS2015 语言:C++   书的第八章是一些数学的知识,以及一个图形库的创建。数学知识是有必要看一看的,我这里就不做多的介绍了,图形库的话反正你现在的win7+系统上也运行不了,看看就好。因为虽然这本书(《Windows游戏编程大师技巧》)非常的经典,但是代码都是比较老的,很多都已经过时了不能运行,所以我们要明确我们的目的,学好基础知识,编写一下程序练练手,熟悉熟悉Direct的流程

暗光之痕的博客 1万+

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

上一篇: Starting a process from KernelMode
下一篇: Kport Direct Access I/O Ports under Win NT/2000/XP
iiprogram
博客等级 码龄25年 1068粉丝 1515原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址