Пример #1
0
    def mk_task(self,
                ip: str,
                mac: str,
                target_id: str,
                scan_config: str,
                comment: str = '') -> Union[str, None]:
        """ create task """

        name = "<name>{0} ({1})</name>".format(mac, ip)
        target_id = "<target id=\"{0}\"/>".format(target_id)
        comment = "<comment>{0}</comment>".format(comment)
        scan_config = "<config id=\"{0}\"/>".format(scan_config)
        cmd = "<create_task>{0}{3}{2}{1}</create_task>".format(
            name, target_id, scan_config, comment)

        raw_data = self._omp(['--xml', cmd])

        if is_ok(raw_data):
            task_id = re.findall(
                '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
                raw_data)[0]

            log.notice("[{0}]: created task {1}".format(mac, task_id))

            return task_id

        else:
            log.warn("[{0}]: failed creating task ({1})".format(mac, ip))
            return None
Пример #2
0
    def mk_target(self,
                  ip: str,
                  mac: str,
                  ports: str,
                  comment: str = '') -> Union[str, None]:
        """ create target """

        name = "<name>{0} ({1})</name>".format(mac, ip)
        hosts = "<hosts>{0}</hosts>".format(ip)
        comment = "<comment>{0}</comment>".format(comment)
        ports = "<port_list id=\"{0}\"/>".format(ports)
        cmd = "<create_target>{0}{3}{1}{2}</create_target>".format(
            name, hosts, ports, comment)

        raw_data = self._omp(['--xml', cmd])

        if is_ok(raw_data):
            target_id = re.findall(
                '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
                raw_data)[0]
            log.notice("[{0}]: created target {1}".format(mac, target_id))

            return target_id

        else:
            log.warn("[{0}]: failed creating target ({1})".format(mac, ip))
            return None
Пример #3
0
def vcheck(recent_version_url: str) -> Union[str, bool, None]:
    """ compare given remote and local VERSION file
    
    return False if failed, None if no newer version found
    or recent version
    """

    try:
        with open('VERSION') as file:
            current_version = file.read().strip()

    except:
        log.warn('failed to read file `VERSION`.')
        return False

    recent_version = get(recent_version_url)
    if not recent_version.status_code == 200:
        log.warn('failed to get recent version.')
        return False
    recent_version = recent_version.text.strip()

    for current, recent in zip(current_version.split('.'),
                               recent_version.split('.')):
        if recent > current:
            log.notice('v{0} is available (current: v{1}).'.format(
                recent_version, current_version))
            return recent_version

    else:
        log.debug('up to date: v{0}.'.format(current_version))
        return None
Пример #4
0
def crawl_net(subnet: str = None) -> Iterator[Union[str, str]]:
    """ scan given subnet for IPs and MACs using nmap """
    
    log.notice("scanning {0} for machines".format(subnet))
    
    cmd = "nmap -sn"
    cmd = cmd.split()
    cmd.append(subnet)
    
    stdout, _ = exe(cmd, timeout=300)
    if not stdout:
        log.warn("failed to scan network.")
        return None, None, None
    
    s = stdout.replace('\n', '|')
    
    for chunk in re.findall(r"Nmap scan report for.*?MAC Address.*?\(.*?\)", s):
        
        try:
            ip = re.findall(ip_pattern, chunk)[0]
            mac = re.findall(mac_pattern, chunk)[0]
        
        except IndexError:
            continue
        
        comment = re.findall(r"{0}\s\(.*?\)".format(mac), chunk)
        comment = '' if len(comment) == 0 else comment[0][19:-1]
        
        log.debug("found {0}, {1} ({2})".format(ip, mac, comment))
        yield ip, mac, comment
Пример #5
0
def openvas_run_task() -> None:
    """ try to start OV task until success, ordered by last_report """

    if tasks_running() > config['run_limit']:
        log.info('limit of {0} running tasks reached, no new task.'.format(
            config['run_limit']))
        return

    for ip, _ in test_order('last_attempt'):
        if machine[ip].running:
            log.info('[{0}]: task already running'.format(
                machine[ip].data['mac']))
            continue

        machine[ip].data['last_attempt'] = now()

        if args.verify:
            real_mac = get_mac(ip)
            if real_mac and not machine[ip].data['mac'] == real_mac:
                log.warn('[{0}]: ERROR: address conflict {1} ({2})'.format(
                    machine[ip].data['mac'], real_mac, ip))
                continue

        if ov.run_task(machine[ip].task):
            log.notice('[{0}]: started OpenVAS task'.format(
                machine[ip].data['mac']))

            sleep(60 * 2)

            if task_running(ip):
                log.debug('[{0}]: running task verified'.format(
                    machine[ip].data['mac']))
                machine[ip].keep = True
                break

            else:
                log.warn('[{0}]: task was aborted'.format(
                    machine[ip].data['mac']))
                continue

        else:
            log.warn('[{0}]: FAILED starting OpenVAS task'.format(
                machine[ip].data['mac']))

            continue

    else:
        log.err('FAILED to start any OpenVAS task.')
Пример #6
0
    vcheck(
        'https://raw.githubusercontent.com/TMagerl/AutoOpenVAS/master/VERSION')

    config = load_config()
    ov = OpenVAS('admin', config['passwd'], config['openvas_ip'],
                 config['openvas_omp_port'], config['openvas_web_port'])

    machine = {}
    load_previous_data()
    import_targets(config['job_source'])

    if args.scan:
        scan_net(args.scan)

    openvas_analysis()
    openvas_populate()

    if args.run:
        openvas_run_task()

    clean_up()
    save_results()

    if args.print:
        print_console()

    log.notice('{0} running tasks'.format(tasks_running()))
    log.debug('done.')

    raise SystemExit(0)