Recognising Library Code and Crypto Constants
Most of the code in a typical binary was not written by the program's author: it is the C runtime, a compression library, a TLS stack, a JSON parser, statically linked and stripped of names. Reversing it line by line is wasted time. The professional move is to recognise it: match known library functions by their bytes, and recognise algorithms by the constants they can't do without. A hash function needs its initial values, AES needs its S-box, CRC-32 needs its polynomial. Finding 0x6a09e667 in a binary is finding SHA-256. This lesson shows both techniques on a toy the session builds.
Definitions
- Library signatures: byte patterns of known library functions with the bytes that change between builds (relocated addresses, displacements) masked out. IDA's FLIRT, Ghidra's Function ID, Binary Ninja's signature kit and radare2's zignatures apply them to name statically linked functions automatically.
- Crypto constants: values specified by an algorithm and therefore present in every implementation. Plugins such as FindCrypt, capa's crypto rules and YARA's crypto signatures scan for them.
| Algorithm | Constants to look for |
|---|---|
| SHA-256 | initial hash 6a09e667 bb67ae85 3c6ef372 a54ff53a ..., round constants 428a2f98 71374491 b5c0fbcf ... |
| SHA-1 / MD5 | MD5 and SHA-1 initial state 67452301 efcdab89 98badcfe 10325476 (SHA-1 adds c3d2e1f0); MD5 sine table d76aa478 e8c7b756 ... |
| AES | S-box 63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76 ...; or the AES-NI instructions aesenc, aesdec |
| CRC-32 | reflected polynomial 0xEDB88320 (or a 256-entry table starting 00000000 77073096 ee0e612c ...) |
| ChaCha20 / Salsa20 | the ASCII string expand 32-byte k |
| TEA / XTEA | the delta 0x9E3779B9 (also used in hashing, so confirm with context) |
Constants are stored in the byte order of the target: on x86-64, the 32-bit value 0x6a09e667 appears in memory as 67 e6 09 6a.
Finding the constants in a toy binary
The session builds a toy that computes a CRC-32 bit by bit (the polynomial is an immediate in the code), keeps a SHA-256 initial state and the first row of the AES S-box as tables (as a real implementation would), and is stripped. A small scanner then looks for the constants in the file. Finally the analyst confirms the CRC-32 guess the way it should always be done: by comparing the program's output with a reference implementation (Python's zlib.crc32).
cat > algo.c <<'SRC'
#include <stdio.h>
#include <stdint.h>
#include <string.h>
const uint32_t sha256_init[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
const uint8_t aes_sbox_row0[16] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76 };
uint32_t mystery(const uint8_t *p, size_t n) { /* what is this? */
uint32_t c = 0xFFFFFFFFu;
for (size_t i = 0; i < n; i++) {
c ^= p[i];
for (int k = 0; k < 8; k++) c = (c >> 1) ^ (0xEDB88320u & -(c & 1));
}
return ~c;
}
int main(int argc, char **argv) {
const char *msg = argc > 1 ? argv[1] : "lab";
printf("%08x %08x %02x\n", mystery((const uint8_t *)msg, strlen(msg)), sha256_init[0], aes_sbox_row0[0]);
return 0;
}
SRC
gcc -O2 algo.c -o algo && strip algo
cat > findcrypt.py <<'PY'
import struct, sys
SIGS = {
"SHA-256 initial hash H0..H1": struct.pack("<II", 0x6a09e667, 0xbb67ae85),
"SHA-256 round constants K0..K1": struct.pack("<II", 0x428a2f98, 0x71374491),
"MD5/SHA-1 initial state": struct.pack("<II", 0x67452301, 0xefcdab89),
"AES S-box (first 8 bytes)": bytes.fromhex("637c777bf26b6fc5"),
"CRC-32 polynomial 0xEDB88320": struct.pack("<I", 0xEDB88320),
"ChaCha/Salsa 'expand 32-byte k'": b"expand 32-byte k",
}
data = open(sys.argv[1], "rb").read()
for name, sig in SIGS.items():
hits = [i for i in range(len(data) - len(sig) + 1) if data.startswith(sig, i)]
print(f"{name:<32} {'found at ' + ', '.join(hex(h) for h in hits) if hits else '-'}")
PY
python3 findcrypt.py algo
echo "== where the polynomial is: an immediate operand in the code"
objdump -d -M intel --no-show-raw-insn algo | grep -m1 -i 'edb88320' | sed 's/^ *//'
echo "== confirm the hypothesis 'mystery = CRC-32' against a reference"
for m in lab "hello world"; do
printf '%-12s program: %s zlib.crc32: %08x\n' "$m" "$(./algo "$m" | cut -d' ' -f1)" \
"$(python3 -c "import zlib,sys; print(zlib.crc32(sys.argv[1].encode()))" "$m")"
doneSHA-256 initial hash H0..H1 found at 0x2040
SHA-256 round constants K0..K1 -
MD5/SHA-1 initial state -
AES S-box (first 8 bytes) found at 0x2020
CRC-32 polynomial 0xEDB88320 found at 0x11f2
ChaCha/Salsa 'expand 32-byte k' -
== where the polynomial is: an immediate operand in the code
11f1: and eax,0xedb88320
== confirm the hypothesis 'mystery = CRC-32' against a reference
lab program: 61d6b1c4 zlib.crc32: 61d6b1c4
hello world program: 0d4a1185 zlib.crc32: 0d4a1185
Every constant the toy contains is found; those it doesn't contain (SHA-256's round constants, MD5, ChaCha) are not. The CRC polynomial sits inside an instruction (and or xor with an immediate), so the scanner finds it in .text, while the tables are in .rodata. The hypothesis "mystery is CRC-32" is then confirmed by matching outputs for two inputs. Without that step a constant is only a hint: 0xEDB88320 can also appear in unrelated code, and a modified algorithm (custom initial value, different polynomial) keeps most constants and changes the result.
Masked signatures for library functions
Library recognition works on code bytes instead of constants. A function's bytes change between builds only where addresses are encoded, so a signature masks those bytes. The example builds a signature from one build of a small function (bytes from the decoder lesson style of encoding) and matches it against two other byte strings: the same function linked at another address, and a different function.
def make_signature(code: bytes, masked: set[int]) -> list:
return [None if i in masked else b for i, b in enumerate(code)]
def matches(sig, data: bytes, start: int = 0) -> bool:
return len(data) - start >= len(sig) and all(s is None or s == data[start + i] for i, s in enumerate(sig))
def show(sig):
return " ".join("??" if b is None else f"{b:02x}" for b in sig)
# Build A of a library function: push rbx; mov rbx, rdi; call rel32; test eax, eax; mov rax, [rip+disp32]; pop rbx; ret
build_a = bytes.fromhex("53" "4889fb" "e8a0100000" "85c0" "488b05f42e0000" "5b" "c3")
# The call displacement (bytes 5-8) and the rip-relative displacement (bytes 14-17) depend on the link layout.
sig = make_signature(build_a, masked=set(range(5, 9)) | set(range(14, 18)))
print("signature:", show(sig))
build_b = bytes.fromhex("53" "4889fb" "e8c4230000" "85c0" "488b0518420000" "5b" "c3") # same code, other addresses
other = bytes.fromhex("53" "4889fb" "e8c4230000" "85c0" "488b0d18420000" "5b" "c3") # loads rcx, not rax
for name, data in [("build A", build_a), ("build B (relinked)", build_b), ("different function", other)]:
print(f"{name:<20} {'MATCH' if matches(sig, data) else 'no match'}")signature: 53 48 89 fb e8 ?? ?? ?? ?? 85 c0 48 8b 05 ?? ?? ?? ?? 5b c3
build A MATCH
build B (relinked) MATCH
different function no match
The relinked copy matches although its displacements differ; the function that loads rcx instead of rax (one byte changed in the ModRM, 05 vs 0d) does not. Real signature systems add the function's length, a checksum of the remaining bytes, and references to other recognised functions, to keep collisions down.
How recognition helps detection
- Malware analysis: identifying the crypto (AES-CBC with a key derived by SHA-256, RC4, ChaCha20) is often how analysts find the configuration or the ransom key handling; recognising library code (zlib, mbed TLS, libcurl) removes thousands of functions from the to-do list.
- Vulnerability research: a library's version string plus its recognised functions tell you whether a known CVE applies (an old OpenSSL or zlib statically linked into firmware is a common finding).
- Detection rules: YARA rules over crypto constants and library byte patterns, with care: legitimate software contains the same constants.
How developers should handle crypto and libraries
- Use vetted libraries (libsodium, OpenSSL/BoringSSL, the OS crypto API) and keep them updated; a statically linked old version is a vulnerability that stays in every copy shipped (Applied Cryptography covers what to use).
- Don't rely on hiding the algorithm: Kerckhoffs's principle says the algorithm will be found (as above, in minutes); only the key must be secret, and it must not be in the binary.
- Publish a software bill of materials (SBOM) so users can tell which libraries are inside (Supply-Chain Security).
Pitfalls
- Endianness: search for both byte orders when the target architecture is unknown.
- Computed tables: some implementations generate the AES S-box or the CRC table at run time; then look for the generator's constants (the polynomial
0x1bfor AES,0xEDB88320for CRC) or dump the table from memory in a debugger. - Hardware instructions (
aesenc,sha256rnds2,crc32) replace tables entirely; search the disassembly for them. Note that x86'scrc32instruction computes CRC-32C (Castagnoli polynomial, reflected0x82F63B78), not the zlib CRC-32 above. - A constant is a hint, not a proof: confirm with test vectors.
Practice
1. You find the bytes 67 e6 09 6a 85 ae 67 bb in an ARM64 little-endian firmware image. Which algorithm?
SHA-256 (its initial hash values H0 = 0x6a09e667, H1 = 0xbb67ae85, little-endian). SHA-224 uses different initial values, and SHA-512's 64-bit initial values (0x6a09e667f3bcc908, ...) share these upper halves but would not produce this 8-byte sequence in memory; confirm with the code that uses them.
2. A binary contains the string expand 32-byte k. What is likely inside, and how do you confirm it?
ChaCha20 or Salsa20. Find the code that reads the string (cross-references), recognise the quarter-round (additions, XORs, rotations by 16, 12, 8, 7 for ChaCha20), and confirm with an RFC 8439 test vector in a debugger.
3. In the signature example, which bytes would you also mask if the function called memcpy through the PLT in another build?
The 4-byte displacement of that call as well, since PLT addresses change with the layout.
Test yourself
Why do crypto constants identify algorithms reliably?
- They are encrypted
- They are part of the algorithm's specification, so every implementation needs them
- Compilers add them
- They are random
How does the SHA-256 value 0x6a09e667 appear in an x86-64 binary's data?
- 6a 09 e6 67
- 67 e6 09 6a
- e6 67 6a 09
- It never appears
What do FLIRT-style signatures mask out?
- Opcodes
- Bytes that change between builds, such as relocated addresses and displacements
- The whole function
- Strings
After finding 0xEDB88320, what confirmed that mystery is CRC-32?
- The function's name
- Its output matched zlib.crc32 on test inputs
- The binary was stripped
- The constant was in .rodata