A stripped binary that wants a flag:
echo hello | ./challFlag: Nope.The flag is not stored in the file. Something derived from it is. Recover the flag.
Stuck? Open one hint at a time.
main and look at what happens to your input between the read and the memcmp. The length check tells you how long the flag is.entry gives us main at FUN_0040051b. The part that matters:
1 sVar4 = strlen(local_48);
2 if (sVar4 == 0x18) {
3 FUN_004004b6(local_48,The input must be 0x18 characters, which is 24. It is passed through FUN_004004b6 and then compared with 24 bytes at DAT_00401400. This is an encoded check, as in Obfuscation: modify, then compare. Rename the function encode and the data target.
1void FUN_004004b6(long param_1,ulong param_2)
2{
3 byte bVar1;
4 ulong local_18;
Go through one trip of the loop:
bVar1 is the input byte at this position, saved before anything changes it.local_9, and the result is used as an index into a table at DAT_00401300. The value found there replaces the input byte.local_9 is set to bVar1, the original input byte, for the next trip.local_9 starts at 0x42. So, calling the table sbox:
out[0] = sbox[in[0] ^ 0x42]
out[i] = sbox[in[i] ^ in[i - 1]]Each output byte depends on the input byte at that position and the one before it. That is the difference from the Obfuscation lesson, and it closes the shortcut that lesson offered. You cannot solve position 10 alone, because you need to know the flag's ninth character to do it.
A table used this way is called a substitution box. It runs from 0x401300 up to 0x401400, where target starts, so it has 256 entries, one for every possible byte.
Two steps were applied: an XOR, then a table lookup. Undo them in reverse order.
The lookup is reversible if no value appears in the table twice, because then each output could only have come from one index. Build an inverse table by going through sbox and recording, for each value, the index it was found at. If the table had repeats, the inverse would have gaps, and a script will show that at once.
With the inverse table, inverse[out[i]] gives back in[i] ^ in[i - 1]. XOR that with in[i - 1] and in[i] is left. For the first byte, the "previous" value is 0x42. After that it is the character we just decoded. So the flag has to be recovered in order, from the left.
Both tables are in the file, and the script can read them straight from it. This binary is not position-independent and loads at 0x400000, so an address like 0x401300 is at offset 0x1300 in the file:
1data = open("chall", "rb").read()
2sbox = data[0x1300:0x1400]
3python3 solve.pyIFC{ch41n3d_t0_th3_l4st}python3 solve.py | ./challFlag: Correct!The first few characters coming out as IFC{ is the sign that every piece is right: the table, its inverse, the starting value and the order. If the output were garbage from the first byte, the starting value or the table would be wrong. If it began well and then fell apart, the chaining would be.
Next is Crackme 4, the last and the hardest.