运行我需要使用参数执行的机器代码。如何?

逆向工程 拆卸 调试 密码学
2021-07-06 00:24:38

我希望运行一个程序来生成种子,但我不知道如何进行,而且我是倒车新手。

我有一个为 x86 架构构建的程序的机器代码。我知道代码的预期功能是从三个 8 字节参数生成一个种子。但是,我不知道如何进行和调试。这种情况下的数字是(作为整数,而不是十六进制)1095923727、3459613537 和 2312051101。

代码的 hexdump 如下所示:

5589 e583 ec18 c704 2410 0000 00e8 26ff
ffff 8945 f0c7 45f4 0000 0000 eb62 8345
f401 8b45 08f7 d0c1 e810 3345 0889 45f8
8b55 f48b 45f8 01d0 69c0 6745 2301 35ef
cdab 8989 4508 8b45 0831 450c 8b45 0801
4510 8b55 f48b 45f8 01d0 69c0 efcd ab89
3567 4523 0189 4508 8b45 0889 45f8 8b45
0c89 4508 8b45 1089 450c 8b45 f889 4510
817d f4ff 0000 0076 958b 550c 8b45 0831
d033 4510 8945 f88b 45f0 8945 fc8b 55fc
8b45 0889 028b 45fc 8d50 048b 450c 8902
8b45 fc8d 5008 8b45 1089 028b 45fc 8d50
0c8b 45f8 8902 8b45 f0c9 c3         

通过反汇编程序(在本例中为 r2)运行它会产生以下结果:

/ (fcn) fcn.00000000 203
|   fcn.00000000 (int arg_8h, int arg_ch, int arg_10h);
|           ; var int local_10h @ rbp-0x10
|           ; var int local_ch @ rbp-0xc
|           ; var int local_8h @ rbp-0x8
|           ; var int local_4h @ rbp-0x4
|           ; arg int arg_8h @ rbp+0x8
|           ; arg int arg_ch @ rbp+0xc
|           ; arg int arg_10h @ rbp+0x10
|           0x00000000      55             push rbp
|           0x00000001      89e5           mov ebp, esp
|           0x00000003      83ec18         sub esp, 0x18
|           0x00000006      c70424100000.  mov dword [rsp], 0x10       ; [0x10:4]=0x4589ffff
|           0x0000000d      e826ffffff     call 0xffffffffffffff38
|           0x00000012      8945f0         mov dword [rbp - local_10h], eax
|           0x00000015      c745f4000000.  mov dword [rbp - local_ch], 0
|       ,=< 0x0000001c      eb62           jmp 0x80
|      .--> 0x0000001e      8345f401       add dword [rbp - local_ch], 1
|      ||   0x00000022      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|      ||   0x00000025      f7d0           not eax
|      ||   0x00000027      c1e810         shr eax, 0x10
|      ||   0x0000002a      334508         xor eax, dword [rbp + arg_8h]
|      ||   0x0000002d      8945f8         mov dword [rbp - local_8h], eax
|      ||   0x00000030      8b55f4         mov edx, dword [rbp - local_ch]
|      ||   0x00000033      8b45f8         mov eax, dword [rbp - local_8h]
|      ||   0x00000036      01d0           add eax, edx
|      ||   0x00000038      69c067452301   imul eax, eax, 0x1234567
|      ||   0x0000003e      35efcdab89     xor eax, 0x89abcdef
|      ||   0x00000043      894508         mov dword [rbp + arg_8h], eax ; [0x8:4]=0x1024
|      ||   0x00000046      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|      ||   0x00000049      31450c         xor dword [rbp + arg_ch], eax
|      ||   0x0000004c      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|      ||   0x0000004f      014510         add dword [rbp + arg_10h], eax
|      ||   0x00000052      8b55f4         mov edx, dword [rbp - local_ch]
|      ||   0x00000055      8b45f8         mov eax, dword [rbp - local_8h]
|      ||   0x00000058      01d0           add eax, edx
|      ||   0x0000005a      69c0efcdab89   imul eax, eax, 0x89abcdef
|      ||   0x00000060      3567452301     xor eax, 0x1234567
|      ||   0x00000065      894508         mov dword [rbp + arg_8h], eax ; [0x8:4]=0x1024
|      ||   0x00000068      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|      ||   0x0000006b      8945f8         mov dword [rbp - local_8h], eax
|      ||   0x0000006e      8b450c         mov eax, dword [rbp + arg_ch] ; [0xc:4]=0xff26e800
|      ||   0x00000071      894508         mov dword [rbp + arg_8h], eax ; [0x8:4]=0x1024
|      ||   0x00000074      8b4510         mov eax, dword [rbp + arg_10h] ; [0x10:4]=0x4589ffff
|      ||   0x00000077      89450c         mov dword [rbp + arg_ch], eax ; [0xc:4]=0xff26e800
|      ||   0x0000007a      8b45f8         mov eax, dword [rbp - local_8h]
|      ||   0x0000007d      894510         mov dword [rbp + arg_10h], eax ; [0x10:4]=0x4589ffff
|      ||   ; JMP XREF from 0x0000001c (fcn.00000000)
|      |`-> 0x00000080      817df4ff0000.  cmp dword [rbp - local_ch], 0xff ; [0xff:4]=-1 ; 255
|      `==< 0x00000087      7695           jbe 0x1e
|           0x00000089      8b550c         mov edx, dword [rbp + arg_ch] ; [0xc:4]=0xff26e800
|           0x0000008c      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|           0x0000008f      31d0           xor eax, edx
|           0x00000091      334510         xor eax, dword [rbp + arg_10h]
|           0x00000094      8945f8         mov dword [rbp - local_8h], eax
|           0x00000097      8b45f0         mov eax, dword [rbp - local_10h]
|           0x0000009a      8945fc         mov dword [rbp - local_4h], eax
|           0x0000009d      8b55fc         mov edx, dword [rbp - local_4h]
|           0x000000a0      8b4508         mov eax, dword [rbp + arg_8h] ; [0x8:4]=0x1024
|           0x000000a3      8902           mov dword [rdx], eax
|           0x000000a5      8b45fc         mov eax, dword [rbp - local_4h]
|           0x000000a8      8d5004         lea edx, dword [rax + 4]    ; 0x4
|           0x000000ab      8b450c         mov eax, dword [rbp + arg_ch] ; [0xc:4]=0xff26e800
|           0x000000ae      8902           mov dword [rdx], eax
|           0x000000b0      8b45fc         mov eax, dword [rbp - local_4h]
|           0x000000b3      8d5008         lea edx, dword [rax + 8]    ; 0x8
|           0x000000b6      8b4510         mov eax, dword [rbp + arg_10h] ; [0x10:4]=0x4589ffff
|           0x000000b9      8902           mov dword [rdx], eax
|           0x000000bb      8b45fc         mov eax, dword [rbp - local_4h]
|           0x000000be      8d500c         lea edx, dword [rax + 0xc]  ; 0xc
|           0x000000c1      8b45f8         mov eax, dword [rbp - local_8h]
|           0x000000c4      8902           mov dword [rdx], eax
|           0x000000c6      8b45f0         mov eax, dword [rbp - local_10h]
|           0x000000c9      c9             leave
\           0x000000ca      c3             ret

如何使用三个输入参数运行上面的代码并生成输出?

2个回答

这应该有效,我想我没有错过任何东西。

至少知道一些预期的输入和输出以进行比较会很高兴。

也不确定调用 0xffffffffffffff38 除了制作整数结构之外是否会对 local_10h 做任何事情..

#include<stdio.h>
#include<string.h>

unsigned * func_00000000 (unsigned arg_8h, unsigned arg_ch, unsigned arg_10h) {
    static unsigned local_10h [4]; //init should be local_10h call 0xffffffffffffff38 ??
    unsigned local_ch;
    unsigned local_8h;
    unsigned local_4h;


    for (local_ch = 0; local_ch <= 0xff; ++local_ch) {
        local_8h = local_4h + (((~arg_8h >> 16 ^ arg_8h) + local_ch) * 0x1234567 ^ 0x89abcdef);
        arg_ch = ((~arg_8h >> 16 ^ arg_8h) + local_ch) * 0x89abcdef ^ 0x1234567;
        arg_8h = arg_10h ^ (((~arg_8h >> 16 ^ arg_8h) + local_ch) * 0x1234567 ^ 0x89abcdef);
        arg_10h = local_8h;
        local_4h = arg_ch;
    }
    local_10h[0] = arg_8h;
    local_10h[1] = arg_10h;
    local_10h[2] = local_4h;
    local_10h[3] = arg_8h ^ arg_10h ^ local_4h;
    return local_10h;
}

main()
{
    unsigned * p;
    int i;
    p = func_00000000(1095923727, 3459613537, 2312051101);

    for ( i = 0; i < 4; i++ ) {
        printf( "%u\n", p[i]);
   }

   return 0;

}

我可以看到三种方法来做到这一点:

  1. 使用 IDA 的AppCall功能从 IDA 的控制台中调用该函数。

  2. 生成调试器(x64,olly)并调试任何进程,在内存中找到一个空区域并将指令指针更改为指向它。由于此函数的参数在堆栈上传递,因此请确保使用所需的输入值覆盖堆栈。

  3. 如果您正在寻找一种更自动化/通用的方法来运行该函数,请从您自己的可执行文件中运行该代码,就好像它是一个编译的 C 函数一样。

    使用 GCC 和扩展 ASM,您可以创建一个小的存根函数,该函数只会将变量推送到堆栈并跳转到包含在数组中定义的原始二进制文件的可执行区域(例如,确保该区域是可执行的并且保护关闭)。您也可以简单地将复制的程序集嵌入到您的代码中。