使用 Assembly 和 C++ 调用函数两次

发布于 2024-09-09 08:10:39 字数 980 浏览 5 评论 0原文

我有一段代码将要调用的函数更改为我的新函数,但我不想只调用我的新函数,我还想调用旧函数。 这是一个例子,所以你可以理解我的意思:

如果我反汇编我的.exe,我会看看这部分:

L00123456:
      mov   eax, [L00654321] //doesn't matter
      mov   ecx, [eax+1Ch]   //doesn't matter
      push  esi              //the only parameter
0x123 call  SUB_L00999999    //this is the function I wanna overwrite
      //...

(0x123是该行的地址) 所以,我使用了这段代码:

DWORD old;
DWORD from = 0x123;
DWORD to   = MyNewFunction;
VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &old);

DWORD disp = to - (from + 5);
*(BYTE *)(from) = 0xE8;
*(DWORD *)(from + 1) = (DWORD)disp;

现在,它不再调用 SUB_L00999999,而是调用 MyNewFunction...

那么... 关于如何仍然调用旧函数的任何想法?

我尝试了类似的方法(以多种方式),但它使我的应用程序崩溃:

int MyNewFunction(int parameter)
{
    DWORD oldfunction = 0x00999999;
    _asm push parameter
    _asm call oldfunction
}

注意:我使用 Visual Studio C++ 2010,这些代码位于 .exe 中加载的 .dll 中。

谢谢。

I have a code that changes the function that would be called, to my new function, but I don't want to call only my new function, I also want to call the old one.
This is an example, so you can understand what I'm saying:

If I disassemble my .exe, I will look at this part:

L00123456:
      mov   eax, [L00654321] //doesn't matter
      mov   ecx, [eax+1Ch]   //doesn't matter
      push  esi              //the only parameter
0x123 call  SUB_L00999999    //this is the function I wanna overwrite
      //...

(0x123 is the address of that line)
So, I used this code:

DWORD old;
DWORD from = 0x123;
DWORD to   = MyNewFunction;
VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &old);

DWORD disp = to - (from + 5);
*(BYTE *)(from) = 0xE8;
*(DWORD *)(from + 1) = (DWORD)disp;

Now, instead of calling SUB_L00999999, it calls MyNewFunction...

So... any ideas on how can I still call the old function?

I tried things like this (in many ways), but it crashes my application:

int MyNewFunction(int parameter)
{
    DWORD oldfunction = 0x00999999;
    _asm push parameter
    _asm call oldfunction
}

Notes: I use Visual Studio C++ 2010 and these codes are in a .dll loaded in an .exe.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2024-09-16 08:10:39

不久前我遇到了这样的问题。不管怎样,_asm call dword ptr [oldfunction]对我有用。

I had a problem like this a while back. Anyway, _asm call dword ptr [oldfunction] worked for me.

喵星人汪星人 2024-09-16 08:10:39

ret 期望栈顶参数是要返回的地址。您可以通过将旧函数地址压入新函数中的 ret 指令之前的堆栈来利用此漏洞。当调用返回(或者更确切地说,分支到旧函数)时,堆栈指针将移动以将原始返回地址(此处为 0x128)保留在顶部,因此堆栈将显示未损坏。 (如果你不绕路的话应该是一样的)。

ret expects the top stack argument to be the address to return to. You can exploit this by pushing the oldfunction address onto the stack immediately before your ret instruction in your new function. As the call returns (or rather, branches to the oldfunction), the stack pointer will shift to leave the original return address (0x128 here) on top, so the stack will appear undamaged. (same as it should have been had you not taken a detour).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文