def clear_prefix(function_address):
"""
Clear the prefix from a given function.
"""
original_name = get_function_name(function_address)
#
# locate the last (rfind) prefix separator in the function name as
# we will want to keep everything that comes after it
#
i = original_name.rfind(PREFIX_SEPARATOR)
# if there is no prefix (separator), there is nothing to trim
if i == -1:
return
# trim the prefix off the original function name and discard it
new_name = original_name[i+1:]
# rename the function with the prefix stripped
idaapi.set_name(function_address, new_name, idaapi.SN_NOWARN)
python类set_name()的实例源码
def gui_rename_function(function_address):
"""
Interactive rename of a function in the IDB.
"""
original_name = get_function_name(function_address)
# prompt the user for a new function name
ok, new_name = prompt_string(
"Please enter function name",
"Rename Function",
original_name
)
#
# if the user clicked cancel, or the name they entered
# is identical to the original, there's nothing to do
#
if not (ok or new_name != original_name):
return
# rename the function
idaapi.set_name(function_address, new_name, idaapi.SN_NOCHECK)
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name({:x}, {!r}) : Stripping invalid chars from function name. : {!r}'.format(__name__, ea, string, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise ValueError("{:s}.set_name({:x}, {!r}) : Unable to apply function name.".format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name({:x}, {!r}) : Stripping invalid chars from function name. : {!r}'.format(__name__, ea, string, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise ValueError("{:s}.set_name({:x}, {!r}) : Unable to apply function name.".format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn("{:s}.set_name({:x}, {!r}) : Stripping invalid chars from function name. : {!r}".format(__name__, ea, string, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise ValueError("{:s}.set_name({:x}, {!r}) : Unable to apply function name.".format(__name__, ea, string))
return res
def set_name(func, string):
'''Set the name of the function ``func`` to ``string``.'''
rt,ea = __addressOfRtOrSt(func)
res = idaapi.validate_name2(buffer(string)[:])
if string and string != res:
logging.warn('{:s}.set_name : Stripping invalid chars from function name {!r} at {:x}. : {!r}'.format(__name__, string, ea, res))
string = res
if rt:
# FIXME: shuffle the new name into the prototype and then re-mangle it
res, ok = get_name(ea), database.set_name(ea, string)
else:
res, ok = get_name(ea), idaapi.set_name(ea, string, idaapi.SN_PUBLIC)
if not ok:
raise AssertionError('{:s}.set_name : Unable to set function name for {:x} : {!r}'.format(__name__, ea, string))
return res
def bulk_prefix():
"""
Prefix the Functions window selection with a user defined string.
"""
# NOTE / COMPAT:
# prompt the user for a prefix to apply to the selected functions
if using_ida7api:
tag = idaapi.ask_str(PREFIX_DEFAULT, 0, "Function Tag")
else:
tag = idaapi.askstr(0, PREFIX_DEFAULT, "Function Tag")
# the user closed the window... ignore
if tag == None:
return
# the user put a blank string and hit 'okay'... notify & ignore
elif tag == '':
idaapi.warning("[ERROR] Tag cannot be empty [ERROR]")
return
#
# loop through all the functions selected in the 'Functions window' and
# apply the user defined prefix tag to each one.
#
for func_name in get_selected_funcs():
# ignore functions that already have the specified prefix applied
if func_name.startswith(tag):
continue
# apply the user defined prefix to the function (rename it)
new_name = '%s%s%s' % (str(tag), PREFIX_SEPARATOR, func_name)
func_addr = idaapi.get_name_ea(idaapi.BADADDR, func_name)
idaapi.set_name(func_addr, new_name, idaapi.SN_NOWARN)
# refresh the IDA views
refresh_views()
def clear_prefix():
"""
Clear user defined prefixes from the selected functions in the Functions window.
"""
#
# loop through all the functions selected in the 'Functions window' and
# clear any user defined prefixes applied to them.
#
for func_name in get_selected_funcs():
#
# locate the last (rfind) prefix separator in the function name as
# we will want to keep everything that comes after it
#
i = func_name.rfind(PREFIX_SEPARATOR)
# if there is no prefix (separator), there is nothing to trim
if i == -1:
continue
# trim the prefix off the original function name and discard it
new_name = func_name[i+1:]
func_addr = idaapi.get_name_ea(idaapi.BADADDR, func_name)
idaapi.set_name(func_addr, new_name, idaapi.SN_NOWARN)
# refresh the IDA views
refresh_views()
#------------------------------------------------------------------------------
# IDA Util
#------------------------------------------------------------------------------
def prefix_function(function_address, prefix):
"""
Prefix a function name with the given string.
"""
original_name = get_function_name(function_address)
new_name = str(prefix) + PREFIX_SEPARATOR + str(original_name)
# rename the function with the newly prefixed name
idaapi.set_name(function_address, new_name, idaapi.SN_NOWARN)
def set_name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def set_name(name):
'''Set the name of the current function to ``name``.'''
# we use ui.current.address() instead of ui.current.function()
# in case the user might be hovering over an import table
# function and wanting to rename that instead.
return set_name(ui.current.address(), name)
def set_name(func, none):
'''Remove the custom-name from the function ``func``.'''
return set_name(func, none or '')
def name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def name(func, string, *suffix):
'''Set the name of the function ``func`` to ``string``.'''
res = (string,) + suffix
return set_name(func, interface.tuplename(*res))
def tag_write(func, key, value):
'''Set the tag ``key`` to ``value`` for the function ``func``.'''
if value is None:
raise AssertionError('{:s}.tag_write : Tried to set tag {!r} to an invalid value.'.format(__name__, key))
# Check to see if function tag is being applied to an import
try:
rt,ea = __addressOfRtOrSt(func)
except LookupError:
# If we're not even in a function, then use a database tag.
logging.warn('{:s}.tag_write : Attempted to set tag for a non-function. Falling back to a database tag. : {:x}'.format(__name__, func))
return database.tag_write(func, key, value)
# If so, then write the tag to the import
if rt:
logging.warn('{:s}.tag_write : Attempted to set tag for a runtime-linked symbol. Falling back to a database tag. : {:x}'.format(__name__, ea))
return database.tag_write(ea, key, value)
# Otherwise, it's a function.
fn = by_address(ea)
# if the user wants to change the '__name__' tag then update the function's name.
if key == '__name__':
return set_name(fn, value)
state = internal.comment.decode(comment(fn, repeatable=1))
res,state[key] = state.get(key,None),value
comment(fn, internal.comment.encode(state), repeatable=1)
if res is None:
internal.comment.globals.inc(fn.startEA, key)
return res
def tag_write(func, key, none):
'''Removes the tag identified by ``key`` from the function ``func``.'''
#fn = by(func)
# Check to see if function tag is being applied to an import
try:
rt,ea = __addressOfRtOrSt(func)
except LookupError:
# If we're not even in a function, then use a database tag.
logging.warn('{:s}.tag_write : Attempted to clear tag for a non-function. Falling back to a database tag. : {:x}'.format(__name__, func))
return database.tag_write(func, key, none)
# If so, then write the tag to the import
if rt:
logging.warn('{:s}.tag_write : Attempted to set tag for a runtime-linked symbol. Falling back to a database tag. : {:x}'.format(__name__, ea))
return database.tag_write(ea, key, none)
# Otherwise, it's a function.
fn = by_address(ea)
# if the user wants to remove the '__name__' tag then remove the name from the function.
if key == '__name__':
return set_name(fn, None)
state = internal.comment.decode(comment(fn, repeatable=1))
res = state.pop(key)
comment(fn, internal.comment.encode(state), repeatable=1)
internal.comment.globals.dec(fn.startEA, key)
return res
#FIXME: define tag_erase
def set_name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def set_name(name):
'''Set the name of the current function to ``name``.'''
# we use ui.current.address() instead of ui.current.function()
# in case the user might be hovering over an import table
# function and wanting to rename that instead.
return set_name(ui.current.address(), name)
def name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def name(func, none):
'''Remove the custom-name from the function ``func``.'''
return set_name(func, none or '')
def name(func, string, *suffix):
'''Set the name of the function ``func`` to ``string``.'''
res = (string,) + suffix
return set_name(func, interface.tuplename(*res))
def tag_write(func, key, value):
'''Set the tag ``key`` to ``value`` for the function ``func``.'''
if value is None:
raise ValueError("{:s}.tag_write({!r}) : Tried to set tag {!r} to an invalid value.".format(__name__, ea, key))
# Check to see if function tag is being applied to an import
try:
rt,ea = __addressOfRtOrSt(func)
except LookupError:
# If we're not even in a function, then use a database tag.
logging.warn('{:s}.tag_write({!r}, {!r}, ...) : Attempted to set tag for a non-function. Falling back to a database tag.'.format(__name__, func, key))
return database.tag_write(func, key, value)
# If so, then write the tag to the import
if rt:
logging.warn('{:s}.tag_write({:x}, {!r}, ...) : Attempted to set tag for a runtime-linked symbol. Falling back to a database tag.'.format(__name__, ea, key))
return database.tag_write(ea, key, value)
# Otherwise, it's a function.
fn = by_address(ea)
# if the user wants to change the '__name__' tag then update the function's name.
if key == '__name__':
return set_name(fn, value)
state = internal.comment.decode(comment(fn, repeatable=1))
res,state[key] = state.get(key,None),value
comment(fn, internal.comment.encode(state), repeatable=1)
if res is None:
internal.comment.globals.inc(fn.startEA, key)
return res
def set_name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def set_name(name):
'''Set the name of the current function to ``name``.'''
# we use ui.current.address() instead of ui.current.function()
# in case the user might be hovering over an import table
# function and wanting to rename that instead.
return set_name(ui.current.address(), name)
def set_name(func, none):
'''Remove the custom-name from the function ``func``.'''
return set_name(func, none or '')
def name(none):
'''Remove the custom-name from the current function.'''
return set_name(ui.current.function(), none or '')
def name(func, string, *suffix):
'''Set the name of the function ``func`` to ``string``.'''
res = (string,) + suffix
return set_name(func, interface.tuplename(*res))