示例#1
0
def main(args):
    """Run the manual anchors script.

    Args:
        args (list): list of command line arguments
    """
    global disas_cmd

    # argument parser
    parser = argparse.ArgumentParser(description='Enables the user to manually defined matches, acting as manual anchors, later to be used by %s\'s Matcher.' % (LIBRARY_NAME))
    parser.add_argument('bin', metavar='bin', type=str,
                        help='path to the disassembler\'s database for the wanted binary')
    parser.add_argument('name', metavar='lib-name', type=str,
                        help='name (case sensitive) of the relevant open source library')
    parser.add_argument('version', metavar='lib-version', type=str,
                        help='version string (case sensitive) as used by the identifier')
    parser.add_argument('config', metavar='configs', type=str,
                        help='path to the *.json "configs" directory')
    parser.add_argument('-D', '--debug', action='store_true', help='set logging level to logging.DEBUG')
    parser.add_argument('-W', '--windows', action='store_true', help='signals that the binary was compiled for Windows')

    # parse the args
    args = parser.parse_args(args)
    library_name    = args.name
    library_version = args.version
    bin_path        = args.bin
    config_path     = args.config
    is_debug        = args.debug
    is_windows      = args.windows

    # open the log
    prompter = Prompter(min_log_level=logging.INFO if not is_debug else logging.DEBUG)
    prompter.info('Starting the Script')

    # use the user supplied flag
    if is_windows:
        setWindowsMode()

    # always init the utils before we start
    initUtils(prompter, None, invoked_before=True)
    # register our contexts
    registerContexts(SourceContext, BinaryContext, IslandContext)

    # Load the information from the relevant library
    lib_config_file = constructConfigPath(library_name, library_version)
    prompter.debug('Loading the configuration file for library: %s', library_name)
    prompter.addIndent()
    cur_config_path = os.path.join(config_path, lib_config_file)
    if not os.path.exists(cur_config_path):
        prompter.error('Missing configuration file (%s) for \"%s\" Version: \"%s\"', lib_config_file, library_name, library_version)
        return
    # Load the configuration file
    fd = open(cur_config_path, 'r')
    library_config = json.load(fd, object_pairs_hook=collections.OrderedDict)
    fd.close()
    prompter.removeIndent()

    # Load the existing knowledge config, if exists
    prompter.debug('Opening knowledge configuration file from path: %s', accumulatedKnowledgePath(bin_path))
    prompter.addIndent()
    knowledge_config = loadKnowledge(bin_path)
    if knowledge_config is None:
        prompter.debug('Failed to find an existing configuration file')
        knowledge_config = {}
    prompter.removeIndent()

    # receive all of the couples from the user
    knowledge_config = recordManualAnchors(library_config, knowledge_config, library_name, prompter)
    prompter.info('Storing the data to the knowledge configuration file')
    storeKnowledge(knowledge_config, bin_path)

    # finished
    prompter.info('Finished Successfully')
示例#2
0
def main(args):
    """Create a .json configuration for the open source library version.

    Args:
        args (list): list of command line arguments
    """
    global disas_cmd

    # argument parser
    parser = argparse.ArgumentParser(
        description=
        'Compiles a *.json configuration file for a specific version of an open source library, later to be used by %s\'s Matcher.'
        % (LIBRARY_NAME))
    parser.add_argument(
        'name',
        metavar='lib-name',
        type=str,
        help='name (case sensitive) of the open source library')
    parser.add_argument(
        'version',
        metavar='lib-version',
        type=str,
        help='version string (case sensitive) as used by the identifier')
    parser.add_argument(
        'couples',
        metavar='dir archive',
        type=str,
        nargs='+',
        help=
        'directory with the compiled *.o / *.obj files + path to the matching *.a / *.lib file (if didn\'t use "--no-archive")'
    )
    parser.add_argument('-D',
                        '--debug',
                        action='store_true',
                        help='set logging level to logging.DEBUG')
    parser.add_argument(
        '-N',
        '--no-archive',
        action='store_false',
        help='extract data from all *.o / *.obj files in the directory')
    parser.add_argument(
        '-W',
        '--windows',
        action='store_true',
        help='signals that the binary was compiled for Windows')

    # parse the args
    args = parser.parse_args(args)
    library_name = args.name
    library_version = args.version
    is_debug = args.debug
    is_windows = args.windows
    using_archives = args.no_archive
    couples = args.couples

    bin_dirs = []
    archive_paths = []
    if using_archives:
        if len(couples) % 2 != 0:
            parser.error(
                "Odd length in list of dir,archive couples, should be: [(directory, archive name), ...]"
            )
        for i in xrange(0, len(couples), 2):
            bin_dirs.append(couples[i])
            archive_paths.append(couples[i + 1])
    else:
        bin_dirs = couples

    # open the log
    prompter = Prompter(
        min_log_level=logging.INFO if not is_debug else logging.DEBUG)
    prompter.info('Starting the Script')

    # requesting the path to the chosen disassembler
    setDisassemblerPath(prompter)
    disas_cmd = identifyDisassemblerHandler(getDisasPath(), prompter)
    if disas_cmd is None:
        return

    # register our contexts
    registerContexts(SourceContext, BinaryContext, IslandContext)

    # use the user supplied flag
    if is_windows:
        setWindowsMode()

    # Check if launched from the src directory
    if not os.path.exists(SCRIPT_PATH):
        prompter.error(
            'The script should be executed from Karta\'s src directory!')
        prompter.error('Exiting')
        return

    # analyze the open source library
    analyzeLibrary(constructConfigPath(library_name, library_version),
                   bin_dirs, archive_paths, prompter)

    # finished
    prompter.info('Finished Successfully')