Skip to content

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

22.9 KB
Raw Blame
id: secret-read-then-destructive
spec: threat-rules/v1.1  # needed for correlation.order
version: 3
title: Secret-file read and recursive delete in one session
description: >
  Within one session, the agent reads a credential-bearing file and runs a recursive
  delete with a path argument within a short window of it, in either order: an
  `rm -r`/`rm -rf`/`rm --recursive` of a path, a `find <path> ... -delete`, or a
  `find <path> ... -exec rm`. It is the steal-then-destroy shape (take the secret,
  then wipe logs or the workspace to cover it), and it covers the scoped deletes
  such as `rm -rf /var/log/* /work/*` that recursive-root-delete leaves alone
  because they do not target a root, home, or bare wildcard.

  The read step is the credential-file-read expression verbatim, so the two rules
  agree on what a secret is; a test in the conformance suite fails if they drift.

  A delete is ignored when the whole command only removes common build and cache
  output (node_modules, dist, build, target, .next, __pycache__, and similar,
  optionally after a `cd`), and a `git rm` is not a filesystem delete. Anything
  else, including a build-dir delete chained with another delete, still fires.
  Experimental and high rather than critical: a developer can read a .env and
  clean a scratch directory in the same five minutes, so this is a strong lead
  rather than proof, and it should earn critical only after real-world tuning.
severity: high
status: experimental
posture: detect
taxonomy:
  beacon_category: context-exfiltration
  owasp_llm: LLM08

correlation:
  scope: session
  window: 300s
  order: any
  steps:
    - id: read_secret
      match: >
        (
          e.event.action == "file.read" && (
            (e.file.path.matches("(?i)(^|/)\\.env($|\\.)") && !e.file.path.matches("(?i)(^|/)\\.env(\\.[\\w-]+)*\\.(example|sample|template|dist)$")) ||
            e.file.path.matches("(?i)\\.aws/credentials") ||
            e.file.path.matches("(?i)(^|/)id_(rsa|ed25519|ecdsa|dsa)$") ||
            e.file.path.matches("(?i)(^|/)\\.git-credentials$") ||
            e.file.path.matches("(?i)/\\.config/git/credentials$") ||
            e.file.path.matches("(?i)(^|/)(\\.netrc|\\.pgpass)$") ||
            e.file.path.matches("(?i)(^|/)credentials(\\.json|\\.toml)?$") ||
            e.file.path.matches("(?i)(^|/)(\\.npmrc|\\.pypirc)$") ||
            e.file.path.matches("(?i)(^|/)\\.docker/config\\.json$") ||
            e.file.path.matches("(?i)(^|/)\\.config/gh/hosts\\.yml$") ||
            e.file.path.matches("(?i)(^|/)application_default_credentials\\.json$") ||
            e.file.path.matches("(?i)/\\.config/gcloud/(credentials|access_tokens)\\.db$") ||
            e.file.path.matches("(?i)/\\.azure/(msal_token_cache|accesstokens)\\.json$") ||
            e.file.path.matches("(?i)\\.tfstate(\\.backup)?$") ||
            e.file.path.matches("(?i)/\\.terraform\\.d/credentials\\.tfrc\\.json$")
          )
        ) || (
          e.event.action == "command.executed" &&
          (e.command.command.matches("(?i)(^|[;&|(`]|\\$\\()\\s*(sudo\\s+)?(cat|tac|head|tail|less|more|bat|nl|strings|xxd|od|hexdump|base64)\\b[^;&|>\\n]*?[\\s/'\"=](\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)($|[\\s'\";|&)<>])") || e.command.command.matches("(^|[;&|(`]|\\$\\()\\s*(sudo\\s+)?tar(?:\\s+[A-Za-z]*[cru][A-Za-z]*|(?:\\s+(?:(?:[^\\s;&|>-][^\\s;&|>]*|-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))|--exclude(?:=[^\\s;&|>]*|\\s+[^\\s;&|>]+)|-x(?:\\s+[^\\s;&|>-][^\\s;&|>]*)*\\s+(?:-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))))*?\\s+(?:-[A-Za-z]*[cru][A-Za-z]*|--(?:create|append|update)))(?:\\s+(?:(?:[^\\s;&|>-][^\\s;&|>]*|-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))|--exclude(?:=[^\\s;&|>]*|\\s+[^\\s;&|>]+)|-x(?:\\s+[^\\s;&|>-][^\\s;&|>]*)*\\s+(?:-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))))*?\\s+['\"]?(?:[^\\s;&|>'\"=-][^\\s;&|>'\"]*/)?(?i:\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)($|[\\s'\";|&)<>])") || e.command.command.matches("(^|[;&|(`]|\\$\\()\\s*(sudo\\s+)?zip(?:\\s+(?:(?:[^\\s;&|>-][^\\s;&|>]*|-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))|--exclude(?:=[^\\s;&|>]*|\\s+[^\\s;&|>]+)|-x(?:\\s+[^\\s;&|>-][^\\s;&|>]*)*\\s+(?:-|-[^\\s;&|>x-][^\\s;&|>]*|-x[^\\s;&|>]+|--(?:|[^\\s;&|>e][^\\s;&|>]*|e|e[^\\s;&|>x][^\\s;&|>]*|ex|ex[^\\s;&|>c][^\\s;&|>]*|exc|exc[^\\s;&|>l][^\\s;&|>]*|excl|excl[^\\s;&|>u][^\\s;&|>]*|exclu|exclu[^\\s;&|>d][^\\s;&|>]*|exclud|exclud[^\\s;&|>e][^\\s;&|>]*|exclude[^\\s;&|>]+))))*?\\s+['\"]?(?:[^\\s;&|>'\"=-][^\\s;&|>'\"]*/)?(?i:\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)($|[\\s'\";|&)<>])") || e.command.command.matches("(?i)(^|[;&|(`]|\\$\\()\\s*(sudo\\s+)?cp\\b[^;&|>\\n]*?[\\s/'\"=](\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)['\"]?\\s+[^\\s;&|<>]") || e.command.command.matches("(?i)(^|[;&|(`]|\\$\\()\\s*(sudo\\s+)?(grep|egrep|fgrep|rg|awk|sed|jq)\\b[^;&|>\\n]*?(\\s['\"]?[^\\s;&|'\"]*/(\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)($|[\\s'\";|&)<>])|\\s[^\\s-][^\\s;&|>]*\\s+(\\.env((\\.[\\w.-]*)?\\.(?:[-0-9_abcfghijklmnopqruvwxyz][\\w-]*|d|d[-0-9_abcdefghjklmnopqrstuvwxyz][\\w-]*|di|di[-0-9_abcdefghijklmnopqrtuvwxyz][\\w-]*|dis|dis[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|dist[\\w-]+|e|e[-0-9_abcdefghijklmnopqrstuvwyz][\\w-]*|ex|ex[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|exa|exa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|exam|exam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|examp|examp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|exampl|exampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|example[\\w-]+|s|s[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|sa|sa[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|sam|sam[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|samp|samp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|sampl|sampl[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|sample[\\w-]+|t|t[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|te|te[-0-9_abcdefghijklnopqrstuvwxyz][\\w-]*|tem|tem[-0-9_abcdefghijklmnoqrstuvwxyz][\\w-]*|temp|temp[-0-9_abcdefghijkmnopqrstuvwxyz][\\w-]*|templ|templ[-0-9_bcdefghijklmnopqrstuvwxyz][\\w-]*|templa|templa[-0-9_abcdefghijklmnopqrsuvwxyz][\\w-]*|templat|templat[-0-9_abcdfghijklmnopqrstuvwxyz][\\w-]*|template[\\w-]+))?|\\.aws/credentials|id_(rsa|ed25519|ecdsa|dsa)|\\.git-credentials|\\.netrc|\\.pgpass|credentials(\\.json|\\.toml)?|\\.npmrc|\\.pypirc|\\.docker/config\\.json|\\.config/gh/hosts\\.yml|application_default_credentials\\.json|\\.config/gcloud/(credentials|access_tokens)\\.db|\\.azure/(msal_token_cache|accesstokens)\\.json|[\\w.-]*\\.tfstate(\\.backup)?|\\.terraform\\.d/credentials\\.tfrc\\.json)\\s*($|[;&|)]))"))
        )
    - id: recursive_delete
      match: >
        e.event.action == "command.executed" && (
          e.command.command.matches("(?i)(^|[;&|(`]|\\bsudo|\\bxargs|/)\\s*rm(\\s+-{1,2}[a-z-]+)*\\s+(-[a-z]*r[a-z]*|--recursive)(\\s+-{1,2}[a-z-]+)*\\s+[^-;&|\\s]") ||
          e.command.command.matches("(?i)\\bfind\\s+[^-;&|\\s][^;&|]*\\s(-delete\\b|-exec(dir)?\\s+(\\S*/)?rm\\s)")
        ) &&
        !e.command.command.matches("(?i)^\\s*(cd\\s+[^;&|]+&&\\s*)?rm(\\s+-{1,2}[a-z-]+)+(\\s+['\"]?(\\./)?([\\w.-]+/)*(node_modules|dist|build|out|target|coverage|\\.next|\\.nuxt|\\.turbo|\\.cache|\\.parcel-cache|__pycache__|\\.pytest_cache|\\.mypy_cache|\\.ruff_cache|\\.gradle|\\.tox|\\.venv|venv)/?\\*?['\"]?)+\\s*$") &&
        !e.command.command.matches("(?i)^\\s*(cd\\s+[^;&|]+&&\\s*)?find\\s+\\.(\\s+-(type\\s+[fd]|name\\s+['\"]?(\\*\\.py[co]|__pycache__|\\.pytest_cache|\\.DS_Store|node_modules)['\"]?|prune|o))+\\s+(-delete|-exec\\s+rm\\s+-[a-z]+\\s+\\{\\}\\s*(\\+|\\\\?;))\\s*$")

emit:
  reason: "Credential file read and recursive delete within one session window"

tests:
  - name: env_read_then_scoped_delete
    # The #701 V04 shape. recursive-root-delete does not match it (no root, home, or
    # bare-wildcard target), so before this rule only credential-file-read fired.
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "/work/app/.env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s1 }
  - name: delete_then_aws_credentials_read
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: command.executed }
        command: { command: "sudo rm -r -f /var/log/agent" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:01:00Z"
        event: { action: file.read }
        file: { path: "/home/u/.aws/credentials" }
        session: { id: s1 }
  - name: ssh_key_read_then_find_delete
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "/home/u/.ssh/id_ed25519" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:02:00Z"
        event: { action: command.executed }
        command: { command: "find /home/u/project -type f -delete" }
        session: { id: s1 }
  - name: git_credentials_read_then_long_flag_delete
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "/home/u/.git-credentials" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:10Z"
        event: { action: command.executed }
        command: { command: "rm --recursive --force \"$HOME/work/data\"" }
        session: { id: s1 }
  - name: build_dir_delete_chained_with_real_delete
    # The build-dir exclusion covers the whole command, not one clause of it.
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:10Z"
        event: { action: command.executed }
        command: { command: "rm -rf node_modules && rm -rf /work/*" }
        session: { id: s1 }
  - name: find_exec_rm_after_netrc_read
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "/home/u/.netrc" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:10Z"
        event: { action: command.executed }
        command: { command: "find /var/log -name '*.log' -exec rm -f {} +" }
        session: { id: s1 }
  - name: build_dir_cleanup_after_env_read
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "/work/app/.env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf node_modules dist .next" }
        session: { id: s1 }
  - name: cd_then_build_cleanup_after_env_read
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env.local" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "cd web && rm -rf ./build/* .turbo" }
        session: { id: s1 }
  - name: pycache_find_cleanup_after_env_read
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "find . -name __pycache__ -type d -exec rm -rf {} +" }
        session: { id: s1 }
  - name: git_rm_is_not_a_filesystem_delete
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "git rm -r --cached vendor" }
        session: { id: s1 }
  - name: non_recursive_rm
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -f /tmp/out.log" }
        session: { id: s1 }
  - name: find_without_delete
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "find /work -name '*.log' -mtime +7" }
        session: { id: s1 }
  - name: delete_outside_window
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:05:01Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s1 }
  - name: read_and_delete_different_sessions
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s2 }
  - name: non_credential_read_then_delete
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: "README.md" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s1 }
  - name: shell_read_then_delete
    verdict: match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: command.executed }
        command: { command: "cat .env" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf ./src" }
        session: { id: s1 }
  - name: env_example_read_then_delete
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env.example" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s1 }
  - name: env_multipart_template_read_then_delete
    verdict: no_match
    events:
      - timestamp: "2026-06-13T10:00:00Z"
        event: { action: file.read }
        file: { path: ".env.local.example" }
        session: { id: s1 }
      - timestamp: "2026-06-13T10:00:30Z"
        event: { action: command.executed }
        command: { command: "rm -rf /var/log/* /work/*" }
        session: { id: s1 }