-
Notifications
You must be signed in to change notification settings - Fork 9
/
sys.go
275 lines (236 loc) · 6.25 KB
/
sys.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
package os
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
pathlib "path"
"github.com/mkishere/sshsyrup/util/termlogger"
log "github.com/sirupsen/logrus"
"github.com/spf13/afero"
"golang.org/x/crypto/ssh"
)
var (
crlf = []byte{'\r', '\n'}
)
var (
funcMap = make(map[string]Command)
fakeFuncList = make(map[string]string)
)
var (
errMsgList = map[string]struct{}{
"Segmentation fault": struct{}{},
"Permission denied": struct{}{},
}
)
// Command interface allow classes to simulate real executable
// that have access to standard I/O, filesystem, arguments, EnvVars,
// and cwd
type Command interface {
GetHelp() string
Exec(args []string, sys Sys) int
Where() string
}
// System provides what most of os/sys does in the honeyport
type System struct {
userId int
cwd string
fSys afero.Fs
sshChan ssh.Channel
envVars map[string]string
width, height int
log *log.Entry
sessionLog termlogger.LogHook
hostName string
}
type Sys interface {
Getcwd() string
Chdir(path string) error
In() io.Reader
Out() io.Writer
Err() io.Writer
Environ() (env []string)
SetEnv(key, value string) error
FSys() afero.Fs
Width() int
Height() int
CurrentUser() int
CurrentGroup() int
Hostname() string
}
type stdoutWrapper struct {
io.Writer
}
type sysLogWrapper struct {
termlogger.StdIOErr
*System
}
func (sys *sysLogWrapper) In() io.Reader { return sys.StdIOErr.In() }
func (sys *sysLogWrapper) Out() io.Writer { return stdoutWrapper{sys.StdIOErr.Out()} }
func (sys *sysLogWrapper) Err() io.Writer { return stdoutWrapper{sys.StdIOErr.Err()} }
// NewSystem initializer a system object containing current user context: ID,
// home directory, terminal dimensions, etc.
func NewSystem(user, host string, fs afero.Fs, channel ssh.Channel, width, height int, log *log.Entry) *System {
if _, exists := IsUserExist(user); !exists {
CreateUser(user, "password")
}
aferoFs := afero.Afero{fs}
if exists, _ := aferoFs.DirExists(usernameMapping[user].Homedir); !exists {
aferoFs.MkdirAll(usernameMapping[user].Homedir, 0755)
}
return &System{
cwd: usernameMapping[user].Homedir,
fSys: aferoFs,
envVars: map[string]string{},
sshChan: channel,
width: width,
height: height,
log: log,
userId: usernameMapping[user].UID,
hostName: host,
}
}
// Getcwd gets current working directory
func (sys *System) Getcwd() string {
return sys.cwd
}
// Chdir change current working directory
func (sys *System) Chdir(path string) error {
if !pathlib.IsAbs(path) {
path = sys.cwd + "/" + path
}
path = pathlib.Clean(path)
if exists, err := afero.DirExists(sys.fSys, path); err == nil && !exists {
return os.ErrNotExist
} else if err != nil {
return err
}
sys.cwd = path
return nil
}
func (sys *System) CurrentUser() int { return sys.userId }
func (sys *System) CurrentGroup() int {
u := GetUserByID(sys.userId)
return u.GID
}
func (sys *System) Hostname() string {
return sys.hostName
}
// In returns a io.Reader that represent stdin
func (sys *System) In() io.Reader { return sys.sshChan }
// Out returns a io.Writer that represent stdout
func (sys *System) Out() io.Writer {
return stdoutWrapper{sys.sshChan}
}
func (sys *System) Err() io.Writer {
return stdoutWrapper{sys.sshChan.Stderr()}
}
func (sys *System) IOStream() io.ReadWriter { return sys.sshChan }
func (sys *System) FSys() afero.Fs { return sys.fSys }
func (sys *System) Width() int { return sys.width }
func (sys *System) Height() int { return sys.height }
// Write replace \n with \r\n before writing to the underlying io.Writer.
// Copied from golang.org/x/crypto/ssh/terminal
func (sw stdoutWrapper) Write(buf []byte) (n int, err error) {
for len(buf) > 0 {
i := bytes.IndexByte(buf, '\n')
todo := len(buf)
if i >= 0 {
todo = i
}
var nn int
nn, err = sw.Writer.Write(buf[:todo])
n += nn
if err != nil {
return n, err
}
buf = buf[todo:]
if i >= 0 {
if _, err = sw.Writer.Write(crlf); err != nil {
return n, err
}
n++
buf = buf[1:]
}
}
return n, nil
}
func (sys *System) Environ() (env []string) {
env = make([]string, 0, len(sys.envVars))
for k, v := range sys.envVars {
env = append(env, fmt.Sprintf("%v=%v", k, v))
}
return
}
func (sys *System) SetEnv(key, value string) error {
sys.envVars[key] = value
return nil
}
func (sys *System) Exec(path string, args []string) (int, error) {
return sys.exec(path, args, nil)
}
func (sys *System) exec(path string, args []string, io termlogger.StdIOErr) (int, error) {
cmd := pathlib.Base(path)
if execFunc, ok := funcMap[cmd]; ok {
defer func() {
if r := recover(); r != nil {
sys.log.WithFields(log.Fields{
"cmd": path,
"args": args,
"error": r,
}).Error("Command has crashed")
sys.Err().Write([]byte("Segmentation fault\n"))
}
}()
var res int
// If logger is not nil, redirect IO to it
if io != nil {
loggedSys := &sysLogWrapper{io, sys}
res = execFunc.Exec(args, loggedSys)
} else {
res = execFunc.Exec(args, sys)
}
return res, nil
} else if output, inList := fakeFuncList[cmd]; inList {
// Print random error message
// Make use of golang map random nature :)
if len(output) == 0 {
return printRandomError(sys)
}
// Read file and write output
content, err := ioutil.ReadFile(output)
if err != nil {
return printRandomError(sys)
}
sys.Out().Write(content)
return 0, nil
}
return 127, &os.PathError{Op: "exec", Path: path, Err: os.ErrNotExist}
}
// RegisterCommand puts the command implementation into map so
// it can be invoked from command line
func RegisterCommand(name string, cmd Command) {
funcMap[name] = cmd
funcMap[cmd.Where()] = cmd
}
// RegisterFakeCommand put commands into register so that when
// typed in terminal they will print out SegFault
func RegisterFakeCommand(cmdList []string) {
for i := range cmdList {
fakeFuncList[cmdList[i]] = ""
}
}
// RegisterCommandOutput gets the file content and associate
// it with the command provided. So that once triggered in
// console the content will be displayed
func RegisterCommandOutput(cmd, pathToOutput string) {
fakeFuncList[cmd] = pathToOutput
}
func printRandomError(sys *System) (int, error) {
for msg := range errMsgList {
sys.Err().Write([]byte(msg + "\n"))
break
}
return 1, nil
}