From 8ba63121e59a4160cbf31064e45a3b953b257cf7 Mon Sep 17 00:00:00 2001 From: markisha64 Date: Tue, 25 Jul 2023 23:14:37 +0200 Subject: [PATCH 1/3] feat: Reference Main script --- ghidra_scripts/ReferenceMain.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ghidra_scripts/ReferenceMain.py diff --git a/ghidra_scripts/ReferenceMain.py b/ghidra_scripts/ReferenceMain.py new file mode 100644 index 0000000..ca2a07f --- /dev/null +++ b/ghidra_scripts/ReferenceMain.py @@ -0,0 +1,29 @@ +# Takes all references to address and points them to same address in main memory +#@author markisha64 +#@category Reference +#@keybinding +#@menupath Tools.References.Reference Main +#@toolbar + +from ghidra.program.flatapi import FlatProgramAPI + +api = FlatProgramAPI(currentProgram) +addFact = api.getAddressFactory() + +refs = currentProgram.referenceManager.getReferencesTo(currentAddress) + +for ref in refs: + instruction = api.getInstructionAt(ref.getFromAddress()) + + if not instruction: + continue + + operand = ref.getOperandIndex() + address = addFact.getAddress(ref.getToAddress().toString("ram:")) + refType = ref.getReferenceType() + + newRef = api.createMemoryReference(instruction, operand, address, refType) + api.setReferencePrimary(newRef) + + api.removeReference(ref) + From 8bb3e9bff9a0649fa898493e18f6fb28c586cece Mon Sep 17 00:00:00 2001 From: markisha64 Date: Tue, 25 Jul 2023 23:45:41 +0200 Subject: [PATCH 2/3] refact: moved to data for better coverage --- ghidra_scripts/ReferenceMain.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ghidra_scripts/ReferenceMain.py b/ghidra_scripts/ReferenceMain.py index ca2a07f..c89e94d 100644 --- a/ghidra_scripts/ReferenceMain.py +++ b/ghidra_scripts/ReferenceMain.py @@ -13,17 +13,15 @@ refs = currentProgram.referenceManager.getReferencesTo(currentAddress) for ref in refs: - instruction = api.getInstructionAt(ref.getFromAddress()) + data = api.getDataAt(ref.getFromAddress()) - if not instruction: + if not data: continue - operand = ref.getOperandIndex() address = addFact.getAddress(ref.getToAddress().toString("ram:")) refType = ref.getReferenceType() + newRef = api.createMemoryReference(data, address, refType) - newRef = api.createMemoryReference(instruction, operand, address, refType) api.setReferencePrimary(newRef) - api.removeReference(ref) From 012d3c372cf62677a75c283e21ac3357454961b5 Mon Sep 17 00:00:00 2001 From: markisha64 Date: Thu, 27 Jul 2023 22:13:44 +0200 Subject: [PATCH 3/3] refact: made it so it moves both data and instruction refs --- ghidra_scripts/ReferenceMain.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ghidra_scripts/ReferenceMain.py b/ghidra_scripts/ReferenceMain.py index c89e94d..674c435 100644 --- a/ghidra_scripts/ReferenceMain.py +++ b/ghidra_scripts/ReferenceMain.py @@ -14,14 +14,19 @@ for ref in refs: data = api.getDataAt(ref.getFromAddress()) - - if not data: - continue + instruction = api.getInstructionAt(ref.getFromAddress()) address = addFact.getAddress(ref.getToAddress().toString("ram:")) refType = ref.getReferenceType() - newRef = api.createMemoryReference(data, address, refType) - api.setReferencePrimary(newRef) + if data: + newRef = api.createMemoryReference(data, address, refType) + api.setReferencePrimary(newRef) + + if instruction: + operand = ref.getOperandIndex() + newRef = api.createMemoryReference(instruction, operand, address, refType) + api.setReferencePrimary(newRef) + api.removeReference(ref)