-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathexec.go
160 lines (127 loc) · 3.69 KB
/
exec.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package commands
import (
"fmt"
"io"
"strings"
"github.com/NHAS/reverse_ssh/internal/server/clients"
"github.com/NHAS/reverse_ssh/internal/terminal"
"github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
"golang.org/x/crypto/ssh"
)
type exec struct {
}
func (e *exec) Run(tty io.ReadWriter, line terminal.ParsedLine) error {
if terminal.IsSet("h", line.Flags) {
fmt.Fprintf(tty, "%s", e.Help(false))
return nil
}
filter := ""
command := ""
if len(line.LeftoversStrings()) > 0 {
if len(line.Leftovers) < 2 {
return fmt.Errorf("Not enough arguments supplied. Needs at least, host|filter command...")
}
filter = line.LeftoversStrings()[0]
command = line.RawLine[line.Leftovers[0].End():]
} else if len(line.FlagsOrdered) > 0 {
args := line.FlagsOrdered[len(line.FlagsOrdered)-1].Args
if len(args) < 2 {
return fmt.Errorf("Not enough arguments supplied. Needs at least, host|filter command...")
}
filter = args[0].Value()
command = line.RawLine[args[0].End():]
}
command = strings.TrimSpace(command)
matchingClients, err := clients.Search(filter)
if err != nil {
return err
}
if len(matchingClients) == 0 {
return fmt.Errorf("Unable to find match for '" + filter + "'\n")
}
if !(terminal.IsSet("q", line.Flags) || terminal.IsSet("raw", line.Flags)) {
fmt.Fprintln(tty, "Effects:")
count := 0
for id, client := range matchingClients {
if count > 5 {
break
}
fmt.Fprintf(tty, "%s (%s)\n", id, client.User()+"@"+client.RemoteAddr().String())
count++
}
if count > 5 {
fmt.Fprintf(tty, "... %d hosts omitted ...\n", len(matchingClients)-count)
}
if !terminal.IsSet("y", line.Flags) {
fmt.Fprintf(tty, "Run command? [N/y] ")
if term, ok := tty.(*terminal.Terminal); ok {
term.EnableRaw()
}
b := make([]byte, 1)
_, err := tty.Read(b)
if err != nil {
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
return err
}
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
if !(b[0] == 'y' || b[0] == 'Y') {
return fmt.Errorf("\nUser did not enter y/Y, aborting")
}
}
}
var c struct {
Cmd string
}
c.Cmd = command
commandByte := ssh.Marshal(&c)
for id, client := range matchingClients {
if !(terminal.IsSet("q", line.Flags) || terminal.IsSet("raw", line.Flags)) {
fmt.Fprint(tty, "\n\n")
fmt.Fprintf(tty, "%s (%s) output:\n", id, client.User()+"@"+client.RemoteAddr().String())
}
newChan, r, err := client.OpenChannel("session", nil)
if err != nil && !terminal.IsSet("q", line.Flags) {
fmt.Fprintf(tty, "Failed: %s\n", err)
continue
}
go ssh.DiscardRequests(r)
defer newChan.Close()
response, err := newChan.SendRequest("exec", true, commandByte)
if err != nil && !terminal.IsSet("q", line.Flags) {
fmt.Fprintf(tty, "Failed: %s\n", err)
continue
}
if !response && !terminal.IsSet("q", line.Flags) {
fmt.Fprintf(tty, "Failed: client refused\n")
continue
}
if terminal.IsSet("q", line.Flags) {
io.Copy(io.Discard, newChan)
continue
}
io.Copy(tty, newChan)
}
return nil
}
func (e *exec) Expect(line terminal.ParsedLine) []string {
if line.Focus == nil {
return []string{autocomplete.RemoteId}
}
return nil
}
func (e *exec) Help(explain bool) string {
if explain {
return "Execute a command on one or more rssh client"
}
return makeHelpText(
"exec [OPTIONS] filter|host command",
"Filter uses glob matching against all attributes of a target (hostname, ip, id), allowing you to run a command against multiple machines",
"\t-q\tQuiet, no output (will also remove confirmation prompt)",
"\t-y\tNo confirmation prompt",
"\t--raw\tDo not label output blocks with the client they came from",
)
}