OverTheWire Bandit: Level 25 → Level 26
Context
This level features a tactic straight out of a hacker movie. We have the SSH key for bandit26, but when we log in, the system immediately kicks us out. This is because the shell for bandit26 isn’t standard /bin/bash but a custom script designed to restrict us.
Our Goal: “Escape” directly from this restricted environment (jailbreak) and spawn a real shell.
Solution
Step 1: Exfiltrate the SSH Key
The bandit26 SSH key is in our home directory. Note: OverTheWire blocks SSH connections from localhost to conserve resources, so we cannot connect directly from bandit25.
We must copy the key to our local machine:
- Print the key:
cat bandit26.sshkey - Copy the content.
- On your local machine, create a file (e.g.,
bandit26.key) and paste the content. - Set permissions:
chmod 600 bandit26.key
Step 2: Resize the Terminal (The Trick)
The system script uses the more command to display text. more has a feature: if the text doesn’t fit on the screen, it pauses and waits for user input (--More--).
This is our exploit!
- Shrink your terminal window drastically. Make it only 5-10 lines tall.
- Run the SSH command from your local machine.
ssh -i bandit26.key bandit26@bandit.labs.overthewire.org -p 2220
Now the connection won’t close. You’ll see --More-- (or %0) at the bottom. Do not press Enter or Space to finish the text!
Step 3: Switch to ‘vi’ Editor
While more is paused, if you press the v key, it launches the vi editor to edit the current content.
- When you see
--More--, press v on your keyboard. - You are now inside the
vieditor.
Step 4: Shell Spawning
vi is powerful; you can execute system commands from within it.
- Press Esc inside
vi(just to be safe). - Type the following command and hit Enter (this sets the shell
viuses)::set shell=/bin/bash - Now launch the shell:
:shell
Step 5: Freedom
Congratulations! You dropped into a command prompt. Check who you are:
whoami
# Output: bandit26
You can now roam freely as bandit26. We don’t get a password file here since we used an SSH key, but we have established a persistent session which allows us to proceed to the next challenge.
Key Takeaways
- Restricted Shells: Systems can enforce limits by setting the default shell to a custom script instead of
/bin/bash. - GTFOBins: A curated list of Unix binaries that can be used to bypass local security restrictions in misconfigured systems. Breaking out using
moreandviis a classic example.