Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optionally pull byte histogram #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions characterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import subprocess
import traceback
from typing import Dict, List, Optional, Tuple, Union
import array

import hachoir.core.config as hachoir_config
from hachoir.core.log import log as hachoir_logger, Log, Logger
Expand Down Expand Up @@ -127,10 +128,19 @@ def start(self) -> None:
def execute(self, request: ServiceRequest) -> None:
request.result = Result()

# 1. Calculate entropy map
with open(request.file_path, 'rb') as fin:
# 1. Calculate entropy map
(entropy, part_entropies) = calculate_partition_entropy(fin)

# 2. Optionally get byte histogram
get_byte_histogram = self.config.get("get_entropy_histogram", False)
if get_byte_histogram:
fin.seek(0)
file_bytes = fin.read()
histogram = array.array('L', [0] * 256)
for byte in file_bytes:
histogram[byte] += 1

entropy_graph_data = {
'type': 'colormap',
'data': {
Expand All @@ -139,9 +149,18 @@ def execute(self, request: ServiceRequest) -> None:
}
}

ResultSection(f"File entropy: {round(entropy, 3)}", parent=request.result, body_format=BODY_FORMAT.GRAPH_DATA,
entropy_sec = ResultSection(f"File entropy: {round(entropy, 3)}", parent=request.result, body_format=BODY_FORMAT.GRAPH_DATA,
body=json.dumps(entropy_graph_data))

if get_byte_histogram:
histogram_data = { 'entropy_byte_histogram' : histogram.tolist() }
histo_subsec = ResultSection(
cccs-dawson marked this conversation as resolved.
Show resolved Hide resolved
'File Byte Histogram',
body_format=BODY_FORMAT.KEY_VALUE,
body=histogram_data,
parent=entropy_sec
)

if request.file_type == "meta/shortcut/windows":
# 2. Parse windows shortcuts
self.parse_link(request.result, request.file_path)
Expand Down