【发布时间】:2020-09-30 15:37:22
【问题描述】:
我正在尝试使用 C++ 和 Detours 库挂钩用 Delphi 编写的程序的用户定义函数。 (DLL 注入)
但是,我无法挂钩它,因为 Delphi 和 C++ 的函数调用约定不匹配。
Delphi使用fastcall函数调用约定,C++也提供了fastcall函数调用约定。
然而,Delphi 的 fastcall 将其参数顺序存储在 EAX、EDX、ECX 和堆栈上,而 C++ 的 fastcall 将其参数顺序存储在 ECX、EDX 和堆栈上。 (这是因为fastcall没有标准。)
由于这些差异,我无法获取存储在 EAX 中的参数。
我该如何解决这个问题?
(本文已由谷歌翻译翻译。)
#include "pch.h"
typedef void(__fastcall* ORGFP)(char); //Prototype of function to hook (reverse engineering)
ORGFP originFunc1 = (ORGFP)((DWORD)GetModuleHandle(NULL) + 0x2B2F20); //Image base of target process + offset of function to hook
ORGFP originFunc2 = (ORGFP)((DWORD)GetModuleHandle(NULL) + 0x2B2A20);
DWORD WriteLog(LPCTSTR lpszFormat, ...) {
TCHAR szLog[512];
DWORD dwCharsWritten;
va_list args;
va_start(args, lpszFormat);
_vstprintf_s(szLog, 512, lpszFormat, args);
va_end(args);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), szLog, _tcslen(szLog), &dwCharsWritten, NULL);
return dwCharsWritten;
}
void __fastcall DetourFunc1(char on) {
WriteLog(TEXT("Function called : BlockInternet(%d)\n"), on);
return originFunc1(on);
}
void __fastcall DetourFunc2(char on) {
WriteLog(TEXT("Function called : BlockInputDevices(%d)\n"), on);
return originFunc2(on);
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
if (DetourIsHelperProcess())
return TRUE;
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
AllocConsole();
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)originFunc1, DetourFunc1);
DetourAttach(&(PVOID&)originFunc2, DetourFunc2);
DetourTransactionCommit();
break;
case DLL_PROCESS_DETACH:
FreeConsole();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)originFunc1, DetourFunc1);
DetourDetach(&(PVOID&)originFunc2, DetourFunc2);
DetourTransactionCommit();
break;
}
return TRUE;
}
#include "pch.h"
#ifndef PCH_H
#define PCH_H
#include "framework.h"
#include <stdio.h>
#include <stdarg.h>
#include <tchar.h>
#include <detours.h>
#endif
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
【问题讨论】:
-
需要一些组装。
-
@500 - 内部服务器错误 // 你能解释一下你需要什么程序集吗?
-
您需要编写代码为汇编中的调用准备参数。您无法让编译器执行此操作。所以你必须承担编译器的工作。那是如果您被限制使用 C++ 代码。你可以做的是使用一个小的 Delphi DLL 来为你处理
register调用。 -
@Remko // 这不是我想要的,但它帮助很大。谢谢!