misc/subtraction

448 points

Writeup by Aryan

This is the source code we are provided with.

import random

n = 696969

a = []
for i in range(n):
    a.append(random.randint(0, n))
    a[i] -= a[i] % 2

print(' '.join(list(map(str, a))))

for turns in range(20):
    c = int(input())
    for i in range(n):
        a[i] = abs(c - a[i])

    if len(set(a)) == 1:
        print(open('/flag.txt', 'r').read())
        break

If we keep subtracting by the mean of the dataset, the numbers will eventually all equal each other. The proof is left as an exercise to the reader.

import socket
import statistics

def main():
    host = 'challs.n00bzunit3d.xyz'
    port = 10029

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((host, port))
        data = b''
        while b'\n' not in data:
            data += s.recv(4096)
        data = data.decode().strip()
        numbers = list(map(int, data.split()))
        print(f"Received numbers: {len(numbers)}")

        for turn in range(20):
            print(f"Iteration {turn + 1}:")
            c = int(statistics.mean(numbers))
            s.sendall(f"{c}\n".encode())
            print(f"Sent value of c: {c}")
            numbers = list(set(abs(c - x) for x in numbers))
            print(f"Amount of numbers: {len(numbers)}")
            
            if len(set(numbers)) == 1:
                response = b''
                while b'\n' not in response:
                    response += s.recv(4096)
                response = response.decode().strip()
                print(response)

if __name__ == "__main__":
    main()

Flag: n00bz{1_sh0uld_t34ch_my_br0th3r_logs_e18be9bd2874}

Last updated