汇编代码中发生了什么?

时间:2015-04-16 07:40:49

标签: assembly

背景:我正在做一个"二进制炸弹"我必须通过6个不同的阶段才能解决这个问题。炸弹。我可以使用调试器来帮助跟踪值并逐步执行程序集的每一行。我陷入了第二阶段。

目标:找到密码'对于满足以下程序的此阶段。正确的密码将使我进入下一阶段

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

0x400ec9 <phase_2>:     push   %rbp
0x400eca <phase_2+1>:   mov    %rsp,%rbp
0x400ecd <phase_2+4>:   sub    $0x30,%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   $0x1,-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    $0x1,%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    $0x1,%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   $0x1,-0x4(%rbp)
0x400f1f <phase_2+86>:  cmpl   $0x5,-0x4(%rbp)
0x400f23 <phase_2+90>:  jle    0x400ef7 <phase_2+46>
0x400f25 <phase_2+92>:  leaveq
0x400f26 <phase_2+93>:  retq

我不知道:这里有一些我不熟悉的新说明。 &#39; cltq&#39;显然执行&#34;签署 - 扩展%eax到%rax&#34;但在这种情况下,我不确定这意味着什么。另外,我不明白行&#39; mov -0x20(%rbp,%rax,4),%edx&#39;它究竟做了什么。我理解它将存储在0x20中的内容移动到%rdx寄存器中,但我当时并不知道内存中该位置应该是什么。我知道在进行调试时,%rbp at -0x20保存了我输入的6个值中的一个。但我并不确定%rax和4究竟做了什么。

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

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

(我不想继续尝试,因为每当我爆炸炸弹时,我都会从作业等级中获得一分)

1 个答案:

答案 0 :(得分:2)

让我看看我是否可以破解这段代码......

我试图把它变成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    $0x30,%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   $0x1,-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    $0x1,%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    $0x1,%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   $0x1,-0x4(%rbp)
0x400f1f <phase_2+86>:  cmpl   $0x5,-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