Docsity

The Buffer Bomb - Laboratory Assignment 3 | CS 4400, Assignments of Computer Science

Toggle sidebar
bg1
CS 4400: Computer Systems
Fall 2008
Lab Assignment 3: The Buffer Bomb
Due Date: Monday, October 20 at 11:59p
Introduction
This assignment helps you develop a detailed understanding of the calling stack organization on an IA32
processor. It involves applying a series of buffer overflow attacks on an executable file bufbomb.
Note: In this lab, you will gain firsthand experience with one of the methods commonly used to exploit
security weaknesses in operating systems and network servers. Our purpose is to help you learn about the
run-time operation of programs and to understand the nature of this form of security weakness so that you
can avoid it when you write network and systems code. We do not condone the use of these or any other
form of attack to gain unauthorized access to any system resources. There are criminal statutes governing
such activities. In any case, finding real application vulnerabilities is far more difficult than it used to be, and
most people who want to do something bad would simply download an existing exploit rather than creating
a new one.
Logistics
You will work by yourself in solving the problems for this assignment. The only “hand-in” will be an
automated logging of your successful attacks. Any clarifications and revisions to the assignment will be
sent to the class mailing list.
Hand Out Instructions
Download buflab-handout.tar.gz from the course web page to a (protected) directory in which you
plan to do your work. Give the command “tar -zxvf buflab-handout.tar.gz”. This will cause
a number of files to be unpacked in the directory:
MAKECOOKIE:Generates a “cookie” based on your CADE login.
BUF BO MB:The code you will attack.
SENDSTRING:A utility to help convert between string formats.
All of these programs are compiled to run on Linux machines.
In the following instructions, we will assume that you have copied the three programs to a protected local
directory, and that you are executing them in that local directory.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download The Buffer Bomb - Laboratory Assignment 3 | CS 4400 and more Assignments Computer Science in PDF only on Docsity!

CS 4400: Computer Systems

Fall 2008

Lab Assignment 3: The Buffer Bomb

Due Date: Monday, October 20 at 11:59p

Introduction

This assignment helps you develop a detailed understanding of the calling stack organization on an IA processor. It involves applying a series of buffer overflow attacks on an executable file bufbomb.

Note: In this lab, you will gain firsthand experience with one of the methods commonly used to exploit security weaknesses in operating systems and network servers. Our purpose is to help you learn about the run-time operation of programs and to understand the nature of this form of security weakness so that you can avoid it when you write network and systems code. We do not condone the use of these or any other form of attack to gain unauthorized access to any system resources. There are criminal statutes governing such activities. In any case, finding real application vulnerabilities is far more difficult than it used to be, and most people who want to do something bad would simply download an existing exploit rather than creating a new one.

Logistics

You will work by yourself in solving the problems for this assignment. The only “hand-in” will be an automated logging of your successful attacks. Any clarifications and revisions to the assignment will be sent to the class mailing list.

Hand Out Instructions

Download buflab-handout.tar.gz from the course web page to a (protected) directory in which you plan to do your work. Give the command “tar -zxvf buflab-handout.tar.gz”. This will cause a number of files to be unpacked in the directory:

MAKECOOKIE: Generates a “cookie” based on your CADE login.

BUFBOMB: The code you will attack.

SENDSTRING: A utility to help convert between string formats.

All of these programs are compiled to run on Linux machines.

In the following instructions, we will assume that you have copied the three programs to a protected local directory, and that you are executing them in that local directory.

Making a Cookie

Your “team name” is just your CADE login. A cookie is a string of eight hexadecimal digits that is (with high probability) unique to your team. You can generate your cookie with the makecookie program giving your team name as the argument. For example:

unix> ./makecookie eparker 0x74b6a4f

In four of the five buffer attacks, your objective will be to make your cookie show up in places where it ordinarily would not.

The BUFBOMB Program

The BUFBOMB program reads a string from standard input with a function getbuf having the following C code:

1 int getbuf() 2 { 3 char buf[12]; 4 Gets(buf); 5 return 1; 6 }

The function Gets is similar to the standard library function gets—it reads a string from standard input (terminated by ‘\n’ or end-of-file) and stores it (along with a null terminator) at the specified destination. In this code, the destination is an array buf having sufficient space for 12 characters including the null terminator.

Neither Gets nor gets has any way to determine whether there is enough space at the destination to store the entire string. Instead, they simply copy the entire string, possibly overrunning the bounds of the storage allocated at the destination.

If the string typed by the user to getbuf is no more than 11 characters long, it is clear that getbuf will return 1, as shown by the following execution example:

unix> ./bufbomb Type string: howdy doody Dud: getbuf returned 0x

Typically an error occurs if we type a longer string:

unix> ./bufbomb Type string: This string is too long Ouch!: You caused a segmentation fault!

As the error message indicates, overrunning the buffer typically causes the program state to be corrupted, leading to a memory access error. Your task is to be more clever with the strings you feed BUFBOMB so that it does more interesting things. These are called exploit strings.

Level 0: Candle (10 pts)

The function getbuf is called within BUFBOMB by a function test having the following C code:

1 void test() 2 { 3 int val; 4 volatile int local = 0xdeadbeef; 5 entry_check(3); /* Make sure entered this function properly (^) / 6 val = getbuf(); 7 / Check for corrupted stack (^) */ 8 if (local != 0xdeadbeef) { 9 printf("Sabotaged!: the stack has been corrupted\n"); 10 } 11 else if (val == cookie) { 12 printf("Boom!: getbuf returned 0x%x\n", val); 13 validate(3); 14 } 15 else { 16 printf("Dud: getbuf returned 0x%x\n", val); 17 } 18 }

When getbuf executes its return statement (line 5 of getbuf), the program ordinarily resumes execution within function test (at line 8 of this function). Within the file bufbomb, there is a function smoke having the following C code:

void smoke() { entry_check(0); /* Make sure entered this function properly (^) */ printf("Smoke!: You called smoke()\n"); validate(0); exit(0); }

Your task is to get BUFBOMB to execute the code for smoke when getbuf executes its return statement, rather than returning to test. You can do this by supplying an exploit string that overwrites the stored return pointer in the stack frame for getbuf with the address of the first instruction in smoke. Note that your exploit string may also corrupt other parts of the stack state, but this will not cause a problem, since smoke causes the program to exit directly.

Some Advice:

  • All the information you need to devise your exploit string for this level can be determined by examin- ing a disassembled version of BUFBOMB.
  • Be careful about byte ordering.
  • You might want to use GDB to step the program through the last few instructions of getbuf to make sure it is doing the right thing.
  • The placement of buf within the stack frame for getbuf depends on which version of GCC was used to compile bufbomb. You will need to pad the beginning of your exploit string with the proper number of bytes to overwrite the return pointer. The values of these bytes can be arbitrary.

Level 1: Sparkler (20 pts)

Within the file bufbomb there is also a function fizz having the following C code:

void fizz(int val) { entry_check(1); /* Make sure entered this function properly (^) */ if (val == cookie) { printf("Fizz!: You called fizz(0x%x)\n", val); validate(1); } else printf("Misfire: You called fizz(0x%x)\n", val); exit(0); }

Similar to Level 0, your task is to get BUFBOMB to execute the code for fizz rather than returning to test. In this case, however, you must make it appear to fizz as if you have passed your cookie as its argument. You can do this by encoding your cookie in the appropriate place within your exploit string.

Some Advice:

  • Note that the program won’t really call fizz—it will simply execute its code. This has important implications for where on the stack you want to place your cookie.

Level 2: Firecracker (30 pts)

A much more sophisticated form of buffer attack involves supplying a string that encodes actual machine instructions. The exploit string then overwrites the return pointer with the starting address of these instruc- tions. When the calling function (in this case getbuf) executes its ret instruction, the program will start executing the instructions on the stack rather than returning. With this form of attack, you can get the pro- gram to do almost anything. The code you place on the stack is called the exploit code. This style of attack is tricky, though, because you must get machine code onto the stack and set the return pointer to the start of this code.

Within the file bufbomb there is a function bang having the following C code:

int global_value = 0;

void bang(int val) { entry_check(2); /* Make sure entered this function properly (^) */ if (global_value == cookie) { printf("Bang!: You set global_value to 0x%x\n", global_value); validate(2); } else

Your job for this level is to supply an exploit string that will cause getbuf to return your cookie back to test, rather than the value 1. You can see in the code for test that this will cause the program to go “Boom!.” Your exploit code should set your cookie as the return value, restore any corrupted state, push the correct return location on the stack, and execute a ret instruction to really return to test.

Some Advice:

  • In order to overwrite the return pointer, you must also overwrite the saved value of %ebp. However, it is important that this value is correctly restored before you return to test. You can do this by either
    1. making sure that your exploit string contains the correct value of the saved %ebp in the correct position, so that it never gets corrupted, or 2) restore the correct value as part of your exploit code. You’ll see that the code for test has some explicit tests to check for a corrupted stack.
  • You can use GDB to get the information you need to construct your exploit string. Set a breakpoint within getbuf and run to this breakpoint. Determine parameters such as the saved return address and the saved value of %ebp.
  • Again, let tools such as GCC and OBJDUMP do all of the work of generating a byte encoding of the instructions.
  • Keep in mind that your exploit string depends on your machine, your compiler, and even your cookie. If you want to be sure the come up with a solution that we can validate, do your work on a lab machine and make sure you include your CADE login on the command line to BUFBOMB.

Once you complete this level, pause to reflect on what you have accomplished. You caused a program to execute machine code of your own design. You have done so in a sufficiently stealthy way that the program did not realize that anything was amiss.

Level 4: Nitroglycerin (10 extra credit pts)

If you have completed the first four levels, you have earned 100 points. You have mastered the principles of the run-time stack operation, and you have gained firsthand experience with buffer overflow attacks. We consider this a satisfactory mastery of the material. You are welcome to stop right now.

The next level is for those who want to push themselves beyond our baseline expectations for the course, and who want to face a challenge in designing buffer overflow attacks that arises in real life. This part of the assignment only counts 10 points, even though it requires a fair amount of work to do, so don’t do it just for the points.

From one run to another, especially by different users, the exact stack positions used by a given procedure will vary. One reason for this variation is that the values of all environment variables are placed near the base of the stack when a program starts executing. Environment variables are stored as strings, requiring different amounts of storage depending on their values. Thus, the stack space allocated for a given user depends on the settings of his or her environment variables. Stack positions also differ when running a program under GDB, since GDB uses stack space for some of its own state.

In the code that calls getbuf, we have incorporated features that stabilize the stack, so that the position of getbuf’s stack frame will be consistent between runs. This made it possible for you to write an exploit string knowing the exact starting address of buf and the exact saved value of %ebp. If you tried to use such an exploit on a normal program, you would find that it works sometimes, but it causes segmentation faults at

other times. Hence the name “dynamite”—an explosive developed by Alfred Nobel that contains stabilizing elements to make it less prone to unexpected explosions.

For this level, we have gone the opposite direction, making the stack positions even less stable than they normally are. Hence the name “nitroglycerin”—an explosive that is notoriously unstable.

When you run BUFBOMB with the command-line flag “-n,” it will run in “Nitro” mode. Rather than calling the function getbuf, the program calls a slightly different function getbufn:

int getbufn() { char buf[512]; Gets(buf); return 1; }

This function is similar to getbuf, except that it has a buffer of 512 characters. You will need this additional space to create a reliable exploit. The code that calls getbufn first allocates a random amount of storage on the stack (using library function alloca) that ranges between 0 and 127 bytes. Thus, if you were to sample the value of %ebp during two successive executions of getbufn, you would find they differ by as much as ± 127.

In addition, when run in Nitro mode, BUFBOMB requires you to supply your string 5 times, and it will execute getbufn 5 times, each with a different stack offset. Your exploit string must make it return your cookie each of these times.

Your task is identical to the task for the Dynamite level. Once again, your job for this level is to supply an exploit string that will cause getbufn to return your cookie back to test, rather than the value 1. You can see in the code for test that this will cause the program to go “KABOOM!.” Your exploit code should set your cookie as the return value, restore any corrupted state, push the correct return location on the stack, and execute a ret instruction to really return to testn.

Some Advice:

  • You can use the program SENDSTRING to send multiple copies of your exploit string. If you have a single copy in the file exploit.txt, then you can use the following command:

unix> cat exploit.txt | ./sendstring -n 5 | ./bufbomb -n -t eparker

You must use the same string for all 5 executions of getbufn. Otherwise it will fail the testing code used by our grading server.

  • The trick is to make use of the nop instruction. It is encoded with a single byte (code 0x90). You can place a long sequence of these at the beginning of your exploit code so that your code will work correctly if the initial jump lands anywhere within the sequence.
  • You will need to restore the saved value of %ebp in a way that is insensitive to variations in stack positions.

the least significant byte first. Note also that it only generated two of the four bytes at the end with value

  1. Had we not added this padding, OBJDUMP gets even more confused and does not emit all of the bytes we want. Finally, we can read off the byte sequence for our code (omitting the final 0s) as:

68 ef cd ab 89 83 c0 11 98 ba dc fe