Lab 2: Disassembling and Defusing a Binary Bomb
Assigned | Friday, April 13 [! :)] |
---|---|
Due Date | Wednesday, April 25 |
Files | Available
at https://courses.cs.washington.edu/courses/cse351/18sp/uwnetid/<username>/lab2-bomb.tar
Note: substitute your UWNetID for <username> and use a web browser (not wget since you will need to authenticate using your browser).
|
Video | You may find this video (or with captions) helpful for getting started with the lab. |
Submissions | Submit defuser.txt
and lab2reflect.txt via
the Canvas
assignments page (go to the Labs section, not the
Homeworks section). Be sure to follow the detailed formatting rules
described below. |
Learning Objectives
- Gain basic familiarity with x86-64 assembly instructions and how they are used to implement comparison, loops, switch statements, recursion, pointers, and arrays.
- Gain experience using the
gdb
debugger to step through assembly code and other tools such asobjdump
.
Overview
The nefarious Dr. Evil has planted a slew of “binary
bombs” on our machines. A binary bomb is a program that consists
of a sequence of phases. Each phase expects you to type a particular
string on stdin
(standard input). If you type the correct string, then
the phase is defused and the bomb proceeds to the next
phase. Otherwise, the bomb explodes by printing “BOOM!!!”
and then terminating. The bomb is defused when every phase has been
defused.
There are too many bombs for us to deal with, so we are giving everyone a bomb to defuse. Your mission, which you have no choice but to accept, is to defuse your bomb before the due date. Good luck, and welcome to the bomb squad!
Code for this lab
Everyone gets a different bomb to diffuse! Substitute <username> in the URL below with your UWNetID in order to find yours.
Go
to https://courses.cs.washington.edu/courses/cse351/18sp/uwnetid/<username>/lab2-bomb.tar
with your web browser to download lab2-bomb.tar
. You need to use your browser because you will authenticate with your UW NetID.
If you are using attu
, first download the tar file
to your local machine and then use scp
to move the file to attu
(use
the scp
command on terminal for Mac and Linux users and Windows with
bash users; for a user interface use WinSCP on Windows and Cyberduck
on Mac). For help on scp
, visit
https://courses.cs.washington.edu/courses/cse391/16au/handouts/MovingAndEditingFiles_12sp.pdf.
Running tar xvf lab2-bomb.tar
from the terminal in the directory where lab2-bomb.tar
is located will extract the lab files to a directory called bomb$NUM
(where $NUM is the ID of your bomb) with the following files:
bomb
- The executable binary bombbomb.c
- Source file with the bomb's main routinedefuser.txt
- File in which you write your defusing solutionlab2reflect.txt
- File for your Reflection answers
Instructions
You should do this assignment on a 64-bit CSE Linux VM or a CSE lab Linux machine or on attu. Be sure to test your solution on one of those platforms before submitting it, to make sure it works when we grade it! In fact, there is a rumor that Dr. Evil has ensured the bomb will always blow up if run elsewhere. There are several other tamper-proofing devices built into the bomb as well, or so they say.
Your job is to find to correct strings to defuse the bomb. Look at the Tools section for ideas and tools to use. Two of the best ways are to (a) use a debugger to step through the disassembled binary and (b) print out the disassembled code and step through it by hand.
The bomb has 5 regular phases. The 6th phase is extra credit, and rumor has it that a secret 7th phase exists. If it does and you can find and defuse it, you will receive additional extra credit points. The phases get progressively harder to defuse, but the expertise you gain as you move from phase to phase should offset this difficulty. Nonetheless, the latter phases are not easy, so please don't wait until the last minute to start! (If you're stumped, check the Hints section at the end of this document.)
The bomb ignores blank input lines. If you run your bomb with a command line argument, for example,
./bomb defuser.txt
then it will read the input lines from defuser.txt until it reaches
EOF (end of file), and then switch over to stdin (standard input from
the terminal). In a moment of weakness, Dr. Evil added this feature so
you don't have to keep retyping the solutions to phases you have
already defused, instead you can put them in defuser.txt
.
To avoid accidentally detonating the bomb, you will need to learn how to single-step through the assembly code in gdb and how to set breakpoints. You will also need to learn how to inspect both the registers and the memory states. One of the nice side-effects of doing the lab is that you will get very good at using a debugger. This is a crucial skill that will pay big dividends the rest of your career.
Resources
There are many online resources that will help you understand any assembly instructions you may encounter. In particular, the instruction references for x86-64 processors distributed by Intel and AMD are exceptionally valuable. They both describe the same ISA, but sometimes one may be easier to understand than the other.
Useful for this Lab
Important Note: The instruction format used in these manuals is known as “Intel format”. This format is very different than the format used in our text, in lecture slides, and in what is produced by gcc, objdump and other tools (which is known as “AT&T format”. You can read more about these differences in our textbook (on p. 177 of the 3e) or on Wikipedia. The biggest difference is that the order of operands is SWITCHED. This also serves as a warning that you may see both formats come up in web searches.
Not Directly Useful, but Good Brainfood Nonetheless
- Intel's Intel 64 and IA-32 Architectures Software Developer Manuals, in particular volumes 1, 2, and 3.
- AMD64 Architecture Programmer's Manual Volume 1: Application Programming
- AMD64 Architecture Programmer's Manual Volume 2: System Programming
- AMD64 Architecture Programmer's Manual Volume 4: 128-bit and 256 bit media instructions
- AMD64 Architecture Programmer's Manual Volume 5: 64-Bit Media and x87 Floating-Point Instructions
x86-64 Calling Conventions
The x86-64 ISA passes the first six arguments to a function in
registers. Registers are used in the following
order: rdi
, rsi
, rdx
, rcx
, r8
, r9
. The
return value for functions is passed in rax
.
Using sscanf
It will be helpful to first familiarize yourself with scanf
("scan format"), which reads in data
from stdin (the keyboard) stores it according to the parameter format into the
locations pointed to by the additional arguments:
int i;
printf("Enter a number: ");
scanf("%d", &i);
- The
printf
prints a prompt, once the user enters in a number and hits enterscanf
will store the input from stdin intoi
with the format of an integer. Notice howscanf
uses the address ofi
as the argument.
sscanf
("string scan format"), which is similiar to scanf
but rather than read in data
from stdin it parses a string that is provided as an argument:
char *mystring = "123, 456"
int a, b;
sscanf(mystring, "%d, %d", &a, &b);
- The first argument,
mystring
, is parsed according to the format string,"%d, %d"
. - After this code is run,
a = 123
andb = 456
.
Documentation for sscanf, scanf, and printf
Tools (Read This!!)
There are many ways of defusing your bomb. You can print out the assembly and examine it in great detail without ever running the program, and figure out exactly what it does. This is a useful technique, but it not always easy to do. You can also run it under a debugger, watch what it does step by step, and use this information to defuse it. Both are useful skills to develop.
We do make one request, please do not use brute force! You could write a program that will try every possible key to find the right one, but the number of possibilities is so large that you won't be able to try them all in time.
There are many tools which are designed to help you figure out both how programs work, and what is wrong when they don't work. Here is a list of some of the tools you may find useful in analyzing your bomb, and hints on how to use them.
gdb
: The GNU debugger is a command-line debugger tool available on virtually every platform. You can trace through a program line by line, examine memory and registers, look at both the source code and assembly code (we are not giving you the source code for most of your bomb), set breakpoints, set memory watch points, and write scripts. Here are some tips for usinggdb
.- To keep the bomb from blowing up every time you type in a wrong input, you'll want to learn how to set breakpoints.
- The CS:APP Student Site has a very handy gdb summary (there is also a more extensive tutorial).
- For other documentation, type
help
at thegdb
command prompt, or type "man gdb", or "info gdb" at a Linux-shell prompt. Some people also like to rungdb
under gdb-mode in Emacs.
objdump -t bomb > bomb_symtab
: This will print out the bomb's symbol table into a file calledbomb_symtab
. The symbol table includes the names of all functions and global variables in the bomb, the names of all the functions the bomb calls, and their addresses. You may learn something by looking at the function names!objdump -d bomb > bomb_disas
: Use this to disassemble all of the code in the bomb into a file calledbomb_disas
. You can also just look at individual functions. Reading the assembler code can tell you how the bomb works. Althoughobjdump -d
gives you a lot of information, it doesn't tell you the whole story. Calls to system-level functions may look cryptic. For example, a call tosscanf
might appear as:8048c36: e8 99 fc ff ff call 80488d4 <_init+0x1a0>
To determine that the call was tosscanf
, you would need to disassemble withingdb
.strings -t x bomb > bomb_strings
: This utility will print the printable strings in your bomb and their offset within the bomb into a file calledbomb_strings
.
Looking for a particular tool? How about documentation? Don't
forget, the commands apropos
and man
are
your friends. In particular, man ascii
is more useful
than you'd think. If you get stumped, use the course's discussion
board.
Hints
If you're still having trouble figuring out what your bomb is doing, here are some hints for what to think about at each stage: (1) comparison, (2) loops, (3) switch statements, (4) recursion, (5) pointers and arrays, (6) sorting linked lists.
Lab 2 Reflection
REMINDER: You will need to be on the CSE VM or attu in order to get addresses that are consistent with our solutions.Start with a fresh copy of lab0.c
and examine part2()
using the following commands:
$ wget https://courses.cs.washington.edu/courses/cse351/18sp/labs/lab0.c
$ gcc -g -std=c99 -o lab0 lab0.c
$ gdb lab0
(gdb) layout split
(gdb) break fillArray
(gdb) break part2
(gdb) run 2
Now answer the following questions:
- What address is the variable
value
stored at in memory? [0.5 pt] - What is the relative address (i.e. how many bytes forwards or backwards) of the variable
array
compared to the variablevalue
? [0.5 pt] - Consider the lea instruction at address 0x400842. Replace it with another lea instruction that has the same effect at this position in this program but does not use %rbp. [1 pt]
- Give the lowest and highest addresses of the instructions that perform the
i * 3 + 2
calculation within the loop (do not include the assert and do not include any instructions that store the result somewhere). [1 pt]
You will find the following GDB commands useful: nexti
, finish
, print
, and refresh
.
Submission Instructions (!)
All you submit is your defuser.txt
and lab2reflect.txt
files. So that our
grading scripts can use your file as-is to defuse your
bombs, please make sure it obeys these formatting rules, else
our script is likely to conclude you defused zero bombs:
- Put your answer for each phase in one line. Your answer for phase 1 should be in the first line, answer for phase 2 on the second line, and so on.
- Do not put your name or other information at the top of the file. Again, you want the first line in the file to be your answer for phase 1.
- Do not add numbering or other “comments” for your answers (e.g.,
1. This is my answer for phase 1
). - Make sure all your answers, including the last one, have a newline character afterwards, so even your last-phase answer is a complete line with a newline. This last newline is important for our grading script even though you will not notice the difference in your own testing.