Ti Kallisti

OverTheWire "Bandit" Write-Up

After "Ellingson" made me realize that I am still a bit lacking in the Binary Exploitation department, I decided to put some focus on improving my skills in that area. I was told that "OverTheWire" is the place to go for that, so I went there.

The first set of challenges is called "Bandit", and it's more of an introduction to the Linux CLI than anything else. BUT, it's the first wargame there, so we might as well start with that.

Level 0

The 0th level requires us to connect to the wargame via SSH. A rudimentary SSH call would be

ssh [username@]HOST [-p Port]
. Username and Port are optional parameters, but an IP or hostname is always required. If we don't specify a username, the command will use the name of the user we are logged in with on our local machine (the output of whoami). If we don't specify a port, the command will use the default SSH port, 22.

The side tells us the username we need to use, "bandit0", and the port, 2222. So the call would be like this:

ssh bandit0@bandit.labs.overthewire.org -p 2220 This is a OverTheWire game server. More information on http://www.overthewire.org/wargames bandit0@bandit.labs.overthewire.org's password:

We will be ask for a password, which is also provided by the site - "bandit0", the same as the username. If we enter that, we will be greeted with a new prompt:

bandit0@bandit:~$

This tells us that we are logged in as "bandit0" on a machine with the hostname "bandit".

Now that we have a foothold on the machine, we should get a feeling for our "surroundings", so to speak, and what we can access. For that, two commands are essential: pwd and ls.

The output of pwd tells us the absolute path of the directory we're currently in:

bandit0@bandit:~$ pwd /home/bandit0

The output of ls tells us which files and directories reside in the current directory. I personally always use ls -l -a - or rather the abbreviated version ls -la - for the following reasons:

bandit0@bandit:~$ ls -la total 24 drwxr-xr-x 2 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc -rw-r--r-- 1 root root 675 May 15 2017 .profile -rw-r----- 1 bandit1 bandit0 33 Oct 16 2018 readme

Nothing out of the ordinary here, apart from the readme file. But first, let's take a closer look at the output and what it means, taking the last line as an example.

The first column of the output, -rw-r----- tells us about the permissions on the file. The first character is for special bits (e.g. SUID, or "d" indicating that we're looking at a directory rather than a file). The other 9 characters are pairs of 3 referring to the permissions of the owning user, the owning group and everyone else respectively. For the readme, we can see that the owning user has read (r) and write (w) permissions, the group has only read (r) permissions and everyone else has no permissions at all on the file. There is a third kind of permission, execute (x), but no such permissions is set for the readme file.

The third and fourth column of the output, bandit1 and bandit0 tell us about the owning user and the owing group respectively. As a takeaway, we can see that the owning group is "bandit0", the group we belong to (more on that further down), which, in combination with the information about permissions, means that we are able to read the contents of the file.

The fifth column, 33 is the size of the file in bytes.

The sixth and seventh column, Oct 16 and 2018 together form the date of the last modification.

And finally, the eighth column, readme, represents the filename.

Side note: In the above ls output, there are two directories: "." and ".." - these are special directories in Linux referring to the current directory(".") and the directory one level above("..") respectively. As the pwd command showed us that we are in "/home/bandit0", ".." is essentially the same as "/home".

Earlier I said that we are part of the group "bandit0" - how do we know that? Well, the command id tells us:

bandit0@bandit:~$ id uid=11000(bandit0) gid=11000(bandit0) groups=11000(bandit0)

uid tells us our user id, followed by the username in brackets.

gid tells us the id of our "main" group, and groups gives us a list of all the groups we belong to, which is currently only bandit0.

As that one file tells us to read it, we shall do so. There are a lot of command-line tools that can be used for that, like vim, nano, more or less, but for just reading a short file like that i prefer to use cat:

bandit0@bandit:~$ cat readme boJ9jbbUNNfktd78OOpsqOltutMc3MY1

A password! The wargame page for Level 0 -> Level 1 tells us we need to get the password from the readme file to get to Level 1. As we now have that, we have officially completed Level 0!

Level 1

With the password from Level 0, we can now get to Level 1 using SSH:

ssh bandit1@bandit.labs.overthewire.org -p 2220 bandit1@bandit.labs.overthewire.org's password: bandit1@bandit:~$

Just in case you're wondering why, when you type the password, it doesn't show up on screen: That's a built-in security feature by Linux, that prevents any shoulder-surfers from getting your password.

Anyway, now that we are in Level 1, we start with the same procedure as in Level 0 - figure out where we are and what we can see:

bandit1@bandit:~$ pwd /home/bandit1 bandit1@bandit:~$ ls -la total 24 -rw-r----- 1 bandit2 bandit1 33 Oct 16 2018 - drwxr-xr-x 2 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc -rw-r--r-- 1 root root 675 May 15 2017 .profile

Interesting - a file called "-". Let's use cat on it and grep the password:

bandit1@bandit:~$ cat -

Hmm, nothing. Though we can see that the file has a size of 33 bytes, so there should be something in there. The problem here is the following:

"-" is a legitimate filename in Linux. But it also refers to the stdin, the standard input. And this is what our cat command thinks we are referring to. So it will just reflect whatever we write back to us. That isn't what we want. So, to fix this, we execute the following:

bandit1@bandit:~$ cat ./- CV1DtqXWVFXTvM2F0k09SHz0YwRINYA9

It works! But why? Well, as explained in Level 0, "." refers to the current directory, so "./-" just refers to the file "-" located in the current directory. But when writing it like this, we avoid any ambiguity with the placeholder for stdin "-", and cat knows what we are actually referring to!

Another level done!

Level 2

The SSH command to get to a level always follows the same pattern, so this will be the last time I write it out here. In the next levels, I am just gonna start after I logged in via SSH.

ssh bandit2@bandit.labs.overthewire.org -p 2220 bandit2@bandit.labs.overthewire.org's password: bandit2@bandit:~$

As always, let's have a look around:

bandit2@bandit:~$ pwd /home/bandit2 bandit2@bandit:~$ ls -la total 24 drwxr-xr-x 2 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc -rw-r--r-- 1 root root 675 May 15 2017 .profile -rw-r----- 1 bandit3 bandit2 33 Oct 16 2018 spaces in this filename

There's the password file, let's just read it and move on:

bandit2@bandit:~$ cat spaces in this filename cat: spaces: No such file or directory cat: in: No such file or directory cat: this: No such file or directory cat: filename: No such file or directory

Would have been too easy, wouldn't it? Well, in the command line, spaces separate parameters. So if we enter the command like above, cat thinks we want to read four files: "spaces", "in", "this", and "filename". To fix this, there's a simple trick: Quotes. Quotes tell cat that everything between them is one single string. So in order to read the file, we would have to write something like this:

bandit2@bandit:~$ cat "spaces in this filename" UmHadQclWmgdLOKQ3YNgjWxGoRMb5luK

Level 3 password: Acquired!

Note that it doesn't matter whether we use double quotes or single quotes, we just have to use them pairwise. That means this will work:

bandit2@bandit:~$ cat 'spaces in this filename' UmHadQclWmgdLOKQ3YNgjWxGoRMb5luK

But this:

bandit2@bandit:~$ cat "spaces in this filename'

or this:

bandit2@bandit:~$ cat 'spaces in this filename"

won't.

Level 3

Let's see what we have:

bandit3@bandit:~$ pwd /home/bandit3 bandit3@bandit:~$ ls -la total 24 drwxr-xr-x 3 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc drwxr-xr-x 2 root root 4096 Oct 16 2018 inhere -rw-r--r-- 1 root root 675 May 15 2017 .profile

Here we can see that there is a directory called "inhere", which probably contains the password file for Level 4. In order to grab it, we need to change directory, or cd, for short:

bandit3@bandit:~$ cd inhere bandit3@bandit:~/inhere$ ls -la total 12 drwxr-xr-x 2 root root 4096 Oct 16 2018 . drwxr-xr-x 3 root root 4096 Oct 16 2018 .. -rw-r----- 1 bandit4 bandit3 33 Oct 16 2018 .hidden

Ha! Our insistence on using the -a parameter for -ls is already paying off :D Let's grab the password and move on:

bandit3@bandit:~/inhere$ cat .hidden pIwrPrtPN36QITSp3EQaw936yaFoFgAB

Some notes on directories in the command line interface: The prompt always shows us the directory we're currently in. "~" represents the home directory of the current user, in our case "/home/bandit3". If we change the directory, the prompt will reflect that. After we execute cd inhere we can see that the directory in the prompt also changes to "~/inhere". This is very useful for orientation in the directory structure.

The pwd command is still a useful tool and should always be on the back of our minds, for three reasons:

  1. On foreign or new systems, we might not always now that the home directory of a user is, so we wouldn't know what "~" stands for.
  2. Command prompts can be configured to not show the directory you're currently in.
  3. If you're reading this block, there's a good chance you're interested in hacking. When you "get a shell", you usually don't get a prompt at all. So pwd is a must if you want to know where you are.

Level 4

bandit4@bandit:~$ ls -la total 24 drwxr-xr-x 3 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc drwxr-xr-x 2 root root 4096 Oct 16 2018 inhere -rw-r--r-- 1 root root 675 May 15 2017 .profile

Same old, same old:

bandit4@bandit:~$ cd inhere/ bandit4@bandit:~/inhere$ ls -la total 48 drwxr-xr-x 2 root root 4096 Oct 16 2018 . drwxr-xr-x 3 root root 4096 Oct 16 2018 .. -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file00 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file01 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file02 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file03 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file04 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file05 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file06 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file07 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file08 -rw-r----- 1 bandit5 bandit4 33 Oct 16 2018 -file09

From Level 1, we already know that dashes at the beginning of filenames are tricky. But we also know how to get around that.

But, as the "-" is not the whole filename this time, the behavior is a little bit different this time, so I want to elaborate on that a bit:

bandit4@bandit:~/inhere$ cat -file00 cat: invalid option -- 'f'

This time, cat doesn't wait for any input from us, it just gives an error and exits. This is because in Linux, it is convention to pass multiple parameters after the following scheme:

[command] -[parameter_name] [parameter_value]

Not all parameters need a value, so the [parameter_value] part is optional. But looking at that scheme, we can deduct that if we execute cat -file00, "file00" is treated as the name of a parameter, rather than a file.

One of those files will contain the password. But it would be terribly tedious to read every file one at a time. Thank Goddess there's a way around that - "*". "*" is a placeholder character, that allows us to output all the files in the directory at once. Combined with our "./"-trick, we get the following:

bandit4@bandit:~/inhere$ cat ./* ����������~% C[�걱>��| ����U"7�w���H��ê�Q����(���#����T�v��(�ִ�����A*� 2J�Ş؇_�y7��.A��u��#���w$N?c�-��Db3��=��<�W�����ht�Z��!��{�U �+��pm���;��:D��^��@�gl�Q���@�%@���ZP*E��1�V���̫*����koReBOKuIDDepwhWk7jZC0RTdopnAYKh FPn� '�U���M��/u XS �mu�z���хN�{���Y�d4����]3�����9(

We will get a lot of garbled output, because most files contain bytes that don't translate well into UTF-8-characters. But we can see one string in there: "koReBOKuIDDepwhWk7jZC0RTdopnAYKh". Most likely, this will be our Level 5 password (Spoiler: it is).

So with that, Level 4 is officially done!

Level 5

bandit5@bandit:~$ ls -la total 24 drwxr-xr-x 3 root root 4096 Oct 16 2018 . drwxr-xr-x 41 root root 4096 Oct 16 2018 .. -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3526 May 15 2017 .bashrc drwxr-x--- 22 root bandit5 4096 Oct 16 2018 inhere -rw-r--r-- 1 root root 675 May 15 2017 .profile bandit5@bandit:~$ cd inhere bandit5@bandit:~/inhere$ ls -la total 88 drwxr-x--- 22 root bandit5 4096 Oct 16 2018 . drwxr-xr-x 3 root root 4096 Oct 16 2018 .. drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere00 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere01 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere02 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere03 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere04 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere05 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere06 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere07 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere08 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere09 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere10 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere11 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere12 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere13 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere14 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere15 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere16 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere17 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere18 drwxr-x--- 2 root bandit5 4096 Oct 16 2018 maybehere19

Okay, this is gonna be a bit more complicated. We get some information from the Level 5 -> Level 6 page, though. Our file is: