The last lesson was about the shape of code. This one is about what the code works on. A fresh decompilation is full of local_20, param_1 and expressions like *(int *)(param_1 + 0x14), and none of those say what the data means. Working that out is half of reading a binary, and it pays off quickly: put the right type on one variable and a whole function changes from arithmetic into something you can read.
We treat this lesson's program as a real unknown. We look at the binary first and at the source only at the very end, to check our work.
chall asks for an account number and a PIN:
./challAccount: 1001PIN: 1111PIN: 2222PIN: 3333Locked out.It gives three attempts and then locks us out. We could have guessed the account number 1001, but nothing about the PIN. By the end of the lesson we will have read it out of the file.
A running program keeps data in a few different places, and which place tells you something about the data. nm shows where three of this program's variables ended up:
nm chall | grep -E ' (accounts|tiers|attempts)$'0000000000403040 d accounts000000000040308c b attempts0000000000401210 r tiersThe letter in the middle is the section. Each section holds a different kind of data:
| Section | nm letter | Holds | Tells you |
|---|---|---|---|
.data | d | Globals with a starting value | The value is in the file and can change |
.rodata | r | Constants and string literals | The value is in the file and never changes |
.bss | b | Globals that start at zero | Nothing is stored; only the size is known |
| The stack | none | Local variables, created when a function runs | Not in the file at all |
So accounts has starting values that we can read out of the file, tiers is a fixed table, and attempts starts at zero and is filled in while the program runs. Local variables do not appear here, because they have no fixed address. Memory in Assembly 101 covers these sections in more depth.
This binary still has its symbols, so the globals have names. In a stripped binary Ghidra calls them DAT_00403040 and so on, after their addresses, and the section is the only hint you get.
Since accounts lives in .data, its contents are in the file. objdump -s prints a section as raw bytes:
objdump -s -j .data challContents of section .data: 403020 00000000 00000000 00000000 00000000 ................ 403030 00000000 00000000 00000000 00000000 ................ 403040 e9030000 616c6963 65000000 00000000 ....alice....... 403050 d5120000 fa000000 ea030000 626f6200 ............bob. 403060 00000000 00000000 8e1c0000 5a000000 ............Z... 403070 eb030000 6361726f 6c000000 00000000 ....carol....... 403080 a6070000 b0040000 ........Each row shows an address, sixteen bytes in hexadecimal, and the same bytes as text on the right. The text column gives us a foothold: alice, bob and carol. Those look like account holders.
Now look at what surrounds each name. Just before alice is e9030000. x86 stores numbers with the lowest byte first, which is called little-endian, so those four bytes are the number 0x000003e9. That is 1001, the account number we typed. Before bob is ea030000, 1002, and before carol is eb030000, 1003.
A pattern is forming: a number, then a name, then more numbers, repeating. The first record starts at 0x403040 and the second, the one beginning with 1002, at 0x403058. That is 24 bytes apart. So accounts looks like three records of 24 bytes each, and we can already guess that one of the other numbers in each record is a PIN. To be sure which, we need to see how the code uses them.
Import chall into Ghidra and open the function tier, a short one that takes a single argument:
1int tier(long param_1)
2{
3 int local_10;
4 int local_c;
Look at *(int *)(param_1 + 0x14). Read from the inside out, it says: take param_1, add 0x14 to it, treat the result as the address of an int, and fetch that int. Ghidra typed param_1 as a long, a plain number, because that is all it could see. But a number that gets an offset added and is then used as an address is a pointer, and a pointer that is read at a fixed offset is a pointer to a struct. The offset is the position of a field. This function reads a 4-byte field at offset 0x14.
find shows the 24 bytes we measured by hand:
1undefined * find(int param_1)
2{
3 int local_c;
4
5 local_c &accounts + local_c * 0x18 is the address of record number local_c, where each record is 0x18 bytes, which is 24. The loop compares the int at the very start of each record with the argument and returns the address of the record that matches. So offset 0 is the account number, and find returns a pointer to one record.
main uses that pointer three more ways:
1 if (local_20[attempts] == *(int *)(local_10 + 0x10)) {
2The format string is the key to the rest. %s is filled by local_10 + 4, so offset 4 is the name. Balance: %d is filled by offset 0x14, so that is the balance. And the value at offset 0x10 is compared with something we typed, right before the welcome message. That is the PIN.
Collect the evidence in a table:
| Offset | Size | How the code uses it | Field |
|---|---|---|---|
0x00 | 4 | Compared with the account number | id |
0x04 | 12 | Printed with %s | name |
0x10 | 4 | Compared with the PIN we typed | pin |
0x14 | 4 | Printed after Balance: | balance |
The name's size comes from the gap: it starts at 4 and the next field starts at 0x10, which is 16. The sizes add up to 24, matching 0x18. This is how every struct gets recovered: collect the offsets that code touches, note the size and use of each, and fill in the gaps.
Now we give Ghidra the struct. In the Data Type Manager, the pane at the bottom left, right-click the entry named after the program, chall, and choose New > Structure. Name it account, add the four fields from the table with their types (int, char[12], int, int), and save.
Then apply it. In tier, click the function name and press F to edit its signature, and change the parameter to account * acct:
1int tier(account *acct)
2{
3 int local_10;
4 int local_c;
*(int *)(param_1 + 0x14) is now acct->balance. Do the same for find, making it return account *, and retype the global itself by clicking accounts in the Listing, pressing T, and entering account[3]:
1account * find(int id)
2{
3 int local_c;
4
5 local_c =The multiplication by 0x18 is gone. Ghidra knows the size of an account, so it writes accounts[local_c] and does the arithmetic out of sight, the same way C does. One struct definition cleaned up every function that touches the data, and it will keep doing so in functions we have not opened yet.
main has one more problem. Here is how it starts, with the pointer already retyped to account * and renamed acct:
1undefined8 main(void)
2{
3 int iVar1;
4 int local_20 [4];Ghidra found an array of four int values, local_20. But look at how the elements are used. local_20[3] receives the account number from scanf and is passed to find. The others are used further down, in the loop that reads PINs:
1 for (; attempts < 3; attempts = attempts + 1) {
2 printf("PIN: ");
3That loop stores into local_20[attempts], and attempts never reaches 3. So elements 0 to 2 are one thing, the PINs we tried, and element 3 is something unrelated that happens to sit next to them on the stack. Ghidra saw four int values side by side, some of them reached through an index, and merged them into one array.
A binary does not record where an array ends. The size has to come from evidence, and the best evidence is the bound of the loop that walks the array. Here the bound is 3. Click local_20, press Ctrl+L, and retype it as int[3]. Ghidra splits off the fourth element as its own variable. With that one renamed id and the array renamed tried, main reads like this:
1undefined8 main(void)
2{
3 int iVar1;
4Compare that with the fragments above. Nothing about the binary changed. We told Ghidra three facts, a struct and two types, and it carried them through.
The opposite mistake is as common. When code only ever touches array elements by fixed position, Ghidra has no index to notice and shows a run of separate variables, such as local_28, local_24 and local_20, four bytes apart. Variables whose names step evenly like that and are used alike are probably one array, and retyping the first as an array joins them.
The check is tried[attempts] == acct->pin, and acct points into accounts, whose starting values are in the file. Since we retyped accounts as an array of structs, double-clicking it shows the values by field:
| Element | id | name | pin | balance |
|---|---|---|---|---|
accounts[0] | 3E9h | "alice" | 12D5h | FAh |
accounts[1] | 3EAh | "bob" | 1C8Eh | 5Ah |
accounts[2] | 3EBh | "carol" | 7A6h | 4B0h |
Ghidra's Listing writes hexadecimal with a trailing h. The same numbers are in the objdump output from earlier. Find alice there, count sixteen bytes on from the start of that record, and you will see d5120000.
Alice's PIN is 0x12d5:
printf '%d\n' 0x12d54821printf '1001\n4821\n' | ./challAccount: PIN: Welcome, alice. Balance: 250 (tier 1)Now we can check our work against the source:
1struct account {
2 int id;
3 char name[12];
4 intThe struct matches field for field, down to the 12-byte name. We chose the same field names here to keep the lesson easy to follow. On a real target you would pick your own, and they would be as good as the author's if they describe what the code does with each field. In main, the author declared int id and int tried[3], which is what we ended up with after correcting Ghidra's array.
.data for globals with starting values, .rodata for constants, .bss for globals that start at zero, and the stack for locals..data and .rodata can be read straight from the file. Multi-byte numbers are little-endian, lowest byte first.Next is Functions & Calls: how arguments get into a function, how to find main when it has no name, and how to tell the author's code from the library's.