Hacking With The Go Programming Language: Why?

Disclaimer: The information presented here is for education purposes. HACKING IS NOT A CRIME!

This post is the first on a series of “Hacking with Go” posts in which I will go over different concepts and features of Go that are very useful for hacking. This series is NOT an introduction to programming in Go. Check out https://tour.golang.org/list for an introduction to programming with Go.

In a recent blog post, I showed a simple two-step guide for learning a new programming language. In it, I mentioned that I was learning Go. My overall experience with Go has been quite positive, and I am very likely going to make it my new programming language of choice to build hacking tools.

I will introduce some concepts and features that explain why Go is so good for hacking. The best resource to dive deeper into these concepts is Go’s official documentation.

Go’s best features for hacking

Cross-Compiling: 

It means that if you are using an Operating System, like Windows, you can easily compile your Go program for other Operating Systems and Architectures. It is a huge plus when building tools that can run on different systems because of the minimal work needed.

The build command is used to compile your Go code, and it can be used to cross-compile by adding some constraints/flags. These include GOOS for the OS and GOARCH for the architecture. You can introduce build constraints in three ways: via the command line, code comments, or a file suffix naming convention. To learn more, visit https://golang.org/doc/install/source#environment

For example, If you want to compile your program to a Linux system and you are in a Windows system, you can run in your command line $ GOOS=”linux” GOARCH=”amd64″ go build <go-program-name>

Keep in mind that cross-compiling runs into some problems if you are using native C bindings.

Single-Binary Output:

Go will compile your application, including any packages and dependencies. into a single binary that executes your program. That single file will run your entire program and you would not need to install dependencies.

A slight issue with this binary, depending on your use-case, is that the binary can be a couple of MB in size. It can be an issue if you want to transfer or embed your program. By default, Go compiles with debugging information and the symbol table. The following command will reduce the file size by about 30%: $ go build -ldflags “-w -s” Always make sure to check the official docs for your specific use case.

Concurrency:

Go has a much simpler concurrency model compared to other languages. You can use goroutines, which are functions or methods that can run simultaneously. They are often described as lightweight threads because the cost of creating them is minimal compared to actual threads in other languages. By using the keyword go before the function, you can execute that function concurrently.

This is a great feature for building networking and web hacking tools. It will not only be fast when running but also the speed of development will be very good.

Getting into concurrency with Go will need its own deep dive in a separate post.

Massive Ecosystem:

One of Go’s main strengths as a general programming language is the rich built-in standard libraries.

For example, if you work in security, you will likely have to write code that handles structured data like JSON or XML. The packages that work with them are encoding/json and encoding/xml. Another commonly used package is the net package. This one provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets.

In the following blog posts, I will introduce some of the hacking tools I will be building with Go. If you have any questions or have ideas of tools to build with Go, please reach out. Thank you!

Resource: I am currently going over the book “BlackHat Go” by Tom Steele, Dan Kottmann, and Chris Patten.