UnixDroid

Lesson 6 of 12 · 8 min

Faster scanning: threads and banner grabbing

Your scanner works but crawls. Speed it up with threads, then teach it to report what each open service actually is.

The scanner you built works, but checks ports one at a time — and spends almost all its time waiting. This lesson fixes that with threads, then teaches the scanner to report what each open service actually is.

Why threads make scanning fast

Port scanning is I/O-bound: your CPU sits idle while the network replies. Threads let you fire off many connection attempts at once, so the waiting overlaps instead of stacking up.

import socket
from concurrent.futures import ThreadPoolExecutor

def scan(port):
    s = socket.socket()
    s.settimeout(1)
    if s.connect_ex(('127.0.0.1', port)) == 0:
        print(f'[+] {port} open')
    s.close()

with ThreadPoolExecutor(max_workers=100) as pool:
    pool.map(scan, range(1, 1025))

ThreadPoolExecutor runs up to 100 scans in parallel — turning a minute into a second.

Many services announce themselves the moment you connect. Reading that first reply — the banner — often reveals the software and version.

def grab_banner(host, port):
    s = socket.socket()
    s.settimeout(2)
    s.connect((host, port))
    banner = s.recv(1024).decode(errors='ignore')
    s.close()
    return banner.strip()
Open portA banner might reveal
22OpenSSH 8.9
21vsFTPd 3.0.3
80The web server, e.g. nginx

A version number is gold to an attacker — and to a defender, because it tells you exactly what needs patching.

Tip: More threads isn't always better. Push max_workers too high and you'll flood your own network card or trip rate limits, making the scan slower and noisier. Start around 100 and tune from there.

From toy to tool

Scan in parallel, grab a banner on every open port, and you've rebuilt the core of Nmap's most-used features in under 40 lines — and you understand what the big tools do under the hood.

Try it yourself
  1. Run the threaded scanner and compare its speed to the single-threaded one.
  2. Lower max_workers to 10, then raise it to 200 — notice the difference.
  3. Call grab_banner on one open port and print whatever it returns.
No Linux machine? Practise in the free online terminal

Quick check

The first reply a service sends when you connect, often revealing its software and version, is called its what?

Need a hint?

You 'grab' it.

Finished this lesson?

Done lessons keep your daily streak going 🔥

0 day streak

0 lessons done

best 0 days

Log in to keep your streak on every device →