Algorithm Search⌘K

Techniquecore

Entropy-based packer detection

Also called entropy analysis of executables.

Flags executables whose sections have byte entropy near 8 bits, a sign of compression or encryption by a packer.

Entropy-based packer detection decides whether an executable file is probably packed, that is compressed or encrypted, by measuring how random its bytes look. A packer is a tool that compresses or encrypts the code of a program and adds a small routine that restores the code in memory when the program runs. Packing is common in malware, because it hides the real code from byte signatures, and it is also used for benign software to make files smaller or to protect them. Compressed and encrypted data have byte values that are close to uniform, so their Shannon entropy is close to the maximum of 8 bits per byte. Machine code and text have more structure and a lower entropy. Lyda and Hamrock (2007) measured this on a large set of files and proposed two cut-offs. The method is fast and simple, and it is a heuristic: it gives a hint that the file needs unpacking or other analysis, not a proof.

In plain words

Look at the bytes of a file and count how often each of the 256 possible byte values appears. In program code, some values, such as common instruction codes and zero bytes, appear very often, and others are rare. In compressed or encrypted data, all values appear about equally often, because a good compressor removes patterns and a good cipher hides them.

Entropy is one number for this. It is 0 when all bytes are the same, and 8 when all 256 values are equally common. A file where most parts score close to 8 probably holds compressed or encrypted data. A scanner then marks it for closer analysis.

When it applies

The method applies when the question is "is this file probably packed?" and a fast answer is useful, for example as a first filter before a slow analysis, or as one feature among many in a classifier. It needs only the bytes of the file, so it runs on files that cannot be run or disassembled.

It does not apply as a malware detector. Packing is not the same as malice. Installers, compressed archives inside programs and protected commercial software are packed and benign, and malware is not always packed. It also does not say which packer was used, or where the original code is.

The base fact behind it: for a block of bb bytes in which byte value ii appears cic_i times, the entropy of the block is

H=−∑i : ci>0ciblog⁡2cib.H = -\sum_{i \,:\, c_i > 0} \frac{c_i}{b} \log_2 \frac{c_i}{b}.

It is at most log⁡2min⁡(b,256)\log_2 \min(b, 256), with equality when the values that appear are all equally common. So H≤8H \le 8, and a block of 256 bytes reaches 8 only if every byte value appears exactly once.

The template

The core step, as Lyda and Hamrock describe it, with the block size bb and the two cut-offs as parameters:

  1. ENTROPY-SCAN(bytes of a file, b, A, M)
  2. 1split the bytes into blocks of b bytes
  3. 2for each block: count the 256 byte values and compute H of the block by the formula above
  4. 3avg ← the mean of H over the blocks; top ← the largest H of a block
  5. 4if avg > A and top > M then report "probably packed or encrypted"
  6. 5return avg, top and the list of block entropies

The time is linear in the size of the file, with 256 counters per block. Lyda and Hamrock used b=256b = 256. On their files, the average entropy was 4.347 for plain text, 5.099 for native executables, 6.801 for packed executables and 7.175 for encrypted executables, and the highest block entropy was 7.233 for packed and 7.303 for encrypted ones. From these values they derived the cut-offs A=6.677A = 6.677 and M=7.199M = 7.199. These numbers are measurements on their data set, which held 21,567 files from 2000 to 2005. The cut-offs are heuristics. They are not properties of packing, and a different data set, block size or file format gives different values.

Why encrypted data scores about 7.2, not 8. The formula on a short block is a biased estimate of the entropy of the source. For 256 bytes drawn uniformly at random, the expected count of each value is 1, but many values appear 0 times or 2 times. On 400 such synthetic blocks, generated with a fixed seed, the mean block entropy was 7.17, with a range from 7.01 to 7.30. With blocks of 4096 random bytes the mean was 7.95. So the value of 7.175 that Lyda and Hamrock measured for encrypted files is what a perfectly random source gives with 256-byte blocks. When you compare cut-offs, always state the block size.

Classic examples

  • The per-block entropy profile. A plot of block entropy against the position in the file shows the parts of a packed program: low entropy for the headers and the small restore routine, and a long plateau close to the maximum for the packed data. The plateau is the part that generic unpacking restores at run time.
  • Packed section detection. Scanners compute the entropy of each section of an executable and flag a code section with a very high value. This is the same template with sections in place of fixed blocks.
  • Entropy as one feature. Perdisci, Lanzi and Lee (2008) used entropy together with other features of the file headers and sections in classifiers that separate packed from not packed executables. A learned classifier over many features, like byte n-gram classification, is more accurate than a fixed cut-off on one number.
  • Compression in general. Data compressed by Huffman coding, LZ77 or arithmetic coding has high byte entropy for the same reason. The entropy of the bytes is a lower bound for the size of a byte-by-byte code, and a compressor removes the gap.

Variations

  • Sliding windows. Compute the entropy of a window that moves by fewer bytes than its length, to find the borders of packed regions more exactly.
  • Other statistics. Tests of uniformity, such as a chi-squared test of the byte counts, and the compression ratio of a general compressor measure the same property. An estimate of Kolmogorov complexity by compression is the idea behind the second.
  • Bias-corrected estimates. Correct the estimate for small blocks, or use larger blocks, so that the values for random data are close to 8 and do not depend on bb.
  • Run-time entropy. Measure the entropy of the memory of a running process over time. When the program restores its code, the entropy of that region drops, so the change can show that the restored code is now in memory.

Pitfalls

  • A cut-off used as a proof. A file above both cut-offs is not shown to be malicious, and a file below them is not shown to be clean. Report the score as a hint, and use it to choose the next analysis.
  • Benign high entropy. Images, audio, archives and certificates inside a benign program have high entropy. Scan the code sections separately from the resources, or the false alarm rate on benign files is high.
  • Padding and low-entropy data. Zero padding and other low-entropy data lower the average. A packed file with a large low-entropy part can stay under the average cut-off. Use the highest block and the per-block profile, not only the mean.
  • Cut-offs from a different block size. The values 6.677 and 7.199 were measured with 256-byte blocks. With 4096-byte blocks, random data scores about 7.95, so the same cut-offs flag nearly everything that is compressed. Recompute the cut-offs on your own data with your block size.
  • The measured numbers as a property of packers. The averages of Lyda and Hamrock describe the files they collected from 2000 to 2005. Packers have changed since then, and Ugarte-Pedrero and coauthors (2015) show a wide range of packer designs. Measure again on current data.

Exercises

  1. Compute HH for a block of 256 zero bytes, for the block of the 256 values 0 to 255 once each, and for 128 copies of the two bytes 61 and 62. The answers are 0, 8 and 1.
  2. Show that H≤log⁡2kH \le \log_2 k for a block in which only kk distinct byte values appear, with equality when they are equally common.
  3. For a block of b=2b = 2 bytes drawn uniformly at random, find the expected value of HH. The answer is 255/256255/256, well under the entropy of 8 bits per byte of the source, which shows the bias of short blocks in its simplest form.

References

  1. Robert Lyda and James Hamrock, Using entropy analysis to find encrypted and packed malware, IEEE Security and Privacy 5(2), 2007, pages 40 to 45, the entropy of 256-byte blocks, the average and highest entropy of plain text, native, packed and encrypted executables, and the two cut-offs derived from them
  2. Roberto Perdisci, Andrea Lanzi and Wenke Lee, Classification of packed executables for accurate computer virus detection, Pattern Recognition Letters 29, 2008, the features of packed files, entropy among them, and classifiers that separate packed from not packed executables
  3. Xabier Ugarte-Pedrero, Davide Balzarotti, Igor Santos and Pablo G. Bringas, SoK: Deep packer inspection: a longitudinal study of the complexity of run-time packers, IEEE Symposium on Security and Privacy 2015, the taxonomy of packers by the complexity of their unpacking, and the measurements on packed samples

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises