Source
import math import string import demistomock as demisto from CommonServerPython import * # noqa: E402 lgtm [py/polluting-import] from CommonServerUserPython import * # noqa: E402 lgtm [py/polluting-import] def calculate_shannon_entropy(data, minimum_entropy): """Algorithm to determine the randomness of a given data. Higher is more random/complex, most English words will yield in average result of 3 Args: data (str): The data to calculate entropy on. minimum_entropy (float): The minimum entropy. Returns: (str, dict, dict). Human readable, context, raw response """ entropy = 0.0 # each unicode code representation of all characters which are considered printable for char in (ord(c) for c in string.printable): # probability of event X p_x = float(data.count(chr(char))) / len(data) if p_x > 0: entropy += -p_x * math.log2(p_x) if entropy >= minimum_entropy: human_readable = tableToMarkdown( "Entropy results", {"Checked Value": data, "Entropy": entropy}, headers=["Checked Value", "Entropy"] ) return human_readable, {"EntropyResult": {"checked_value": data, "entropy": entropy}}, {} return f"Entropy for {data} is {entropy} - lower than {minimum_entropy}", {}, {} def main(): try: data = demisto.args().get("data", "") minimum_entropy = float(demisto.args().get("minimum_entropy", 0)) return_outputs(*calculate_shannon_entropy(data, minimum_entropy)) except Exception as ex: return_error(f"Failed to execute calculate entropy script. Error: {ex!s}") if __name__ in ("__main__", "__builtin__", "builtins"): main()
README
Calculates the entropy for the given data.
Script Data
| Name | Description |
|---|---|
| Script Type | python3 |
| Tags | entropy |
| Cortex XSOAR Version | 0.0.0 |
Inputs
| Argument Name | Description |
|---|---|
| data | The data for which to calculate entropy. |
| minimum_entropy | The minimum entropy value. Default is 0. |
Outputs
| Path | Description | Type |
|---|---|---|
| EntropyResult.checked_value | The given value (data). | String |
| EntropyResult.entropy | The entropy score. | Number |
Script Example
!CalculateEntropy data=abcd
Context Example
{
"EntropyResult": {
"checked_value": "abcd",
"entropy": 2
}
}
Human Readable Output
Entropy results
| Checked Value | Entropy |
|---|---|
| abcd | 2.0 |