unzipper
WebページのURLと、以下のファイルが与えられた。
An URL of a web page and these files were given:
unzipper
|-- Dockerfile
|-- docker-stuff
| |-- cleanup
| |-- default
| `-- www.conf
`-- index.php
index.php
は、以下の処理をするものだった。
- セッションごとにランダムな名前の作業用ディレクトリを用意する。
$_FILES['file']
が設定されている場合は、そのファイルをzipファイルとして展開する。
- そうでなく、
$_GET['file']
が設定されている場合は、
- 指定されたパスを
realpath
関数で処理した結果が、空でも文字列 flag
を含むものでもないかをチェックする。
- チェックを通れば、指定されたパスを
readfile
関数に渡す。
また、Dockerfile
より、ファイル /flag.txt
が用意されることが読み取れた。
What index.php
does is:
- For each sessions, prepare a working directory having a random name.
- If
$_FILES['file']
is set, unzip the file.
- If it is not set and
$_GET['file']
is set:
- Apply the function
realpath
to the path and check if the result is not empty nor contains a string flag
.
- If it passes the check, call the function
readfile
with the path.
Also, from the file Dockerfile
, I found that there will be a file /flag.txt
.
ここで、各関数の以下の性質を用いる。
realpath
関数は、渡されたパス中のシンボリックリンクを展開する。
readfile
関数は、読み込むファイルをURLで指定できる。
読み込むファイルのパスとして file:///flag.txt
を指定すると、
readfile
関数はこれをURLと解釈してファイル /flag.txt
を読みに行く一方、
realpath
関数はこれをディレクトリ file:
内のファイル flag.txt
と解釈する。
この flag.txt
をシンボリックリンクにしておくことで、realpath
の処理結果から文字列 flag
を消し、readfile
関数を実行させることができる。
これを実行するため、まずWebページにファイルを送信するための以下のファイルを用意した。
I decided to use these characteristics of the functions:
- The function
realpath
will expand symbolic links in the given path.
- The function
readfile
accepts an URL as the path of the file to read.
When I specify file:///flag.txt
as the path of the file to read,
the functon readfile
will treat this as an URL and read the file /flag.txt
.
On the other hand, the function realpath
will interpret this as the file flag.txt
in the directory file:
.
We can remove a string flag
from the result of the function realpath
to have it call the function readfile
by preparing this flag.txt
as a symbolic link.
To realize this, firstly I created this file for sending files to the web page:
send.html
さらに、CS50 Sandboxを開き、以下のコマンドでファイル file_flag.zip
を作成した。
Then, I opened CS50 Sandbox and created a file file_flag.zip
using these connamds:
mkdir file:
cd file:
touch meow.txt
ln -s meow.txt flag.txt
cd ..
zip -ry file_flag.zip file:
作成した file_flag.zip
を send.html
から送信した後、http://65.108.176.76:8200/?file=file:///flag.txt
にアクセスすることで、flagが得られた。
I obtained the flag by sending the file file_flag.zip
via send.html
and accessing http://65.108.176.76:8200/?file=file:///flag.txt
after that.
hxp{at_least_we_have_all_the_performance_in_the_world..._lolphp_:/}
writeup by MikeCAT
hxp CTF 2021