-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
shell.go
217 lines (169 loc) · 4.38 KB
/
shell.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//go:build !windows
// +build !windows
package handlers
import (
"bufio"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"github.com/NHAS/reverse_ssh/internal"
"github.com/NHAS/reverse_ssh/pkg/logger"
"github.com/creack/pty"
"golang.org/x/crypto/ssh"
)
var (
shells []string
goodShells = []string{
"zsh",
"bash",
"fish",
"dash",
"ash",
"csh",
"ksh",
"tcsh",
"sh",
}
)
func getSystemShells() ([]string, error) {
file, err := os.Open("/etc/shells")
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
allSystemShells := []string{}
potentialShells := []string{}
search := make(map[string]bool)
for _, goodShell := range goodShells {
search[goodShell] = true
}
for scanner.Scan() {
line := scanner.Text()
if len(line) > 0 && line[0] == '#' || strings.TrimSpace(line) == "" {
continue
}
allSystemShells = append(allSystemShells, line)
shellPath := strings.TrimSpace(line)
//if shell name is not in our list of known good shells skip it
if !search[filepath.Base(shellPath)] {
continue
}
potentialShells = append(potentialShells, shellPath)
}
if len(potentialShells) == 0 {
potentialShells = allSystemShells
}
return potentialShells, nil
}
func init() {
var (
err error
potentialShells []string
)
for _, goodShell := range goodShells {
good, err := exec.LookPath(goodShell)
if err != nil {
continue
}
potentialShells = append(potentialShells, good)
}
if len(potentialShells) == 0 {
potentialShells, err = getSystemShells()
if err != nil {
//Last ditch effort to find a shell
for _, goodShell := range goodShells {
potentialShells = append(potentialShells, path.Join("/opt/bin/", goodShell))
potentialShells = append(potentialShells, path.Join("/opt/", goodShell))
potentialShells = append(potentialShells, path.Join("/user/local/bin", goodShell))
potentialShells = append(potentialShells, path.Join("/user/local/sbin", goodShell))
potentialShells = append(potentialShells, path.Join("/usr/bin/", goodShell))
potentialShells = append(potentialShells, path.Join("/bin/", goodShell))
potentialShells = append(potentialShells, path.Join("/sbin/", goodShell))
}
}
}
for _, s := range potentialShells {
if stats, err := os.Stat(s); err != nil && (os.IsNotExist(err) || !stats.IsDir()) {
continue
}
shells = append(shells, s)
}
}
func runCommandWithPty(command string, args []string, user *internal.User, requests <-chan *ssh.Request, log logger.Logger, connection ssh.Channel) {
if user.Pty == nil {
log.Error("Requested to run a command with a pty, but did not start a pty")
return
}
// Fire up a shell for this session
shell := exec.Command(command, args...)
shell.Env = os.Environ()
close := func() {
connection.Close()
if shell.Process != nil {
err := shell.Process.Kill()
if err != nil {
log.Warning("Failed to kill shell(%s)", err)
}
}
log.Info("Session closed")
}
// Allocate a terminal for this channel
var err error
var shellIO io.ReadWriteCloser
shell.Env = append(shell.Env, "TERM="+user.Pty.Term)
log.Info("Creating pty...")
shellIO, err = pty.StartWithSize(shell, &pty.Winsize{Cols: uint16(user.Pty.Columns), Rows: uint16(user.Pty.Rows)})
if err != nil {
log.Info("Could not start pty (%s)", err)
close()
return
}
// pipe session to bash and visa-versa
var once sync.Once
go func() {
io.Copy(connection, shellIO)
once.Do(close)
}()
go func() {
io.Copy(shellIO, connection)
once.Do(close)
}()
go func() {
for req := range requests {
switch req.Type {
case "window-change":
if shellf, ok := shellIO.(*os.File); ok {
w, h := internal.ParseDims(req.Payload)
err = pty.Setsize(shellf, &pty.Winsize{Cols: uint16(w), Rows: uint16(h)})
if err != nil {
log.Warning("Unable to set terminal size: %s", err)
}
}
default:
log.Warning("Unknown request %s", req.Type)
if req.WantReply {
req.Reply(false, nil)
}
}
}
}()
defer once.Do(close)
shell.Wait()
}
// This basically handles exactly like a SSH server would
func shell(user *internal.User, connection ssh.Channel, requests <-chan *ssh.Request, log logger.Logger) {
path := ""
if len(shells) != 0 {
path = shells[0]
}
if user.Pty != nil {
runCommandWithPty(path, nil, user, requests, log, connection)
return
}
runCommand(path, nil, connection)
}