Repository navigation
Replies: 3 comments 1 reply
|
Hello, |
0 replies
|
here a simple case, it's will be get #!/usr/bin/env python3
"""
Database exploration example for IDA Domain API.
This example demonstrates how to open an IDA database and explore its basic properties.
"""
import argparse
from dataclasses import asdict
import ida_domain
from ida_domain import Database
from ida_domain.database import IdaCommandOptions
import ida_hexrays
import ida_lines
import ida_funcs
class LVarRefsVisitor(ida_hexrays.ctree_visitor_t):
def __init__(self, cfunc: ida_hexrays.cfunc_t, lvar: ida_hexrays.lvar_t, lvar_idx: int):
super().__init__(ida_hexrays.CV_FAST)
self.cfunc = cfunc
self.lvar = lvar
self.lvar_idx = lvar_idx
self.refs: list[ida_hexrays.citem_t] = []
def visit_expr(self, expr: ida_hexrays.cexpr_t):
if expr.op == ida_hexrays.cot_var:
if expr.v.idx == self.lvar_idx:
pci: ida_hexrays.citem_t = self.cfunc.body.find_parent_of(expr)
if pci.cexpr is None:
raise ValueError(f'No parent statement found for {expr.dstr()}')
self.refs.append(pci)
return 0
def get_lvar_references(fn: ida_hexrays.cfunc_t, lvar: ida_hexrays.lvar_t, lvar_idx: int) -> list[ida_hexrays.citem_t]:
visitor = LVarRefsVisitor(fn, lvar, lvar_idx)
visitor.apply_to(fn.body, None)
return visitor.refs
def explore_database(db_path):
"""Explore basic database information."""
ida_options = IdaCommandOptions(auto_analysis=True, new_database=True)
with Database.open(db_path, ida_options) as db:
# Get basic information
print(f'Address range: {hex(db.minimum_ea)} - {hex(db.maximum_ea)}')
# Get metadata
print('Database metadata:')
metadata_dict = asdict(db.metadata)
for key, value in metadata_dict.items():
print(f' {key}: {value}')
print(f'Total functions: {len(db.functions)}')
start_fn = db.functions.get_function_by_name('start')
print(f'Start function: {start_fn.name}')
print(f'Start function address: {hex(start_fn.start_ea)}')
print(f'Start function size: {start_fn.size()}')
start_pseudocode = db.functions.get_pseudocode(start_fn)
print(f'Start function pseudocode: \n{"\n".join(start_pseudocode)}')
# get local variables referenced in the start function
failure = ida_hexrays.hexrays_failure_t()
cfunc: ida_hexrays.cfunc_t = ida_hexrays.decompile(start_fn.start_ea, failure)
local_vars: list[ida_hexrays.lvar_t] = cfunc.get_lvars()
print(f'Local variables: {[var.name for var in local_vars]}')
print('-' * 100)
for idx, var in enumerate(local_vars):
print(f'Local variable: {var.name}')
refs = get_lvar_references(cfunc, var, idx)
print(f'Local variable references: {len(refs)}')
for ref in refs:
sx, sy = cfunc.find_item_coords(ref)
print(f' > {sy}: {start_pseudocode[sy]}')
print('-' * 100)
def main():
"""Main entry point with argument parsing."""
parser = argparse.ArgumentParser(description='Database exploration example')
parser.add_argument(
'-f', '--input-file', help='Binary input file to be loaded', type=str, required=True
)
args = parser.parse_args()
explore_database(args.input_file)
if __name__ == '__main__':
from dotenv import load_dotenv
load_dotenv()
main() |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
TL;DR
Xrefs for lvars, we can get the local variables references (include coords and codeline just like LSP) in pseudocode.
I have implemented a PoC using the IDAPython SDK, I don't know it's I'm not sure if it's appropriate to enhancing ida-domain.
All reactions