Skip to content

Instantly share code, notes, and snippets.

@darkarnium
Last active September 21, 2021 18:59
Show Gist options
  • Save darkarnium/55dcfefbe409badcd70069bbe245a74e to your computer and use it in GitHub Desktop.
Save darkarnium/55dcfefbe409badcd70069bbe245a74e to your computer and use it in GitHub Desktop.
IDA - Bind a hotkey to find the next address marked as Unknown (F3)
import idaapi
def find_next_unknown():
'''
Attempts to find the next unknown section from the cursor. This will only
look a maximum of 0xFFFF bytes into the future to prevent infinite loops.
'''
s_addr = ScreenEA()
e_addr = s_addr + 0xFFFF # Maximum look ahead
c_addr = s_addr
while c_addr < e_addr:
if isUnknown(GetFlags(c_addr)):
# Skip NULLs.
if Byte(c_addr) == 0x0 and Byte(c_addr + 0x1) == 0x0:
c_addr += 0x2
continue
# Mark NOPs automatically.
if Byte(c_addr) == 0x1F and \
Byte(c_addr + 1) == 0x20 and \
Byte(c_addr + 2) == 0x03 and \
Byte(c_addr + 3) == 0xD5:
print('[+] NOP found at 0x{0:0x}, marking.'.format(c_addr))
ida_auto.auto_make_code(c_addr)
ida_auto.auto_wait()
c_addr += 0x4
continue
break
c_addr += 0x1
if c_addr < e_addr:
print('[+] Jumping to 0x{0:0x}'.format(c_addr))
Jump(c_addr)
else:
print('[!] Maximum look ahead exceeded')
# Bind to F3.
idaapi.CompileLine(
'static key_F3() { RunPythonStatement("find_next_unknown()"); }'
)
AddHotkey('F3', 'key_F3')
@darkarnium
Copy link
Author

darkarnium commented Apr 23, 2021

"""Find and jump to the next unknown."""

import ida_bytes
import ida_expr
import ida_kernwin
import idc


def find_next_unknown():
    """
    Attempts to find the next unknown section from the cursor. This will only look a
    maximum of 0xFFFF bytes into the future to prevent infinite loops.
    """
    s_addr = idc.get_screen_ea()
    e_addr = s_addr + 0xFFFF  # Maximum look ahead
    c_addr = s_addr

    while c_addr < e_addr:
        if ida_bytes.is_unknown(ida_bytes.get_full_flags(c_addr)):
            break
        c_addr += 4

    if c_addr < e_addr:
        print("[+] Jumping to 0x{0:0x}".format(c_addr))
        ida_kernwin.jumpto(c_addr)

    else:
        print("[!] Maximum look ahead exceeded")
        ida_kernwin.jumpto(c_addr)


# Bind to F3.
ida_expr.compile_idc_text(
    'static key_F3() { RunPythonStatement("find_next_unknown()"); }'
)
ida_kernwin.add_idc_hotkey("F3", "key_F3")

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