示例#1
0
def apimethod_nmap_purge_database():
    """Purge the redis database"""
    db = None
    try:
        db = NMAPScansDB()
        db.flush()
    finally:
        del db
示例#2
0
def apimethod_get_nmap_scan_status(task_id):
    """Returns the nmap status for the given task
    Args:
        task_id: The task id which status you want to know
    Returns:
        job(str): A python dic with the job information.
    Raises:
        APINMAPScanKeyNotFound: When the given id doesn't exist
        APINMAPScanException: When something wrong happen
    """

    try:
        # the nmap could be scheduled in celery but not launched.
        # in this case there is no nmap status on the database.
        job = None
        db = NMAPScansDB()
        tries = 3
        while tries > 0:
            try:
                raw_data = db.get(task_id)
                job = ast.literal_eval(raw_data)
                tries = 0
            except RedisDBKeyNotFound:
                # Maybe the job is not in the database yet
                # check if the job is scheduled.
                task = is_task_in_celery(task_id)
                if task is not None:
                    if task_id == task['id']:
                        task_kwargs = task['kwargs']
                        # La info va a de kwargs
                        job = {"job_id": task['id'],
                               "sensor_id": task_kwargs['sensor_id'],
                               "idm": task_kwargs['idm'],
                               "target_number": task_kwargs['targets_number'],
                               "scan_params": {"target": task_kwargs['target'],
                                               "scan_type": task_kwargs['scan_type'],
                                               "rdns": task_kwargs['rdns'],
                                               "autodetect": task_kwargs['autodetect'],
                                               "scan_timing": task_kwargs['scan_timing'],
                                               "scan_ports": task_kwargs['scan_ports']},
                               "status": "In Progress",
                               "scanned_hosts": 0,
                               "scan_user": task_kwargs['scan_ports'],
                               "start_time": int(time.time()),
                               "end_time": -1,
                               "remaining_time": -1
                               }
                        tries = 0

                time.sleep(1)
            tries -= 1
    except Exception as e:
        raise APINMAPScanException(str(e))
    finally:
        del db
    if job is None:
        raise APINMAPScanKeyNotFound()
    return job
示例#3
0
def apimethod_nmap_purge_database():
    """Purge the redis database"""
    try:
        db = NMAPScansDB()
        db.flush()
    except Exception:
        raise
    finally:
        del db
示例#4
0
def apimethod_nmapdb_delete_task(task_id):
    """Delete task from the nmapdb
    Raises:
        APINMAPScanCannotBeSaved
    """
    db = None
    try:
        db = NMAPScansDB()
        db.delete_key(task_id)
    finally:
        del db
示例#5
0
def apimethod_nmapdb_delete_task(task_id):
    """Deelte
    Raises:
        APINMAPScanCannotBeSaved
    """
    try:
        db = NMAPScansDB()
        db.delete_key(task_id)
    except Exception:
        raise
    finally:
        del db
示例#6
0
def apimethod_get_nmap_scan_list(user):
    """Monitors all NMAP scan list
    Args:
        user: User login
    Returns:
        scans(dic): A python dic with all jobs.
    Raises:
        Exception: When something wrong happen
    """
    user_scans = []
    db = NMAPScansDB()
    scans = db.get_all()
    del db

    for scan in scans:
        if scan['scan_user'] == user:
            user_scans.append(scan)
    return user_scans
示例#7
0
def apimethod_nmapdb_add_task(task_id, task_data):
    """Add a new nmap task to the nmapdb
    Raises:
        APINMAPScanCannotBeSaved
    """
    rt = False
    try:
        db = NMAPScansDB()
        db.add(task_id, task_data)
        rt = True
    except NMAPScanCannotBeSaved:
        api_log.error("[apimethod_nmapdb_add_task] NMAPScanCannotBeSaved - Cannot save task")
        raise APINMAPScanCannotBeSaved()
    except Exception as e:
        api_log.error("[apimethod_nmapdb_add_task] Cannot save task %s" % str(e))
    finally:
        del db
    return rt