简体   繁体   中英

Understanding and translating assembly code

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 is not visible and I need to determine the correct password by looking at the assembly code.

The code indicates the password for this method is 6 numbers, which is passed as "input" to method phase 2 (I am trying to avoid triggering ).

I am having trouble understanding what is going on here so if anyone can help me translate this into C code, or if i need to look in any particular registers/locations it would help greatly. There are 4 more phases which are each supposed to be more complex so I want to get a good understanding in how to approach reading these.

Also if anyone has a good resource (like a printable table) with assembly code keywords that would be helpful too, and also if there are any differences between 32-bit and 64-bit registers i need to worry about other than the register names..

(gdb) disas
Dump of assembler code for function phase_2:
   0x0000000000400f49 <+0>: push   %rbp
   0x0000000000400f4a <+1>: push   %rbx
   0x0000000000400f4b <+2>: sub    $0x28,%rsp
   0x0000000000400f4f <+6>: mov    %fs:0x28,%rax
   0x0000000000400f58 <+15>:    mov    %rax,0x18(%rsp)
   0x0000000000400f5d <+20>:    xor    %eax,%eax
   0x0000000000400f5f <+22>:    mov    %rsp,%rsi
   0x0000000000400f62 <+25>:    callq  0x401708 <read_six_numbers>
   0x0000000000400f67 <+30>:    cmpl   $0x0,(%rsp)
   0x0000000000400f6b <+34>:    jne    0x400f74 <phase_2+43>
   0x0000000000400f6d <+36>:    cmpl   $0x1,0x4(%rsp)
   0x0000000000400f72 <+41>:    je     0x400f79 <phase_2+48>
   0x0000000000400f74 <+43>:    callq  0x4016d2 <explode_bomb>
   0x0000000000400f79 <+48>:    mov    %rsp,%rbx
   0x0000000000400f7c <+51>:    lea    0x10(%rsp),%rbp
   0x0000000000400f81 <+56>:    mov    0x4(%rbx),%eax
   0x0000000000400f84 <+59>:    add    (%rbx),%eax
   0x0000000000400f86 <+61>:    cmp    %eax,0x8(%rbx)
   0x0000000000400f89 <+64>:    je     0x400f90 <phase_2+71>
=> 0x0000000000400f8b <+66>:    callq  0x4016d2 <explode_bomb>
   0x0000000000400f90 <+71>:    add    $0x4,%rbx
   0x0000000000400f94 <+75>:    cmp    %rbp,%rbx
   0x0000000000400f97 <+78>:    jne    0x400f81 <phase_2+56>
   0x0000000000400f99 <+80>:    mov    0x18(%rsp),%rax
   0x0000000000400f9e <+85>:    xor    %fs:0x28,%rax
   0x0000000000400fa7 <+94>:    je     0x400fae <phase_2+101>
   0x0000000000400fa9 <+96>:    callq  0x400b90 <__stack_chk_fail@plt>
   0x0000000000400fae <+101>:   add    $0x28,%rsp
   0x0000000000400fb2 <+105>:   pop    %rbx
   0x0000000000400fb3 <+106>:   pop    %rbp
   0x0000000000400fb4 <+107>:   retq   
End of assembler dump.

Your assembly is equivalent to this, see phase_2 function

#include <stdio.h>

__attribute__((noinline)) void read_six_numbers(void *xxx, int *num)
{
    num[0] = 0;
    num[1] = 1;
    num[2] = 1;
    num[3] = 2;
    num[4] = 3;
    num[5] = 5;
}


__attribute__((noinline)) void explode_bomb()
{
    printf("explode_bomb.\n");
}

void phase_2(void *xxx)
{
    int num[6];
    int i;

    read_six_numbers(xxx, num);

    if (num[0] != 0 || num[1] != 1)
        explode_bomb();

    for (i = 0; i < 4; i++) {
        if (num[i] + num[i + 1] == num[i + 2])
            continue;

        explode_bomb();
    }
}

int main()
{
    phase_2(NULL);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM