Skip to content

Commit

Permalink
Binary Ninja update and fix (#2205)
Browse files Browse the repository at this point in the history
* Fix binja warning (use of a deprecated API method)

* Update binja plugin
> Fix json openning and parsing
> Fix base address

* Fix code_style

* lint black update
  • Loading branch information
Maxou56800 authored Jul 12, 2024
1 parent bb86d14 commit 76913af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion capa/features/extractors/binja/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class BinjaFeatureExtractor(StaticFeatureExtractor):
def __init__(self, bv: binja.BinaryView):
super().__init__(hashes=SampleHashes.from_bytes(bv.file.raw.read(0, len(bv.file.raw))))
super().__init__(hashes=SampleHashes.from_bytes(bv.file.raw.read(0, bv.file.raw.length)))
self.bv = bv
self.global_features: List[Tuple[Feature, Address]] = []
self.global_features.extend(capa.features.extractors.binja.file.extract_file_format(self.bv))
Expand Down
26 changes: 21 additions & 5 deletions scripts/import-to-bn.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def load_analysis(bv):
return 0
binaryninja.log_info(f"Using capa file {path}")

doc = json.loads(path.read_bytes().decode("utf-8"))
with Path(path).open("r", encoding="utf-8") as file:
doc = json.load(file)

if "meta" not in doc or "rules" not in doc:
binaryninja.log_error("doesn't appear to be a capa report")
Expand All @@ -83,20 +84,35 @@ def load_analysis(bv):
binaryninja.log_error("sample mismatch")
return -2

# Retreive base address
capa_base_address = 0
if "analysis" in doc["meta"] and "base_address" in doc["meta"]["analysis"]:
if doc["meta"]["analysis"]["base_address"]["type"] == "absolute":
capa_base_address = int(doc["meta"]["analysis"]["base_address"]["value"])

rows = []
for rule in doc["rules"].values():
if rule["meta"].get("lib"):
continue
if rule["meta"].get("capa/subscope"):
continue
if rule["meta"]["scope"] != "function":
if rule["meta"]["scopes"].get("static") != "function":
continue

name = rule["meta"]["name"]
ns = rule["meta"].get("namespace", "")
for va in rule["matches"].keys():
va = int(va)
rows.append((ns, name, va))
for matches in rule["matches"]:
for match in matches:
if "type" not in match.keys():
continue
if "value" not in match.keys():
continue
va = match["value"]
# Substract va and CAPA base_address
va = int(va) - capa_base_address
# Add binja base address
va = va + bv.start
rows.append((ns, name, va))

# order by (namespace, name) so that like things show up together
rows = sorted(rows)
Expand Down

0 comments on commit 76913af

Please sign in to comment.