Assignment: UAF
Contents
Changelog:
- 19 April 2025: update binaries; fix bug re: print_escaped and missing “
Your Task
-
Download the two versions of a vulnerable (uafA.exe and uafB.exe) [last updated 19 March 2025] and their source code (uafA.cc and uafB.cc).
-
Start with the “uafA” version of the program, which is much simpler. Submit a file called
uafA-attack.py3such that runningpython3 uafA-attack.py3 > commands.txt ./uafA.exe < commands.txt(or submit a similar file named
uafA-attack.py2oruafA-attack.cc, etc., which we will run similarly to prior assignmnets, depending ont he type of file) will produce output that ends withI recommend YOUR-COMPUTING-ID get a grade of A for the UAF assignment. > Exiting.(We do not care about other output the program produces.)
Your exploit must work on the executable version of the programs we supply, not any slightly different executable you produce.
-
do the same thing for the “uafB.exe” version of the program.
About the programs / Hints
General Usage
-
Both of these programs prompt for commands and making help gives a list of commands:
> help Available commands: setup-info set-info-0 STRING set-info-1 STRING set-info-2 STRING free-info setup-grader ASSIGNMENT grade STUDENT free-grader exitThe grader commands can be used as follows:
> setup-grader UAF (grader address 0x1234567) > grade mst3k I recommend mst3k get a grade of F for the UAF assignment.(The output will vary slightly between uafA and uafB)
Your job is to supply input to setup the grader, so that you can run the commands “grade YOUR-COMPUTING-ID” then “exit”, and the last lines of output from the program will be:
I recommend YOUR-COMPUTING-ID get a grade of A for the UAF assignment. > Exiting.(The last line is a normal prompt for the exit command followed by its output.)
-
The programs implement the commands above using an
InfoTrackerclass and aGraderclass. The Grader class is an abstract superclass which is implemented by theGraderImplsubclass.C++ implements both of these classes using VTables.
The
setup-infoandsetup-gradercommands create new instances of these classes and store pointers to them in global variables. Thefree-infoandfree-gradercommands delete these instances, but do not reset the pointers. -
The
setup-infoandsetup-gradercommands show the addresses of theInfoTrackerandGraderobjects they create to make it easier for you to determine whether they were allocated in the same address. (On a less cooperative program, one might use a debugger to determine this.)
Notes on C++
-
In C++
std::cout << A << B << C;printsA,B, andCto stdout.std::cout << ... << std::endlprints a newline to stdout and flushes stdout. -
C++ uses “name mangling” to encode function and method names. Since C++, like Java, supports having multiple functions with the same name but different parameters, this is done even for “normal” functions. (For example: _Z13read_argumentiPc is
read_argument(int, char*). A version ofread_argumentwith different peramaters would have a different mangled name.) You can use thec++filtutility to translate these “managled” name. For example,objdump -d uafA.exe | c++filt >output.txtwill write disasembly with the “mangled” names replaced with more readable names.
-
A method
fooor a classAis calledA::foowhen it’s full name is written out. If -
In this C++ code, we follow a coding style where instance variables have a
_at the end of their names.
The vulnerability
-
The programs have a use-after-free vulnerability which provides an attacker substantial control. One example of how this can be triggered to cause a crash is as follows:
> setup-info (info address XXX) > free-info > setup-grader UAF (grader address XXX) > set-info-0 XXX" info[0]: "XXX > grade fooThis will result in a segmentation fault. What happens is that the struct used by the info tracker has been freed but there’s still a pointer to it that the
set-info-NUMBERcommand tries to use.In the code, you will see that the pointer to the info object is stored in a global variable called
info_trackerand the pointer to the grader object is stored in a global variable calledgrader. (The outputinfo[0]: "XXX"is showing that the value of info slot 0 isXXXafter being set.)Since the object for grading was allocated to the same place,
set-info-NUMBERcan overwrite information used by thegradefunction. This happens to include the virtual table pointer. Thegradecommand tries to use this virtual table pointer to find a function to output the grade, and because it’s corrupted (by writing XXX there), it fails. -
You can use this use-after-free vulnerability to make the programs produce the desired output.
-
In the case of
uafA, you can change the information used by the normal grade-outputting function. -
In the case of
uafB, you will probably need to take advantage of changing the VTable in use. Since the executable we supply does not make writable regions of memory executable, you should expect to look for existing code that would make sense to jump to.