Tracee > Discovery > Kubernetes API Server Connection Detected
LOW
Source
Tracee
ID
TRC-13
Version
0.1.0
Date
28 Jun 2022

Kubernetes API Server Connection Detected

A connection to the kubernetes API server was detected. The K8S API server is the brain of your K8S cluster, adversaries may try and communicate with the K8S API server to gather information/credentials, or even run more containers and laterally expand their grip on your systems.

MITRE ATT&CK

Discovery: Cloud Service Discovery,

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

import (
	"fmt"
	"strings"

	"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 K8sApiConnection struct {
	cb                    detect.SignatureHandler
	apiAddressContainerId map[string]string
}

func (sig *K8sApiConnection) Init(cb detect.SignatureHandler) error {
	sig.cb = cb
	sig.apiAddressContainerId = make(map[string]string)

	return nil
}

func (sig *K8sApiConnection) GetMetadata() (detect.SignatureMetadata, error) {
	return detect.SignatureMetadata{
		ID:          "TRC-13",
		Version:     "0.1.0",
		Name:        "Kubernetes API server connection detected",
		Description: "A connection to the kubernetes API server was detected. The K8S API server is the brain of your K8S cluster, adversaries may try and communicate with the K8S API server to gather information/credentials, or even run more containers and laterally expand their grip on your systems.",
		Tags:        []string{"container"},
		Properties: map[string]interface{}{
			"Severity":     1,
			"MITRE ATT&CK": "Discovery: Cloud Service Discovery",
		},
	}, nil
}

func (sig *K8sApiConnection) GetSelectedEvents() ([]detect.SignatureEventSelector, error) {
	return []detect.SignatureEventSelector{
		{Source: "tracee", Name: "execve", Origin: "container"},
		{Source: "tracee", Name: "security_socket_connect", Origin: "container"},
	}, nil
}

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

	if !ok {
		return fmt.Errorf("failed to cast event's payload")
	}
	containerID := eventObj.ContainerID
	if containerID == "" {
		return nil
	}

	switch eventObj.EventName {

	case "execve":
		// the usage of 'envp' argument of the execve event is vulnerable to TOCTOU attack.
		// in the future we plan to add 'envp' argument to sched_process_exec, and when it'll happen, we should start
		// using sched_process_exec instead of execve in this signature.
		envpArg, err := helpers.GetTraceeArgumentByName(eventObj, "envp")
		if err != nil {
			return nil
		}
		envs := envpArg.Value.([]string)

		apiIPAddress := getApiAddressFromEnvs(envs)
		if apiIPAddress != "" {
			sig.apiAddressContainerId[containerID] = apiIPAddress
		}

	case "security_socket_connect":

		apiAddress, exists := sig.apiAddressContainerId[containerID]
		if !exists {
			return nil
		}

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

		if ip == apiAddress {
			m, _ := sig.GetMetadata()
			sig.cb(detect.Finding{
				SigMetadata: m,
				Event:       event,
				Data: map[string]interface{}{
					"ip": apiAddress,
				},
			})
		}
	}
	return nil
}

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

func (sig *K8sApiConnection) Close() {}

func getApiAddressFromEnvs(envs []string) string {
	for _, env := range envs {
		if strings.Contains(env, "KUBERNETES_SERVICE_HOST=") {
			i := strings.Index(env, "=")
			return strings.TrimSpace(env[i+1:])
		}
	}
	return ""
}

func getIPFromAddr(addrArg trace.Argument) (string, error) {

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

	if addr["sa_family"] == "AF_INET" {
		return addr["sin_addr"], nil
	} else if addr["sa_family"] == "AF_INET6" {
		return addr["sin6_addr"], nil
	}

	return "", nil
}