Strings and Entropy
The two cheapest static checks on an unknown file are also two of the most informative. Strings (runs of printable characters) give away messages, file paths, URLs, registry keys, format strings, library versions and compiler names. Entropy (how unpredictable the bytes are) tells you whether a region is code, text, compressed data or encrypted data, without understanding any of it. Together they answer "is this file hiding something?" in seconds, which is why every triage tool starts with them. The Python Bytes and Encodings lesson covers the encodings used here.
Definitions
strings: prints sequences of at least printable characters (default 4,-nto change). By default it looks for 7-bit ASCII text (-e Salso accepts 8-bit bytes such as Latin-1);-e lfinds 16-bit little-endian text (UTF-16LE, the native string type of the Windows API, where"Hi"is48 00 69 00), and-e L32-bit little-endian text (UTF-32, whatwchar_tstrings are on Linux, wherewchar_tis 4 bytes). Many Windows strings are invisible without-e l.- Shannon entropy of a byte sequence, with the fraction of bytes equal to value :
when every byte is the same; when all 256 values are equally frequent. English text is around 4 to 4.5, x86-64 machine code around 5 to 6.5, compressed or encrypted data close to 8.
- Sliding-window entropy: computed over consecutive windows (for example 256 bytes) shows where in a file the high-entropy parts are: one encrypted blob inside an otherwise normal program stands out as a plateau.
Entropy of different kinds of data
The example computes for data it generates: repeated bytes, English text, the same text compressed with zlib, the text XOR-ed with a one-byte key, and pseudo-random bytes from a fixed seed (real keys come from secrets or os.urandom). Then it scans a composite blob with a sliding window.
import math, random, zlib
from collections import Counter
def entropy(data: bytes) -> float:
if not data:
return 0.0
n = len(data)
return abs(-sum(c / n * math.log2(c / n) for c in Counter(data).values())) # abs: no -0.00
text = (b"The quick analyst reads the strings first, then the headers, then the code. "
b"Every finding is written down with its evidence and the offset where it was seen. ") * 12
samples = {
"zeros (1 KiB)": bytes(1024),
"English text": text,
"text, zlib -9": zlib.compress(text, 9),
"text XOR 0x5A": bytes(b ^ 0x5A for b in text),
"random (seed 1)": random.Random(1).randbytes(2048),
}
for name, data in samples.items():
print(f"{name:<18} {len(data):5d} bytes H = {entropy(data):.2f} bits/byte")
# A composite "file": text, then 1 KiB of random-looking bytes, then text again.
blob = text[:1024] + random.Random(7).randbytes(1024) + text[:1024]
print("--- sliding window, 256 bytes")
for off in range(0, len(blob), 256):
h = entropy(blob[off:off + 256])
print(f"{off:5d} {h:4.2f} {'#' * round(h * 4):<32} {'HIGH' if h > 7.0 else ''}")zeros (1 KiB) 1024 bytes H = 0.00 bits/byte
English text 1896 bytes H = 3.97 bits/byte
text, zlib -9 133 bytes H = 6.73 bits/byte
text XOR 0x5A 1896 bytes H = 3.97 bits/byte
random (seed 1) 2048 bytes H = 7.91 bits/byte
--- sliding window, 256 bytes
0 4.01 ################
256 3.97 ################
512 3.94 ################
768 3.95 ################
1024 7.20 ############################# HIGH
1280 7.10 ############################ HIGH
1536 7.18 ############################# HIGH
1792 7.19 ############################# HIGH
2048 4.01 ################
2304 3.97 ################
2560 3.94 ################
2816 3.95 ################
Three lessons hide in those numbers. Compression and encryption both push entropy towards 8 (the 133-byte zlib output reaches only 6.73 because it is short; see the window-size point below), so high entropy says "packed, compressed or encrypted", not which. A one-byte XOR doesn't change entropy at all: it only renames the byte values, so the histogram has the same shape, which is why simple XOR "encryption" is caught by other means (strings disappear, but the entropy stays text-like and frequency analysis breaks it). And a 256-byte window of random data scores below 8 (around 7.2) simply because 256 samples can't fill 256 bins evenly: thresholds depend on the window size.
The entropy-scan lab plots the same sliding window over a synthetic 32 KiB file. It starts on a document with a 10 KiB embedded archive and a 256-byte window: the archive peaks at only 7.281 bits per byte, so the default 7.2 threshold flags just 11 of 128 windows. Switch the window to 1024 bytes and the peak rises to 7.818, with 38 of 125 windows flagged; the data did not change, only the sample size. Then pick the ransomware-encrypted file: at 1024 bytes its encrypted blocks peak at 7.836, almost the same as the archive, which is the "packed, compressed or encrypted, not which" point in numbers.
Strings and entropy on real files
The session compiles a toy with an ASCII string, a UTF-16 string (u"...", as Windows stores text), a wchar_t string (L"...", UTF-32 on Linux) and an embedded 1 KiB table of pseudo-random bytes, then runs strings both ways and computes the entropy of the lab sample files and of the binary's sections with the system's Python.
python3 -c "import random; d=random.Random(3).randbytes(1024); print('const unsigned char blob[1024] = {' + ','.join(map(str, d)) + '};')" > blob.h
cat > tool.c <<'SRC'
#include <stdio.h>
#include <uchar.h>
#include <wchar.h>
#include "blob.h"
const char16_t u16[] = u"C:\\ProgramData\\LabTool\\settings.ini";
const wchar_t u32[] = L"/var/lib/labtool/state.db";
int main(void) {
printf("lab tool v2 (build 2026-01-15) %d %d %d\n", u16[0], (int)u32[0], blob[5]);
return 0;
}
SRC
gcc -O2 tool.c -o tool
echo "== strings (ASCII)"; strings -n 8 tool | grep -i 'lab'
echo "== strings -e l (UTF-16LE)"; strings -e l -n 8 tool
echo "== strings -e L (UTF-32LE)"; strings -e L -n 8 tool
cat > ent.py <<'PY'
import math, sys, subprocess
from collections import Counter
def H(d):
n = len(d); return -sum(c/n*math.log2(c/n) for c in Counter(d).values()) if n else 0
for path in sys.argv[1:]:
d = open(path, "rb").read()
print(f"{path.split('/')[-1]:<14} {len(d):6d} bytes H = {H(d):.2f}")
PY
python3 ent.py $SAMPLES/files/hello.txt $SAMPLES/files/hello.txt.gz $SAMPLES/files/random.bin tool
for s in .text .rodata .data; do objcopy -O binary --only-section=$s tool sec.bin; printf '%-7s' "$s"; python3 ent.py sec.bin | cut -c15-; done== strings (ASCII)
lab tool v2 (build 2026-01-15) %d %d %d
== strings -e l (UTF-16LE)
C:\ProgramData\LabTool\settings.ini
== strings -e L (UTF-32LE)
/var/lib/labtool/state.db
hello.txt 51 bytes H = 4.25
hello.txt.gz 69 bytes H = 5.08
random.bin 4096 bytes H = 7.95
tool 16056 bytes H = 2.27
.text 297 bytes H = 5.15
.rodata 1312 bytes H = 7.27
.data 16 bytes H = 0.67
The ASCII strings finds the version banner but neither wide string; -e l finds the UTF-16 Windows path and -e L the UTF-32 Linux path. The sample files line up as expected, with one caveat: text is low and the 4 KiB random file is near 8, but hello.txt.gz scores only 5.08 because 69 bytes are far too few to fill 256 byte values (the same effect as the 133-byte zlib output above). Entropy needs enough data to mean anything. Inside the binary, .text (code) is in the typical machine-code range and the random table sits in .rodata, lifting that section's entropy far above the rest: the sliding window would show exactly where.
How analysts use them to detect hidden content
- Strings to look for: URLs and IP addresses, file paths, registry keys (
Software\Microsoft\Windows\CurrentVersion\Runis the classic persistence key), user agents, command lines, format strings with%snear network functions, PDB paths left by the compiler (C:\Users\dev\project\Release\tool.pdbnames the developer's folder), library version strings (they map to known CVEs). - The absence of strings in a program that clearly does something is a finding in itself: strings are encrypted or built at run time ("stack strings", assembled one character at a time with
mov byte [rsp+n], 'h'). Tools such as FLOSS emulate code to recover them. - Entropy above about 7.2 over large regions of a code section, a section with a high-entropy body and a small low-entropy stub, or a high-entropy overlay after the last section: packed or encrypted content (Packing).
How it is done right (for developers)
- Don't put secrets in strings. Anything in the binary, encoded or not, is readable: API keys, passwords, private URLs belong in configuration and secrets stores (Secrets Management).
- Remove build paths with
-ffile-prefix-map=(GCC/Clang) or/PDBALTPATH(MSVC) if the internal folder layout is sensitive. - Expect that legitimate compressed resources (images, fonts) raise entropy too: an analyst will look, so document them.
Pitfalls
- Minimum length:
-n 4floods you with junk from code bytes;-n 8is a better first pass. - UTF-16 and other encodings: always run
-e l(and-e bfor big-endian) on Windows files; UTF-8 non-ASCII text is split by plainstrings. - Entropy says nothing about intent: a game's compressed assets and a ransomware payload look the same.
- Small windows underestimate entropy; compare like with like.
Practice
1. What is $H$ for a file whose bytes alternate between exactly two values?
Each value has , so bit per byte.
2. A string XOR-ed with a one-byte key no longer appears in strings. Does entropy reveal it, and what does?
No: entropy is unchanged. A brute force over the 256 keys looking for readable text does (tools such as FLOSS and CyberChef's XOR brute force).
3. Why does a 256-byte window of perfectly random bytes score below 8?
With only 256 samples, most of the 256 values appear 0, 1 or 2 times rather than exactly once, so the measured distribution is not uniform; the expected value is about 7.2 bits per byte.
Test yourself
Which strings option finds Windows wide (UTF-16LE) strings?
- -n 8
- -e l
- -a
- -t x
What does entropy close to 8 bits per byte over a large region usually mean?
- English text
- Machine code
- Compressed or encrypted data
- A region of zeros
Why did XOR with 0x5A leave the entropy of the text unchanged?
- XOR is strong encryption
- XOR with a constant only relabels byte values, so the histogram keeps its shape
- zlib was used
- The key was random
In the entropy-scan lab, the same embedded archive peaks at 7.281 bits per byte with a 256-byte window and at 7.818 with a 1024-byte window. Why?
- The archive is decompressed at the larger size
- 256 samples can't fill 256 byte values evenly, so small windows underestimate the entropy of near-uniform data
- The threshold changed
- Larger windows include the zero padding
A program clearly opens network connections but contains no readable host names or URLs. What does it suggest?
- It has no network code
- Its strings are encrypted or built at run time
- It is stripped
- It is a PIE