Lab2 Instruction
Lab2 Instruction
Introduction
In this lab, you will learn how buffer overflows and other memory vulnerabilities are used
to takeover vulnerable programs. The goal is to investigate a program I provide and
then figure out how to use it to gain shell access to systems.
In 1996 Aleph One wrote the canonical paper on smashing the stack. You should read
this as it gives a detailed description of how stack smashing works. Today, many
compilers and operating systems have implemented security features, which stop the
attacks described in the paper. However, it still provides very relevant background for
newer attacks and (specifically) this lab assignment.
http://www1.telhai.ac.il/sources/private/academic/cs/557/2659/Materials/Smashing.pdf
http://www.enderunix.org/docs/en/bof-eng.txt
Software Requirements
All required files and source code are packed in the provided Lab 2 virtual machine.
The Kali Linux VM has all the required files. Select the VM named Lab2-
BufferOverflows for this lab.
Login the Kali Linux with username root, and password [TBA in the class].
In the Kali Linux, you should be able to see a folder named Lab2-BufferOverflows. This
file contains all of the source code for the lab 2.
There are many protections in current compilers and operating systems to stop stack
attacks like the one we want to do. We have to disable some security options to allow
the exploit to work (Note that the VM image you get has configured the environemnt).
Disable Address Space Layout Randomization
Address Space Layout Randomization (ASLR) is a security features used in most
Operating system today. ASLR randomly arranges the address spaces of processes,
including stack, heap, and libraries. It provides a mechanism for making the exploitation
hard to success. You can configure ASLR in Linux using the
/proc/sys/kernel/randomize_va_space interface. The following values are supported:
0 – No randomization
1 – Conservative randomization
2 – Full randomization
Disable ASLR, run:
$ echo 0 > /proc/sys/kernel/randomize_va_space
Enable ASLR, run:
$ echo 2 > /proc/sys/kernel/randomize_va_space
Note that you will need root privilege to configure the interface. Using vi to modify the
interface may have errors. The screenshot below shows the value of
/proc/sys/kernel/randomize_va_space
However, this configuration will not survive after a reboot. You will have to configure this
in sysctl. Add a file /etc/sysctl.d/01-disable-aslr.conf containing:
kernel.randomize_va_space = 0
This will permanently disable ASLR.
The goal of the exploitation is to teach you how buffer overflows work. You must gain a
shell by passing a malicious input into a vulnerable program. The vulnerability
takes as input a file named "badfile". Your job is to create a badfile that results in the
vulnerable program producing a shell. Note that you also have a nop sled to make the
vulnerability work even if your shellcode moves by a few bytes. In the Lab2-
BufferOverflows folder, it contains the C files you need to use. The screenshot below
shows that.
BOF.c
In BOF.c there is an un-bounded strcpy, which means anything that is not null-
terminated will overwrite the buffer boundaries and (hopefully) put some information into
the stack that you will design. Your exploit must work with my version of BOF.c (can't
change it to make your code work).
#include <stdio.h>
int main( ) {
char *name[2];
name[0] = "/bin/sh";
name[1] = NULL;
execve(name[0], name, NULL);
}
To compile the testShellCode.c and createBadfile.c, you do not need to add the compile
flags mentioned early. You can just simplely compile it with gcc
$ gdb BOF
You need to understand the assembly code to find where the return address is on the
stack. Next, type run in the GDB to execute the BOF program.
$ (gdb) run
Note that you can always type help in the GDB to learn the commands.
Happy Exploiting!