-
Notifications
You must be signed in to change notification settings - Fork 4
/
r1001_exec_binary_not_in_base_image.go
182 lines (147 loc) · 5.03 KB
/
r1001_exec_binary_not_in_base_image.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
package rule
import (
"fmt"
"os"
"path/filepath"
"syscall"
"github.com/prometheus/procfs"
log "github.com/sirupsen/logrus"
"github.com/armosec/kubecop/pkg/approfilecache"
"github.com/kubescape/kapprofiler/pkg/tracing"
)
const (
R1001ID = "R1001"
R1001ExecBinaryNotInBaseImageRuleName = "Exec Binary Not In Base Image"
)
var R1001ExecBinaryNotInBaseImageRuleDescriptor = RuleDesciptor{
ID: R1001ID,
Name: R1001ExecBinaryNotInBaseImageRuleName,
Description: "Detecting exec calls of binaries that are not included in the base image",
Tags: []string{"exec", "malicious", "binary", "base image"},
Priority: RulePriorityCritical,
Requirements: RuleRequirements{
EventTypes: []tracing.EventType{tracing.ExecveEventType},
NeedApplicationProfile: false,
},
RuleCreationFunc: func() Rule {
return CreateRuleR1001ExecBinaryNotInBaseImage()
},
}
type R1001ExecBinaryNotInBaseImage struct {
BaseRule
}
type R1001ExecBinaryNotInBaseImageFailure struct {
RuleName string
Err string
FixSuggestionMsg string
RulePriority int
FailureEvent *tracing.ExecveEvent
}
func (rule *R1001ExecBinaryNotInBaseImage) Name() string {
return R1001ExecBinaryNotInBaseImageRuleName
}
func CreateRuleR1001ExecBinaryNotInBaseImage() *R1001ExecBinaryNotInBaseImage {
return &R1001ExecBinaryNotInBaseImage{}
}
func (rule *R1001ExecBinaryNotInBaseImage) DeleteRule() {
}
func (rule *R1001ExecBinaryNotInBaseImage) ProcessEvent(eventType tracing.EventType, event interface{}, appProfileAccess approfilecache.SingleApplicationProfileAccess, engineAccess EngineAccess) RuleFailure {
if eventType != tracing.ExecveEventType {
return nil
}
execEvent, ok := event.(*tracing.ExecveEvent)
if !ok {
return nil
}
if !IsExecBinaryInUpperLayer(execEvent) {
return nil
}
return &R1001ExecBinaryNotInBaseImageFailure{
RuleName: rule.Name(),
Err: fmt.Sprintf("Process image \"%s\" binary is not from the container image \"%s\"", execEvent.PathName, "<image name TBA> via PodSpec"),
FixSuggestionMsg: "If this is an expected behavior it is strongly suggested to include all executables in the container image. If this is not possible you can remove the rule binding to this workload.",
FailureEvent: execEvent,
RulePriority: R1001ExecBinaryNotInBaseImageRuleDescriptor.Priority,
}
}
func IsExecBinaryInUpperLayer(execEvent *tracing.ExecveEvent) bool {
// Find a process with the same mount namespace ID as the exec event.
process, err := findProcessByMountNamespace(execEvent)
if err != nil {
//log.Printf("Error finding process by mount namespace: %s\n", err)
return false
}
// Get the overlay mount point for the process.
upperLayerPath, err := getOverlayMountPoint(process)
if err != nil {
return false
}
return fileExists(filepath.Join(upperLayerPath, execEvent.PathName))
}
func findProcessByMountNamespace(execEvent *tracing.ExecveEvent) (*procfs.Proc, error) {
procs, err := procfs.AllProcs()
if err != nil {
return nil, err
}
for _, proc := range procs {
// Check if the mount namespace ID matches the specified namespaceID
mountNamespaceId, err := getMountNamespaceID(proc.PID)
if err != nil {
log.Debugf("Error reading mount namespace ID for PID %d: %s\n", proc.PID, err)
continue
}
if mountNamespaceId == execEvent.MountNsID {
return &proc, nil
}
}
return nil, fmt.Errorf("no matching process found for mount namespace %d", execEvent.MountNsID)
}
func getMountNamespaceID(pid int) (uint64, error) {
nsPath := fmt.Sprintf("/proc/%d/ns/mnt", pid)
stat := syscall.Stat_t{}
err := syscall.Stat(nsPath, &stat)
if err != nil {
return 0, err
}
return stat.Ino, nil
}
func getOverlayMountPoint(process *procfs.Proc) (string, error) {
// Read the mount info for the process, and find the overlay mount point. (There should only be one?).
if mounts, err := process.MountInfo(); err == nil {
for _, mount := range mounts {
if mount.FSType == "overlay" {
return mount.SuperOptions["upperdir"], nil
}
}
}
return "", fmt.Errorf("failed to get mount point for pid %d", process.PID)
}
func fileExists(filePath string) bool {
info, err := os.Stat(filepath.Join("/host", filePath))
if os.IsNotExist(err) {
log.Debugf("File %s does not exist %s \n", filePath, err)
return false
}
return !info.IsDir()
}
func (rule *R1001ExecBinaryNotInBaseImage) Requirements() RuleRequirements {
return RuleRequirements{
EventTypes: []tracing.EventType{tracing.ExecveEventType},
NeedApplicationProfile: false,
}
}
func (rule *R1001ExecBinaryNotInBaseImageFailure) Name() string {
return rule.RuleName
}
func (rule *R1001ExecBinaryNotInBaseImageFailure) Error() string {
return rule.Err
}
func (rule *R1001ExecBinaryNotInBaseImageFailure) Event() tracing.GeneralEvent {
return rule.FailureEvent.GeneralEvent
}
func (rule *R1001ExecBinaryNotInBaseImageFailure) Priority() int {
return rule.RulePriority
}
func (rule *R1001ExecBinaryNotInBaseImageFailure) FixSuggestion() string {
return rule.FixSuggestionMsg
}