Sooner or later, every CTF player sees a value like 0x7b4654436f636970 in a debugger, decodes it, and gets {FTCocip — the flag prefix, backwards. That's endianness: the order in which a multi-byte number's bytes are stored in memory.
Big-endian vs little-endian
Take the 32-bit number 0x12345678. It's four bytes: 12 34 56 78. There are two common ways to lay them out in memory, from lowest address to highest:
- Big-endian — most significant byte first:
12 34 56 78. Reads the way we write numbers. Used by network protocols ("network byte order") and some older architectures. - Little-endian — least significant byte first:
78 56 34 12. Used by x86, x86-64 and, in practice, ARM — so almost every CTF binary.
Why strings look reversed
Strings are stored byte by byte in order, so "picoCTF{" in memory is 70 69 63 6f 43 54 46 7b. When a debugger or a %p leak reads those 8 bytes as a single little-endian number, the last byte becomes the most significant: 0x7b4654436f636970. Reverse the bytes and you get the string back:
>>> (0x7b4654436f636970).to_bytes(8, "little")
b'picoCTF{'
Why exploits use p64()
When you overwrite a return address with 0x401196, you have to write the bytes in the order the CPU will read them back: 96 11 40 00 00 00 00 00. pwntools does this for you:
from pwn import p64, u64, p32
p64(0x401196) # b'\x96\x11@\x00\x00\x00\x00\x00'
u64(b'\x96\x11@\x00\x00\x00\x00\x00') # 0x401196
p64/p32 pack a number into little-endian bytes; u64/u32 unpack them. In plain Python, struct.pack("<Q", addr) does the same — the < means little-endian, > big-endian.
Where else it bites
- Reversing — a crackme comparing your input to
0x67616c66wants"flag", not"galf" - File formats — PNG and JPEG store numbers big-endian, BMP and ZIP little-endian; reading a header wrong gives absurd sizes
- Network captures — protocol fields are big-endian even when the machines aren't
- Hex dumps —
xxdshows bytes in file order;hexdumpwithout-Cgroups them into little-endian 16-bit words, which looks scrambled
The rule of thumb
Bytes in a file or a string are in order. Numbers read from memory on x86 are little-endian. When something looks backwards, it almost always is — reverse the bytes and look again. For more on reading binaries, see static vs dynamic analysis and our explanation of return addresses.