Why entropy can't see XOR-obfuscated code (and what to measure instead)
Most packer and obfuscation detectors threshold on Shannon entropy. Above ~7.0 means "compressed or encrypted", below means "probably fine". That heuristic has a blind spot big enough to drive a truck through, and it is
Most packer and obfuscation detectors threshold on Shannon entropy. Above ~7.0 means "compressed or encrypted", below means "probably fine". That heuristic has a blind spot big enough to drive a truck through, and it is the most common obfuscation there is.
The measurement that doesn't move
XOR is a bijection. It maps each byte value to exactly one other byte value, one-to-one. Shannon entropy is computed from the multiset of byte frequencies. A bijection permutes the labels on that multiset without changing which counts appear:
H(XOR(data, k)) == H(data) for every key k
This is an identity, not an approximation.
I measured it on the .text section of a real Windows DLL — C:\Windows\System32\version.dll, 12,288 bytes:
| Case | Entropy | Disassembly coverage |
|---|---|---|
| Original | 5.4067 | 100.00% |
| XOR 0x5A | 5.4067 | 2.54% |
| XOR 0xFF | 5.4067 | 0.69% |
| XOR 0x01 | 5.4067 | 0.13% |
| Permuted | 5.4067 | 0.41% |
Entropy delta on every transformed row: +0.0000.
Same numbers on advapi32.dll (471,040-byte .text): 6.2347 / 22.14% original, 6.2347 / 0.00% after XOR 0x5A.
A tool that only measures entropy says "normal" for a section whose code is now entirely undecodable.
What does move
Decodable instruction coverage: start a disassembler at the section base, count what fraction of bytes valid instructions consume.
- Real x64 code: typically 20–100% (not 100% — alignment padding and inline data sit between functions)
- XOR-obfuscated code: 0.00% — the decoder cannot form a single valid instruction
That gap is the signal. No key needed, no unpacking, no execution.
import capstone, pefile
def coverage(code: bytes) -> float:
md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
md.skipdata = False
n = 0
for ins in md.disasm(code, 0x1000):
n += ins.size
return n / len(code) if code else 0.0
That's the whole detector. Four lines.
The key falls out for free
Once you measure coverage, recovering a single-byte XOR key is a by-product: try all 255 candidates, keep the one whose output decodes.
0x5A → 100.00% ← key
0x03 → 5.70%
0x23 → 4.36%
0x6F → 4.24%
0xCB → 4.13%
94.30 percentage points between the right answer and the second-best guess. Nothing to tune, nothing to eyeball. Applied to a real DLL, the restored file is byte-identical to the original (SHA256 verified).
I tested the existing tools first
Before building anything I ran the same three files through two established PE packer detectors.
entropystrip 1.3.0:
$ entropystrip version_orig.dll
│ .text │ 5.41 │ R+X │ OK │
$ entropystrip version_xor5A.dll
│ .text │ 5.41 │ R+X │ OK │
With --deep, the full report is also identical — same score (50/100), same per-section statuses. The only differing fields are SHA256 and SSDEEP. The direction is even inverted: the pristine binary got WARNING Suspicious (from a 7.47-entropy overlay, a real finding), and the obfuscated one did not.
packerscope 0.4.0:
file is_packed confidence
version_orig False 0.02040816326530612
version_xor5A False 0.02040816326530612
version_xorFF False 0.02040816326530612
Confidence identical to all 17 decimal places. max_section_entropy: 5.4067 in every report.
Neither tool is wrong. Entropy, IAT, entrypoint and signature detectors all measure what they claim to measure. The gap is that none of them measure "does this code still decode", and that is the only measurement that moves under XOR.
Both directions of false positive are real
A section holding data with code-like flags also scores near zero. In advapi32.dll, fothk is a 4,096-byte RX section with entropy 0.0159 — near zero because it is a jump table of identical stubs. Entropy flags nothing; coverage would flag it as suspicious. Reading it shows it is benign.
So the verdict has to combine both numbers rather than threshold either one.
The guard that matters more than the detector
An early version of my recovery script searched keys on a clean file, found a meaningless 5.70% "best" (key 0x59) and wrote a corrupted copy over a working DLL.
The fix was not a better search. It was refusing to act:
if base_coverage >= 0.20:
print("This section already decodes. No recovery needed.")
print("If the file really fails to load, the cause is not single-byte XOR —")
print("it may be a broken import table, a missing dependency, or a signature problem.")
return 0 # write nothing
Any tool that can modify a file needs a condition under which it refuses to. Mine is coverage above 20%.
What this does not cover
- Multi-byte XOR, block ciphers, virtualized code. The 255-key search does not scale; the tool exits rather than guessing.
- Import tables, relocations, signatures. If a file fails to load for those reasons, coverage looks normal and the tool correctly says it is not a single-byte XOR problem. It will not fix the real cause.
- Maliciousness. Protectors obfuscate legitimately. The measurement says "this code is not decodable as-is", nothing more.
Where it came from
I was looking at a DLL that had "suddenly stopped loading" and needed to know which of three things it was: encrypted, corrupted, or replaced with a loader stub. Three different repair strategies. An entropy reading said "clean". It was not clean.
That check turned into a tool. Both the detector and the recovery script, with the full three-tool comparison and reproduction steps, are here:
https://github.com/gem3match-dev/pe-obfuscation-detection
pip install peobf for the command-line detector, or a Windows GUI build on the releases page.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.