Skip to content

Commit e0eda06

Browse files
committed
update to comply with idascript +remove deprecated functions
1 parent a4bcdf6 commit e0eda06

File tree

2 files changed

+18
-59
lines changed

2 files changed

+18
-59
lines changed

src/binexport/__main__.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import os
1313

1414
from multiprocessing import Pool, Queue, Manager
15+
16+
from idascript import IDA_PATH_ENV, get_ida_path
1517
from binexport import ProgramBinExport
1618
from binexport.utils import logger
1719
from binexport.types import DisassemblerBackend
@@ -54,48 +56,37 @@ def recursive_file_iter(p: Path) -> Generator[Path, None, None]:
5456
yield from recursive_file_iter(f)
5557

5658

57-
def export_job(ingress, egress, backend: DisassemblerBackend) -> bool:
59+
def export_job(ingress, egress, backend: DisassemblerBackend) -> None:
5860
while True:
5961
try:
6062
file = ingress.get(timeout=0.5)
61-
res = ProgramBinExport.from_binary_file(
62-
file.as_posix(), backend=backend, open_export=False
63-
)
63+
res = ProgramBinExport.generate(file.as_posix(), backend=backend)
6464
egress.put((file, res))
65-
except Exception as e:
66-
# Might not be printed as triggered withing a fork
67-
logger.error(traceback.format_exception(e).decode())
68-
egress.put((file, e))
6965
except queue.Empty:
7066
pass
7167
except KeyboardInterrupt:
7268
break
69+
except Exception as e:
70+
# Might not be printed as triggered withing a fork
71+
logger.error(traceback.format_exception(e))
72+
egress.put((file, e))
7373

7474

75-
def __check_path() -> bool:
76-
global IDA_BINARY
77-
if "PATH" in os.environ:
78-
for p in os.environ["PATH"].split(":"):
79-
for bin_name in __get_names():
80-
if (Path(p) / bin_name).exists():
81-
IDA_BINARY = (Path(p) / bin_name).resolve()
82-
return True
83-
return False
84-
8575
def check_disassembler_availability(disass: DisassemblerBackend, disass_path: str) -> bool:
8676
"""
8777
Check if the disassembler is available in the system.
78+
It also set the necessary environment variables.
79+
8880
:param disass: Disassembler backend to check
8981
:param disass_path: Path of the disassembler (if not in PATH)
9082
:return: True if the disassembler is available, False otherwise
9183
"""
9284
if disass == DisassemblerBackend.IDA:
9385
if disass_path:
9486
ida_path = Path(disass_path)
95-
os.environ["IDA_PATH_ENV"] = str(ida_path) if ida_path.is_dir() else str(ida_path.parent)
87+
os.environ[IDA_PATH_ENV] = str(ida_path)
9688
try:
97-
from idascript import __check_path
98-
return __check_path()
89+
return bool(get_ida_path())
9990
except ImportError:
10091
logger.error("Cannot import idascript python module")
10192
return False
@@ -106,12 +97,12 @@ def check_disassembler_availability(disass: DisassemblerBackend, disass_path: st
10697
os.environ["GHIDRA_PATH"] = disass_path
10798
return ghidra_path.exists()
10899
else:
109-
logger.error(f"Ghidra path {ghidra_path} does not exist")
100+
logger.error(f"Ghidra path {disass_path} does not exist")
110101
return False
111102

112103
elif disass == DisassemblerBackend.BINARY_NINJA:
113104
try:
114-
import binaryninja
105+
import binaryninja # type: ignore
115106
except ImportError:
116107
logger.error("Cannot import binaryninja python module")
117108
return False
@@ -133,8 +124,8 @@ def check_disassembler_availability(disass: DisassemblerBackend, disass_path: st
133124
"--disass-path",
134125
type=click.Path(exists=True),
135126
default="",
136-
help="Path of the disassembler (if not in PATH)" \
137-
"Can be provided with IDA_PATH, GHIDRA_PATH env variables",
127+
help="Path of the disassembler (dir or binary for IDA, dir for Ghidra)" \
128+
"(if not provided search $PATH or environment variable IDA_PATH, GHIDRA_PATH)",
138129
)
139130
@click.option("-t", "--threads", type=int, default=1, help="Thread number to use")
140131
@click.option("-v", "--verbose", count=True, help="To activate or not the verbosity")
@@ -150,13 +141,6 @@ def main(disassembler: str,
150141
binexporter is a very simple utility to generate a .BinExport file
151142
for a given binary or a directory. It opens all binary files and export
152143
the them seamlessly.
153-
154-
:param disassembler: Disassembler engine to use
155-
:param disass_path: Path of the disassembler (if not in PATH)
156-
:param input_file: Path of the binary to export
157-
:param threads: number of threads to use
158-
:param verbose: To activate or not the verbosity
159-
:param stop_on_error: Stop if any of the worker raises an exception
160144
"""
161145

162146
logging.basicConfig(format="%(message)s", level=logging.DEBUG if verbose else logging.INFO)
@@ -198,7 +182,7 @@ def main(disassembler: str,
198182
if isinstance(res, Exception):
199183
logger.error(f"Error while processing {path}: {res}")
200184
if stop_on_error:
201-
logger.error(traceback.format_exception(res).decode())
185+
logger.error(traceback.format_exception(res))
202186
pool.terminate()
203187
break
204188
else:

src/binexport/program.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -124,29 +124,6 @@ def open(export_file: pathlib.Path | str) -> ProgramBinExport:
124124
"""
125125
return ProgramBinExport(export_file)
126126

127-
@staticmethod
128-
def from_binary_file(
129-
exec_file: pathlib.Path | str,
130-
output_file: str | pathlib.Path = "",
131-
open_export: bool = True,
132-
override: bool = False,
133-
backend: DisassemblerBackend = DisassemblerBackend.IDA,
134-
) -> ProgramBinExport | bool:
135-
"""
136-
DEPRECATED: Use `ProgramBinExport.from_binary` instead."""
137-
if not open_export:
138-
export_path = ProgramBinExport.generate(
139-
exec_file=exec_file,
140-
output_file=output_file,
141-
override=override,
142-
backend=backend,
143-
timeout=600,
144-
)
145-
return export_path.exists()
146-
else:
147-
ProgramBinExport.from_binary(exec_file=exec_file, output_file=output_file,
148-
override=override, backend=backend)
149-
150127
@staticmethod
151128
def from_binary(
152129
exec_file: pathlib.Path | str,
@@ -163,11 +140,9 @@ def from_binary(
163140
164141
:param exec_file: executable file path
165142
:param output_file: BinExport output file
166-
:param open_export: whether or not to open the binexport after export
167143
:param override: Override the .BinExport if already existing. (default false)
168144
:param backend: The backend to use. (Either 'IDA' or 'Ghidra')
169-
:return: an instance of ProgramBinExport if open_export is true, else boolean
170-
on whether it succeeded
145+
:return: an instance of ProgramBinExport
171146
"""
172147
binexport_file = ProgramBinExport.generate(
173148
exec_file=exec_file,

0 commit comments

Comments
 (0)