[OverTheWire] Bandit 21-32

Ostatni wpis poświęcony serii związanej z doskonaleniem umiejętności w posługiwaniu się Linuxem.

21

Level Goal

A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.

Commands you may need to solve this level

cron, crontab, crontab(5) (use “man 5 crontab” to access this)

W tym zadaniu mamy do czynienia z programem cron służącym do automatyzacji zadań w Linuxie. W tym miejscu należy również wspomnieć o crontabach – tabelach programu cron. W skrócie deamon cron czyta tabelę danego użytkownika (crontab) i na jej podstawie okresowo wywołuje inne programy.

Zajrzałem więc do folderu, który został podany w poleceniu:

bandit21@bandit:~$ ls -l /etc/cron.d/
total 24
-rw-r--r-- 1 root root 62 May 14 13:40 cronjob_bandit15_root
-rw-r--r-- 1 root root 62 Jul 11 15:56 cronjob_bandit17_root
-rw-r--r-- 1 root root 120 May 7 20:14 cronjob_bandit22
-rw-r--r-- 1 root root 122 May 7 20:14 cronjob_bandit23
-rw-r--r-- 1 root root 120 May 14 09:41 cronjob_bandit24
-rw-r--r-- 1 root root 62 May 14 14:04 cronjob_bandit25_root

Plik który nas interesuje to cronjob_bandit22. Zajrzałem do niego:

bandit21@bandit:~$ cat /etc/cron.d/cronjob_bandit22
@reboot bandit22 /usr/bin/cronjob_bandit22.sh &> /dev/null
* * * * bandit22 /usr/bin/cronjob_bandit22.sh &> /dev/null

Wynik pokazuje, że na maszynie wykonuje się okresowo skrypt cronjob_bandit22.sh. Zajrzałem, więc do niego:

bandit21@bandit:~$ cat /usr/bin/cronjob_bandit22.sh 
#!/bin/bash
chmod 644 /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
cat /etc/bandit_pass/bandit22 > /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv

Skrypt zapisuje hasło użytkownika bandit22 w pliku t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv. Pozostało mi zajrzeć do tego pliku:

bandit21@bandit:~$ cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv
Yk7owGAcWjwMVRwrTesJEwB7WVOiILLI

22

Level Goal

A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: Looking at shell scripts written by other people is a very useful skill. The script for this level is intentionally made easy to read. If you are having problems understanding what it does, try executing it to see the debug information it prints.

Commands you may need to solve this level

cron, crontab, crontab(5) (use “man 5 crontab” to access this)

Podobnie jak we wcześniejszym przykładzie zajrzałem do folderu cron.d oraz odczytałem interesujący mnie skrypt:

bandit22@bandit:~$ ls -l /etc/cron.d/
total 24
-rw-r--r-- 1 root root 62 May 14 13:40 cronjob_bandit15_root
-rw-r--r-- 1 root root 62 Jul 11 15:56 cronjob_bandit17_root
-rw-r--r-- 1 root root 120 May 7 20:14 cronjob_bandit22
-rw-r--r-- 1 root root 122 May 7 20:14 cronjob_bandit23
-rw-r--r-- 1 root root 120 May 14 09:41 cronjob_bandit24
-rw-r--r-- 1 root root 62 May 14 14:04 cronjob_bandit25_root
bandit22@bandit:~$ cat /etc/cron.d/cronjob_bandit23
@reboot bandit23 /usr/bin/cronjob_bandit23.sh &> /dev/null
* * * * bandit23 /usr/bin/cronjob_bandit23.sh &> /dev/null
bandit22@bandit:~$ cat /usr/bin/cronjob_bandit23.sh
#!/bin/bash

myname=$(whoami)
mytarget=$(echo I am user $myname | md5sum | cut -d ' ' -f 1)

echo "Copying passwordfile /etc/bandit_pass/$myname to /tmp/$mytarget"

cat /etc/bandit_pass/$myname > /tmp/$mytarget

Skrypt na początku zapisuje w zmiennej myname wynik polecenia whoami. Następnie w zmiennej mytarget zapisuje sumę kontrolną md5 pierwszego wyrażenia, a następnie modyfikuje output poleceniem cut. Na samym końcu skrpyt kopiuje hasło użytkownika bandit23 do pliku o nazwie takiej samej jak zmienna mytarget.

Sprawdziłem jak działa md5sum:

mateusz@kali:~/overthewire/banidt$ whoami | md5sum
cb30c6b346b7e6565a9e659140afe8de -

Nie myśląc długo zmodyfikowałem nieco skrypt oraz przetestowałem go na lokalnej maszynie:

#!/bin/bash

myname=bandit23
mytarget=$(echo I am user $myname | md5sum | cut -d ' ' -f 1)

echo $mytarget
mateusz@kali:~/overthewire/banidt$ chmod +x script23.sh
mateusz@kali:~/overthewire/banidt$ ./script23.sh
8ca319486bfbbc3663ea0fbe81326349

Następnie przeszedłem na maszynę bandit, odszukałem i odczytałem plik o nazwie 8ca319486bfbbc3663ea0fbe81326349:

bandit22@bandit:~$ cat /tmp/8ca319486bfbbc3663ea0fbe81326349
jc1udXuA1tiHqjIsL8yaapX5XIAI6i0n

W wyniku otrzymałem hasło do następnego poziomu.

23

Level Goal

A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed.
NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level!
NOTE 2: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around…

Commands you may need to solve this level

cron, crontab, crontab(5) (use “man 5 crontab” to access this)

Z zadania wynika, że aby przejść ten poziom trzeba będzie napisać swój własny skrypt :). Na początku jednak wykonałem podstawowe sprawdzenie, takie samo jak w powyższych poziomach:

bandit23@bandit:~$ ls -l /etc/cron.d
total 24
-rw-r--r-- 1 root root 62 May 14 13:40 cronjob_bandit15_root
-rw-r--r-- 1 root root 62 Jul 11 15:56 cronjob_bandit17_root
-rw-r--r-- 1 root root 120 May 7 20:14 cronjob_bandit22
-rw-r--r-- 1 root root 122 May 7 20:14 cronjob_bandit23
-rw-r--r-- 1 root root 120 May 14 09:41 cronjob_bandit24
-rw-r--r-- 1 root root 62 May 14 14:04 cronjob_bandit25_root
bandit23@bandit:~$ cat /etc/cron.d/cronjob_bandit24
@reboot bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null
* * * * bandit24 /usr/bin/cronjob_bandit24.sh &> /dev/null

Następnie zajrzełem do skryptu:

bandit23@bandit:~$ cat /usr/bin/cronjob_bandit24.sh
#!/bin/bash

myname=$(whoami)

cd /var/spool/$myname
echo "Executing and deleting all scripts in /var/spool/$myname:"
for i in * .*;
do
    if [ "$i" != "." -a "$i" != ".." ];
    then
        echo "Handling $i"
        owner="$(stat --format "%U" ./$i)"
        if [ "${owner}" = "bandit23" ]; then
            timeout -s 9 60 ./$i
        fi
        rm -f ./$i
    fi
done

Powyższy skrypt wykonuje wszystkie pliki w scieżce /var/spool/$myname po czym je usuwa.

Aby uzyskać hasło do następnego poziomu stworzyłem w folderze /tmp własny folder o nazwie testowyfolder. Następnie napisałem następujący skrypt (używając edytora tekstowego vim):

#!/bin/bash

cat /etc/bandit_pass/bandit24 > /tmp/testowyfolder/password

Skrypt ma za zadanie odczytać hasło użytkownika bandit 24 oraz zapisać go w pliku password umieszczonym w moim folderze. Aby wszystko zadziałało poprawnie utworzyłem wcześniej plik password oraz nadałem odpowiednie uprawnienia:

bandit23@bandit:/tmp/testowyfolder$ chmod 777 script.sh
bandit23@bandit:/tmp/testowyfolder$ touch password
bandit23@bandit:/tmp/testowyfolder$ chmod 666 password
bandit23@bandit:/tmp/testowyfolder$ ls -la
total 840
drwxr-sr-x 2 bandit23 root 4096 Oct 1 09:37 .
drwxrws-wt 4225 root root 847872 Oct 1 09:38 ..
-rw-rw-rw- 1 bandit23 root 0 Oct 1 09:37 password
-rwxrwxrwx 1 bandit23 root 73 Oct 1 09:37 script.sh

Następnie przeniosłem mój skrypt do lokalizacji /var/spool/bandit24/ aby został wykonany przez daemona cron:

bandit23@bandit:/tmp/testowyfolder$ cp script.sh /var/spool/bandit24/
bandit23@bandit:/tmp/testowyfolder$ ls -la /var/spool/bandit24/script.sh
-rwxr-xr-x 1 bandit23 bandit23 73 Oct 1 09:38 /var/spool/bandit24/script.sh

Po chwili skrypt nadpisał plik password:

bandit23@bandit:/tmp/testowyfolder$ cat password
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ

24

Level Goal

A daemon is listening on port 30002 and will give you the password for bandit25 if given the password for bandit24 and a secret numeric 4-digit pincode. There is no way to retrieve the pincode except by going through all of the 10000 combinations, called brute-forcing.

Aby przejść ten poziom należy napisać skrypt, który metodą siłową (ang. bruteforce) złamie 4 cyfrowy pincode. Na początku sprawdziłem jak zachowuje się daemon wystawiony na porcie 30002:

bandit24@bandit:~$ nc localhost 30002
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ 0000
Wrong! Please enter the correct pincode. Try again.

Widzimy, że daemon nas wita oraz podaje instrukcje jak wpisywać dane wejściowe. Następnie po błędnym wpisaniu danych wyświetla komunikat: Wrong! Please enter the correct pincode. Try again. Jest to cenna informacja, którą wykorzystam pisząc mój skrypt.

Pisząc skrypt w języku bash natrafiłem na istotny problem (plułem sobie w brodę, dlaczego od początku nie wybrałem pythona). Brute-force wykonywał się bardzo wolno….

Do zagadnienia podszedłem w inny sposób. Otóż na początku napisałem skrypt który zapisał mi wszystkie możliwe kombinacje wejściowe do nasłuchującego programu:

#!/bin/bash

passwd="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
for i in {0000..9999};do
	echo $passwd $i >> combinations.txt
done

Następnie uruchomiłem skrypt oraz wynik działania skryptu (combinations.txt) przekazałem do nasłuchującego programu. Następnie użyłem narzędzia grep, żeby odfiltrować poprawny wynik:

bandit24@bandit:/tmp/testowyfolder1$ cat combinations.txt | nc localhost 30002 | grep password
I am the pincode checker for user bandit25. Please enter the password for user bandit24 and the secret pincode on a single line, separated by a space.
The password of user bandit25 is uNG9O58gUE7snukf3bvZ0rxhtnjzSGzG

Po chwili uzyskałem hasło do następnego poziomu.

25

Level Goal

Logging in to bandit26 from bandit25 should be fairly easy… The shell for user bandit26 is not /bin/bash, but something else. Find out what it is, how it works and how to break out of it.

Commands you may need to solve this level

ssh, cat, more, vi, ls, id, pwd

W tym przykładzie dostajemy informację, że powłoką dla użytkownika bandit26 nie jest /bin/bash. Sprawdziłem zatem plik /etc/passwd:

bandit25@bandit:~$ cat /etc/passwd | grep bandit26
bandit26:x:11026:11026:bandit level 26:/home/bandit26:/usr/bin/showtext

Dostałem informację, że powłoką systemową dla bandit26 jest /usr/bin/showtext:

bandit25@bandit:~$ cat /usr/bin/showtext 
#!/bin/sh

export TERM=linux

more ~/text.txt
exit 0

Sprawdziłem również folder domowy użytkownika bandit25:

bandit25@bandit:~$ ls -l
total 4
-r-------- 1 bandit25 bandit25 1679 May 7 20:14 bandit26.sshkey

Okazało się, że w folderze znajduje się klucz prywatny użytkownika bandit26. Niestety po zalogowaniu od razu zostajemy rozłączeni, ponieważ jak już wcześniej wspomniałem użytkownik ma podmienioną powłokę systemową.

Showtext korzysta z polecenia more. Z more możemy łatwo przejść do edytora vim, wciskając przycisk v.

Na początku minimalizujemy okno aby wyzwolić (ztrigerować) polecenie more. Następnie przechodzimy do vim wciskając v. Aby przejść do edycji interesującego nas pliku z hasłem wpisujemy: :e /etc/bandit_pass/bandit26.

Na dole widoczny wprowadzone przeze mnie polecenie

Odczytanie pliku z hasłem

W wyniku przeprowadzonych działań uzyskałem hasło użytkownika bandit26.

Jednakże nie uzyskałem jeszcze odpowiedniej powłoki. Korzystając z gtfobins oraz odtwarzając powyższe kroki zrobiłem podmianę powłoki dla użytkownika bandit26:

Instrukcja jak wyjść z vima do shella

Zgodnie z powższym, wyjście z vima i uzyskanie shella użytkownika bandit26

26

Level Goal

Good job getting a shell! Now hurry and grab the password for bandit27!

Commands you may need to solve this level

ls

Posiadając powłokę użytkonika bandit26 sprawdziłem jego folder domowy:

$ /bin/bash -e
bandit26@bandit:~$ ls -l
total 12
-rwsr-x--- 1 bandit27 bandit26 7296 May 7 20:14 bandit27-do
-rw-r----- 1 bandit26 bandit26 258 May 7 20:14 text.txt

Znowu mamy do czynienia z plikiem z ustawionym bitem setuid. Jak wiemy z wcześniejszych poziomów, niezależnie kto wykonuje plik z SETUID, będzie on wykonywany z uprawnieniami właściciela, czyli w tym przypadku bandit27. Wiedząc to odczytałem zatem hasło użytkownika bandit27:

bandit26@bandit:~$ ./bandit27-do
Run a command as another user.
Example: ./bandit27-do id
$ ./bandit27-do cat /etc/bandit_pass/bandit27
3ba3118a22e93127a4ed485be72ef5ea

Kolejne poziomy będą dotyczyły rozproszonego systemu kontroli wersji – git.

27

Level Goal

There is a git repository at ssh://bandit27-git@localhost/home/bandit27-git/repo. The password for the user bandit27-git is the same as for the user bandit27.
Clone the repository and find the password for the next level.

Commands you may need to solve this level

git

Ponownie stworzyłem własny folder w lokalizacji /tmp a następnie sklonowałem do niego repozytorium podane w poleceniu (wpisując uprzednio hasło użytkownika bandit27):

bandit27@bandit:/tmp$ mkdir testowyfolder2
bandit27@bandit:/tmp$ cd testowyfolder2
bandit27@bandit:/tmp/testowyfolder2$ git clone ssh://bandit27-git@localhost/home/bandit27-git/repo

Cloning into 'repo'…
Could not create directory '/home/bandit27/.ssh'.
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:98UL0ZWr85496EtCRkKlo20X3OPnyPSB5tB5RPbhczc.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/bandit27/.ssh/known_hosts).
This is a OverTheWire game server. More information on http://www.overthewire.org/wargames
bandit27-git@localhost's password:
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), 288 bytes | 0 bytes/s, done.

bandit27@bandit:/tmp/testowyfolder2$ ls
repo
bandit27@bandit:/tmp/testowyfolder2$ cd repo/
bandit27@bandit:/tmp/testowyfolder2/repo$ ls
README
bandit27@bandit:/tmp/testowyfolder2/repo$ cat README
The password to the next level is: 0ef186ac70e04ea33b4c1853d2526fa2

Okazało się, że repozytorium posiada plik README, w którym zapisane jest hasło do następnego poziomu.

28

Level Goal

There is a git repository at ssh://bandit28-git@localhost/home/bandit28-git/repo. The password for the user bandit28-git is the same as for the user bandit28.
Clone the repository and find the password for the next level.

Commands you may need to solve this level

git

Wykonałem te same czynności co powyżej. Po sklonowaniu repozytorium zajrzałem do folderu:

bandit28@bandit:/tmp/testowyfolder3/repo$ ls -la
total 16
drwxr-sr-x 3 bandit28 root 4096 Oct 1 11:24 .
drwxr-sr-x 3 bandit28 root 4096 Oct 1 11:24 ..
drwxr-sr-x 8 bandit28 root 4096 Oct 1 11:24 .git
-rw-r--r-- 1 bandit28 root 111 Oct 1 11:24 README.md

Tym razem w folderze repo znajduje się ukryty folder git oraz plik README. Odczytałem najpierw plik README:

# Bandit Notes
Some notes for level29 of bandit.

## credentials

- username: bandit29
- password: xxxxxxxxxx

Zawartość pliku sugeruje, że hasło zostało zakodowane, czy też umyślnie “zaciemnione”. Sprawdziłem historię commitów:

bandit28@bandit:/tmp/testowyfolder3/repo$ git log

Uzyskany wynik:

commit edd935d60906b33f0619605abd1689808ccdd5ee
Author: Morla Porla <morla@overthewire.org>
Date:   Thu May 7 20:14:49 2020 +0200

    fix info leak

commit c086d11a00c0648d095d04c089786efef5e01264
Author: Morla Porla <morla@overthewire.org>
Date:   Thu May 7 20:14:49 2020 +0200

    add missing data

commit de2ebe2d5fd1598cd547f4d56247e053be3fdc38
Author: Ben Dover <noone@overthewire.org>
Date:   Thu May 7 20:14:49 2020 +0200

    initial commit of README.md

Jak widzimy na załączonym obrazku, istnieją trzy commity. Odczytałem drugi commit, zakomentowany “add missing data”, który zawierał właściwe hasło:

bandit28@bandit:/tmp/testowyfolder3/repo$ git show c086d11a00c0648d095d04c089786efef5e01264

commit c086d11a00c0648d095d04c089786efef5e01264
Author: Morla Porla morla@overthewire.org
Date: Thu May 7 20:14:49 2020 +0200
add missing data
diff --git a/README.md b/README.md
index 7ba2d2f..3f7cee8 100644
--- a/README.md
+++ b/README.md
@@ -4,5 +4,5 @@ Some notes for level29 of bandit.
## credentials
username: bandit29
-- password:
+- password: bbc96594b4e001778eee9975372716b2

29

Level Goal

There is a git repository at ssh://bandit29-git@localhost/home/bandit29-git/repo. The password for the user bandit29-git is the same as for the user bandit29.
Clone the repository and find the password for the next level.

Commands you may need to solve this level

git

Podobnie jak wcześniej, najpierw stoworzyłem folder a następnie sklonowałem do niego repozytorium. Sprawdzamiłem co jest w folderze repo:

bandit29@bandit:/tmp/testowyfolder4/repo$ ls -la
total 16
drwxr-sr-x 3 bandit29 root 4096 Oct 1 13:37 .
drwxr-sr-x 3 bandit29 root 4096 Oct 1 13:36 ..
drwxr-sr-x 8 bandit29 root 4096 Oct 1 13:37 .git
-rw-r--r-- 1 bandit29 root 131 Oct 1 13:37 README.md

Taka sama sytuacja jak w poprzednim poziomie. Sprawdziłem historię commitów (poleceniem git log -p):

commit 208f463b5b3992906eabf23c562eda3277fea912
Author: Ben Dover <noone@overthewire.org>
Date:   Thu May 7 20:14:51 2020 +0200

    fix username

diff --git a/README.md b/README.md
index 2da2f39..1af21d3 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,6 @@ Some notes for bandit30 of bandit.
 
 ## credentials
 
-- username: bandit29
+- username: bandit30
 - password: <no passwords in production!>
 

commit 18a6fd6d5ef7f0874bbdda2fa0d77b3b81fd63f7
Author: Ben Dover <noone@overthewire.org>
Date:   Thu May 7 20:14:51 2020 +0200

    initial commit of README.md

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..2da2f39
--- /dev/null
+++ b/README.md
@@ -0,0 +1,8 @@
+# Bandit Notes
+Some notes for bandit30 of bandit.
+
+## credentials
+
+- username: bandit29
+- password: <no passwords in production!>
+

Po przejrzeniu commitów, wewnątrz nie znalazłem nic ciekawego. Postanowiłem wylistować wszystkie dostępne branche (tzw. gałęzie):

bandit29@bandit:/tmp/testowyfolder4/repo$ git branch -a

Wynik:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  remotes/origin/sploits-dev

Następnie przeszedłem na branch dev:

bandit29@bandit:/tmp/testowyfolder4/repo$ git checkout dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
bandit29@bandit:/tmp/testowyfolder4/repo$ git branch -a
*dev
 master
 remotes/origin/HEAD -> origin/master
 remotes/origin/dev
 remotes/origin/master
 remotes/origin/sploits-dev

I ponownie sprawdziłem historię commitów (w której znalazłem hasło do następnego poziomu):

bandit29@bandit:/tmp/testowyfolder4/repo$ git log -p -1
commit bc833286fca18a3948aec989f7025e23ffc16c07
Author: Morla Porla <morla@overthewire.org>
Date:   Thu May 7 20:14:52 2020 +0200

    add data needed for development

diff --git a/README.md b/README.md
index 1af21d3..39b87a8 100644
--- a/README.md
+++ b/README.md
@@ -4,5 +4,5 @@ Some notes for bandit30 of bandit.
 ## credentials
 
 - username: bandit30
-- password: <no passwords in production!>
+- password: 5b90576bedb2cc04c86a9e924ce42faf

Dla przejrzystości tym razem ograniczyłem wyjście do jednego wyniku.

30

Level Goal

There is a git repository at ssh://bandit30-git@localhost/home/bandit30-git/repo. The password for the user bandit30-git is the same as for the user bandit30.
Clone the repository and find the password for the next level.

Commands you may need to solve this level

git

Początek taki sam jak we wcześniejszych przykładach. Zajrzełem do folderu repo:

bandit30@bandit:/tmp/testowyfolder5/repo$ ls -la
total 16
drwxr-sr-x 3 bandit30 root 4096 Oct 1 14:52 .
drwxr-sr-x 3 bandit30 root 4096 Oct 1 14:51 ..
drwxr-sr-x 8 bandit30 root 4096 Oct 1 14:52 .git
-rw-r--r-- 1 bandit30 root 30 Oct 1 14:52 README.md
bandit30@bandit:/tmp/testowyfolder5/repo$ cat README.md
just an epmty file… muahaha

Sprawdziłem historię commitów:

bandit30@bandit:/tmp/testowyfolder5/repo$ git log -p

commit 3aefa229469b7ba1cc08203e5d8fa299354c496b
Author: Ben Dover <noone@overthewire.org>
Date:   Thu May 7 20:14:54 2020 +0200

    initial commit of README.md

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..029ba42
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+just an epmty file... muahaha

Nic. Sprawdziłem jeszcze tagi i okazało się to świentym pomysłem:

bandit30@bandit:/tmp/testowyfolder5/repo$ git tag
secret
bandit30@bandit:/tmp/testowyfolder5/repo$ git tag | git show secret
47e603bb428404d265f59c42920d81e5

Okazało się, że w tagu znajdował się password do kolejnego levela.

31

Level Goal

There is a git repository at ssh://bandit31-git@localhost/home/bandit31-git/repo. The password for the user bandit31-git is the same as for the user bandit31.
Clone the repository and find the password for the next level.

Commands you may need to solve this level

git

Podobnie jak wcześniej stworzyłem folder oraz sklonowałem tam zawartość rezpozytorium:

bandit31@bandit:/tmp/testowyfolder6/repo$ ls -la
total 20
drwxr-sr-x 3 bandit31 root 4096 Oct 1 19:13 .
drwxr-sr-x 3 bandit31 root 4096 Oct 1 19:12 ..
drwxr-sr-x 8 bandit31 root 4096 Oct 1 19:13 .git
-rw-r--r-- 1 bandit31 root 6 Oct 1 19:13 .gitignore
-rw-r--r-- 1 bandit31 root 147 Oct 1 19:13 README.md
bandit31@bandit:/tmp/testowyfolder6/repo$ cat README.md
This time your task is to push a file to the remote repository.

Details:
   File name: key.txt
   Content: 'May I come in?'
   Branch: master

W pliku README znalazłem wskazówkę, że tym razem należy dodać plik do zdalnego repozytorium. Plik ma mieć określoną nazwę, zawartość oraz gałąź do której będzie dodany. Na początku stworzyłem wymagany plik:

bandit31@bandit:/tmp/testowyfolder6/repo$ touch key.txt
bandit31@bandit:/tmp/testowyfolder6/repo$ echo "May I come in?"> key.txt
bandit31@bandit:/tmp/testowyfolder6/repo$ cat key.txt
May I come in?

W folderze znalazłem również plik .gitignore:

bandit31@bandit:/tmp/testowyfolder6/repo$ cat .gitignore
*.txt

Jego zawartość sugeruje, że git ma ignorować wszystkie pliki tekstowe. Usunąłem więc ten plik przed próbą dodania stworzonego pliku key.txt:

bandit31@bandit:/tmp/testowyfolder6/repo$ rm .gitignore
bandit31@bandit:/tmp/testowyfolder6/repo$ ls -la
total 20
drwxr-sr-x 3 bandit31 root 4096 Oct 1 19:33 .
drwxr-sr-x 3 bandit31 root 4096 Oct 1 19:12 ..
drwxr-sr-x 8 bandit31 root 4096 Oct 1 19:13 .git
-rw-r--r-- 1 bandit31 root 15 Oct 1 19:29 key.txt
-rw-r--r-- 1 bandit31 root 147 Oct 1 19:13 README.md

Następnie korzystając z dokumentacji gita dodałem utworzony plik w następujący sposób:

bandit31@bandit:/tmp/testowyfolder6/repo$ git branch -a
*master
 remotes/origin/HEAD -> origin/master
 remotes/origin/master
bandit31@bandit:/tmp/testowyfolder6/repo$ git add key.txt
bandit31@bandit:/tmp/testowyfolder6/repo$ git commit -m "Upload a file"
[master b029f75] Upload a file
1 file changed, 1 insertion(+)
create mode 100644 key.txt
bandit31@bandit:/tmp/testowyfolder6/repo$ git push origin master

Could not create directory '/home/bandit31/.ssh'.
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:98UL0ZWr85496EtCRkKlo20X3OPnyPSB5tB5RPbhczc.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/bandit31/.ssh/known_hosts).
This is a OverTheWire game server. More information on http://www.overthewire.org/wargames
bandit31-git@localhost's password:
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: ### Attempting to validate files… ####
remote:
remote: .oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
remote:
remote: Well done! Here is the password for the next level:
remote: 56a9bf19c63d650ce78e6ec0354ee45e
remote:
remote: .oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
remote:
To ssh://localhost/home/bandit31-git/repo
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'ssh://bandit31-git@localhost/home/bandit31-git/repo'

Na początku sprawdziłem na jakiej gałęzi się znajduję. Następnie przygotowałem plik do zatwierdzenia w lokalnym repozytorium. Zatwierdziłem ten plik w lokalnym repozytorium (zrobiłem tzw. commit przed wysłaniem go na zdalne repo). Na końcu zrobiłem tzw. pusha, czyli wysłałem plik na zdalne repozytorium.

W wyniku otrzymałem odpowiedź z hasłem do następnego poziomu.

Jako bonus, sprawdziłem też historię commitów:

bandit31@bandit:/tmp/testowyfolder6/repo$ git log
commit b029f759be0eb1f564f7fac45e1de813e29bcc9e
Author: bandit31 <bandit31@overthewire.org>
Date:   Thu Oct 1 19:36:30 2020 +0200

    Upload a file

commit 701b33b545902a670a46088731949ae040983d80
Author: Ben Dover <noone@overthewire.org>
Date:   Thu May 7 20:14:56 2020 +0200

    initial commit

Powyżej wyraźnie widać dodany przeze mnie commit 🙂


32

After all this git stuff its time for another escape. Good luck!

Commands you may need to solve this level

sh, man

Na daną chwilę ostatni poziom do rozwiązania w Bandit. Po zalogowaniu się na maszynę dostałem następujący komunikat:

WELCOME TO THE UPPERCASE SHELL
>> 

Mamy do czynienia z UPPERCASE SHELLem ( cokolwiek to znaczy 😮 ). Spróbowałem podmienić powłokę na /bin/bash:

>> ls
sh: 1: LS: not found
>> /bin/bash -i
sh: 1: /BIN/BASH: not found

Jednakże bezskutecznie. Wydaje się, że obecna powłoka podmienia wielkość wszystkich liter które wpisuję po znaku zachęty.

Aby zamienić powłokę wypróbowałem jeszcze jednego sposobu. Odkryłem że w Linuxie występują tzw. zmienne specjalne, które są definiowane przez system i w przeciwieństwie do innych zmiennych nie można modyfikować ich wartości. Jedną z tych zmiennych jest $0. Na lokalnej maszynie sprawdziłem co kryje się pod tą zmienną:

mateusz@kali:~$ echo $0
/bin/bash

Jak widać zmienna zawiera nazwę mojej powłoki. Wywołałem zmienną na zdalnej maszynie:

>> $0
$ id
uid=11033(bandit33) gid=11032(bandit32) groups=11032(bandit32)

Następnie zmieniłem powłokę z sh na bash i finalnie odczytałem hasło:

$ /bin/bash
bandit33@bandit:~$ ls -la
total 28
drwxr-xr-x  2 root     root     4096 May  7 20:14 .
drwxr-xr-x 41 root     root     4096 May  7 20:14 ..
-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
-rwsr-x---  1 bandit33 bandit32 7556 May  7 20:14 uppershell
bandit33@bandit:~$ cat /etc/bandit_pass/bandit33 
c9c3199ddf4121b10cf581a98d51caee

ZAKOŃCZENIE

Jest to koniec przygody z maszyną Bandit. W komentarzach dajcie znać czy wam się podobało oraz czy chcecie kontynuację serii OverTheWire ( w szczególności zapatruję się na maszyny: Natas i Krypton poświęcone tematyce web-security oraz krypto 😉 )