0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OverTheWire: Bandit 解説 (level 4 → level 5 から level 7 → level 8)

0
Posted at

今回もOverTheWireのBanditの問題解説をする。

OverTheWireやBanditが何か気になる人は前回の記事OverTheWire: Bandit 解説 (level 0 から level 3 → level 4)を参照してほしい。

目次

  1. Level 4 → Level 5
  2. Level 5 → Level 6
  3. Level 6 → Level 7
  4. Level 7 → Level 8

解説

Level 4 → Level 5

The password for the next level is stored in the only human-readable file in the inhere directory. Tip: if your terminal is messed up, try the “reset” command.

パスワードはinhereというディレクトリの中の人が読めるファイルにあるらしい。

とりあえず

cd inhere

でinhereディレクトリに移動する。

このinhereの中がどのようになっているかを確認するために

ls -lah

とすると

total 48K
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file00
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file01
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file02
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file03
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file04
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file05
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file06
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file07
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file08
-rw-r----- 1 bandit5 bandit4   33 Jun 24 14:59 -file09
drwxr-xr-x 2 root    root    4.0K Jun 24 14:59 .
drwxr-xr-x 3 root    root    4.0K Jun 24 14:59 ..

と出力される。

-file00から-file09までのどこかにパスワードが隠されていそうだ。

もちろんすべてのフォルダの中身を確認してパスワードを見つけることも可能だが、ここでは人が読めるファイルという条件があるため、その条件を用いてパスワードがあるファイルを特定していく。

そのような条件を用いるためのコマンドはfileコマンドである。

基本構文:file ファイル名

fileコマンドを使うとファイルの種類がわかる。

今回は調べたいファイルが複数あるため*を用いて一括で調べる。

file ./-file*

*は任意の文字列(0文字以上)にマッチするという意味だ。

出力は

./-file00: data
./-file01: data
./-file02: data
./-file03: data
./-file04: data
./-file05: data
./-file06: OpenPGP Public Key
./-file07: ASCII text
./-file08: data
./-file09: Motorola S-Record; binary data in text format

となる。

人間が読めるファイルとはバイナリファイルなどではなく、意味のある文字列が書かれているファイルだという事。

つまりASCII textである-file07を見ればよい。

cat ./-file07

とするとパスワードが見つかる。

Level 5 → Level 6

The password for the next level is stored in a file somewhere under the inhere directory and has all of the following properties:
・human-readable
・1033 bytes in size
・not executable

パスワードはinhereディレクトリのどこかにあり、そのファイルは「人間が読める」「1033バイト」「実行不可能」という条件がある。

とりあえず

cd inhere

としてinhereに移動し

ls -lah

とすると

total 88K
drwxr-x--- 22 root bandit5 4.0K Jun 24 14:59 .
drwxr-xr-x  3 root root    4.0K Jun 24 14:59 ..
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere00
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere01
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere02
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere03
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere04
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere05
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere06
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere07
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere08
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere09
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere10
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere11
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere12
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere13
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere14
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere15
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere16
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere17
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere18
drwxr-x---  2 root bandit5 4.0K Jun 24 14:59 maybehere19

と出力された。

おそらくmaybehere00からmaybehere19のどこかにパスワードはあるのだろう。

一つ一つ中身を探していくのは途方もないので、ここではfindコマンドを使うことによって、一気に条件に合ったファイルを見つける。

基本構文:find [検索する場所] [条件]

[条件]には様々な条件を入れることができる。

  • -type fは通常のファイルを探す
  • -type dはディレクトリを探す
  • -name [名前]は[名前]に一致するファイルを探す
  • -iname [名前]は大文字小文字を区別せずに[名前]に一致するファイルを探す
  • -size [サイズ]は[サイズ]のサイズを持つものを探す
  • -executableは実行可能なものを探す

今回探すのは「人間が読める」「1033バイト」「実行不可能」のファイルであるということだった。

よってinhereディレクトリにおいて

find . -type f -size 1033c ! -executable 

とすると

./maybehere07/.file2

と出力され、maybehere07ディレクトリの.file2ファイルが目的のファイルだとわかる。(.はカレントディレクトリを表し、!は否定を表す)

よってあとは

cat ./maybehere/.file2

とすればパスワードを得ることができる。

Level 6 → Level 7

The password for the next level is stored somewhere on the server and has all of the following properties:
・owned by user bandit7
・owned by group bandit6
・33 bytes in size

パスワードはサーバのどこかに隠されており、「bandit7というユーザーが所有している」「bandit6というグループが所有している」「サイズは33バイトである」という条件がある。

この3つの条件でファイルを探すために前問同様、findコマンドを用いる。findコマンドはユーザーやグループによっても絞り込むことができる。

  • -user [ユーザー名]は特定のユーザー名を持つファイルを探す
  • -group [グループ名]は特定のグループ名を持つファイルを探す

よって

find / -user "bandit7" -group "bandit6" -size 33c

とすると

find: ‘/root’: Permission denied
find: ‘/boot/efi’: Permission denied
find: ‘/boot/lost+found’: Permission denied
...
略
...
find: ‘/var/lib/chrony’: Permission denied
/var/lib/dpkg/info/bandit7.password

と出力され、パスワードを持っていそうなファイルパスが見つかる。(/ はルートディレクトリ / 以下を全部検索する)

ちなみにPermission deniedというエラーがたくさん出てきて非常に見ずらいが、そんな時は2>/dev/nullを使うことで解決できる。

2>/dev/nullはエラーメッセージを捨てるという意味。詳しいことは各々AIに聞いてもらったほうが早いのだが、簡単に言うと、2 は標準エラー出力、> はプログラムの出力先変更、/dev/null は特殊なファイルでここに送ったデータを全部捨てるという意味だ。

すなわち「標準エラー出力の出力先を/dev/nullファイルに変更する」という事だ。

find / -user "bandit7" -group "bandit6" -size 33c 2>/dev/null

とすることで

/var/lib/dpkg/info/bandit7.password

のみを得ることができる。

あとはcatでも使って

cat /var/lib/dpkg/info/bandit7.password

とすればパスワードが見つかる。

Level 7 → Level 8

The password for the next level is stored in the file data.txt next to the word millionth

パスワードはdata.txtファイルの"millionth"という文字列の隣にあるらしい。

ls -lah

としてファイルを確認してみると

total 4.1M
drwxr-xr-x   2 root    root    4.0K Sep 26 21:55 .
drwxr-xr-x 150 root    root    4.0K Sep 26 21:58 ..
-rw-r--r--   1 root    root     220 Feb 13  2026 .bash_logout
-rw-r--r--   1 root    root    3.8K Sep 26 21:38 .bashrc
-rw-r--r--   1 root    root     807 Feb 13  2026 .profile
-rw-r-----   1 bandit8 bandit7 4.0M Sep 26 21:55 data.txt

となる。data.txtが存在していることがわかる。

ではdata.txtの中身を確認するために

cat data.txt

としてみると、大量の文字列が表示される。とても"millionth"という文字列を探すことはできなさそうである。

ここで使えるのがgrepコマンドである。

基本構文:grep "探したい文字列" ファイル名

grepコマンドを使うことで、ファイルの中から指定した文字列やパターンに一致する行を探すことができる。

grepコマンドは基本的に一致する文字列だけを表示するのではなく、その文字列を含む行を表示する。

よって

grep "millionth" data.txt

とするとパスワードを得ることができる。

また補足として様々なオプションを紹介する。

  • -iは大文字・小文字の区別をなくす
  • -nは行番号を表示させる
  • -A "数字”は一致した行の後ろ"数字"行も表示
  • -B "数字”は一致した行の前"数字"行も表示
  • -C "数字”は一致した行の前後"数字"行も表示

最後に

今回はLevel 4 → Level 5からLevel 7 → Level 8までを解説した。findやgrepなどのコマンドを使うことによって、条件からパスワードを見つけに行く問題が多かった。ただ単に時間をかけてゴリ押しでパスワードを探すのではなく、コマンドを使ってスマートに見つけることで勉強になる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?