|
| 1 | +package detectors |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aquasecurity/tracee/api/v1beta1" |
| 7 | + "github.com/aquasecurity/tracee/api/v1beta1/detection" |
| 8 | + "github.com/aquasecurity/tracee/common/parsers" |
| 9 | +) |
| 10 | + |
| 11 | +func init() { |
| 12 | + register(&SystemRequestKeyConfigModification{}) |
| 13 | +} |
| 14 | + |
| 15 | +// SystemRequestKeyConfigModification detects modifications to sysrq configuration files. |
| 16 | +// Adversaries may use this to control system behavior or gather information for container escape. |
| 17 | +type SystemRequestKeyConfigModification struct { |
| 18 | + logger detection.Logger |
| 19 | +} |
| 20 | + |
| 21 | +func (d *SystemRequestKeyConfigModification) GetDefinition() detection.DetectorDefinition { |
| 22 | + return detection.DetectorDefinition{ |
| 23 | + ID: "TRC-1031", |
| 24 | + Requirements: detection.DetectorRequirements{ |
| 25 | + Events: []detection.EventRequirement{ |
| 26 | + { |
| 27 | + Name: "security_file_open", |
| 28 | + Dependency: detection.DependencyRequired, |
| 29 | + ScopeFilters: []string{"container=started"}, |
| 30 | + DataFilters: []string{ |
| 31 | + "pathname=/proc/sys/kernel/sysrq", |
| 32 | + "pathname=/proc/sysrq-trigger", |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ProducedEvent: v1beta1.EventDefinition{ |
| 38 | + Name: "system_request_key_mod", |
| 39 | + Description: "System request key configuration modification", |
| 40 | + Version: &v1beta1.Version{Major: 1, Minor: 0, Patch: 0}, |
| 41 | + }, |
| 42 | + ThreatMetadata: &v1beta1.Threat{ |
| 43 | + Name: "System request key configuration modification", |
| 44 | + Description: "An attempt to modify and activate the System Request Key configuration file was detected. The system request key allows immediate input to the kernel through simple key combinations. Adversaries may use this feature to immediately shut down or restart a system. With read access to kernel logs, host related information such as listing tasks and CPU registers may be disclosed and could be used for container escape.", |
| 45 | + Severity: v1beta1.Severity_HIGH, |
| 46 | + Mitre: &v1beta1.Mitre{ |
| 47 | + Tactic: &v1beta1.MitreTactic{Name: "Privilege Escalation"}, |
| 48 | + Technique: &v1beta1.MitreTechnique{Id: "T1611", Name: "Escape to Host"}, |
| 49 | + }, |
| 50 | + Properties: map[string]string{"Category": "privilege-escalation"}, |
| 51 | + }, |
| 52 | + AutoPopulate: detection.AutoPopulateFields{Threat: true, DetectedFrom: true}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (d *SystemRequestKeyConfigModification) Init(params detection.DetectorParams) error { |
| 57 | + d.logger = params.Logger |
| 58 | + d.logger.Debugw("SystemRequestKeyConfigModification detector initialized") |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (d *SystemRequestKeyConfigModification) OnEvent(ctx context.Context, event *v1beta1.Event) ([]detection.DetectorOutput, error) { |
| 63 | + flags, err := v1beta1.GetDataSafe[int32](event, "flags") |
| 64 | + if err != nil { |
| 65 | + return nil, nil |
| 66 | + } |
| 67 | + |
| 68 | + if !parsers.IsFileWrite(int(flags)) { |
| 69 | + return nil, nil |
| 70 | + } |
| 71 | + |
| 72 | + d.logger.Debugw("sysrq modification detected", "container", v1beta1.GetContainerID(event)) |
| 73 | + return detection.Detected(), nil |
| 74 | +} |
| 75 | + |
| 76 | +func (d *SystemRequestKeyConfigModification) Close() error { |
| 77 | + d.logger.Debugw("SystemRequestKeyConfigModification detector closed") |
| 78 | + return nil |
| 79 | +} |
0 commit comments