如何使用 typedef 或内联在 C++ 中调用此 x86 ASM CALL

How can I call this x86 ASM CALL in C++ with typedef or inline

我在 5 个不同的地方发现了这个调用 sub_10636F0 试图找出如何从注入目标应用程序的 C++ DLL 中调用它所以它有完整访问该应用程序中的所有调用。

我有一张图表,上面列出了所有被调用的地方,其中大部分调用都在其上方的另一个调用之后被截断,以确保其完整性。

我在 Whosebug 上阅读了大量关于这个主题的问题,从 Necrolis 那里找到了一些很好的答案,说如果它是 EDX 那么你可以使用 __fastcall。 我在谷歌上搜索了 ECX,它似乎也用于 __fastcall 所以 ECX 或 EDX 的意思是 __fastcall.

但是它调用的函数使用了包装器

sub     esp, 5F4h
add     esp, 5F4h
retn    8

我不知道这是关于再次进行大量研究的,我认为开头的 SUB ESP, XXX 和结尾的 ADD ESP, XXX 仅用于 _cdecl 转换

我当前的代码如下所示

typedef void(__fastcall *TThreeParamter)(int, int, int);
typedef void(__fastcall *TTwoParamter)(int, int);
typedef void(__fastcall *TOneParamter)(int);
typedef void(__fastcall *TZeroParamter)();

TTwoParamter sub_10636F0 = (TTwoParamter)(DWORD)GetModuleHandle(NULL) + 0xC636EF;

//the call
sub_10636F0(0x11223344, 0x55667788);


不要问我为什么 0xC636EF 与 sub 中的 10636F0 不同,我可以告诉你它在调试器中检查后进入了正确的 sub,每次程序重新启动时 sub 都会继续移动要么是一种保护方法,要么可能是因为该程序加载了 50 多个 dll,并且地址需要四处移动。

我尝试了所有不同的配置,2 int 的,3 int 的都没用..


IDA 检测到此方法有 3 个参数,但反编译的伪代码中没有使用最后一个参数,我无法弄清楚,

伪代码看起来像这样(我对其进行了大量修改,比如将其从 __thiscall 更改为 _fastcall)

Pseudo-code from IDA

//probably wrong.. packet is a variable not a parameter which will crash
void __fastcall sub_10636F0(int var1)
{
  __int128 v1; // xmm0@0
  int v2; // esi@1
  int v3; // ebx@1
  SOCKET v4; // ebp@1
  int v5; // eax@2
  int v6; // ecx@3
  int v7; // [sp+8h] [bp-5FCh]@7
  char a2a[1492]; // [sp+10h] [bp-5F4h]@2
  int v9; // [sp+5E4h] [bp-20h]@2
  int v10; // [sp+5E8h] [bp-1Ch]@2
  struct _FILETIME SystemTimeAsFileTime; // [sp+5F0h] [bp-14h]@2
  __int16 v12; // [sp+5F8h] [bp-Ch]@2
  int packet; // [sp+608h] [bp+4h]@0
  int to; // [sp+60Ch] [bp+8h]@0

  v2 = to;
  v3 = var1;
  v4 = *(_DWORD *)(packet + 220);
  if ( v4 != -1 )
  {
    //snipped lots of code
  }
}

//probably wrong.. packet is a variable not a paramter which will crash
void __fastcall sub_10636F0(int var1, int var2)
{
  __int128 v2; // xmm0@0
  int v3; // esi@1
  int v4; // ebx@1
  SOCKET v5; // ebp@1
  int v6; // eax@2
  int v7; // ecx@3
  int v8; // [sp+8h] [bp-5FCh]@7
  char a2a[1492]; // [sp+10h] [bp-5F4h]@2
  int v10; // [sp+5E4h] [bp-20h]@2
  int v11; // [sp+5E8h] [bp-1Ch]@2
  struct _FILETIME SystemTimeAsFileTime; // [sp+5F0h] [bp-14h]@2
  __int16 v13; // [sp+5F8h] [bp-Ch]@2
  int packet; // [sp+608h] [bp+4h]@0
  int to; // [sp+60Ch] [bp+8h]@0

  v3 = to;
  v4 = var1;
  v5 = *(_DWORD *)(packet + 220);
  if ( v5 != -1 )
  {
    //snipped lots of code
  }
}

//this looks the best, but still `to` isn't detected as paramter

void __fastcall sub_10636F0(int var1, int var2, int var3)
{
  __int128 v3; // xmm0@0
  int v4; // esi@1
  int v5; // ebx@1
  SOCKET v6; // ebp@1
  int v7; // eax@2
  int v8; // ecx@3
  int v9; // [sp+8h] [bp-5FCh]@7
  char a2a[1492]; // [sp+10h] [bp-5F4h]@2
  int v11; // [sp+5E4h] [bp-20h]@2
  int v12; // [sp+5E8h] [bp-1Ch]@2
  struct _FILETIME SystemTimeAsFileTime; // [sp+5F0h] [bp-14h]@2
  __int16 v14; // [sp+5F8h] [bp-Ch]@2
  int to; // [sp+60Ch] [bp+8h]@0

  v4 = to; //still doesn't detect this..
  v5 = var1; //okay this isn't bad another parameter
  v6 = *(_DWORD *)(var3 + 220); //like this detects this as parameter class atleast 
  if ( v6 != -1 )
  {
    //snipped lots of code
  }
}

这是 IDA 默认推荐的代码

char __userpurge sub_10636F0@<al>(int a1@<ecx>, __int128 a2@<xmm0>, int a3, int a4)
{
  int v4; // esi@1
  int v5; // ebx@1
  SOCKET v6; // ebp@1
  int v7; // eax@2
  int v8; // ecx@3
  int v9; // eax@8
  char v11; // [sp+8h] [bp-5FCh]@7
  int v12; // [sp+10h] [bp-5F4h]@4
  int v13; // [sp+24h] [bp-5E0h]@2
  int v14; // [sp+28h] [bp-5DCh]@2
  int v15; // [sp+2Ch] [bp-5D8h]@2
  int v16; // [sp+30h] [bp-5D4h]@2
  char v17; // [sp+34h] [bp-5D0h]@2
  signed int v18; // [sp+5E4h] [bp-20h]@2
  int v19; // [sp+5E8h] [bp-1Ch]@2
  int v20; // [sp+5F0h] [bp-14h]@2
  __int16 v21; // [sp+5F8h] [bp-Ch]@2

  v4 = a4;
  v5 = a1;
  v6 = *(_DWORD *)(a3 + 220);
  if ( v6 == -1 )
    return 0;

   //Snipped code

  if ( v9 >= 0 && v9 == *(_DWORD *)(v4 + 1492) )
    return 1;
  return 0;
}



Function in ASM

.text:010636F0 ; void __fastcall sub_10636F0(int var1, int var2, int var3)
.text:010636F0 sub_10636F0     proc near               ; CODE XREF: sub_1062960+E0p
.text:010636F0                                         ; sub_10637E0+D4p ...
.text:010636F0
.text:010636F0 a2              = byte ptr -5F4h
.text:010636F0 var_20          = dword ptr -20h
.text:010636F0 var_1C          = dword ptr -1Ch
.text:010636F0 SystemTimeAsFileTime= _FILETIME ptr -14h
.text:010636F0 var_C           = word ptr -0Ch
.text:010636F0 var_4           = dword ptr -4
.text:010636F0 packet          = dword ptr  4
.text:010636F0 to              = dword ptr  8
.text:010636F0 test            = dword ptr  0Ch
.text:010636F0
.text:010636F0                 sub     esp, 5F4h
.text:010636F6                 mov     eax, ___security_cookie
.text:010636FB                 xor     eax, esp
.text:010636FD                 mov     [esp+5F4h+var_4], eax
.text:01063704                 push    ebx
.text:01063705                 push    ebp             ; a5
.text:01063706                 push    esi             ; a4
.text:01063707                 mov     esi, [esp+600h+to]
.text:0106370E                 push    edi             ; a3
.text:0106370F                 mov     edi, [esp+604h+packet]
.text:01063716                 mov     ebx, ecx
.text:01063718                 mov     ebp, [edi+0DCh]
.text:0106371E                 cmp     ebp, 0FFFFFFFFh
.text:01063721                 jz      loc_10637BB
        SNIP TONS OF CODE HERE
.text:010637BB
.text:010637BB loc_10637BB:                            ; CODE XREF: sub_10636F0+31j
.text:010637BB                                         ; sub_10636F0+BDj ...
.text:010637BB                 xor     al, al
.text:010637BD
.text:010637BD loc_10637BD:                            ; CODE XREF: sub_10636F0+C9j
.text:010637BD                 mov     ecx, [esp+604h+var_4]
.text:010637C4                 pop     edi
.text:010637C5                 pop     esi
.text:010637C6                 pop     ebp
.text:010637C7                 pop     ebx
.text:010637C8                 xor     ecx, esp
.text:010637CA                 call    @__security_check_cookie@4 ; __security_check_cookie(x)
.text:010637CF                 add     esp, 5F4h
.text:010637D5                 retn    8
.text:010637D5 sub_10636F0     endp ; sp-analysis failed



Calls to this function in ASM

.text:010638B0                 push    esi             ; packet
.text:010638B1                 push    ebx             ; this
.text:010638B2                 mov     ecx, ebp        ; this
.text:010638B4                 call    sub_10636F0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text:01062A2E                 mov     byte ptr [esi+5E9h], 1
.text:01062A35
.text:01062A35 loc_1062A35:                            ; CODE XREF: sub_1062960+C1j
.text:01062A35                 add     dword ptr [esi+5D4h], 2
.text:01062A3C                 push    esi             ; packet
.text:01062A3D                 push    edi             ; this
.text:01062A3E                 mov     ecx, ebx        ; this
.text:01062A40                 call    sub_10636F0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text:01063AF4                 mov     eax, [ebx+1128h]
.text:01063AFA                 mov     [esp+1A4h+var_AC], eax
.text:01063B01                 push    esi             ; packet
.text:01063B02                 lea     eax, [esp+1A8h+to]
.text:01063B06                 push    eax             ; this
.text:01063B07                 mov     ecx, ebx        ; this
.text:01063B09                 mov     [esp+1ACh+var_4], 1
.text:01063B14                 call    sub_10636F0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text:01089145 loc_1089145:                            ; CODE XREF: sub_10890B0+4Fj
.text:01089145                                         ; sub_10890B0+67j
.text:01089145                 mov     ecx, [edi+110h] ; this
.text:0108914B                 push    esi             ; packet
.text:0108914C                 push    edi             ; this
.text:0108914D                 call    sub_10636F0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.text:01089CBA                 mov     ecx, [esi+110h] ; this
.text:01089CC0                 push    edi             ; packet
.text:01089CC1                 push    esi             ; this
.text:01089CC2                 call    sub_10636F0

I have no idea what this is about again doing tons of research I think SUB ESP, XXX at beginning and ADD ESP, XXX at end are used only for _cdecl conversions

不,它用于所有使用局部变量的函数(关于它是如何完成的有微小的变化,但是堆栈 space 需要通过从 ESP 中减去来分配,而 "freed" 通过将相同的数量添加到堆栈指针。

但是,RET 8 确实表明调用约定不是 _cdecl,而是调用者清理堆栈的约定。有几个不同的 calling conventions 匹配这个,但我觉得它是 C++ 代码和一个成员函数,这将使它成为 thiscall - 这确实让它有点难以模拟,因为你想要thisECX 中。

ret 8 表示函数有 8 个字节的参数,所以两个 intvoid * 变量。

我不相信有一种简单的方法可以做到这一点。你也许可以做这样的事情。使用带有两个参数的虚函数创建 class X

class X
{
   virtual void Func(int x, int y) { }
};

然后找出编译器将 vtable 放在哪里,并修改 func 的 vtable 以指向您的目标函数,而不是 class 的空实现。

现在您可以使用X创建一个实例:

 X* p = new X;

然后调用func.

 p->func(1, 2);

但是,如果你运气不好,编译器并没有意识到你把vtable弄乱了,而直接调用了这个函数。所以你可能需要用单独的编译和其他东西做一些技巧。

换句话说,您的工作已经完成。但是如果你不需要花点时间,逆向工程就一点乐趣也没有了。

当然骗子的方法就是写几行内联汇编,像这样:

void CallMyFunc(void *func, int a, int b, int c)
{
    __asm(mov ecx, a
          push b
          push c
          call *func);
}

[距离我上次编写 Windows 内联汇编代码已经有 10 年了,如果语法不太正确,我们深表歉意 - 将其视为 "rough sketch" 并修改它直到它真正编译...]