def _get_basic_blocks(self):
"""
Extract basic block information from the target binary using S2E's IDA
Pro script.
This extraction is done within a temporary directory so that we don't
pollute the file system with temporary idbs and other such things.
"""
logger.info('Generating basic block information from IDA Pro')
try:
with TemporaryDirectory() as temp_dir:
target_path = self._project_desc['target_path']
# Copy the binary to the temporary directory. Because projects
# are created with a symlink to the target program, then IDA
# Pro will generate the idb and bblist files in the symlinked
# target's directory. Which is not what we want
target_name = os.path.basename(target_path)
temp_target_path = os.path.join(temp_dir, target_name)
shutil.copyfile(target_path, temp_target_path)
# Run the IDA Pro extractBasicBlocks script
env_vars = os.environ.copy()
env_vars['TVHEADLESS'] = '1'
# This is required if s2e-env runs inside screen
env_vars['TERM'] = 'xterm'
ida = sh.Command(self._ida_path)
ida('-A', '-B',
'-S%s' % self.install_path('bin', 'extractBasicBlocks.py'),
temp_target_path, _out=os.devnull, _tty_out=False,
_cwd=temp_dir, _env=env_vars)
# Check that the basic block list file was correctly generated
bblist_file = os.path.join(temp_dir, '%s.bblist' % target_name)
if not os.path.isfile(bblist_file):
raise CommandError('Failed to generate bblist file for '
'%s' % target_name)
# Parse the basic block list file
#
# to_basic_block takes a 3-tuple read from the bblist file and
# converts it to a BasicBlock
to_basic_block = lambda tup: BasicBlock(int(tup[0], 16),
int(tup[1], 16),
tup[2])
with open(bblist_file, 'r') as f:
return [to_basic_block(l.rstrip().split(' ')) for l in f]
except ErrorReturnCode as e:
raise CommandError(e)
评论列表
文章目录