Ejemplo n.º 1
0
def import_annotation(binary: Binary, type_: FileType,
                      path: Path) -> Annotation:
    """Imports functions from annotation file and creates Annotation object.

    Args:
        binary: The Binary to which to import functions.

	Returns:
        the Annotation, with newly created functions.
    """

    if not path.is_file():
        raise FileNotFoundError
    logger.info(f"Importing {type_.name} file from {path}")
    ann_cls, fnc_gen = FileTypeMapping[type_]
    binary.partition()
    annotation = ann_cls(binary=binary, path=str(path))
    meta = fnc_gen(path)
    for name, addr, size, mode in meta:
        if not binary.range_is_valid(addr, addr + size):
            logger.debug(
                f"{name} @{addr:#08X}-{addr+size:#08X} not in valid range")
            continue
        if len(name) > 0 and binary.range_is_valid(addr, addr + size):
            fnc = Function.get(binary=binary, addr=addr)
            if not fnc:
                fnc = binary.functions.create(addr=addr,
                                              size=size,
                                              name=name,
                                              mode=mode)
            annotation.functions.add(fnc)
    binary.annotations.add(annotation)
    return annotation
Ejemplo n.º 2
0
def get_or_create_binary(path: Path, make_target=False) -> Binary:
    """import_binary creates a Binary and stores it in database

	Args:
        binary_path: the file path of the binary

	Returns: Binary
    """

    logger.info(f"retrieving binary for path {path}")
    binary = Binary.get(filepath=str(path))
    if not binary:
        if not path.is_file():
            raise FileNotFoundError
        binary = Binary(filepath=str(path), name=path.name)
    elif make_target:
        binary.is_target = True
    binary.partition()
    return binary
Ejemplo n.º 3
0
def match_matchers_against(target: Binary,
                           graph=None,
                           parallelize=True) -> Iterable[Match]:
    """match_candidates_against matches all candidates against the given
    Binary.

	Args:
        target: the target binary to match the candidates against
    """

    settings = SettingsStorage.get_settings()
    parallelize = parallelize or settings.get("matcher_parallelization", False)
    if graph is None:
        graph = makeGraph()
    bin_ = target.read()
    partitions = target.partition()
    if parallelize:
        matches = parallel_prepartioned_graph_match(graph, bin_, partitions)
    else:
        matches = prepartioned_graph_match(graph, bin_, partitions)
    match_results = []
    for matchers, size, end in matches:
        start = end - size
        match, new = Match.deduplicate(target, start, size,
                                       dict(certainty=1 / len(matchers)))
        for matcher in matchers:
            match.matched_by.add(matcher)
        if new:
            target.matches.add(match)
            match_results.append(match)
    orm.commit()

    if settings.get("find_fnc_starts", False):
        starts = find_starts(target,
                             start_cut=settings.get("fnc_start_size", 8))
        match_results.extend(starts)
        orm.commit()
    return match_results