Skip to content

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

5.5 KB
Raw Blame
id: path-injection-into-login-file
version: 2
title: PATH shadowing entry written to a shell startup file
description: >
  A command writes a PATH line into a shell startup or login file, by redirecting or
  tee-ing into it, and that line adds a directory anyone can plant binaries in: /tmp,
  /var/tmp or /dev/shm, the current directory (`.` or an empty entry such as
  `PATH=:$PATH`), or a relative path. Such an entry shadows system binaries on every
  new shell or login, outside any agent sandbox and with no further prompt. Ordinary
  tool directories (`$HOME/.local/bin`, `.cargo/bin`, `/usr/local/go/bin`) do not fire:
  a command alone cannot tell them from an attacker's absolute directory.
severity: high
status: stable
posture: detect
taxonomy:
  beacon_category: risky-command
  owasp_llm: LLM05
  mitre_atlas: AML.T0011

match: >
  e.event.action == "command.executed" &&
  e.command.command.matches("(?i)(>>?|\\btee\\b)\\s*([^|;&]*[\\s/])?(\\.bashrc|\\.bash_profile|\\.bash_login|\\.profile|\\.zshrc|\\.zprofile|\\.zshenv|\\.zlogin|\\.kshrc|\\.cshrc|\\.tcshrc|config\\.fish|etc/profile|profile\\.d/[^\\s;&|\"']+)([\"'\\s]|$)") && (
    e.command.command.matches("(?i)\\bPATH\\s*\\+?=\\s*[\"']?([^\\s;&|\"']*:)?(/tmp|/var/tmp|/dev/shm)(/[^\\s:;&|\"']*)?(:|[\"'\\s]|$)") ||
    e.command.command.matches("(?i)\\bPATH\\s*\\+?=\\s*[\"']?(\\.?:|[^\\s;&|\"']*:\\.?(:|[\"'\\s]|$))") ||
    e.command.command.matches("(?i)\\bPATH\\s*\\+?=\\s*[\"']?([^\\s;&|\"']*:)?([A-Za-z0-9_]|\\.\\.?/|\\.[A-Za-z0-9_])[^\\s:;&|\"']*(:|[\"'\\s]|$)") ||
    e.command.command.matches("(?i)\\bset\\s+-[gxU]+\\s+PATH\\b[^;&|]*\\s((/tmp|/var/tmp|/dev/shm)(/[^\\s;&|\"']*)?|\\.)([\"'\\s]|$)")
  )

emit:
  reason: "PATH entry that shadows system binaries (world-writable, relative or current directory) written to a shell startup/login file"

tests:
  - name: export_path_into_bashrc
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=/tmp/bin:$PATH' >> ~/.bashrc" }
        session: { id: s1 }
  - name: path_assign_into_zshrc
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "printf 'PATH=.:$PATH\\n' >> /home/dev/.zshrc" }
        session: { id: s1 }
  - name: fish_set_path_into_config
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'set -gx PATH /tmp/bin $PATH' >> ~/.config/fish/config.fish" }
        session: { id: s1 }
  - name: profile_d_dropin
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=/dev/shm/.x:$PATH' >> /etc/profile.d/custom.sh" }
        session: { id: s1 }
  - name: export_path_tee_into_bashrc
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=/tmp/bin:$PATH' | tee -a ~/.bashrc" }
        session: { id: s1 }
  - name: path_tee_into_profile_d
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=bin:$PATH' | sudo tee /etc/profile.d/custom.sh" }
        session: { id: s1 }
  - name: empty_entry_appended_to_path
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=$PATH:' >> ~/.bash_profile" }
        session: { id: s1 }
  - name: current_directory_appended_to_path
    verdict: match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=\"$PATH:.\"' >> ~/.zshrc" }
        session: { id: s1 }
  - name: local_bin_tool_install
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=\"$HOME/.local/bin:$PATH\"' | tee -a ~/.zshrc" }
        session: { id: s1 }
  - name: cargo_bin_tool_install
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=\"$HOME/.cargo/bin:$PATH\"' >> ~/.bashrc" }
        session: { id: s1 }
  - name: go_bin_appended_system_wide
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile" }
        session: { id: s1 }
  - name: build_log_named_like_profile
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "PATH=bin:$PATH ./build.sh | tee ~/.profile-build.log" }
        session: { id: s1 }
  - name: tmp_mentioned_outside_path_value
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc && cd /tmp" }
        session: { id: s1 }
  - name: bashrc_read_then_tee_elsewhere
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "grep PATH ~/.bashrc | tee path-notes.txt" }
        session: { id: s1 }
  - name: ephemeral_path_export_no_redirect
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "export PATH=/usr/local/bin:$PATH" }
        session: { id: s1 }
  - name: path_written_to_ordinary_file
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "echo 'export PATH=/opt/bin:$PATH' >> setup-notes.txt" }
        session: { id: s1 }
  - name: bashrc_read_only
    verdict: no_match
    events:
      - event: { action: command.executed }
        command: { command: "grep PATH ~/.bashrc" }
        session: { id: s1 }