-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugins/device_tree: Refactor and use
AnalysisPluginV0
Version bumped because unknown-model is now also part of the summary. Defining the schema allowed us to clean up much of the logic. While there is some arguable code left this is a step in the right direction. Also required a change in the architecture_detection plugin to accomodate that the result can be None.
- Loading branch information
Showing
7 changed files
with
272 additions
and
162 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,75 @@ | ||
from analysis.PluginBase import AnalysisBasePlugin | ||
from __future__ import annotations | ||
|
||
from helperFunctions.tag import TagColor | ||
from objects.file import FileObject | ||
from plugins.mime_blacklists import MIME_BLACKLIST_COMPRESSED | ||
from analysis.plugin.compat import AnalysisBasePluginAdapterMixin | ||
from analysis.plugin import AnalysisPluginV0, Tag | ||
from typing import Optional, Dict, TYPE_CHECKING | ||
|
||
from ..internal.device_tree_utils import dump_device_trees | ||
|
||
|
||
class AnalysisPlugin(AnalysisBasePlugin): | ||
""" | ||
Device Tree Plug-in | ||
""" | ||
|
||
NAME = 'device_tree' | ||
DESCRIPTION = 'get the device tree in text from the device tree blob' | ||
VERSION = '1.0.1' | ||
MIME_BLACKLIST = [*MIME_BLACKLIST_COMPRESSED, 'audio', 'image', 'video'] # noqa: RUF012 | ||
FILE = __file__ | ||
|
||
def process_object(self, file_object: FileObject): | ||
file_object.processed_analysis[self.NAME] = {'summary': []} | ||
|
||
device_trees = dump_device_trees(file_object.binary) | ||
if device_trees: | ||
file_object.processed_analysis[self.NAME]['device_trees'] = device_trees | ||
for result in device_trees: | ||
model = result.get('model') | ||
if model: | ||
file_object.processed_analysis[self.NAME]['summary'].append(model) | ||
self.add_analysis_tag( | ||
file_object=file_object, | ||
tag_name=self.NAME, | ||
value=self.NAME.replace('_', ' '), | ||
color=TagColor.ORANGE, | ||
propagate=False, | ||
) | ||
from ..internal.schema import Schema | ||
from ..internal.schema import DeviceTree, IllegalDeviceTreeError | ||
|
||
if TYPE_CHECKING: | ||
import io | ||
|
||
|
||
class AnalysisPlugin(AnalysisPluginV0, AnalysisBasePluginAdapterMixin): | ||
def __init__(self): | ||
metadata = AnalysisPluginV0.MetaData( | ||
name='device_tree', | ||
description='get the device tree in text from the device tree blob', | ||
version='1.1.0', | ||
system_version=None, | ||
mime_blacklist=[*MIME_BLACKLIST_COMPRESSED, 'audio', 'image', 'video'], | ||
timeout=10, | ||
Schema=Schema, | ||
) | ||
super().__init__(metadata=metadata) | ||
|
||
def summarize(self, result: Schema) -> list[str]: | ||
models = [device_tree.model for device_tree in result.device_trees if device_tree.model] | ||
|
||
if not models: | ||
return ['unknown-model'] | ||
|
||
return models | ||
|
||
return file_object | ||
def analyze( | ||
self, | ||
file_handle: io.FileIO, | ||
virtual_file_path: dict, | ||
analyses: Dict[str, dict], | ||
) -> Optional[Schema]: | ||
del virtual_file_path, analyses | ||
|
||
binary = file_handle.readall() | ||
|
||
offset = binary.find(DeviceTree.Header.MAGIC, 0) | ||
device_trees = [] | ||
|
||
while offset >= 0: | ||
try: | ||
device_tree = DeviceTree.from_binary(binary, offset=offset) | ||
# We found a valid device tree. | ||
# Skip only the header because device trees may contain devicetrees tehmselves. | ||
offset += DeviceTree.Header.SIZE | ||
device_trees.append(device_tree) | ||
except IllegalDeviceTreeError: | ||
offset += 1 | ||
|
||
offset = binary.find(DeviceTree.Header.MAGIC, offset) | ||
|
||
if len(device_trees) == 0: | ||
return None | ||
|
||
return Schema(device_trees=device_trees) | ||
|
||
def get_tags(self, result: Schema, summary: list[str]) -> list[Tag]: | ||
del result, summary | ||
return [ | ||
Tag( | ||
name=self.metadata.name, | ||
value='device tree', | ||
color=TagColor.ORANGE, | ||
), | ||
] |
This file contains 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
Oops, something went wrong.