DEV Community

Huynh Thanh Phuc
Huynh Thanh Phuc

Posted on • Updated on

Building a Simple TCP Port Scanner in Go

Hey! 🚀

I'm excited to share a simple yet powerful tool I've built using Go - a TCP port scanner. This tool allows you to quickly identify open ports on a target server, making it a handy utility for network administrators and security enthusiasts. Let's dive into the code and explore how it works!

https://github.com/ThanhPhucHuynh/tcp-scanner-go-basic/tree/main

func worker(ports, results chan int) {
    for p := range ports {
        address := fmt.Sprintf("scanme.nmap.org:%d", p)
        conn, err := net.Dial("tcp", address)
        if err != nil {
            results <- 0
            continue
        }
        conn.Close()
        results <- p
    }
}
Enter fullscreen mode Exit fullscreen mode

And call this func in main

ports := make(chan int, MAX_PORTS)
results := make(chan int)
var open_ports []int

for i := 0; i < cap(ports); i++ {
    go worker(ports, results)
}

go func() {
    for i := 1; i <= MAX_PORTS; i++ {
        ports <- i
    }
}()

for i := 0; i < MAX_PORTS; i++ {
    port := <-results
    if port != 0 {
        open_ports = append(open_ports, port)
    }
}
Enter fullscreen mode Exit fullscreen mode

Understanding the Code

First, let's break down the key components of the code:

Goroutines and Channels: The use of goroutines and channels in Go is central to the concurrent execution of tasks. Goroutines are lightweight threads, and channels facilitate communication between them.

worker() Function: This function is responsible for attempting to establish a TCP connection to a given port on the target server. If the connection is successful, the port is considered open, and its number is sent through the results channel.

main() Function: The main function sets up the necessary channels and goroutines, distributing the task of scanning ports among the workers. It then collects the results, filters out closed ports, and finally, prints the open ports in ascending order.

Running the Scanner
To run the port scanner, simply copy the code into a Go file (e.g., main.go) and execute it. The scanner defaults to scanning the well-known ports (1-65535) on the target server scanme.nmap.org. You can customize the target address by modifying the address variable in the worker() function.

go run main.go
Enter fullscreen mode Exit fullscreen mode

Result

Image description

How It Works

The scanner utilizes the net.Dial function to attempt a TCP connection to each port. If the connection is successful, the port is considered open, and its number is added to the list of open_ports. The final list is then sorted and printed.

Conclusion

This simple TCP port scanner in Go demonstrates the language's concurrency capabilities. It's a great starting point for those looking to explore network programming or enhance their Go skills. Feel free to experiment, modify, and extend the code to suit your specific needs!

I hope you find this tool useful in your network exploration endeavors. Happy coding!

Top comments (0)