本文整理汇总了Python中elftools.elf.sections.SymbolTableSection方法的典型用法代码示例。如果您正苦于以下问题:Python sections.SymbolTableSection方法的具体用法?Python sections.SymbolTableSection怎么用?Python sections.SymbolTableSection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elftools.elf.sections
的用法示例。
在下文中一共展示了sections.SymbolTableSection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: symbols
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def symbols(self):
if not self.__check_session():
return
rows = []
for section in self.elf.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
for cnt, symbol in enumerate(section.iter_symbols()):
rows.append([
cnt,
hex(symbol['st_value']),
hex(symbol['st_size']),
describe_symbol_type(symbol['st_info']['type']),
symbol.name
])
self.log('info', "ELF Symbols:")
self.log('table', dict(header=['Num', 'Value', 'Size', 'Type', 'Name'], rows=rows))
示例2: process_binary
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def process_binary(bv, binfile):
with open(binfile, 'rb') as f:
elffile = ELFFile(f)
symbol_tables = [s for s in elffile.iter_sections() if isinstance(s, SymbolTableSection)]
for section in symbol_tables:
if not isinstance(section, SymbolTableSection):
continue
if section['sh_entsize'] == 0:
continue
for nsym, symbol in enumerate(section.iter_symbols()):
sym_addr = symbol['st_value']
sym_size = symbol['st_size']
if is_data_variable_section(bv, sym_addr):
dynamic_symbols[sym_addr] = sym_size
示例3: get_section_names
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def get_section_names(elffile):
section_names = []
symbol_names = []
for section in elffile.iter_sections():
# Get names of all sections in ELF file
if len(section.name) > 0:
section_names.append(section.name)
# If symbol tables exist for the section, take inventory
if isinstance(section, SymbolTableSection):
for i in range(0, section.num_symbols()):
if len(section.get_symbol(i).name) > 0:
symbol_names.append(section.get_symbol(i).name)
return section_names, symbol_names
示例4: get_func_address
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def get_func_address(self, name):
address = None
with open(self.path, 'rb') as stream:
elf = ELFFile(stream)
for section in elf.iter_sections():
if isinstance(section, SymbolTableSection):
for symbol in section.iter_symbols():
if symbol.name == name:
self.logger.debug('%s', symbol.entry)
address = symbol.entry['st_value']
break
if address:
break
else:
raise RuntimeError('Failed to find {}'.format(name))
return self.handle_address(address + self.libc.imageBase)
示例5: binary_symbols
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def binary_symbols(binary):
"""
helper method for getting all binary symbols with SANDSHREW_ prepended.
We do this in order to provide the symbols Manticore should hook on to
perform main analysis.
:param binary: str for binary to instrospect.
:rtype list: list of symbols from binary
"""
def substr_after(string, delim):
return string.partition(delim)[2]
with open(binary, "rb") as f:
elffile = ELFFile(f)
for section in elffile.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
symbols = [sym.name for sym in section.iter_symbols() if sym]
return [
substr_after(name, PREPEND_SYM) for name in symbols if name.startswith(PREPEND_SYM)
]
示例6: do_symbols
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def do_symbols(self, *args):
"""
:= symbols
"""
# Locals
elf = None
try:
if self.target_library:
# Create a new ELFFile() instance
elf = ELFFile(self.target_library[0])
for section in elf.iter_sections():
# Once we find the symbol table, print each symbol
if isinstance(section, SymbolTableSection):
self.logger.binja_log("info", "Found symbol table (!)")
for i, symbol in enumerate(section.iter_symbols()):
self.logger.binja_log("info", symbol.name)
else:
self.logger.binja_log("info", "Target library not selected (!)")
except Exception as e:
BinjaError("function : {}".format(e))
示例7: scan_section
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def scan_section(main, features, feat_type, lib_path, section):
"""
Function to extract dynamic names from a shared library file.
"""
try:
from elftools.elf.sections import SymbolTableSection
if not section or not isinstance(section, SymbolTableSection) or section['sh_entsize'] == 0:
return 0
logger.info("symbol table '%s' contains %s entries", section.name, section.num_symbols())
count = 0
for nsym, symbol in enumerate(section.iter_symbols()):
# set the type checks
if feat_type == 'function':
FEATURE_TYPE = 'STT_FUNC'
elif feat_type == 'variable':
FEATURE_TYPE = 'STT_OBJECT'
else:
raise Exception("Unexpected feat_type: " + feat_type)
# check for types and update features
if symbol['st_info']['type'] == FEATURE_TYPE and symbol['st_shndx'] != 'SHN_UNDEF':
# bookeeping function/variable names
func = symbol.name
if not func in features:
features[func] = 1
else:
features[func] += 1
count += 1
if not main.TEST_LIB and stats_logger:
stats_logger.debug("%s, %ss, %s, %s", os.path.basename(lib_path), feat_type, len(features),
len(set(features)))
return count
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.error("[%s, %s, %s] Error extracting %ss: %s", exc_type, fname, exc_tb.tb_lineno, feat_type, str(e))
示例8: get_objects
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def get_objects(main, lib_path):
"""
Function to extract global/external objs from a shared library file.
"""
try:
from elftools.elf.elffile import ELFFile
from elftools.common.exceptions import ELFError
from elftools.elf.sections import SymbolTableSection
with open(lib_path, 'rb') as stream:
elffile = ELFFile(stream)
for section in elffile.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
if section['sh_entsize'] == 0:
continue
logger.info("symbol table '%s' contains %s entries", section.name, section.num_symbols())
for nsym, symbol in enumerate(section.iter_symbols()):
if symbol['st_info']['type'] == 'STT_OBJECT' and symbol['st_shndx'] != 'SHN_UNDEF':
yield symbol.name
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logger.error("[%s, %s, %s] Error extracting objects: %s", exc_type, fname, exc_tb.tb_lineno, str(e))
示例9: scan_section
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def scan_section(functions, lib_path, section):
"""
Function to extract function names from a shared library file.
"""
try:
from elftools.elf.sections import SymbolTableSection
if not section or not isinstance(section, SymbolTableSection) or section['sh_entsize'] == 0:
return 0
print "symbol table '%s' contains %s entries" % (section.name, section.num_symbols())
count = 0
for nsym, symbol in enumerate(section.iter_symbols()):
if symbol['st_info']['type'] == 'STT_FUNC' and symbol['st_shndx'] != 'SHN_UNDEF':
# bookeeping function names
func = symbol.name
if not func in functions:
functions[func] = 1
else:
functions[func] += 1
count += 1
return count
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print "[%s, %s, %s] Error extracting functions: %s" % (exc_type, fname, exc_tb.tb_lineno, str(e))
return 0
示例10: scan_section
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def scan_section(functions, elffile, lib_path, section):
try:
from elftools.elf.sections import SymbolTableSection
from elftools.common.exceptions import ELFError
if not section or not isinstance(section, SymbolTableSection) or section['sh_entsize'] == 0:
return 0
print "symbol table '%s' contains %s entries" % (section.name, section.num_symbols())
count = 0
for nsym, symbol in enumerate(section.iter_symbols()):
# iterate over all functions
if (symbol['st_info']['type'] == 'STT_FUNC' and symbol['st_shndx'] != 'SHN_UNDEF' and
symbol.name not in blacklisted_functions and
not symbol.name.startswith(('__gnu_Unwind_', '___Unwind_', '__aeabi_', '__gnu_unwind_', '_Unwind_',
'__aeabi_unwind_'))):
# functions from .text section
section = elffile.get_section(symbol['st_shndx'])
if section.name != '.text':
continue
# bookeeping function names
func = symbol.name
if not func in functions:
functions[func] = symbol['st_value']
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print "[%s, %s, %s] Error extracting functions: %s" % (exc_type, fname, exc_tb.tb_lineno, str(e))
示例11: load_symbols_elf
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def load_symbols_elf(filename):
""" Load the symbol tables contained in the file
"""
f = open(filename, 'rb')
elffile = ELFFile(f)
symbols = []
for section in elffile.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
if section['sh_entsize'] == 0:
logger.warn("Symbol table {} has a sh_entsize of zero.".format(section.name))
continue
logger.info("Symbol table {} contains {} entries.".format(section.name, section.num_symbols()))
for _, symbol in enumerate(section.iter_symbols()):
if describe_symbol_shndx(symbol['st_shndx']) != "UND" and \
describe_symbol_type(symbol['st_info']['type']) == "FUNC":
symbols.append((symbol['st_value'], symbol['st_size'], symbol.name))
f.close()
symbols_by_addr = {
addr: (name, size, True) for addr, size, name in symbols
}
return symbols_by_addr
示例12: _find_symbol
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def _find_symbol(self, name: str):
symbol_tables = (s for s in self.elf.iter_sections() if isinstance(s, SymbolTableSection))
for section in symbol_tables:
if section["sh_entsize"] == 0:
continue
for symbol in section.iter_symbols():
if describe_symbol_type(symbol["st_info"]["type"]) == "FUNC":
if symbol.name == name:
return symbol["st_value"]
return None
示例13: resolve
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def resolve(self, symbol):
"""
A helper method used to resolve a symbol name into a memory address when
injecting hooks for analysis.
:param symbol: function name to be resolved
:type symbol: string
:param line: if more functions present, optional line number can be included
:type line: int or None
"""
with open(self.binary_path, "rb") as f:
elffile = ELFFile(f)
# iterate over sections and identify symbol table section
for section in elffile.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
# get list of symbols by name
symbols = section.get_symbol_by_name(symbol)
if not symbols:
continue
# return first indexed memory address for the symbol,
return symbols[0].entry["st_value"]
raise ValueError(f"The {self.binary_path} ELFfile does not contain symbol {symbol}")
示例14: load_section_info
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def load_section_info(self, sections):
symtab = dict()
thumbtab = list()
code_addrs = []
for section in sections:
if isinstance(section, SymbolTableSection):
syms, thumbs = self.load_symbol_table(section.iter_symbols())
symtab.update(syms)
thumbtab.extend(thumbs)
elif section['sh_flags'] == 6: # Assumption: Code section's flag is AX (ALLOC=2 & EXEC=4)
code_addrs.append({'address': section['sh_addr'], 'size': section['sh_size']})
return symtab, thumbtab, code_addrs
示例15: flist_from_symtab
# 需要导入模块: from elftools.elf import sections [as 别名]
# 或者: from elftools.elf.sections import SymbolTableSection [as 别名]
def flist_from_symtab(self):
symbol_tables = [
sec for sec in self.elffile.iter_sections()
if isinstance(sec, SymbolTableSection)
]
function_list = dict()
for section in symbol_tables:
if not isinstance(section, SymbolTableSection):
continue
if section['sh_entsize'] == 0:
continue
for symbol in section.iter_symbols():
if symbol['st_other']['visibility'] == "STV_HIDDEN":
continue
if (symbol['st_info']['type'] == 'STT_FUNC'
and symbol['st_shndx'] != 'SHN_UNDEF'):
function_list[symbol['st_value']] = {
'name': symbol.name,
'sz': symbol['st_size'],
'visibility': symbol['st_other']['visibility'],
'bind': symbol['st_info']['bind'],
}
return function_list