Repository navigation
Conversation
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) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BLUF
H()ingrouping/pe_parse.pynever counted byte values, so every PE section entropy written to Redis is wrong.data.count(bytes(x))—bytes(x)is x NUL bytes on Python 3 and the digit string"0".."255"on Python 2, never the single bytex.eval_packed's6.0 < entropy < 6.7packer test is unreachable on Python 3, so the+50packer contribution is dead and everypackedscore is understated.Counter(bytearray(data))and index it — correct on Python 2 and 3, and O(n) instead of O(256n).What was wrong
dataissection.get_data(), a bytes object frompefile.bytes(x)builds a buffer ofxNUL bytes.bytes(0)isb'', sodata.count(b'')returnslen(data) + 1and yieldsp_x > 1, contributing a small negative term.bytes(65)is 65 NUL bytes, notb'A', so it matches nothing in practice. Every other term is zero.bytes is str, sobytes(x)is"0","1", …"255"and the loop counts occurrences of ASCII digit substrings, not byte values.Under neither interpreter does the loop count occurrences of byte value
x.Concrete impact
entropyhash field written atgrouping/pe_parse.py:98is garbage. Measured before the fix: an all-zero 1 KiB buffer scored21.4533(above the theoretical maximum of 8.0) and a uniform 0..255 block scored0.0256.eval_packed's6.0 < float(entropy) < 6.7window can never be entered and the+50packer weight never applies. Packer scores are systematically missing that term on Python 3 and arbitrary on Python 2.What this changes
Counter(bytearray(data))and indexes it inside the existingrange(256)loop.bytearray(data)iterates as integers on both Python 2 and 3, which is why the wrap is kept rather thanCounter(data)(that would key on 1-character strings under Python 2). Absent byte values return 0 from theCounter, so theif p_x > 0guard keeps working unchanged.from collections import Counter, matching the existing usage ingrouping/examples/helper.py.float()division for Python 2, same loop, same early return for empty data, no new dependency (collectionsis stdlib).Note: entropy values already stored in Redis are stale and should be re-parsed if
packedscores matter, since the packer window now actually fires.Out of scope on purpose:
standalone/Kinginyourcastle.pycarries a second, differently broken copy ofH()(data.count(chr(x))); it is not touched here.How it was verified
Syntax gate over all tracked Python files (
ast.parse) plusbash -n hashes-extract.sh, thenH()exercised directly on buffers with known Shannon entropy:0x000x00+ 512x0x01The repository has no test suite, so no framework was introduced;
H()was extracted withastand executed standalone, which avoids needingpefile/redisinstalled.Local verification
Output:
Note: Two corrections to the "How it was verified" section of this PR body, both measured on this tree (Python 3.14):
512x 0x00 + 512x 0x01case is 13.4067, not0.0117. The other three before-values in the table (0.0256, 21.4533, 0.0000) are correct as written.ast.parsesyntax gate over all tracked Python files does not pass cleanly, before or after this change: four pre-existing Python-2-only files fail to parse under Python 3 (grouping/examples/helper.py,grouping/examples/ssdeep_highmatch.py,standalone/Database.py,standalone/Kinginyourcastle.py). They are unrelated to this change. Verification was therefore scoped to the changed file (python3 -m py_compile grouping/pe_parse.py).Also unverified end to end:
pefileandredisare not installed in the verification environment, and the module'sSafeConfigParserimp