Skip to content

Instantly share code, notes, and snippets.

@52617365
Created August 11, 2024 11:39
Show Gist options
  • Save 52617365/435d0e0f58acc1915009370f006e55a6 to your computer and use it in GitHub Desktop.
Save 52617365/435d0e0f58acc1915009370f006e55a6 to your computer and use it in GitHub Desktop.
This script gets all the stub functions from the __objc_stubs section and renames them depending on the function it's calling
# Example stub caller function that this binary ninja script renames would be like this:
# 1001dc540 int64_t sub_8282828818(void* arg1)
# 1001dc550 return _objc_msgSend(self: arg1, cmd: "_finalRestoreBlock") __tailcall
# Result will be:
# 1001dc540 int64_t objc_stub_caller__finalRestoreBlock(void* arg1)
# 1001dc550 return _objc_msgSend(self: arg1, cmd: "_finalRestoreBlock") __tailcall
from binaryninja import *
objc_stub_section = bv.get_section_by_name("__objc_stubs")
objc_stub_section_start = objc_stub_section.start
objc_stub_section_end = objc_stub_section.end
all_functions = bv.functions
functions_in_objc_stub_section = []
for f in all_functions:
if f.start > objc_stub_section_start and f.start < objc_stub_section_end:
functions_in_objc_stub_section.append(f)
for f in functions_in_objc_stub_section:
insts = list(f.high_level_il.instructions)
if len(insts) == 1:
tokens = insts[0].tokens
rename_function_name = f"objc_stub_caller_{tokens[6]}"
f.name = rename_function_name
@52617365
Copy link
Author

52617365 commented Aug 11, 2024

tokens[6] is not robust and it just happens to be the second argument passed to the __objc_msgSend function with the ones i wanted to rename.
This was a one-off so I didn't care to make it nicer. That being said the index should always be available if the function is directly returning the __objc_msgSend function call result (and all the method stubs in the section were.)

EDIT: the index has to be changed to something more robust like some binja api function that gets the second argument because the following leads to the wrong index.
return _objc_msgSend(self: *(arg1 + 0x20), cmd: "notifyDestruction") __tailcall

I think the following would fix this?

hlil_params = insts[0].params
second_arg = hlil_params[2]

@52617365
Copy link
Author

If someone knows a better way to get the second argument in binja please tell me since I need it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment