Kubernetes API Server Connection Detected

LOW
Source
Tracee
ID
TRC-1013
Version
0.1.0
Date
10 Dec 2024

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
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(ctx detect.SignatureContext) error {
	sig.cb = ctx.Callback
	sig.apiAddressContainerId = make(map[string]string)

	return nil
}

func (sig *K8sApiConnection) GetMetadata() (detect.SignatureMetadata, error) {
	return detect.SignatureMetadata{
		ID:          "TRC-1013",
		Version:     "0.1.0",
		Name:        "Kubernetes API server connection detected",
		EventName:   "k8s_api_connection",
		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: "sched_process_exec", 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.Container.ID
	if containerID == "" {
		return nil
	}

	switch eventObj.EventName {
	case "sched_process_exec":
		envVars, err := helpers.GetTraceeSliceStringArgumentByName(eventObj, "env")
		if err != nil {
			return nil
		}

		apiIPAddress := getApiAddressFromEnvs(envVars)
		if apiIPAddress != "" {
			sig.apiAddressContainerId[containerID] = apiIPAddress
		}
	case "security_socket_connect":
		apiAddress, exists := sig.apiAddressContainerId[containerID]
		if !exists {
			return nil
		}

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

		supportedFamily, err := helpers.IsInternetFamily(remoteAddr)
		if err != nil {
			return err
		}
		if !supportedFamily {
			return nil
		}

		ip, err := helpers.GetIPFromRawAddr(remoteAddr)
		if err != nil {
			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 ""
}