3. Magic revealed


It looks worse than you can imagine!

I can imagine some pretty bad things!

That's why I said *worse*!

 Terry Pratchett, Moving Pictures

The fancy output format of The address of main was chosen for a reason. It is valid input for /bin/sh. Let's check out whether the program from The magic of the Elf has main at the same offset.

3.1. objdump -d

The output of objdump includes function labels. Filtering the complete disassembly can yield the desired code without prior knowledge of the function address. But since we already have the value we use --start-address for symmetry with ndisasm. That option accepts only numeric values, not symbol names.

The filter to pretty up this disassembly is at objdump_format.pl (i)

Output: out/sparc-debian2.2-linux/magic_elf/objdump.asm
   1073c:   9d e3 bf 98         save    %sp, -104, %sp
   10740:   90 10 20 01         mov     1, %o0
   10744:   13 00 00 40         sethi   %hi(0x10000), %o1
   10748:   92 12 60 01         or      %o1, 1, %o159 ! 10001 <*ABS*+0x10001>
   1074c:   40 00 44 5a         call    218b4 <_PROCEDURE_LINKAGE_TABLE_+0x30>
   10750:   94 10 20 03         mov     3, %o2
   10754:   81 c7 e0 08         ret
   10758:   91 e8 20 00         restore %g0, 0, %o0

This looks like a real main. So both programs indeed have main at the same offset. Unfortunately a brief look through /bin proves this to be pure chance. And instead of a real system call for write(2) we see something strange. It resolves to a location in a shared library. But what function in what library?

3.2. GDB to the rescue

The filter to pretty up this disassembly is at gdb_format.pl (i)

Command: pre/sparc-debian2.2-linux/magic_elf/gdb.sh

/bin/echo "[func=${func}]"
pre/sparc-debian2.2-linux/magic_elf/gdb_core.sh ${file} ${func} \
| pre/sparc-debian2.2-linux/magic_elf/gdb_format.pl

Output: out/sparc-debian2.2-linux/magic_elf/gdb
0x1073c <main>:               save          %sp, -104, %sp
0x10740 <main+4>:             mov           1, %o0
0x10744 <main+8>:             sethi         %hi(0x10000), %o1
0x10748 <main+12>:            or            %o1, 1, %o1         ! 0x10001
0x1074c <main+16>:            call          0x218b4 <write>
0x10750 <main+20>:            mov           3, %o2
0x10754 <main+24>:            ret           
0x10758 <main+28>:            restore       %g0, 0, %o0

Looks better. We need a way to retrieve the function name, write, from this output. Then we can feed gdb this argument for disassembly.

Command: pre/sparc-debian2.2-linux/evil_magic/first_gdb_func.sed
#!/bin/sed -nf
/.*<\(.*\)>$/ {

Command: pre/sparc-debian2.2-linux/evil_magic/gdb_write.sh
func=$( pre/sparc-debian2.2-linux/evil_magic/first_gdb_func.sed \
	< out/sparc-debian2.2-linux/magic_elf/gdb )

/bin/echo "[func=${func}]"
pre/sparc-debian2.2-linux/magic_elf/gdb_core.sh ${file} ${func} \
| pre/sparc-debian2.2-linux/magic_elf/gdb_format.pl

Output: out/sparc-debian2.2-linux/evil_magic/write.gdb
0x218b4 <write>:              sethi         %hi(0xc000), %g1
0x218b8 <write+4>:            b,a           0x21884 <_PROCEDURE_LINKAGE_TABLE_>
0x218bc <write+8>:            nop           

Oops. Shared libraries don't share their secrets with everyone.

3.3. In doubt use force

We can now search for a fine manual explaining how to debug shared libraries. Or just compile the bugger static.

Seems we found an easy way to fill up the hard disk. Anyway, what has gdb(1) to say about it?

The function was called write before, it is called write now. Let's look what is behind the name.

3.4. Write your name

Above disassembly is not guaranteed to work. The names of symbols imported by libraries differ from one platform to the other, and from one compiler to the other. A more rational approach is to search the listing of all symbols for similar names and identical addresses.

I suspect there is actually order behind the chaos. The symbol __write, with a varying number of leading underscores, seems to be "the real thing" on all platforms. The aliases for the value, 0x184e8, differ a lot.

There are two man pages giving some overview of system calls, intro(2) and syscalls(2). /usr/include/unistd.h declares a traditional general purpose interface called syscall. Not all Linux system have man page syscall(2), though. Anyway, the statement mov 4,%g1 corresponds to the value of __NR_write in /usr/include/asm/unistd.h. Note that Linux uses ta 0x10 while Solaris uses ta 8. There are other differences, but they are beyond the scope of this document.