Skip to main content

Dispel the magic

In this session, your mission will be to solve the small reverse-engineering challenge called magic .

The function magic to analyze has the following prototype.

int magic (int);

Hexdump

As usual, here is the summary of the basic reverse-engineering information.

info

The x86-32 calling convention states that arguments are passed in the stack. The stack pointer is esp. The memory layout at the callee entry is as follow.

esp offsetSizeValueBINSEC syntax
+04 bytesReturn address@[esp, 4]
+44 bytesFirst argument@[esp + 4, 4]
+84 bytesSecond argument@[esp + 8, 4]
...
+(4*i+4)4 bytesith argument@[esp + 4 * (i + 1), 4]

The return value is put in eax.

00000000000000100000002000000030000000400000005000000060000000700000008000000090000000a0000000b0000000c0000000d0000000e0000000f000000100000001100000012000000130000001400000015000000160000001700000018000000190000001a0000001b0000001c0000001d0000001e0000001f000000200000002100000022000000230000002400000025000000260000002700000028000000290
7f45 4c46 0101 0100 0000 0000 0000 0000 0200 0300 0100 0000 e080 0408 3400 0000 d001 0000 0000 0000 3400 2000 0100 2800 0500 0400 0100 0000 5400 0000 5480 0408 5480 0408 0a01 0000 0a01 0000 0500 0000 0100 0000 8b54 2404 31c0 89d1 0fc9 84c9 790f d1ea f5d1 d071 f905 07f6 f66a 0f94 c0c3 0001 0203 0405 0607 0809 ffff ffff ffff ff0a 0b0c 0d0e 0fff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ff0a 0b0c 0d0e 0fff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff 4645 4544 4330 4445 0a00 4445 4144 4645 4544 0a00 3842 4144 4630 3044 0a00 b9c2 8004 08e8 6200 0000 ba08 0000 0089 e1bb 0200 0000 b803 0000 00cd 8031 c089 4424 080f b614 2444 84d2 7419 7834 83ea 3078 2f0f b692 7280 0408 84d2 7824 c1e0 0409 d0eb de50 e829 ffff ff84 c074 13b9 cc80 0408 e813 0000 0031 dbb8 0100 0000 cd80 b9d6 8004 0868 3b81 0408 ba09 0000 00bb 0100 0000 b804 0000 00cd 80c3 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 5480 0408 0000 0000 0300 0100 0100 0000 e080 0408 0000 0000 1000 0100 0800 0000 5480 0408 1e00 0000 1200 0100 005f 7374 6172 7400 6d61 6769 6300 002e 7379 6d74 6162 002e 7374 7274 6162 002e 7368 7374 7274 6162 002e 7465 7874 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1b00 0000 0100 0000 0600 0000 5480 0408 5400 0000 0a01 0000 0000 0000 0000 0000 0100 0000 0000 0000 0100 0000 0200 0000 0000 0000 0000 0000 6001 0000 4000 0000 0300 0000 0200 0000 0400 0000 1000 0000 0900 0000 0300 0000 0000 0000 0000 0000 a001 0000 0e00 0000 0000 0000 0000 0000 0100 0000 0000 0000 1100 0000 0300 0000 0000 0000 0000 0000 ae01 0000 2100 0000 0000 0000 0000 0000 0100 0000 0000 0000 0000
Legend
Headers Magic
Code Instructions
Read-Only Data Strings
Data Other Sections
ELF Header:
  Class:               ELF32                        
  Data:                2's complement, little endian
  Type:                EXEC                         
  Machine:             x86                          
  Entry point address: 0x80480e0                    

Section Headers:
  [Nr] Name      Type     Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]           NULL     00000000 000000 000000 00      0   0  0
  [ 1] .text     PROGBITS 08048054 000054 00010a 00  AX  0   0  1
  [ 2] .symtab   SYMTAB   00000000 000160 000040 10      3   2  4
  [ 3] .strtab   STRTAB   00000000 0001a0 00000e 00      0   0  1
  [ 4] .shstrtab STRTAB   00000000 0001ae 000021 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), G (group), T (TLS), O (extra OS processing required)

Symbol table '.symtab' contains 4 entries:
  Num:    Value Size Type    Bind   Section Name  
    0: 00000000    0 NOTYPE  LOCAL  UND           
    1: 08048054    0 SECTION LOCAL  .text         
    2: 080480e0    0 NOTYPE  GLOBAL .text   _start
    3: 08048054   30 FUNC    GLOBAL .text   magic

Your solution

# Enter your script here
# starting from ...
# ...
# reach ...
# cut at ...
Output

Proposed solution
starting from <magic>
@[esp + 4, 4] := nondet as x
reach <magic> return such that al = 1 then print x
cut at <magic> return
Output

More detail at https://github.com/binsec/binsec/blob/master/doc/sse/beginners.md