-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathprocess_image.go
More file actions
536 lines (473 loc) · 15.8 KB
/
Copy pathprocess_image.go
File metadata and controls
536 lines (473 loc) · 15.8 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package scan
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"syscall"
"fmt"
"github.com/deepfence/SecretScanner/core"
"github.com/deepfence/SecretScanner/output"
"github.com/deepfence/SecretScanner/signature"
"github.com/deepfence/vessel"
)
// Data type to store details about the container image after parsing manifest
type manifestItem struct {
Config string
RepoTags []string
Layers []string
LayerIds []string `json:",omitempty"`
}
var (
imageTarFileName = "save-output.tar"
maxSecretsExceeded = errors.New("number of secrets exceeded max-secrets")
)
type ImageScan struct {
imageName string
imageId string
tempDir string
imageManifest manifestItem
numSecrets uint
}
// Function to retrieve contents of container images layer by layer
// @parameters
// imageScan - Structure with details of the container image to scan
// @returns
// Error - Errors, if any. Otherwise, returns nil
func (imageScan *ImageScan) extractImage(saveImage bool) error {
imageName := imageScan.imageName
tempDir := imageScan.tempDir
imageScan.numSecrets = 0
if saveImage {
err := imageScan.saveImageData()
if err != nil {
core.GetSession().Log.Error("scanImage: Could not save container image: %s. Check if the image name is correct.", err)
return err
}
}
_, err := extractTarFile(imageName, path.Join(tempDir, imageTarFileName), tempDir)
if err != nil {
core.GetSession().Log.Error("scanImage: Could not extract image tar file: %s", err)
return err
}
imageManifest, err := extractDetailsFromManifest(tempDir)
if err != nil {
core.GetSession().Log.Error("ProcessImageLayers: Could not get image's history: %s,"+
" please specify repo:tag and check disk space \n", err.Error())
return err
}
imageScan.imageManifest = imageManifest
// reading image id from imanifest file json path and tripping off extension
imageScan.imageId = strings.TrimSuffix(imageScan.imageManifest.Config, ".json")
return nil
}
// Function to scan extracted layers of container images for secrets file by file
// @parameters
// imageScan - Structure with details of the container image to scan
// @returns
// []output.SecretFound - List of all secrets found
// Error - Errors, if any. Otherwise, returns nil
func (imageScan *ImageScan) scan() ([]output.SecretFound, error) {
tempDir := imageScan.tempDir
defer core.DeleteTmpDir(tempDir)
tempSecretsFound, err := imageScan.processImageLayers(tempDir)
if err != nil {
core.GetSession().Log.Error("scanImage: %s", err)
return tempSecretsFound, err
}
return tempSecretsFound, nil
}
// ScanSecretsInDir Scans a given directory recursively to find all secrets inside any file in the dir
// @parameters
// layer - layer ID, if we are scanning directory inside container image
// baseDir - Parent directory
// fullDir - Complete path of the directory to be scanned
// isFirstSecret - indicates if some secrets are already printed, used to properly format json
// @returns
// []output.SecretFound - List of all secrets found
// Error - Errors if any. Otherwise, returns nil
func ScanSecretsInDir(layer string, baseDir string, fullDir string, isFirstSecret *bool,
numSecrets *uint, matchedRuleSet map[uint]uint) ([]output.SecretFound, error) {
var tempSecretsFound []output.SecretFound
if matchedRuleSet == nil {
matchedRuleSet = make(map[uint]uint)
}
if layer != "" {
core.UpdateDirsPermissionsRW(fullDir)
}
session := core.GetSession()
maxFileSize := *session.Options.MaximumFileSize * 1024
var file core.MatchFile
var relPath string
var contents []byte
var secrets []output.SecretFound
walkErr := filepath.Walk(fullDir, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
var scanDirPath string
if layer != "" {
scanDirPath = strings.TrimPrefix(path, baseDir+"/"+layer)
if scanDirPath == "" {
scanDirPath = "/"
}
} else {
scanDirPath = path
}
if f.IsDir() {
if core.IsSkippableDir(scanDirPath, baseDir) {
return filepath.SkipDir
}
return nil
}
if uint(f.Size()) > maxFileSize || core.IsSkippableFileExtension(path) {
return nil
}
// No need to scan sym links. This avoids hangs when scanning stderr, stdour or special file descriptors
// Also, the pointed files will anyway be scanned directly
if core.IsSymLink(path) {
return nil
}
file = core.NewMatchFile(path)
relPath, err = filepath.Rel(filepath.Join(baseDir, layer), file.Path)
if err != nil {
session.Log.Warn("scanSecretsInDir: Couldn't remove prefix of path: %s %s %s",
baseDir, layer, file.Path)
relPath = file.Path
}
// Add RW permissions for reading and deleting contents of containers, not for regular file system
if layer != "" {
err = os.Chmod(file.Path, 0600)
if err != nil {
session.Log.Error("scanSecretsInDir changine file permission: %s", err)
}
}
contents, err = ioutil.ReadFile(file.Path)
if err != nil {
session.Log.Error("scanSecretsInDir reading file: %s", err)
// return tempSecretsFound, err
} else {
// fmt.Println(relPath, file.Filename, file.Extension, layer)
secrets, err = signature.MatchPatternSignatures(contents, relPath, file.Filename, file.Extension,
layer, numSecrets, matchedRuleSet)
if err != nil {
session.Log.Info("relPath: %s, Filename: %s, Extension: %s, layer: %s",
relPath, file.Filename, file.Extension, layer)
session.Log.Error("scanSecretsInDir: %s", err)
// return tempSecretsFound, err
}
if !(*session.Options.Quiet) {
output.PrintColoredSecrets(secrets, isFirstSecret)
}
tempSecretsFound = append(tempSecretsFound, secrets...)
}
secrets = signature.MatchSimpleSignatures(relPath, file.Filename, file.Extension, layer, numSecrets)
if !(*session.Options.Quiet) {
output.PrintColoredSecrets(secrets, isFirstSecret)
}
tempSecretsFound = append(tempSecretsFound, secrets...)
// Don't report secrets if number of secrets exceeds MAX value
if *numSecrets >= *session.Options.MaxSecrets {
return maxSecretsExceeded
}
return nil
})
if walkErr != nil {
if walkErr == maxSecretsExceeded {
session.Log.Warn("filepath.Walk: %s", walkErr)
fmt.Printf("filepath.Walk: %s\n", walkErr)
} else {
session.Log.Error("Error in filepath.Walk: %s", walkErr)
fmt.Printf("Error in filepath.Walk: %s\n", walkErr)
}
}
return tempSecretsFound, nil
}
// Extract all the layers of the container image and then find secrets in each layer one by one
// @parameters
// imageScan - Structure with details of the container image to scan
// imageManifestPath - Complete path of directory where manifest of image has been extracted
// @returns
// []output.SecretFound - List of all secrets found
// Error - Errors if any. Otherwise, returns nil
func (imageScan *ImageScan) processImageLayers(imageManifestPath string) ([]output.SecretFound, error) {
var tempSecretsFound []output.SecretFound
var err error
var isFirstSecret bool = true
// extractPath - Base directory where all the layers should be extracted to
extractPath := path.Join(imageManifestPath, core.ExtractedImageFilesDir)
layerIDs := imageScan.imageManifest.LayerIds
layerPaths := imageScan.imageManifest.Layers
matchedRuleSet := make(map[uint]uint)
loopCntr := len(layerPaths)
var secrets []output.SecretFound
for i := 0; i < loopCntr; i++ {
core.GetSession().Log.Debug("Analyzing layer path: %s", layerPaths[i])
core.GetSession().Log.Debug("Analyzing layer: %s", layerIDs[i])
// savelayerID = layerIDs[i]
completeLayerPath := path.Join(imageManifestPath, layerPaths[i])
targetDir := path.Join(extractPath, layerIDs[i])
core.GetSession().Log.Info("Complete layer path: %s", completeLayerPath)
core.GetSession().Log.Info("Extracted to directory: %s", targetDir)
err = core.CreateRecursiveDir(targetDir)
if err != nil {
core.GetSession().Log.Error("ProcessImageLayers: Unable to create target directory"+
" to extract image layers... %s", err)
return tempSecretsFound, err
}
_, error := extractTarFile("", completeLayerPath, targetDir)
if error != nil {
core.GetSession().Log.Error("ProcessImageLayers: Unable to extract image layer. Reason = %s", error.Error())
// Don't stop. Print error and continue with remaning extracted files and other layers
// return tempSecretsFound, error
}
core.GetSession().Log.Debug("Analyzing dir: %s", targetDir)
secrets, err = ScanSecretsInDir(layerIDs[i], extractPath, targetDir, &isFirstSecret, &imageScan.numSecrets, matchedRuleSet)
tempSecretsFound = append(tempSecretsFound, secrets...)
if err != nil {
core.GetSession().Log.Error("ProcessImageLayers: %s", err)
// return tempSecretsFound, err
}
// Don't report secrets if number of secrets exceeds MAX value
if imageScan.numSecrets >= *core.GetSession().Options.MaxSecrets {
return tempSecretsFound, nil
}
}
return tempSecretsFound, nil
}
// Save container image as tar file in specified directory
// @parameters
// imageScan - Structure with details of the container image to scan
// @returns
// Error - Errors if any. Otherwise, returns nil
func (imageScan *ImageScan) saveImageData() error {
imageName := imageScan.imageName
outputParam := path.Join(imageScan.tempDir, imageTarFileName)
drun, err := vessel.NewRuntime()
if err != nil {
return err
}
fmt.Printf("Scanning image %s for secrets...\n", outputParam)
_, err = drun.Save(imageName, outputParam)
if err != nil {
return err
}
core.GetSession().Log.Info("Image %s saved in %s", imageName, imageScan.tempDir)
return nil
}
// Extract the contents of container image and save it in specified dir
// @parameters
// imageName - Name of the container image to save
// imageTarPath - Complete path where tarball of the image is stored
// extractPath - Complete path of directory where contents of image are to be extracted
// @returns
// string - directory where contents of image are extracted
// Error - Errors, if any. Otherwise, returns nil
func extractTarFile(imageName, imageTarPath string, extractPath string) (string, error) {
core.GetSession().Log.Debug("Started extracting tar file %s", imageTarPath)
path := extractPath
// Extract the contents of image from tar file
if err := untar(imageTarPath, path); err != nil {
fmt.Println(err)
return "", err
}
core.GetSession().Log.Debug("Finished extracting tar file %s", imageTarPath)
return path, nil
}
// Extract all the details from image manifest
// @parameters
// path - Complete path where image contents are extracted
// @returns
// manifestItem - The manifestItem containing details about image layers
// Error - Errors, if any. Otherwise, returns nil
func untar(tarName string, xpath string) (err error) {
tarFile, err := os.Open(tarName)
if err != nil {
return err
}
defer func() {
err = tarFile.Close()
}()
absPath, err := filepath.Abs(xpath)
if err != nil {
return err
}
tr := tar.NewReader(tarFile)
if strings.HasSuffix(tarName, ".gz") || strings.HasSuffix(tarName, ".gzip") {
gz, err := gzip.NewReader(tarFile)
if err != nil {
return err
}
defer gz.Close()
tr = tar.NewReader(gz)
}
// untar each segment
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
// determine proper file path info
finfo := hdr.FileInfo()
fileName := hdr.Name
if filepath.IsAbs(fileName) {
fileName, err = filepath.Rel("/", fileName)
if err != nil {
return err
}
}
absFileName := filepath.Join(absPath, fileName)
if strings.Contains(fileName, "/") {
relPath := strings.Split(fileName, "/")
var absDirPath string
if len(relPath) > 1 {
dirs := relPath[0 : len(relPath)-1]
absDirPath = filepath.Join(absPath, strings.Join(dirs, "/"))
}
if err := os.MkdirAll(absDirPath, 0755); err != nil {
fmt.Println(err.Error())
}
}
if finfo.Mode().IsDir() {
if err := os.MkdirAll(absFileName, 0755); err != nil {
return err
}
continue
}
// create new file with original file mode
file, err := os.OpenFile(absFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, finfo.Mode().Perm())
if err != nil {
fmt.Println(err.Error())
return err
}
// fmt.Printf("x %s\n", absFileName)
n, cpErr := io.Copy(file, tr)
if closeErr := file.Close(); closeErr != nil { // close file immediately
fmt.Println("clserr:" + closeErr.Error())
return err
}
if cpErr != nil {
fmt.Println("copyErr:" + cpErr.Error())
return cpErr
}
if n != finfo.Size() {
return fmt.Errorf("unexpected bytes written: wrote %d, want %d", n, finfo.Size())
}
}
return nil
}
// Extract all the details from image manifest
// @parameters
// path - Complete path where image contents are extracted
// @returns
// manifestItem - The manifestItem containing details about image layers
// Error - Errors, if any. Otherwise, returns nil
func extractDetailsFromManifest(path string) (manifestItem, error) {
mf, err := os.Open(path + "/manifest.json")
if err != nil {
return manifestItem{}, err
}
defer mf.Close()
var manifest []manifestItem
if err = json.NewDecoder(mf).Decode(&manifest); err != nil {
return manifestItem{}, err
} else if len(manifest) != 1 {
return manifestItem{}, err
}
var layerIds []string
for _, layer := range manifest[0].Layers {
trimmedLayerId := strings.TrimSuffix(layer, "/layer.tar")
// manifests saved by some versions of skopeo has .tar extentions
trimmedLayerId = strings.TrimSuffix(trimmedLayerId, ".tar")
layerIds = append(layerIds, trimmedLayerId)
}
manifest[0].LayerIds = layerIds
// ImageScan.imageManifest = manifest[0]
return manifest[0], nil
}
// Execute the specified command and return the output
// @parameters
// name - Command to be executed
// args - all the arguments to be passed to the command
// @returns
// string - contents of standard output
// string - contents of standard error
// int - exit code of the executed command
func runCommand(name string, args ...string) (stdout string, stderr string, exitCode int) {
var defaultFailedCode = 1
var outbuf, errbuf bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Run()
stdout = outbuf.String()
stderr = errbuf.String()
if err != nil {
// try to get the exit code
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
} else {
// This will happen (in OSX) if `name` is not available in $PATH,
// in this situation, exit code could not be get, and stderr will be
// empty string very likely, so we use the default fail code, and format err
// to string and set to stderr
log.Printf("Could not get exit code for failed program: %v, %v", name, args)
exitCode = defaultFailedCode
if stderr == "" {
stderr = err.Error()
}
}
} else {
// success, exitCode should be 0 if go is ok
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
return
}
type ImageExtractionResult struct {
Secrets []output.SecretFound
ImageId string
}
func ExtractAndScanImage(image string) (*ImageExtractionResult, error) {
tempDir, err := core.GetTmpDir(image)
if err != nil {
return nil, err
}
// defer core.DeleteTmpDir(tempDir)
imageScan := ImageScan{imageName: image, imageId: "", tempDir: tempDir}
err = imageScan.extractImage(true)
if err != nil {
return nil, err
}
secrets, err := imageScan.scan()
if err != nil {
return nil, err
}
return &ImageExtractionResult{ImageId: imageScan.imageId, Secrets: secrets}, nil
}
func ExtractAndScanFromTar(tarFolder string, imageName string) (*ImageExtractionResult, error) {
// defer core.DeleteTmpDir(tarFolder)
imageScan := ImageScan{imageName: imageName, imageId: "", tempDir: tarFolder}
err := imageScan.extractImage(false)
if err != nil {
return nil, err
}
secrets, err := imageScan.scan()
if err != nil {
return nil, err
}
return &ImageExtractionResult{ImageId: imageScan.imageId, Secrets: secrets}, nil
}