Rock Paper Scissors — Daily AlpacaHack

code/game.py

import random

FLAG = "Alpaca{REDACTED}"
HANDS = ["r", "p", "s"]
ROUNDS = 1000
TARGET_WIN = int(ROUNDS * 0.6)

def shuffle(items):
    return sorted(items, key=lambda _: random.getrandbits(1))

print("ROCK PAPER SCISSORS GAME")
print(f"Win {TARGET_WIN} times in {ROUNDS} rounds.")
print("Hands: r / p / s")

win = 0
hands = HANDS[:]
for i in range(ROUNDS):
    hands = shuffle(hands)
    opponent = hands[0]
    you = input(f"Round {i+1} > ").strip()
    assert you in HANDS, "Invalid hand."
    result = (HANDS.index(you) - HANDS.index(opponent)) % 3
    win += (result == 1)

    print(f"Opponent: {opponent}, You: {you}")
    print(["Draw", "Win!", "Lose..."][result])
    print(f"Win count: {win}\\n")

if win >= TARGET_WIN:
    print(f"Wow, you broke the game ... Flag: {FLAG}")
else:
    print("Leave the game.")

🤔

solve

from pwn import *

IP = '192.168.1.1' # 書いãĻね
PORT = 10000

WINNING_HAND = {
    'r': 'p',  # グãƒŧãĢはパãƒŧ
    'p': 's',  # パãƒŧãĢはチョキ
    's': 'r'   # チョキãĢはグãƒŧ
}
next_hand = 'p'

io = remote(IP, PORT)

for i in range(1000):
    io.recvuntil(f"Round {i+1} > ".encode())
    io.sendline(next_hand.encode())
    response = io.recvline().decode()
    opponent_hand = response.split("Opponent: ")[1][0]
    next_hand = WINNING_HAND[opponent_hand]
    print(i)

io.interactive()

äģĨ下を垗る

99
[*] Switching to interactive mode
Lose...
Win count: 606

Wow, you broke the game ... Flag: Alpaca{REDACTED}
[*] Got EOF while reading in interactive