overthwire やってみた

level 12->13

この辺から、急にむずくなった気がする。まあ、tar の理解をしなければならないだけなんですど。

お題

The password for the next level is stored in the file data.txt, which is a hexdump of a file that has been repeatedly compressed. For this level it may be useful to create a directory under /tmp in which you can work using mkdir. For example: mkdir /tmp/myname123. Then copy the datafile using cp, and rename it using mv (read the manpages!)

何重にも圧縮されているファイルからパスワードを解読せよってことです。最初は hexdump file.

// create temp file
$ mkdir /tmp/mydirectory

// copy data.txt file into the directory.
$ cp data.txt /tmp/mydirectory

$ cd /tmp/mydirectory/

// reverse hexdump file with xxd
$ cat data.txt | xxd -r data.txt > data_xxd_reverse
$ file data_xxd_reverse 
data_xxd_reverse: gzip compressed data, was "data2.bin", last modified: Tue Oct 16 12:00:23 2018, max compression, from Unix

// unzip data file with zcat
$ zcat data_xxd_reverse > data_zcat
$ file data_zcat
data_zcat: bzip2 compressed data, block size = 900k

// decode data file with bzip2
$ bzip2 -d data_zcat
bzip2: Can't guess original name for data_zcat -- using data_zcat.out
$ file data_zcat.out
data_zcat.out: gzip compressed data, was "data4.bin", last modified: Tue Oct 16 12:00:23 2018, max compression, from Unix

$ zcat data_zcat.out > data_zcat2
$ file data_zcat2
data_zcat2: POSIX tar archive (GNU) 

// extract file with tar from achive
$ tar -xvf data_zcat2

$ file data5.bin
data5.bin: POSIX tar archive (GNU)

$ tar -xvf data5.bin
$ file data6.bin
data6.bin: bzip2 compressed data, block size = 900k

$ bzip2 -d data6.bin
bzip2: Can't guess original name for data6.bin -- using data6.bin.out
$ file data6.bin.out
data6.bin.out: POSIX tar archive (GNU)

$ tar -xvf data6.bin.out
$ file data8.bin
data8.bin: gzip compressed data, was "data9.bin", last modified: Tue Oct 16 12:00:23 2018, max compression, from Unix

$ zcat data8.bin > data_zcat3
$ file data_zcat3
data_zcat3: ASCII text // => ♪───O(≧∇≦)O────♪

$ cat data_zcat3 // => output password

参考: GNU tar 1.32: GNU tar: an archiver tool

tar について

tape archive の略。archive とは呼ばれる、他のファイルがひとまとめになった単一のファイルをさす。archive に含まれるファイルを取り出すことを extarct という。

上でしている tar comannd の動作を言語化すると、「data_zcat2 に含まれるファイルを全て抜き出し、その過程を出力せよ。」というもの。

オプションはそれぞれ
-x : --extract
-v : --verbose
-f : --file

level 13 -> 14

お題

The password for the next level is stored in /etc/bandit_pass/bandit14 and can only be read by user bandit14. For this level, you don’t get the next password, but you get a private SSH key that can be used to log into the next level. Note: localhost is a hostname that refers to the machine you are working on

パスワードは bandit14 user からでしか読み取れない bandit14 ファイルに保存されているそうです。 private SSH key があるらしい。

とりあえず、何も考えず cat してみる。

$ cat  /etc/bandit_pass/bandit14 
cat: /etc/bandit_pass/bandit14: Permission denied

Permission denied された。なぜか? 自分が bandit14 user じゃないからではと考えた。

$ whoami
bandit13 // -> 現在 bandit13 と関連づいている。

今、自分は bandit13 user であることがわかった。

bandit14 ファイルの権限を調べる。

$ ls -al  /etc/bandit_pass/bandit14 
-r-------- 1 bandit14 bandit14 33 Oct 16 14:00 /etc/bandit_pass/bandit14

-r--------/etc/bandit_pass/bandit14 はファイルであり、owner からしか読み取ることができない。

ちなみに、例えば -rwxrw-r-- だったとしたら
・ファイルである
owner から読み取り・書き込み・実行ができる(rwx)
group から読み取り・書きこみができる(rw)
other から読み取りができる(r)
という意味になる。

参照:What do the fields in ls -al output mean? - Unix & Linux Stack Exchange

ownergroupother はそれぞれ

f:id:bitsukun75:20190325151947p:plain

参照: Understanding Basic File Permissions and ownership in Linux – The Geek Diary

対象ファイルを読み取れるユーザーと現在のユーザーが違うことがわかった。 対象ファイルの権限を変えてみる

$ chown bandit13:bandit13 /etc/bandit_pass/bandit14
chown: changing ownership of '/etc/bandit_pass/bandit14': Operation not permitted

操作できない。

bandit14 になりすましてファイルを読み取るとことなのか・・・?と考えているときにホームディレクトリに ssh 秘密鍵があることに気づいた。

$ pwd
/home/bandit13
$ ls
sshkey.private

ssh キーは AWS とか Git に接続するときに使っていたのでどんなものかは軽く知っていた。ただ今回の問題とどう関係があるのだろうか?

private key と public key はペアで使うもの。 bandit14 が public key だとしたら、このホームディレクトリにある private を使ってどうにかしてれるのでは?

なーんだ簡単だ、ssh の鍵を指定してっと・・・と下のようにしてやったら broken pipe と出て原因わからなかった。

$ ssh bandit14@bandit.labs.overthewire.org -i sshkey.private

bandit HP の推奨コマンドを片っ端から調べるがどれも別ホストへ接続状況を調べるコマンドだった。

bandit14 に繋がらないのにどう使えっていうんだよ・・・と思いつつ問題文を見直したら、 丁寧に Note: localhost is a hostname that refers to the machine you are working onと書いてあるのを見落としていた。

つまりこう。

$ ssh bandit14@localhost -i sshkey.private // -> success!
$ cat /etc/bandit_pass/bandit14 // -> password found

level14 -> 15

お題

The password for the next level can be retrieved by submitting the password of the current level to port 30000 on localhost.

$ nc localhost 30000
{paste password}
Correct!
{New password}

level15 -> 16

お題

The credentials for the next level can be retrieved by submitting the password of the current level to a port on localhost in the range 31000 to 32000. First find out which of these ports have a server listening on them. Then find out which of those speak SSL and which don’t. There is only 1 server that will give the next credentials, the others will simply send back to you whatever you send to it.