26.07.2018 Views

hacking-the-art-of-exploitation

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Bouncing <strong>of</strong>f linux-gate refers to a shared object, exposed by <strong>the</strong> kernel,<br />

which looks like a shared library. The program ldd shows a program’s<br />

shared library dependencies. Do you notice anything interesting about<br />

<strong>the</strong> linux-gate library in <strong>the</strong> output below?<br />

matrix@loki /<strong>hacking</strong> $ $ uname -a<br />

Linux <strong>hacking</strong> 2.6.17 #2 SMP Sun Apr 11 03:42:05 UTC 2007 i686 GNU/Linux<br />

matrix@loki /<strong>hacking</strong> $ cat /proc/sys/kernel/randomize_va_space<br />

1<br />

matrix@loki /<strong>hacking</strong> $ ldd ./aslr_demo<br />

linux-gate.so.1 => (0xffffe000)<br />

libc.so.6 => /lib/libc.so.6 (0xb7eb2000)<br />

/lib/ld-linux.so.2 (0xb7fe5000)<br />

matrix@loki /<strong>hacking</strong> $ ldd /bin/ls<br />

linux-gate.so.1 => (0xffffe000)<br />

librt.so.1 => /lib/librt.so.1 (0xb7f95000)<br />

libc.so.6 => /lib/libc.so.6 (0xb7e75000)<br />

libpthread.so.0 => /lib/libpthread.so.0 (0xb7e62000)<br />

/lib/ld-linux.so.2 (0xb7fb1000)<br />

matrix@loki /<strong>hacking</strong> $ ldd /bin/ls<br />

linux-gate.so.1 => (0xffffe000)<br />

librt.so.1 => /lib/librt.so.1 (0xb7f50000)<br />

libc.so.6 => /lib/libc.so.6 (0xb7e30000)<br />

libpthread.so.0 => /lib/libpthread.so.0 (0xb7e1d000)<br />

/lib/ld-linux.so.2 (0xb7f6c000)<br />

matrix@loki /<strong>hacking</strong> $<br />

Even in different programs and with ASLR enabled, linux-gate.so.1 is<br />

always present at <strong>the</strong> same address. This is a virtual dynamically shared object<br />

used by <strong>the</strong> kernel to speed up system calls, which means it’s needed in<br />

every process. It is loaded straight from <strong>the</strong> kernel and doesn’t exist anywhere<br />

on disk.<br />

The important thing is that every process has a block <strong>of</strong> memory containing<br />

linux-gate’s instructions, which are always at <strong>the</strong> same location, even<br />

with ASLR. We are going to search this memory space for a certain assembly<br />

instruction, jmp esp. This instruction will jump EIP to where ESP is pointing.<br />

First, we assemble <strong>the</strong> instruction to see what it looks like in machine code.<br />

matrix@loki /<strong>hacking</strong> $ cat > jmpesp.s<br />

BITS 32<br />

jmp esp<br />

matrix@loki /<strong>hacking</strong> $ nasm jmpesp.s<br />

matrix@loki /<strong>hacking</strong> $ hexdump -C jmpesp<br />

00000000 ff e4 |..|<br />

00000002<br />

matrix@loki /<strong>hacking</strong> $<br />

Using this information, a simple program can be written to find this<br />

pattern in <strong>the</strong> program’s own memory.<br />

Countermeasures 385

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!