From ce163e98c1cd547ea0b43385cdb03dc721c07fae Mon Sep 17 00:00:00 2001 From: Manuel Blatt Date: Thu, 29 Aug 2024 16:23:57 +0200 Subject: [PATCH] polish string slicer integration --- .../binaryninja/rust_string_detection.py | 23 ++++++++++---- decompiler/util/default.json | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/decompiler/frontend/binaryninja/rust_string_detection.py b/decompiler/frontend/binaryninja/rust_string_detection.py index 45fb386d..8290874a 100644 --- a/decompiler/frontend/binaryninja/rust_string_detection.py +++ b/decompiler/frontend/binaryninja/rust_string_detection.py @@ -4,20 +4,29 @@ from binaryninja import BinaryView from decompiler.util.options import Options -string_slicer_path = "/home/manuel/repos/" - class RustStringDetection: - """TODO:""" + """ + This 'stage' detects certain Rust strings (string slices), which are struct based strings. + It requires the RustStringSlicer. A path to the tool needs to be configured via the options. + + The stage is executed before lifting, as it uses the Binary Ninja API to identify string slices + and 'mark' them, by assigning the appropriate type. + It can be configured to run always, never, or for Rust binaries only. + """ def __init__(self, binary_view: BinaryView, options: Options): self._bv = binary_view - # TODO: add to default settings, change fallback self._enabled = options.getboolean("rust-string-detection.enabled", fallback=True) self._rust_binaries_only = options.getboolean("rust-string-detection.rust_binaries_only", fallback=True) + self._string_slicer_path = options.getstring("rust-string-detection.string_slicer_path") self._debug_submodules = options.getboolean("logging.debug-submodules") def is_rust_binary(self): + """ + Simple heurstic to determine, whether the binary is a Rust binary. + + """ for _ in self._bv.find_all_data(self._bv.start, self._bv.end, "rustc".encode("utf-8")): return True for _ in self._bv.find_all_data(self._bv.start, self._bv.end, "cargo".encode("utf-8")): @@ -26,7 +35,9 @@ def is_rust_binary(self): def run(self): """ - TODO: + Runs the Rust String Slicer, if the required conditions are met. + + String Slicer's path will be added to Python's path before importing the module. """ if not self._enabled: return @@ -37,7 +48,7 @@ def run(self): logging.info("Starting Rust String Slicer") try: - sys.path.append(string_slicer_path) + sys.path.append(self._string_slicer_path) from rust_string_slicer.binja_plugin.actions import RecoverStringFromReadOnlyDataTask, RustStringSlice if not RustStringSlice.check_binary_ninja_type_exists(self._bv): diff --git a/decompiler/util/default.json b/decompiler/util/default.json index 94fb9da3..194472af 100644 --- a/decompiler/util/default.json +++ b/decompiler/util/default.json @@ -129,6 +129,36 @@ "is_hidden_from_cli": false, "argument_name": "--remove-no-return-boilerplate" }, + { + "dest": "rust-string-detection.enabled", + "default": true, + "title": "Detect Rust string slices", + "type": "boolean", + "description": "enable the detection of Rust string slices. Requires setting up Rust String Slicer", + "is_hidden_from_gui": false, + "is_hidden_from_cli": false, + "argument_name": "--detect-rust-string-slices" + }, + { + "dest": "rust-string-detection.rust_binaries_only", + "default": true, + "title": "Restrict string slice detection to Rust binaries", + "type": "boolean", + "description": "string slices will only be detected for Rust binaries", + "is_hidden_from_gui": false, + "is_hidden_from_cli": false, + "argument_name": "--string-slices-rust-only" + }, + { + "dest": "rust-string-detection.string_slicer_path", + "default": "", + "title": "Rust String Slicer Path", + "type": "string", + "description": "Path to the Rust String Slicer folder", + "is_hidden_from_gui": false, + "is_hidden_from_cli": false, + "argument_name": "--rust-string-slicer-path" + }, { "dest": "array-access-detection.enabled", "default": true,