CS 642 Homework #4: Warning!
CS 642 Homework #4: Warning!
Warning!
In this assignment, you will construct and launch attacks against a
vulnerable computer on the CS network. The network administrators
are aware of this homework exercise and expect attacks against the
machine. However, you may not use the techniques in this homework
to attack any other machine in the CS network or greater Internet,
and you may not use root-level access on the target machine to launch
further attacks. Use your head: there are University and criminal
repercussions for such activity.
2. Construct a buffer overflow exploit and run it against a vulnerable HTTP server to gain
user-level access to the remote machine. The server runs in a chroot jail, so you will
have very limited filesystem access. This stage has bypass instructions if you cannot
build a successful overflow exploit.
3. Gain root access by constructing a format string attack against a vulnerable setuid-root
program running in the chroot jail. This stage has bypass instructions if you cannot
build a successful format string attack.
4. As root, break out of the chroot jail to gain full filesystem access. Copy the password
file /etc/shadow to your CSL lab account.
Email your tarball or ZIP file to [email protected] before 11:59 p.m. on the due date.
• You follow the directions listed here, but things do not work as described.
• You think your exploit is valid, but it still does not work.
[ nmap results follow ]
Q3: How does nmap identify whether a port is open, filtered, or closed?
There is a simple http server running on 128.105.48.223. This server contains a buffer
overflow vulnerability that will be our point of entry to the system in Stage 2. You can
actually verify that the server correctly delivers files by opening http://128.105.48.223/index.
html in your favorite web browser.
Q4: Explain the buffer overflow bug in httpd.c. How serious is this bug?
Q5: Propose a source code patch that will remove the vulnerability.
Now, you will construct an exploit for this vulnerability. The compiled httpd running on
128.105.48.223 is located at ~mihai/public/642/httpd. Grab a copy so that you can work
on the exploit locally. You will attack 128.105.48.223 only when your exploit first works
against a local httpd.
#
# This file (. cshrc ) is read by each new ...
#
# There are comments to explain most of what ...
[ output continues ]
After you construct a buffer that you believe exploits the overflow vulnerability, you can
test the buffer by simply feeding it to httpd as input via a UNIX pipe or redirected input.
You can build a buffer by writing a text file that specifies each byte of your buffer as a
two-character hexadecimal duplet. For example, the byte sequence 0x3f5c88 would be listed
in the text file as “ 3f 5c 88”. The program ~mihai/public/642/make_bin will read the text
file as input and output a file with the binary representation of the hex duplets. The file
overflow.bin would then be the input to httpd.
$ ./ httpd < overflow . bin
$ cat overflow . hex | make_bin | ./ httpd
Q6: Analyze the assembly code in ~mihai/public/642/shellcode.s. What is this code doing?
Although you may take any approach you like to build your exploit buffer, we suggest
the following:
2. Figure out how to include shellcode in the buffer. You can use the hexadecimal-encoded
shellcode at ~mihai/public/642/shellcode.hex. You just need to figure out where to
put the shellcode in your buffer.
3. Identify the address where the shellcode will appear in memory after the buffer overrun.
Make your buffer overwrite the return address with this value. As a result, when the
function returns, the program’s execution will jump into the shellcode.
Special note. Even a correct exploit will segfault when tested on the CSL lab machines.
A correct exploit overwrites the return address to specify the value in the stack where the
shellcode resides. The CSL machines have a security restriction in place that will not allow
instructions on the stack to execute. When the program reads the overwritten return address
and attempts to execute code in the stack, it will segfault. The 128.105.48.223 machine does
not have this restriction.
To test your exploit on a CSL lab machine, you must use a debugger such as gdb. At a
return instruction, the return address about to be used will be at the top of the stack. To see
if your exploit will work, run httpd inside the debugger. Insert a breakpoint at the return
instruction that uses the clobbered return address. Run the program, with the exploit buffer
as input, up to the breakpoint. Check that the return address targets the shellcode in your
buffer.
.........
Once you have an exploit that you believe may be successful, run it against the remotely
executing httpd on 128.105.48.223. The weird syntax of the command that sends the exploit
to 128.105.48.223 will keep the connection open after sending your exploit code:
$ ( cat overflow . bin ; cat ) | nc 128.105.48.223 80
If your exploit is successful, you will receive a bash-shell prompt on the remote machine.
To logout of 128.105.48.223 after a successful attack, type “ exit” to close the bash prompt
and then type “ ^d” (control-d) to close the locally-executing cat process. Include the
overflow.hex file in your tarball or ZIP file that you hand in.
Alternative instructions for Stage 2: If you are unable to construct a buffer that
successfully triggers the overflow vulnerability to give you a shell on 128.105.48.223, you
can connect to 128.105.48.223 via ssh to continue the homework assignment. Your level of
access will be equivalent to the access gained via a successful exploit.
4. The x86 processor is little endian. This affects how you specify values that overwrite
the return address.
Q8: Propose a source code patch that will remove the vulnerability.
You will now create a format string attack against su. This attack is different from the
buffer overflow exploit: you will not insert shellcode. Instead, you will use the format string
to overwrite just a return address. When the return address is used, the program’s execution
will bypass the code of su.c that performs safety checks such as password verification and
will execute a shell with root privilege.
Format string attacks are exceptionally complicated, so we have written almost the entire
format string for you. Your task is to specify what address should overwrite the existing
return address. You must analyze the code of su to find an existing instruction to which the
return instruction should jump.
After you identify such an address, generate the format string using the command
make_string located at ~mihai/public/642/make_string. This example will create a format
string that will overwrite the return address with the address of the main function:
$ make_string 0 x8048970 > format . bin
Q9: Look at the format string produced by make_string. How does this attack work? What is
the meaning of the bizarre format string? Why does this string cause a return address to be
overwritten?
Special note. Even a correct exploit will segfault when tested on the CSL lab machines.
The 128.105.48.223 machine does not have this restriction. It is sufficient for you to identify
the right instruction to which the return instruction should jump. You can test your exploit
using a debugger such as gdb. Run su inside the debugger and set a breakpoint at the
instruction you identified. If the exploit is correct, the execution will stop at that instruction.
.........
Once you have a format string that executes a shell locally, attack the remote su process.
Connect back to 128.105.48.223 via the buffer overflow or via the alternate instructions.
Move into the directory /home/student and make a new directory for yourself. Upload your
format string to your new directory. You can use scp and the student account to upload
your format string file, without losing any points. Run the attack against su. A successful
attack will give you a bash prompt with root privileges.
root
Clean up after yourself by removing the directory and any files that you added. If your
attack is successful, include your format string in the tarball or ZIP file that you
hand in.
Alternative instructions for Stage 3: If you are unable to construct a format string
that successfully triggers the vulnerability in su, you can gain root access by executing
/bin/make-me-root.
This is worth 0 (zero) points.
You are still inside the chroot jail and have limited filesystem access, but you now have
the privileges that you need to break out of the jail.
Q11: The chroot jail on 128.105.48.223 was insecure and allowed you to break out. How could
this jail have been made secure?
Q12: You attacked the programs /bin/httpd and /bin/su inside the chroot jail. What is the
complete path to these programs from the actual filesystem root directory?