Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

rules: detect destructive and privileged commands

Justin D'Souza97f70d1c304b
diff --git a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/builtin-security-review.spec.ts b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/builtin-security-review.spec.ts
index 1123f57d..af0787d5 100644
--- a/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/builtin-security-review.spec.ts
+++ b/cli/beacon/internal/endpoint/dashboard/testdata/lens-e2e/tests/builtin-security-review.spec.ts
@@ -5,7 +5,7 @@ test('security review: findings grouped by risk, the combination flagged, eviden
const frame = page.frameLocator('iframe.lens-frame');
await expect(frame.locator('#root')).toBeVisible();
- await expect(frame.locator('#root > p').first()).toHaveText('5 findings across 81 rules evaluated.');
+ await expect(frame.locator('#root > p').first()).toHaveText('5 findings across 86 rules evaluated.');
await expect(frame.locator('.alert')).toContainText('combines untrusted input, sensitive access and a consequential action');
const rows = frame.locator('tbody tr');
diff --git a/docs/detections/index.mdx b/docs/detections/index.mdx
index 4a6e1701..5b96f977 100644
--- a/docs/detections/index.mdx
+++ b/docs/detections/index.mdx
@@ -53,7 +53,7 @@ Detections focus on risky agent behavior that is hard to understand from one raw
- **Credential access:** reads of `.env`, SSH keys, cloud credentials, browser session stores, shell history, process tables, Kubernetes kubeconfigs and service-account tokens, GPG private keyrings, password-manager vaults (KeePass, 1Password, Bitwarden, LastPass, pass), OS keychain secret reads (`security find-generic-password -w`, `secret-tool lookup`), or a signed-in CLI printing a live token (`gcloud auth print-access-token`, `gh auth token`, `aws configure export-credentials`, `az account get-access-token`).
- **Context exfiltration:** network commands, paste and transfer services, DNS tunneling, Git bundles, outbound MCP calls, secret-bearing request bodies, SSH reverse-tunnel or SOCKS-proxy invocations, or archive-then-upload pipelines.
-- **Risky commands:** destructive shell operations, persistence installation (crontab, systemd timers, kernel modules), shell escapes, privileged container runs, curl-to-shell patterns, packages installed from a URL, git ref or non-default index (`pip install git+https://…`, `--extra-index-url`, `npm i https://…tgz`), sudoers edits, root-equivalent changes, or history-rewriting git operations (filter-branch, filter-repo, rebase -i, force-push to protected branches).
+- **Risky commands:** destructive shell operations, database drops and flushes, infrastructure teardown, cloud credential creation and permission grants, Beacon telemetry or shell-history tampering, discarded local Git work, persistence installation (crontab, systemd timers, kernel modules), shell escapes, privileged container runs, curl-to-shell patterns, packages installed from a URL, git ref or non-default index (`pip install git+https://…`, `--extra-index-url`, `npm i https://…tgz`), sudoers edits, root-equivalent changes, or history-rewriting git operations (filter-branch, filter-repo, rebase -i, force-push to protected branches).
- **Sensitive edits:** changes to CI pipelines, dependency manifests, shell startup files, authorization code, autostart units, agent instruction files and agent settings, hooks, MCP configs and plugins for Claude Code, Codex, Cursor, Gemini CLI, Copilot, Kiro, OpenCode, Windsurf and Cline (whether written by an edit tool or by a shell command), SSH authorized_keys or sshd_config, /etc/hosts or hosts.allow/deny, container or IaC files (Dockerfile, Terraform, Kubernetes manifests, Helm values), or other control surfaces.
- **Prompt injection:** instructions that try to override roles, ignore prior instructions, exfiltrate context, hide with invisible characters, or decode and execute hidden payloads.
- **Approval and policy abuse:** denied actions that still execute, high-risk approvals without a reason, attempts to spawn around runtime permission controls (agent CLIs launched with permission-elevating env vars, sandbox-escape primitives like nsenter / unshare / chroot / pivot_root), or re-invocations of denied commands through a renamed binary or temp-path shim.
@@ -72,14 +72,16 @@ Beacon ships with a small built-in baseline so `beacon scan` works before any ru
| --- | --- |
| `credential-access` | Credential-file reads, browser session store reads, shell history scans, SSH agent enumeration, Kubernetes kubeconfig reads, GPG secret-key exports, password-manager vault reads, OS keychain secret reads, cloud CLI token export |
| `context-exfiltration` | Secret read then egress, credentials in curl data, upload to paste or transfer sites, SSH tunnel egress, archive-then-upload pipelines |
-| `risky-command` | Curl piped to shell, recursive root delete, sudoers tampering, persistence installation, crontab modification, kernel-module load, package install from an untrusted source |
+| `risky-command` | Curl piped to shell, recursive root delete, sudoers tampering, persistence installation, crontab modification, kernel-module load, package install from an untrusted source, database drop or flush, infrastructure teardown, cloud permission grants, telemetry or history tampering |
| `sensitive-edit` | CI pipeline edits, dependency manifest edits, shell login file edits, agent instruction and control surface edits across runtimes (including shell writes like `tee .mcp.json`), SSH authorized_keys edits, /etc/hosts edits, container / IaC file edits |
| `prompt-injection` | Role override jailbreaks, hidden payloads, markdown-image exfiltration, instruction override markers, injected instructions in files, web pages, or MCP results the agent reads |
| `external-access` | Cloud metadata endpoint access, link-local target fetches, external MCP tool calls |
| `approval-abuse` | Permission-bypass execution or high-risk approvals without reviewer context, denied-command re-invocation through renamed binaries |
-| `source-control` | Git hook installation, remote tampering, Git config execution injection, history rewriting (filter-branch / filter-repo / rebase -i), force-push to protected branches |
+| `source-control` | Git hook installation, remote tampering, Git config execution injection, history rewriting (filter-branch / filter-repo / rebase -i), force-push to protected branches, hard reset, forced clean, whole-tree restore, stash clear |
| `agent-control` | Agent CLIs spawned with permission-elevating env vars, sandbox-escape primitives, inter-agent credential handoff, MCP servers / plugins / extensions installed by command |
+Telemetry-tampering detections cover commands that remove Beacon logs or plugins, stop its services, uninstall or disconnect it, or clear or disable shell history. They can only fire if the command was captured before collection stopped.
+
Rules only evaluate events that exist in the runtime log. For example, an MCP detection needs MCP-like activity to be emitted by a supported harness, and a prompt-injection rule needs retained prompt or content fields to be present under the endpoint's local data-retention settings.
## Detection flow
diff --git a/rules/risky-command/cloud-permission-grant.rule.yaml b/rules/risky-command/cloud-permission-grant.rule.yaml
new file mode 100644
index 00000000..34207e77
--- /dev/null
+++ b/rules/risky-command/cloud-permission-grant.rule.yaml
@@ -0,0 +1,287 @@
+id: cloud-permission-grant
+version: 1
+title: Cloud credentials created or permissions granted
+description: >
+ An agent creates cloud access keys, attaches or adds IAM policies or bindings,
+ assigns an Azure role, resets service-principal credentials, or creates a
+ Kubernetes role binding. This records a privileged action for review, not
+ proof that the resulting permissions were broader or malicious.
+severity: high
+status: experimental
+posture: detect
+taxonomy:
+ beacon_category: risky-command
+ owasp_llm: LLM06
+
+match: >
+ e.event.action == "command.executed" && (
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?aws(?:[ \\t]+--(?:profile|region|endpoint-url)[ \\t]+[^\\s;&|]+)*[ \\t]+iam[ \\t]+(?:create-access-key|attach-(?:user|group|role)-policy|put-(?:user|group|role)-policy|create-policy|create-policy-version|add-user-to-group)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gcloud(?:[ \\t]+--(?:project|account|configuration)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+iam[ \\t]+service-accounts[ \\t]+keys[ \\t]+create(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gcloud(?:[ \\t]+--(?:project|account|configuration)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+(?:projects|organizations|folders|iam[ \\t]+service-accounts)[ \\t]+(?:add-iam-policy-binding|set-iam-policy)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gcloud(?:[ \\t]+--(?:project|account|configuration)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+(?:storage[ \\t]+buckets|run[ \\t]+services|secrets)[ \\t]+add-iam-policy-binding(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?az[ \\t]+(?:role[ \\t]+assignment[ \\t]+create|ad[ \\t]+sp[ \\t]+credential[ \\t]+reset)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?kubectl(?:[ \\t]+(?:-n|--namespace|--context|--kubeconfig)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+create[ \\t]+(?:rolebinding|clusterrolebinding)(?:[ \\t]|$|[;&|\\n<>#])")
+ )
+
+emit:
+ reason: "Cloud credentials created or permissions granted"
+
+tests:
+ - name: match_1_aws_iam_create_access_key_user_name_agent
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-access-key --user-name agent" }
+ session: { id: s1 }
+ - name: match_2_aws_profile_prod_iam_attach_user_policy_user_name_agent_policy_arn_arn_aws_
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws --profile prod iam attach-user-policy --user-name agent --policy-arn arn:aws:iam::aws:policy/AdministratorAccess" }
+ session: { id: s1 }
+ - name: match_3_aws_iam_attach_role_policy_role_name_agent
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam attach-role-policy --role-name agent" }
+ session: { id: s1 }
+ - name: match_4_aws_iam_put_group_policy_group_name_dev
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam put-group-policy --group-name dev" }
+ session: { id: s1 }
+ - name: match_5_aws_iam_create_policy_policy_name_admin
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-policy --policy-name admin" }
+ session: { id: s1 }
+ - name: match_6_aws_iam_create_policy_version_policy_arn_arn_example
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-policy-version --policy-arn arn:example" }
+ session: { id: s1 }
+ - name: match_7_aws_iam_add_user_to_group_group_name_admins_user_name_agent
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam add-user-to-group --group-name admins --user-name agent" }
+ session: { id: s1 }
+ - name: match_8_gcloud_iam_service_accounts_keys_create_key_json_iam_account_agent_example_
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud iam service-accounts keys create key.json --iam-account [email protected]" }
+ session: { id: s1 }
+ - name: match_9_gcloud_projects_add_iam_policy_binding_prod_member_serviceaccount_agent_exa
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud projects add-iam-policy-binding prod --member serviceAccount:[email protected] --role roles/owner" }
+ session: { id: s1 }
+ - name: match_10_gcloud_iam_service_accounts_add_iam_policy_binding_agent_example_test_role_
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud iam service-accounts add-iam-policy-binding [email protected] --role roles/iam.serviceAccountUser" }
+ session: { id: s1 }
+ - name: match_11_gcloud_organizations_set_iam_policy_123_policy_json
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud organizations set-iam-policy 123 policy.json" }
+ session: { id: s1 }
+ - name: match_12_gcloud_storage_buckets_add_iam_policy_binding_gs_data_role_roles_storage_ad
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud storage buckets add-iam-policy-binding gs://data --role roles/storage.admin" }
+ session: { id: s1 }
+ - name: match_13_az_role_assignment_create_assignee_agent_role_owner
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az role assignment create --assignee agent --role Owner" }
+ session: { id: s1 }
+ - name: match_14_az_ad_sp_credential_reset_id_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az ad sp credential reset --id app" }
+ session: { id: s1 }
+ - name: match_15_kubectl_create_rolebinding_admin_clusterrole_admin_user_agent
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl create rolebinding admin --clusterrole=admin --user=agent" }
+ session: { id: s1 }
+ - name: match_16_kubectl_n_prod_create_clusterrolebinding_admin_clusterrole_cluster_admin_us
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl -n prod create clusterrolebinding admin --clusterrole=cluster-admin --user=agent" }
+ session: { id: s1 }
+ - name: no_match_1_aws_iam_list_access_keys
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam list-access-keys" }
+ session: { id: s1 }
+ - name: no_match_2_aws_iam_get_user_policy_user_name_agent_policy_name_admin
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam get-user-policy --user-name agent --policy-name admin" }
+ session: { id: s1 }
+ - name: no_match_3_aws_iam_list_attached_role_policies_role_name_agent
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam list-attached-role-policies --role-name agent" }
+ session: { id: s1 }
+ - name: no_match_4_gcloud_iam_service_accounts_keys_list
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud iam service-accounts keys list" }
+ session: { id: s1 }
+ - name: no_match_5_gcloud_projects_get_iam_policy_prod
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud projects get-iam-policy prod" }
+ session: { id: s1 }
+ - name: no_match_6_az_role_assignment_list
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az role assignment list" }
+ session: { id: s1 }
+ - name: no_match_7_az_ad_sp_credential_list_id_app
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az ad sp credential list --id app" }
+ session: { id: s1 }
+ - name: no_match_8_kubectl_get_rolebindings
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl get rolebindings" }
+ session: { id: s1 }
+ - name: no_match_9_kubectl_describe_clusterrolebinding_admin
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl describe clusterrolebinding admin" }
+ session: { id: s1 }
+ - name: no_match_10_aws_iam_list_users_echo_create_access_key
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam list-users; echo create-access-key" }
+ session: { id: s1 }
+ - name: no_match_11_gcloud_projects_list_echo_add_iam_policy_binding
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud projects list\necho add-iam-policy-binding" }
+ session: { id: s1 }
+ - name: no_match_12_kubectl_get_pods_echo_create_rolebinding
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl get pods; echo create rolebinding" }
+ session: { id: s1 }
+ - name: no_match_13_aws_iam_get_user_create_access_key
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam get-user # create-access-key" }
+ session: { id: s1 }
+ - name: no_match_14_kubectl_create_role_admin
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl create role admin" }
+ session: { id: s1 }
+ - name: no_match_15_az_ad_sp_show_id_app
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az ad sp show --id app" }
+ session: { id: s1 }
+ - name: newline
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "true\naws iam create-access-key --user-name agent" }
+ session: { id: s1 }
+ - name: then
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if true; then aws iam create-access-key --user-name agent" }
+ session: { id: s1 }
+ - name: do
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "for x in a; do aws iam create-access-key --user-name agent" }
+ session: { id: s1 }
+ - name: else
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if false; then true; else aws iam create-access-key --user-name agent" }
+ session: { id: s1 }
+ - name: redirect_or_comment_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-access-key --user-name agent 2>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_2
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-access-key --user-name agent >/dev/null 2>&1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_3
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-access-key --user-name agent&>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_4
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws iam create-access-key --user-name agent # maintenance" }
+ session: { id: s1 }
+ - name: mention_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"aws iam create-access-key --user-name agent\"" }
+ session: { id: s1 }
+ - name: mention_2
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep -F \"aws iam create-access-key --user-name agent\"" }
+ session: { id: s1 }
+ - name: mention_3
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git commit -m \"aws iam create-access-key --user-name agent\"" }
+ session: { id: s1 }
+ - name: wrong_event_action
+ verdict: no_match
+ events:
+ - event: { action: tool.invoked }
+ command: { command: "aws iam create-access-key --user-name agent" }
+ session: { id: s1 }
diff --git a/rules/risky-command/database-drop-or-flush.rule.yaml b/rules/risky-command/database-drop-or-flush.rule.yaml
new file mode 100644
index 00000000..65d334bc
--- /dev/null
+++ b/rules/risky-command/database-drop-or-flush.rule.yaml
@@ -0,0 +1,288 @@
+id: database-drop-or-flush
+version: 1
+title: Database dropped, truncated or flushed
+description: >
+ An agent invokes a destructive database operation through a SQL client, Redis,
+ or a framework reset command. Inline SQL must begin with DROP or TRUNCATE;
+ migration-file contents and arbitrary SQL scripts are not inspected.
+severity: high
+status: experimental
+posture: detect
+taxonomy:
+ beacon_category: risky-command
+ owasp_llm: LLM06
+
+match: >
+ e.event.action == "command.executed" && (
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:psql|mysql|mariadb)\\b[^;&|\\n#]*[ \\t](?:-c|-e|--command|--execute)(?:[ \\t]+|=)[\\\"\\x27]?[ \\t]*(?:(?i:DROP)[ \\t]+(?i:TABLE|DATABASE|SCHEMA|INDEX|VIEW|MATERIALIZED|SEQUENCE|TYPE|FUNCTION|PROCEDURE|TRIGGER|EXTENSION|ROLE|USER)\\b|(?i:TRUNCATE)[ \\t]+[a-zA-Z_\\\"\\x27])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?sqlite3(?:[ \\t]+-[\\w-]+)*[ \\t]+[^\\s;&|]+[ \\t][\\\"\\x27][ \\t]*(?i:DROP|TRUNCATE)[ \\t]+(?i:TABLE|INDEX|VIEW|TRIGGER)\\b") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?dropdb(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?mysqladmin(?:[ \\t]+(?:-[upPhS]|--(?:user|password|port|host|socket))(?:=|[ \\t]+)?[^\\s;&|]+|[ \\t]+(?:--force|-f))*[ \\t]+drop(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?redis-cli(?:[ \\t]+(?:-[hpansu]|--(?:host|port|pass|user|uri))(?:=|[ \\t]+)[^\\s;&|]+|[ \\t]+--(?:tls|raw|no-auth-warning))*[ \\t]+(?i:FLUSHALL|FLUSHDB)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:bundle[ \\t]+exec[ \\t]+)?(?:rails|rake)[ \\t]+db:(?:drop|reset|migrate:reset)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:(?:npx|pnpm[ \\t]+exec|bunx)[ \\t]+)?prisma[ \\t]+migrate[ \\t]+reset(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:python[0-9.]*[ \\t]+(?:[^\\s;&|]*/)?manage\\.py|django-admin)[ \\t]+flush(?:[ \\t]|$|[;&|\\n<>#])")
+ )
+
+emit:
+ reason: "Database dropped, truncated or flushed"
+
+tests:
+ - name: match_1_psql_app_c_drop_table_users
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: match_2_psql_app_c_truncate_users
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"TRUNCATE users;\"" }
+ session: { id: s1 }
+ - name: match_3_sudo_usr_bin_psql_app_command_drop_schema_public_cascade
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "sudo /usr/bin/psql app --command=\"DROP SCHEMA public CASCADE\"" }
+ session: { id: s1 }
+ - name: match_4_mysql_e_truncate_table_sessions
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mysql -e \"TRUNCATE TABLE sessions\"" }
+ session: { id: s1 }
+ - name: match_5_mariadb_execute_drop_database_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mariadb --execute=\"DROP DATABASE app\"" }
+ session: { id: s1 }
+ - name: match_6_sqlite3_app_db_drop_table_users
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "sqlite3 app.db \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: match_7_dropdb_production
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "dropdb production" }
+ session: { id: s1 }
+ - name: match_8_mysqladmin_u_root_drop_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mysqladmin -u root drop app" }
+ session: { id: s1 }
+ - name: match_9_redis_cli_h_cache_flushall
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli -h cache FLUSHALL" }
+ session: { id: s1 }
+ - name: match_10_redis_cli_flushdb_async
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli FLUSHDB ASYNC" }
+ session: { id: s1 }
+ - name: match_11_redis_cli_flushdb
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli FLUSHDB" }
+ session: { id: s1 }
+ - name: match_12_mysqladmin_drop_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mysqladmin drop app" }
+ session: { id: s1 }
+ - name: match_13_bundle_exec_rails_db_drop
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "bundle exec rails db:drop" }
+ session: { id: s1 }
+ - name: match_14_rake_db_reset
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rake db:reset" }
+ session: { id: s1 }
+ - name: match_15_npx_prisma_migrate_reset_force
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "npx prisma migrate reset --force" }
+ session: { id: s1 }
+ - name: match_16_python3_manage_py_flush_no_input
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "python3 manage.py flush --no-input" }
+ session: { id: s1 }
+ - name: match_17_django_admin_flush
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "django-admin flush" }
+ session: { id: s1 }
+ - name: no_match_1_psql_app_c_select_from_users
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"SELECT * FROM users\"" }
+ session: { id: s1 }
+ - name: no_match_2_mysql_e_show_tables
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mysql -e \"SHOW TABLES\"" }
+ session: { id: s1 }
+ - name: no_match_3_grep_drop_migrations_001_sql
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep DROP migrations/001.sql" }
+ session: { id: s1 }
+ - name: no_match_4_cat_migrations_drop_sql
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cat migrations/drop.sql" }
+ session: { id: s1 }
+ - name: no_match_5_echo_drop_table_users
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo DROP TABLE users" }
+ session: { id: s1 }
+ - name: no_match_6_redis_cli_get_key
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli GET key" }
+ session: { id: s1 }
+ - name: no_match_7_redis_cli_get_flushall
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli GET FLUSHALL" }
+ session: { id: s1 }
+ - name: no_match_8_redis_cli_set_note_flushdb
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "redis-cli SET note FLUSHDB" }
+ session: { id: s1 }
+ - name: no_match_9_sqlite3_app_db_select_drop_table_users
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "sqlite3 app.db \"SELECT 'DROP TABLE users'\"" }
+ session: { id: s1 }
+ - name: no_match_10_rails_db_migrate
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rails db:migrate" }
+ session: { id: s1 }
+ - name: no_match_11_prisma_migrate_status
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "prisma migrate status" }
+ session: { id: s1 }
+ - name: no_match_12_psql_app_c_select_1_echo_drop_table_users
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"SELECT 1\"; echo DROP TABLE users" }
+ session: { id: s1 }
+ - name: no_match_13_mysqladmin_status_echo_drop_app
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "mysqladmin status\necho drop app" }
+ session: { id: s1 }
+ - name: no_match_14_psql_app_c_drop_table_users
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app # -c DROP TABLE users" }
+ session: { id: s1 }
+ - name: newline
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "true\npsql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: then
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if true; then psql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: do
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "for x in a; do psql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: else
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if false; then true; else psql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
+ - name: redirect_or_comment_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"DROP TABLE users;\" 2>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_2
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"DROP TABLE users;\" >/dev/null 2>&1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_3
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"DROP TABLE users;\"&>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_4
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "psql app -c \"DROP TABLE users;\" # maintenance" }
+ session: { id: s1 }
+ - name: mention_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"psql app -c \\\"DROP TABLE users;\\\"\"" }
+ session: { id: s1 }
+ - name: mention_2
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep -F \"psql app -c \\\"DROP TABLE users;\\\"\"" }
+ session: { id: s1 }
+ - name: mention_3
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git commit -m \"psql app -c \\\"DROP TABLE users;\\\"\"" }
+ session: { id: s1 }
+ - name: wrong_event_action
+ verdict: no_match
+ events:
+ - event: { action: tool.invoked }
+ command: { command: "psql app -c \"DROP TABLE users;\"" }
+ session: { id: s1 }
diff --git a/rules/risky-command/infrastructure-teardown.rule.yaml b/rules/risky-command/infrastructure-teardown.rule.yaml
new file mode 100644
index 00000000..60b9412b
--- /dev/null
+++ b/rules/risky-command/infrastructure-teardown.rule.yaml
@@ -0,0 +1,326 @@
+id: infrastructure-teardown
+version: 1
+title: Infrastructure destroyed or removed
+description: >
+ An agent tears down infrastructure, a namespace, a whole cloud bucket, or a
+ cloud project, resource group, cluster or stack. Plans and individual pod or
+ object deletions are excluded. These commands may be legitimate maintenance
+ but warrant review; the rule does not assert that deletion succeeded.
+severity: high
+status: experimental
+posture: detect
+taxonomy:
+ beacon_category: risky-command
+ owasp_llm: LLM06
+
+match: >
+ e.event.action == "command.executed" && (
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:terraform|tofu)(?:[ \\t]+-chdir=[^\\s;&|]+)?[ \\t]+destroy(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:pulumi|cdk)[ \\t]+destroy(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?kubectl(?:[ \\t]+(?:-n|--namespace|--context|--kubeconfig)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+delete[ \\t]+(?:namespace|namespaces|ns)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?aws(?:[ \\t]+--(?:profile|region|endpoint-url)[ \\t]+[^\\s;&|]+)*[ \\t]+(?:s3[ \\t]+rb|(?:cloudformation[ \\t]+delete-stack|eks[ \\t]+delete-cluster)|s3api[ \\t]+delete-bucket)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?aws(?:[ \\t]+--(?:profile|region|endpoint-url)[ \\t]+[^\\s;&|]+)*[ \\t]+s3[ \\t]+rm\\b[^;&|\\n#]*[ \\t]--recursive(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gcloud(?:[ \\t]+--(?:project|account|configuration)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+(?:projects[ \\t]+delete|container[ \\t]+clusters[ \\t]+delete|storage[ \\t]+buckets[ \\t]+delete)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gcloud(?:[ \\t]+--(?:project|account|configuration)(?:=|[ \\t]+)[^\\s;&|]+)*[ \\t]+storage[ \\t]+rm\\b[^;&|\\n#]*[ \\t](?:--recursive|-r)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?gsutil[ \\t]+(?:-m[ \\t]+)?(?:rb(?:[ \\t]|$|[;&|\\n<>#])|rm\\b[^;&|\\n#]*[ \\t]-[a-zA-Z]*[rR][a-zA-Z]*(?:[ \\t]|$|[;&|\\n<>#]))") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?az[ \\t]+(?:group[ \\t]+delete|aks[ \\t]+delete|storage[ \\t]+container[ \\t]+delete)(?:[ \\t]|$|[;&|\\n<>#])")
+ )
+
+emit:
+ reason: "Infrastructure destroyed or removed"
+
+tests:
+ - name: match_1_terraform_destroy_auto_approve
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroy -auto-approve" }
+ session: { id: s1 }
+ - name: match_2_tofu_chdir_infra_destroy
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "tofu -chdir=infra destroy" }
+ session: { id: s1 }
+ - name: match_3_pulumi_destroy_yes
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "pulumi destroy --yes" }
+ session: { id: s1 }
+ - name: match_4_cdk_destroy_force
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cdk destroy --force" }
+ session: { id: s1 }
+ - name: match_5_kubectl_context_prod_delete_namespace_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl --context prod delete namespace app" }
+ session: { id: s1 }
+ - name: match_6_kubectl_delete_ns_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl delete ns app" }
+ session: { id: s1 }
+ - name: match_7_aws_s3_rb_s3_company_force
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws s3 rb s3://company --force" }
+ session: { id: s1 }
+ - name: match_8_aws_s3_rm_s3_company_recursive
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws s3 rm s3://company --recursive" }
+ session: { id: s1 }
+ - name: match_9_aws_s3api_delete_bucket_bucket_company
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws s3api delete-bucket --bucket company" }
+ session: { id: s1 }
+ - name: match_10_aws_region_us_east_1_cloudformation_delete_stack_stack_name_app
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws --region us-east-1 cloudformation delete-stack --stack-name app" }
+ session: { id: s1 }
+ - name: match_11_aws_eks_delete_cluster_name_prod
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws eks delete-cluster --name prod" }
+ session: { id: s1 }
+ - name: match_12_gcloud_projects_delete_prod
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud projects delete prod" }
+ session: { id: s1 }
+ - name: match_13_gcloud_container_clusters_delete_prod
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud container clusters delete prod" }
+ session: { id: s1 }
+ - name: match_14_gcloud_storage_buckets_delete_gs_company
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud storage buckets delete gs://company" }
+ session: { id: s1 }
+ - name: match_15_gcloud_storage_rm_recursive_gs_company
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud storage rm --recursive gs://company" }
+ session: { id: s1 }
+ - name: match_16_gsutil_m_rm_r_gs_company
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gsutil -m rm -r gs://company" }
+ session: { id: s1 }
+ - name: match_17_gsutil_rb_gs_company
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gsutil rb gs://company" }
+ session: { id: s1 }
+ - name: match_18_az_group_delete_name_prod_yes
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az group delete --name prod --yes" }
+ session: { id: s1 }
+ - name: match_19_az_aks_delete_name_prod_resource_group_prod
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az aks delete --name prod --resource-group prod" }
+ session: { id: s1 }
+ - name: match_20_az_storage_container_delete_name_data
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az storage container delete --name data" }
+ session: { id: s1 }
+ - name: no_match_1_terraform_plan
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform plan" }
+ session: { id: s1 }
+ - name: no_match_2_terraform_plan_destroy
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform plan -destroy" }
+ session: { id: s1 }
+ - name: no_match_3_tofu_plan_destroy
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "tofu plan -destroy" }
+ session: { id: s1 }
+ - name: no_match_4_terraform_state_list
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform state list" }
+ session: { id: s1 }
+ - name: no_match_5_kubectl_delete_pod_old_pod
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl delete pod old-pod" }
+ session: { id: s1 }
+ - name: no_match_6_kubectl_get_namespace
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl get namespace" }
+ session: { id: s1 }
+ - name: no_match_7_aws_s3_rm_s3_company_one_txt
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws s3 rm s3://company/one.txt" }
+ session: { id: s1 }
+ - name: no_match_8_gcloud_storage_rm_gs_company_one_txt
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "gcloud storage rm gs://company/one.txt" }
+ session: { id: s1 }
+ - name: no_match_9_aws_cloudformation_list_stacks
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws cloudformation list-stacks" }
+ session: { id: s1 }
+ - name: no_match_10_az_group_list
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "az group list" }
+ session: { id: s1 }
+ - name: no_match_11_terraform_plan_echo_destroy
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform plan; echo destroy" }
+ session: { id: s1 }
+ - name: no_match_12_terraform_plan_echo_destroy
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform plan\necho destroy" }
+ session: { id: s1 }
+ - name: no_match_13_aws_s3_ls_echo_recursive
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "aws s3 ls; echo --recursive" }
+ session: { id: s1 }
+ - name: no_match_14_terraform_plan_destroy
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform plan # destroy" }
+ session: { id: s1 }
+ - name: no_match_15_terraform_destroyer
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroyer" }
+ session: { id: s1 }
+ - name: no_match_16_kubectl_delete_configmap_app
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl delete configmap app" }
+ session: { id: s1 }
+ - name: no_match_17_kubectl_get_pods_echo_delete_namespace_prod
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "kubectl get pods; echo delete namespace prod" }
+ session: { id: s1 }
+ - name: newline
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "true\nterraform destroy -auto-approve" }
+ session: { id: s1 }
+ - name: then
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if true; then terraform destroy -auto-approve" }
+ session: { id: s1 }
+ - name: do
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "for x in a; do terraform destroy -auto-approve" }
+ session: { id: s1 }
+ - name: else
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if false; then true; else terraform destroy -auto-approve" }
+ session: { id: s1 }
+ - name: redirect_or_comment_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroy -auto-approve 2>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_2
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroy -auto-approve >/dev/null 2>&1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_3
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroy -auto-approve&>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_4
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "terraform destroy -auto-approve # maintenance" }
+ session: { id: s1 }
+ - name: mention_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"terraform destroy -auto-approve\"" }
+ session: { id: s1 }
+ - name: mention_2
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep -F \"terraform destroy -auto-approve\"" }
+ session: { id: s1 }
+ - name: mention_3
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git commit -m \"terraform destroy -auto-approve\"" }
+ session: { id: s1 }
+ - name: wrong_event_action
+ verdict: no_match
+ events:
+ - event: { action: tool.invoked }
+ command: { command: "terraform destroy -auto-approve" }
+ session: { id: s1 }
diff --git a/rules/risky-command/telemetry-or-history-tampering.rule.yaml b/rules/risky-command/telemetry-or-history-tampering.rule.yaml
new file mode 100644
index 00000000..321aede7
--- /dev/null
+++ b/rules/risky-command/telemetry-or-history-tampering.rule.yaml
@@ -0,0 +1,440 @@
+id: telemetry-or-history-tampering
+version: 1
+title: Beacon telemetry or shell history disabled or removed
+description: >
+ An agent deletes or truncates Beacon data, removes its managed plugin files,
+ stops its services, uninstalls or disconnects it, or clears/disables shell
+ history. Legitimate cleanup and maintenance can also match. Reading logs or
+ history and querying service status do not match. Detection requires the
+ command to have reached the runtime log before collection was interrupted.
+severity: high
+status: experimental
+posture: detect
+taxonomy:
+ beacon_category: risky-command
+ owasp_llm: LLM06
+
+match: >
+ e.event.action == "command.executed" && (
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:rm|unlink|shred)\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?(?:(?:[^\\s;&|\\\"\\x27<>]*/)?(?:\\.beacon(?:/[^\\s;&|\\\"\\x27<>]*)?|runtime\\.jsonl(?:\\.[^\\s;&|\\\"\\x27<>]*)?|var/log/beacon-agent(?:/[^\\s;&|\\\"\\x27<>]*)?)|[^\\s;&|\\\"\\x27<>]*/(?:plugins?|extensions)/beacon(?:-endpoint)?(?:\\.[jt]s|/[^\\s;&|\\\"\\x27<>]*)?|[^\\s;&|\\\"\\x27<>]*/\\.(?:bash_history|zsh_history|sh_history))[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?truncate\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?(?:(?:[^\\s;&|\\\"\\x27<>]*/)?(?:\\.beacon(?:/[^\\s;&|\\\"\\x27<>]*)?|runtime\\.jsonl(?:\\.[^\\s;&|\\\"\\x27<>]*)?|var/log/beacon-agent(?:/[^\\s;&|\\\"\\x27<>]*)?)|[^\\s;&|\\\"\\x27<>]*/(?:plugins?|extensions)/beacon(?:-endpoint)?(?:\\.[jt]s|/[^\\s;&|\\\"\\x27<>]*)?|[^\\s;&|\\\"\\x27<>]*/\\.(?:bash_history|zsh_history|sh_history))[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:cat[ \\t]+/dev/null|:|true|(?:echo|printf)(?:[ \\t]+[\\\"\\x27][\\\"\\x27])?)[ \\t]*>[ \\t]*[\\\"\\x27]?(?:(?:[^\\s;&|\\\"\\x27<>]*/)?(?:\\.beacon(?:/[^\\s;&|\\\"\\x27<>]*)?|runtime\\.jsonl(?:\\.[^\\s;&|\\\"\\x27<>]*)?|var/log/beacon-agent(?:/[^\\s;&|\\\"\\x27<>]*)?)|[^\\s;&|\\\"\\x27<>]*/(?:plugins?|extensions)/beacon(?:-endpoint)?(?:\\.[jt]s|/[^\\s;&|\\\"\\x27<>]*)?|[^\\s;&|\\\"\\x27<>]*/\\.(?:bash_history|zsh_history|sh_history))[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?>[ \\t]*[\\\"\\x27]?(?:(?:[^\\s;&|\\\"\\x27<>]*/)?(?:\\.beacon(?:/[^\\s;&|\\\"\\x27<>]*)?|runtime\\.jsonl(?:\\.[^\\s;&|\\\"\\x27<>]*)?|var/log/beacon-agent(?:/[^\\s;&|\\\"\\x27<>]*)?)|[^\\s;&|\\\"\\x27<>]*/(?:plugins?|extensions)/beacon(?:-endpoint)?(?:\\.[jt]s|/[^\\s;&|\\\"\\x27<>]*)?|[^\\s;&|\\\"\\x27<>]*/\\.(?:bash_history|zsh_history|sh_history))[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?systemctl[ \\t]+(?:--user[ \\t]+)?(?:stop|disable|mask|kill)\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?(?:[^\\s;&|\\\"\\x27]*/)?(?:com\\.beacon\\.endpoint\\.[\\w.*-]+|beacon-collector(?:\\.service)?|beacon-otelcol)[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?service[ \\t]+beacon-collector[ \\t]+stop(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?launchctl[ \\t]+(?:unload|bootout|remove|stop|kill)\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?(?:[^\\s;&|\\\"\\x27]*/)?(?:com\\.beacon\\.endpoint\\.[\\w.*-]+|beacon-collector(?:\\.service)?|beacon-otelcol)[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:pkill|killall)\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?(?:[^\\s;&|\\\"\\x27]*/)?(?:com\\.beacon\\.endpoint\\.[\\w.*-]+|beacon-collector(?:\\.service)?|beacon-otelcol)[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?beacon[ \\t]+endpoint[ \\t]+(?:uninstall|disconnect|hooks[ \\t]+uninstall)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?history[ \\t]+(?:-c|-w[ \\t]+/dev/null)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:unset[ \\t]+HISTFILE|(?:export[ \\t]+)?(?:HISTFILE[ \\t]*=[ \\t]*(?:/dev/null|[\\\"\\x27][\\\"\\x27])|(?:HISTSIZE|HISTFILESIZE)[ \\t]*=[ \\t]*0))(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:export[ \\t]+)?HISTFILE=[ \\t]*(?:$|[;&|\\n#]|[0-9]*[<>]|&>)") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?set[ \\t]+\\+o[ \\t]+history(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?(?:rm|unlink|shred|truncate)\\b[^;&|\\n#]*[ \\t][\\\"\\x27]?[^\\s;&|\\\"\\x27]*/\\.(?:bash_history|zsh_history|sh_history)[\\\"\\x27]?(?:[ \\t]|$|[;&|\\n<>#])")
+ )
+
+emit:
+ reason: "Beacon telemetry or shell history disabled or removed"
+
+tests:
+ - name: match_1_rm_rf_beacon
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.beacon/" }
+ session: { id: s1 }
+ - name: match_2_rm_var_log_beacon_agent_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm /var/log/beacon-agent/runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_3_truncate_s_0_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "truncate -s 0 runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_4_beacon_endpoint_logs_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: ": > ~/.beacon/endpoint/logs/runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_5_cat_dev_null_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cat /dev/null > runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_6_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: ">runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_7_echo_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"\" > runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_8_systemctl_stop_beacon_collector_service
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "systemctl stop beacon-collector.service" }
+ session: { id: s1 }
+ - name: match_9_service_beacon_collector_stop
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "service beacon-collector stop" }
+ session: { id: s1 }
+ - name: match_10_systemctl_user_disable_beacon_collector_service
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "systemctl --user disable beacon-collector.service" }
+ session: { id: s1 }
+ - name: match_11_launchctl_unload_library_launchagents_com_beacon_endpoint_collector_plist
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "launchctl unload ~/Library/LaunchAgents/com.beacon.endpoint.collector.plist" }
+ session: { id: s1 }
+ - name: match_12_launchctl_bootout_gui_501_com_beacon_endpoint_collector
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "launchctl bootout gui/501/com.beacon.endpoint.collector" }
+ session: { id: s1 }
+ - name: match_13_pkill_f_beacon_otelcol
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "pkill -f beacon-otelcol" }
+ session: { id: s1 }
+ - name: match_14_killall_beacon_otelcol
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "killall beacon-otelcol" }
+ session: { id: s1 }
+ - name: match_15_beacon_endpoint_uninstall
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "beacon endpoint uninstall" }
+ session: { id: s1 }
+ - name: match_16_beacon_endpoint_disconnect
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "beacon endpoint disconnect" }
+ session: { id: s1 }
+ - name: match_17_beacon_endpoint_hooks_uninstall
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "beacon endpoint hooks uninstall" }
+ session: { id: s1 }
+ - name: match_18_rm_config_opencode_plugins_beacon_ts
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ~/.config/opencode/plugins/beacon.ts" }
+ session: { id: s1 }
+ - name: match_19_rm_cline_plugins_beacon_ts
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ~/.cline/plugins/beacon.ts" }
+ session: { id: s1 }
+ - name: match_20_rm_pi_agent_extensions_beacon_ts
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ~/.pi/agent/extensions/beacon.ts" }
+ session: { id: s1 }
+ - name: match_21_rm_rf_openclaw_extensions_beacon_endpoint
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.openclaw/extensions/beacon-endpoint" }
+ session: { id: s1 }
+ - name: match_22_launchctl_kill_sigterm_gui_501_com_beacon_endpoint_collector
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "launchctl kill SIGTERM gui/501/com.beacon.endpoint.collector" }
+ session: { id: s1 }
+ - name: match_23_history_c
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "history -c" }
+ session: { id: s1 }
+ - name: match_24_histfile
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "HISTFILE=" }
+ session: { id: s1 }
+ - name: match_25_export_histfile
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "export HISTFILE=" }
+ session: { id: s1 }
+ - name: match_26_unset_histfile
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "unset HISTFILE" }
+ session: { id: s1 }
+ - name: match_27_export_histfile_dev_null
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "export HISTFILE=/dev/null" }
+ session: { id: s1 }
+ - name: match_28_histsize_0
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "HISTSIZE=0" }
+ session: { id: s1 }
+ - name: match_29_printf_runtime_jsonl
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "printf \"\" > runtime.jsonl" }
+ session: { id: s1 }
+ - name: match_30_bash_history
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: ": > ~/.bash_history" }
+ session: { id: s1 }
+ - name: match_31_cat_dev_null_zsh_history
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cat /dev/null > ~/.zsh_history" }
+ session: { id: s1 }
+ - name: match_32_set_o_history
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "set +o history" }
+ session: { id: s1 }
+ - name: match_33_rm_zsh_history
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ~/.zsh_history" }
+ session: { id: s1 }
+ - name: no_match_1_beacon_endpoint_status
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "beacon endpoint status" }
+ session: { id: s1 }
+ - name: no_match_2_tail_f_beacon_endpoint_logs_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "tail -f ~/.beacon/endpoint/logs/runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_3_cat_var_log_beacon_agent_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cat /var/log/beacon-agent/runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_4_systemctl_status_beacon_collector_service
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "systemctl status beacon-collector.service" }
+ session: { id: s1 }
+ - name: no_match_5_launchctl_print_gui_501_com_beacon_endpoint_collector
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "launchctl print gui/501/com.beacon.endpoint.collector" }
+ session: { id: s1 }
+ - name: no_match_6_history
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "history" }
+ session: { id: s1 }
+ - name: no_match_7_history_10
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "history 10" }
+ session: { id: s1 }
+ - name: no_match_8_rm_notes_txt_cat_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm notes.txt; cat runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_9_rm_notes_txt_echo_beacon
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm notes.txt\necho ~/.beacon/" }
+ session: { id: s1 }
+ - name: no_match_10_systemctl_status_beacon_collector_service_echo_stop
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "systemctl status beacon-collector.service; echo stop" }
+ session: { id: s1 }
+ - name: no_match_11_rm_plugins_unrelated_ts
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ./plugins/unrelated.ts" }
+ session: { id: s1 }
+ - name: no_match_12_echo_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_13_cat_runtime_jsonl_backup_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "cat runtime.jsonl > backup.jsonl" }
+ session: { id: s1 }
+ - name: no_match_14_echo_hello_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo hello >> runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_15_histsize_1000
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "HISTSIZE=1000" }
+ session: { id: s1 }
+ - name: no_match_16_set_o_history
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "set -o history" }
+ session: { id: s1 }
+ - name: no_match_17_rm_file_runtime_jsonl
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm file # runtime.jsonl" }
+ session: { id: s1 }
+ - name: no_match_18_systemctl_status_stop_beacon_collector_service
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "systemctl status # stop beacon-collector.service" }
+ session: { id: s1 }
+ - name: no_match_19_service_beacon_collector_status
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "service beacon-collector status" }
+ session: { id: s1 }
+ - name: no_match_20_echo_history_c
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo history -c" }
+ session: { id: s1 }
+ - name: no_match_21_rm_beacon_backup
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm ~/.beacon-backup" }
+ session: { id: s1 }
+ - name: no_match_22_rm_runtime_jsonl_notes
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm runtime.jsonl-notes" }
+ session: { id: s1 }
+ - name: newline
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "true\nrm -rf ~/.beacon/" }
+ session: { id: s1 }
+ - name: then
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if true; then rm -rf ~/.beacon/" }
+ session: { id: s1 }
+ - name: do
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "for x in a; do rm -rf ~/.beacon/" }
+ session: { id: s1 }
+ - name: else
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if false; then true; else rm -rf ~/.beacon/" }
+ session: { id: s1 }
+ - name: redirect_or_comment_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.beacon/ 2>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_2
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.beacon/ >/dev/null 2>&1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_3
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.beacon/&>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_4
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "rm -rf ~/.beacon/ # maintenance" }
+ session: { id: s1 }
+ - name: mention_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"rm -rf ~/.beacon/\"" }
+ session: { id: s1 }
+ - name: mention_2
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep -F \"rm -rf ~/.beacon/\"" }
+ session: { id: s1 }
+ - name: mention_3
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git commit -m \"rm -rf ~/.beacon/\"" }
+ session: { id: s1 }
+ - name: wrong_event_action
+ verdict: no_match
+ events:
+ - event: { action: tool.invoked }
+ command: { command: "rm -rf ~/.beacon/" }
+ session: { id: s1 }
diff --git a/rules/source-control/git-local-work-discarded.rule.yaml b/rules/source-control/git-local-work-discarded.rule.yaml
new file mode 100644
index 00000000..8a4b1a2d
--- /dev/null
+++ b/rules/source-control/git-local-work-discarded.rule.yaml
@@ -0,0 +1,366 @@
+id: git-local-work-discarded
+version: 1
+title: Local Git work discarded
+description: >
+ An agent hard-resets Git, force-cleans directories or ignored files, restores
+ the whole working tree, or clears every stash. The command expresses intent
+ to discard work; telemetry alone cannot establish whether uncommitted work
+ existed. Dry-run clean, branch checkout and staged-only restores are excluded.
+severity: medium
+status: experimental
+posture: detect
+taxonomy:
+ beacon_category: source-control
+ owasp_llm: LLM06
+
+match: >
+ e.event.action == "command.executed" && (
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+reset\\b[^;&|\\n#]*[ \\t]--hard(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+clean[ \\t]+(?:-[dfxXq]+[ \\t]+)*(?:-[dxXq]*f[dfxXq]*[dxX][dfxXq]*|-[dfxXq]*[dxX][dfxXq]*f[dfxXq]*|-[dfxXq]*f[dfxXq]*[ \\t]+(?:-[dfxXq]+[ \\t]+)*-[dfxXq]*[dxX][dfxXq]*|-[dfxXq]*[dxX][dfxXq]*[ \\t]+(?:-[dfxXq]+[ \\t]+)*-[dfxXq]*f[dfxXq]*)(?:[ \\t]+-[dfxXq]+)*(?:[ \\t]+--)?(?:[ \\t]+[^\\s;&|<>#-][^\\s;&|<>#]*)*[ \\t]*(?:$|[;&|\\n#]|[0-9]*[<>]|&>)") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+checkout[ \\t]+(?:HEAD[ \\t]+)?(?:--[ \\t]+)?[\\\"\\x27]?(?:\\.|:/)[\\\"\\x27]?[ \\t]*(?:$|[;&|\\n#]|[0-9]*[<>]|&>)") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+checkout[ \\t]+(?:-f|--force)(?:[ \\t]|$|[;&|\\n<>#])") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+restore[ \\t]+(?:(?:(?:--source|-s)(?:=|[ \\t]+)[^\\s;&|]+|--worktree|-W)[ \\t]+)*(?:--[ \\t]+)?[\\\"\\x27]?(?:\\.|:/)[\\\"\\x27]?[ \\t]*(?:$|[;&|\\n#]|[0-9]*[<>]|&>)") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+restore[ \\t]+(?:(?:--staged|-S)[ \\t]+)?(?:--worktree|-W|-SW|-WS)[ \\t]+(?:(?:--staged|-S)[ \\t]+)?(?:--[ \\t]+)?[\\\"\\x27]?(?:\\.|:/)[\\\"\\x27]?[ \\t]*(?:$|[;&|\\n#]|[0-9]*[<>]|&>)") ||
+ e.command.command.matches("(^|[;&|\\n])[ \\t]*(?:(?:then|do|else)[ \\t]+)?(?:sudo[ \\t]+)?(?:/[^\\s;&|]*/)?git(?:[ \\t]+-C[ \\t]+[^\\s;&|]+)*[ \\t]+stash[ \\t]+clear(?:[ \\t]|$|[;&|\\n<>#])")
+ )
+
+emit:
+ reason: "Local Git work discarded"
+
+tests:
+ - name: match_1_git_reset_hard_head_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --hard HEAD~1" }
+ session: { id: s1 }
+ - name: match_2_git_c_repo_reset_hard
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git -C repo reset --hard" }
+ session: { id: s1 }
+ - name: match_3_git_clean_fd
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fd" }
+ session: { id: s1 }
+ - name: match_4_git_clean_fx
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fX" }
+ session: { id: s1 }
+ - name: match_5_git_clean_xdf
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -xdf" }
+ session: { id: s1 }
+ - name: match_6_git_clean_f_d
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -f -d" }
+ session: { id: s1 }
+ - name: match_7_git_clean_x_f
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -x -f" }
+ session: { id: s1 }
+ - name: match_8_git_clean_ffdx_build
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -ffdx -- build" }
+ session: { id: s1 }
+ - name: match_9_git_checkout
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout -- ." }
+ session: { id: s1 }
+ - name: match_10_git_checkout
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout ." }
+ session: { id: s1 }
+ - name: match_11_git_checkout_f_main
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout -f main" }
+ session: { id: s1 }
+ - name: match_12_git_restore
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore ." }
+ session: { id: s1 }
+ - name: match_13_git_restore_worktree
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --worktree -- ." }
+ session: { id: s1 }
+ - name: match_14_git_stash_clear
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git stash clear" }
+ session: { id: s1 }
+ - name: match_15_git_checkout_head
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout HEAD -- ." }
+ session: { id: s1 }
+ - name: match_16_git_restore_source_head
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --source=HEAD -- ." }
+ session: { id: s1 }
+ - name: match_17_git_restore_staged_worktree
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --staged --worktree ." }
+ session: { id: s1 }
+ - name: match_18_git_restore_sw
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore -SW ." }
+ session: { id: s1 }
+ - name: match_19_git_restore_dev_null
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore .>/dev/null" }
+ session: { id: s1 }
+ - name: match_20_git_checkout_discard
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout -- . # discard" }
+ session: { id: s1 }
+ - name: match_21_git_clean_fd_2_dev_null
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fd 2>/dev/null" }
+ session: { id: s1 }
+ - name: match_22_git_checkout
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout -- \".\"" }
+ session: { id: s1 }
+ - name: no_match_1_git_reset_soft_head_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --soft HEAD~1" }
+ session: { id: s1 }
+ - name: no_match_2_git_reset_mixed
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --mixed" }
+ session: { id: s1 }
+ - name: no_match_3_git_clean_nfd
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -nfd" }
+ session: { id: s1 }
+ - name: no_match_4_git_clean_fdn
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fdn" }
+ session: { id: s1 }
+ - name: no_match_5_git_clean_n_fd
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -n -fd" }
+ session: { id: s1 }
+ - name: no_match_6_git_clean_fd_n
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fd -n" }
+ session: { id: s1 }
+ - name: no_match_7_git_clean_f
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -f" }
+ session: { id: s1 }
+ - name: no_match_8_git_clean_d
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -d" }
+ session: { id: s1 }
+ - name: no_match_9_git_checkout_feature
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout feature" }
+ session: { id: s1 }
+ - name: no_match_10_git_checkout_file_go
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git checkout -- file.go" }
+ session: { id: s1 }
+ - name: no_match_11_git_restore_staged_file_go
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --staged file.go" }
+ session: { id: s1 }
+ - name: no_match_12_git_restore_staged
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --staged ." }
+ session: { id: s1 }
+ - name: no_match_13_git_restore_file_go
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore file.go" }
+ session: { id: s1 }
+ - name: no_match_14_git_stash_list
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git stash list" }
+ session: { id: s1 }
+ - name: no_match_15_git_status_echo_hard
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git status; echo --hard" }
+ session: { id: s1 }
+ - name: no_match_16_git_reset_echo_hard
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset\necho --hard" }
+ session: { id: s1 }
+ - name: no_match_17_git_clean_f_echo_d
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -f; echo -d" }
+ session: { id: s1 }
+ - name: no_match_18_git_reset_hard
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset # --hard" }
+ session: { id: s1 }
+ - name: no_match_19_git_clean_fd_dry_run
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fd --dry-run" }
+ session: { id: s1 }
+ - name: no_match_20_git_clean_nfd
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -Nfd" }
+ session: { id: s1 }
+ - name: no_match_21_git_clean_fd_build_n
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git clean -fd build -n" }
+ session: { id: s1 }
+ - name: no_match_22_git_restore_source_head_staged
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git restore --source=HEAD --staged ." }
+ session: { id: s1 }
+ - name: newline
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "true\ngit reset --hard HEAD~1" }
+ session: { id: s1 }
+ - name: then
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if true; then git reset --hard HEAD~1" }
+ session: { id: s1 }
+ - name: do
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "for x in a; do git reset --hard HEAD~1" }
+ session: { id: s1 }
+ - name: else
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "if false; then true; else git reset --hard HEAD~1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_1
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --hard HEAD~1 2>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_2
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --hard HEAD~1 >/dev/null 2>&1" }
+ session: { id: s1 }
+ - name: redirect_or_comment_3
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --hard HEAD~1&>/dev/null" }
+ session: { id: s1 }
+ - name: redirect_or_comment_4
+ verdict: match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git reset --hard HEAD~1 # maintenance" }
+ session: { id: s1 }
+ - name: mention_1
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "echo \"git reset --hard HEAD~1\"" }
+ session: { id: s1 }
+ - name: mention_2
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "grep -F \"git reset --hard HEAD~1\"" }
+ session: { id: s1 }
+ - name: mention_3
+ verdict: no_match
+ events:
+ - event: { action: command.executed }
+ command: { command: "git commit -m \"git reset --hard HEAD~1\"" }
+ session: { id: s1 }
+ - name: wrong_event_action
+ verdict: no_match
+ events:
+ - event: { action: tool.invoked }
+ command: { command: "git reset --hard HEAD~1" }
+ session: { id: s1 }