gpt4 book ai didi

c - "Trapping"Windows用户空间进程自己的sysenter调用

转载 作者:可可西里 更新时间:2023-11-01 13:42:39 26 4
gpt4 key购买 nike

我正在 Windows 中开发一个运行时非 native 二进制翻译器,到目前为止,我已经能够通过使用一个丑陋的使用 Windows SEH 处理无效中断的 hack;但只是因为系统调用 vector 与 Windows 中的不同,所以我可以通过执行以下操作来捕获这些“软”异常:

static int __stdcall handler_cb(EXCEPTION_POINTERS* pes, ...)
{

if (pes->ExceptionRecord->ExceptionCode != EXCEPTION_ACCESS_VIOLATION)
return EXCEPTION_CONTINUE_SEARCH;

char* instruct = (char*) pes->ContextRecord->Eip;

if (!instruct)
handle_invalid_instruction(instruct);

switch (instruct[0])
{
case 0xcd: // INT
{
if (instruct[1] != 0x99) // INT 0x99
handle_invalid_instruction(instruct);
handle_syscall_translation();
...
}
...
default:
halt_and_catch_fire();
}
return EXCEPTION_SUCCESS;
}

它工作得很好(但很慢),问题是 Windows 首先尝试处理指令/中断,对于使用 sysenter/sysexit 而不是 int 0x99 的非 native 二进制文件,一些 systenter 指令在非- native 二进制文件实际上是有效的 NT 内核在执行时调用自身,这意味着我的处理程序永远不会被调用,更糟的是; “主机”操作系统的状态也受到损害。有没有办法在 Windows 中“捕获”sysenter 指令?我该怎么做呢?

最佳答案

据我所知,没有办法(从用户模式进程)“禁用”SYSENTER,因此执行它会产生异常。 (我假设您的程序不会尝试 SYSEXIT,因为只有 Ring 0 可以这样做)。

我认为你唯一的选择就是像 VirtualBox 那样做,扫描无效指令,用非法操作码或类似的东西替换它们,你可以捕获并模拟它们。参见 10.4. Details about software virtualization .

To fix these performance and security issues, VirtualBox contains a Code Scanning and Analysis Manager (CSAM), which disassembles guest code, and the Patch Manager (PATM), which can replace it at runtime.

Before executing ring 0 code, CSAM scans it recursively to discover problematic instructions. PATM then performs in-situ patching, i.e. it replaces the instruction with a jump to hypervisor memory where an integrated code generator has placed a more suitable implementation. In reality, this is a very complex task as there are lots of odd situations to be discovered and handled correctly. So, with its current complexity, one could argue that PATM is an advanced in-situ recompiler.

关于c - "Trapping"Windows用户空间进程自己的sysenter调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30175249/

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