如何在Python代码中使用IDA的反编译C函数?

问题描述 投票:1回答:1

我从IDA的反编译(F5)选项中获得了此C函数。

我想在我的Python程序中使用它,如何以最简单的方式来做到这一点?

__int64 __fastcall manipulateBeforSend(__int64 a1, int a2)
{
  int v2; // w0
  __int64 result; // x0
  int i; // [xsp+1Ch] [xbp-4h]

  for ( i = 0; i < a2 - 3; i += 4 )
    *(_DWORD *)(a1 + 4LL * (i / 4)) ^= 0xDEAD1337;// leet? XOR 
  while ( 1 )
  {
    result = (unsigned int)a2;
    if ( i >= a2 )
      break;
    LOBYTE(v2) = i & 3;
    if ( i <= 0 )
      v2 = -(-i & 3);
    *(_BYTE *)(a1 + i++) ^= 0xDEAD1337 >> 8 * v2;
  }
  return result;
}

@@ Marco Bonelli,您帮了我很多,谢谢!但我一直得到那些错误:

manipulate.c:1:0: warning: -fPIC ignored for target (all code is position independent)
 #include <stdint.h>
 ^
In file included from C:/TDM-GCC-64/x86_64-w64-mingw32/include/crtdefs.h:10:0,
                 from C:/TDM-GCC-64/x86_64-w64-mingw32/include/stdint.h:28,
                 from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/stdint.h:9,
                 from manipulate.c:1:
manipulate.c:4:17: error: two or more data types in declaration specifiers
 typedef int64_t __int64;
                 ^
manipulate.c:4:17: error: two or more data types in declaration specifiers
 typedef int64_t __int64;
                 ^
manipulate.c:4:1: warning: useless type name in empty declaration
 typedef int64_t __int64;
 ^
python c decompiling ida
1个回答
1
投票

这是一个“快速”解决方案:

  1. 首先,使用typedef定义IDA使用的类型:

    #include <stdint.h>
    
    typedef uint8_t _BYTE;
    typedef int64_t __int64;
    typedef uint32_t _DWORD;
    

    您还可以通过IDA通过“文件”->“产品文件”->“创建C头文件”来执行此操作,但是由于类型很少,因此在这种情况下,手工操作比较简单。

    ] >
  2. 然后用工作代码替换不需要的宏/值:

  3. __int64 __fastcall manipulateBeforSend(__int64 a1, int a2)
    // remove __fastcall:
    __int64 manipulateBeforSend(__int64 a1, int a2)
    
    
    LOBYTE(v2) = i & 3;
    // convert using a bit mask:
    v2 = (v2 & 0xffffff00) | (i & 3);
    
  4. 现在有问题:作为@Ctx makes us notice,您的C代码正在取消引用第一个参数,这很可能是因为它是uint32_t*指针,而不仅仅是int64

  5. *(_DWORD *)(a1 + 4LL * (i / 4))
    // and also 
    *(_BYTE *)(a1 + i++) ^= 0xDEAD1337 >> 8 * v2;
    

    您可能应该花更多时间对指针最初使用的东西进行逆向工程。要解决此问题,您可以创建一个伪数组并将其添加到您的C代码中以使其起作用,如下所示:

    static uint32_t fakearr[1024 * 1024] = {0};
    
    __int64 manipulateBeforSend(int a2)
    {
      uint32_t *a1 = fakearr;
      // ...
    

    当然,通常您可能希望使用一个实数数组,为此您可以看一下this answer

  6. 然后将所有(有效的)代码放入.c文件中:

  7. #include <stdint.h>
    
    typedef uint8_t _BYTE;
    typedef int64_t __int64;
    typedef uint32_t _DWORD;
    
    uint32_t fakearr[1024 * 1024] = {0};
    
    __int64 manipulateBeforSend(int a2)
    {
      uint32_t *a1 = fakearr;
    
      int v2; // w0
      __int64 result; // x0
      int i; // [xsp+1Ch] [xbp-4h]
    
      for ( i = 0; i < a2 - 3; i += 4 )
        *(_DWORD *)(a1 + 4LL * (i / 4)) ^= 0xDEAD1337;// leet? XOR
      while ( 1 )
      {
        result = (unsigned int)a2;
        if ( i >= a2 )
          break;
        v2 = (v2 & 0xffffff00) | (i & 3);
        if ( i <= 0 )
          v2 = -(-i & 3);
        *(_BYTE *)(a1 + i++) ^= 0xDEAD1337 >> 8 * v2;
      }
      return result;
    }
    
  8. 将代码编译为共享库:

  9. gcc -fPIC -shared -o mylib.so mylib.c
    
  10. 现在您可以使用ctypes module从Python加载它:

  11. ctypes
© www.soinside.com 2019 - 2024. All rights reserved.