this links to the homepage btw

2025-09-27

.
├── The Problem
│  └── Evolutionary Algorithm
└── Code

CTF Write Up: Evolution Edition

I am choosing to make/post this write-up first because I am particularly fond of the question. Not often do you see evolutionary biology appear in a cybersecurity competition but here it is.

The Problem

Here you must submit a json file containing a floating point array that represents 100 genomes. The server scores this genome and if you get the right one within some tolerance you get the flag. Our team was one of this first to get this problem done, it may not have been immediately obvious to others.

Evolutionary Algorithm

Nature's way of optimizing a space characterized by trillions of variables to an unknowable objective function. You should consider it impressive, you are a result of this method of optimization. I implemented it in the following way: select the top ten genomes, duplicate them 10 times with random variation and resubmit the new 100 genomes to the server.

Code

#!/bin/python
import json
import random
import subprocess
import numpy as np

def generate_samples(num_samples=100, array_length=10):
    data = {
        "samples": [
            [random.random() for _ in range(array_length)]
            for _ in range(num_samples)
        ]
    }
    return data

def get_genetic_fitness(strains):
    result = subprocess.run(
        ["nc","chal.sunshinectf.games","25201"], 
        input=strains.encode(),
        capture_output=True
    )
    out_text = result.stdout.decode()
    print(out_text)
    #out_text= string
    lines = out_text.splitlines()
    fitness = "\n".join(lines[7:])
    return json.loads(fitness)

def get_studs(fitness,samples):
    indices = np.argsort(fitness["scores"])[-10:]
    genomes = []
    for i in indices:
        genomes.append(samples["samples"][i])
    return genomes

def generate_new_samples(genomes, repetitions=10, mutation_rate=0.1):
    sample_array = []
    for genome in genomes:
        for _ in range(repetitions):
            sample_array.append(
	[i+random.uniform(-mutation_rate, mutation_rate) for i in genome])
    data = {
        "samples": sample_array
    }
    return data

if __name__ == "__main__":
    taumaeba_strains = generate_samples()
    while True:
        genetic_fitness = get_genetic_fitness(json.dumps(taumaeba_strains))
        elite_genomes = get_studs(genetic_fitness,taumaeba_strains)
        taumaeba_strains = generate_new_samples(elite_genomes)
    #json.dumps(samples)