def bapida(idapatch, popenpatch, monkeypatch, idadir): from bap.utils import config ida = Ida() bap = Bap(BAP_PATH) def run_bap(args): if args[0] == BAP_PATH: res = bap.call(args) or 0 if isinstance(res, int): return 'exit ' + str(res) else: return str(res) config.set('bap_executable_path', bap.path) monkeypatch.setattr('os.access', lambda p, x: p == BAP_PATH) idapatch({ 'register_timer': ida.register_timer, 'get_input_ida_file_path': lambda: 'true' }) idapatch(ns='idc', attrs={ 'Message': ida.message, 'Warning': ida.warning, 'SetStatus': ida.set_status, }) popenpatch(run_bap) monkeypatch.setattr('bap.utils.ida.output_symbols', lambda out: None) return (bap, ida)
def test_set_and_get(idadir): from bap.utils.config import get, set, is_set for path in ('foo', 'foo.bar'): assert get(path) is None set(path, 'hello') assert get(path) == 'hello' assert not is_set(path) set(path, 'true') assert is_set(path)
def config_path(): if config.get('bap_executable_path') is not None: return default_bap_path = '' from subprocess import check_output, CalledProcessError import os try: default_bap_path = check_output(['which', 'bap']).strip() except (OSError, CalledProcessError) as e: # Cannot run 'which' command OR # 'which' could not find 'bap' try: default_bap_path = os.path.join( check_output(['opam', 'config', 'var', 'bap:bin']).strip(), 'bap' ) except OSError: # Cannot run 'opam' pass if not default_bap_path.endswith('bap'): default_bap_path = '' def confirm(msg): from idaapi import askyn_c, ASKBTN_CANCEL, ASKBTN_YES return askyn_c(ASKBTN_CANCEL, msg) == ASKBTN_YES while True: bap_path = idaapi.askfile_c(False, default_bap_path, 'Path to bap') if bap_path is None: if confirm('Are you sure you don\'t want to set path?'): return else: continue if not bap_path.endswith('bap'): if not confirm("Path does not end with bap. Confirm?"): continue if not os.path.isfile(bap_path): if not confirm("Path does not point to a file. Confirm?"): continue break config.set('bap_executable_path', bap_path)
def config_path(): if config.get('bap_executable_path') is not None: return default_bap_path = '' from subprocess import check_output, CalledProcessError import os try: default_bap_path = check_output(['which', 'bap']).strip() except (OSError, CalledProcessError) as e: # Cannot run 'which' command OR # 'which' could not find 'bap' try: default_bap_path = os.path.join( check_output(['opam', 'config', 'var', 'bap:bin']).strip(), 'bap') except OSError: # Cannot run 'opam' pass if not default_bap_path.endswith('bap'): default_bap_path = '' def confirm(msg): from idaapi import askyn_c, ASKBTN_CANCEL, ASKBTN_YES return askyn_c(ASKBTN_CANCEL, msg) == ASKBTN_YES while True: bap_path = idaapi.askfile_c(False, default_bap_path, 'Path to bap') if bap_path is None: if confirm('Are you sure you don\'t want to set path?'): return else: continue if not bap_path.endswith('bap'): if not confirm("Path does not end with bap. Confirm?"): continue if not os.path.isfile(bap_path): if not confirm("Path does not point to a file. Confirm?"): continue break config.set('bap_executable_path', bap_path)
def check_and_configure_bap(): """Ensures that bap location is known.""" if not config.get('bap_executable_path'): path = ask_user(bap.find()) if path and len(path) > 0: config.set('bap_executable_path', path)
def config_bap_api(): if config.get('enabled', section='bap_api') is None: config.set('enabled', '1', section='bap_api')