Even just a few levels in, you might already be tired of typing out all these file paths.
Luckily, the shell has a solution: globbing!
That's what we'll learn in this module.
Before executing commands that you enter, the shell first performs expansions on them, and one of these expansions is globbing.
Globbing lets you reference files without typing them all out, or typing out their full paths.
Let's dig in!
The first glob we'll learn is *.
When it encounters a * character in any argument, the shell will treat it as "wildcard" and try to replace that argument with any files that match the pattern.
It's easier to show you than explain:
Now, practice this yourself!
Starting from your home directory, change your directory to /challenge, but use globbing to keep the argument you pass to cd to at most four characters!
Once you're there, run /challenge/run for the flag!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Next, let's learn about ?.
When it encounters a ? character in any argument, the shell will treat it as a single-character wildcard.
This works like *, but only matches one character.
For example:
Now, practice this yourself!
Starting from your home directory, change your directory to /challenge, but use the ? character instead of c and l in the argument to cd!
Once you're there, run /challenge/run for the flag!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Next, we will cover [].
The square brackets are, essentially, a limited form of ?, in that instead of matching any character, [] is a wildcard for some subset of potential characters, specified within the brackets.
For example, [pwn] will match the character p, w, or n.
For example:
Try it here!
We've placed a bunch of files in /challenge/files.
Change your working directory to /challenge/files and run /challenge/run with a single argument that bracket-globs into file_b, file_a, file_s, and file_h!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Globbing happens on a path basis, so you can expand entire paths with your globbed arguments.
For example:
Now it's your turn.
Once more, we've placed a bunch of files in /challenge/files.
Starting from your home directory, run /challenge/run with a single argument that bracket-globs into the absolute paths to the file_b, file_a, file_s, and file_h files!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
So far, you've specified one glob at a time, but you can do more!
Bash supports the expansion of multiple globs in a single word.
For example:
What happens above is that the shell looks for all files in / that start with anything (including nothing), then have an f and an l, and end in anything (including ag, which makes flag).
Now you try it.
We put a few happy, but diversely-named files in /challenge/files.
Go cd there and run /challenge/run, providing a single argument: a short (3 characters or less) globbed word with two * globs in it that covers every word that contains the letter p.
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Now, let's put the previous levels together!
We put a few happy, but diversely-named files in /challenge/files.
Go cd there and, using the globbing you've learned, write a single, short (6 characters or less) glob that (when passed as an argument to /challenge/run) will match the files "challenging", "educational", and "pwning"!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Sometimes, you want to filter out files in a glob!
Luckily, [] helps you do just this.
If the first character in the brackets is a ! or (in newer versions of bash) a ^, the glob inverts, and that bracket instance matches characters that aren't listed.
For example:
Armed with this knowledge, go forth to /challenge/files and run /challenge/run with all files that don't start with p, w, or n!
NOTE: The ! character has a different special meaning in bash when it's not the first character of a [] glob, so keep that in mind if things stop making sense! ^ does not have this problem, but is also not compatible with older shells.
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
As tempting as it might be, using * to shorten what must be typed on the commandline can lead to mistakes.
Your glob might expand to unintended files, and you might not spot it until the rm command is already running!
No one is safe from this style of error.
A safer alternative when you are trying to specify a specific target is tab completion.
If you hit tab in the shell, it'll try to figure out what you're going to type and automatically complete it.
Auto-completion is super useful, and this challenge will explore its use in specifying files.
This challenge has copied the flag into /challenge/pwncollege, and you can freely cat that file.
But you can't type the filename: we used some serious trickery to make sure that you must tab-complete it.
Try it out!
hacker@dojo:~$ ls /challenge
DESCRIPTION.md pwncollege
hacker@dojo:~$ cat /challenge/pwncollege
cat: /challenge/pwncollege: No such file or directory
hacker@dojo:~$ cat /challenge/pwn<TAB>
pwn.college{HECK YEAH}
hacker@dojo:~$
When you hit that tab key, the name will expand and you'll be able to read the file.
Good luck!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Consider the following situation:
hacker@dojo:~$ ls
flag flamingo flowers
hacker@dojo:~$ cat f<TAB>
There are multiple options!
What happens?
What happens varies based on the specific shell and its options.
By default bash will auto-expand until the first point when there are multiple options (in this case, fl).
When you hit tab a second time, it'll print out those options.
Other shells and configurations, instead, will cycle through the options.
This challenge has a /challenge/files directory with a bunch of files starting with pwncollege.
Tab-complete from /challenge/files/p or so, and make your way to the flag!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
Tab completion is for more than files!
You can also tab-complete commands.
This level has a command that starts with pwncollege, and it'll give you the flag.
Type pwncollege and hit the tab key to auto-complete it!
NOTE:
You can auto-complete any command, but be careful: callous auto-completes without double-checking the result can wreak havoc in your shell if you accidentally run the wrong commands!
Connect with SSH
Link your SSH key, then connect with: ssh hacker@pwn.college
30-Day Scoreboard:
This scoreboard reflects solves for challenges in this module after the module launched in this dojo.