Dev.to Security 🔐 Cybersecurity 👁 0 📖 10 min read

From 20 Seconds to 2: Building a Network Auditor for Kenyan SMEs in Go

After spending four years learning how packets move, how subnets are carved up, and what happens when a port answers a SYN while in university, I joined Zone01 Kisumu, where I started learning Go and building production

After spending four years learning how packets move, how subnets are carved up, and what happens when a port answers a SYN while in university, I joined Zone01 Kisumu, where I started learning Go and building production ready systems.

This is the story of how those two worlds collided in a project called SME-Shield: a network security auditing tool built in Go for small and medium-sized businesses in Kenya. It covers the architecture, the concurrency fix that cut scan times from about 20 seconds to 1-2, the bugs that cost me an embarrassing number of hours, and what I'd do differently.

If you're learning Go and have a background in something else, I hope you see yourself in this.

Why small businesses?

Walk into a Sacco, a private hospital, a hotel, a school or a county government office in Kenya, and ask who looks after the network. Often the honest answer is "the guy who set up the Wi-Fi", or "an IT person who also fixes the printers".

These organisations hold things attackers love: member savings, patient records, guest payment details, student data. But they can't afford enterprise security suites, and they rarely have a dedicated security team. Tools like Nmap are fantastic, but they assume you already know what you're looking at. The output is a wall of text, and the person reading it may not be a networking specialist.

So the idea was simple: take what a scanner like Nmap does, wrap it in something a non-specialist can use, and produce a report they can hand to a manager. Scan the network, find devices, see which ports are open and which services (and versions) are running, and present it clearly, on a schedule, without anyone typing flags into a terminal.

SME-Shield is still a personal project. The long-term goal is to pitch it to real Kenyan businesses, and that goal shapes a lot of the decisions below.

Step zero: the port scanner

SME-Shield didn't start as SME-Shield. It started as a small port scanner: point it at a network, it finds devices, reports which ones have open ports, and tries to identify the version of the service listening.

That first tool taught me the thing every networking student knows in theory and has to feel in practice: scanning is mostly waiting. You send a probe and then you sit there until something answers or a timeout expires. Most of the time, nothing answers.

Which brings us to the first big lesson.

The 20-second problem

My first discovery loop was sequential. For each host, for each port, dial, wait, move on. On a small network it was already painfully slow. Discovery took around 20 seconds, which doesn't sound terrible until you remember a user is watching a spinner and wondering if the app has crashed.

Here's a simplified version of what the slow code looked like:

func scanSequential(host string, ports []int, timeout time.Duration) []int {
    var open []int
    for _, p := range ports {
        addr := net.JoinHostPort(host, strconv.Itoa(p))
        conn, err := net.DialTimeout("tcp", addr, timeout)
        if err != nil {
            continue
        }
        conn.Close()
        open = append(open, p)
    }
    return open
}

Every closed or filtered port costs you the full timeout. Multiply that by hundreds of ports and several hosts and you get your 20 seconds.

The fix is the reason people fall in love with Go. Each probe is independent, and waiting is exactly what goroutines are cheap at:

func scanConcurrent(host string, ports []int, timeout time.Duration, workers int) []int {
    var (
        mu   sync.Mutex
        wg   sync.WaitGroup
        open []int
        sem  = make(chan struct{}, workers) // limit concurrent dials
    )

    for _, p := range ports {
        wg.Add(1)
        sem <- struct{}{} // acquire a slot
        go func(port int) {
            defer wg.Done()
            defer func() { <-sem }() // release the slot

            addr := net.JoinHostPort(host, strconv.Itoa(port))
            conn, err := net.DialTimeout("tcp", addr, timeout)
            if err != nil {
                return
            }
            conn.Close()

            mu.Lock()
            open = append(open, port)
            mu.Unlock()
        }(p)
    }

    wg.Wait()
    sort.Ints(open)
    return open
}

Three details here matter more than the goroutines themselves:

  1. The semaphore channel. Launching one goroutine per port with no limit is how you exhaust file descriptors or hammer a fragile router. A buffered channel as a semaphore is the simplest way to cap concurrency.
  2. The mutex around the results slice. Appending to a shared slice from multiple goroutines is a data race. Run your code with go run -race early and often. It will humble you.
  3. Sorting at the end. Concurrency destroys ordering. Users expect to see ports in ascending order, so sort before returning.

After moving the port probing (and device discovery) to goroutines, discovery dropped from roughly 20 seconds to about 1-2 seconds. Same network, same results, a completely different feel.

One note on being a good citizen: a fast scanner is also a loud scanner. Tune your worker count and timeouts for the network you're on, and only ever scan networks you own or have written permission to test. For a tool aimed at businesses, "authorised scanning only" is not a footnote. It should be part of the product.

Architecture: boring on purpose

After the scanner worked, I did what I think is the most important thing a beginner can do. I stopped adding features and restructured the whole thing. SME-Shield went through a full architectural revamp, and I landed on a layered layout:

cmd/server/          # entry point
internal/
  audit/             # audit logic
  scanner/           # port & service probing
  network/           # device discovery
  hub/               # WebSocket hub
  handlers/          # HTTP handlers
  models/            # data structures
  auth/              # sessions, passwords
  scheduler/         # recurring scans
platform/
  db/                # SQLite access
  nvd/
  report/            # PDF generation
  email/
ui/                  # frontend assets

The rule I follow: internal/ holds the application's own logic, platform/ holds adapters to the outside world (database, reports, email), and cmd/ just wires things together. Handlers don't know how scanning works; scanners don't know about HTTP.

It isn't clever, and that's the point. When something breaks at 11pm, I want to know which folder to open.

The stack is deliberately small: the standard library's net/http, gorilla/websocket for live updates, go-sqlite3 for storage, gofpdf for reports, golang.org/x/crypto for bcrypt, and yaml.v3 for config. SQLite means there's no database server to install or babysit, which matters when your target user can't maintain one.

Features that made it feel like a product

A scanner is a tool. A product has to do a few more things:

  • Live streaming results over WebSockets. Instead of waiting for the full scan, the UI shows devices and ports appearing as they're discovered. Combined with the concurrency fix, a scan feels alive rather than frozen.
  • Scheduled recurring scans. Security isn't a one-time event. A scheduler runs audits periodically so the business can see what changed.
  • PDF reports. This is the feature I think matters most for SMEs. The person who pays for the tool is rarely the person who reads Nmap output. A PDF with timestamps (in East Africa Time, because a report that says 03:00 when it's 06:00 in Nairobi looks broken) gives them something to forward to a manager.
  • Authentication. First-run setup page creates the admin account. Passwords are hashed with bcrypt at cost 12, and sessions use HMAC-SHA256 signed cookies.
  • Docker. A multi-stage Alpine build: GCC in the builder stage (SQLite needs CGO), and only sqlite-libs, ca-certificates and tzdata in the final image.

The bugs that taught me the most

Here's the honest part. Most of my learning didn't come from features. It came from bugs. These are the ones I'd warn my past self about.

1. http.ServeFile and the mysterious 301

I was serving my UI's HTML files with http.ServeFile, and my pages kept redirecting instead of loading. After some digging: http.ServeFile issues a 301 redirect when the request path ends in /index.html, to normalise the URL.

My fix was to bypass it for HTML and read the file directly:

func serveFileDirectly(w http.ResponseWriter, path string) {
    data, err := os.ReadFile(path)
    if err != nil {
        http.NotFound(w, nil)
        return
    }
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    w.Write(data)
}

Now every .html file goes through that function. The lesson isn't "ServeFile is bad". It's that helper functions have behaviours you don't know about until they bite you. Read the docs for the standard library calls you rely on.

2. filepath.Join and the leading slash

I was joining a base directory with a request path, something like filepath.Join("ui", r.URL.Path), and getting results that surprised me. Request paths start with /, and I'd assumed Join would treat that as relative. Fixing it meant trimming the prefix first:

rel := strings.TrimPrefix(r.URL.Path, "/")
full := filepath.Join("ui", rel)

Also worth knowing: if you're building paths from user input, you need to think about path traversal (../../etc/passwd) too. Cleaning and trimming is the start of that conversation, not the end of it.

3. Auth middleware ordering

This one is subtle. SME-Shield has a first-run setup page where the admin creates an account. But my auth middleware was redirecting unauthenticated requests to login, and on a fresh install there are no users, so you can't log in, and you can't reach setup either. A beautiful little deadlock.

The fix was about ordering. The middleware has to check whether setup has been completed first; if no user exists, redirect to /setup. And /setup plus its API endpoint must be explicitly listed as public routes until setup finishes:

func authMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        if !IsSetupDone() {
            if r.URL.Path == "/setup" || r.URL.Path == "/api/setup" {
                next.ServeHTTP(w, r)
                return
            }
            http.Redirect(w, r, "/setup", http.StatusSeeOther)
            return
        }
        // ...normal session checks
        next.ServeHTTP(w, r)
    })
}

Order of checks in middleware is the logic. Draw it on paper before you write it.

4. "Method not allowed" on the schedule route

I'd registered GET and POST for the schedule endpoint as separate routes, and one of them kept returning 405. The fix was to register a single handler and branch on r.Method inside it. Boring, but a good reminder that routing behaviour depends on exactly how you register patterns.

5. SQLite needs CGO

go-sqlite3 wraps the C library, so building without CGO_ENABLED=1 fails in confusing ways. I put it in the Makefile so I'd never have to remember it again. If you containerise, remember that your builder stage needs a C compiler. That's why mine includes GCC while the runtime image stays tiny.

Building on a machine you don't control

Something nobody tells you about bootcamp life: you don't always control your environment. I work on shared machines with no sudo. The Go installation is owned by another user, so go tries to write module caches in places I can't touch.

The fix was to point Go at directories I own, and make it permanent:

export GOPATH=$HOME/go
export GOMODCACHE=$HOME/go/pkg/mod

Added to ~/.bashrc, and the problem disappeared.

Docker was similar. On these machines it runs in rootless mode, which means --network=host doesn't bind to the real host's localhost the way you'd expect. For local development, I found it more reliable to just run make run and save Docker for when I'm building the final image. Knowing why a tool behaves differently in a restricted environment turns a day of frustration into ten minutes of reading.

The day my repo broke

At one point my local repository became corrupted. Git refused to behave, and the backup I'd made had a corrupted .git folder too.

What saved me: the source files in the backup were intact. I cloned a fresh copy from GitHub and restored my source files over it.

The lessons were old ones, learned the hard way:

  • Push often. The remote is your real backup.
  • A backup of a .git folder isn't necessarily a backup of your history; it's a backup of your files.
  • Corruption isn't the end of the world if your source is safe and your remote is current.

What I'd do differently

  • Write tests earlier. The scanner logic is very testable (spin up a local listener on a random port, assert it's found). I tested too late.
  • Think about legal and ethical guardrails from day one. A scanner for businesses should make it difficult to point at things you shouldn't.
  • Document as I go. I built in batches covering a full feature lifecycle, from architecture to debugging, and I'd have saved myself time by writing short notes after each one.
  • Decide on a vulnerability data source early. Knowing a service version is only half the story; mapping it to known vulnerabilities is where an auditor becomes genuinely useful. My layout has room for this, and it's the area I most want to grow.

Where this is going

I'm leaning towards cloud and DevOps as my specialisation, and SME-Shield has turned out to be a good proving ground for it: containers, multi-stage builds, scheduled jobs, environment configuration, reliable deploys. The networking background is the foundation; the cloud and DevOps skills are what let me ship it.

And honestly, that's the thing I'd tell anyone from a networking or systems background who's picking up a language: you already understand the problem domain. Writing the code is just a new way to express what you know.

If you're building something similar, or you work with SMEs and see gaps I'm missing, I'd love to hear from you in the comments. You can find my projects on GitHub at rjeff-sudo.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.