In the last lesson the imports saved us. With every function name gone, fgets, strlen and puts were still there, and we rebuilt the program around them. Those names exist because a dynamically linked program borrows its C library from the system and has to say which functions it wants.
A statically linked program does not borrow. The linker copies every library function it needs into the file, so there is nothing to ask the system for and no list of names. Strip a binary like that and you have hundreds of functions with no labels at all, of which the author wrote a handful. This lesson is about finding the handful.
This lesson's chall asks for a token:
echo 123 | ./challToken: Invalid.It is a tiny program. The source in the download is twenty lines long, with a main and one helper. file shows what is different about it:
file challchall: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=0b82645f056d5cc9e4c2677620f76385c3bdae42, for GNU/Linux 3.2.0, strippedstatically linked and stripped, with no mention of an interpreter. For comparison, build the same source the ordinary way and look at the sizes:
gcc -std=gnu17 -no-pie -o dynamic source.cwc -c chall dynamic726880 chall 12792 dynamic739672 totalThe static build is more than fifty times larger, and none of the extra is the author's code. It is printf, fgets and puts, plus everything those functions call in turn, plus the machinery for files, memory, locales and errors that a C library carries. You will often be able to guess that a binary is static from its size alone: a simple command-line tool that weighs most of a megabyte is carrying its library with it.
The tools we have relied on come up empty:
nm -D challnm: chall: no symbolsThere are no dynamic symbols, because nothing is loaded at run time. There is no PLT either, so the rule from Functions & Calls, that a call into the PLT is a library call, has nothing to work with. A call to printf and a call to the author's helper look identical: a call to an address in the same file.
Import chall into Ghidra and the Symbol Tree confirms it. Imports is empty, and Functions holds 742 entries, nearly all of them named FUN_ and an address.
The strings do not help the way they did before, either:
strings chall | wc -l4216The library brought its own strings with it: error messages, locale names, format fragments. The author's three or four are in there somewhere among four thousand. strings chall | grep Token still works if you know what to look for, and following the cross-reference from a known message is still a good way in. But browsing the list is over.
mainOne thing is unchanged. The file still has an entry point, the startup code is the same as ever, and it still hands main to the function that starts the C library:
1void processEntry entry(undefined8 param_1,undefined8 param_2,undefined1 param_3)
2{
3 undefined1 auStack_8 [8];
4
Compare this with the entry from the last two lessons. The shape is identical: one call, seven arguments, then a loop that never runs. The only difference is that the function being called has no name. We do not need one. We know this code, so FUN_00404510 is __libc_start_main, and its first argument, FUN_00401c1b, is main. Rename both.
That is two functions out of 742 identified in a minute, and one of them is the one we wanted.
1undefined8 FUN_00401c1b(void)
2{
3 undefined4 uVar1;
4 int iVar2;
5main calls five different functions. In a dynamic binary, four of them would have had names. Here we have to work out which are the library's and what they are. The wrong way to do that is to open one:
1ulong FUN_0040c090(undefined8 param_1)
2{
3 int *piVar1;
4 undefined1 *That is the first quarter of it. It goes on through locks, buffer pointers and a handful of goto statements, and it calls more functions like itself. This is what production library code looks like once it has been optimized and decompiled. It is hard to read, and reading it would tell us nothing that the manual page does not.
The better way is to identify each function from the outside, using how main calls it.
Look at the calls again, as a set of clues:
FUN_0040c090 is called with a string and nothing else, at the two places where the program prints a line and stops. A function that takes one string and prints it with a newline is puts. That monster above is puts.FUN_00405b90("Token: ") is also called with a string, but the output had no newline after Token:. It behaves like printf.FUN_0040bd00(local_28,0x20,PTR_DAT_004a86d8) takes a buffer, the size of that buffer, and a pointer to some global, and its result is tested against zero. A buffer, its size and a stream: that is the argument list of fgets, and fgets returns a null pointer on failure.FUN_00404a10(local_28) takes the buffer that was just filled and returns a number that goes into the check. Something that turns text into a number.The last one is small enough to open:
1void FUN_00404a10(undefined8 param_1)
2{
3 FUN_00404fd0(param_1,0,10);
4 returnIt passes its argument on with two more: a zero and the number 10. A function taking a string, a null pointer and a base of 10 is strtol, and the C library's atoi is exactly this one-line wrapper around it. So FUN_00404a10 is atoi. Ghidra shows it as void for the reason we met in Functions & Calls: the result passes straight through in rax.
We named four library functions without understanding the inside of any of them. The method is the one you would use on a stranger's code in any language: look at what goes in, what comes out, and what the program does around the call. Running the program helps too. We knew Token: appears without a newline and Invalid. with one because we ran it.
Sometimes the arguments are not enough. A second kind of evidence sits at the very bottom of the library, where it talks to the operating system.
A program cannot print, read or open a file on its own. It asks the kernel, using the syscall instruction, with a number in eax that says which service it wants. A dynamic binary has no syscall instructions of its own, because they are all inside the shared C library. A static binary has them all:
objdump -d -M intel --no-show-raw-insn chall | grep -c syscall121The numbers are fixed by Linux and listed in any syscall table. A few are worth knowing by heart:
eax | System call | What it does |
|---|---|---|
| 0 | read | Read from a file or input |
| 1 | write | Write to a file or output |
| 2 | open | Open a file |
| 3 | close | Close a file |
| 60 | exit | End the program |
A function that loads 1 into eax and executes syscall is writing output, whatever it is called. Whatever calls that function is a step up, and after a few steps you reach something like puts. Reading upward from system calls is slower than reading the arguments, but it always works, because a program cannot do anything visible without them. System Calls in Assembly 101 covers the mechanism.
Identifying functions one at a time is fine for the four that main calls. It does not scale to 742. For that there are library signatures: databases of the byte patterns of known library functions, which a tool can match against a binary to name functions automatically. Compiled code for strlen from a given version of a given library is the same bytes in every program that links it, so a match is good evidence.
IDA calls its version FLIRT. Ghidra's is Function ID, and it runs as part of the default analysis. When it matches, functions come out of analysis already named, and a static binary becomes nearly as easy as a dynamic one.
The catch is coverage. A signature database only knows the library versions someone built it from. This chall was linked against a recent C library that Ghidra's bundled databases do not include, and Function ID recognized almost nothing. That is what you saw in the Symbol Tree. It is a common outcome with a new or unusual library, and the remedy is more signatures: Ghidra can build a Function ID database from any library you have a copy of, and people publish ready-made ones.
So try signatures first, because when they work they save hours. When they do not, the manual methods above are what is left.
Four of the five calls in main are accounted for. The remaining one is ours:
1bool FUN_00401c05(int param_1)
2{
3 return param_1 == 0x14b;
4}It is three instructions long and it sits right next to main in memory, at 0x401c05 against 0x401c1b. That is a useful clue in its own right. The linker lays out a program's own object files together and the library's after them, so the author's functions cluster in one small address range, usually near main. The library functions main called were all far away, at 0x404a10 and above.
The source of this function says token * 3 + 7 == 1000. Ghidra has solved the equation for us and shows the only value that passes, 0x14b:
printf '%d\n' 0x14b331echo 331 | ./challToken: Valid.The work in this lesson was mostly deciding what not to read. Out of 742 functions, we opened four: entry, main, the atoi wrapper and the check. We glanced at a fifth and backed out.
Some signs that you have wandered into library code and should back out:
goto, and touches global structures through many fixed offsets.in_FS_OFFSET), or error codes.main in the address space.When you see these, stop, go back up to the caller, and ask what the function must be from how it is used. Give it a name, even a tentative one, and carry on. The author's logic is what you came for, and it is almost always small and near main.
main: the startup code is the same, so the function it calls is __libc_start_main and the first argument is main.main.Next is Obfuscation, where the code is easy to find and has been written to be hard to follow.