Double evaluation in .bash_prompt of dotfiles allows a malicious repository to execute arbitrary commands
The dotfiles .bash_prompt script builds the shell prompt by embedding the output of git and svn status commands into PS1, which bash then re-evaluates. A malicious repository with a crafted branch name can therefore run arbitrary commands when the victim changes into that directory.
- Triaged in 18 hours
- Fixed in 3 days
- Paid in 18 hours
- Disclosed after 3 days
Summary
Due to the improper usage of the PS1 environment variable in .bash_prompt of dotfiles, a malicious repository can execute arbitrary commands when changed the current directory to it.
Description
The PS1 environment variable of bash supports command substitutions. For example, setting PS1 to $(echo hello) executes echo hello each time the prompt is displayed.
Because .bash_prompt of dotfiles uses the following code to display the VCS information, if any outputs of these commands contain command substitution syntaxes, it'll be evaluated while printing the prompt.
export PS1="\n${command_mark}${color_user_host}\u${color_reset} @ ${color_user_host}$hostname${color_reset} in ${color_folder}\w${color_reset} \
$(vcs_prompt) \
\n> "
Since vcs_prompt contains the information of Git or SVN, a malicious repository with a crafted branch name can execute arbitrary commands.
function vcs_prompt {
GIT_PROMPT=$(git_status)
SVN_PROMPT=$(svn_status)
if [[ -n $GIT_PROMPT ]]; then
echo -n "\n$GIT_PROMPT"
if [[ -n $SVN_PROMPT ]]; then
echo -n ", $SVN_PROMPT"
fi
elif [[ -n $SVN_PROMPT ]]; then
echo "\n$SVN_PROMPT"
fi
}
Steps to reproduce
- Set up dotfiles. (For the minimal setup, I used (Attachment on the original report: setup.sh) to set up
.bash_prompt.) - Create a git repository with a crafted name:
git init -b '$(touch${IFS}/tmp/pwned)' repo - Enter the repository:
cd repo - Confirm that
touch /tmp/pwnedis executed.
Impact
An attacker can execute arbitrary commands by tricking the victim into entering a malicious directory.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The script places untrusted repository data (branch names) into PS1, which bash evaluates for command substitutions, causing a second round of evaluation.
Impact
An attacker can execute arbitrary commands by tricking the victim into entering a malicious directory.
What the attacker needed
Dotfiles .bash_prompt installed; victim enters a malicious repository
How it was found
- Set up the dotfiles so that .bash_prompt is loaded.
- Create a git repository whose branch name contains command substitution syntax.
- Change into that repository directory.
- Observe that the embedded command has been executed.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Your shell prompt is the text shown before each command you type. This script puts information from the current git repository into that prompt, and the shell then treats parts of that text as commands to run. So a repository whose branch name looks like a command can make your computer run that command just by you entering the folder.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.