PK [ [ misc-mega-rust-1/.gitignore/target sonk.elf lib*.a **/history .cargo_home *~ *.dmp \#*\# .\#* cfg nvram target.docker PK 8٣ misc-mega-rust-1/Cargo.lock# This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "base64" version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "const-default" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b396d1f76d455557e1218ec8066ae14bba60b4b36ecd55577ba979f5db7ecaa" [[package]] name = "critical-section" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" [[package]] name = "embedded-alloc" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f2de9133f68db0d4627ad69db767726c99ff8585272716708227008d3f1bddd" dependencies = [ "const-default", "critical-section", "linked_list_allocator", "rlsf", ] [[package]] name = "game" version = "0.1.0" dependencies = [ "heapless", "megarust", "ufmt", ] [[package]] name = "hash32" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" dependencies = [ "byteorder", ] [[package]] name = "heapless" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" dependencies = [ "hash32", "stable_deref_trait", "ufmt-write", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "libc" version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" [[package]] name = "linked_list_allocator" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286" [[package]] name = "megarust" version = "0.1.0" dependencies = [ "critical-section", "embedded-alloc", "heapless", "itoa", "ufmt", ] [[package]] name = "proc-macro2" version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] [[package]] name = "rlsf" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "222fb240c3286247ecdee6fa5341e7cdad0ffdf8e7e401d9937f2d58482a20bf" dependencies = [ "cfg-if", "const-default", "libc", "svgbobdoc", ] [[package]] name = "rom" version = "0.1.0" dependencies = [ "game", ] [[package]] name = "stable_deref_trait" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "svgbobdoc" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2c04b93fc15d79b39c63218f15e3fdffaa4c227830686e3b7c5f41244eb3e50" dependencies = [ "base64", "proc-macro2", "quote", "syn", "unicode-width", ] [[package]] name = "syn" version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "ufmt" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a64846ec02b57e9108d6469d98d1648782ad6bb150a95a9baac26900bbeab9d" dependencies = [ "ufmt-macros", "ufmt-write", ] [[package]] name = "ufmt-macros" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d337d3be617449165cb4633c8dece429afd83f84051024079f97ad32a9663716" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "ufmt-write" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69" [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unicode-width" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" PK "Q} misc-mega-rust-1/Cargo.toml[workspace] members = [ "libs/megarust", "game", "rom" ] resolver = "2" [profile.release] lto = "fat" panic = "abort" [profile.dev] lto = "fat" panic = "abort" PK Yo o misc-mega-rust-1/MakefileGAME_SRC = $(shell find ./game/src/ -name "*.rs") LIB_SRC = $(shell find ./libs/ -name "*.rs") sonk.md: $(GAME_SRC) $(LIB_SRC) (docker images | grep mega) || docker build ./toolchain -t mega docker run --rm -t -v $(realpath ./):/src mega sh -c "cd /src/rom; ./.build.sh" podman: $(GAME_SRC) $(LIB_SRC) sudo podman run --rm -t -v $(realpath ./):/src mega sh -c "cd /src/rom; ./.build.sh" clean: (docker images | grep mega) || docker build ./toolchain -t mega docker run --rm -t -v $(realpath ./):/src mega sh -c "rm -rf /src/target.docker" rm -f sonk.md sonk.elf run: sonk.md mame genesis -cart $< .PHONY: clean PK LH H misc-mega-rust-1/README.md# Mega Sonk in MegaRust To run the game: ``` mame genesis -cart sonk.md ``` Record your solution and send it up to the server to get the flag! Make sure your recording is at most 5 minutes long. ``` mame genesis -cart sonk.md -record solution.inp python3 submit.py /home/user/.mame/inp/solution.inp mega.2025.ctfcompetition.com 1337 ``` To rebuild the game: ``` make sonk.md ``` On the first run this builds the compilation toolchain which could take up to half an hour. Rebuild and run the game: ``` make run ``` ## Credits Compilation toolchain based on * https://github.com/ricky26/rust-mega-drive * https://github.com/rust-lang/rustc_codegen_gcc Graphics credits: * https://opengameart.org/content/dashie-supertux-advance-style * https://opengameart.org/content/plastic-shamtastic * https://opengameart.org/content/wasp-0 PK N N misc-mega-rust-1/megadrive.xMEMORY { ROM (rx) : ORIGIN = 0, LENGTH = 0x400000 RAM (rwx) : ORIGIN = 0xFF0000, LENGTH = 0x10000 } ENTRY(_start) SECTIONS { _stack_top = 0x1000000; _stack_bottom = _stack_top - 0x500; .text : { *(.text .text.*); } > ROM .rodata : { . = ALIGN(4); *(.rodata .rodata.*); } > ROM .data : AT(ADDR(.rodata) + SIZEOF(.rodata)) { . = ALIGN(4); _data_start = .; *(.data); _data_end = .; } > RAM .bss (NOLOAD) : { . = ALIGN(4); _bss_start = .; *(.bss .bss.*); _bss_end = .; } > RAM _data_src = LOADADDR(.data); _heap_start = 0xFF0100; _heap_end = _stack_bottom; /* Flag ID stored at fixed offset for server-side verification */ GOT_FLAG = 0xFF0000; /*/DISCARD/ : */ } PK H )w w misc-mega-rust-1/pow.py# Taken from https://github.com/google/kctf/blob/v1/docker-images/challenge/pow.py import base64 import os import secrets import socket import sys import hashlib try: import gmpy2 HAVE_GMP = True except ImportError: HAVE_GMP = False sys.stderr.write("[NOTICE] Running 10x slower, gotta go fast? pip3 install gmpy2\n") VERSION = 's' MODULUS = 2**1279-1 CHALSIZE = 2**128 def python_sloth_root(x, diff, p): exponent = (p + 1) // 4 for i in range(diff): x = pow(x, exponent, p) ^ 1 return x def python_sloth_square(y, diff, p): for i in range(diff): y = pow(y ^ 1, 2, p) return y def gmpy_sloth_root(x, diff, p): exponent = (p + 1) // 4 for i in range(diff): x = gmpy2.powmod(x, exponent, p).bit_flip(0) return int(x) def gmpy_sloth_square(y, diff, p): y = gmpy2.mpz(y) for i in range(diff): y = gmpy2.powmod(y.bit_flip(0), 2, p) return int(y) def sloth_root(x, diff, p): if HAVE_GMP: return gmpy_sloth_root(x, diff, p) else: return python_sloth_root(x, diff, p) def sloth_square(x, diff, p): if HAVE_GMP: return gmpy_sloth_square(x, diff, p) else: return python_sloth_square(x, diff, p) def encode_number(num): size = (num.bit_length() // 24) * 3 + 3 return str(base64.b64encode(num.to_bytes(size, 'big')), 'utf-8') def decode_number(enc): return int.from_bytes(base64.b64decode(bytes(enc, 'utf-8')), 'big') def decode_challenge(enc): dec = enc.split('.') if dec[0] != VERSION: raise Exception('Unknown challenge version') return list(map(decode_number, dec[1:])) def encode_challenge(arr): return '.'.join([VERSION] + list(map(encode_number, arr))) def get_challenge(diff): x = secrets.randbelow(CHALSIZE) return encode_challenge([diff, x]) def solve_challenge(chal): [diff, x] = decode_challenge(chal) y = sloth_root(x, diff, MODULUS) return encode_challenge([y]) def verify_challenge(chal, sol): [diff, x] = decode_challenge(chal) [y] = decode_challenge(sol) res = sloth_square(y, diff, MODULUS) return (x == res) or (MODULUS - x == res) PK <@ @ misc-mega-rust-1/rustfmt.tomlgroup_imports = "StdExternalCrate" imports_granularity = "Item" PK GP misc-mega-rust-1/sonk.md " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " "SEGA MEGADRIVE (C)2021.JAN TESTPROJECT TESTPROJECT GM MK-0000 -01 J6 F N /Ns.| F% N ?N g` NsNr% Nu o J g"o / ЈذfNu o J g/ / Јfp Nu / o cJ g"@"/ ҈زfNuJ g"o S" SSgSSf`O o / "/ /o g*"h / /( "i /A /H NP" o J gO NuJg"h /o /A /h "i O NB O Nu o "h /P "i NY/ o "/ Ҁbtdp$XNu// // Hp/H N * o / ѐO B $XNuQ// / B ez" "A ? b06 C A 2| o " ҉etdFp$&PNu$ v B4 ? bR0 @ B A 2| `@ 2| `/ Ho o Hp/I N * / o ѐO B $&PNuB@H@H @ 0 ? @ B A 2| ` HH8 $o &/ * g@ / gD(/ f4Rt gR j / /* h NPJ gx pH@"LNuv t `x t `O"/