forked from P3GLEG/Whaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
397 lines (362 loc) · 9.98 KB
/
main.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// By Pegleg <[email protected]>
package main
import (
"bufio"
"context"
"flag"
"io"
"os"
"regexp"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/fatih/color"
"archive/tar"
"io/ioutil"
"encoding/json"
"errors"
_ "net/http/pprof"
"github.com/buger/jsonparser"
"path/filepath"
"net/url"
"fmt"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
)
const FilePerms = 0700
var filelist = flag.String("f", "", "File containing images to analyze seperated by line")
var verbose = flag.Bool("v", false, "Print all details about the image")
var filter = flag.Bool("filter", true, "Filters filenames that create noise such as" +
" node_modules. Check ignore.go file for more details")
var extractLayers = flag.Bool("x", false, "Save layers to current directory")
var specificVersion = flag.String("sV", "", "Set the docker client ID to a specific version -sV=1.36")
var re *regexp.Regexp
type Manifest struct {
Config string `json:"Config"`
RepoTags []string `json:"RepoTags"`
Layers []string `json:"Layers"`
}
type ProgressDetail struct {
Current int `json:"current"`
Total int `json:"total"`
}
type Status struct {
Status string `json:"status"`
ID string `json:"id"`
ProgressDetail ProgressDetail `json:"progressDetail"`
}
type dockerHist struct {
Created string `json:"created"`
CreatedBy string `json:"created_by"`
EmptyLayer bool `json:"empty_layer"`
LayerID string
Layers []string
}
func printEnvironmentVariables(info types.ImageInspect) {
if len(info.Config.Env) > 0 {
color.White("Environment Variables")
for _, ele := range info.Config.Env {
color.Yellow("|%s", ele)
}
color.White("\n")
}
}
func printPorts(info types.ImageInspect) {
if len(info.Config.ExposedPorts) > 0 {
color.White("Open Ports")
for i := range info.Config.ExposedPorts {
color.Green("|%s", i.Port())
}
color.White("\n")
}
}
func printUserInfo(info types.ImageInspect) {
color.White("Image user")
if len(info.Config.User) == 0 {
color.Red("|%s", "User is root")
} else {
color.Blue("|Image is running as User: %s", info.Config.User)
}
color.White("\n")
}
func analyze(cli *client.Client, imageID string) {
info, _, err := cli.ImageInspectWithRaw(context.Background(), imageID)
if err != nil {
out, err := cli.ImagePull(context.Background(), imageID, types.ImagePullOptions{})
if err != nil {
color.Red(err.Error())
if strings.Contains(err.Error(),"Maximum supported API version is"){
color.Yellow("Use the -sV flag to change your client version. ./whaler -sV=1.36 %s", imageID)
}
return
}
defer out.Close()
fd, isTerminal := term.GetFdInfo(os.Stdout)
if err := jsonmessage.DisplayJSONMessagesStream(out, os.Stdout, fd, isTerminal, nil); err != nil {
fmt.Println(err)
}
if err != nil {
color.Red(err.Error())
return
}
info, _, err = cli.ImageInspectWithRaw(context.Background(), imageID)
if err != nil {
color.Red(err.Error())
return
}
}
color.White("Analyzing %s", imageID)
color.White("Docker Version: %s", info.DockerVersion)
color.White("GraphDriver: %s", info.GraphDriver.Name)
printEnvironmentVariables(info)
printPorts(info)
printUserInfo(info)
err = analyzeImageFilesystem(cli, imageID)
if err != nil {
color.Red("%s", err)
}
}
func analyzeSingleImage(cli *client.Client, imageID string) {
analyze(cli, imageID)
}
func analyzeMultipleImages(cli *client.Client) {
f, _ := os.Open(*filelist)
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
var imageIDs []string
for scanner.Scan() {
imageIDs = append(imageIDs, scanner.Text())
}
f.Close()
for _, imageID := range imageIDs{
analyzeSingleImage(cli, imageID)
}
}
func extractImageLayers(cli *client.Client, imageID string, history []dockerHist) error{
var startAt = 1
if *verbose {
startAt = 0
}
outputDir := filepath.Join(".", url.QueryEscape(imageID))
os.MkdirAll(outputDir, FilePerms)
f, err := os.Create(filepath.Join(outputDir, "mapping.txt"))
if err != nil{
return err
}
var layersToExtract = make(map[string]int)
for i := startAt; i < len(history); i++ { //Skip the first layer as it clutters it
if strings.Contains(history[i].CreatedBy, "ADD") || strings.Contains(history[i].CreatedBy, "COPY") {
layersToExtract[history[i].LayerID] = 1
layerID := strings.Split(history[i].LayerID, "/")[0]
f.WriteString(fmt.Sprintf("%s:%s\n", layerID, history[i].CreatedBy))
}
}
f.Close()
imageStream, err := cli.ImageSave(context.Background(), []string{imageID})
defer imageStream.Close()
if err != nil {
return err
}
tr := tar.NewReader(imageStream)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if _, ok := layersToExtract[hdr.Name]; ok{
layerID := strings.Split(hdr.Name, "/")[0]
os.MkdirAll(filepath.Join(outputDir, layerID), FilePerms)
ttr := tar.NewReader(tr)
for {
hdrr, err := ttr.Next()
if err == io.EOF {
break
}
if err != nil {
color.Red("%s", err)
}
name := hdrr.Name
switch hdrr.Typeflag {
case tar.TypeDir:
os.MkdirAll(filepath.Join(outputDir, layerID, name), FilePerms)
case tar.TypeReg:
data := make([]byte, hdrr.Size)
ttr.Read(data)
ioutil.WriteFile(filepath.Join(outputDir, layerID, name), data, FilePerms)
case tar.TypeSymlink:
/*
Skipping Symlinks as there can be dangerous behavior here
dest := filepath.Join(outputDir, layerID, name)
source := hdrr.Linkname
if _, err := os.Stat(dest); !os.IsNotExist(err) {
color.Red("Refusing to overwrite existing file: %s", dest)
}else {
os.Symlink(source, dest)
}
*/
}
}
}
}
imageStream.Close()
return nil
}
func analyzeImageFilesystem(cli *client.Client, imageID string) (error) {
imageStream, err := cli.ImageSave(context.Background(), []string{imageID})
if err != nil {
return err
}
tr := tar.NewReader(imageStream)
var configs []Manifest
var hist []dockerHist
var layers = make(map[string][]string)
color.White("Potential secrets:")
for {
imageFile, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if strings.Contains(imageFile.Name, ".json") && imageFile.Name != "manifest.json" {
jsonBytes, _ := ioutil.ReadAll(tr)
h, _, _, _ := jsonparser.Get(jsonBytes, "history")
err = json.Unmarshal(h, &hist)
if err != nil {
return errors.New("unable to parse history from json file ")
}
}
if imageFile.Name == "manifest.json" { //This file contains the sorted order of layers by the commands executed
byteValue, _ := ioutil.ReadAll(tr)
err = json.Unmarshal(byteValue, &configs)
if err != nil {
return errors.New("unable to parse manifest.json")
}
}
if strings.Contains(imageFile.Name, "layer.tar") {
ttr := tar.NewReader(tr)
layers[imageFile.Name] = make([]string, 0)
for {
tarLayerFile, err := ttr.Next()
if err == io.EOF {
break
}
if err != nil {
color.Red("%s", err)
}
layers[imageFile.Name] = append(layers[imageFile.Name], tarLayerFile.Name)
match := re.Find([]byte(tarLayerFile.Name))
if match == nil {
scanFilename(tarLayerFile.Name, imageFile.Name)
}
}
}
}
layerIndex := 0
result := hist[:0]
for _, i := range hist {
if !i.EmptyLayer {
i.LayerID = configs[0].Layers[layerIndex]
i.Layers = layers[i.LayerID]
layerIndex++
result = append(result, i)
} else {
result = append(result, i)
}
}
if layerIndex != len(configs[0].Layers) {
return errors.New("layers should always be 1:1 with commands")
}
imageStream.Close()
printResults(result)
if *extractLayers {
err = extractImageLayers(cli, imageID, result)
if err != nil{
return err
}
}
return nil
}
func printResults(layers []dockerHist) {
color.White("Dockerfile:")
if *verbose {
for i := 0; i < len(layers); i++ {
color.Green("%s\n", cleanString(layers[i].CreatedBy))
for _, l := range layers[i].Layers {
color.Blue("\t%s", l)
}
}
} else {
for i := 1; i < len(layers); i++ {
color.Green("%s\n", cleanString(layers[i].CreatedBy))
if strings.Contains(layers[i].CreatedBy, "ADD") || strings.Contains(layers[i].CreatedBy, "COPY") {
for _, l := range layers[i].Layers {
if *filter {
match := re.Find([]byte(l))
if match == nil {
color.Green("\t%s", l)
}
} else {
color.Green("\t%s", l)
}
}
color.Green("")
}
}
}
color.White("")
}
func cleanString(str string) string {
s := strings.Join(strings.Fields(str), " ")
s = strings.Replace(s, "&&", " \\\n\t&&", -1)
// if a shell has been set, commands will have '/bin/sh -c #(nop)' at
// the start, yes commands like EXPOSE and LABEL, so strip those out.
// /bin/sh -c #(nop) COPY file:d4375883ed5db...
//
// otherwise lines can start with the default '/bin/sh -c ...' for a
// RUN.
if strings.HasPrefix(s, "/bin/sh -c #(nop) ") {
s = strings.Replace(s, "/bin/sh -c #(nop) ", "", -1)
} else if strings.HasPrefix(s, "RUN /bin/sh -c ") {
// it's just a regular RUN line. We can remove the '/bin/sh -c'
// part as that won't be in the original Dockerfile
s = strings.Replace(s, "/bin/sh -c ", "", -1)
} else if strings.HasPrefix(s, "/bin/sh -c ") {
// in this case, there's no RUN provided, its just bare shell
// command, so replace the sh -c with RUN sh -c emulating a
// Dockerfile
s = strings.Replace(s, "/bin/sh -c ", "RUN ", -1)
}
return s
}
func main() {
var cli *client.Client
var err error
flag.Parse()
re = regexp.MustCompile(strings.Join(InternalWordlist, "|"))
compileSecretPatterns()
if len(*specificVersion) > 0 {
cli, err = client.NewClientWithOpts(client.WithVersion(*specificVersion))
} else{
cli, err = client.NewClientWithOpts()
}
if err != nil {
color.Red(err.Error())
return
}
repo := flag.Arg(0)
if len(*filelist) > 0{
analyzeMultipleImages(cli)
} else if len(repo) > 0 {
imageID := repo
analyzeSingleImage(cli, imageID)
} else {
color.Red("Please provide a repository image to analyze. ./whaler nginx:latest")
return
}
cli.Close()
}