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.

eader@<strong>hacking</strong>:~/booksrc $ sudo su jose<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $ id<br />

uid=501(jose) gid=501(jose) groups=501(jose)<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $<br />

As <strong>the</strong> user jose, <strong>the</strong> simplenote program will run as jose if it is executed,<br />

but it won’t have access to <strong>the</strong> /tmp/notes file. This file is owned by <strong>the</strong> user<br />

reader, and it only allows read and write permission to its owner.<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $ ls -l /tmp/notes<br />

-rw------- 1 reader reader 36 2007-09-07 05:20 /tmp/notes<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $ ./simplenote "a note for jose"<br />

[DEBUG] buffer @ 0x804a008: 'a note for jose'<br />

[DEBUG] datafile @ 0x804a070: '/tmp/notes'<br />

[!!] Fatal Error in main() while opening file: Permission denied<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $ cat /tmp/notes<br />

cat: /tmp/notes: Permission denied<br />

jose@<strong>hacking</strong>:/home/reader/booksrc $ exit<br />

exit<br />

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

This is fine if reader is <strong>the</strong> only user <strong>of</strong> <strong>the</strong> simplenote program; however,<br />

<strong>the</strong>re are many times when multiple users need to be able to access certain<br />

portions <strong>of</strong> <strong>the</strong> same file. For example, <strong>the</strong> /etc/passwd file contains account<br />

information for every user on <strong>the</strong> system, including each user’s default login<br />

shell. The command chsh allows any user to change his or her own login shell.<br />

This program needs to be able to make changes to <strong>the</strong> /etc/passwd file, but<br />

only on <strong>the</strong> line that pertains to <strong>the</strong> current user’s account. The solution to<br />

this problem in Unix is <strong>the</strong> set user ID (setuid) permission. This is an additional<br />

file permission bit that can be set using chmod. When a program with<br />

this flag is executed, it runs as <strong>the</strong> user ID <strong>of</strong> <strong>the</strong> file’s owner.<br />

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

/usr/bin/chsh<br />

reader@<strong>hacking</strong>:~/booksrc $ ls -l /usr/bin/chsh /etc/passwd<br />

-rw-r--r-- 1 root root 1424 2007-09-06 21:05 /etc/passwd<br />

-rwsr-xr-x 1 root root 23920 2006-12-19 20:35 /usr/bin/chsh<br />

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

The chsh program has <strong>the</strong> setuid flag set, which is indicated by an s in <strong>the</strong><br />

ls output above. Since this file is owned by root and has <strong>the</strong> setuid permission<br />

set, <strong>the</strong> program will run as <strong>the</strong> root user when any user runs this program.<br />

The /etc/passwd file that chsh writes to is also owned by root and only allows<br />

<strong>the</strong> owner to write to it. The program logic in chsh is designed to only allow<br />

writing to <strong>the</strong> line in /etc/passwd that corresponds to <strong>the</strong> user running <strong>the</strong><br />

program, even though <strong>the</strong> program is effectively running as root. This<br />

means that a running program has both a real user ID and an effective user<br />

ID. These IDs can be retrieved using <strong>the</strong> functions getuid() and geteuid(),<br />

respectively, as shown in uid_demo.c.<br />

Programming 89

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

Saved successfully!

Ooh no, something went wrong!