MEDIUM
Source
Tracee
ID
TRC-1027
Version
1
Date
10 Apr 2024

Scheduled Tasks Modification Detected

The task scheduling functionality or files were modified. Crontab schedules task execution or enables task execution at boot time. Adversaries may add or modify scheduled tasks in order to persist a reboot, thus maintaining malicious execution on the affected host.

MITRE ATT&CK

Persistence: Cron

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

import (
	"fmt"
	"path"
	"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 ScheduledTaskModification struct {
	cb           detect.SignatureHandler
	cronFiles    []string
	cronDirs     []string
	cronCommands []string
}

func (sig *ScheduledTaskModification) Init(ctx detect.SignatureContext) error {
	sig.cb = ctx.Callback
	sig.cronFiles = []string{"/etc/crontab", "/etc/anacrontab", "/etc/cron.deny", "/etc/cron.allow"}
	sig.cronDirs = []string{"/etc/cron.hourly", "/etc/cron.daily", "/etc/cron.weekly", "/etc/cron.monthly", "/etc/cron.d", "/var/spool/cron/crontabs", "var/spool/anacron"}
	sig.cronCommands = []string{"crontab", "at", "batch", "launchd"}
	return nil
}

func (sig *ScheduledTaskModification) GetMetadata() (detect.SignatureMetadata, error) {
	return detect.SignatureMetadata{
		ID:          "TRC-1027",
		Version:     "1",
		Name:        "Scheduled tasks modification detected",
		EventName:   "scheduled_task_mod",
		Description: "The task scheduling functionality or files were modified. Crontab schedules task execution or enables task execution at boot time. Adversaries may add or modify scheduled tasks in order to persist a reboot, thus maintaining malicious execution on the affected host.",
		Properties: map[string]interface{}{
			"Severity":             2,
			"Category":             "persistence",
			"Technique":            "Cron",
			"Kubernetes_Technique": "",
			"id":                   "attack-pattern--2acf44aa-542f-4366-b4eb-55ef5747759c",
			"external_id":          "T1053.003",
		},
	}, nil
}

func (sig *ScheduledTaskModification) GetSelectedEvents() ([]detect.SignatureEventSelector, error) {
	return []detect.SignatureEventSelector{
		{Source: "tracee", Name: "security_file_open", Origin: "*"},
		{Source: "tracee", Name: "security_inode_rename", Origin: "*"},
		{Source: "tracee", Name: "sched_process_exec", Origin: "*"},
	}, nil
}

func (sig *ScheduledTaskModification) OnEvent(event protocol.Event) error {
	eventObj, ok := event.Payload.(trace.Event)
	if !ok {
		return fmt.Errorf("invalid event")
	}

	switch eventObj.EventName {
	case "security_file_open":
		pathname, err := helpers.GetTraceeStringArgumentByName(eventObj, "pathname")
		if err != nil {
			return err
		}

		flags, err := helpers.GetTraceeStringArgumentByName(eventObj, "flags")
		if err != nil {
			return err
		}

		if helpers.IsFileWrite(flags) {
			return sig.checkFileOrDir(event, pathname)
		}
	case "security_inode_rename":
		newPath, err := helpers.GetTraceeStringArgumentByName(eventObj, "new_path")
		if err != nil {
			return err
		}

		return sig.checkFileOrDir(event, newPath)
	case "sched_process_exec":
		pathname, err := helpers.GetTraceeStringArgumentByName(eventObj, "pathname")
		if err != nil {
			return err
		}
		basename := path.Base(pathname)

		for _, cronCommand := range sig.cronCommands {
			if basename == cronCommand {
				return sig.match(event)
			}
		}
	}

	return nil
}

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

func (sig *ScheduledTaskModification) checkFileOrDir(event protocol.Event, pathname string) error {
	for _, cronFile := range sig.cronFiles {
		if pathname == cronFile {
			return sig.match(event)
		}
	}

	for _, cronDir := range sig.cronDirs {
		if strings.HasPrefix(pathname, cronDir) {
			return sig.match(event)
		}
	}

	return nil
}

func (sig *ScheduledTaskModification) match(event protocol.Event) error {
	metadata, err := sig.GetMetadata()
	if err != nil {
		return err
	}
	sig.cb(&detect.Finding{
		SigMetadata: metadata,
		Event:       event,
		Data:        nil,
	})

	return nil
}