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-sunos5.9/magic_elf/objdump.asm
   105c0:   9d e3 bf 90         save    %sp, -112, %sp
   105c4:   90 10 20 01         mov     1, %o0
   105c8:   13 00 00 40         sethi   %hi(0x10000), %o1
   105cc:   92 12 60 01         or      %o1, 1, %o159 ! 10001 <_START_+0x1>
   105d0:   40 00 40 4d         call    20704 <_PROCEDURE_LINKAGE_TABLE_+0x6c>
   105d4:   94 10 20 03         mov     3, %o2
   105d8:   81 c7 e0 08         ret
   105dc:   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-sunos5.9/magic_elf/gdb.sh

/usr/bin/echo "[func=${func}]"
pre/sparc-sunos5.9/magic_elf/gdb_core.sh ${file} ${func} \
| pre/sparc-sunos5.9/magic_elf/gdb_format.pl

Output: out/sparc-sunos5.9/magic_elf/gdb
0x105c0 <main>:               save          %sp, -112, %sp
0x105c4 <main+4>:             mov           1, %o0
0x105c8 <main+8>:             sethi         %hi(0x10000), %o1
0x105cc <main+12>:            or            %o1, 1, %o1         ! 0x10001
0x105d0 <main+16>:            call          0x20704 <write>
0x105d4 <main+20>:            mov           3, %o2
0x105d8 <main+24>:            ret           
0x105dc <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-sunos5.9/evil_magic/first_gdb_func.sed
#!/usr/xpg4/bin/sed -nf
/.*<\(.*\)>$/ {

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

/usr/bin/echo "[func=${func}]"
pre/sparc-sunos5.9/magic_elf/gdb_core.sh ${file} ${func} \
| pre/sparc-sunos5.9/magic_elf/gdb_format.pl

Output: out/sparc-sunos5.9/evil_magic/write.gdb
0x20704 <write>:              sethi         %hi(0x1b000), %g1
0x20708 <write+4>:            b,a           0x20698 <_PROCEDURE_LINKAGE_TABLE_>
0x2070c <write+8>:            nop           
0x20710 <write+12>:           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, 0x93760, differ a lot.

intro(2) describes the surroundings of system calls and contains a list of available man-pages. An additional look into directory /usr/share/man/sman2 can't hurt, though. A general purpose interface to system calls is described by syscall(2). Anyway, the statement mov 4,%g1 corresponds to the value of SYS_write in /usr/include/sys/syscall.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.