Kubescape's CEL (Common Expression Language) runtime threat detection rules library. This repository contains a collection of security rules that can be used for runtime threat detection in Kubernetes environments.
The rule library provides a structured way to define security rules using YAML format. Each rule is defined as a Custom Resource Definition (CRD) instance that can be deployed to Kubernetes clusters for runtime threat detection.
Each rule is defined in a YAML file with the following structure:
apiVersion: kubescape.io/v1
kind: Rules
metadata:
name: rule-name-rule
namespace: kubescape
labels:
app: kubescape
spec:
rules:
- name: "Rule Display Name"
enabled: true
id: "R####"
description: "Description of what the rule detects"
expressions:
message: "CEL expression for alert message"
unique_id: "CEL expression for unique identifier"
rule_expression:
- event_type: "event_type_name"
expression: "CEL expression for detection logic"
profile_dependency: 0 # 0=Required, 1=Optional, 2=NotRequired
severity: 1
support_policy: false
tags:
- "tag1"
- "tag2"
Field | Type | Description | Required |
---|---|---|---|
name |
string | Human-readable rule name | Yes |
enabled |
boolean | Whether the rule is active | Yes |
id |
string | Unique rule identifier (format: R####) | Yes |
description |
string | Detailed description of the rule | Yes |
expressions.message |
string | CEL expression for alert message | Yes |
expressions.unique_id |
string | CEL expression for unique event ID | Yes |
expressions.rule_expression |
array | Array of detection expressions | Yes |
profile_dependency |
integer | Profile dependency level (0,1,2) | Yes |
severity |
integer | Rule severity level | Yes |
support_policy |
boolean | Whether rule supported by rule policy | Yes |
tags |
array | Array of tags for categorization | Yes |
state |
object | Rule state | No |
exec
- Process execution eventsopen
- File access eventscapabilities
- Linux capability eventsdns
- DNS query eventsnetwork
- Network connection eventssyscall
- System call eventsrandomx
- XMRig mining eventssymlink
- Symbolic link eventshardlink
- Hard link eventsssh
- SSH connection eventshttp
- HTTP request eventsptrace
- Process tracing eventsiouring
- IO_uring eventsfork
- Process fork eventsexit
- Process exit eventsprocfs
- Proc filesystem events
Create a new directory in pkg/rules/
with the naming convention:
r####-descriptive-name/
Example:
pkg/rules/r0001-unexpected-process-launched/
Create a YAML file in your rule directory with the rule definition (see example unexpected-process-launched.yaml)
Create a rule_test.go
file in your rule directory (see example rule_test.go)
Run the tests to ensure your rule works correctly:
go test -v ./pkg/rules/r0001-unexpected-process-launched/
The gen.sh
script automatically combines all individual rule YAML files into a single CRD instance.
./gen.sh
- Scans the
pkg/rules/
directory for all YAML files - Combines all individual rule definitions into a single Rule instance
- Generates
rules-crd.yaml
with all rules in the spec array - Validates the generated YAML (if
yq
is available)
The script generates rules-crd.yaml
containing:
apiVersion: kubescape.io/v1
kind: Rules
metadata:
name: kubescape-rules
namespace: kubescape
labels:
app: kubescape
spec:
rules:
- name: "Rule 1"
# ... rule 1 definition
- name: "Rule 2"
# ... rule 2 definition
# ... all other rules
bash
shellyq
(optional, for YAML validation)
- Create a new rule following the directory structure and naming conventions
- Write the rule YAML with proper CEL expressions
- Add comprehensive tests in
rule_test.go
- Test your rule with
go test -v ./pkg/rules/your-rule/
- Generate the combined CRD with
./gen.sh
- Deploy the generated
rules-crd.yaml
to your Kubernetes cluster
go test -v ./pkg/rules/...
go test -v ./pkg/rules/r0001-unexpected-process-launched/
./gen.sh
# Check the generated rules-crd.yaml file
Rules use Common Expression Language (CEL) for expressions. Key concepts:
Defines the alert message format:
"'Unexpected process launched: ' + event.Comm + ' with PID ' + string(event.Pid)"
Creates a unique identifier for deduplication:
"event.Comm + '_' + string(event.Pid) + '_' + event.ExePath"
Defines the detection logic:
"!data.profile_checks.exec_path"
- Use descriptive names for rules and directories
- Follow the ID numbering convention (R####)
- Write comprehensive tests for each rule
- Use appropriate tags for categorization
- Set correct severity levels based on impact
- Document complex CEL expressions with comments
- Test both positive and negative scenarios
- Validate generated YAML before deployment
- Fork the repository
- Create a feature branch
- Add your rule following the guidelines
- Write comprehensive tests
- Run the generation script
- Submit a pull request