淡々と備忘録を綴る

IT周りの行き詰まったところやメモを備忘録として記録します

picoCTF 2024[General Skills] 400pts SansAlpha WriteUp

概要

2024年3月に実施されたpicoCTFに参加してきたので、備忘録がてらwriteupを書きます。 今回はgeneral問題の一番点数の高かった400pts問題のwirteupです。

問題

The Multiverse is within your grasp! Unfortunately, the server that contains the secrets of the multiverse is in a universe where keyboards only have numbers and (most) symbols. Additional details will be available after launching your challenge instance.

多元宇宙はあなたの手の中にあります! 残念なことに、多元世界の秘密が含まれるサーバーは、キーボードに数字と (ほとんどの) 記号しかない世界にあります。

解法

ssh先のサーバーでは英文字が使えないらしい。
多分英文字を使わずにコマンドを打ってディレクトリを見たらflagのテキストかなんかがあるだろうと推測。
シェル芸という世界を耳にしたことはあったものの、全然詳しくなかったので以下のサイトを参考にペイロードを組み立てた。

zenn.dev

参考サイトとは違い、今回は数字の使用が許されているので多少助かった。

まず最初にlsを打ってみる。

.>&$
"$(<$)"
__="${_##*. ??}"

これは. コマンドの結果をファイル$に格納したあと、ファイルを読み出して変数__にトリミングしながら格納している。

これで__に文字列lename [arguments]が入る

___***=${__**:0:1}${*__: -2:1}

これでlsが作れた。__の文字列からlとsを抜き出して文字を作っている。

問題サーバーで実行してみる。

SansAlpha$ $___
'$'   blargh   on-calastran.txt

怪しいtxtがあるので、txtを見たい。あとblarghってなに?catとか打ちたいけど、あまりコマンドつくるのはだるいなと思案。とりあえずファイルの種類を見たいのでls -lを実行する(llは問題サーバー上で実行できなかった。)

$___ -${__:0:1}

これでls -lが打てた。

SansAlpha$ $___ -${__:0:1}
total 8
-rw-rw-r-- 1 ctf-player ctf-player   69 Mar 19 15:37 '$'
drwxr-xr-x 1 ctf-player ctf-player   22 Mar 12 00:10  blargh
-rw-r--r-- 1 root       root       1160 Feb  7 17:25  on-calastran.txt

ディレクトリだる。cd作ることも視野にいれる。

とりあえず引数にblarghやらon-calastran.txtやらを簡単に取れるようにしたいから、lsからなんとかしたい

変数__ にcの文字がないのでcatが作れない。nlコマンドで一旦代用する。

最終的には以下のコマンドを打ちたい。lsの結果を使ってnlするコマンド。

____=($(${___}))
$_____ "${____[2]}"

このとき

$___ = ls
$____ = ls結果の受け皿
$_____ = nl

を目指す。

実行してテキストファイルの中身を見ると以下だった。フラグここにないんかい。

SansAlpha$ .>&$
SansAlpha$ "$(<$)"
bash: $'bash: .: filename argument required\n.: usage: . filename [arguments]': command not found

SansAlpha$ __="${_##*. ??}"
SansAlpha$ ___=${__:0:1}${__: -2:1}
SansAlpha$ $___
'$'   blargh   on-calastran.txt

SansAlpha$ _____=${__:2:1}${__:0:1} ____=($(${___}))
SansAlpha$ _____=${__:2:1}${__:0:1} 
SansAlpha$ ____=($(${___}))
SansAlpha$ $_____ "${____[2]}"
     1    The Calastran multiverse is a complex and interconnected web of realities, each
     2    with its own distinct characteristics and rules. At its core is the Nexus, a
     3    cosmic hub that serves as the anchor point for countless universes and
     4    dimensions. These realities are organized into Layers, with each Layer
     5    representing a unique level of existence, ranging from the fundamental building
     6    blocks of reality to the most intricate and fantastical realms. Travel between
     7    Layers is facilitated by Quantum Bridges, mysterious conduits that allow
     8    individuals to navigate the multiverse. Notably, the Calastran multiverse
     9    exhibits a dynamic nature, with the Fabric of Reality continuously shifting and
    10    evolving. Within this vast tapestry, there exist Nexus Nodes, focal points of
    11    immense energy that hold sway over the destinies of entire universes. The
    12    enigmatic Watchers, ancient beings attuned to the ebb and flow of the
    13    multiverse, observe and influence key events. While the structure of Calastran
    14    embraces diversity, it also poses challenges, as the delicate balance between
    15    the Layers requires vigilance to prevent catastrophic breaches and maintain the
    16    cosmic harmony.

ここで、もう一個のディレクトリの中身をみたいのでcdを考える。

というかtreeでもいいかも。treeでええやん。

というわけで以下の形にして実行

$___ = ls
$____ = ls結果の受け皿
$_____ = nl
$______ = tree

treeはいってないやん。ふぁっきん。

SansAlpha$ ______=${__: -3:1}${__:9:1}${__:1:1}${__:1:1}
SansAlpha$ $______ 
bash: tree: command not found

よって、使える文字を追加してシェルを作り直す。目的はcdを使うこと。(これはローカルでテストしている。後述するが問題サーバーではこの方法はできなかった。)

┌──(kali㉿kali)-[~/ctf/pico]
└─$ .1>>&$
                                                                            
┌──(kali㉿kali)-[~/ctf/pico]
└─$ cat \$
bash: .: filename argument required
.: usage: . filename [arguments]
.1: command not found

treeはいらないので以下の形にする。ついでにlsの-指定が崩れるので書き直す。

$___ = ls
$____ = ls結果の受け皿
$_____ = nl
$______ = cd

これを実現するためのコマンドを今一度改めてまとめる。これを実行すれば次に進めるはず。

**.>&$
.1>>&$
"$(<$)"
__="${_##*. ??}"
# このときの__の中身は以下
# $ echo $__          
# lename [arguments
# .1: command not found**
___***=${__**:0:1}${*__:16:1} # lsの作成
$___ -${__:0:1} # ls -lを実行してファイル確認
____=($(${___})) # ls結果を格納する配列の作成
_____=${__:2:1}${__:0:1} # nlの作成
$_____ "${____[2]}" # nlでファイルを表示
______=${__:23:1}${__:29:1} # cdの作成
$______ "${____[1]}" # cdの実行
$___ -${__:0:1} # ls -lを実行してファイル確認

$______ "${____[3]}"

ところがどっこい。問題サーバーでは>>が使えないらしい。よって別の変数を使って回避する。

**.1>&$
"$(<$)"
_______="${_##*.}"
# $ echo $_______     
# 1: command not found**

______=${**_______**:3:1}${**_______**:9:1} # cdの作成
$______ "${____[1]}" #cdの実行
$___ -${__:0:1} # ls -lを実行してファイル確認

これにともない、前段の手順を修正。

**.>&$
"$(<$)"
__="${_##*. ??}"
# このときの__の中身は以下
# $ echo $__          
# lename [arguments]**
___***=${__**:0:1}${*__:16:1} # lsの作成
$___ -${__:0:1} # ls -lを実行してファイル確認
____=($(${___})) # ls結果を格納する配列の作成
_____=${__:2:1}${__:0:1} # nlの作成
$_____ "${____[2]}" # nlでファイルを表示

現状の変数状況をまとめると以下。

$__ = **文字列「lename [arguments]」
$___ = ls
$____ = ls結果の受け皿
$_____ = nl
$______ = cd
$_______ = 文字列「1: command not found」

ディレクトリを見るとflagがあった。

SansAlpha$ $______ "${____[1]}" # cd blargh
SansAlpha$ $___ -${__:0:1} # ls -l
total 8
-rw-r--r-- 1 root root   53 Mar 12 00:10 flag.txt
-rw-r--r-- 1 root root 1090 Feb  7 17:25 on-alpha-9.txt

あとはさっき作ったlsの配列を更新してflagをみるだけ。

SansAlpha$ ____=($(${___}))
SansAlpha$ $_____ "${____[1]}"
     1    Alpha-9, a distinctive layer within the Calastran multiverse, stands as a
     2    sanctuary realm offering individuals a rare opportunity for rebirth and
     3    introspection. Positioned as a serene refuge between the higher and lower
     4    Layers, Alpha-9 serves as a cosmic haven where beings can start anew,
     5    unburdened by the complexities of their past lives. The realm is characterized
     6    by ethereal landscapes and soothing energies that facilitate healing and
     7    self-discovery. Quantum Resonance Wells, unique to Alpha-9, act as conduits for
     8    individuals to reflect on their past experiences from a safe and contemplative
     9    distance. Here, time flows differently, providing a respite for those seeking
    10    solace and renewal. Residents of Alpha-9 find themselves surrounded by an
    11    atmosphere of rejuvenation, encouraging personal growth and the exploration of
    12    untapped potential. While the layer offers a haven for introspection, it is not
    13    without its challenges, as individuals must confront their past and navigate
    14    the delicate equilibrium between redemption and self-acceptance within this
    15    tranquil cosmic retreat.

SansAlpha$ $_____ "${____[0]}"
     1    return 0 picoCTF{7h15_mu171v3r53_15_m4dn355_8b3d83ad}

ペイロードすべてをまとめると以下のようになる。

SansAlpha$ .
bash: .: filename argument required
.: usage: . filename [arguments]

SansAlpha$ .>&$
SansAlpha$ __="${_##*. ??}"
SansAlpha$ ___=${__:0:1}${__:16:1}
SansAlpha$ $___ 
bash: .: filename argument required
.: usage: . filename [arguments]

SansAlpha$ "$(<$)"
bash: $'bash: .: filename argument required\n.: usage: . filename [arguments]': command not found

SansAlpha$ ___=${__:0:1}${__:16:1}
SansAlpha$ $___ 
bash: .: filename argument required
.: usage: . filename [arguments]

SansAlpha$ $___ -${__:0:1}
bash: .: -.: invalid option
.: usage: . filename [arguments]

SansAlpha$ .>&$
SansAlpha$ "$(<$)"
bash: $'bash: .: filename argument required\n.: usage: . filename [arguments]': command not found

SansAlpha$ __="${_##*. ??}"
SansAlpha$ ___=${__:0:1}${__:16:1}
SansAlpha$ $___ -${__:0:1}
total 8
-rw-rw-r-- 1 ctf-player ctf-player   69 Mar 20 04:39 '$'
drwxr-xr-x 1 ctf-player ctf-player   22 Mar 12 00:10  blargh
-rw-r--r-- 1 root       root       1160 Feb  7 17:25  on-calastran.txt

SansAlpha$ $___ 
'$'   blargh   on-calastran.txt

SansAlpha$ ____=($(${___}))
SansAlpha$ _____=${__:2:1}${__:0:1}
SansAlpha$ .1>&$
SansAlpha$ "$(<$)"
bash: bash: .1: command not found: command not found

SansAlpha$ _______="${_##*.}"
SansAlpha$ ______=${_______:3:1}${_______:9:1}
SansAlpha$ $______ "${____[2]}"
bash: cd: on-calastran.txt: Not a directory

SansAlpha$ $______ "${____[1]}"
SansAlpha$ $___ -${__:0:1}
total 8
-rw-r--r-- 1 root root   53 Mar 12 00:10 flag.txt
-rw-r--r-- 1 root root 1090 Feb  7 17:25 on-alpha-9.txt

SansAlpha$ ____=($(${___}))
SansAlpha$ $_____ "${____[1]}"
     1    Alpha-9, a distinctive layer within the Calastran multiverse, stands as a
     2    sanctuary realm offering individuals a rare opportunity for rebirth and
     3    introspection. Positioned as a serene refuge between the higher and lower
     4    Layers, Alpha-9 serves as a cosmic haven where beings can start anew,
     5    unburdened by the complexities of their past lives. The realm is characterized
     6    by ethereal landscapes and soothing energies that facilitate healing and
     7    self-discovery. Quantum Resonance Wells, unique to Alpha-9, act as conduits for
     8    individuals to reflect on their past experiences from a safe and contemplative
     9    distance. Here, time flows differently, providing a respite for those seeking
    10    solace and renewal. Residents of Alpha-9 find themselves surrounded by an
    11    atmosphere of rejuvenation, encouraging personal growth and the exploration of
    12    untapped potential. While the layer offers a haven for introspection, it is not
    13    without its challenges, as individuals must confront their past and navigate
    14    the delicate equilibrium between redemption and self-acceptance within this
    15    tranquil cosmic retreat.

SansAlpha$ $_____ "${____[0]}"
     1    return 0 picoCTF{7h15_mu171v3r53_15_m4dn355_8b3d83ad}

SansAlpha$  Connection to mimas.picoctf.net closed by remote host.
Connection to mimas.picoctf.net closed.
                                            

おわり。