TCPサーバの接続情報と、ファイル chall および chall.c が与えられた。
chall.c は、入力文字列を printf の第1引数として渡した後、
入力文字列を第1引数として strcmp 関数を呼び出し、返り値が非0ならばこれを繰り返す、という処理を含んでいた。
chall に file コマンドを使うと、64ビットのELFファイルであることがわかったので、
すると、以下の部分があった。
Information for connecting to a TCP server, and files chall and chall.c were given.
chall.c had a part that passes entered string as the first argument of printf function,
and then call strcmp function with the entered string as the first argument, and repeat this if its return value is not zero.
I used the file command to the file chall. It revealed that the file is a 64-bit ELF file. Seeing this, I disassembled that using objdump in
I found this part in the result.
よって、アドレス 0x404040 に8バイトのデータ 0x00000000004010d0 を書き込めば、strcmp を system に置き換え、入力したコマンドを実行できるようになるはずである。
これを行うため、そこまでに出力した文字数を指定の位置に書き込める printf の書式 %n を用いる。
さらに、以下の指定子も用いる。
(数値)$ : 指定した位置のデータを用いるhh : (%n では) 1バイトで書き込むh : (%n では) 2バイトで書き込むまず、以下の文字列を送信した。
This is suggesting that we can replace strcmp with system and enable to execute entered command by writing 8-byte data 0x00000000004010d0 to the address 0x404040.
To achieve this, I used the %n specifier for printf, which writes the number of characters printed so far to the specified address.
I also used these modifiers.
(number)$ : use data at the specified positionhh : (when used with %n) write 1-byte datah : (when used with %n) write 2-byte dataTo begin with, I sent this string.
すると、以下の文字列が出力された。
Then, this string is printed.
このことから、位置 9 が入力文字列の24バイト目 (0-origin) に対応することがわかる。
この性質を用い、以下の書き込みを行うデータ payload.bin を作成した。
0x404040 + 4 に、4バイトの 0x00000000 を書き込む0x404040 + 1 に、1バイトの 0x10 を書き込む0x404040 + 2 に、2バイトの 0x0040 を書き込む0x404040 + 0 に、1バイトの 0xd0 を書き込むThis suggests that the position 9 corresponds to the 24th byte (0-origin) of the input string.
Using this characteristics, I created data payload.bin to write in these way:
0x404040 + 40x404040 + 10x404040 + 20x404040 + 0
このデータを
I sent this data via "Send File" on
これは、strcmp が system に置き換わり、入力した文字列をコマンドとして実行しようとしたことを示している。
そして、実行に失敗し、非0が返るため、もう一度入力ができる。
そこで、/bin/sh を入力すると、シェルを起動できる。
起動したシェルで ls -al コマンドを実行すると、ファイル FLAG があることがわかった。
cat FLAG コマンドを実行すると、flagが得られた。
This is because strcmp is replaced with system and it is trying to execute the string entered as a command.
The execution fails and a non-zero value is returned, so I have another chance to enter a string.
I succeeded to launch the shell by entering /bin/sh.
On the shell, I executed a command ls -al and found a file FLAG.
I obtained the flag by executing a command cat FLAG.