-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfake_shell.go
365 lines (321 loc) · 9.14 KB
/
fake_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package main
import (
"fmt"
"io"
"regexp"
"strings"
"time"
"github.com/gliderlabs/ssh"
"github.com/toxyl/glog"
"github.com/toxyl/gutils"
"github.com/toxyl/ossh/utils"
"golang.org/x/term"
)
const (
fakeShellInitialWidth = 80
fakeShellInitialHeight = 24
)
type FakeShell struct {
session *ssh.Session
terminal *term.Terminal
writer *utils.SlowWriter
created time.Time
stats *FakeShellStats
prompt string
cwd string
logger *glog.Logger
}
func (fs *FakeShell) User() string {
return (*fs.session).User()
}
func (fs *FakeShell) Host() string {
return gutils.ExtractHostFromAddr((*fs.session).RemoteAddr())
}
func (fs *FakeShell) Close() {
_, err := fs.terminal.Write([]byte(""))
if err != nil {
if err == io.EOF {
(*fs.session).Close()
return
} else {
panic(err)
}
}
(*fs.session).Close()
}
func (fs *FakeShell) UpdatePrompt(path string) {
fs.prompt = fmt.Sprintf("%s@%s:%s# ", fs.User(), Conf.HostName, path)
fs.terminal.SetPrompt(fs.prompt)
}
func (fs *FakeShell) RecordExec(input, output string) {
fs.stats.recording.AddInputEvent(fs.prompt + input)
fs.writer.WriteLn(output)
fs.stats.recording.AddOutputEvent(output)
}
func (fs *FakeShell) RecordWriteLn(output string) {
fs.writer.WriteLn(output)
fs.stats.recording.AddOutputEvent(output)
}
func (fs *FakeShell) RecordWrite(output string) {
fs.writer.Write(output)
// TODO do we need to record this separately?
fs.stats.recording.AddOutputEvent(output)
}
// WriteBinary writes a binary value.
// Unlike the Record* functions it does not record this in the session capture.
func (fs *FakeShell) WriteBinary(val int) {
fs.writer.Write(string(val))
}
// WriteBinary writes a binary value and sends it.
// Unlike the Record* functions it does not record this in the session capture.
func (fs *FakeShell) WriteBinaryLn(val int) {
fs.writer.WriteLn(string(val))
}
// ReadBytes reads and returns a byte array with the given number of bytes from the SSH session.
func (fs *FakeShell) ReadBytes(numBytes int) ([]byte, error) {
b := make([]byte, numBytes)
_, err := (*fs.session).Read(b)
return b, err
}
// ReadString reads and returns a string with the given length from the SSH session.
func (fs *FakeShell) ReadString(length int) (string, error) {
b, err := fs.ReadBytes(length)
if err != nil {
return "", err
}
return string(b), nil
}
// ReadBytesUntil reads from the SSH session until it encounters the separator byte.
// The separator byte will be discarded and the read bytes will be returned as byte array.
func (fs *FakeShell) ReadBytesUntil(sep byte) ([]byte, error) {
bytes := []byte{}
for {
b, err := fs.ReadBytes(1)
if err != nil {
return nil, err
}
if b[0] != sep {
bytes = append(bytes, b[0])
} else {
break
}
}
return bytes, nil
}
// ReadBytesUntilEOF reads from the SSH session until it encounters an error or EOF.
// It will not read more bytes than the given maxBytes.
// The read bytes will be returned as byte array.
func (fs *FakeShell) ReadBytesUntilEOF(maxBytes int) ([]byte, error) {
bytes := []byte{}
i := 0
for {
if i >= maxBytes {
break
}
b, err := fs.ReadBytes(1)
if err != nil {
if err.Error() == "EOF" {
break
}
return nil, err
}
bytes = append(bytes, b[0])
i++
}
return bytes, nil
}
func (fs *FakeShell) Exec(line string, s *Session, iSeq, lSeq int) bool {
fs.stats.AddCommandToHistory(line)
line = string(regexEnvVarPrefixes.ReplaceAll([]byte(line), []byte("$1")))
pieces := strings.Split(line, " ")
command := pieces[0]
args := pieces[1:]
rmtH, rmtP := gutils.SplitHostPortFromAddr((*fs.session).RemoteAddr())
lclH, lclP := gutils.SplitHostPortFromAddr((*fs.session).LocalAddr())
cmd := fmt.Sprintf("%s %s", glog.Reason(command), glog.Wrap(strings.Join(args, " "), glog.LightBlue))
if lSeq > 1 {
cmd = fmt.Sprintf("(%s/%s) %s", glog.Int(iSeq), glog.Int(lSeq), cmd)
}
s.UpdateActivity()
fs.logger.Info("%s: %s", s.LogID(), cmd)
defer func() {
s.UpdateActivity()
}()
if !s.Whitelisted {
// 1) make sure the client waits some time at least,
// the more input the more wait time, hehe
dly := len(line) * int(Conf.InputDelay)
gutils.RandomSleep(dly, dly*2, time.Millisecond)
}
// Ignore just pressing enter with whitespace
if strings.TrimSpace(line) == "" {
return false
}
// 3) check if command should exit immediately
for _, cmd := range Conf.Commands.Exit {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, gutils.GeneratePseudoEmptyString(0)) // just to waste some more time ;)
return true
}
}
SrvMetrics.IncrementExecutedCommands()
data := struct {
User string
IP string
IPLocal string
Port int
PortLocal int
HostName string
InputRaw string
Command string
Arguments []string
}{
User: s.User,
IP: rmtH,
IPLocal: lclH,
Port: rmtP,
PortLocal: lclP,
HostName: Conf.HostName,
InputRaw: line,
Command: command,
Arguments: args,
}
// 3) check if command matches a simple command
for _, cmd := range Conf.Commands.Simple {
if strings.HasPrefix(line+" ", cmd[0]+" ") {
fs.RecordExec(line, ParseTemplateFromString(cmd[1], data))
return false
}
}
// 4) check if command should return permission denied error
for _, cmd := range Conf.Commands.PermissionDenied {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, ParseTemplateFromString("{{ .Command }}: permission denied", data))
return false
}
}
// 5) check if command should return disk i/o error
for _, cmd := range Conf.Commands.DiskError {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, ParseTemplateFromString(gutils.GenerateGarbageString(1000)+"\nend_request: I/O error", data))
return false
}
}
// 6) check if command should return command not found error
for _, cmd := range Conf.Commands.CommandNotFound {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, ParseTemplateFromString("{{ .Command }}: command not found", data))
return false
}
}
// 7) check if command should return file not found error
for _, cmd := range Conf.Commands.FileNotFound {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, ParseTemplateFromString("\"{{ .Command }}\": No such file or directory (os error 2)", data))
return false
}
}
// 8) check if command should return not implemented error
for _, cmd := range Conf.Commands.NotImplemented {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, ParseTemplateFromString("{{ .Command }}: Function not implemented", data))
return false
}
}
// 9) check if command should return bullshit
for _, cmd := range Conf.Commands.Bullshit {
if strings.HasPrefix(line+" ", cmd+" ") {
fs.RecordExec(line, gutils.GenerateGarbageString(1000))
return false
}
}
instr := strings.TrimSpace(line)
instrCmd := strings.Split(instr, " ")[0]
// 10) check if there is a go-implemented command for this
if goCmd, found := CmdLookup[instrCmd]; found {
return goCmd(fs, instr)
}
// 11) check if we have a template for the command
fs.RecordExec(line, ParseTemplateToString(command, data))
return false
}
func (fs *FakeShell) HandleInput(s *Session) {
for {
line, err := fs.terminal.ReadLine()
if err != nil {
if err == io.EOF {
break
} else {
panic(err)
}
}
// execute all rewriters
for _, rw := range Conf.Commands.Rewriters {
re := regexp.MustCompile(rw[0])
if re.MatchString(line) {
line = re.ReplaceAllString(line, rw[1])
}
}
lines := strings.Split(line, "\n")
mustExit := false
for i, ln := range lines {
if fs.Exec(ln, s, i+1, len(lines)) {
mustExit = true
break
}
}
if mustExit {
break
}
}
}
func (fs *FakeShell) Process(s *Session) *FakeShellStats {
if (*fs.session).RawCommand() != "" {
// this means the client passed a command along (e.g. with -t/-tt param),
// let's run it and then close the connection.
raw := (*fs.session).RawCommand()
// execute all rewriters
for _, rw := range Conf.Commands.Rewriters {
re := regexp.MustCompile(rw[0])
if re.MatchString(raw) {
raw = re.ReplaceAllString(raw, rw[1])
}
}
commands := strings.Split(raw, "\n")
for i, cmd := range commands {
if fs.Exec(cmd, s, i+1, len(commands)) {
break
}
}
} else {
fs.HandleInput(s)
}
fs.Close()
return fs.stats
}
func NewFakeShell(s *Session) *FakeShell {
fs := &FakeShell{
session: s.SSHSession,
terminal: nil,
writer: nil,
created: time.Now(),
stats: &FakeShellStats{
CommandsExecuted: 0,
CommandHistory: []string{},
Host: "",
User: (*s.SSHSession).User(),
recording: utils.NewASCIICastV2(fakeShellInitialWidth, fakeShellInitialHeight),
},
logger: glog.NewLogger("Fake Shell", glog.OliveGreen, Conf.Debug.FakeShell, logMessageHandler),
}
fs.terminal = term.NewTerminal(*s.SSHSession, "")
fs.writer = utils.NewSlowWriter(Conf.Ratelimit, fs.terminal)
if s.Whitelisted {
fs.writer.SetRatelimit(10000) // set ridiculously high to effectively disable rate limit
}
fs.stats.Host = fs.Host()
fs.cwd = "/home/" + (*s.SSHSession).User()
fs.UpdatePrompt("~")
fs.logger.Debug("%s: Fake shell ready, current working directory: %s", s.LogID(), glog.File(fs.cwd))
return fs
}