Category: Reverse Engineering
Points: 400 pts
What will asm4(“picoCTF_75806”) return? Submit the flag as a hexadecimal value (starting with ‘0x’). NOTE: Your submission for this question will NOT be in the normal flag format. Source located in the directory at /problems/asm4_6_0a1c480ca5c932037f143f80d653e3a8.
After the initial setup, the stack looks like this:
| Address | Content |
|---|---|
| ebp+8 | Address of “picoCTF_75806” |
| ebp+4 | Return address |
| ebp | Old ebp |
| ebp-4 | Old ebx |
| ebp-8 | |
| ebp-0xc | 0x0 |
| ebp-0x10 | 0x276 |
1 | <+23>: add DWORD PTR [ebp-0xc],0x1 |
This part counts the number of characters in the input string and stores it at ebp-0xc. So after this part, the value stored at ebp-0xc will be 0xd.
1 | <+42>: mov DWORD PTR [ebp-0x8],0x1 |
Stores 0x1 at ebp-0x8. Updated stack:
| Address | Content |
|---|---|
| ebp-8 | 0x1 |
| ebp-0xc | 0xd |
| ebp-0x10 | 0x276 |
1 | <+138>: mov eax,DWORD PTR [ebp-0xc] |
0x1 is less than 0xc, so we jump to asm4+51.
This is a long loop, so let’s dissect it little by little:
1 | <+51>: mov edx,DWORD PTR [ebp-0x8] |
edx now contains input_string[1], which is ‘i’.
1 | <+65>: mov eax,DWORD PTR [ebp-0x8] |
eax now contains input_string[0], which is ‘p’.
1 | <+82>: sub edx,eax |
The value of edx becomes ‘i’ - ‘p’, which is -7. The value of ebx becomes -7 + 0x276 = 0x26f.
1 | <+94>: mov eax,DWORD PTR [ebp-0x8] |
edx now contains input_string[2], which is ‘c’.
1 | <+111>: mov ecx,DWORD PTR [ebp-0x8] |
eax now contains input_string[1], which is ‘i’.
1 | <+125>: sub edx,eax |
The value of edx becomes ‘c’ - ‘i’, which is -6. The value stored at ebp-0x10 becomes -6 + 0x26f = 0x269.
Updated stack:
| Address | Content |
|---|---|
| ebp-8 | 0x2 |
| ebp-0xc | 0xd |
| ebp-0x10 | 0x269 |
0x2 is less than 0xc, so we jump to asm4+51 again.
By now we can stop manually going over the loops, because we’ve already figured out what the code does. Essentially, for each i in 1 to 11, it calculates input_string[i] - input_string[i-1] + input_string[i+1] - input_string[i] and adds to the total, which is initially 0x276. So we can write a simple Python script for calculating this, and the result turns out to be 515, or 0x203 in hex.