Bandit Level 8 → Level 9
Hello everyone!
If you want to try it yourself before seeing the solution, here you have the URL:
https://overthewire.org/wargames/bandit/
When we start we are presented with the following statement:
The password for the next level is stored in the file data.txt and is the only line of text that occurs only once
To enter the level, one must do the command:
$ ssh -p 2220 -l bandit8 bandit.labs.overthewire.org
With the password, which is the flag from the previous level.
And we are in!
Once inside lets see the home (~) folder's content with the command:
~$ ls
And the file data.txt appeared.
In order to see the file's content we could do the cat command:
~$ cat data.txt
The output was a massive amount of characters, what appeared to be flags. In order to find the word that appears only once, we will have to using a few commands.
First I considered to use uniq with the flag -u after a cat, like this:
~$ cat data.txt | uniq -u
However what I realized was that the uniq command compares lines that are followed, meaning that if you have a file with:
aaaaa
bbbbb
ccccc
aaaaa
The uniq command with the flag -u will say that all of this lines are unique, and only occur once. This happens because this command compares the aaaa with the bbbbb and because they are different he says that aaaaa appears once, even if it appears later on, like it does.
In order to tackle this problem we could sort the file first! Like this:
~$ sort data.txt | uniq -u
And this command works just fine!
And the ouput is a simple line with the flag.
However, there is another way. We could use the flag -c of the uniq command after the sort, and with this, we would get a list of all the different words and the number of times they occur in the file, and we could finally grep that output for the one that appear once, like this:
~$ sort data.txt | uniq -c | grep "^ *1 "
The ^ symbol matchs the starting position of any line matches the preceding element zero or more times. In this case, the preceding element is a space. We have to add a final space because if not it will match with numbers that start with 1, like 10, 11 and so on.
~$ ls
And the file data.txt appeared.
In order to see the file's content we could do the cat command:
~$ cat data.txt
The output was a massive amount of characters, what appeared to be flags. In order to find the word that appears only once, we will have to using a few commands.
First I considered to use uniq with the flag -u after a cat, like this:
~$ cat data.txt | uniq -u
However what I realized was that the uniq command compares lines that are followed, meaning that if you have a file with:
aaaaa
bbbbb
ccccc
aaaaa
The uniq command with the flag -u will say that all of this lines are unique, and only occur once. This happens because this command compares the aaaa with the bbbbb and because they are different he says that aaaaa appears once, even if it appears later on, like it does.
In order to tackle this problem we could sort the file first! Like this:
~$ sort data.txt | uniq -u
And this command works just fine!
And the ouput is a simple line with the flag.
However, there is another way. We could use the flag -c of the uniq command after the sort, and with this, we would get a list of all the different words and the number of times they occur in the file, and we could finally grep that output for the one that appear once, like this:
~$ sort data.txt | uniq -c | grep "^ *1 "
The ^ symbol matchs the starting position of any line matches the preceding element zero or more times. In this case, the preceding element is a space. We have to add a final space because if not it will match with numbers that start with 1, like 10, 11 and so on.
And if you wanna see the flag:
See you soon, thank you!
Cheers
Marcelo Silva
Comments
Post a Comment