/
main.go
460 lines (398 loc) · 15.6 KB
/
main.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"archive/tar"
"bytes"
"context"
"flag"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"time"
"unicode"
"golang.org/x/crypto/ssh"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
)
func main() {
debug := flag.Bool("debug", false, "Enable debug output.")
outputDir := flag.String("outputdir", "./", "Directory to output session log files to.")
globalSessionId := flag.String("id", "", "Global session id, for log file names etc. Defaults to epoch.")
hostname := flag.String("hostname", "", "Hostname to use in container. Default is container default.")
networkmode := flag.String("networkmode", "none", "Docker network mode to use for containers. Valid options are 'none', 'bridge' or 'host'. Defaults to 'none'. Use with caution!")
sessionTimeout := flag.Int("sessiontimeout", 1800, "Timeout in seconds before closing a session. Default to 1800.")
pcapEnabled := flag.Bool("pcap", false, "Enable packet capture. Could potentially use up a lot of disk space.")
privateKeyFile := flag.String("privatekey", "", "Path to private key for SSH server if providing your own is preferable. If left empty, one will be created for each session.")
sshBindAddress := flag.String("bindaddress", "0.0.0.0:22", "SSH bind address and port in format 'ip:port'. Default is '0.0.0.0:22'")
permitAttempt := flag.Int("permitattempt", 1, "Attempt number to accept connection on. Default is to accept on first.")
saveAlteredFiles := flag.Bool("savefiles", false, "Save altered files. Will create one tarball per SSH session. Defaults to false")
flag.Parse()
logger := log.New(os.Stderr, fmt.Sprintf("%s: ", APP_NAME), log.Ldate|log.Ltime|log.Lshortfile)
if *globalSessionId == "" {
tstr := strconv.Itoa(int(time.Now().Unix()))
globalSessionId = &tstr
}
sessions := make(map[string]*sessionData)
usePcap := *pcapEnabled
if *networkmode != "none" &&
*networkmode != "bridge" &&
*networkmode != "host" {
logger.Println("No valid network mode given.")
os.Exit(ERR_DOCKER_INVALID_NETWORK_MODE)
}
if usePcap && (*networkmode == "none" || *networkmode == "host") {
usePcap = false
logger.Println("WARNING: Disabling packet capture, only available in 'bridge' network mode.")
}
logger.Println("Starting minipot")
logger.Println("Connecting to Docker engine")
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
logger.Println("Error while creating Docker engine client: ", err)
os.Exit(ERR_DOCKER_ENGINE_CLIENT_CREATE)
}
if usePcap {
err := buildPCAPContainer(ctx, cli, *logger)
if err != nil {
logger.Println("Error while building packet capture container: ", err)
os.Exit(ERR_DOCKER_IMAGE_BUILD)
}
}
err = buildPotContainer(ctx, cli, *logger)
if err != nil {
logger.Println("Error while building pot container: ", err)
os.Exit(ERR_DOCKER_IMAGE_BUILD)
}
logger.Println("Build complete")
privateKey, err := createPrivateKey(*privateKeyFile)
if err != nil {
logger.Println("Error while loading or generating private key: ", err)
os.Exit(ERR_PRIVATE_KEY_GETORCREATE)
}
private, err := ssh.ParsePrivateKey(privateKey)
if err != nil {
logger.Println("Failed to parse private key: ", err)
os.Exit(ERR_PRIVATE_KEY_PARSE)
}
logger.Println("Serving SSH")
listener, err := net.Listen("tcp", *sshBindAddress)
if err != nil {
logger.Println("SSH listen failed: ", err)
os.Exit(ERR_SSH_SERVE)
}
for {
nConn, err := listener.Accept()
if err != nil {
logger.Println("SSH accept failed: ", err)
os.Exit(ERR_SSH_ACCEPT)
}
session := sessionData{
MinipotSessionID: *globalSessionId,
GuestEnvHostname: *hostname,
NetworkMode: *networkmode,
sessionTimeout: *sessionTimeout,
PcapEnabled: usePcap,
permitAttempt: *permitAttempt,
saveAlteredFiles: *saveAlteredFiles,
}
config := &ssh.ServerConfig{
PasswordCallback: authCallBackWrapper(&session, sessions, *debug, *logger),
}
config.AddHostKey(private)
logger.Println("New SSH session")
go handleClient(nConn, cli, config, &session, sessions, *outputDir, *debug)
}
}
func handleClient(nConn net.Conn, cli *client.Client, config *ssh.ServerConfig, session *sessionData, sessions map[string]*sessionData, outputDir string, debug bool) {
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
if debug {
log.Println("User failed to login: ", err)
}
session.LoginError = err.Error()
session = sessions[session.SourceIP]
} else {
session = sessions[session.SourceIP]
id := session.sshSessionAttemptNumber
logger := log.New(os.Stderr, fmt.Sprintf("%s (session %s-%s-%d): ", APP_NAME, session.MinipotSessionID, session.ClientSessionId, session.sshSessionAttemptNumber), log.Ldate|log.Ltime|log.Lshortfile)
session.minipotSessionContext, session.minipotSessionCancel = context.WithTimeout(context.Background(), time.Duration(session.sessionTimeout)*time.Second)
newCtx := context.Background()
session.sshSessionContext, session.sshSessionCancel = context.WithCancel(newCtx)
go func() {
time.Sleep(time.Duration(session.sessionTimeout) * time.Second)
session.sshSessionCancel()
session.minipotSessionCancel()
}()
// If container ID is set in session, there should be a container running with that ID.
// Resume container and attach to it.
if session.containerID == "" {
if debug {
logger.Println("Creating container.")
}
// Create a new Docker network for this session, we don't want containers sharing networks
session.networkID = fmt.Sprintf("%s-%s", session.MinipotSessionID, session.ClientSessionId)
_, err = cli.NetworkCreate(context.Background(), session.networkID, types.NetworkCreate{
Attachable: true,
})
if err != nil {
logger.Println("WARNING: Error while creating network. Might exist already, trying to create container anyway.")
}
resp, err := cli.ContainerCreate(context.Background(), &container.Config{
Image: DOCKER_CLIENT_ENV_NAME,
AttachStderr: true,
AttachStdin: true,
Tty: true,
AttachStdout: true,
OpenStdin: true,
Hostname: session.GuestEnvHostname,
Env: append(session.environmentVariables, "USR="+session.User), // This is for the ovveride entrypoint, to create a user
},
&container.HostConfig{
AutoRemove: true,
NetworkMode: container.NetworkMode(session.NetworkMode),
}, nil, nil, "")
if err != nil {
logger.Println("Error while creating container: ", err)
os.Exit(ERR_CONTAINER_CREATE)
}
session.containerID = resp.ID
if session.NetworkMode != "none" {
err = cli.NetworkConnect(context.Background(), session.networkID, session.containerID, &network.EndpointSettings{})
if err != nil {
logger.Println("Error connecting guest container to network: ", err)
os.Exit(ERR_CONTAINER_NETWORK_CONNECT)
}
}
err = cli.ContainerStart(context.Background(), session.containerID, types.ContainerStartOptions{})
if err != nil {
logger.Println("Error while starting container: ", err)
os.Exit(ERR_CONTAINER_START)
}
var pcap = container.ContainerCreateCreatedBody{}
if session.PcapEnabled {
inspection, err := cli.ContainerInspect(context.Background(), session.containerID) // Inspect container so we can get the name
if err != nil {
logger.Println("Could not get container inspect:", err)
}
name := inspection.Name
pcap, err = cli.ContainerCreate(context.Background(), &container.Config{
Image: PCAP_IMAGE,
},
&container.HostConfig{
AutoRemove: true,
NetworkMode: container.NetworkMode("container:" + name), // Connect directly to containers network so stack is shared
}, &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{},
}, nil, "")
if err != nil {
logger.Println("Error while creating container: ", err)
os.Exit(ERR_CONTAINER_CREATE)
}
err = cli.ContainerStart(context.Background(), pcap.ID, types.ContainerStartOptions{})
if err != nil {
logger.Println("Error while starting container: ", err)
os.Exit(ERR_CONTAINER_START)
}
session.pcapContainerID = pcap.ID
}
}
inputChan := make(chan byte)
// Input collector
go func() {
line := ""
for {
b := <-inputChan
// Handle control characters and log them as well
if b == 127 { // DELETE
line = fmt.Sprintf("%s<BACKSPACE>", line)
} else if b == 9 { // TAB
line = fmt.Sprintf("%s<TAB>", line)
} else if b == 13 { // CR, create new log line
i := Input{
Data: line,
Time: time.Now(),
}
session.ClientSessions[id].UserInput = append(session.ClientSessions[id].UserInput, i)
line = ""
} else {
line = fmt.Sprintf("%s%s", line, string(b))
}
}
}()
go func() {
// Save modified file paths
diffs, err := getContainerFileDiff(cli, session.containerID, *logger, debug)
if err != nil {
logger.Println("Error while getting diffs: ", err)
} else {
for _, path := range diffs {
session.ClientSessions[session.sshSessionAttemptNumber].modifiedFilesIgnore = append(session.ClientSessions[session.sshSessionAttemptNumber].modifiedFilesIgnore, path)
}
}
if debug {
logger.Println("Attaching to container")
}
containerAttachOpts := types.ContainerAttachOptions{
Stdin: true,
Stdout: true,
Stderr: true,
Stream: true,
}
cHjResp, err := cli.ContainerAttach(context.Background(), session.containerID, containerAttachOpts)
if err != nil {
logger.Println("Error while attaching to container:", err)
os.Exit(ERR_CONTAINER_ATTACH)
}
go ssh.DiscardRequests(reqs)
startReadChan := make(chan bool) // These are here to pause reading and writing until we have a shell session, we don't want to read on exec
startWriteChan := make(chan bool)
// Start handling requests
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
log.Fatalf("Could not accept channel: %v", err)
}
go func(in <-chan *ssh.Request) {
for req := range in {
payloadStripControl := strings.Map(func(r rune) rune {
if unicode.IsPrint(r) {
return r
}
return -1
}, string(req.Payload))
if debug {
logger.Println("New SSH request of type: ", req.Type)
logger.Println("Request payload: ", payloadStripControl)
logger.Println("Want reply:", req.WantReply)
}
request := sshRequest{
Type: req.Type,
Payload: payloadStripControl,
}
session.ClientSessions[session.sshSessionAttemptNumber].SSHRequests = append(session.ClientSessions[session.sshSessionAttemptNumber].SSHRequests, request)
switch req.Type {
case "shell":
req.Reply(true, nil)
go handleContainerInput(channel, cHjResp, session, *logger, startReadChan, startWriteChan, inputChan)
go handleSSHInput(channel, cHjResp, session, *logger, startReadChan, startWriteChan, inputChan)
startReadChan <- true // Pause reading container output, we don't want to read anything before this
startWriteChan <- true // Pause writing to container, we don't want to write anything before this
case "exec":
args := strings.Split(payloadStripControl, " ")
cResp, err := cli.ContainerExecCreate(context.Background(), session.containerID, types.ExecConfig{
User: session.User,
Cmd: args,
AttachStdout: true,
AttachStderr: false,
AttachStdin: true,
Detach: false,
Tty: false,
})
if err == nil {
execResp, err := cli.ContainerExecAttach(context.Background(), cResp.ID, types.ExecStartCheck{})
if err != nil {
logger.Println("Error while attaching to exec: ", err)
}
req.Reply(true, nil)
go handleContainerExecInput(channel, execResp, session, *logger, startReadChan, startWriteChan, inputChan)
go handleSSHExecInput(channel, execResp, session, *logger, startReadChan, startWriteChan, inputChan)
startReadChan <- true // Pause reading container output, we don't want to read anything before this
startWriteChan <- true // Pause writing to container, we don't want to write anything before this
} else {
logger.Println("Error while creating exec: ", err)
err = req.Reply(false, nil)
if err != nil {
logger.Println("Error while sending request reply:", err)
}
}
default:
logger.Println("Unknown request: ", req.Type)
}
}
}(requests)
}
}()
<-session.sshSessionContext.Done() // Something cancelled the SSH session
session.activeSSHSession = false
logger.Printf("SSH session ended\n")
// Save modified file paths
diffs, err := getContainerFileDiff(cli, session.containerID, *logger, debug)
if err != nil {
logger.Println("Error while getting diffs: ", err)
} else {
session.ClientSessions[session.sshSessionAttemptNumber].ModifiedFiles = append(session.ClientSessions[session.sshSessionAttemptNumber].ModifiedFiles, diffs...)
}
session.ClientSessions[session.sshSessionAttemptNumber].ModifiedFiles = session.removeIgnoredModifiedFiles()
if session.saveAlteredFiles {
logger.Println("Saving modified files.")
err = session.createModifiedFilesTar(cli)
if err != nil {
logger.Println("Error while creating modified files tar: ", err)
}
}
<-session.minipotSessionContext.Done() // Wait for minipot session to end
if id == 0 {
logger.Printf("Writing log\n")
err = session.createLog(outputDir) // Create text log
if err != nil {
logger.Println("WARNING: Error while writing log: ", err)
}
err = session.createJsonLog(outputDir) // JSON log
if err != nil {
logger.Println("WARNING: Error while writing JSON log: ", err)
}
if debug {
logger.Printf("Waiting for Minipot session end before killing container.\n")
}
if debug {
logger.Printf("Minipot session ended, cleaning up.\n")
}
// TODO all this needs to be done in separate files for each session
if session.PcapEnabled {
logger.Printf("Getting PCAP data\n")
ior, _, err := cli.CopyFromContainer(context.Background(), session.pcapContainerID, "/session.pcap") // Copy PCAP file, comes as TAR archive
if err != nil {
logger.Println("WARNING: Error getting PCAP data: ", err)
} else {
defer ior.Close()
// Get data from TAR archive
tarReader := tar.NewReader(ior)
tarReader.Next()
buf := new(bytes.Buffer)
buf.ReadFrom(tarReader)
err = session.createPCAPFile(outputDir, buf.Bytes()) // Create PCAP file in log dir
if err != nil {
logger.Println("WARNING: Error creating PCAP file: ", err)
}
}
// Cleanup PCAP
logger.Printf("Killing PCAP container\n")
err = cli.ContainerKill(context.Background(), session.pcapContainerID, "SIGKILL")
if err != nil {
logger.Println("Error while killing container: ", err)
}
}
err = cli.ContainerKill(context.Background(), session.containerID, "SIGKILL")
if err != nil {
logger.Println("WARNING: Error while killing container: ", err)
}
logger.Println("Removing network.")
err = cli.NetworkRemove(context.Background(), session.networkID)
if err != nil {
logger.Println("WARNING: error while removing network: ", err)
}
delete(sessions, session.SourceIP)
}
}
nConn.Close()
log.Printf("SSH session ended\n")
}