-
Notifications
You must be signed in to change notification settings - Fork 156
/
process_container.go
207 lines (188 loc) 路 7 KB
/
process_container.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
package scan
import (
"errors"
"fmt"
"os/exec"
"strings"
"github.com/deepfence/YaraHunter/core"
"github.com/deepfence/YaraHunter/pkg/output"
"github.com/deepfence/golang_deepfence_sdk/utils/tasks"
"github.com/deepfence/vessel"
containerdRuntime "github.com/deepfence/vessel/containerd"
crioRuntime "github.com/deepfence/vessel/crio"
dockerRuntime "github.com/deepfence/vessel/docker"
podmanRuntime "github.com/deepfence/vessel/podman"
vesselConstants "github.com/deepfence/vessel/utils"
log "github.com/sirupsen/logrus"
)
type ContainerScan struct {
containerID string
tempDir string
namespace string
}
// Function to retrieve contents of container
// @parameters
// containerScan - Structure with details of the container to scan
// @returns
// Error - Errors, if any. Otherwise, returns nil
func (containerScan *ContainerScan) extractFileSystem() error {
// Auto-detect underlying container runtime
containerRuntime, endpoint, err := vessel.AutoDetectRuntime()
if err != nil {
return err
}
var containerRuntimeInterface vessel.Runtime
switch containerRuntime {
case vesselConstants.DOCKER:
containerRuntimeInterface = dockerRuntime.New(endpoint)
case vesselConstants.CONTAINERD:
containerRuntimeInterface = containerdRuntime.New(endpoint)
case vesselConstants.CRIO:
containerRuntimeInterface = crioRuntime.New(endpoint)
case vesselConstants.PODMAN:
containerRuntimeInterface = podmanRuntime.New(endpoint)
}
if containerRuntimeInterface == nil {
return errors.New("could not detect container runtime")
}
err = containerRuntimeInterface.ExtractFileSystemContainer(containerScan.containerID, containerScan.namespace, containerScan.tempDir+".tar")
if err != nil {
return err
}
runCommand("mkdir", containerScan.tempDir)
_, stdErr, retVal := runCommand("tar", "-xf", containerScan.tempDir+".tar", "-C"+containerScan.tempDir)
if retVal != 0 {
return errors.New(stdErr)
}
runCommand("rm", containerScan.tempDir+".tar")
return nil
}
// Function to scan extracted layers of container file system for IOC file by file
// @parameters
// containerScan - Structure with details of the container to scan
// @returns
// []output.IOCFound - List of all IOC found
// Error - Errors, if any. Otherwise, returns nil
func (containerScan *ContainerScan) scanPath(ctx *tasks.ScanContext, scanner *Scanner, containerPath string) ([]output.IOCFound, error) {
var iocsFound []output.IOCFound
err := scanner.ScanIOCInDir("", "", "/fenced/mnt/host/"+containerPath, nil, &iocsFound, true, ctx)
if err != nil {
log.Errorf("findIOCInContainer: %s", err)
return iocsFound, err
}
return iocsFound, nil
}
func (containerScan *ContainerScan) scanPathStream(ctx *tasks.ScanContext, scanner *Scanner, containerPath string) (chan output.IOCFound, error) {
return scanner.ScanIOCInDirStream("", "", "/fenced/mnt/host/"+containerPath, nil, true, ctx)
}
// Function to scan extracted layers of container file system for IOC file by file
// @parameters
// containerScan - Structure with details of the container to scan
// @returns
// []output.IOCFound - List of all IOC found
// Error - Errors, if any. Otherwise, returns nil
func (containerScan *ContainerScan) scan(ctx *tasks.ScanContext, scanner *Scanner) ([]output.IOCFound, error) {
var iocsFound []output.IOCFound
err := scanner.ScanIOCInDir("", "", containerScan.tempDir, nil, &iocsFound, false, ctx)
if err != nil {
log.Errorf("findIOCInContainer: %s", err)
return iocsFound, err
}
return iocsFound, nil
}
func (containerScan *ContainerScan) scanStream(ctx *tasks.ScanContext, scanner *Scanner) (chan output.IOCFound, error) {
return scanner.ScanIOCInDirStream("", "", containerScan.tempDir, nil, false, ctx)
}
type ContainerExtractionResult struct {
IOC []output.IOCFound
ContainerID string
}
func GetFileSystemPathsForContainer(containerID string, namespace string) ([]byte, error) {
// fmt.Println(append([]string{"docker"}, "|", "jq" , "-r" , "'map([.Name, .GraphDriver.Data.MergedDir]) | .[] | \"\\(.[0])\\t\\(.[1])\"'"))
return exec.Command("docker", "inspect", strings.TrimSpace(containerID)).Output()
}
func (s *Scanner) ExtractAndScanContainer(ctx *tasks.ScanContext, containerID string, namespace string) ([]output.IOCFound, error) {
var iocsFound []output.IOCFound
tempDir, err := core.GetTmpDir(containerID, *s.TempDirectory)
if err != nil {
return iocsFound, err
}
defer func() { _ = core.DeleteTmpDir(tempDir) }()
containerScan := ContainerScan{containerID: containerID, tempDir: tempDir, namespace: namespace}
containerRuntime, _, err := vessel.AutoDetectRuntime()
if err != nil {
return nil, err
}
switch containerRuntime {
case vesselConstants.DOCKER:
containerPath, err := GetFileSystemPathsForContainer(containerID, namespace)
if err != nil {
return nil, err
}
if strings.Contains(string(containerPath), "\"MergedDir\":") {
if strings.Contains(strings.Split(string(containerPath), "\"MergedDir\": \"")[1], "/merged\"") {
containerPathToScan := strings.Split(strings.Split(string(containerPath), "\"MergedDir\": \"")[1], "/merged\"")[0] + "/merged"
fmt.Println("Container Scan Path", containerPathToScan)
iocsFound, _ = containerScan.scanPath(ctx, s, containerPathToScan)
}
}
case vesselConstants.CONTAINERD, vesselConstants.CRIO, vesselConstants.PODMAN:
err = containerScan.extractFileSystem()
if err != nil {
return nil, err
}
iocsFound, err = containerScan.scan(ctx, s)
if err != nil {
return iocsFound, err
}
}
return iocsFound, nil
}
func (s *Scanner) ExtractAndScanContainerStream(ctx *tasks.ScanContext, containerID string, namespace string) (chan output.IOCFound, error) {
tempDir, err := core.GetTmpDir(containerID, *s.TempDirectory)
if err != nil {
return nil, err
}
res := make(chan output.IOCFound, outputChannelSize)
go func() {
defer func() { _ = core.DeleteTmpDir(tempDir) }()
defer close(res)
var middle chan output.IOCFound
containerScan := ContainerScan{containerID: containerID, tempDir: tempDir, namespace: namespace}
var containerRuntime string
containerRuntime, _, err = vessel.AutoDetectRuntime()
if err != nil {
return
}
switch containerRuntime {
case vesselConstants.DOCKER:
containerPath, err := GetFileSystemPathsForContainer(containerID, namespace)
if err != nil {
return
}
if strings.Contains(string(containerPath), "\"MergedDir\":") {
if strings.Contains(strings.Split(string(containerPath), "\"MergedDir\": \"")[1], "/merged\"") {
containerPathToScan := strings.Split(strings.Split(string(containerPath), "\"MergedDir\": \"")[1], "/merged\"")[0] + "/merged"
fmt.Println("Container Scan Path", containerPathToScan)
middle, err = containerScan.scanPathStream(ctx, s, containerPathToScan)
if err != nil {
return
}
}
}
case vesselConstants.CONTAINERD, vesselConstants.CRIO, vesselConstants.PODMAN:
err = containerScan.extractFileSystem()
if err != nil {
return
}
middle, err = containerScan.scanStream(ctx, s)
if err != nil {
return
}
}
for i := range middle {
res <- i
}
}()
return res, nil
}