Exemplo n.º 1
0
async def status_update_poller() -> None:
    """Coroutine for periodically printing updates about the scan status."""
    interval = get_db_value('status-interval')
    verbose = get_db_value('verbose-status')
    cmd_len = get_db_value('cmd-print-width')
    if interval <= 0:
        raise BscanInternalError(
            'Attempted status update polling with non-positive interval of ' +
            str(interval))

    time_elapsed = float(0)
    while True:
        await asyncio.sleep(_STATUS_POLL_PERIOD)

        stats: RuntimeStats = get_runtime_stats()
        if stats.num_active_targets < 1:
            break

        time_elapsed += _STATUS_POLL_PERIOD
        if time_elapsed >= interval:
            time_elapsed = float(0)
            msg = ('Scan status: ' + str(stats.num_total_subprocs) +
                   ' spawned subprocess(es) currently running across ' +
                   str(stats.num_active_targets) + ' target(s)')
            if verbose:
                subl = db['sublemon']
                print_i_d2(msg, ', listed below')
                for sp in subl.running_subprocesses:
                    print_i_d3(shortened_cmd(sp.cmd, cmd_len))
            else:
                print_i_d2(msg)
Exemplo n.º 2
0
async def run_udp_s(target: str) -> Set[ParsedService]:
    """Run a UDP scan on a target."""
    print_i_d2(target, ': beginning UDP scan')
    udp_config = get_db_value('udp-scan')
    cmd = udp_config.scan.format(
        target=target,
        fout=get_scan_file(target, 'udp.' + udp_config.name))
    services = await _parse_port_scan(target, cmd, udp_config.pattern)
    print_i_d2(target, ': finished UDP scan')
    return services
Exemplo n.º 3
0
async def run_ts(target: str) -> Set[ParsedService]:
    """Run a thorough TCP scan on a target using the configured method."""
    print_i_d2(target, ': beginning TCP thorough scan')
    ts_config = get_db_value('thorough-scan')
    cmd = ts_config.scan.format(
        target=target,
        fout=get_scan_file(target, 'tcp.thorough.' + ts_config.name))
    services = await _parse_port_scan(target, cmd, ts_config.pattern)
    print_i_d2(target, ': finished TCP thorough scan')
    return services
Exemplo n.º 4
0
async def run_qs(target: str) -> Set[ParsedService]:
    """Run a quick scan on a target via the configured method."""
    print_i_d2(target, ': beginning TCP quick scan')
    qs_config = get_db_value('quick-scan')
    cmd = qs_config.scan.format(
        target=target,
        fout=get_scan_file(target, 'tcp.quickscan.' + qs_config.name))
    services = await _parse_port_scan(target, cmd, qs_config.pattern)
    print_i_d2(target, ': finished TCP quick scan')
    return services
Exemplo n.º 5
0
async def scan_target(target: str) -> None:
    """Run quick, thorough, and service scans on a target."""
    do_ts = not get_db_value('quick-only')
    do_s_scans = not get_db_value('no-service-scans')
    await add_active_target(target)

    # block on the initial quick scan
    qs_parsed_services = await run_qs(target)
    qs_unmatched_services, qs_joined_services = \
        join_services(target, qs_parsed_services)
    _print_matched_services(target, qs_joined_services)
    _print_unmatched_services(target, qs_unmatched_services)

    # schedule service scans based on qs-found ports
    if do_s_scans:
        qs_s_scan_cmds: List[List[str]] = \
            [js.build_scans() for js in qs_joined_services]
        qs_s_scans: List[Coroutine[Any, Any, Any]] = \
            [run_service_s(target, cmd) for cmd in chain(*qs_s_scan_cmds)]
        qs_s_scan_tasks = [ensure_future(scan) for scan in qs_s_scans]
    else:
        qs_s_scan_tasks = []

    # block on the thorough scan, if enabled
    if do_ts:
        ts_parsed_services: Set[ParsedService] = await run_ts(target)
    else:
        ts_parsed_services = set()
        print_i_d2(target, ': skipping thorough scan')

    # diff open ports between quick and thorough scans
    new_services: Set[ParsedService] = ts_parsed_services - qs_parsed_services
    ts_joined_services: List[DetectedService] = []
    if new_services and do_s_scans:
        ts_unmatched_services, ts_joined_services = \
            join_services(target, new_services)
        _print_matched_services(target, ts_joined_services)
        _print_unmatched_services(target, ts_unmatched_services)
        ts_s_scan_cmds = [js.build_scans() for js in ts_joined_services]
        ts_s_scans = [run_service_s(target, cmd) for
                      cmd in chain(*ts_s_scan_cmds)]
        ts_s_scan_tasks = [ensure_future(scan) for scan in ts_s_scans]
    elif do_ts:
        print_i_d2(target, ': thorough scan discovered no additional '
                   'services')
        ts_s_scan_tasks = []
    else:
        ts_s_scan_tasks = []

    # write recommendations file for further manual TCP commands
    for js in chain(qs_joined_services, ts_joined_services):
        if not js.recommendations:
            continue

        with open(get_recommendations_txt_file(js.target), 'a') as f:
            fprint = partial(print, file=f, sep='')
            section_header = (
                'The following commands are recommended for service ' +
                js.name + ' running on port(s) ' + js.port_str() + ':')
            fprint(section_header)
            fprint('-'*len(section_header))
            for rec in js.build_recommendations():
                fprint(rec)
            fprint()

    # run UDP scan
    if get_db_value('udp'):
        udp_services = await run_udp_s(target)
        for service in udp_services:
            print_i_d3(
                target, ': detected service ', blue(service.name),
                ' on UDP port ', blue(str(service.port)))

    # block on any pending service scan tasks
    await gather(*chain(qs_s_scan_tasks, ts_s_scan_tasks))

    await remove_active_target(target)