From 1f13a966e5428135ebf7e54e9383aeef4377bd3b Mon Sep 17 00:00:00 2001 From: elhoim Date: Thu, 17 Sep 2026 08:22:12 +0000 Subject: [PATCH] Fix byte histogram in PE section entropy computation H() counted data.count(bytes(x)), which never yields the number of occurrences of byte value x: on Python 3 bytes(x) is a run of x NUL bytes (bytes(0) is b'', so the count is len(data) + 1 and the entropy goes negative), and on Python 2 bytes is str, so it counts the decimal digit strings "0".."255". Every section entropy stored in Redis was therefore wrong, and because the value stayed at or below zero on Python 3, eval_packed's 6.0 < entropy < 6.7 packer window was unreachable and its +50 contribution never applied. Build the histogram once with Counter(bytearray(data)) and index it. bytearray() iterates as integers on both Python 2 and 3, so the fix keeps the module's dual-interpreter support. Co-Authored-By: Claude Opus 5 (1M context) --- grouping/pe_parse.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/grouping/pe_parse.py b/grouping/pe_parse.py index 4d6a20b..1b752e3 100755 --- a/grouping/pe_parse.py +++ b/grouping/pe_parse.py @@ -6,6 +6,7 @@ import math import argparse import redis +from collections import Counter try: from configparser import SafeConfigParser except ImportError: @@ -108,9 +109,10 @@ def H(data): if not data: return 0 + occurrences = Counter(bytearray(data)) entropy = 0 for x in range(256): - p_x = float(data.count(bytes(x))) / len(data) + p_x = float(occurrences[x]) / len(data) if p_x > 0: entropy += - p_x * math.log(p_x, 2) return entropy