我从 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;
^
最佳答案
这是一个“快速”解决方案:
首先,使用
typedef
定义IDA使用的类型:#include <stdint.h> typedef uint8_t _BYTE; typedef int64_t __int64; typedef uint32_t _DWORD;
您还可以通过 IDA 通过"file"->“生成文件”->“创建 C 头文件”来执行此操作,但由于类型很少,因此在这种情况下手动执行会更简单。
然后用工作代码替换不需要的宏/值:
__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);
现在有一个问题:如@Ctx makes us notice ,您的 C 代码取消引用第一个参数,很可能是因为它是一个
uint32_t*
指针,而不仅仅是一个int64
:*(_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为此。
然后将所有(工作)代码放入
.c
文件中:#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; }
将代码编译为共享库:
gcc -fPIC -shared -o mylib.so mylib.c
现在您可以使用
ctypes
module 从 Python 加载它:>>> from ctypes import cdll, c_int32 >>> mylib = cdll.LoadLibrary('./mylib.so') >>> mylib.manipulateBeforSend(c_int32(1)) 1
关于python - 如何在Python代码中使用IDA反编译的C函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59788576/