Printf
Printf
1 Overview
This exercise introduces the printf function and encourages the student to explore the manner in which the
function references memory addresses in response to its given format specification. This lab provides an
introduction to techniques that are used in the more advanced printf labs (formatstring and format64).
1.1 Background
This exercise assumes the student has some basic C language programming experience and is somewhat
familiar with the use of gdb1
No coding is required in this lab, but it will help if the student can understand a simple C program. The
gdb program is used to explore the executing program, including viewing a bit of its disassembly. Some
assembly language background would be helpful in performing the lab, but is not necessary.
2 Lab Environment
This lab runs in the Labtainer framework, available at http://nps.edu/web/c3o/labtainers. That site includes
links to a pre-built virtual machine that has Labtainers installed, however Labtainers can be run on any Linux
host that supports Docker containers or on Docker Desktop on PCs and Macs.
From your labtainer-student directory start the lab using:
labtainer printf
A link to this lab manual will be displayed.
3 Tasks
3.1 Review the printTest.c program
A terminal opens when you start the lab. At that terminal, view the printTest.c program. Use either vi or
nano, or just type less printTest.c.
Observe the syntax of the first printf statement. The first parameter is a format string that contains
literal text to be displayed, and one or more one or more conversion specifications that determine how any
remaining parameters are displayed. The conversion specification begins with the % symbol. In the first
printf statement, the conversion specification is a %d, which directs printf to display the parameter as an
integer. Thus, the value of var1 would be displayed as an integer following the string ”var1 is: ”.
The \n “escape n” sequence causes printf to generate a newline.
The second printf statement illustrates how we can display the values of multiple parameters. In this
case, the hexadecimal representation of an integer (the %x) followed by a string (using the %s conversion
specification).
The printf function has an extremely rich set of conversion specifications, but most those are not impor-
tant for this lab. What is important for this lab is the manner in which printf references memory to find the
values to be displayed.
1
This lab manual provides detailed gdb commands to accomplished the prescribed tasks, and can serve as an introduction to
gdb.
Labtainers 2
The third printf statement is vulnerable to mischief, as we will see in this lab.
./mkit
./printTest
low memory
In figure 1, we see the var1 value has been pushed on the stack, followed by the pointer of the format
string.
%8x
which directs printf to display the word as an 8 digit hexadecimal value. We’ll combine a raft of those
format conversions and provide that as input when the program prompts us for a string
AAAA%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.%8x.
Run the program (without gdb) and provide the above string as input. Where do the displayed values come
from? Run the program in gdb again, this time set a break at line number of the vulnerable call to printf and
use run to start the program. Before the program reaches your breakpoint, it will primpt you to enter the
string. Paste the above string and the program will then break at the (almost) call to printf. Use
display/i $pc
nexti
<return>....
to step to the call to printf@plt and then display the stack content.
x/20x2 $esp
Find the first (and only) parameter to the printf statement and confirm it is the address of your user-provided
format string:
x/s <address>
The use the c command to continue, allowing the program to output the results of the printf statement.
Compare that output to what you see in memory just past the address of the format string.3
4 Submission
After finishing the lab, go to the terminal on your Linux system that was used to start the lab and type:
stoplab
When you stop the lab, the system will display a path to the zipped lab results on your Linux system. Provide
that file to your instructor, e.g., via the Sakai site.
This lab was developed for the Labtainers framework by the Naval Postgraduate School, Center
for Cybersecurity and Cyber Operations under sponsorship from the National Science Foundation.
This work is in the public domain, and cannot be copyrighted.
3
You may notice the content of memory changes between each run of the program. This is due to Address Space Layout
Randomization. Google it.