Created
November 8, 2024 10:27
-
-
Save ndeadly/2047211ebb3c034df2d414f00f31b912 to your computer and use it in GitHub Desktop.
IDA script to automate locating nn::diag::detail::VAbortImpl and nn::detail::UnexpectedDefaultImpl functions and marking them __noreturn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ida_funcs | |
import ida_search | |
import ida_auto | |
import idautils | |
# Find the bounds of the program | |
segments = list(idautils.Segments()) | |
MODULE_START_ADDR = min(segments) | |
MODULE_END_ADDR = get_segm_end(max(segments)) | |
def get_calling_function_address(address): | |
calling_func = ida_funcs.get_func(address) | |
return calling_func.start_ea | |
def find_svc_function(svc_id): | |
# Find usages svc instruction with matching id | |
search_addr = None | |
ea_addr = MODULE_START_ADDR | |
while True: | |
ea_addr, operand = ida_search.find_imm(ea_addr, ida_search.SEARCH_DOWN, svc_id) | |
# Terminate the loop if nothing found | |
if ea_addr == BADADDR: | |
break | |
if print_insn_mnem(ea_addr) == 'SVC': | |
search_addr = ea_addr | |
break; | |
# Return None if svc instruction not found | |
if not search_addr: | |
return None | |
# Return address of the function using the svc instruction | |
return get_calling_function_address(search_addr) | |
def mark_function_noreturn(address): | |
f = ida_funcs.get_func(address) | |
f.flags |= ida_funcs.FUNC_NORET | |
ida_funcs.update_func(f) | |
# locate svc::Break function | |
svc_break_addr = find_svc_function(0x26) | |
# Locate nn::diag::detail::Abort function. Should be the last xref to svc::Break | |
xrefs = list(idautils.CodeRefsTo(svc_break_addr, True)) | |
abort_addr = get_calling_function_address(xrefs[-1]) | |
# locate nn::diag::detail::VAbortImpl and nn::detail::UnexpectedDefaultImpl by xreffing nn::diag::detail::Abort | |
xrefs = list(idautils.CodeRefsTo(abort_addr, True)) | |
vabortimpl_addr = get_calling_function_address(xrefs[0]) | |
unexpecteddefaultimpl_addr = get_calling_function_address(xrefs[-1]) | |
# Mark the functions we located above as __noreturn | |
mark_function_noreturn(vabortimpl_addr) | |
mark_function_noreturn(unexpecteddefaultimpl_addr) | |
# Reanalyse the program | |
ida_auto.auto_mark_range(MODULE_START_ADDR, MODULE_END_ADDR, ida_auto.AU_USED) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment