Skip to content

Commit 7e9234c

Browse files
committed
Dumping binary data to a temporary file if the original is missing
Closes #64
1 parent 287552b commit 7e9234c

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

binjaplug.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import platform
55
import sys
66
import traceback
7+
import tempfile
78

89
import binaryninja
910
from binaryninja import BinaryView, Symbol, SymbolType, Type, Structure, StructureType, FunctionGraphType, LowLevelILOperation, MediumLevelILOperation
@@ -395,6 +396,8 @@ def __init__(self, bv):
395396
self.old_symbols = []
396397
self.old_dvs = set()
397398

399+
self.temp_file = None
400+
398401
# ensure bv path adheres to OS convention
399402
if platform.system() == 'Windows':
400403
self.bv.file.original_filename = self.bv.file.original_filename.replace('/', '\\')
@@ -573,8 +576,24 @@ def exec(self):
573576
self.connecting = True
574577
fpath = self.bv.file.original_filename
575578

579+
# TODO: Detect modifications and run from tmp if any exist #60
580+
run_from_tmp = False
576581
if not os.path.exists(fpath):
577-
raise Exception('cannot find debug target: ' + fpath)
582+
# Original target was deleted
583+
run_from_tmp = True
584+
585+
if run_from_tmp:
586+
self.temp_file = tempfile.NamedTemporaryFile(prefix="bndebugfile_")
587+
temp_name = self.temp_file.name
588+
# macOS symlinks /tmp to /private/tmp
589+
temp_name = os.path.realpath(temp_name)
590+
591+
self.bv.file.raw.save(temp_name)
592+
os.chmod(temp_name, 0o700)
593+
594+
# Swap the real path for this temp one
595+
fpath = temp_name
596+
self.modules.translations[fpath] = self.bv.file.original_filename
578597

579598
adapter = DebugAdapter.get_new_adapter(self.adapter_type, stdout=self.on_stdout)
580599
self.adapter = QueuedAdapter.QueuedAdapter(adapter)
@@ -641,6 +660,10 @@ def quit(self):
641660
self.remote_arch = None
642661
self.memory_dirty()
643662

663+
# Clean up temporary dumped file
664+
if self.temp_file is not None:
665+
self.temp_file = None
666+
644667
def restart(self):
645668
self.quit()
646669
time.sleep(1)

0 commit comments

Comments
 (0)