forked from genuinetools/amicontained
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-arm64.go
192 lines (166 loc) · 4.16 KB
/
main-arm64.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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"runtime"
//"github.com/genuinetools/amicontained/version"
"github.com/genuinetools/pkg/cli"
"github.com/jessfraz/bpfd/proc"
"github.com/sirupsen/logrus"
"github.com/tv42/httpunix"
//"golang.org/x/sys/unix"
)
var (
debug bool
docker_sock_hunt bool
)
func main() {
// Create a new cli program.
p := cli.NewProgram()
p.Name = "amicontained"
p.Description = "A container introspection tool"
// Set the GitCommit and Version.
//p.GitCommit = version.GITCOMMIT
//p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("ship", flag.ExitOnError)
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
p.FlagSet.BoolVar(&docker_sock_hunt, "s", false, "enable docker sock hunting")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
// Set the main program action.
p.Action = func(ctx context.Context, args []string) error {
goarch := runtime.GOARCH
fmt.Printf("GOARCH: %s\n", goarch)
euid := os.Geteuid()
fmt.Printf("Effective UID that executes this binary: %v\n", euid)
// Container Runtime
runtime := proc.GetContainerRuntime(0, 0)
fmt.Printf("Container Runtime: %s\n", runtime)
// Namespaces
namespaces := []string{"pid"}
fmt.Println("Has Namespaces:")
for _, namespace := range namespaces {
ns, err := proc.HasNamespace(namespace)
if err != nil {
fmt.Printf("\t%s: error -> %v\n", namespace, err)
continue
}
fmt.Printf("\t%s: %t\n", namespace, ns)
}
// User Namespaces
userNS, userMappings := proc.GetUserNamespaceInfo(0)
fmt.Printf("\tuser: %t\n", userNS)
if len(userMappings) > 0 {
fmt.Println("User Namespace Mappings:")
for _, userMapping := range userMappings {
fmt.Printf("\tContainer -> %d\tHost -> %d\tRange -> %d\n", userMapping.ContainerID, userMapping.HostID, userMapping.Range)
}
}
// AppArmor Profile
aaprof := proc.GetAppArmorProfile(0)
fmt.Printf("AppArmor Profile: %s\n", aaprof)
// Capabilities
caps, err := proc.GetCapabilities(0)
if err != nil {
logrus.Warnf("getting capabilities failed: %v", err)
}
if len(caps) > 0 {
fmt.Println("Capabilities:")
for k, v := range caps {
if len(v) > 0 {
fmt.Printf("\t%s -> %s\n", k, strings.Join(v, " "))
}
}
}
// Seccomp
seccompMode := proc.GetSeccompEnforcingMode(0)
fmt.Printf("Seccomp: %s\n", seccompMode)
// arm64 broken
// https://github.com/genuinetools/amicontained/pull/15/commits
// seccompIter()
// Docker.sock
if docker_sock_hunt {
fmt.Println("Looking for Docker.sock")
getValidSockets("/")
}
return nil
}
// Run our program.
p.Run()
}
func walkpath(path string, info os.FileInfo, err error) error {
if err != nil {
if debug {
fmt.Println(err)
}
} else {
switch mode := info.Mode(); {
case mode&os.ModeSocket != 0:
if debug {
fmt.Println("Valid Socket: " + path)
}
resp, err := checkSock(path)
if err == nil {
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
fmt.Println("Valid Docker Socket: " + path)
} else {
if debug {
fmt.Println("Invalid Docker Socket: " + path)
}
}
defer resp.Body.Close()
} else {
if debug {
fmt.Println("Invalid Docker Socket: " + path)
}
}
default:
if debug {
fmt.Println("Invalid Socket: " + path)
}
}
}
return nil
}
func getValidSockets(startPath string) ([]string, error) {
err := filepath.Walk(startPath, walkpath)
if err != nil {
if debug {
fmt.Println(err)
}
return nil, err
}
return nil, nil
}
func checkSock(path string) (*http.Response, error) {
if debug {
fmt.Println("[-] Checking Sock for HTTP: " + path)
}
u := &httpunix.Transport{
DialTimeout: 100 * time.Millisecond,
RequestTimeout: 1 * time.Second,
ResponseHeaderTimeout: 1 * time.Second,
}
u.RegisterLocation("dockerd", path)
var client = http.Client{
Transport: u,
}
resp, err := client.Get("http+unix://dockerd/info")
if resp == nil {
return nil, err
}
return resp, nil
}