Skip to content

Instantly share code, notes, and snippets.

@nstarke
Created August 25, 2019 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nstarke/bc662d2858756f4812d74f7fb3eab28a to your computer and use it in GitHub Desktop.
Save nstarke/bc662d2858756f4812d74f7fb3eab28a to your computer and use it in GitHub Desktop.
Find Entropy of Strings
#!/usr/bin/env python
#
# find-entropy.py
#
# A simple Utility to measure entropy of strings.
# Usage should be something like this:
#
# $ strings file.txt | python find-entropy.py
#
# The preset criteria of 3.75 should weed out most non-human created strings
#
import math
import string
import sys
def shannon_entropy(data):
"""
Adapted from http://blog.dkbza.org/2007/05/scanning-data-for-entropy-anomalies.html
by way of truffleHog (https://github.com/dxa4481/truffleHog)
"""
if not data:
return 0
entropy = 0
for x in string.printable:
p_x = float(data.count(x)) / len(data)
if p_x > 0:
entropy += - p_x * math.log(p_x, 2)
return entropy
for line in sys.stdin:
entropy = shannon_entropy(line)
if entropy > 3.75:
print (line[:-1], entropy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment