• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1669

Diffusing A Binary Bomb

I am having trouble with this bomb.  I know i need to feed it six numbers

when   eax = *(esi + ebx * 4 - 8) + 5;

how can this be true?
eax = *(esi + ebx * 4 - 4)

I did some math incrementing ebx and found the equations are always 1 different from each other:
eax = *(esi + ebx * 4 - 8) + 5;
2 = 8 - 8 = 0 + 5 = 5  (2 would be ebx here)
- 4  = 4    = 4
3 = 12 - 8 = 4 + 5 = 9 (3 would be ebx here)
- 4 = 8     = 8
4 = 16 - 8 = 8 + 5 = 13 (4 would be ebx here)
16 - 4 = 12    = 12

/* unknown */ void  ebx;
/* unknown */ void  esi;
/* unknown */ void  Vfffffff8;

Vfffffff8 = ebp - 0x20;
*esp = A8;
ebx = 2;
esi = ebp - 0x20;
do {
eax = *(esi + ebx * 4 - 8) + 5;
if(*(esi + ebx * 4 - 4) != eax) {
eax = explode_bomb();
}
ebx = ebx + 1;
} while(ebx != 7);
}

Could someone push me in the right direction?

I have IDA Pro, and Recstudio... but i don't know how to assign values to the variables and take the loops for a "test run".

also i don't understand how to figure out ebp?

thank you!
``````Here is the ASM representation:
Address  Mnemonic                                 SP In  SP Out  SP offs  Intermediate Representation
08048dc0: push ebp                                     0      60      0   (save)ebp
08048dc1: mov ebp,esp                                                 4   ebp = esp
08048dc3: push esi                                                    4   (save)esi
08048dc4: push ebx                                                    8   (save)ebx
08048dc5: sub esp,byte +0x30                                         12   esp = esp - 0x30
08048dc8: lea eax,[ebp-0x20]                                         60   eax = ebp - 0x20
08048dcb: mov [esp+0x4],eax                                          60   *(esp + 4) = eax
08048dcf: mov eax,[ebp+0x8]                                          60   eax = *(ebp + 8)
08048dd2: mov [esp],eax                                              60   *esp = eax
08048dd5: call 0x8049393                                             60   eax = read_six_numbers()
08048dda: mov ebx,0x2                                                60   ebx = 2
08048ddf: lea esi,[ebp-0x20]                                         60   esi = ebp - 0x20
08048de2: mov eax,[esi+ebx*4-0x8]                     60      60     60   eax = *(esi + ebx * 4 - 8)
08048de6: add eax,byte +0x5                                          60   eax = eax + 5
08048de9: cmp [esi+ebx*4-0x4],eax                                    60   *(esi + ebx * 4 - 4) :: eax
08048ded: jz 0x8048df4                                               60   == ? L08048df4 :
08048def: call 0x8049351                              60      60     60   eax = explode_bomb()
08048df4: add ebx,byte +0x1                           60      60     60   ebx = ebx + 1
08048df7: cmp ebx,byte +0x7                                          60   ebx :: 7
08048dfa: jnz 0x8048de2                                              60   != ? 0x8048de2 :
08048dfc: add esp,byte +0x30                          60       0     60   esp = esp + 0x30
08048dff: pop ebx                                                    12   (restore)ebx
08048e00: pop esi                                                     8   (restore)esi
08048e01: pop ebp                                                     4   (restore)ebp
08048e02: ret                                                         0   return

And the code converted to C (using RecStudio):

/*	Procedure: 0x08048DC0 - 0x08048E02
*	Argument size: 4
*	Local size: 0
*	Save regs size: 48
*	Called by:
*		main()
*/

phase_2(A8)
/* unknown */ void  A8;
{
/* unknown */ void  ebx;
/* unknown */ void  esi;
/* unknown */ void  Vfffffff8;

Vfffffff8 = ebp - 0x20;
*esp = A8;
ebx = 2;
esi = ebp - 0x20;
do {
eax = *(esi + ebx * 4 - 8) + 5;
if(*(esi + ebx * 4 - 4) != eax) {
eax = explode_bomb();
}
ebx = ebx + 1;
} while(ebx != 7);
}

--------
/* unknown */ void  A8;
/* unknown */ void  Ac;
{
/* unknown */ void  V0;
/* unknown */ void  V4;
/* unknown */ void  V8;
/* unknown */ void  Vc;
/* unknown */ void  V10;
/* unknown */ void  V14;
/* unknown */ void  V18;

V18 = Ac + 0x14;
V14 = Ac + 0x10;
V10 = Ac + 0xc;
Vc = Ac + 8;
V8 = Ac + 4;
V4 = Ac;
V0 = "%d %d %d %d %d %d";
*esp = A8;
eax = L080489D4();
if(eax <= 5) {
eax = explode_bomb();
}
}
----------------
/*	Procedure: 0x080489D4 - 0x080489E3
*	Argument size: 0
*	Local size: 0
*	Save regs size: 0
*	Called by:
*		phase_5()
*		phase_4()
*		phase_3()
*		phase_defused()
*/

L080489d4()
{

goto ( *L0804a16c);
(save)0xf0;
goto L080487E4;
}
--------------------
/*	Procedure: 0x080487E4 - 0x080487F3
*	Argument size: 0
*	Local size: 0
*	Save regs size: 0
*/

L080487e4()
{

(save)M0804a0ec;
goto ( *L0804a0f0);
*eax = *eax + al;
*eax = *eax + al;
}
``````
0
braker15
• 5
• 3
2 Solutions

Commented:
>> when   eax = *(esi + ebx * 4 - 8) + 5;
>>
>> how can this be true?
>> eax = *(esi + ebx * 4 - 4)

Well, it's quite straightforward, if you realize what esi is. What does it point to ?

>> I did some math incrementing ebx and found the equations are always 1 different from each other:

To be more exact, the calculated addresses have a difference of 4 == sizeof(int).

>> Could someone push me in the right direction?

Let me give you a hint : which arguments does read_six_numbers take ? What does the function do ? Where does it place its result ?

>> but i don't know how to assign values to the variables and take the loops for a "test run".

You can actually run the application - you'll be asked for input data, and you'll see the result of that.
If you want to observe the registry and stack contents while the code runs, you can run it inside a debugger, like gdb for example.

>> also i don't understand how to figure out ebp?

ebp is the base pointer. It points to the start of the current stack frame. A stack frame is set up at the beginning of each function call, and contains all local stack data for that function, as well as the return address, the saved ebp of the previous stack frame, etc.

This is where the stack frame is set up :

>> 08048dc0: push ebp
>> 08048dc1: mov ebp,esp

ie. the old base pointer is saved on the stack, so it can be restored when the function ends.
And the new base pointer is set to the current top of the stack to start a new stack frame.

At the end of the function, the stack frame is destroyed again, and we go back to the previous stack frame :

>> 08048e01: pop ebp
>> 08048e02: ret

ie. the saved base pointer is restored, and the code jumps to the return address (using ret)
0

Author Commented:
i am at a loss here, i don't even understand what this is:
.text:08049393 var_28          = dword ptr -28h
.text:08049393 var_24          = dword ptr -24h
.text:08049393 var_20          = dword ptr -20h
.text:08049393 var_1C          = dword ptr -1Ch
.text:08049393 var_18          = dword ptr -18h
.text:08049393 var_14          = dword ptr -14h
.text:08049393 var_10          = dword ptr -10h
.text:08049393 var_C           = dword ptr -0Ch
.text:08049393 arg_0           = dword ptr  8
.text:08049393 arg_4           = dword ptr  0Ch

it seems like variables are being assigned memory locations... why are they negative?

i guess i am having trouble understanding "the big picture".

Is there maybe something with pictures, arrows, and code that could give me the basics here?
``````.text:08049393                 public read_six_numbers
.text:08049393 read_six_numbers proc near              ; CODE XREF: phase_6+16p
.text:08049393                                         ; phase_2+15p
.text:08049393
.text:08049393 var_28          = dword ptr -28h
.text:08049393 var_24          = dword ptr -24h
.text:08049393 var_20          = dword ptr -20h
.text:08049393 var_1C          = dword ptr -1Ch
.text:08049393 var_18          = dword ptr -18h
.text:08049393 var_14          = dword ptr -14h
.text:08049393 var_10          = dword ptr -10h
.text:08049393 var_C           = dword ptr -0Ch
.text:08049393 arg_0           = dword ptr  8
.text:08049393 arg_4           = dword ptr  0Ch
.text:08049393
.text:08049393                 push    ebp
.text:08049394                 mov     ebp, esp
.text:08049396                 sub     esp, 28h
.text:08049399                 mov     edx, [ebp+arg_4]
.text:0804939C                 lea     eax, [edx+14h]
.text:0804939F                 mov     [esp+28h+var_C], eax
.text:080493A3                 lea     eax, [edx+10h]
.text:080493A6                 mov     [esp+28h+var_10], eax
.text:080493AA                 lea     eax, [edx+0Ch]
.text:080493B1                 lea     eax, [edx+8]
.text:080493B4                 mov     [esp+28h+var_18], eax
.text:080493B8                 lea     eax, [edx+4]
.text:080493BB                 mov     [esp+28h+var_1C], eax
.text:080493BF                 mov     [esp+28h+var_20], edx
.text:080493C3                 mov     [esp+28h+var_24], offset unk_8049BD9
.text:080493CB                 mov     eax, [ebp+arg_0]
.text:080493CE                 mov     [esp+28h+var_28], eax
.text:080493D1                 call    sub_80489D4
.text:080493D6                 cmp     eax, 5
.text:080493D9                 jg      short locret_80493E0
.text:080493DB                 call    explode_bomb
.text:080493E0
.text:080493E0 locret_80493E0:                         ; CODE XREF: read_six_numbers+46j
.text:080493E0                 leave
.text:080493E1                 retn
``````
0

Commented:
>> i am at a loss here, i don't even understand what this is:

It looks like you're using IDA to analyze the code. That's ok, but it might be better to use an actual debugger, so you can analyze the code while it's running (as I suggested earlier).

In any case :

>> it seems like variables are being assigned memory locations...

That's right. All local variables for the function are assigned a certain location on the stack (within the current stack frame). The list you posted are just all those local variables, and IDA gave them a human readable name that is used in the rest of the function code (which should theoretically make it easier to read the code).
Be aware though that IDA doesn't have all information, and in this case, I think that it will confuse you rather than help you. So, I really suggest to concentrate on the code, and use a debugger to see what's happening, and where the values you give as input go.

>> why are they negative?

Do you know what two's complement is ?

http://en.wikipedia.org/wiki/Two%27s_complement

The reason WHY they are negative, is because the stack grows downwards (towards lower memory addresses). So, to move UP in the stack, the memory addresses get smaller.

>> i guess i am having trouble understanding "the big picture".

The idea of this kind of assignment is to help you get comfortable with assembly, and with the memory model. It's normal that you don't see the big picture yet, but with time, you will. For now, maybe it's best to read up on stack layout, and stack frames.
If you have course notes, then consult those. Otherwise, take a look at this :

http://en.wikipedia.org/wiki/Call_stack

(and especially the "Structure" paragraph which talks about stack frames). A good understanding of the stack is necessary to solve this assignment. So, read up on it, and if you have any questions, don't hesitate to ask them here ...
0

Author Commented:
I don't understand what i should be typing in gdb to view the registers adresses and values before, during, and after the loop

here's what i tried:

user@bomb:~/bomb23\$ gdb bomb
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) break phase_2
Breakpoint 1 at 0x8048dc5
(gdb) run
Starting program: /home/user/bomb23/bomb
Welcome to my fiendish little bomb. You have 6 phases with
which to blow yourself up. Have a nice day!
(gdb)x 0x8048dc4

BOOM!!!
The bomb has blown up.

Program exited with code 010.
(gdb)

``````08048dc0 <phase_2>:
8048dc0:       55                      push   %ebp
8048dc1:       89 e5                   mov    %esp,%ebp
8048dc3:       56                      push   %esi
8048dc4:       53                      push   %ebx
8048dc5:       83 ec 30                sub    \$0x30,%esp
8048dc8:       8d 45 e0                lea    -0x20(%ebp),%eax
8048dcb:       89 44 24 04             mov    %eax,0x4(%esp)
8048dcf:       8b 45 08                mov    0x8(%ebp),%eax
8048dd2:       89 04 24                mov    %eax,(%esp)
8048dd5:       e8 b9 05 00 00          call   8049393 <read_six_numbers>
8048dda:       bb 02 00 00 00          mov    \$0x2,%ebx
8048ddf:       8d 75 e0                lea    -0x20(%ebp),%esi
8048de2:       8b 44 9e f8             mov    -0x8(%esi,%ebx,4),%eax
8048de6:       83 c0 05                add    \$0x5,%eax
8048de9:       39 44 9e fc             cmp    %eax,-0x4(%esi,%ebx,4)
8048ded:       74 05                   je     8048df4 <phase_2+0x34>
8048def:       e8 5d 05 00 00          call   8049351 <explode_bomb>
8048df4:       83 c3 01                add    \$0x1,%ebx
8048df7:       83 fb 07                cmp    \$0x7,%ebx
8048dfa:       75 e6                   jne    8048de2 <phase_2+0x22>
8048dfc:       83 c4 30                add    \$0x30,%esp
8048dff:       5b                      pop    %ebx
8048e00:       5e                      pop    %esi
8048e01:       5d                      pop    %ebp
8048e02:       c3                      ret
``````
0

Author Commented:
okay, i made some progress, i wasn't specifying my input file when i used command 'run'

(gdb) break phase_2
(gdb) run sol.txt
(gdb) disas
(gdb) break *0x08048de9
Breakpoint 2 at 0x8048de9
(gdb) ni
0x08048dc8 in phase_2 ()
0x08048dc8 in phase_2 ()
(gdb) step
Single stepping until exit from function phase_2,
which has no line number information.

Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb)
(gdb) info registers
eax            0x9      9
ecx            0x0      0
edx            0x20     32
ebx            0x2      2
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x206    [ PF IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51
(gdb)

since %eax is 9... should the first of my six numbers be 9?
``````Breakpoint 1, 0x08048dc5 in phase_2 ()
Current language:  auto; currently asm
(gdb) disas
Dump of assembler code for function phase_2:
0x08048dc0 <phase_2+0>: push   %ebp
0x08048dc1 <phase_2+1>: mov    %esp,%ebp
0x08048dc3 <phase_2+3>: push   %esi
0x08048dc4 <phase_2+4>: push   %ebx
0x08048dc5 <phase_2+5>: sub    \$0x30,%esp
0x08048dc8 <phase_2+8>: lea    -0x20(%ebp),%eax
0x08048dcb <phase_2+11>:        mov    %eax,0x4(%esp)
0x08048dcf <phase_2+15>:        mov    0x8(%ebp),%eax
0x08048dd2 <phase_2+18>:        mov    %eax,(%esp)
0x08048dda <phase_2+26>:        mov    \$0x2,%ebx
0x08048ddf <phase_2+31>:        lea    -0x20(%ebp),%esi
0x08048de2 <phase_2+34>:        mov    -0x8(%esi,%ebx,4),%eax
0x08048de9 <phase_2+41>:        cmp    %eax,-0x4(%esi,%ebx,4)
0x08048ded <phase_2+45>:        je     0x8048df4 <phase_2+52>
0x08048def <phase_2+47>:        call   0x8049351 <explode_bomb>
0x08048df7 <phase_2+55>:        cmp    \$0x7,%ebx
0x08048dfa <phase_2+58>:        jne    0x8048de2 <phase_2+34>
0x08048dff <phase_2+63>:        pop    %ebx
0x08048e00 <phase_2+64>:        pop    %esi
0x08048e01 <phase_2+65>:        pop    %ebp
0x08048e02 <phase_2+66>:        ret
End of assembler dump.
(gdb)
``````
0

Author Commented:
YESS!!!!!!!!!!!!!!!!!!!!!! I GOT IT

I kept doing
(gdb) break *0x08048de9
(gdb) info registers

until i had all the values in eax!!!

This is freaking sweeet!!!!!!!!!!!!!!

``````Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb) info registers
eax            0xb      11
ecx            0x0      0
edx            0x1f     31
ebx            0x2      2
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x202    [ IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51
(gdb)

Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb) info registers
eax            0x10     16
ecx            0x0      0
edx            0x1f     31
ebx            0x3      3
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x212    [ AF IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51

Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb) info registers
eax            0x15     21
ecx            0x0      0
edx            0x1f     31
ebx            0x4      4
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x202    [ IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51

Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb) info registers
eax            0x1a     26
ecx            0x0      0
edx            0x1f     31
ebx            0x5      5
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x202    [ IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51
(gdb)
Breakpoint 2, 0x08048de9 in phase_2 ()
(gdb) info registers
eax            0x1f     31
ecx            0x0      0
edx            0x1f     31
ebx            0x6      6
esp            0xbffff7e0       0xbffff7e0
ebp            0xbffff818       0xbffff818
esi            0xbffff7f8       -1073743880
edi            0x0      0
eip            0x8048de9        0x8048de9 <phase_2+41>
eflags         0x202    [ IF ]
cs             0x73     115
ss             0x7b     123
ds             0x7b     123
es             0x7b     123
fs             0x0      0
gs             0x33     51
(gdb)
``````
0

Author Commented:
thank you so much for all your help!
0

Commented:
Good :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.