Bandit Level 2
Bandit Level 2 — read a file literally named "-" and learn why commands treat a leading dash as an option.
Source: This content is aggregated from overthewire (MIT). Visit the original repository for the latest version.
Level Goal
The password for the next level is stored in a file called - located in the home directory
Commands you may need to solve this level
ls , cd , cat , file , du , find
Helpful Reading Material
Study guide
Level map: logged in as bandit1, find the password for bandit2.
What this level teaches
Why some filenames break commands, and how to talk to a command about a file that looks like an option.
Key ideas
- Options start with a dash.
cat -n filemeans “number the lines”. A bare-is conventionally “standard input”, so a command that receives only-does not see a file at all. - The shell passes arguments through unchanged. It is the program, not the shell, that decides
-is special. - Two standard remedies exist: make the argument unmistakably a path, or tell the program that options have ended. Both are described in the man pages of nearly every command.
How to approach it
- Run
lsand notice the odd filename. Trycaton it and observe what happens; failing on purpose is a good way to learn. - Think about how to write a path that does not begin with a dash. The current directory has a short name you already know.
- Search
man catorman bashfor the “end of options” convention, which is useful for every command you will ever use.
Why it matters
Attackers deliberately create files whose names look like command-line flags, which can trick scripts that run rm * or tar * into executing options. Knowing the mechanism is how you defend against it.