Ejemplo n.º 1
0
def start_scan_processes(options):
    """
    preparing for attacks and managing multi-processing for host

    Args:
        options: all options

    Returns:
        True when it ends
    """
    scan_unique_id = generate_random_token(32)
    # find total number of targets + types + expand (subdomain, IPRanges, etc)
    # optimize CPU usage
    info(messages("regrouping_targets"))
    options.targets = expand_targets(options, scan_unique_id)
    number_of_total_targets = len(options.targets)
    options.targets = [
        targets.tolist() for targets in numpy.array_split(
            options.targets, options.set_hardware_usage
            if options.set_hardware_usage <= len(options.targets) else len(
                options.targets))
    ]
    info(messages("removing_old_db_records"))
    from database.db import remove_old_logs
    for target_group in options.targets:
        for target in target_group:
            for module_name in options.selected_modules:
                remove_old_logs({
                    "target": target,
                    "module_name": module_name,
                    "scan_unique_id": scan_unique_id,
                })
    for _ in range(options.targets.count([])):
        options.targets.remove([])
    active_processes = []
    info(
        messages("start_multi_process").format(number_of_total_targets,
                                               len(options.targets)))
    process_number = 0
    for targets in options.targets:
        process_number += 1
        process = multiprocessing.Process(target=parallel_scan_process,
                                          args=(
                                              options,
                                              targets,
                                              scan_unique_id,
                                              process_number,
                                          ))
        process.start()
        active_processes.append(process)
    exit_code = wait_for_threads_to_finish(active_processes, sub_process=True)
    create_report(options, scan_unique_id)
    return exit_code
Ejemplo n.º 2
0
def sort_logs(log_in_file, language, graph_flag, scan_id, scan_cmd, verbose_level, api_flag, profile, scan_method,
              ports):
    """
    sort all events, create log file in HTML/TEXT/JSON and remove old logs

    Args:
        log_in_file: output filename
        language: language
        graph_flag: graph name
        scan_id: scan hash id
        scan_cmd: scan cmd
        verbose_level: verbose level number
        api_flag: API flag
        profile: profiles
        scan_method: module names
        ports: ports

    Returns:
        True if success otherwise None
    """
    _HOST = messages(language, "HOST")
    _USERNAME = messages(language, "USERNAME")
    _PASSWORD = messages(language, "PASSWORD")
    _PORT = messages(language, "PORT")
    _TYPE = messages(language, "TYPE")
    _DESCRIPTION = messages(language, "DESCRIPTION")
    _TIME = messages(language, "TIME")
    events_num = 0
    report_type = ""
    JSON_FROM_DB = __logs_by_scan_id(scan_id, language)
    JSON_Data = sorted(JSON_FROM_DB, key=sorted)
    if compatible.version() is 2:
        import sys
        reload(sys)
        sys.setdefaultencoding('utf8')
    if (len(log_in_file) >= 5 and log_in_file[-5:] == '.html') or (
            len(log_in_file) >= 4 and log_in_file[-4:] == '.htm'):
        report_type = "HTML"
        data = sorted(JSON_FROM_DB, key=lambda x: sorted(x.keys()))
        # if user want a graph
        _graph = ''
        if graph_flag is not None:
            _graph = build_graph(graph_flag, language, data, 'HOST', 'USERNAME', 'PASSWORD', 'PORT', 'TYPE',
                                 'DESCRIPTION')
        from lib.html_log import _log_data
        _css = _log_data.css_1
        _table = _log_data.table_title.format(_graph, _css, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE, _DESCRIPTION,
                                              _TIME)

        for value in data:
            _table += _log_data.table_items.format(value['HOST'], value['USERNAME'], value['PASSWORD'],
                                                   value['PORT'], value['TYPE'], value['DESCRIPTION'], value['TIME'])
            events_num += 1
        _table += _log_data.table_end + '<p class="footer">' + messages(language, "nettacker_version_details") \
            .format(compatible.__version__, compatible.__code_name__, now()) + '</p>'
        __log_into_file(log_in_file, 'w' if type(_table) ==
                                            str else 'wb', _table, language, final=True)
    elif len(log_in_file) >= 5 and log_in_file[-5:] == '.json':
        graph_flag = ""
        report_type = "JSON"
        data = json.dumps(JSON_Data)
        events_num = len(JSON_Data)
        __log_into_file(log_in_file, 'w', data, language, final=True)
    else:
        graph_flag = ""
        report_type = "TEXT"
        data, events_num = __build_texttable(JSON_FROM_DB, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE,
                                             _DESCRIPTION, _TIME, language)
        __log_into_file(log_in_file, 'wb', data, language, final=True)
    data = data if report_type == "TEXT" else __build_texttable(JSON_FROM_DB, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE,
                                                                _DESCRIPTION, _TIME, language)[0]
    info(messages(language, "updating_database"))
    category = []
    for sm in scan_method:
        if sm.rsplit("_")[-1] not in category:
            category.append(sm.rsplit("_")[-1])
    category = ",".join(list(set(category)))
    scan_method = ",".join(scan_method)
    if ports is None:
        ports = "default"
    submit_report_to_db(now(), scan_id, log_in_file, events_num, 0 if verbose_level is 0 else 1, api_flag, report_type,
                        graph_flag, category, profile, scan_method, language, scan_cmd, ports)
    info(messages(language, "removing_logs_db"))
    hosts = []
    for log in JSON_Data:
        if log["HOST"] not in hosts:
            hosts.append(log["HOST"])
    for host in hosts:
        for sm in scan_method.rsplit(','):
            remove_old_logs(host, sm, scan_id, language)
    # info(messages(language,"inserting_logs_db"))
    # for log in JSON_Data:
    #     submit_logs_to_db(language, log)
    if events_num:
        info(messages(language, "summary_report"))
        write(data)
    else:
        info(messages(language, "no_event_found"))
    info(messages(language, "file_saved").format(log_in_file))
    return True