- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试转换来自 CHook 的代码论坛发布了这段代码,它执行 EAT Hook :
#include <Windows.h>
#include <Psapi.h>
#include <string>
#if PSAPI_VERSION == 1
#pragma comment(lib, "Psapi.lib")
#endif
template <typename DestType, typename SrcType>
DestType* ByteOffset(SrcType* ptr, ptrdiff_t offset)
{
return reinterpret_cast<DestType*>(reinterpret_cast<unsigned char*>(ptr) + offset);
}
bool eat_hook(void* old_function, void* new_function)
{
HMODULE hModule;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)old_function, &hModule);
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS NtHeader = ByteOffset<IMAGE_NT_HEADERS>(DosHeader, DosHeader->e_lfanew);
if (IMAGE_NT_SIGNATURE != NtHeader->Signature)
{
MessageBox(0, "Bad NT header signature", "Error", 0);
return false;
}
PIMAGE_EXPORT_DIRECTORY ExportDirectory = ByteOffset<IMAGE_EXPORT_DIRECTORY>(DosHeader,
NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
DWORD* functions = ByteOffset<DWORD>(DosHeader, ExportDirectory->AddressOfFunctions);
for (size_t i = 0; i < ExportDirectory->NumberOfFunctions; ++i)
{
if (functions[i] == (DWORD)old_function - (DWORD)hModule)
{
DWORD oldProtection;
if (!VirtualProtect(functions + i, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtection))
{
MessageBox(0, "VirtualProtect failed", "Error", 0);
return false;
}
functions[i] = reinterpret_cast<DWORD>(new_function) - reinterpret_cast<DWORD>(DosHeader);
if (!VirtualProtect(functions + i, sizeof(DWORD), oldProtection, &oldProtection))
{
MessageBox(0, "VirtualProtect failed", "Error", 0);
return false;
}
return true;
}
}
return false;
}
bool iat_hook(void* old_function, void* new_function)
{
HMODULE hModule;
DWORD sizeNeeded;
if (0 == EnumProcessModules(GetCurrentProcess(), &hModule, sizeof(hModule), &sizeNeeded))
{
MessageBox(0, "EnumProcessModules failed", "Error", 0);
return false;
}
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)hModule;
PIMAGE_NT_HEADERS NtHeader = ByteOffset<IMAGE_NT_HEADERS>(DosHeader, DosHeader->e_lfanew);
if (IMAGE_NT_SIGNATURE != NtHeader->Signature)
{
MessageBox(0, "Bad NT header signature", "Error", 0);
return false;
}
PIMAGE_IMPORT_DESCRIPTOR ImportDirectory = ByteOffset<IMAGE_IMPORT_DESCRIPTOR>(DosHeader,
NtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
for (size_t i = 0; ImportDirectory[i].Characteristics; ++i)
{
PIMAGE_THUNK_DATA thunk = ByteOffset<IMAGE_THUNK_DATA>(hModule, ImportDirectory[i].FirstThunk);
PIMAGE_THUNK_DATA origThunk = ByteOffset<IMAGE_THUNK_DATA>(hModule, ImportDirectory[i].OriginalFirstThunk);
for (; origThunk->u1.Function; origThunk++, thunk++)
{
if (thunk->u1.Function == (DWORD)old_function)
{
DWORD oldProtection;
if (!VirtualProtect(&thunk->u1.Function, sizeof(DWORD), PAGE_EXECUTE_READWRITE, &oldProtection))
{
MessageBox(0, "VirtualProtect failed", "Error", 0);
return false;
}
thunk->u1.Function = reinterpret_cast<DWORD>(new_function);
if (!VirtualProtect(&thunk->u1.Function, sizeof(DWORD), oldProtection, &oldProtection))
{
MessageBox(0, "VirtualProtect failed", "Error", 0);
return false;
}
return true;
}
}
}
return false;
}
bool hook(void* old_function, void* new_function)
{
return eat_hook(old_function, new_function) && iat_hook(old_function, new_function);
}
从 C++ 到 Delphi,但我在 var 声明方面遇到了问题,特别是“函数”var。
这是我的 Delphi 转换的不完整代码:
function eat_hook(old_function, new_function:pointer):boolean;
var
Module: HMODULE;
DosHeader: PImageDosHeader;
NtHeaders: PImageNtHeaders;
ExportDirectory: PImageExportDirectory;
functions: PDWORD;
i: size_t;
oldProtection: DWORD;
begin
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, pointer(old_function), Module);
DosHeader := PImageDosHeader(Module);
NTHeaders := PImageNtHeaders(DWORD(DOSHeader) + DWORD(DOSHeader^._lfanew));
if IMAGE_NT_SIGNATURE <> NtHeaders.Signature then begin
MessageBox(0, 'Bad NT header signature', 'Error', 0);
exit;
end;
ExportDirectory := PImageExportDirectory(PAnsiChar(DosHeader) + NtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
functions := PDWORD(PAnsiChar(DosHeader)+dword(ExportDirectory.AddressOfFunctions));
for i:=0 to ExportDirectory.NumberOfFunctions do begin
if not VirtualProtect(functions, sizeof(dword), PAGE_EXECUTE_READWRITE, @oldProtection) then begin
MessageBox(0, 'VirtualProtect failed', 'Error', 0);
exit;
end;
functions[i] := DWORD(new_function) - DWORD(DosHeader);
if not VirtualProtect(pointer(functions), sizeof(dword), oldProtection, @oldProtection) then begin
MessageBox(0, 'VirtualProtect failed', 'Error', 0);
exit;
end;
end;
end;
尝试分配给 functions[i]
的行导致编译错误:
[DCC Error]: E2016 Array type required
我该如何解决这个问题?
最佳答案
您可以利用以下事实:您正在按顺序写入数组 functions
并递增指针,而不是使用数组索引。
functions := PDWORD(PAnsiChar(DosHeader)+dword(ExportDirectory.AddressOfFunctions));
for i := 0 to ExportDirectory.NumberOfFunctions-1 do begin
if not VirtualProtect(functions, sizeof(dword), PAGE_EXECUTE_READWRITE, @oldProtection) then begin
MessageBox(0, 'VirtualProtect failed', 'Error', 0);
exit;
end;
functions^ := DWORD(new_function) - DWORD(DosHeader);
if not VirtualProtect(functions, sizeof(dword), oldProtection, @oldProtection) then begin
MessageBox(0, 'VirtualProtect failed', 'Error', 0);
exit;
end;
inc(functions);
end;
这里的技巧是每次循环时 functions
都指向数组中的第 ith 项。每次迭代完成后,inc(functions)
将指针前进到下一项,为下一次迭代做好准备。
我还更正了您的 for
循环。在您的 Delphi 代码中,您执行的一次迭代过多。
关于c++ - 将 VirtualProtect EAT Hook 例程从 C 转换为 Delphi 的 Delphi 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9484319/
我想将一个字符串分成两组。该字符串的结构非常简单,但我无法使其工作。 txt library(stringr) >str_match(txt, "([a-zA-Z]*)([0-9].*)")
介绍性胡言乱语: 我当前的项目有点像 Lisp 解析器,而 RegEx 确实是一个奇迹,尽管它在这个特定的函数中让我有点头疼: 该函数应该做什么: 接收一个包含方程的字符串并返回它的格式,以便解析器可
解析代码 $str = 'My name is Michael. I am a sportsman!'; preg_match('|My name is (.*?)\. I am a (.*?)|',
解析代码 $str = 'My name is Michael. I am a sportsman!'; preg_match('|My name is (.*?)\. I am a (.*?)|',
之后: String content = "Some \n multiline \n content"; //actually, content comes from request (tomcat
我编写了这段代码,通过更改 IAT 和 EAT 中的地址来 Hook API 函数:http://pastebin.com/7d9N1J2c 当我想 Hook “recv”或“connect”时,这很
如何处理 TestNG 中的预期异常,以便无论测试方法中的代码是否抛出异常,测试都会通过 我有 testng 测试方法,它创建某些对象的列表。在 for 循环中,针对每个对象执行特定操作。此操作可能会
我在 shell 变量中有一些多行字符串。字符串的所有行都有至少几个空白字符的未知缩进级别(在我的示例中为 8 个空格,但可以是任意的)。例如,让我们看一下这个示例字符串: I am
我正在使用官方 Sybase JDBC 驱动程序连接到数据库并通过创建 CallableStatement、将参数绑定(bind)到它并在其上调用 .execute() 来调用存储过程。 但是,我发现
我正在开发一个抽屉,用于在我的应用程序的屏幕之间导航。我有一个主屏幕,其标签为“Home”,在编译应用程序时Drawer仅显示“Hom”而不显示全名(Home) 环境: react 原生0.60 re
我使用 for 循环将请求发送到 http://www.example.com : for(int i = 0; i .我将这个 10 槽函数连接到信号 finished(QNetworkR
我有一些学校的 C 练习练习,从昨天开始我就一直在做这些练习,但我对某个特定的练习非常生硬。 在这个程序中,它必须重复最多 5 次或直到用户未能回答“安全问题”: 必须要求用户输入密码(最初是“abc
据我所见,当您向下滚动时,下面的 div 会吞噬上面的图像,从而创建我认为不错的视觉效果。 我唯一的问题是我不知道搜索有关该过程本身的教程的术语是什么。 有人知道我会使用什么搜索词来找到好的教程吗?我
我有一个 UI 线程和另一个线程,我想使用 PostMessage() 从另一个 trhead 向 UI 线程发送一条用户定义的消息。 如果UI线程显示一个消息框,然后我发送用户自定义消息,消息框的消
我遇到了一个(至少对我而言)非常的奇怪情况。 我正在尝试重写蛇,并且移动进行得非常顺利,只有蛇在吃自己,虽然我正在删除 1 段并添加 1 (x + 1 - 1 = x?) 但是蛇消失了,而 Array
我将如何进行导出地址表 (EAT) Hook ? 我能够进行 IAT(导入地址表) Hook 。 有人能给我举个例子吗? 谢谢! 最佳答案 EAT Hook 遵循与 IAT Hook 非常相似的方法,
我所说的“吃”是指:当 Sprite A(马里奥)与 Sprite B(一枚硬币)发生碰撞时,会检测到碰撞并将硬币从场景中移除;然而,马里奥的运动并没有因为与硬币的碰撞而改变。 目前我正在使用 SKP
由于我的其他错误已解决,我针对此错误发布了一个新问题。 我制作了一个贪吃蛇 Canvas 游戏,但是当您同时按下两个按钮时我的蛇往往会吃掉自己。我不确定如何正确解释它,但事情就是这样: 假设我的蛇向左
首先这是我正在使用的: KDE neon 用户版 5.20 (Ubuntu 20.04) symfony CLI v4.21.3 php v7.4.3 PostgreSQL v12.5 Apache2
Closed. This question is opinion-based。它当前不接受答案。 想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 已关闭6年。
我是一名优秀的程序员,十分优秀!