Decoding and understanding assembly code

前端 未结 1 958
抹茶落季
抹茶落季 2021-01-30 09:19

So a little background. I am a beginner with c and assembly code, we have an \"bomb\" assignment (written in c)which calls methods that require certain passwords, but the code i

相关标签:
1条回答
  • 2021-01-30 10:10

    Here is a C equivalent of phase2:

    int t[6];
    read_six_numbers (t);
    if ((t[0] != 0) || (t[1] != 1)) {
        explode_bomb();
    }
    
    for (int i = 2; i < 6; i++) {
            if (t[i] != t[i - 2] + t[i - 1]) {
                explode_bomb();
        }
    }
    

    So the password is 0, 1, 1, 2, 3, 5.

    How did I do this ? By gradually replacing the assembly with C.

    You'll note that the stack pointer (rsp) never changes. You can see the stack as an array t of 32 bits numbers. That is each time you move by 4 bytes you move to the next element. i.e. 0(%rsp), 4(%rsp), ... are equivalent to t[0], t[1], ...

    I'll show you a possible gradual transformation of the bit you have trouble with:

                    lea    0x8(%rsp),%rbx
                    lea    0x18(%rsp),%rbp
    <phase_2+42>:   mov    -0x8(%rbx),%eax
                    add    -0x4(%rbx),%eax
                    cmp    %eax,(%rbx)
                    je     <phase_2+57>
                    callq  explode_bomb
    <phase_2+57>:   add    $0x4,%rbx
                    cmp    %rbp,%rbx
                    jne    phase_2+42
    ------------------------------------------------------
                        rbx = rsp + 8;
                        rbp = rsp + 24;
    <phase_2+42>:       eax = [rbx - 8];
                        eax += [rbx - 4];
                        if (eax == [rbx]) goto <phase_2+57>;
                        explode_bomb();
    <phase_2+57>:       rbx += 4;
                        if (rbx != rbp) goto phase_2+42;
    ------------------------------------------------------
    rbx = rsp + 8;
    rbp = rsp + 24;
    do {
        eax = [rbx - 8] + [rbx - 4];
            if (eax != [rbx]) {
            explode_bomb();
        }
            rbx += 4;
    } while (rbx != rbp);
    ------------------------------------------------------
    rbx = 8;
    do {
        eax = [rsp + rbx - 8] + [rsp + rbx - 4];
            if (eax != [rsp + rbx]) {
            explode_bomb();
        }
            rbx += 4;
    } while (rbx < 24);
    ------------------------------------------------------
    i = 2;
    do {
        eax = t[i - 2] + t[i - 1];
            if (eax != t[i]) {
            explode_bomb();
        }
            i += 1;
    } while (i < 6);
    ------------------------------------------------------
    for (int i = 2; i < 6; i++) {
        if (t[i] != t[i - 2] + t[i - 1]) {
                explode_bomb();
            }
    }
    

    If you take the time to understand these transformations you'll be able to transform and understand any piece of assembly.

    0 讨论(0)
提交回复
热议问题