車輪の再開発ブログ

Python & CTF Beginner!

picoCTF 2022 Operation Oni writeup

コンテスト中に解けなかった問題の復習です。

問題文

Download this disk image, find the key and log into the remote machine.
Note: if you are using the webshell, download and extract the disk image into /tmp not your home directory.
* Download disk image
* Remote machine: ssh -i key_file -p XXXXXX ctf-player@saturn.picoctf.net

XXXXは伏字。 圧縮されたイメージファイル ( disk.img.gz ) がダウンロードできる。

ヒント

なし

解法

全く手も足も出なかったので、kusuwada先生のwriteup picoCTF2022 [Forensics] writeup - 好奇心の足跡 を読む。

ふむふむ。ざっくりとした流れは、「ディスクイメージの中に秘密鍵があるので獲得して、用意されたリモートサーバーにsshで入る」っぽい。

(ここで、自分の英語力がないことが発覚。「find the key and log into the remote machine.」って、「キーとログをリモートサーバーの中から探せ」じゃなくて、「keyを見つけて、リモートマシーンにログインせよ」なんですね。下のコマンドにも、key_fileって書いてくれてるし・・・。すごい親切な問題じゃん。)

ということで、まずは、秘密鍵を取り出すところから。

まずは、gz展開をする。

# gunzip disk.img.gz

# ls
disk.img

sleuthkit を使って、探し出す。全体を、mmlsで確認。

# mmls disk.img 
DOS Partition Table
Offset Sector: 0
Units are in 512-byte sectors

      Slot      Start        End          Length       Description
000:  Meta      0000000000   0000000000   0000000001   Primary Table (#0)
001:  -------   0000000000   0000002047   0000002048   Unallocated
002:  000:000   0000002048   0000206847   0000204800   Linux (0x83)
003:  000:001   0000206848   0000471039   0000264192   Linux (0x83)

003の中身は、

# fls -o 0000206848 disk.img 
d/d 11: lost+found
d/d 12: boot
d/d 13: etc
d/d 79: proc
d/d 80: dev
d/d 81: tmp
d/d 82: lib
d/d 85: var
d/d 94: usr
d/d 104:    bin
d/d 118:    sbin
d/d 458:    home
d/d 464:    media
d/d 468:    mnt
d/d 469:    opt
d/d 470:    root
d/d 471:    run
d/d 473:    srv
d/d 474:    sys
V/V 33049:  $OrphanFiles

今回は、writeupを読んで、場所を知っているので、root ディレクトリ(470) の .ssh ディレクトリ (3916)の中身が

# fls -o 0000206848 disk.img 3916
r/r 2345:   id_ed25519
r/r 2346:   id_ed25519.pub

と辿ります。 id_ed25519 があるので、key_fileとして出力しておきます。

# fcat -o 0000206848 /root/.ssh/id_ed25519 disk.img > key_file

(ここで、sshがdockerに入っていないことがわかったので、apt intall sshをした。)

ssh -i key_file -p XXXXX ctf-player@saturn.picoctf.net

をすると、

# ssh -i key_file -p 59214 ctf-player@saturn.picoctf.net
The authenticity of host '[saturn.picoctf.net]:59214 ([18.217.86.78]:59214)' can't be established.
ECDSA key fingerprint is SHA256:0L/+wJ14/Sk4s6Ue+TxXnAW7qNBuaMeIxA9dXp2zzaU.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[saturn.picoctf.net]:59214,[18.217.86.78]:59214' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'key_file' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "key_file": bad permissions
ctf-player@saturn.picoctf.net's password: 

とkey_fileのファイルの権限がオープンすぎるぞ怒られたので、

chmod 600 key_file

をしてやり直すと、ログインできました。

ログイン後は、

ctf-player@challenge:~$ ls
flag.txt
ctf-player@challenge:~$ cat flag.txt 
picoCTF{k3y_5l3u7h_339601ed}

に鍵があります。