Skip to content

Commit 47d5aab

Browse files
author
rdmmf
committed
added ghidra-function metadatas
1 parent ea4290f commit 47d5aab

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

mispghidra/PyMISPGhidra.py

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,27 @@ def create_file_objects(self, event, ghidraProgram=None):
240240
)
241241

242242
def get_function_infos(self, func):
243+
# Initialize all potential conditional values to None
244+
ext_lib = fh_hex = fx_hex = fn_sig = fn_code = calling_convention = None
245+
243246
# Basic Info
244247
entry_point = func.getEntryPoint()
245248

246249
# Handle External/Thunk logic
247-
ext_lib = None
248250
if func.isThunk():
249251
thunked = func.getThunkedFunction(True)
250252
if thunked and thunked.getExternalLocation():
251253
ext_lib = thunked.getExternalLocation().getLibraryName()
252254
elif func.getExternalLocation():
253255
ext_lib = func.getExternalLocation().getLibraryName()
254256

257+
instruction_count = len(
258+
list(self.ghidraProgram.getListing().getInstructions(func.getBody(), True))
259+
)
260+
255261
# FID Hashes
256262
hash_function = self.FIDservice.hashFunction(func)
257-
fh_hex = None
258-
fx_hex = None
259-
260263
if hash_function:
261-
# Handle potential property vs method access in pyghidra/ghidra versions
262264
fh = hash_function.getFullHash()
263265
fx = hash_function.getSpecificHash()
264266
fh_hex = Long.toHexString(fh)
@@ -270,6 +272,23 @@ def get_function_infos(self, func):
270272
[format(f & 0xFFFFFFFF, "08x") for f in signature.features]
271273
)
272274

275+
# Decompilation logic
276+
decomp_results = self.decompiler.decompileFunction(func, 30, self.monitor)
277+
try:
278+
decomp_func = decomp_results.getDecompiledFunction()
279+
fn_sig = decomp_func.getSignature()
280+
fn_code = decomp_func.getC()
281+
except:
282+
print("There was an error in decompilation!")
283+
284+
calling_convention = (
285+
str(func.getCallingConventionName())
286+
if func.getCallingConventionName()
287+
else None
288+
)
289+
lang_id = self.ghidraProgram.getLanguageID().toString()
290+
comp_id = self.ghidraProgram.getCompilerSpec().getCompilerSpecID().toString()
291+
print(lang_id, comp_id)
273292
return {
274293
"function-name": func.getName(),
275294
"entrypoint-address": entry_point.getOffset(),
@@ -280,10 +299,14 @@ def get_function_infos(self, func):
280299
"function-scope": "import" if func.isExternal() else "internal",
281300
"decompiler-minor-version": self.decompiler.getMinorVersion(),
282301
"decompiler-major-version": self.decompiler.getMajorVersion(),
283-
"instruction-count": func.getBody().getNumAddresses(),
302+
"instruction-count": instruction_count,
284303
"fid-fh-hash": fh_hex,
285304
"fid-fx-hash": fx_hex,
286305
"bsim-vector": vector_csv,
306+
"decompiled-function": fn_code,
307+
"function-signature": fn_sig,
308+
"return-type": func.getReturnType().getName(),
309+
"calling-convention": calling_convention,
287310
}
288311

289312
def _create_object_from_function(self, func):
@@ -335,6 +358,23 @@ def _create_object_from_function(self, func):
335358

336359
ghidra_function.add_attribute("bsim-vector", value=info["bsim-vector"])
337360

361+
if info["decompiled-function"]:
362+
ghidra_function.add_attribute(
363+
"decompiled-function", value=info["decompiled-function"]
364+
)
365+
366+
if info["function-signature"]:
367+
ghidra_function.add_attribute(
368+
"function-signature", value=info["function-signature"]
369+
)
370+
371+
if info["calling-convention"]:
372+
ghidra_function.add_attribute(
373+
"calling-convention", value=info["calling-convention"]
374+
)
375+
376+
ghidra_function.add_attribute("return-type", value=info["return-type"])
377+
338378
logger.info(
339379
f"Created MISP object for {info['function-name']} at {hex(info['entrypoint-address'])}"
340380
)

mispghidra/misp/object-templates/ghidra-function/definition.json

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
"disable_correlation": true
88
},
99
"label": {
10-
"description": "ghidra symbol label",
10+
"description": "ghidra symbol label(s) associated with the function",
1111
"misp-attribute": "text",
1212
"ui-priority": 0,
1313
"disable_correlation": true,
1414
"multiple": true
1515
},
1616
"function-scope": {
17-
"description": "ghidra function type",
17+
"description": "ghidra function scope (export, import, internal)",
1818
"misp-attribute": "text",
1919
"ui-priority": 0,
2020
"disable_correlation": true,
@@ -25,13 +25,13 @@
2525
]
2626
},
2727
"is-thunk": {
28-
"description": "ghidra function type",
28+
"description": "identifies a thunk function",
2929
"misp-attribute": "boolean",
3030
"ui-priority": 0,
3131
"disable_correlation": true
3232
},
3333
"external-library": {
34-
"description": "external library name if the function is an external",
34+
"description": "external library name if the function is an import",
3535
"misp-attribute": "text",
3636
"ui-priority": 0,
3737
"disable_correlation": true
@@ -49,7 +49,7 @@
4949
"disable_correlation": true
5050
},
5151
"entrypoint-address": {
52-
"description": "function entry point address",
52+
"description": "function entrypoint address (integer in a text for consistency with the entrypoint-address in ELF/PE/Mach-O Objects)",
5353
"misp-attribute": "text",
5454
"ui-priority": 0,
5555
"disable_correlation": true
@@ -96,6 +96,18 @@
9696
"misp-attribute": "text",
9797
"ui-priority": 0,
9898
"disable_correlation": true
99+
},
100+
"return-type": {
101+
"description": "The data type returned by the function",
102+
"misp-attribute": "text",
103+
"ui-priority": 0,
104+
"disable_correlation": true
105+
},
106+
"calling-convention": {
107+
"description": "The calling convention used by the function (e.g., cdecl, stdcall)",
108+
"misp-attribute": "text",
109+
"ui-priority": 0,
110+
"disable_correlation": true
99111
}
100112
},
101113
"description": "ghidra function",

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ Event correlation graph
111111
Event graph
112112

113113
<img src="img/function_call_graph.png">
114+
115+
# License
116+
117+
This software is licensed under [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0)
118+
119+
Copyright 2026 Thomas Caillet
120+
Copyright 2026 CIRCL - Computer Incident Response Center Luxembourg

0 commit comments

Comments
 (0)