こんとろーるしーこんとろーるぶい

週末にカチャカチャッターン!したことを貼り付けていくブログ

TSG CTF 2019 Writeup - Obliterated File, Obliterated File Again

Obliterated File

Question

※ This problem has unintended solution, fixed as "Obliterated File Again". Original problem statement is below.

Working on making a problem of TSG CTF, I noticed that I have staged and committed the flag file by mistake before I knew it.
I googled and found the following commands, so I'm not sure but anyway typed them. It should be ok, right?

※ この問題は非想定な解法があり,"Obliterated File Again" で修正されました.元の問題文は以下の通りです.

TSG CTFに向けて問題を作っていたんですが,いつの間にか誤ってflagのファイルをコミットしていたことに気付いた!
とにかく,Google先生にお伺いして次のようなコマンドを打ちこみました.よくわからないけどこれできっと大丈夫...?

$ git filter-branch --index-filter "git rm -f --ignore-unmatch problem/flag" --prune-empty -- --all
$ git reflog expire --expire=now --all
$ git gc --aggressive --prune=now
Difficulty Estimate: easy

Solution

添付ファイルを展開すると、.gitを含むファイル群。

.git/objects配下を適当にgit cat-file -p <ハッシュ>で表示するがフラグは見つからない。
packファイルがあったので、packファイルをunpackする。
以下の記事を参考にした。

qiita.com

root@kali:~/Contest/TSGCTF2019/easy_web# cd /tmp/

root@kali:/tmp# git init newrepo
Initialized empty Git repository in /tmp/newrepo/.git/

root@kali:/tmp# cd newrepo/

# unpackする
root@kali:/tmp/newrepo# git unpack-objects < ~/Contest/TSGCTF2019/easy_web/.git/objects/pack/pack-358c51ff6239c4616442ad260a7f71391fec6fc2.pack
Unpacking objects: 100% (99/99), done.

# objects配下に大量のファイルが展開されていることを確認する
root@kali:/tmp/newrepo# ll .git/objects/*/*
-r--r--r-- 1 root root 193 May  5 10:41 .git/objects/00/ba81bd54c79a5e712435ee9ecd2b2d8585917c
-r--r--r-- 1 root root 313 May  5 10:41 .git/objects/02/d365359d84a5d4f4317fa3549fe073a024c502
-r--r--r-- 1 root root 160 May  5 10:41 .git/objects/03/afe118213dd7af1979e70f12d1deca4d3d5477
(snip)

# cat-fileするためにハッシュに加工する
root@kali:/tmp/newrepo# ll .git/objects/*/* | sed 's@.*objects@@' | sed 's@/@@g'
00ba81bd54c79a5e712435ee9ecd2b2d8585917c
02d365359d84a5d4f4317fa3549fe073a024c502
03afe118213dd7af1979e70f12d1deca4d3d5477
(snip)

root@kali:/tmp/newrepo# mkdir export

# objects配下の全ファイルをcat-fileで生成
root@kali:/tmp/newrepo# for object in `ll .git/objects/*/* | sed 's@.*objects@@' | sed 's@/@@g'`; do git cat-file -p $object > ./export/$object; done

root@kali:/tmp/newrepo# ll export/
total 392
-rw-r--r-- 1 root root  230 May  5 10:51 00ba81bd54c79a5e712435ee9ecd2b2d8585917c
-rw-r--r-- 1 root root  458 May  5 10:51 02d365359d84a5d4f4317fa3549fe073a024c502
-rw-r--r-- 1 root root  218 May  5 10:51 03afe118213dd7af1979e70f12d1deca4d3d5477
(snip)

# flagでgrepすると111eb967d40ae9bc7b2d16bbab7aaac5746ba1dcにフラグがありそう
root@kali:/tmp/newrepo# grep flag export/*
export/02d365359d84a5d4f4317fa3549fe073a024c502:flag = File.open("./flag", "r") do |f|
export/02d365359d84a5d4f4317fa3549fe073a024c502:    db.exec "INSERT INTO accounts VALUES ('admin', '#{flag}');"
export/6eec6e57cc9eb5aa67f09fb73bdb3b933d7fdded:The flag is admin's password.
export/8ce8f78879f344df4e079a81048e7e18fdb29fed:100644 blob 111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc    flag
export/b8b02f91a5b2407cb4014c81440ce7620c4830bc:100644 blob 111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc    flag
export/c9319554ea383df062bafa9e96915ffe62136457:100644 blob 111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc    flag
export/e518bb214047db324b2e9b09d5617d84c6cc4ebf:100644 blob 111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc    flag
export/ebc4754f23719c17eedf24af0187be86b52e71d2:flag = File.open("./flag", "r") do |f|
export/ebc4754f23719c17eedf24af0187be86b52e71d2:    db.exec "INSERT INTO accounts VALUES ('admin', '#{flag}');"

root@kali:/tmp/newrepo# file export/111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc
export/111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc: zlib compressed data

# zlib圧縮ファイルであるため展開
root@kali:/tmp/newrepo# printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" |cat - ./export/111eb967d40ae9bc7b2d16bbab7aaac5746ba1dc | gzip -dc > flag
gzip: stdin: unexpected end of file

root@kali:/tmp/newrepo# cat flag
TSGCTF{$_git_update-ref_-d_refs/original/refs/heads/master}`

フラグゲット
TSGCTF{$_git_update-ref_-d_refs/original/refs/heads/master}

Obliterated File Again

Question

I realized that the previous command had a mistake. It should be right this time...?

さっきのコマンドには間違いがあったことに気づきました.これで今度こそ本当に,本当に大丈夫なはず......?

$ git filter-branch --index-filter "git rm -f --ignore-unmatch *flag" --prune-empty -- --all
$ git reflog expire --expire=now --all
$ git gc --aggressive --prune=now
Difficulty Estimate: easy - medium

Solution

Obliterated File と全く同じ手順でフラグゲットできた。
Obliterated Fileが想定外の解法だった?

TSGCTF{$_git_update-ref_-d_refs/original/refs/heads/master_S0rry_f0r_m4king_4_m1st4k3_0n_th1s_pr0bl3m}