Understanding/decoding obscure assembly code
Question
Like my previous question, this involves an assignment where a method is called which requires a certain password, the code is hidden and we have to infer the password from the assembly code (I want to avoid hitting . I've completed a few phases so far and I'm getting better at understanding, however this phase has a few aspects I am having trouble with. So far I know the password to this phase is two integers. Backtracing has been my goto method for some of these but is not very helpful for this phase.
- I understand cltq expands eax(rax) to 4 words, but I'm not sure how this affects calculations, and also unsure what happens if that line is hit multiple times.
- the phase5+82 -> phase5+65 (a loop?) What values am I trying to start with so ecx(rcx) can pass the final comparison?
- mov 0x402600(,%rax,4),%eax <- what does this line do exactly? The blank is throwing me off, is a blank = 0?
Any other help understanding what is going on and how I should approach figuring out the input would be helpful, I have attempted to convert this back into C code, like previous phases
0x00000000004010b4 <phase_5+0>: sub $0x18,%rsp 0x00000000004010b8 <phase_5+4>: lea 0x10(%rsp),%rcx 0x00000000004010bd <phase_5+9>: lea 0x14(%rsp),%rdx 0x00000000004010c2 <phase_5+14>: mov $0x4026aa,%esi 0x00000000004010c7 <phase_5+19>: mov $0x0,%eax 0x00000000004010cc <phase_5+24>: callq 0x400b80<sscanf@plt> 0x00000000004010d1 <phase_5+29>: cmp $0x1,%eax 0x00000000004010d4 <phase_5+32>: jg 0x4010db<phase_5+39> 0x00000000004010d6 <phase_5+34>: callq 0x401421(explode_bomb) 0x00000000004010db <phase_5+39>: mov 0x14(%rsp),%eax 0x00000000004010df <phase_5+43>: and $0xf,%eax 0x00000000004010e2 <phase_5+46>: mov %eax,0x14(%rsp) 0x00000000004010e6 <phase_5+50>: cmp $0xf,%eax 0x00000000004010e9 <phase_5+53>: je 0x40111b <phase_5+103> 0x00000000004010eb <phase_5+55>: mov $0x0,%edx 0x00000000004010f0 <phase_5+60>: mov $0x0,%ecx 0x00000000004010f5 <phase_5+65>: add $0x1,%edx 0x00000000004010f8 <phase_5+68>: cltq 0x00000000004010fa <phase_5+70>: mov 0x402600(,%rax,4),%eax 0x0000000000401101 <phase_5+77>: add %eax,%ecx 0x0000000000401103 <phase_5+79>: cmp $0xf,%eax 0x0000000000401106 <phase_5+82>: jne 0x4010f5 <phase_5+65> 0x0000000000401108 <phase_5+84>: movl $0xf,0x14(%rsp) 0x0000000000401110 <phase_5+92>: cmp $0xf,%edx 0x0000000000401113 <phase_5+95>: jne 0x40111b <phase_5+103> 0x0000000000401115 <phase_5+97>: cmp %ecx,0x10(%rsp) 0x0000000000401119 <phase_5+101>: je 0x401120 <phase_5+108> 0x000000000040111b <phase_5+103>: callq 0x401421 <explode_bomb> 0x0000000000401120 <phase_5+108>: add $0x18,%rsp 0x0000000000401124 <phase_5+112>: retq
Solution
The code translates to this:
0x402600: int table[15];
0x4026aa: const char *format;
void func (const char *str)
{
int a, b, count, sum;
if (sscanf (str, format, &a, &b) != 2) {
explode_bomb();
}
a = a & 0xF;
if (a == 0xF) {
explode_bomb();
}
sum = 0;
count = 0;
while (a != 0xF) {
a = table[a];
sum += a;
count++;
}
if ((count != 0xF) || (sum != b)) {
explode_bomb ();
}
}
To answer your specific points:
cltq is used to clear the 4 most significant bytes of rax so as to not interfere with the address calculation in the following instruction. It has no impact on the calculation.
mov 0x402600(,%rax,4),%eax <- what does this line do exactly? The blank is throwing me off, is a blank = 0?
Yes, this is just mov dword eax, [0x402600 + 0 + rax * 4]
Once you have the C equivalent it is easy to figure out how to find a solution.