A Beginner’s Guide to Entropy

August 16, 2016 | Views: 8587

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Recently on Cybrary OP3N, I read this article on Distributed Random Bit Generators. While the article was informative, I thought that readers unfamiliar with the concept of entropy might not be able to grasp some of its contents. I was thus inspired to write the this complete beginner’s guide to Shannon entropy, including a use case in binary analysis.

Entropy in Information Theory

Entropy is probably best known as an idea from thermodynamics, but the information theory definition of entropy was introduced by Claude Shannon in 1948 (and hence is also known as the Shannon entropy). It measures the unpredictability of information, typically from a data source.

The mathematical definition of the entropy of a sequence X, denoted H(X), is:

Where n is the length of the sequence, xi is the ith symbol in X, and b is the number of symbols in the source alphabet.

If you’re familiar with probability, you’ll note this is the expected value of the negative logarithm of P(x), also known as the expected value of the information I(X). A sequence of only one symbol repeated, such as “AAAAAAAA,” would have entropy 0.

The maximum entropy is achieved when all symbols in the source alphabet (0 and 1 in the binary case) are equally likely, which is fairly intuitive. If any symbol occurred most often, we’d use that fact to predict with higher accuracy than random guessing. But, if all symbols are equally probable, then we cannot improve on random guessing.

Although the general formula is for any b-ary alphabet, the convention when working with files is to calculate the entropy in bits per byte-character. This means that our alphabet is {0, 1} and that we can sum over all 28 or 256 possible characters (note that summing over all possible characters is equivalent to summing over each character in the sequence; we’re able to take a shortcut because we know in advance the number of possible symbols).

In practical terms, the entropy will be calculated by finding the frequency of each ASCII character and then finding:

Compression and Encryption

Consider a string of English plaintext, such as:

We the People, of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defense, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America.

The entropy of this sequence, computed in the traditional fashion, is H = 4.2724.

Compressed using gzip, this string looks like:


Now, the entropy of the compressed string is H = 5.78974.

Note: Although the compression is difficult to see because the string is short, gzip compressed it by 148%, from 328 bytes to 222 bytes.

Encrypted with AES-256 and the key “entropy”, this string looks like:


The entropy of the encrypted string seen here is H = 5.90141.

Both compression and encryption schemes increase the entropy of plaintext, because there are not as many patterns present as in natural language. However, there’s inherent tension between the processes of compression and encryption.

In high-level terms, encryption transforms data to appear as random as possible, while compression works by taking advantage of the repetitiveness and non-randomness of data. Therefore, if you encrypt data and then try to compress it, the data will be virtually uncompressible (assuming the encryption has been performed correctly).

A common misconception is that reversing the order, by compressing and then encrypting, solves the problem. This will successfully reduce the size of the encrypted data, but it also has issues that have been described at length by security researchers (Dan Boneh of Stanford, for one prominent example), because compression algorithms leak information about the plaintext data. I will not go into details about these types of attacks here, but if you’re interested, read about the CRIME and BREACH exploits.

Used in Binary Analysis

Returning to the topic of the calculating the entropy of a binary file, it’s useful to find the limit of that value, so we know what range of numbers to expect. Again, the highest entropy means that:

Because of the 256 ASCII values, the maximum Shannon entropy of a file is 8. What are the practical applications of this fact? Namely, if the entropy of a binary file approaches that theoretical limit, we can be reasonably confident that its bits have been manipulated through compression or encryption.

When I spoke to one malware analyst, he said that his rule-of-thumb was that a file with an entropy over 6 was likely to be compressed or encrypted, and anything over 7 almost certainly was. If the entropy is lower but the file is evidently not plaintext, it might be merely obfuscated. Applying a single-bit XOR mask, for example, does not alter the entropy of a file at all (think about why), and yet would still render our text from above unrecognizable.

Once we determine that a file is compressed or encrypted, the next step would be to try to diagnose the scheme, particularly by seeing if the headers match any common compression algorithms (assuming we do not have a file extension). Otherwise, de-obfuscation is a much simpler task than decryption, and there are a variety of cryptologic techniques for this.

One can also use the measures of entropy before and after compression or encryption to determine how efficient their algorithm is. If a compressed file still has a relatively low entropy, it suggests that the algorithm was not very good at removing the redundant contents of the file.

v:* {behavior:url(#default#VML);}
o:* {behavior:url(#default#VML);}
w:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}




/* Style Definitions */
{mso-style-name:”Table Normal”;
mso-padding-alt:0in 5.4pt 0in 5.4pt;

Hopefully, this exercise has provided a fundamental understanding of Shannon entropy, and hinted towards its usefulness in analyzing binaries.

Share with Friends
Use Cybytes and
Tip the Author!
Share with Friends
Ready to share your knowledge and expertise?
  1. You lost me at the 3rd paragraph =(

    Thank you for your post anyway.

  2. Hi,

    I’m glad my article inspired you to write yours. Good job explaining the maths background. It’s a pitty they broke some of the formatting on your article — looks like you worked pretty hard on it.

    Small point though: I wrote on Deterministic Random Bit Generators, not Distributed 🙂

  3. Thanks for the writeup,
    there is some junk css code on the writeup at the last
    mistake ??

Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play

Support Cybrary

Donate Here to Get This Month's Donor Badge

Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?