此汇编代码中发生了什么?

What is happening in this Assembly code?

背景:我正在做一个 "binary bomb" 任务,我必须通过 6 个不同的阶段才能 'defuse' 炸弹。我可以使用调试器来帮助跟踪值并单步执行程序集的每一行。我卡在第二阶段了。

目标: 找到满足以下程序的本阶段'password'。正确的密码会让我进入下一阶段

我想我知道的: 看起来马上,它正在尝试读入 6 个数字。当我反汇编整个炸弹的 main() 函数时,我注意到一行带有“%d %d %d %d %d %d”,所以这个阶段必须需要 6 个数字。此外,看起来有一个从 1 开始并在计数器大于 5 后结束的循环。它似乎在内存的 -0x4(%rbp) 部分被跟踪。它看起来像 at line,它将 %eax 中跟踪的内容与用户输入的内容进行比较(之前存储在 %edx at line 中以查看它们是否相等。如果不相等,炸弹就会爆炸。如果他们是,它将继续循环。

0x400ec9 <phase_2>:     push   %rbp
0x400eca <phase_2+1>:   mov    %rsp,%rbp
0x400ecd <phase_2+4>:   sub    [=10=]x30,%rsp
0x400ed1 <phase_2+8>:   mov    %rdi,-0x28(%rbp)
0x400ed5 <phase_2+12>:  lea    -0x20(%rbp),%rsi
0x400ed9 <phase_2+16>:  mov    -0x28(%rbp),%rdi
0x400edd <phase_2+20>:  callq  0x4013e9 <read_six_numbers>
0x400ee2 <phase_2+25>:  mov    -0x20(%rbp),%eax
0x400ee5 <phase_2+28>:  test   %eax,%eax
0x400ee7 <phase_2+30>:  jns    0x400eee <phase_2+37>
0x400ee9 <phase_2+32>:  callq  0x401983 <explode_bomb>
0x400eee <phase_2+37>:  movl   [=10=]x1,-0x4(%rbp)
0x400ef5 <phase_2+44>:  jmp    0x400f1f <phase_2+86>
0x400ef7 <phase_2+46>:  mov    -0x4(%rbp),%eax
0x400efa <phase_2+49>:  cltq
0x400efc <phase_2+51>:  mov    -0x20(%rbp,%rax,4),%edx
0x400f00 <phase_2+55>:  mov    -0x4(%rbp),%eax
0x400f03 <phase_2+58>:  sub    [=10=]x1,%eax
0x400f06 <phase_2+61>:  cltq
0x400f08 <phase_2+63>:  mov    -0x20(%rbp,%rax,4),%eax
0x400f0c <phase_2+67>:  add    -0x4(%rbp),%eax
0x400f0f <phase_2+70>:  add    [=10=]x1,%eax
0x400f12 <phase_2+73>:  cmp    %eax,%edx
0x400f14 <phase_2+75>:  je     0x400f1b <phase_2+82>
0x400f16 <phase_2+77>:  callq  0x401983 <explode_bomb>
0x400f1b <phase_2+82>:  addl   [=10=]x1,-0x4(%rbp)
0x400f1f <phase_2+86>:  cmpl   [=10=]x5,-0x4(%rbp)
0x400f23 <phase_2+90>:  jle    0x400ef7 <phase_2+46>
0x400f25 <phase_2+92>:  leaveq
0x400f26 <phase_2+93>:  retq

我不知道的地方: 这里有一些我不熟悉的新指令。 'cltq' 显然执行 "sign-extend %eax to %rax" 但我不太确定在这种情况下这意味着什么。另外,我不明白 'mov -0x20(%rbp, %rax, 4), %edx' 这一行以及它到底做了什么。我知道它正在将存储在 0x20 中的内容移动到 %rdx 寄存器,但我不知道当时内存中的那个位置应该是什么。我知道在进行调试时,-0x20 处的 %rbp 包含我输入的 6 个值之一。但我不太确定 %rax 和 4 到底做了什么。

如果有人能看到所需的 6 个数字,那就太好了!如果没有,我希望有人至少可以阐明这段代码以及我必须帮助推动我朝着正确方向前进的问题!谢谢

我测试的一些错误方案: 1 2 3 4 5 6; 10 20 30 40 50 60

(我不想继续尝试,因为每次我引爆炸弹,作业成绩都会扣一分)

让我看看我能否破解这个密码...

我试图将其转换为 C 代码,所以这里是:

void read_six_numbers(int *array);
void explode();

int phase_2()
{
    int array[6];

    read_six_numbers(array);

    if (array[0] < 0)
    {
        explode();
    }

    for (int i = 1; i <= 5; ++i)
    {
        int cur  = array[i];   // %edx
        int prev = array[i-1]; // %eax

        prev += i;
        prev++;

        if (prev != cur)
        {
            explode();
        }
    }
}

因此,要通过测试,您需要以下数字序列:

n, n + 2, (n + 2) + 3, ((n + 2) + 3) + 4, (((n + 2) + 3) + 4) + 5, ((((n + 2) + 3) + 4) + 5) + 6

n, n + 2, n + 5, n + 9, n + 14, n + 20

其中 n 为正数或零。

这是我对你的汇编代码的注释:

0x400ec9 <phase_2>:     push   %rbp
0x400eca <phase_2+1>:   mov    %rsp,%rbp
0x400ecd <phase_2+4>:   sub    [=13=]x30,%rsp          // Reserve some space on the stack...
0x400ed1 <phase_2+8>:   mov    %rdi,-0x28(%rbp)
0x400ed5 <phase_2+12>:  lea    -0x20(%rbp),%rsi   // Compute the address of -0x20(%rbp) and store it in %rsi (I'm assuming this is an argument for read_six_numbers below)
0x400ed9 <phase_2+16>:  mov    -0x28(%rbp),%rdi
0x400edd <phase_2+20>:  callq  0x4013e9 <read_six_numbers>
0x400ee2 <phase_2+25>:  mov    -0x20(%rbp),%eax       // From the code this looks like an array. Load its first element.
0x400ee5 <phase_2+28>:  test   %eax,%eax              // bitwise and that sets the flags.
0x400ee7 <phase_2+30>:  jns    0x400eee <phase_2+37>  // Jump not signed (jump if positive), so we can deduce Rule 1: First number is positive!
0x400ee9 <phase_2+32>:  callq  0x401983 <explode_bomb> // Explode!
0x400eee <phase_2+37>:  movl   [=13=]x1,-0x4(%rbp)         // -0x4(%rbp) looks like a counter that starts at one so I'll call it `int i = 0;`
0x400ef5 <phase_2+44>:  jmp    0x400f1f <phase_2+86>   // Goto loop condition!
0x400ef7 <phase_2+46>:  mov    -0x4(%rbp),%eax         // Loop body:
0x400efa <phase_2+49>:  cltq   // Extend sign for %eax (converts %eax [int32] to %rax [int64])
0x400efc <phase_2+51>:  mov    -0x20(%rbp,%rax,4),%edx // Our read values are in an array located at -0x20(%rbp) I'll call it array from now on. So this line ends up being %edx = array[i]
0x400f00 <phase_2+55>:  mov    -0x4(%rbp),%eax         // reset i back to it's last value...
0x400f03 <phase_2+58>:  sub    [=13=]x1,%eax               // Subtract 1 from i (are we checking our values backwards?)
0x400f06 <phase_2+61>:  cltq
0x400f08 <phase_2+63>:  mov    -0x20(%rbp,%rax,4),%eax // Load array[i] into %eax
0x400f0c <phase_2+67>:  add    -0x4(%rbp),%eax         // Add i to this value? Nice trick...
0x400f0f <phase_2+70>:  add    [=13=]x1,%eax
0x400f12 <phase_2+73>:  cmp    %eax,%edx
0x400f14 <phase_2+75>:  je     0x400f1b <phase_2+82>
0x400f16 <phase_2+77>:  callq  0x401983 <explode_bomb>
0x400f1b <phase_2+82>:  addl   [=13=]x1,-0x4(%rbp)
0x400f1f <phase_2+86>:  cmpl   [=13=]x5,-0x4(%rbp)         // Loop condition: Check i against 5.
0x400f23 <phase_2+90>:  jle    0x400ef7 <phase_2+46>   // If i <= 5 goto Loop body
0x400f25 <phase_2+92>:  leaveq
0x400f26 <phase_2+93>:  retq