Endianness Explained: Why Your Bytes Look Backwards in CTFs

Article · Rev Eng

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:

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

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.

← Back to Blog