Skip to content

[medium] Fix EP-section entropy check never firing in packer scoring - #3

Open
elhoim wants to merge 1 commit into
MISP:masterfrom
elhoim:fix-ep-section-entropy-index
Open

elhoim wants to merge 1 commit into
MISP:masterfrom
elhoim:fix-ep-section-entropy-index

Conversation

@elhoim

@elhoim elhoim commented Sep 17, 2026

Copy link
Copy Markdown

BLUF

  • Priority: medium.
  • epSecIndex was a string, so the packer rule that compares it to 6 and indexes secEntropies with it never ran.
  • Result: the highest-weighted rule (+60) was dead, and every packer score was systematically 60 points low for the samples it was meant to catch.
  • Fix: cast to int and bound the index against the actual list length, plus skip rows whose entropy value is empty.
  • One-line change in standalone/Kinginyourcastle.py. No behaviour change anywhere outside --packerdata scoring.

What was wrong

check_ep_section() returns the entry-point section as the text name|index
(e.g. .text|0), and Database.py stores it in the epSection text column.
In the packer-scoring loop:

epSecIndex = epSection.split('|')[1]
# max. 6 secEntropies available
if epSecIndex <=6:
    if float(secEntropies[epSecIndex]) > 6.7 or float(secEntropies[epSecIndex]) < 6.0:
        eval = eval + 60

str.split() returns strings, and the value is never cast. Under Python 2 —
the only interpreter this file runs on — '0' <= 6 is always False, because
strings sort above ints in CPython 2's default cross-type ordering. The branch
therefore never executed and the failure was completely silent: no exception,
no log line.

Two further problems were hidden behind that dead branch:

  • secEntropies always holds exactly six items (Database.py selects
    secEntropy1..secEntropy6), so valid indices are 0..5. The bound <= 6
    is off by one: index 6 would raise IndexError the moment the comparison
    started working.
  • When the entry point falls inside no section, check_ep_section() returns
    pos == len(pe.sections). For a sample with fewer than six sections the
    corresponding secEntropy column holds the empty string (absent sections are
    stored as "", not NULL), so float('') would raise ValueError — which,
    being unhandled, would abort the whole update_packed pass for every
    remaining row. That combination — entry point outside any section, few
    sections — is a classic packed/malformed-PE signature, i.e. exactly the
    population this script exists to score.

Impact

Entry-point section entropy is the single strongest packer signal in this
heuristic, weighted +60 against the next-highest +90/+50. Because the
rule never fired, every score written by --packerdata has been low by 60 for
the samples it was designed to flag, compressing the packed/not-packed
separation the score is supposed to provide.

Note for consumers: after this fix, scores for affected samples rise by
60. update_packed() only stores the value, and nothing in this repository
thresholds on the packed column, but anything downstream that does will see a
shifted distribution.

What this changes

standalone/Kinginyourcastle.py, one hunk:

epSecIndex = int(epSection.split('|')[1])
# max. 6 secEntropies available
if epSecIndex < len(secEntropies) and secEntropies[epSecIndex]:
    if float(secEntropies[epSecIndex]) > 6.7 or float(secEntropies[epSecIndex]) < 6.0:
        eval = eval + 60
  • int(...) makes the value usable both as a comparison operand and as a list index.
  • < len(secEntropies) replaces <= 6, fixing the off-by-one without hardcoding
    the width a second time. The existing comment stays accurate.
  • and secEntropies[epSecIndex] mirrors the if item[5]: idiom used a few lines
    above and keeps the now-reachable branch from crashing on an empty entropy cell.

Nothing else is touched: no reformatting, no modernisation, no dependency changes.

How it was verified

This repository has no test suite, no build system and no CI, so the rule was
reproduced standalone and exercised directly (the module itself is Python 2
only — print statements, file(), pefile — so it cannot be imported under
python3):

case before after
`'.text 0', all entropies 7.1` +0 (rule dead)
`' 1', 1-section sample (['7.1','','','','','']`) n/a
`'UPX1 6'`, six entropies n/a

The pre-fix comparison '0' <= 6 was confirmed to be a silent no-op rather than
an error on Python 2 (it raises TypeError on Python 3, which is how the dead
branch is demonstrable today).

Syntax was checked with bash -n hashes-extract.sh and by AST-parsing the
patched module; the file's pre-existing Python 2 syntax (line 389) is unchanged
by this patch and fails to parse under python3 at HEAD as well.

epSection is stored as the text "name|index", so split('|')[1] yields a
string. Comparing it to 6 is silently False on Python 2, leaving the
highest-weighted packer rule (+60) unreachable and every score 60 low.

Cast the index to int, bound it against len(secEntropies) instead of the
off-by-one "<= 6", and skip rows whose entropy cell is empty, which
happens when the entry point falls outside every section of a sample
with fewer than six sections.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant