-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathexec.go
134 lines (106 loc) · 2.96 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
package commands
import (
"fmt"
"io"
"strings"
"github.com/NHAS/reverse_ssh/internal/server/users"
"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) ValidArgs() map[string]string {
return map[string]string{
"q": "Quiet, no output (will also remove confirmation prompt)",
"y": "No confirmation prompt",
"raw": "Do not label output blocks with the client they came from",
}
}
func (e *exec) Run(user *users.User, tty io.ReadWriter, line terminal.ParsedLine) error {
if len(line.Arguments) < 2 {
return fmt.Errorf("Not enough arguments supplied. Needs at least, host|filter command...")
}
filter := ""
command := ""
if len(line.ArgumentsAsStrings()) > 0 {
filter = line.ArgumentsAsStrings()[0]
command = line.RawLine[line.Arguments[0].End():]
}
command = strings.TrimSpace(command)
matchingClients, err := user.SearchClients(filter)
if err != nil {
return err
}
if len(matchingClients) == 0 {
return fmt.Errorf("Unable to find match for '" + filter + "'\n")
}
if !(line.IsSet("q") || line.IsSet("raw")) {
if !line.IsSet("y") {
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 !(line.IsSet("q") || line.IsSet("raw")) {
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 && !line.IsSet("q") {
fmt.Fprintf(tty, "Failed: %s\n", err)
continue
}
go ssh.DiscardRequests(r)
response, err := newChan.SendRequest("exec", true, commandByte)
if err != nil && !line.IsSet("q") {
fmt.Fprintf(tty, "Failed: %s\n", err)
continue
}
if !response && !line.IsSet("q") {
fmt.Fprintf(tty, "Failed: client refused\n")
continue
}
if line.IsSet("q") {
io.Copy(io.Discard, newChan)
continue
}
io.Copy(tty, newChan)
newChan.Close()
}
fmt.Fprint(tty, "\n")
return nil
}
func (e *exec) Expect(line terminal.ParsedLine) []string {
return []string{autocomplete.RemoteId}
}
func (e *exec) Help(explain bool) string {
if explain {
return "Execute a command on one or more rssh client"
}
return terminal.MakeHelpText(e.ValidArgs(),
"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",
)
}