HIGH
Source
Tracee
ID
TRC-1
Version
0.1.0
Date
28 Jun 2022

Standard Input Output Over Socket

Redirection of process’s standard input/output to socket

MITRE ATT&CK

Persistence: Server Software Component

Go Source

  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
package main

import (
	"fmt"

	"github.com/aquasecurity/tracee/signatures/helpers"
	"github.com/aquasecurity/tracee/types/detect"
	"github.com/aquasecurity/tracee/types/protocol"
	"github.com/aquasecurity/tracee/types/trace"
)

type connectedAddress struct {
	ip   string
	port string
}

type stdioOverSocket struct {
	cb              detect.SignatureHandler
	pidFDAddressMap map[int]map[int]connectedAddress
}

func (sig *stdioOverSocket) Init(cb detect.SignatureHandler) error {
	sig.cb = cb
	sig.pidFDAddressMap = make(map[int]map[int]connectedAddress)

	return nil
}

func (sig *stdioOverSocket) GetMetadata() (detect.SignatureMetadata, error) {
	return detect.SignatureMetadata{
		ID:          "TRC-1",
		Version:     "0.1.0",
		Name:        "Standard Input/Output Over Socket",
		Description: "Redirection of process's standard input/output to socket",
		Tags:        []string{"linux", "container"},
		Properties: map[string]interface{}{
			"Severity":     3,
			"MITRE ATT&CK": "Persistence: Server Software Component",
		},
	}, nil
}

func (sig *stdioOverSocket) GetSelectedEvents() ([]detect.SignatureEventSelector, error) {
	return []detect.SignatureEventSelector{
		{Source: "tracee", Name: "security_socket_connect"},
		{Source: "tracee", Name: "dup"},
		{Source: "tracee", Name: "dup2"},
		{Source: "tracee", Name: "dup3"},
		{Source: "tracee", Name: "close"},
		{Source: "tracee", Name: "sched_process_exit"},
	}, nil
}

func (sig *stdioOverSocket) OnEvent(event protocol.Event) error {
	eventObj, ok := event.Payload.(trace.Event)

	if !ok {
		return fmt.Errorf("failed to cast event's payload")
	}

	pid := eventObj.ProcessID

	switch eventObj.EventName {

	case "security_socket_connect":

		remoteAddrArg, err := helpers.GetTraceeArgumentByName(eventObj, "remote_addr")
		if err != nil {
			return err
		}

		var address connectedAddress
		address, err = getAddressfromAddrArg(remoteAddrArg)
		if err != nil {
			return err
		}

		sockfdArg, err := helpers.GetTraceeArgumentByName(eventObj, "sockfd")
		if err != nil {
			return err
		}

		sockfd := int(sockfdArg.Value.(int32))

		err = isSocketOverStdio(sig, event, address, sockfd)
		if err != nil {
			return err
		}

		_, pidExists := sig.pidFDAddressMap[pid]
		if !pidExists {
			sig.pidFDAddressMap[pid] = make(map[int]connectedAddress)
		}

		if address.ip != "" && address.port != "" {
			sig.pidFDAddressMap[pid][sockfd] = address
		}

	case "dup":

		pidSocketMap, pidExists := sig.pidFDAddressMap[pid]

		if !pidExists {
			return nil
		}

		oldFdArg, err := helpers.GetTraceeArgumentByName(eventObj, "oldfd")
		if err != nil {
			return err
		}

		srcFd := int(oldFdArg.Value.(int32))

		dstFd := eventObj.ReturnValue

		err = isSocketDuplicatedIntoStdio(sig, event, pidSocketMap, srcFd, dstFd)
		if err != nil {
			return err
		}

	case "dup2", "dup3":

		pidSocketMap, pidExists := sig.pidFDAddressMap[pid]

		if !pidExists {
			return nil
		}

		oldFdArg, err := helpers.GetTraceeArgumentByName(eventObj, "oldfd")
		if err != nil {
			return err
		}

		srcFd := int(oldFdArg.Value.(int32))

		newFdArg, err := helpers.GetTraceeArgumentByName(eventObj, "newfd")
		if err != nil {
			return err
		}

		dstFd := int(newFdArg.Value.(int32))

		err = isSocketDuplicatedIntoStdio(sig, event, pidSocketMap, srcFd, dstFd)
		if err != nil {
			return err
		}

	case "close":

		currentFdArg, err := helpers.GetTraceeArgumentByName(eventObj, "fd")
		if err != nil {
			return err
		}

		currentFd := int(currentFdArg.Value.(int32))

		delete(sig.pidFDAddressMap[pid], currentFd)

	case "sched_process_exit":

		delete(sig.pidFDAddressMap, pid)

	}

	return nil
}

func (sig *stdioOverSocket) OnSignal(s detect.Signal) error {
	return nil
}
func (sig *stdioOverSocket) Close() {}

func isSocketDuplicatedIntoStdio(sig *stdioOverSocket, event protocol.Event, pidSocketMap map[int]connectedAddress, srcFd int, dstFd int) error {
	address, socketfdExists := pidSocketMap[srcFd]

	// this means that a socket FD is duplicated into one of the standard FDs
	if socketfdExists {
		isSocketOverStdio(sig, event, address, dstFd)
	}

	return nil
}

func isSocketOverStdio(sig *stdioOverSocket, event protocol.Event, address connectedAddress, fd int) error {

	stdAll := []int{0, 1, 2}

	if intInSlice(fd, stdAll) {
		m, _ := sig.GetMetadata()
		sig.cb(detect.Finding{
			SigMetadata: m,
			Event:       event,
			Data: map[string]interface{}{
				"ip":   address.ip,
				"port": address.port,
				"fd":   fd,
			},
		})
	}

	return nil
}

func intInSlice(a int, list []int) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

func getAddressfromAddrArg(arg trace.Argument) (connectedAddress, error) {

	addr, isOk := arg.Value.(map[string]string)
	if !isOk {
		return connectedAddress{}, fmt.Errorf("couldn't convert arg to addr")
	}

	if addr["sa_family"] == "AF_INET" {
		return connectedAddress{ip: addr["sin_addr"], port: addr["sin_port"]}, nil
	} else if addr["sa_family"] == "AF_INET6" {
		return connectedAddress{ip: addr["sin6_addr"], port: addr["sin6_port"]}, nil
	}

	return connectedAddress{}, nil
}