Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Watch calls with ltrace and strace

After this lesson: I can see what a program asks the library and the system.

55 min lesson8 commandsReverse engineering lab: reading programs with gdb, radare2, strace and ltrace on one VM

See what a program asks for

ltrace and strace are tracing tools: they print each call a program makes, with its arguments, while it runs.

That matters because a program's requests, such as which file it opens, show what it needs. You do not have to read any assembly. You will trace a practice program that you run yourself.

By the end of this lesson

Note

I can see what a program asks the library and the system.

Key words

WordWhat it meansExample
Library calla request to ready-made code that comes with Linuxfopen
System calla request from a program to the operating systemopenat
Kernelthe core of the operating system, which does the work
Tracea printed list of the calls a program made
Return valuethe answer a call gives back
ENOENTthe error for a file that does not exist

The cells students fill in are left blank in this preview.

Two tools watch two layers

A program asks for things in two layers. A library call goes to ready-made code, such as fopen, which opens a file. A system call goes to the kernel, which does the real work. Opening a file ends in the system call openat.

ltrace prints the library calls. strace prints the system calls. Neither one needs you to read any assembly.

Worked example: one file, two views

ToolIt watchesA line it prints for the practice program
ltraceLibrary callsfopen("/tmp/shop-settings.txt", "r") = nil
straceSystem callsopenat(AT_FDCWD, "/tmp/shop-settings.txt", O_RDONLY) = -1 ENOENT (No such file or directory)

The cells students fill in are left blank in this preview.

Write the practice program

Command
printf '#include <stdio.h>\nint main(void) {\n    FILE *f = fopen("/tmp/shop-settings.txt", "r");\n    if (f) { fclose(f); puts("Settings found."); return 0; }\n    puts("Settings missing.");\n    return 1;\n}\n' > reader.c

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Compile it

Command
gcc -O0 -no-pie -fno-stack-protector -o reader reader.c

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Watch the library call

Command
ltrace ./reader 2>&1 | grep fopen

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Watch the system call

Command
strace -e trace=openat ./reader 2>&1 | grep shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A missing file is a clue

A result of -1 ENOENT means the file the program asked for is not there. A program that looks for a settings file, a data file or a licence file names it in its own request.

A defender can use that: the missing name shows what the program expects to find. The next Runs make the file, run the program, trace it again and tidy up.

Make the file the program wants

Command
touch /tmp/shop-settings.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Run the program again

Command
./reader

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Trace the call again

Command
strace -e trace=openat ./reader 2>&1 | grep shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Remove the file again

Command
rm /tmp/shop-settings.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a trace shows every function in the program. It does not. strace shows only requests to the kernel, and ltrace only calls into shared libraries. A function inside the program itself appears in neither.

About this lab and its source: Reverse engineering lab: reading programs with gdb, radare2, strace and ltrace on one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.