Exemplo n.º 1
0
def __log_into_file(filename, mode, data, language, final=False):
    """
    write a content into a file (support unicode) and submit logs in database. if final=False its writing log in
    the database.

    Args:
        filename: the filename
        mode: writing mode (a, ab, w, wb, etc.)
        data: content
        language: language
        final: True if it's final report otherwise False (default False)

    Returns:
        True if success otherwise None
    """
    log = ''
    if version() is 2:
        if isinstance(data, str):
            try:
                log = json.loads(data)
            except ValueError:
                log = ''

        if isinstance(log, dict):
            if final:
                with open(filename, mode) as save:
                    save.write(data + '\n')
            else:
                submit_logs_to_db(language, data)
        else:
            if not final:
                flock = lockfile.FileLock(filename)
                flock.acquire()
            with open(filename, mode) as save:
                save.write(data + '\n')
            if not final:
                flock.release()
    else:

        if isinstance(data, str):
            try:
                log = json.loads(data)
            except ValueError:
                log = ''

        if isinstance(log, dict):
            if final:
                with open(filename, mode, encoding='utf-8') as save:
                    save.write(data + '\n')
            else:
                submit_logs_to_db(language, data)
        else:
            if not final:
                flock = lockfile.FileLock(filename)
                flock.acquire()
            with open(filename, mode, encoding='utf-8') as save:
                save.write(data + '\n')
            if not final:
                flock.release()
    return True
Exemplo n.º 2
0
def __log_into_file(filename, mode, data, language, final=False):
    """
    write a content into a file (support unicode) and submit logs in database. if final=False its writing log in
    the database.

    Args:
        filename: the filename
        mode: writing mode (a, ab, w, wb, etc.)
        data: content
        language: language
        final: True if it's final report otherwise False (default False)

    Returns:
        True if success otherwise None
    """
    if version() is 2:

        if _builder(_paths(), default_paths())["tmp_path"] in filename:
            if not final:
                flock = lockfile.FileLock(filename)
                flock.acquire()
            with open(filename, mode) as save:
                save.write(data + '\n')
            if not final:
                flock.release()
        else:
            if final:
                with open(filename, mode) as save:
                    save.write(data + '\n')
            else:
                submit_logs_to_db(language, data)

    else:

        if _builder(_paths(), default_paths())["tmp_path"] in filename:
            if not final:
                flock = lockfile.FileLock(filename)
                flock.acquire()
            with open(filename, mode, encoding='utf-8') as save:
                save.write(data + '\n')
            if not final:
                flock.release()
        else:
            if final:
                with open(filename, mode, encoding='utf-8') as save:
                    save.write(data + '\n')
            else:
                submit_logs_to_db(language, data)
    return True
Exemplo n.º 3
0
def process_conditions(
        event,
        module_name,
        target,
        scan_unique_id,
        options,
        response,
        process_number,
        module_thread_number,
        total_module_thread_number,
        request_number_counter,
        total_number_of_requests
):
    from core.alert import (success_event_info,
                            verbose_info,
                            messages)

    if 'save_to_temp_events_only' in event.get('response', ''):
        from database.db import submit_temp_logs_to_db
        submit_temp_logs_to_db(
            {
                "date": now(model=None),
                "target": target,
                "module_name": module_name,
                "scan_unique_id": scan_unique_id,
                "event_name": event['response']['save_to_temp_events_only'],
                # "options": options,
                "options": {},
                "event": event,
                "data": response
            }
        )
    if event['response']['conditions_results'] and 'save_to_temp_events_only' not in event.get('response', ''):
        from database.db import submit_logs_to_db

        # remove sensitive information before submitting to db
        from config import nettacker_api_config
        options = copy.deepcopy(options)
        for key in nettacker_api_config():
            try:
                del options[key]
            except Exception:
                continue
        del event['response']['conditions']
        event_request_keys = copy.deepcopy(event)
        del event_request_keys['response']
        submit_logs_to_db(
            {
                "date": now(model=None),
                "target": target,
                "module_name": module_name,
                "scan_unique_id": scan_unique_id,
                # "options": options,
                "options": {},
                "event": event
            }
        )
        success_event_info(
            messages("send_success_event_from_module").format(
                process_number,
                module_name,
                target,
                module_thread_number,
                total_module_thread_number,
                request_number_counter,
                total_number_of_requests,
                ", ".join(
                    [
                        "{}: {}".format(
                            key,
                            event_request_keys[key]
                        ) for key in event_request_keys
                    ]
                ),
                ", ".join(event['response']['conditions_results'].keys())
            )
        )
        verbose_info(
            json.dumps(event)
        )
        return True
    else:
        del event['response']['conditions']
        verbose_info(
            messages("send_unsuccess_event_from_module").format(
                process_number,
                module_name,
                target,
                module_thread_number,
                total_module_thread_number,
                request_number_counter,
                total_number_of_requests
            )
        )
        verbose_info(
            json.dumps(event)
        )
        return 'save_to_temp_events_only' in event['response']