TExcelTAD
Home › Malware Analysis, Digital Forensics & Incident Response › Cryptographic Hashes and Why They Are Brittle

Cryptographic Hashes and Why They Are Brittle

Every threat report ends with a table of hashes, and every analyst's first action on a new file is to compute its SHA-256 and search for it. Hashes are the universal name of a file: precise, short, easy to share and easy to search in logs, EDR consoles and intelligence platforms. They are also the weakest indicator there is, because the attacker can change them for free. This lesson shows both sides and what analysts do about the second.

The properties of hash functions themselves are taught in Hash Properties and MD5 and SHA-1 Collisions; here we use them.

Definitions

  • File hash: a cryptographic hash (MD5, SHA-1, SHA-256) of the whole file. Equal hashes mean identical files (for SHA-256, for all practical purposes).
  • Avalanche effect: changing one bit of input changes about half of the output bits, so similar files have unrelated hashes.
  • Hash lookup: searching a hash in a reputation service, a malware repository or your own EDR telemetry. It reveals nothing about the file to the service except that you have it.
  • Known-good lists: hash sets of legitimate software, such as the NIST NSRL (National Software Reference Library), used to exclude known files from an investigation.
  • Partial hashes: hashes of a part of a file (a section, the code, a resource) or of a normalised feature (the import table: imphash), which survive some changes the file hash doesn't.

Identical, then one byte apart

The session hashes a synthetic sample, makes a "variant" that differs in a single byte (the way a builder stamps a campaign ID into each copy), and a second variant with junk appended at the end. The program logic would be the same in all three; the hashes have nothing in common.

bash · try it yourself
python3 - <<'PY'
import random
body = b"MZ" + bytes(62) + b"LABSAMPLE builder v1 campaign=0001 server=update.beacon.lab.test\0"
body += random.Random(5).randbytes(2048)
open("variant-a.bin", "wb").write(body)
open("variant-b.bin", "wb").write(body.replace(b"campaign=0001", b"campaign=0002"))
open("variant-c.bin", "wb").write(body + b"\0" * 16)
PY
sha256sum variant-*.bin | tee known.sha256
md5sum variant-a.bin
cmp -l variant-a.bin variant-b.bin
ls -l variant-*.bin | awk '{print $5, $9}'
echo "--- later: is variant-a unchanged?"
sha256sum -c known.sha256 --quiet && echo "all three match the recorded hashes"
printf 'X' | dd of=variant-a.bin bs=1 seek=100 conv=notrunc status=none
sha256sum -c known.sha256 2>&1 | head -3
ef3b87ff6f5265246b98a45c03f28b6ae4fd0db3eb0434898b924f0f44a1b8ff  variant-a.bin
cce676f53c3d5f417946909bf61830e96d1fdb6252d5f24b323f27460386e766  variant-b.bin
f807490628cf7ae5709801f7248f35511e702a2d35b35743c5378cfe01deb674  variant-c.bin
731967ef30dba32b4f5180a1f82bcbb2  variant-a.bin
  98  61  62
2177 variant-a.bin
2177 variant-b.bin
2193 variant-c.bin
--- later: is variant-a unchanged?
all three match the recorded hashes
variant-a.bin: FAILED
variant-b.bin: OK
variant-c.bin: OK

cmp -l lists the only differing byte (offset 98, in decimal and counted from 1, the byte values in octal: 61 and 62, the characters 1 and 2), yet the SHA-256 values are unrelated. Variant C differs only by trailing zeros. The last commands show the other side of brittleness, which is the reason hashes are used for integrity: after one byte is overwritten, sha256sum -c reports the file as FAILED. Forensic evidence handling relies on exactly this (evidence integrity).

Same code, different hash

What stays the same across variants is usually the part that matters: the code. The example hashes the whole file and the "code region" separately for the three variants (building them the same way in the browser):

python · try it yourself
import hashlib, random

body = b"MZ" + bytes(62) + b"LABSAMPLE builder v1 campaign=0001 server=update.beacon.lab.test\0"
body += random.Random(5).randbytes(2048)
variants = {"a": body, "b": body.replace(b"campaign=0001", b"campaign=0002"), "c": body + b"\0" * 16}

CODE = slice(129, 129 + 2048)            # the random block stands in for the program's code
for name, data in variants.items():
    whole = hashlib.sha256(data).hexdigest()[:16]
    code = hashlib.sha256(data[CODE]).hexdigest()[:16]
    print(f"variant {name}: file sha256 {whole}…  code sha256 {code}…")
variant a: file sha256 ef3b87ff6f526524…  code sha256 3173a10acaf561fc…
variant b: file sha256 cce676f53c3d5f41…  code sha256 3173a10acaf561fc…
variant c: file sha256 f807490628cf7ae5…  code sha256 3173a10acaf561fc…

Three file hashes, one code hash. Real tools apply this idea in several ways: hashes per PE section, the imphash of the import table, the Authenticode hash that skips the signature and checksum fields, hashes of resources, and finally hashes designed to tolerate differences: fuzzy hashes.

How hashes are used in detection

  • Confirming a known file: an EDR search for a SHA-256 across all endpoints answers "where else is this?" in seconds. That is the hash's real strength, and why reports publish them.
  • Blocking: block lists of hashes in EDR or application control. Cheap, exact, and trivially bypassed by recompiling or appending a byte.
  • Excluding the known-good: NSRL and vendor hash sets remove thousands of operating-system files from a forensic review.
  • Integrity: evidence images, collected logs and the sample itself are hashed at acquisition and verified before each use.

How to make hash indicators worth more

  • Publish SHA-256 (and, if a partner asks, MD5 and SHA-1 alongside it, never instead of it).
  • Pair every file hash with features that survive rebuilding: imphash, section hashes, fuzzy hashes, YARA rules on code or strings.
  • For blocking, prefer allow-listing (only signed or approved software runs) to deny-listing hashes: an allow list doesn't need to know the attacker's next hash.

Pitfalls

  • Treating MD5 as identity. MD5 and SHA-1 collisions can be made deliberately; two different files with the same MD5 exist. Use SHA-256 for identification and integrity.
  • "No hits" as an all-clear. A hash nobody has seen means a new or per-victim build, not a harmless file.
  • Hashing the wrong thing. A sample extracted from an archive, a memory dump of a process and the file on disk all have different hashes; record which one you mean.
  • Hash case and format. Search tools may expect lowercase hex; copy them exactly.

Practice

1. Why do malware builders often produce a different hash for every victim?

Because it defeats hash block lists and makes each sample look new to reputation services, at almost no cost: a per-victim ID, a random padding or a new compilation changes the hash completely.

2. Which byte differs between variant A and B, and how does cmp -l report it?

The digit of campaign=0001 versus 0002. cmp -l prints the byte position counted from 1 in decimal (98 = 64 header bytes + "LABSAMPLE builder v1 campaign=000" is 33 characters, plus 1) and the two byte values in octal (61 and 62, the ASCII codes of 1 and 2).

3. A partner sends only MD5 hashes. What do you ask for, and why?

SHA-256 (and ideally the samples or YARA rules through a trusted channel). MD5 is fine as a search key but not as proof of identity, because collisions can be constructed.

Test yourself

Two samples differ in one byte. How similar are their SHA-256 hashes?

  1. Identical
  2. They differ in one hex digit
  3. Unrelated: about half the bits differ
  4. They share the first half

What is the NSRL used for in forensics?

  1. Listing known malware
  2. Excluding known-good software files by hash
  3. Cracking passwords
  4. Signing evidence

Why are file hashes at the bottom of the 'pyramid of pain'?

  1. They are hard to compute
  2. The attacker can change them at almost no cost
  3. They are secret
  4. They identify the attacker personally

Which use of hashes is NOT weakened by their brittleness?

  1. Blocking a malware family
  2. Verifying the integrity of an evidence image
  3. Detecting new variants
  4. Finding similar samples