Script: Auto-detect available script targets (GetAvailableTargets)

This commit is contained in:
Katy Coe
2020-08-06 05:15:43 +02:00
parent 15fb96ba9a
commit 25d5a68755
3 changed files with 13 additions and 5 deletions

View File

@@ -0,0 +1,58 @@
# Ghidra-specific implementation
from ghidra.app.cmd.function import ApplyFunctionSignatureCmd
from ghidra.app.script import GhidraScriptUtil
from ghidra.app.util.cparser.C import CParserUtils
from ghidra.program.model.symbol import SourceType
def SetName(addr, name):
createLabel(toAddr(addr), name, True)
def MakeFunction(start, name=None):
addr = toAddr(start)
# Don't override existing functions
fn = getFunctionAt(addr)
if fn is not None and name is not None:
# Set existing function name if name available
fn.setName(name, SourceType.USER_DEFINED)
elif fn is None:
# Create new function if none exists
createFunction(addr, name)
# Set header comment if name available
if name is not None:
setPlateComment(addr, name)
def DefineCode(code):
# Code declarations are not supported in Ghidra
# This only affects string literals for metadata version < 19
# TODO: Replace with creating a DataType for enums
pass
def SetFunctionType(addr, sig):
MakeFunction(addr)
typeSig = CParserUtils.parseSignature(None, currentProgram, sig)
ApplyFunctionSignatureCmd(toAddr(addr), typeSig, SourceType.USER_DEFINED, False, True).applyTo(currentProgram)
def SetType(addr, type):
if type.startswith('struct '):
type = type[7:]
t = getDataTypes(type)[0]
addr = toAddr(addr)
removeDataAt(addr)
createData(addr, t)
def SetComment(addr, text):
setEOLComment(toAddr(addr), text)
def SetHeaderComment(addr, text):
setPlateComment(toAddr(addr), text)
def CustomInitializer():
# Ghidra sets the image base for ELF to 0x100000 for some reason
# https://github.com/NationalSecurityAgency/ghidra/issues/1020
if currentProgram.getExecutableFormat().endswith('(ELF)'):
currentProgram.setImageBase(toAddr(0), True)
def GetScriptDirectory():
# Ghidra doesn't define __file__ so we have to iterate all the scripts
return next(iter(filter(lambda x: x.getName() == '%SCRIPTFILENAME%', GhidraScriptUtil.getAllScripts())), None).getParentFile().toString()

View File

@@ -0,0 +1,39 @@
# IDA-specific implementation
import idaapi
def SetName(addr, name):
ret = idc.set_name(addr, name, SN_NOWARN | SN_NOCHECK)
if ret == 0:
new_name = name + '_' + str(addr)
ret = idc.set_name(addr, new_name, SN_NOWARN | SN_NOCHECK)
def MakeFunction(start):
ida_funcs.add_func(start)
def DefineCode(code):
idc.parse_decls(code)
def SetFunctionType(addr, sig):
SetType(addr, sig)
def SetType(addr, type):
ret = idc.SetType(addr, type)
if ret is None:
print('SetType(0x%x, %r) failed!' % (addr, type))
def SetComment(addr, text):
idc.set_cmt(addr, text, 1)
def SetHeaderComment(addr, text):
SetComment(addr, text)
def CustomInitializer():
print('Processing Types')
original_macros = ida_typeinf.get_c_macros()
ida_typeinf.set_c_macros(original_macros + ";_IDA_=1")
idc.parse_decls("%TYPE_HEADER_RELATIVE_PATH%", idc.PT_FILE)
ida_typeinf.set_c_macros(original_macros)
def GetScriptDirectory():
return os.path.dirname(os.path.realpath(__file__))