Golang for Pentesters / Red Teamers

c0c0n 2019

28 September 2019

Talk: Golang for Pentesters / RedTeamers

Abstract

Pentesters and redteamers alike are nowadays faced with multiple challenges where conventional tools fail or just doesn’t work and you need to whip up a tool of your own may be just for this one task or may be as a new tool entirely. With that in mind This talk will explore using Golang as a language of choice in such scenarios. Golang is a recent entry in the world of modern languages. This language has bought the capabilities of compiled languages as well as interpreted languages in a single place and as cherry on top it has brilliant cross platform capabilities. This talk will explore how Golang can be used by a professionals to automate routine task’s and how a single codebase can be leveraged to craft binaries for multiple platform.
In the current world preferred choices are python / bash however we would like to leverage Go for its inherent capabilities such as low level I/O handling or handling high volume of incoming data (web requests / large file content) and in-build concurrency capabilities which allows golang to perform magnitudes faster then python or bash or any other typed or interpreted language.
The talk will guide from starting to write a basic hello world program in Golang to leveraging the brilliant modules available in the language to perform routine tasks like,

  1. port scanning,
  2. network query,
  3. web requests,
  4. user input handling,
  5. network monitoring
  6. Log parsing

This talk will provide the necessary boilerplates to get started with writing your own next gen tool in golang and take the next step in evolving information security.

AI Generated Summary

AI Generated Content Disclaimer

Note: This summary is AI-generated and may contain inaccuracies, errors, or omissions. If you spot any issues, please contact the site owner for corrections. Errors or omissions are unintended.

Presented at c0c0n 12 (2019), “Golang for Pentesters / RedTeamers” introduces the Go programming language as a practical tool for offensive security work. Across 32 slides, the deck covers why Go suits pentesting and red teaming (cross-platform compiled binaries, no runtime dependencies, rich standard library, active security tooling ecosystem), walks through setting up a Go development environment, and demonstrates essential modules for pentesting tasks such as DNS lookups, port scanning, web scraping, and JSON handling. It closes with building and cross-compiling binaries, Docker deployment, code security with gosec, dependency management, binary size reduction, and automating the entire build process with Makefiles.

Summary

The talk opens with a disclaimer: it is focused on Golang or the Go programming language, will not do performance comparisons or debate other languages, and will not deep dive but rather explore golang to give people a headstart. The why section frames Go as combining the ease of programming of an interpreted, dynamically typed language with the efficiency and safety of a statically typed, compiled one. For pentesters the pitch is concrete: cross-platform across Windows, Linux, and Mac, a fat static binary by default with no third-party dependency, not even a runtime, and the ability to run in bare dockers or directly above the kernel. A tools catalogue grounds the argument in what pentesters already use: Gobuster, Bettercap 2.0, GoBot2, GoAT, Cracklord, GoCrack, Merlin, and Vuls, with more findable via the github pentesting topic page filtered on Go.

The setup section covers installing Go from binaries at golang.org/dl or via apt/yum, and the two paths that confuse newcomers: GOPATH (where packages are installed, typically under the user home in go) and GOROOT. Hello World demonstrates package main, import fmt, and func main, run via go run or compiled via go build.

The modules section opens with the philosophy that these days we do not write programs but collect modules and stick them together, so a language is as good as the modules it provides. The walkthrough covers argparse for command-line inputs; the net package for DNS lookups with the tip that a registered domain always has an NS record; publicsuffix for pulling effective TLD plus one out of arbitrary strings like blog.abc.com; encoding/json for marshaling structured output; port scanning via net.Dial and net.DialTimeout with timeouts; and soup, inspired by Python’s BeautifulSoup, for scraping.

The optimization and operations arc covers gofmt applying a uniform formatting syntax and ending formatting debates, binary size reduction via go build -ldflags ‘-s -w’ followed by UPX compression, Docker deployment in a scratch container, and cross-compiling with env GOOS=linux GOARCH=amd64 style commands.

The closing sections turn to hygiene and automation. Secure your code: run periodic checks with gosec, recursively or with specific or excluded rule sets, with a note that Snyk has recently introduced Go module dependency checks. Update management: build after updating the dependency (go get -u against the module URL) to ensure building against latest. A complete Makefile then automates everything: linux, windows, and mac builds, a docker target building the scratch image, clean, check, updatedep, and combined all and complete targets. The final slides note that fat binaries are hard to reverse engineer, suiting red team tooling, and that reproducible builds, producing byte-by-byte matching binaries, are achieved by maintaining a consistent build environment, provided via Docker builds.

Key Themes

Notable Points

  1. Takeaway: set up a Go development environment and start by building simple pentesting utilities (port scanners, DNS enumerators, web scrapers) using the modules covered: net, soup, argparse, and leverage cross-compilation to build for all target platforms from a single codebase with GOOS/GOARCH environment variables.
  2. Takeaway: use scratch Docker containers for deploying Go-based tools, creating minimal attack surface and tiny container images since Go binaries have zero runtime dependencies.
  3. Takeaway: reduce binary sizes with -ldflags ‘-s -w’ compiler flags and UPX compression to make tools less conspicuous for red team operations.
  4. Takeaway: integrate gosec into your development workflow (and CI/CD via Makefile) to catch security issues in your own pentesting tools before deployment.
  5. Takeaway: explore existing Go security tools (Gobuster, Bettercap, Merlin, Cracklord) both as ready-to-use utilities and as reference implementations, and take advantage of Go’s difficult-to-reverse-engineer binaries for red team implants and post-exploitation tools where operational security matters.
  6. The tools catalogue cites specific repositories: github.com/OJ/gobuster, github.com/bettercap/bettercap, github.com/SaturnsVoid/GoBot2, github.com/petercunha/GoAT, github.com/jmmcatee/cracklord, github.com/fireeye/gocrack, github.com/Ne0nd0g/merlin, github.com/future-architect/vuls, plus github.com/topics/pentesting?l=go.
  7. Go environments hinge on GOPATH (packages installed under ~/go) and GOROOT (/usr/lib default); alternate toolchains install with go get golang.org/dl/go1.13.1, go1.13.1 download, then go1.13.1 version.
  8. DNS work uses net.LookupIP, net.LookupCNAME, net.LookupAddr, net.LookupNS, net.LookupMX, and net.LookupTXT, with the tip that a registered domain always has an NS record; publicsuffix.EffectiveTLDPlusOne maps blog.abc.com to google.com and test.abc.co.in to abc.co.in.
  9. Port scanning uses net.Dial or net.DialTimeout (the timeout fires after Timeout seconds; alternative github.com/anvie/port-scanner); gosec runs recursively via gosec ./…, specific rules via gosec -include=G101,G203,G401, exclusions via gosec -exclude=G303.
  10. The Makefile exposes linux, windows, mac, docker, clean, check, updatedep, all, and complete targets, and the reproducible-builds reference is blog.filippo.io/reproducing-go-binaries-byte-by-byte.

Notable Quotes

“Language is as good as the modules it provides”

“Fat Static Binary by default”

“no 3rd party dependency not even runtime”

“As fat binaries have no dependencies they can run in Scratch”

“gosec is one good code review tool for golang”

“The trick is maintain a consistent build environment”

Social chatter