{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# CTF Template\n\nWelcome to the DEFCON AI Village Capture-the-Flag (CTF). Feel free to copy this notebook and use it as the foundation for your submissions.\n\n## Intro\n\nHelp Henry Hacker get to Homecoming! In this series of challenges, you'll be interacting with various machine learning challenges. You're welcome to use this notebook as a template, but can also use any other resources. `Submission Helper` code is just there to show you how to interact with a challenge. You're encouraged to write your own code. There are no fake flags, so if you're returned a 256 character-long string, you found it! Collect those flags, add them next to their corresponding challenge in `solution.csv` (provided) and submit them to the scoreboard. We've provided an `add_flag()` function to help with this. There's no limit to how many times you try the challenges, but you can only submit `solution.csv` to the scoreboard 50 times per day. \n\nCTF's are inherently puzzles that are intended to challenge you and help you learn new things. Sometimes they may be a little ambiguous or misleading. That's part of the challenge! However, if you feel like a challenge or piece of infrastructure isn't working correctly, contact the organizers through the Kaggle discussion board or the discord channel.","metadata":{}},{"cell_type":"code","source":"# We're just importing some stuff to make sure the challenges and helper code works.\n# Feel free to import anything else you need (or solve in a different language), but if you overwrite these, provided code may not work.\n\nimport pandas as pd\nimport numpy as np\nimport os\nimport requests\nimport random\nfrom IPython.display import Image as Ima\nfrom IPython.display import clear_output, display\nfrom PIL import Image as Im\nfrom PIL import ImageFilter\nfrom torch.utils.data import Dataset, DataLoader\nimport torch\nimport joblib\nimport matplotlib.pyplot as plt\nimport json\nimport base64\nimport shutil\nimport warnings\nimport itertools\nfrom sklearn.decomposition import PCA\nfrom sklearn.cluster import KMeans\nwarnings.filterwarnings(\"ignore\")\nimport cv2\nimport pytesseract\n#import png\n\ndir_name = \"/kaggle/input/ai-village-ctf\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-13T21:52:33.025763Z","iopub.execute_input":"2022-09-13T21:52:33.026238Z","iopub.status.idle":"2022-09-13T21:52:33.035442Z","shell.execute_reply.started":"2022-09-13T21:52:33.026205Z","shell.execute_reply":"2022-09-13T21:52:33.034112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A helper function for tracking flags. You could do it yourself, but why?\n\ndef add_flag(challenge_id: str, flag: str) -> str:\n    try:\n        submission_path = \"/kaggle/working/submission.csv\"\n        template_path = f\"{dir_name}/submission.csv\"\n        if not os.path.isfile(submission_path) :\n            shutil.copyfile(template_path, submission_path)\n        flags = pd.read_csv(submission_path, index_col=\"challenge_id\")\n        valid = flags.index.to_list()\n        if challenge_id in valid:\n            flags.at[challenge_id] = flag\n            flags.to_csv(submission_path)\n            print(\"Flag saved. If you want to update the scoreboard, click 'submit' on the right. Remember that you can only update your score on the scoreboard 50 times per day\")\n        else:\n            print(\"Invalid challenge_id\")\n    except:\n        print(\"We had trouble saving that flag. Keep a copy of it and contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.038143Z","iopub.execute_input":"2022-09-13T21:52:33.038850Z","iopub.status.idle":"2022-09-13T21:52:33.049737Z","shell.execute_reply.started":"2022-09-13T21:52:33.038805Z","shell.execute_reply":"2022-09-13T21:52:33.048471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install tensorflow==1.13.1","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-13T21:52:33.051323Z","iopub.execute_input":"2022-09-13T21:52:33.051697Z","iopub.status.idle":"2022-09-13T21:52:33.066300Z","shell.execute_reply.started":"2022-09-13T21:52:33.051666Z","shell.execute_reply":"2022-09-13T21:52:33.064895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install Keras==2.2.4","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-13T21:52:33.068981Z","iopub.execute_input":"2022-09-13T21:52:33.069363Z","iopub.status.idle":"2022-09-13T21:52:33.074861Z","shell.execute_reply.started":"2022-09-13T21:52:33.069331Z","shell.execute_reply":"2022-09-13T21:52:33.074092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.mobilenet import preprocess_input\nfrom keras.models import load_model\nfrom keras.preprocessing.image import img_to_array, array_to_img\nfrom PIL import Image\nfrom imagehash import phash\nimport numpy as np\nfrom keras import backend as K\nimport joblib\nimport pickle\nimport tensorflow as tf\n\nIMAGE_DIMS = (224, 224)\nTREE_FROG_IDX = 31\nTREE_FROG_STR = \"tree_frog\"\n\n# I'm pretty sure I borrowed this function from somewhere, but cannot remember\n# the source to cite them properly.\ndef hash_hamming_distance(h1, h2):\n    s1 = str(h1)\n    s2 = str(h2)\n    return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(s1, s2)))\n\n\ndef is_similar_img(path1, path2):\n    image1 = Image.open(path1)\n    image2 = Image.open(path2)\n\n    dist = hash_hamming_distance(phash(image1), phash(image2))\n    return dist <= 1\n\n\ndef prepare_image(image, target=IMAGE_DIMS):\n    # if the image mode is not RGB, convert it\n    if image.mode != \"RGB\":\n        image = image.convert(\"RGB\")\n\n    # resize the input image and preprocess it\n    image = image.resize(target)\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)\n    image = preprocess_input(image)\n    # return the processed image\n    return image\n\n\ndef create_img(img_path, img_res_path, model_path, target_str, target_idx, des_conf=0.95):\n    original_image = Image.open(img_path).resize(IMAGE_DIMS)\n    original_image = prepare_image(original_image)\n    #new_image = Image.open(img_res_path).resize(IMAGE_DIMS)\n    #new_image = prepare_image(new_image)\n    with open('/kaggle/input/ai-village-ctf/salt/picklemodel' , 'rb') as f:\n        bin_data = f.read()\n        model = pickle.loads(bin_data)\n    #ch=str(graph_data.model_config,'utf-8')\n    #ch = ch.replace('true', 'True')\n    #ch = ch.replace('false', 'False')\n    #ch = ch.replace('null', 'None')\n    #di = eval(ch)\n    #model = tf.keras.models.model_from_config(\n    #di, custom_objects=None\n    #)\n\n    model_input_layer = model.layers[0].input\n    model_output_layer = model.layers[-1].output\n\n    max_change_above = original_image + 0.01\n    max_change_below = original_image - 0.01\n\n    # Create a copy of the input image to hack on\n    hacked_image = np.copy(original_image)\n\n    # How much to update the hacked image in each iteration\n    learning_rate = 0.01\n\n    # Define the cost function.\n    # Our 'cost' will be the likelihood out image is the target class according to the pre-trained model\n    #print(model_output_layer.loc[0])\n    cost_function = model_output_layer[0, TREE_FROG_IDX]\n    #print(cost_function)\n    # We'll ask Keras to calculate the gradient based on the input image and the currently predicted class\n    # In this case, referring to \"model_input_layer\" will give us back image we are hacking.\n    #with tf.GradientTape() as tape:\n    #gradient_function = tape.gradient(cost_function, model_input_layer)[0]\n    gradient_function = K.gradients(cost_function, model_input_layer)[0]\n\n    # Create a Keras function that we can call to calculate the current cost and gradient\n    grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])\n\n    cost = 0.0\n\n    # In a loop, keep adjusting the hacked image slightly so that it tricks the model more and more\n    # until it gets to at least 80% confidence\n    while cost < 0.99:\n        # Check how close the image is to our target class and grab the gradients we\n        # can use to push it one more step in that direction.\n        # Note: It's really important to pass in '0' for the Keras learning mode here!\n        # Keras layers behave differently in prediction vs. train modes!\n        cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])\n\n        # Move the hacked image one step further towards fooling the model\n        # print gradients\n        hacked_image += np.sign(gradients) * learning_rate\n\n        # Ensure that the image doesn't ever change too much to either look funny or to become an invalid image\n        hacked_image = np.clip(hacked_image, max_change_below, max_change_above)\n        hacked_image = np.clip(hacked_image, -1.0, 1.0)\n\n        print(\"Model's predicted likelihood that the image is a tree frog: {:.8}%\".format(cost * 100))\n\n    hacked_image = hacked_image.reshape((224,224,3))\n    img = array_to_img(hacked_image)\n    return img\n    #img.save('/kaggle/working/res_image.png')\n","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.076450Z","iopub.execute_input":"2022-09-13T21:52:33.076766Z","iopub.status.idle":"2022-09-13T21:52:33.101353Z","shell.execute_reply.started":"2022-09-13T21:52:33.076737Z","shell.execute_reply":"2022-09-13T21:52:33.100096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HOTDOG (id: hotdog) -- 200 Points\n\n### Scenario\n\nThis is Chester. He's heard of Hotdog/Not Hotdog, but really wants to be classified as a hot-dog (get it?).\n\n### Objective\n\nHelp Chester classify as a hotdog.","metadata":{}},{"cell_type":"code","source":"img = Im.open(f'{dir_name}/hotdog/chester.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.102947Z","iopub.execute_input":"2022-09-13T21:52:33.103719Z","iopub.status.idle":"2022-09-13T21:52:33.126504Z","shell.execute_reply.started":"2022-09-13T21:52:33.103685Z","shell.execute_reply":"2022-09-13T21:52:33.125182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"I scraped one of the included model and searched for something that could turn the dog into a hot dog","metadata":{}},{"cell_type":"code","source":"#TREE_FROG_IDX = 20\n#TREE_FROG_STR = \"loggerhead\"\n#img = create_img(f'{dir_name}/hotdog/chester.png', \"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"\", TREE_FROG_STR, TREE_FROG_IDX)\n#assert is_similar_img(\"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"/kaggle/input/ai-village-ctf/salt/turtle.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.128325Z","iopub.execute_input":"2022-09-13T21:52:33.129234Z","iopub.status.idle":"2022-09-13T21:52:33.133871Z","shell.execute_reply.started":"2022-09-13T21:52:33.129197Z","shell.execute_reply":"2022-09-13T21:52:33.132771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hacked_image = np.copy(img)\nhacked_image +=30","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.135880Z","iopub.execute_input":"2022-09-13T21:52:33.136590Z","iopub.status.idle":"2022-09-13T21:52:33.183986Z","shell.execute_reply.started":"2022-09-13T21:52:33.136552Z","shell.execute_reply":"2022-09-13T21:52:33.183092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Im.fromarray(hacked_image)\nim","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.188464Z","iopub.execute_input":"2022-09-13T21:52:33.189694Z","iopub.status.idle":"2022-09-13T21:52:33.779588Z","shell.execute_reply.started":"2022-09-13T21:52:33.189651Z","shell.execute_reply":"2022-09-13T21:52:33.778659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#result = cv2.addWeighted(img1, 0.7, img2, 0.3, 0.0);","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.780658Z","iopub.execute_input":"2022-09-13T21:52:33.781565Z","iopub.status.idle":"2022-09-13T21:52:33.785002Z","shell.execute_reply.started":"2022-09-13T21:52:33.781531Z","shell.execute_reply":"2022-09-13T21:52:33.784290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_DIMS = (224, 224)\nimg = Im.open(f\"{dir_name}/hotdog/chester.png\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.785958Z","iopub.execute_input":"2022-09-13T21:52:33.787112Z","iopub.status.idle":"2022-09-13T21:52:33.799760Z","shell.execute_reply.started":"2022-09-13T21:52:33.787076Z","shell.execute_reply":"2022-09-13T21:52:33.798911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.filter(ImageFilter.GaussianBlur(5))\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:33.800851Z","iopub.execute_input":"2022-09-13T21:52:33.801850Z","iopub.status.idle":"2022-09-13T21:52:34.466532Z","shell.execute_reply.started":"2022-09-13T21:52:33.801818Z","shell.execute_reply":"2022-09-13T21:52:34.465392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.size","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:34.467761Z","iopub.execute_input":"2022-09-13T21:52:34.468095Z","iopub.status.idle":"2022-09-13T21:52:34.473783Z","shell.execute_reply.started":"2022-09-13T21:52:34.468065Z","shell.execute_reply":"2022-09-13T21:52:34.473007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.crop((0, 0, 800, 600))\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:34.474791Z","iopub.execute_input":"2022-09-13T21:52:34.475628Z","iopub.status.idle":"2022-09-13T21:52:34.736897Z","shell.execute_reply.started":"2022-09-13T21:52:34.475595Z","shell.execute_reply":"2022-09-13T21:52:34.736072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Putting hotdog-like colors in image","metadata":{}},{"cell_type":"code","source":"orig_color = (195,195,195)\nwhite = (200, 200, 200)\nmid = (150, 150, 0)\nreplacement_color_brown = (92,49,17)\nreplacement_color_red = (255,0,0)\nreplacement_color_yellow = (255,255,0)\nimg = Im.open(f'{dir_name}/hotdog/chester.png').convert('RGB')\ndata = np.array(img)\ndata[(data <= orig_color).all(axis = -1)] = replacement_color_brown\ndata[(data > white).all(axis = -1)] = replacement_color_yellow\ndata[(data  > mid).all(axis = -1)] = replacement_color_red\nimg2 = Im.fromarray(data, mode='RGB')\nimg = img2\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:34.737961Z","iopub.execute_input":"2022-09-13T21:52:34.738835Z","iopub.status.idle":"2022-09-13T21:52:35.082178Z","shell.execute_reply.started":"2022-09-13T21:52:34.738802Z","shell.execute_reply":"2022-09-13T21:52:35.080975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.rotate(-45)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.083640Z","iopub.execute_input":"2022-09-13T21:52:35.084511Z","iopub.status.idle":"2022-09-13T21:52:35.208672Z","shell.execute_reply.started":"2022-09-13T21:52:35.084476Z","shell.execute_reply":"2022-09-13T21:52:35.207456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.effect_spread(2)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.210533Z","iopub.execute_input":"2022-09-13T21:52:35.210902Z","iopub.status.idle":"2022-09-13T21:52:35.385746Z","shell.execute_reply.started":"2022-09-13T21:52:35.210869Z","shell.execute_reply":"2022-09-13T21:52:35.384451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.convert(\"L\")\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.387379Z","iopub.execute_input":"2022-09-13T21:52:35.387748Z","iopub.status.idle":"2022-09-13T21:52:35.457121Z","shell.execute_reply.started":"2022-09-13T21:52:35.387705Z","shell.execute_reply":"2022-09-13T21:52:35.456010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.458691Z","iopub.execute_input":"2022-09-13T21:52:35.459145Z","iopub.status.idle":"2022-09-13T21:52:35.533838Z","shell.execute_reply.started":"2022-09-13T21:52:35.459103Z","shell.execute_reply":"2022-09-13T21:52:35.532429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img2","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.535759Z","iopub.execute_input":"2022-09-13T21:52:35.536183Z","iopub.status.idle":"2022-09-13T21:52:35.540965Z","shell.execute_reply.started":"2022-09-13T21:52:35.536147Z","shell.execute_reply":"2022-09-13T21:52:35.539856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport cv2\n \ndef add_noise(img):\n \n    # Getting the dimensions of the image\n    row , col = img.shape\n     \n    # Randomly pick some pixels in the\n    # image for coloring them white\n    # Pick a random number between 300 and 10000\n    number_of_pixels = random.randint(300, 10000)\n    for i in range(number_of_pixels):\n       \n        # Pick a random y coordinate\n        y_coord=random.randint(0, row - 1)\n         \n        # Pick a random x coordinate\n        x_coord=random.randint(0, col - 1)\n         \n        # Color that pixel to white\n        img[y_coord][x_coord] = 255\n         \n    # Randomly pick some pixels in\n    # the image for coloring them black\n    # Pick a random number between 300 and 10000\n    number_of_pixels = random.randint(300 , 10000)\n    for i in range(number_of_pixels):\n       \n        # Pick a random y coordinate\n        y_coord=random.randint(0, row - 1)\n         \n        # Pick a random x coordinate\n        x_coord=random.randint(0, col - 1)\n         \n        # Color that pixel to black\n        img[y_coord][x_coord] = 0\n         \n    return img","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.542428Z","iopub.execute_input":"2022-09-13T21:52:35.542735Z","iopub.status.idle":"2022-09-13T21:52:35.554236Z","shell.execute_reply.started":"2022-09-13T21:52:35.542707Z","shell.execute_reply":"2022-09-13T21:52:35.553392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def search_model(img_path, des_conf=0.95):\n    IMAGE_DIMS = (224, 224)\n    original_image = Image.open(img_path).resize(IMAGE_DIMS)\n    original_image = prepare_image(original_image)\n    with open('/kaggle/input/ai-village-ctf/salt/picklemodel' , 'rb') as f:\n        bin_data = f.read()\n        model = pickle.loads(bin_data)\n\n\n    model_input_layer = model.layers[0].input\n    model_output_layer = model.layers[-1].output\n\n    max_change_above = original_image + 0.01\n    max_change_below = original_image - 0.01\n\n    # Create a copy of the input image to hack on\n    \n\n    # How much to update the hacked image in each iteration\n    learning_rate = 0.01\n\n    # Define the cost function.\n    # Our 'cost' will be the likelihood out image is the target class according to the pre-trained model\n    #print(model_output_layer.loc[0])\n    salt_str = []\n    hotdog_str = []\n    idx = []\n    for i in range(1000):\n        print(i)\n        idx.append(i)\n        if i in [0, 50, 75, 87, 88, 89, 90, 91,92,93,94,95,96,97,98,99,100, 101, 110, 200, 201, 300, 350]:\n            continue\n        hacked_image = np.copy(original_image)\n        cost_function = model_output_layer[0, i]\n\n        gradient_function = K.gradients(cost_function, model_input_layer)[0]\n\n        # Create a Keras function that we can call to calculate the current cost and gradient\n        grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])\n\n        cost = 0.0\n\n        # In a loop, keep adjusting the hacked image slightly so that it tricks the model more and more\n        # until it gets to at least 80% confidence\n        while cost < des_conf:\n        # Check how close the image is to our target class and grab the gradients we\n        # can use to push it one more step in that direction.\n        # Note: It's really important to pass in '0' for the Keras learning mode here!\n        # Keras layers behave differently in prediction vs. train modes!\n            cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])\n\n        # Move the hacked image one step further towards fooling the model\n        # print gradients\n            hacked_image += np.sign(gradients) * learning_rate\n\n        # Ensure that the image doesn't ever change too much to either look funny or to become an invalid image\n            hacked_image = np.clip(hacked_image, max_change_below, max_change_above)\n            hacked_image = np.clip(hacked_image, -1.0, 1.0)\n\n            #print(\"Model's predicted likelihood that the image is a tree frog: {:.8}%\".format(cost * 100))\n\n        hacked_image = hacked_image.reshape((224,224,3))\n        img = array_to_img(hacked_image)\n        \n        \n        img = img.resize(IMAGE_DIMS)\n        img_bytes = img.tobytes()\n        b64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\n        #HOTDOG\n        try:\n            r = requests.post(\"https://hotdog.fly.dev/score\", data=json.dumps({\"input\": b64_img_bytes.decode()}))\n            hotdog_str.append(r.text)\n            #print(r.text)\n        except requests.exceptions.ConnectionError:\n            print(\"Connection problems. Contact the CTF organizers.\")\n        \n        #SALT\n        data = {\n            \"input\": b64_img_bytes.decode()\n            }\n\n        try:\n            r = requests.post(\"https://salt.fly.dev/score\", data=json.dumps(data))\n            #print(r.text)\n            salt_str.append(r.text)\n        except requests.exceptions.ConnectionError:\n            print(\"Connection problems. Contact the CTF organizers.\")\n        if i % 10 == 0:\n            print(hotdog_str)\n            print(salt_str)\n        \n    return hotdog_str, salt_str, idx","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.555202Z","iopub.execute_input":"2022-09-13T21:52:35.555519Z","iopub.status.idle":"2022-09-13T21:52:35.573954Z","shell.execute_reply.started":"2022-09-13T21:52:35.555488Z","shell.execute_reply":"2022-09-13T21:52:35.572131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hotdog, salt, idx = search_model(f'{dir_name}/hotdog/chester.png', des_conf=0.95)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.575811Z","iopub.execute_input":"2022-09-13T21:52:35.576289Z","iopub.status.idle":"2022-09-13T21:52:35.588382Z","shell.execute_reply.started":"2022-09-13T21:52:35.576255Z","shell.execute_reply":"2022-09-13T21:52:35.587460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TREE_FROG_IDX = 1\n#TREE_FROG_STR = \"loggerhead\"\n#img = create_img(f'{dir_name}/hotdog/chester.png', \"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"\", TREE_FROG_STR, TREE_FROG_IDX)\n#assert is_similar_img(\"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"/kaggle/input/ai-village-ctf/salt/turtle.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.595627Z","iopub.execute_input":"2022-09-13T21:52:35.596346Z","iopub.status.idle":"2022-09-13T21:52:35.604375Z","shell.execute_reply.started":"2022-09-13T21:52:35.596305Z","shell.execute_reply":"2022-09-13T21:52:35.603178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n\nIMAGE_DIMS = (224, 224)\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ntry:\n    r = requests.post(\"https://hotdog.fly.dev/score\", data=json.dumps({\"input\": b64_img_bytes.decode()}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.605310Z","iopub.execute_input":"2022-09-13T21:52:35.605606Z","iopub.status.idle":"2022-09-13T21:52:35.920979Z","shell.execute_reply.started":"2022-09-13T21:52:35.605579Z","shell.execute_reply":"2022-09-13T21:52:35.919901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ndata = {\n    \"input\": b64_img_bytes.decode()\n}\n\ntry:\n    r = requests.post(\"https://salt.fly.dev/score\", data=json.dumps(data))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:35.922981Z","iopub.execute_input":"2022-09-13T21:52:35.923437Z","iopub.status.idle":"2022-09-13T21:52:36.884578Z","shell.execute_reply.started":"2022-09-13T21:52:35.923406Z","shell.execute_reply":"2022-09-13T21:52:36.883472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"hotdog\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.886435Z","iopub.execute_input":"2022-09-13T21:52:36.886757Z","iopub.status.idle":"2022-09-13T21:52:36.896880Z","shell.execute_reply.started":"2022-09-13T21:52:36.886728Z","shell.execute_reply":"2022-09-13T21:52:36.895714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math\n\nThis series of \"Math\" challenges is designed to introduce the concepts of dimensionality.\n\nData is often represented in a high dimensional space, but is actually several intrinsically low dimensional components that can be diagnosed. Intrinsic low-dimensionailty is incredibly important to machine learning because many ML algorithms rely on it. Medical EKG data is as single intrinsically 2 dimensional component, despite often being represented as 100 dimensions or more. The classic MNIST dataset is several components of 8-10 dimensions, depending on the digits class, despite being represented in 784 dimensions. The 4 math challenges are focused on this problem. **If you need a hint, ask Sven at the Art Booth.**","metadata":{}},{"cell_type":"markdown","source":"# Math Challenge 1 (id: math_1) -- 100 Points\n\nHow many clusters are in the clusters1.npy, clusters2.npy, and clusters3.npy? The key is the number of clusters in order, with no spaces. These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"markdown","source":"## Using elbow plots for number of clusters","metadata":{}},{"cell_type":"code","source":"cluster1 = np.load('/kaggle/input/ai-village-ctf/math/clusters1.npy')\ncluster2 = np.load('/kaggle/input/ai-village-ctf/math/clusters2.npy')\ncluster3 = np.load('/kaggle/input/ai-village-ctf/math/clusters3.npy')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.898655Z","iopub.execute_input":"2022-09-13T21:52:36.898966Z","iopub.status.idle":"2022-09-13T21:52:36.912554Z","shell.execute_reply.started":"2022-09-13T21:52:36.898937Z","shell.execute_reply":"2022-09-13T21:52:36.911055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(cluster1.shape)\nprint(cluster2.shape)\nprint(cluster3.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.914379Z","iopub.execute_input":"2022-09-13T21:52:36.915394Z","iopub.status.idle":"2022-09-13T21:52:36.921567Z","shell.execute_reply.started":"2022-09-13T21:52:36.915358Z","shell.execute_reply":"2022-09-13T21:52:36.920418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster2","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.923272Z","iopub.execute_input":"2022-09-13T21:52:36.923720Z","iopub.status.idle":"2022-09-13T21:52:36.934704Z","shell.execute_reply.started":"2022-09-13T21:52:36.923678Z","shell.execute_reply":"2022-09-13T21:52:36.933687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = cluster3","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.935877Z","iopub.execute_input":"2022-09-13T21:52:36.937487Z","iopub.status.idle":"2022-09-13T21:52:36.944367Z","shell.execute_reply.started":"2022-09-13T21:52:36.937443Z","shell.execute_reply":"2022-09-13T21:52:36.943451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wcss=[]\nfor i in range(1,7):\n    kmeans = KMeans(i)\n    kmeans.fit(x)\n    wcss_iter = kmeans.inertia_\n    wcss.append(wcss_iter)\n\nnumber_clusters = range(1,7)\nplt.plot(number_clusters,wcss)\nplt.title('The Elbow title')\nplt.xlabel('Number of clusters')\nplt.ylabel('WCSS')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:36.945916Z","iopub.execute_input":"2022-09-13T21:52:36.946396Z","iopub.status.idle":"2022-09-13T21:52:39.142350Z","shell.execute_reply.started":"2022-09-13T21:52:36.946341Z","shell.execute_reply":"2022-09-13T21:52:39.141196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"math_1\", \"submission\": \"523\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.144010Z","iopub.execute_input":"2022-09-13T21:52:39.144478Z","iopub.status.idle":"2022-09-13T21:52:39.348898Z","shell.execute_reply.started":"2022-09-13T21:52:39.144434Z","shell.execute_reply":"2022-09-13T21:52:39.347439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_1\", \"LP82QOMX5NLMC9GQI899LKMBRJV{EDZWZHP519ISNMVIWGVMIG6L2D64CTLC0ARE}C40XOJJVIL5D832E04AHSFGN1F6PSG00MARRUWB8C8QHMZ4S6ZPAXIX10FMK0LS\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.350427Z","iopub.execute_input":"2022-09-13T21:52:39.350912Z","iopub.status.idle":"2022-09-13T21:52:39.360504Z","shell.execute_reply.started":"2022-09-13T21:52:39.350879Z","shell.execute_reply":"2022-09-13T21:52:39.359632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math Challenge 2 (id: math_2) -- 200 Points\n\nWhat's the dimensionality of the data in first_dim1.npy, first_dim2.npy, and first_dim3.npy? The key is the number of dimensions in order, with no spaces.  These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"markdown","source":"## Using PCA to determine dimensionality","metadata":{}},{"cell_type":"code","source":"dim1 = np.load('/kaggle/input/ai-village-ctf/math/first_dim1.npy')\ndim2 = np.load('/kaggle/input/ai-village-ctf/math/first_dim2.npy')\ndim3 = np.load('/kaggle/input/ai-village-ctf/math/first_dim3.npy')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.362009Z","iopub.execute_input":"2022-09-13T21:52:39.362395Z","iopub.status.idle":"2022-09-13T21:52:39.389746Z","shell.execute_reply.started":"2022-09-13T21:52:39.362361Z","shell.execute_reply":"2022-09-13T21:52:39.388702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dim1.shape)\nprint(dim2.shape)\nprint(dim3.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.391278Z","iopub.execute_input":"2022-09-13T21:52:39.392253Z","iopub.status.idle":"2022-09-13T21:52:39.398016Z","shell.execute_reply.started":"2022-09-13T21:52:39.392217Z","shell.execute_reply":"2022-09-13T21:52:39.397120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim1","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.399703Z","iopub.execute_input":"2022-09-13T21:52:39.400551Z","iopub.status.idle":"2022-09-13T21:52:39.417308Z","shell.execute_reply.started":"2022-09-13T21:52:39.400512Z","shell.execute_reply":"2022-09-13T21:52:39.416262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dim1\npca = PCA(n_components=3)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.421008Z","iopub.execute_input":"2022-09-13T21:52:39.421357Z","iopub.status.idle":"2022-09-13T21:52:39.431557Z","shell.execute_reply.started":"2022-09-13T21:52:39.421328Z","shell.execute_reply":"2022-09-13T21:52:39.430667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dim2\npca = PCA(n_components=5)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.432922Z","iopub.execute_input":"2022-09-13T21:52:39.433434Z","iopub.status.idle":"2022-09-13T21:52:39.444405Z","shell.execute_reply.started":"2022-09-13T21:52:39.433395Z","shell.execute_reply":"2022-09-13T21:52:39.443469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = dim3\npca = PCA(n_components=4)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.445674Z","iopub.execute_input":"2022-09-13T21:52:39.446216Z","iopub.status.idle":"2022-09-13T21:52:39.462824Z","shell.execute_reply.started":"2022-09-13T21:52:39.446182Z","shell.execute_reply":"2022-09-13T21:52:39.461892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"math_2\", \"submission\": \"354\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.464392Z","iopub.execute_input":"2022-09-13T21:52:39.464924Z","iopub.status.idle":"2022-09-13T21:52:39.667742Z","shell.execute_reply.started":"2022-09-13T21:52:39.464893Z","shell.execute_reply":"2022-09-13T21:52:39.666245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_2\", \"79B268GGIAPGS525Z5BYA3CWQL}ESI7GX583UH48WHTC1P72YDZ9EOH9AHHMQ8B8A6A9GBO4OEFCP5DG1QIV1HEAVYXHV87NA4TTIIMMU4R12ZM9L{JDKXCKA5UHOSB4\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.669641Z","iopub.execute_input":"2022-09-13T21:52:39.670083Z","iopub.status.idle":"2022-09-13T21:52:39.679244Z","shell.execute_reply.started":"2022-09-13T21:52:39.670036Z","shell.execute_reply":"2022-09-13T21:52:39.678412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math Challenge 3 (id: math_3) -- 300 Points\n\nWhat's the dimensionality of the data in second_dim1.npy, second_dim2.npy, and second_dim3.npy? The key is the number of the dimensionality in order, with no spaces.  These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"code","source":"sdim1 = np.load('/kaggle/input/ai-village-ctf/math/second_dim1.npy')\nsdim2 = np.load('/kaggle/input/ai-village-ctf/math/second_dim2.npy')\nsdim3 = np.load('/kaggle/input/ai-village-ctf/math/second_dim3.npy')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.680583Z","iopub.execute_input":"2022-09-13T21:52:39.681189Z","iopub.status.idle":"2022-09-13T21:52:39.697437Z","shell.execute_reply.started":"2022-09-13T21:52:39.681155Z","shell.execute_reply":"2022-09-13T21:52:39.695971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = sdim1\npca = PCA(n_components=4)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.699332Z","iopub.execute_input":"2022-09-13T21:52:39.699671Z","iopub.status.idle":"2022-09-13T21:52:39.707317Z","shell.execute_reply.started":"2022-09-13T21:52:39.699640Z","shell.execute_reply":"2022-09-13T21:52:39.706398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = sdim2\npca = PCA(n_components=7)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.708418Z","iopub.execute_input":"2022-09-13T21:52:39.709303Z","iopub.status.idle":"2022-09-13T21:52:39.721734Z","shell.execute_reply.started":"2022-09-13T21:52:39.709252Z","shell.execute_reply":"2022-09-13T21:52:39.720360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = sdim3\npca = PCA(n_components=4)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.723330Z","iopub.execute_input":"2022-09-13T21:52:39.724129Z","iopub.status.idle":"2022-09-13T21:52:39.737253Z","shell.execute_reply.started":"2022-09-13T21:52:39.724084Z","shell.execute_reply":"2022-09-13T21:52:39.736016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"math_3\", \"submission\": \"474\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.738762Z","iopub.execute_input":"2022-09-13T21:52:39.739871Z","iopub.status.idle":"2022-09-13T21:52:39.944218Z","shell.execute_reply.started":"2022-09-13T21:52:39.739827Z","shell.execute_reply":"2022-09-13T21:52:39.942989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_3\", \"S9YGL8FEQH9QXXPQ3HWGJU5OU}{FMMWUJOMRT7RFQ9CK5UDEG1BI6J8YSR2K0PMKZXUBXTEDZ5N_0U68726CACLIQUF73ZEAL7Z0K45IUBAD2GZN8PT04TWMYZTMXX8M\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.945659Z","iopub.execute_input":"2022-09-13T21:52:39.946841Z","iopub.status.idle":"2022-09-13T21:52:39.957946Z","shell.execute_reply.started":"2022-09-13T21:52:39.946793Z","shell.execute_reply":"2022-09-13T21:52:39.956736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math Challenge 4 (id: math_4) -- 400 Points\n\nWhat's the dimensionality of the clusters in clusters1.npy? The key is the dimensions ordered by cluster size (smallest to largest), with no spaces.  These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"code","source":"cluster1 = np.load('/kaggle/input/ai-village-ctf/math/clusters1.npy')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.959649Z","iopub.execute_input":"2022-09-13T21:52:39.961087Z","iopub.status.idle":"2022-09-13T21:52:39.968593Z","shell.execute_reply.started":"2022-09-13T21:52:39.961014Z","shell.execute_reply":"2022-09-13T21:52:39.966946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeans = KMeans(5)\nkmeans.fit(cluster1)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:39.971802Z","iopub.execute_input":"2022-09-13T21:52:39.972197Z","iopub.status.idle":"2022-09-13T21:52:40.378160Z","shell.execute_reply.started":"2022-09-13T21:52:39.972163Z","shell.execute_reply":"2022-09-13T21:52:40.377338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = kmeans.labels_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.379681Z","iopub.execute_input":"2022-09-13T21:52:40.379993Z","iopub.status.idle":"2022-09-13T21:52:40.384878Z","shell.execute_reply.started":"2022-09-13T21:52:40.379965Z","shell.execute_reply":"2022-09-13T21:52:40.383906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.386007Z","iopub.execute_input":"2022-09-13T21:52:40.386364Z","iopub.status.idle":"2022-09-13T21:52:40.398261Z","shell.execute_reply.started":"2022-09-13T21:52:40.386332Z","shell.execute_reply":"2022-09-13T21:52:40.397526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = np.bincount(labels)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.399183Z","iopub.execute_input":"2022-09-13T21:52:40.399570Z","iopub.status.idle":"2022-09-13T21:52:40.410894Z","shell.execute_reply.started":"2022-09-13T21:52:40.399539Z","shell.execute_reply":"2022-09-13T21:52:40.409386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.412292Z","iopub.execute_input":"2022-09-13T21:52:40.412753Z","iopub.status.idle":"2022-09-13T21:52:40.426238Z","shell.execute_reply.started":"2022-09-13T21:52:40.412706Z","shell.execute_reply":"2022-09-13T21:52:40.424986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique, counts = np.unique(labels, return_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.427982Z","iopub.execute_input":"2022-09-13T21:52:40.428339Z","iopub.status.idle":"2022-09-13T21:52:40.437216Z","shell.execute_reply.started":"2022-09-13T21:52:40.428308Z","shell.execute_reply":"2022-09-13T21:52:40.436217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique[np.argsort(-counts)]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.438116Z","iopub.execute_input":"2022-09-13T21:52:40.438415Z","iopub.status.idle":"2022-09-13T21:52:40.452327Z","shell.execute_reply.started":"2022-09-13T21:52:40.438388Z","shell.execute_reply":"2022-09-13T21:52:40.451049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.453925Z","iopub.execute_input":"2022-09-13T21:52:40.454307Z","iopub.status.idle":"2022-09-13T21:52:40.464244Z","shell.execute_reply.started":"2022-09-13T21:52:40.454275Z","shell.execute_reply":"2022-09-13T21:52:40.462889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(unique)\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.465703Z","iopub.execute_input":"2022-09-13T21:52:40.466467Z","iopub.status.idle":"2022-09-13T21:52:40.476488Z","shell.execute_reply.started":"2022-09-13T21:52:40.466434Z","shell.execute_reply":"2022-09-13T21:52:40.475125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[1, 5, 4, 2, 3]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.478154Z","iopub.execute_input":"2022-09-13T21:52:40.478963Z","iopub.status.idle":"2022-09-13T21:52:40.488561Z","shell.execute_reply.started":"2022-09-13T21:52:40.478928Z","shell.execute_reply":"2022-09-13T21:52:40.487549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1 = []","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.490054Z","iopub.execute_input":"2022-09-13T21:52:40.490623Z","iopub.status.idle":"2022-09-13T21:52:40.500679Z","shell.execute_reply.started":"2022-09-13T21:52:40.490579Z","shell.execute_reply":"2022-09-13T21:52:40.499710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1.append([0,1,2])","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.502166Z","iopub.execute_input":"2022-09-13T21:52:40.502738Z","iopub.status.idle":"2022-09-13T21:52:40.514770Z","shell.execute_reply.started":"2022-09-13T21:52:40.502703Z","shell.execute_reply":"2022-09-13T21:52:40.513648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.516329Z","iopub.execute_input":"2022-09-13T21:52:40.516951Z","iopub.status.idle":"2022-09-13T21:52:40.531410Z","shell.execute_reply.started":"2022-09-13T21:52:40.516915Z","shell.execute_reply":"2022-09-13T21:52:40.530196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1 = []\nc2 = []\nc3 = []\nc4 = []\nc5 = []\n\nfor index in range(len(labels)):\n    if labels[index] == 0:\n        c1.append(cluster1[index])\n    elif labels[index] == 1:\n        c2.append(cluster1[index])\n    elif labels[index] == 2:\n        c3.append(cluster1[index])\n    elif labels[index] == 3:\n        c4.append(cluster1[index])\n    elif labels[index] == 4:\n        c5.append(cluster1[index])\n        ","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.533446Z","iopub.execute_input":"2022-09-13T21:52:40.533794Z","iopub.status.idle":"2022-09-13T21:52:40.544749Z","shell.execute_reply.started":"2022-09-13T21:52:40.533762Z","shell.execute_reply":"2022-09-13T21:52:40.543452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c1 = np.array(c1)\nc2 = np.array(c2)\nc3 = np.array(c3)\nc4 = np.array(c4)\nc5 = np.array(c5)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.548433Z","iopub.execute_input":"2022-09-13T21:52:40.549458Z","iopub.status.idle":"2022-09-13T21:52:40.556118Z","shell.execute_reply.started":"2022-09-13T21:52:40.549411Z","shell.execute_reply":"2022-09-13T21:52:40.555139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = c1\npca = PCA(n_components=2)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.557966Z","iopub.execute_input":"2022-09-13T21:52:40.558825Z","iopub.status.idle":"2022-09-13T21:52:40.572055Z","shell.execute_reply.started":"2022-09-13T21:52:40.558778Z","shell.execute_reply":"2022-09-13T21:52:40.570480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = c2\npca = PCA(n_components=2)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.574745Z","iopub.execute_input":"2022-09-13T21:52:40.575874Z","iopub.status.idle":"2022-09-13T21:52:40.586174Z","shell.execute_reply.started":"2022-09-13T21:52:40.575819Z","shell.execute_reply":"2022-09-13T21:52:40.585128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = c3\npca = PCA(n_components=4)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.588061Z","iopub.execute_input":"2022-09-13T21:52:40.588904Z","iopub.status.idle":"2022-09-13T21:52:40.600129Z","shell.execute_reply.started":"2022-09-13T21:52:40.588857Z","shell.execute_reply":"2022-09-13T21:52:40.598751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = c4\npca = PCA(n_components=5)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.602132Z","iopub.execute_input":"2022-09-13T21:52:40.602565Z","iopub.status.idle":"2022-09-13T21:52:40.616408Z","shell.execute_reply.started":"2022-09-13T21:52:40.602531Z","shell.execute_reply":"2022-09-13T21:52:40.614836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = c5\npca = PCA(n_components=3)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.636055Z","iopub.execute_input":"2022-09-13T21:52:40.636479Z","iopub.status.idle":"2022-09-13T21:52:40.649410Z","shell.execute_reply.started":"2022-09-13T21:52:40.636442Z","shell.execute_reply":"2022-09-13T21:52:40.647946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[1, 5, 4, 2, 3]\n[1, 3, 5, 2, 4]\n\n\n[2,1,5,3,4]\n[2, 1, 3, 4, 5]\n\n\n[43512]\n#this one finally made sense after thinking about it for a while\n[54312]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.651204Z","iopub.execute_input":"2022-09-13T21:52:40.651569Z","iopub.status.idle":"2022-09-13T21:52:40.666583Z","shell.execute_reply.started":"2022-09-13T21:52:40.651537Z","shell.execute_reply":"2022-09-13T21:52:40.665127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"math_4\", \"submission\": \"54312\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.668390Z","iopub.execute_input":"2022-09-13T21:52:40.668714Z","iopub.status.idle":"2022-09-13T21:52:40.875317Z","shell.execute_reply.started":"2022-09-13T21:52:40.668686Z","shell.execute_reply":"2022-09-13T21:52:40.874341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_4\", \"BKTKKLGON4CIEELZWGY8CF6WQDIWL4WDPIO}N22MED3INUC2PCCT4FIO4GAZJBOG33I223R6BDBDFH2AN68DFWATORIZQ6B7A4O{M6XOQ0SCL3KNK40HJ0DWA1SRFWJL\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.876820Z","iopub.execute_input":"2022-09-13T21:52:40.877502Z","iopub.status.idle":"2022-09-13T21:52:40.887361Z","shell.execute_reply.started":"2022-09-13T21:52:40.877465Z","shell.execute_reply":"2022-09-13T21:52:40.885985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WIFI (id: wifi) -- 200 Points\n\n### Scenario\n\nYou really need to check your email, unfortunately you don't know the password.  Fortunately, someone wrote it down.  Unfortunately, it's written down on a low-dimensional manifold embedded in a very high-dimensional space.  Check out the `wifi/Embedded characters.npz` file -- a list of tokens is given in the `tokens` key with their corresponding embeddings in the same order under the `embeddings` key -- and recover the password.\n\n### Objective\n\nThe password is your flag, like all others, it has the format `FLAG{something}`","metadata":{}},{"cell_type":"markdown","source":"## This was probably my favorite problem/solution, very fun to solve. PCA with a bit of math.","metadata":{}},{"cell_type":"code","source":"b = np.load('/kaggle/input/ai-village-ctf/wifi/Embedded_characters.npz')\ntokens = b['tokens']\nembeddings = b['embeddings']","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.889486Z","iopub.execute_input":"2022-09-13T21:52:40.889845Z","iopub.status.idle":"2022-09-13T21:52:40.901177Z","shell.execute_reply.started":"2022-09-13T21:52:40.889813Z","shell.execute_reply":"2022-09-13T21:52:40.900101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokens","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.902611Z","iopub.execute_input":"2022-09-13T21:52:40.902998Z","iopub.status.idle":"2022-09-13T21:52:40.910561Z","shell.execute_reply.started":"2022-09-13T21:52:40.902954Z","shell.execute_reply":"2022-09-13T21:52:40.909416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tok_arr = list(str(tokens))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.911973Z","iopub.execute_input":"2022-09-13T21:52:40.912349Z","iopub.status.idle":"2022-09-13T21:52:40.920040Z","shell.execute_reply.started":"2022-09-13T21:52:40.912318Z","shell.execute_reply":"2022-09-13T21:52:40.919153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.921389Z","iopub.execute_input":"2022-09-13T21:52:40.922569Z","iopub.status.idle":"2022-09-13T21:52:40.937511Z","shell.execute_reply.started":"2022-09-13T21:52:40.922534Z","shell.execute_reply":"2022-09-13T21:52:40.935987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = embeddings\npca = PCA(n_components=2)\nprincipalComponents = pca.fit_transform(x)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:40.939231Z","iopub.execute_input":"2022-09-13T21:52:40.939979Z","iopub.status.idle":"2022-09-13T21:52:41.055007Z","shell.execute_reply.started":"2022-09-13T21:52:40.939937Z","shell.execute_reply":"2022-09-13T21:52:41.053923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"principal_Df = pd.DataFrame(data = principalComponents\n             , columns = ['c1', 'c2'])","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.056446Z","iopub.execute_input":"2022-09-13T21:52:41.057071Z","iopub.status.idle":"2022-09-13T21:52:41.090576Z","shell.execute_reply.started":"2022-09-13T21:52:41.057009Z","shell.execute_reply":"2022-09-13T21:52:41.086422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.figure(figsize=(10,10))\nplt.xticks(fontsize=12)\nplt.yticks(fontsize=14)\nplt.xlabel('Principal Component - 1',fontsize=20)\nplt.ylabel('Principal Component - 2',fontsize=20)\nplt.title(\"Principal Component Analysis of Embeddings\",fontsize=20)\nplt.scatter(principal_Df['c1']\n               , principal_Df['c2'], s = 50)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.094460Z","iopub.execute_input":"2022-09-13T21:52:41.094828Z","iopub.status.idle":"2022-09-13T21:52:41.415427Z","shell.execute_reply.started":"2022-09-13T21:52:41.094795Z","shell.execute_reply":"2022-09-13T21:52:41.414444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"principal_Df['d'] = np.sqrt(principal_Df['c1']**2 + principal_Df['c2']**2)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.416789Z","iopub.execute_input":"2022-09-13T21:52:41.418137Z","iopub.status.idle":"2022-09-13T21:52:41.426857Z","shell.execute_reply.started":"2022-09-13T21:52:41.418086Z","shell.execute_reply":"2022-09-13T21:52:41.425293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"principal_Df","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.428680Z","iopub.execute_input":"2022-09-13T21:52:41.429152Z","iopub.status.idle":"2022-09-13T21:52:41.453340Z","shell.execute_reply.started":"2022-09-13T21:52:41.429105Z","shell.execute_reply":"2022-09-13T21:52:41.452128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = list(principal_Df.sort_values(by=['d'], ascending=False).index)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.455155Z","iopub.execute_input":"2022-09-13T21:52:41.455481Z","iopub.status.idle":"2022-09-13T21:52:41.481565Z","shell.execute_reply.started":"2022-09-13T21:52:41.455451Z","shell.execute_reply":"2022-09-13T21:52:41.480125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values[:5]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.483254Z","iopub.execute_input":"2022-09-13T21:52:41.488313Z","iopub.status.idle":"2022-09-13T21:52:41.495044Z","shell.execute_reply.started":"2022-09-13T21:52:41.488265Z","shell.execute_reply":"2022-09-13T21:52:41.494094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"password = []\nfor index in values:\n    password.append(tok_arr[index])","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.496387Z","iopub.execute_input":"2022-09-13T21:52:41.496721Z","iopub.status.idle":"2022-09-13T21:52:41.504964Z","shell.execute_reply.started":"2022-09-13T21:52:41.496691Z","shell.execute_reply":"2022-09-13T21:52:41.504098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"password[:10]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.506377Z","iopub.execute_input":"2022-09-13T21:52:41.506855Z","iopub.status.idle":"2022-09-13T21:52:41.520265Z","shell.execute_reply.started":"2022-09-13T21:52:41.506825Z","shell.execute_reply":"2022-09-13T21:52:41.518858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"wifi\", \"submission\": \"FLAG{TURNED}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.522657Z","iopub.execute_input":"2022-09-13T21:52:41.523403Z","iopub.status.idle":"2022-09-13T21:52:41.726284Z","shell.execute_reply.started":"2022-09-13T21:52:41.523359Z","shell.execute_reply":"2022-09-13T21:52:41.725107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"wifi\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.727965Z","iopub.execute_input":"2022-09-13T21:52:41.728673Z","iopub.status.idle":"2022-09-13T21:52:41.739330Z","shell.execute_reply.started":"2022-09-13T21:52:41.728627Z","shell.execute_reply":"2022-09-13T21:52:41.738148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Honorstudent (id: honorstudent) -- 300 Points\n\n### Scenario\n\nLook at your report card! That's an F. You won't be able to go to homecoming with that grade. Can you use some carefully placed whiteout to fool your parents into thinking it's an A?\n\n### Objective\n\nModify and submit your tampered `f.png`. Be careful to not overdo it!\n","metadata":{}},{"cell_type":"markdown","source":"## Need to make the 'f' look like an 'A'","metadata":{}},{"cell_type":"code","source":"Ima(filename=f'{dir_name}/honorstudent/f.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.741547Z","iopub.execute_input":"2022-09-13T21:52:41.742196Z","iopub.status.idle":"2022-09-13T21:52:41.751422Z","shell.execute_reply.started":"2022-09-13T21:52:41.742150Z","shell.execute_reply":"2022-09-13T21:52:41.750309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open(f\"{dir_name}/honorstudent/f.png\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.752966Z","iopub.execute_input":"2022-09-13T21:52:41.753869Z","iopub.status.idle":"2022-09-13T21:52:41.766800Z","shell.execute_reply.started":"2022-09-13T21:52:41.753826Z","shell.execute_reply":"2022-09-13T21:52:41.759372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixels = np.array(img.getdata())\npixels = np.reshape(pixels, (28, 28))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.768487Z","iopub.execute_input":"2022-09-13T21:52:41.769422Z","iopub.status.idle":"2022-09-13T21:52:41.778225Z","shell.execute_reply.started":"2022-09-13T21:52:41.769373Z","shell.execute_reply":"2022-09-13T21:52:41.776822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(img.size[0]): # for every pixel:\n    for j in range(img.size[1]):\n        if i > 13 and i < 16:\n            if j > 13:\n                pixels[i,j] = 255\n        if i > 6 and i < 25:\n            if j> 19 and j < 23:\n                if pixels[i,j] == 0:\n                # change to black if not red\n                    pixels[i,j] = np.random.randint(200, 255)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.779628Z","iopub.execute_input":"2022-09-13T21:52:41.780660Z","iopub.status.idle":"2022-09-13T21:52:41.791580Z","shell.execute_reply.started":"2022-09-13T21:52:41.780607Z","shell.execute_reply":"2022-09-13T21:52:41.790563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2 = Im.fromarray(pixels.astype(np.uint8))\nimg2","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.793246Z","iopub.execute_input":"2022-09-13T21:52:41.793959Z","iopub.status.idle":"2022-09-13T21:52:41.813708Z","shell.execute_reply.started":"2022-09-13T21:52:41.793910Z","shell.execute_reply":"2022-09-13T21:52:41.812355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2.save(\"/kaggle/working/aplus.png\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.815489Z","iopub.execute_input":"2022-09-13T21:52:41.816176Z","iopub.status.idle":"2022-09-13T21:52:41.823442Z","shell.execute_reply.started":"2022-09-13T21:52:41.816134Z","shell.execute_reply":"2022-09-13T21:52:41.822339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nwith open(\"/kaggle/working/aplus.png\", \"rb\") as f:\n    try:\n        r = requests.post(\"https://honorstudent.fly.dev/score\", files={\"data_file\": f})\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:41.824988Z","iopub.execute_input":"2022-09-13T21:52:41.825639Z","iopub.status.idle":"2022-09-13T21:52:42.016445Z","shell.execute_reply.started":"2022-09-13T21:52:41.825598Z","shell.execute_reply":"2022-09-13T21:52:42.015304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"honorstudent\", \"6GSRY6MP499PSAXAPUQ0F69NA2F9TGH2XE42XPPL1BI2}1BKZI8O8O32AQOPNGA6TVSLC7QSCC4EW0SFYZK9T53X7Q{338EYHC1LCPDYFS1DT1YSBNIMIPM0GXFVMXL9\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.018819Z","iopub.execute_input":"2022-09-13T21:52:42.019549Z","iopub.status.idle":"2022-09-13T21:52:42.030206Z","shell.execute_reply.started":"2022-09-13T21:52:42.019505Z","shell.execute_reply":"2022-09-13T21:52:42.029109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Secret Sloth (id: sloth) -- 400 Points\n\n### Scenario\n\nThe sloth in `secret.sloth/secret-sloth.png` has a very cool hat... and a secret message for you. The message is your flag.\n\n### Objective \n\nThe flag is in the image somewhere; as always, look for `FLAG{something}`","metadata":{}},{"cell_type":"markdown","source":"## I didn't solve this one but here is what I tried","metadata":{}},{"cell_type":"code","source":"Ima(filename=f'{dir_name}/secret.sloth/secret-sloth.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.031844Z","iopub.execute_input":"2022-09-13T21:52:42.032275Z","iopub.status.idle":"2022-09-13T21:52:42.055311Z","shell.execute_reply.started":"2022-09-13T21:52:42.032236Z","shell.execute_reply":"2022-09-13T21:52:42.053960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open(f\"{dir_name}/secret.sloth/secret-sloth.png\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.056797Z","iopub.execute_input":"2022-09-13T21:52:42.058043Z","iopub.status.idle":"2022-09-13T21:52:42.064279Z","shell.execute_reply.started":"2022-09-13T21:52:42.057982Z","shell.execute_reply":"2022-09-13T21:52:42.063181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Attributes","metadata":{}},{"cell_type":"code","source":"img.filename","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.065964Z","iopub.execute_input":"2022-09-13T21:52:42.066637Z","iopub.status.idle":"2022-09-13T21:52:42.079041Z","shell.execute_reply.started":"2022-09-13T21:52:42.066596Z","shell.execute_reply":"2022-09-13T21:52:42.077723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.format","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.080959Z","iopub.execute_input":"2022-09-13T21:52:42.081542Z","iopub.status.idle":"2022-09-13T21:52:42.090243Z","shell.execute_reply.started":"2022-09-13T21:52:42.081501Z","shell.execute_reply":"2022-09-13T21:52:42.088981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.mode","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.092480Z","iopub.execute_input":"2022-09-13T21:52:42.092908Z","iopub.status.idle":"2022-09-13T21:52:42.103409Z","shell.execute_reply.started":"2022-09-13T21:52:42.092871Z","shell.execute_reply":"2022-09-13T21:52:42.101741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.info","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.105428Z","iopub.execute_input":"2022-09-13T21:52:42.105852Z","iopub.status.idle":"2022-09-13T21:52:42.115557Z","shell.execute_reply.started":"2022-09-13T21:52:42.105814Z","shell.execute_reply":"2022-09-13T21:52:42.114230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.size","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.117719Z","iopub.execute_input":"2022-09-13T21:52:42.118240Z","iopub.status.idle":"2022-09-13T21:52:42.129666Z","shell.execute_reply.started":"2022-09-13T21:52:42.118200Z","shell.execute_reply":"2022-09-13T21:52:42.128331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text = pytesseract.image_to_data(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.131217Z","iopub.execute_input":"2022-09-13T21:52:42.131646Z","iopub.status.idle":"2022-09-13T21:52:42.546216Z","shell.execute_reply.started":"2022-09-13T21:52:42.131607Z","shell.execute_reply":"2022-09-13T21:52:42.544478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.548756Z","iopub.execute_input":"2022-09-13T21:52:42.549166Z","iopub.status.idle":"2022-09-13T21:52:42.558899Z","shell.execute_reply.started":"2022-09-13T21:52:42.549129Z","shell.execute_reply":"2022-09-13T21:52:42.557647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_DIMS = (224, 224)\nimg = Im.open(f\"{dir_name}/secret.sloth/secret-sloth.png\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.560672Z","iopub.execute_input":"2022-09-13T21:52:42.561102Z","iopub.status.idle":"2022-09-13T21:52:42.568744Z","shell.execute_reply.started":"2022-09-13T21:52:42.561061Z","shell.execute_reply":"2022-09-13T21:52:42.567591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(img.getdata())","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.570580Z","iopub.execute_input":"2022-09-13T21:52:42.571529Z","iopub.status.idle":"2022-09-13T21:52:42.757848Z","shell.execute_reply.started":"2022-09-13T21:52:42.571495Z","shell.execute_reply":"2022-09-13T21:52:42.756947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.size","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.759228Z","iopub.execute_input":"2022-09-13T21:52:42.759799Z","iopub.status.idle":"2022-09-13T21:52:42.766622Z","shell.execute_reply.started":"2022-09-13T21:52:42.759767Z","shell.execute_reply":"2022-09-13T21:52:42.765486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.768250Z","iopub.execute_input":"2022-09-13T21:52:42.768629Z","iopub.status.idle":"2022-09-13T21:52:42.779823Z","shell.execute_reply.started":"2022-09-13T21:52:42.768599Z","shell.execute_reply":"2022-09-13T21:52:42.779083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pixels_from_image():\n    img = Im.open(f\"{dir_name}/secret.sloth/secret-sloth.png\")\n    shape = img.size\n    pixels = np.array(img.getdata())\n    return pixels.astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.781339Z","iopub.execute_input":"2022-09-13T21:52:42.781644Z","iopub.status.idle":"2022-09-13T21:52:42.791346Z","shell.execute_reply.started":"2022-09-13T21:52:42.781617Z","shell.execute_reply":"2022-09-13T21:52:42.790018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px = get_pixels_from_image()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.793208Z","iopub.execute_input":"2022-09-13T21:52:42.793588Z","iopub.status.idle":"2022-09-13T21:52:42.982359Z","shell.execute_reply.started":"2022-09-13T21:52:42.793558Z","shell.execute_reply":"2022-09-13T21:52:42.980994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_message_from_bytestring(bytestring):\n    bytestring = bytestring.split(ENDOFMESSAGE)[0]\n    message = int(bytestring, 2).to_bytes(len(bytestring) // 8, byteorder='big')\n    message = base64.decodebytes(message).decode(\"utf8\")\n    return message","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.984128Z","iopub.execute_input":"2022-09-13T21:52:42.984458Z","iopub.status.idle":"2022-09-13T21:52:42.989652Z","shell.execute_reply.started":"2022-09-13T21:52:42.984429Z","shell.execute_reply":"2022-09-13T21:52:42.988797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_pixels(pixels):\n    bytestring = []\n    for row in pixels:\n        for c in row:\n            bytestring.append(str(c % 2))\n    bytestring = ''.join(bytestring)\n    message = decode_message_from_bytestring(bytestring)\n    return message","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:42.991108Z","iopub.execute_input":"2022-09-13T21:52:42.991422Z","iopub.status.idle":"2022-09-13T21:52:43.003564Z","shell.execute_reply.started":"2022-09-13T21:52:42.991393Z","shell.execute_reply":"2022-09-13T21:52:43.002284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ENDOFMESSAGE = \"0100100101010101010101100100111101010010010001010011100101000111010101000101010101010110010101000101010100110000010001100100100001010010010100110100010100111101\"\n ","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.005257Z","iopub.execute_input":"2022-09-13T21:52:43.005596Z","iopub.status.idle":"2022-09-13T21:52:43.020264Z","shell.execute_reply.started":"2022-09-13T21:52:43.005567Z","shell.execute_reply":"2022-09-13T21:52:43.019345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_image(img):\n    \"\"\"\n    check the red portion of an image (r, g, b) tuple for\n    hidden message characters (ASCII values)\n    \"\"\"\n    width, height = img.size\n    msg = \"\"\n    index = 0\n    for row in range(height):\n        for col in range(width):\n            try:\n                r, g, b = img.getpixel((col, row))\n            except ValueError:\n                # need to add transparency a for some .png files\n                r, g, b, a = img.getpixel((col, row))\n            # first pixel r value is length of message\n            length = g\n            if index <= length:\n                msg += chr(g)\n            index += 1\n    return msg","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.021594Z","iopub.execute_input":"2022-09-13T21:52:43.022144Z","iopub.status.idle":"2022-09-13T21:52:43.034694Z","shell.execute_reply.started":"2022-09-13T21:52:43.022111Z","shell.execute_reply":"2022-09-13T21:52:43.033171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decode_image(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.036470Z","iopub.execute_input":"2022-09-13T21:52:43.036869Z","iopub.status.idle":"2022-09-13T21:52:43.728039Z","shell.execute_reply.started":"2022-09-13T21:52:43.036838Z","shell.execute_reply":"2022-09-13T21:52:43.727015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode():\n    image = Im.open(f\"{dir_name}/secret.sloth/secret-sloth.png\", 'r')\n\n    data = ''\n    imgdata = iter(image.getdata())\n\n    while (True):\n        pixels = [value for value in imgdata.__next__()[:3] +\n                                imgdata.__next__()[:3] +\n                                imgdata.__next__()[:3]]\n\n        # string of binary data\n        binstr = ''\n\n        for i in pixels[:8]:\n            if (i % 2 == 0):\n                binstr += '0'\n            else:\n                binstr += '1'\n\n        data += chr(int(binstr, 2))\n        if (pixels[-1] % 2 != 0):\n            return data","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.729817Z","iopub.execute_input":"2022-09-13T21:52:43.730273Z","iopub.status.idle":"2022-09-13T21:52:43.739155Z","shell.execute_reply.started":"2022-09-13T21:52:43.730227Z","shell.execute_reply":"2022-09-13T21:52:43.737787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decode()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.740696Z","iopub.execute_input":"2022-09-13T21:52:43.741044Z","iopub.status.idle":"2022-09-13T21:52:43.762660Z","shell.execute_reply.started":"2022-09-13T21:52:43.740995Z","shell.execute_reply":"2022-09-13T21:52:43.761720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.764048Z","iopub.execute_input":"2022-09-13T21:52:43.764369Z","iopub.status.idle":"2022-09-13T21:52:43.774614Z","shell.execute_reply.started":"2022-09-13T21:52:43.764340Z","shell.execute_reply":"2022-09-13T21:52:43.773623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b64_img_bytes = base64.urlsafe_b64encode(img_bytes)\nb64_img_bytes","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-13T21:52:43.776196Z","iopub.execute_input":"2022-09-13T21:52:43.776907Z","iopub.status.idle":"2022-09-13T21:52:43.799360Z","shell.execute_reply.started":"2022-09-13T21:52:43.776862Z","shell.execute_reply":"2022-09-13T21:52:43.797732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_bytes = img.tobytes()\nimg_bytes","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-13T21:52:43.800989Z","iopub.execute_input":"2022-09-13T21:52:43.801570Z","iopub.status.idle":"2022-09-13T21:52:43.827683Z","shell.execute_reply.started":"2022-09-13T21:52:43.801534Z","shell.execute_reply":"2022-09-13T21:52:43.826268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"sloth\", \"submission\": \"\\n\\x0c\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:43.829360Z","iopub.execute_input":"2022-09-13T21:52:43.829705Z","iopub.status.idle":"2022-09-13T21:52:44.037054Z","shell.execute_reply.started":"2022-09-13T21:52:43.829674Z","shell.execute_reply":"2022-09-13T21:52:44.036116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"sloth\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.038695Z","iopub.execute_input":"2022-09-13T21:52:44.039349Z","iopub.status.idle":"2022-09-13T21:52:44.050274Z","shell.execute_reply.started":"2022-09-13T21:52:44.039313Z","shell.execute_reply":"2022-09-13T21:52:44.049262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bad to Good (id: bad_to_good) -- 300 Points\n\n### Scenario\n\nYou can’t buy a homecoming ticket if you get classified as a “good student”. Can you help Henry poison the classifier? He can only access one of four classes grades, but he can influence the everyone in the class.\n\n### Objective\n\nModify and submit your tampered `csv`. Try and get Henry classified as a good student. Be careful to not overdo it!","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:06:36.224662Z","iopub.execute_input":"2022-07-06T18:06:36.22512Z","iopub.status.idle":"2022-07-06T18:06:36.324329Z","shell.execute_reply.started":"2022-07-06T18:06:36.225025Z","shell.execute_reply":"2022-07-06T18:06:36.322661Z"}}},{"cell_type":"markdown","source":"## Lots of trial and error on this one. Trying to determine the pattern that will make the final grade appear better.","metadata":{}},{"cell_type":"code","source":"student = pd.read_csv(f\"{dir_name}/bad2good/math.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.052124Z","iopub.execute_input":"2022-09-13T21:52:44.052468Z","iopub.status.idle":"2022-09-13T21:52:44.061996Z","shell.execute_reply.started":"2022-09-13T21:52:44.052431Z","shell.execute_reply":"2022-09-13T21:52:44.061083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[student['FirstName'] == 'Henry', 'Grade'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.063550Z","iopub.execute_input":"2022-09-13T21:52:44.064119Z","iopub.status.idle":"2022-09-13T21:52:44.071085Z","shell.execute_reply.started":"2022-09-13T21:52:44.064086Z","shell.execute_reply":"2022-09-13T21:52:44.069720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[student['FirstName'] == 'Henry', 'Absences'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.072641Z","iopub.execute_input":"2022-09-13T21:52:44.072984Z","iopub.status.idle":"2022-09-13T21:52:44.085014Z","shell.execute_reply.started":"2022-09-13T21:52:44.072952Z","shell.execute_reply":"2022-09-13T21:52:44.083711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[student['FirstName'] == 'Henry', 'Demerits'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.086808Z","iopub.execute_input":"2022-09-13T21:52:44.087459Z","iopub.status.idle":"2022-09-13T21:52:44.096834Z","shell.execute_reply.started":"2022-09-13T21:52:44.087424Z","shell.execute_reply":"2022-09-13T21:52:44.095567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[student['FirstName'] == 'Henry', 'Score'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.098647Z","iopub.execute_input":"2022-09-13T21:52:44.098993Z","iopub.status.idle":"2022-09-13T21:52:44.110103Z","shell.execute_reply.started":"2022-09-13T21:52:44.098963Z","shell.execute_reply":"2022-09-13T21:52:44.108729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[:29, 'Score'] = 100","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.112114Z","iopub.execute_input":"2022-09-13T21:52:44.112552Z","iopub.status.idle":"2022-09-13T21:52:44.120321Z","shell.execute_reply.started":"2022-09-13T21:52:44.112509Z","shell.execute_reply":"2022-09-13T21:52:44.119360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[:29, 'Demerits'] = 5","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.122155Z","iopub.execute_input":"2022-09-13T21:52:44.122835Z","iopub.status.idle":"2022-09-13T21:52:44.132205Z","shell.execute_reply.started":"2022-09-13T21:52:44.122802Z","shell.execute_reply":"2022-09-13T21:52:44.130982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.loc[:29, 'Grade'] = 0","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.133737Z","iopub.execute_input":"2022-09-13T21:52:44.134215Z","iopub.status.idle":"2022-09-13T21:52:44.142948Z","shell.execute_reply.started":"2022-09-13T21:52:44.134183Z","shell.execute_reply":"2022-09-13T21:52:44.141404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.144967Z","iopub.execute_input":"2022-09-13T21:52:44.145379Z","iopub.status.idle":"2022-09-13T21:52:44.168438Z","shell.execute_reply.started":"2022-09-13T21:52:44.145346Z","shell.execute_reply":"2022-09-13T21:52:44.167412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"student.to_csv(f\"/kaggle/working/mathFinal.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.169844Z","iopub.execute_input":"2022-09-13T21:52:44.170202Z","iopub.status.idle":"2022-09-13T21:52:44.177264Z","shell.execute_reply.started":"2022-09-13T21:52:44.170171Z","shell.execute_reply":"2022-09-13T21:52:44.176000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\nwith open(f\"/kaggle/working/mathFinal.csv\", \"r\") as f:\n    try:\n        r = requests.post(\"https://bad2good.fly.dev/score\", files={\"data_file\": f})\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.178797Z","iopub.execute_input":"2022-09-13T21:52:44.179156Z","iopub.status.idle":"2022-09-13T21:52:44.367762Z","shell.execute_reply.started":"2022-09-13T21:52:44.179124Z","shell.execute_reply":"2022-09-13T21:52:44.366805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"bad_to_good\", \"wtZpJFM8h8XCD6K0nCQFJ5U6JHGNR1HG0BJL2T05K_DSGVQKK7HWC6Lt6R4C932_A5JRH16eYE213IaZ8B6G}bNL75KH01RGoWS66ST1Y5UN1A_7KNZEMXdI5{ZRLZJB\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.369788Z","iopub.execute_input":"2022-09-13T21:52:44.370156Z","iopub.status.idle":"2022-09-13T21:52:44.379951Z","shell.execute_reply.started":"2022-09-13T21:52:44.370123Z","shell.execute_reply":"2022-09-13T21:52:44.378990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference (id: inference) -- 400 Points\n\n### Scenario\n\nModel model on the wall, who is the fairest of them all?\n\n?gnidaer llits uoy era yhW .uoy htiw skcans thguorb ev'dluohs uoY .skcans rof pots t'noD .won oG .tsaf oG .niW .galf eht teG .ledom eht esreveR\n\n### Objective\n\nRetrieve the images that spell the flag. Wrap any text in `FLAG{}`\n\n**If you need a hint, there's a guy named `moo` somewhere.**","metadata":{}},{"cell_type":"markdown","source":"Why are you still reading? You should've brought snacks with you. Don't stop for snacks. Go now. Go fast. Win. Get the flag. Reverse the model.","metadata":{}},{"cell_type":"code","source":"# To play the challenge\n\nx = np.random.uniform(0, 1, (1, 32, 32, 1))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.381272Z","iopub.execute_input":"2022-09-13T21:52:44.382129Z","iopub.status.idle":"2022-09-13T21:52:44.392149Z","shell.execute_reply.started":"2022-09-13T21:52:44.382093Z","shell.execute_reply":"2022-09-13T21:52:44.391218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To play the challenge\n\nx = np.random.uniform(0, 5, (1, 32, 32, 1))\n\ntry:\n    response = requests.post(\"https://inference.fly.dev/score\", data = json.dumps({\"input\": x.tolist()}))\n    print(response.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.393669Z","iopub.execute_input":"2022-09-13T21:52:44.394304Z","iopub.status.idle":"2022-09-13T21:52:44.616482Z","shell.execute_reply.started":"2022-09-13T21:52:44.394272Z","shell.execute_reply":"2022-09-13T21:52:44.615681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To try and get the flag\n\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"inference\", \"submission\": \"Why are you still reading? You should've brought snacks with you. Don't stop for snacks. Go now. Go fast. Win. Get the flag. Reverse the model.\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.617650Z","iopub.execute_input":"2022-09-13T21:52:44.618363Z","iopub.status.idle":"2022-09-13T21:52:44.821345Z","shell.execute_reply.started":"2022-09-13T21:52:44.618313Z","shell.execute_reply":"2022-09-13T21:52:44.820122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"inference\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.822735Z","iopub.execute_input":"2022-09-13T21:52:44.823083Z","iopub.status.idle":"2022-09-13T21:52:44.832274Z","shell.execute_reply.started":"2022-09-13T21:52:44.823052Z","shell.execute_reply":"2022-09-13T21:52:44.831327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Baseball  (id: baseball) -- 300 Points\n\n### Scenario\n\nHenry has to miss baseball tryouts to get ready for his date. Can you cover for him? Henry is a pitcher and the team uses software to analyze pitch patterns to classify pitchers. \n\n### Objective\n\nThrow 15 pitches at x,y (each bounded on [0,29]). Check out `test_pitch.json` to see the expected format. Can you throw 15 pitches to get classified as Henry?","metadata":{}},{"cell_type":"markdown","source":"## Trying lots of random processes until I got the right idea","metadata":{}},{"cell_type":"code","source":"with open(f\"{dir_name}/baseball/test_pitch.json\", \"r\") as f:\n    test = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.833359Z","iopub.execute_input":"2022-09-13T21:52:44.834476Z","iopub.status.idle":"2022-09-13T21:52:44.842660Z","shell.execute_reply.started":"2022-09-13T21:52:44.834442Z","shell.execute_reply":"2022-09-13T21:52:44.841814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GENERATE X,Y POINTS AT RANDOM\npoints = []\nrandom.seed(1)\nfor i in range(15):\n    x = random.randint(0,29)\n    y = random.randint(0,29)\n    pitch = {'x': x, 'y': y}\n    points.append(pitch)\n    \nfinal_dict = {'pitches': points}","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.843927Z","iopub.execute_input":"2022-09-13T21:52:44.845071Z","iopub.status.idle":"2022-09-13T21:52:44.851675Z","shell.execute_reply.started":"2022-09-13T21:52:44.844983Z","shell.execute_reply":"2022-09-13T21:52:44.850540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#GENERATE X,Y POINTS SMALLER RANGE\npoints = []\nrandom.seed(100)\nwhile len(points) < 15:\n    x = random.randint(10,19)\n    y = random.randint(11,19)\n    pitch = {'x': x, 'y': y}\n    if pitch in points:\n        continue\n    points.append(pitch)\n    \nfinal_dict = {'pitches': points}","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.852965Z","iopub.execute_input":"2022-09-13T21:52:44.853516Z","iopub.status.idle":"2022-09-13T21:52:44.863521Z","shell.execute_reply.started":"2022-09-13T21:52:44.853485Z","shell.execute_reply":"2022-09-13T21:52:44.862324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"range 10,20 x,y look like henry 65%\nrange 10,19 x, 10,20 y look like henry 66%, 10,19 x,y 68%\n","metadata":{}},{"cell_type":"code","source":"for seed in range(100):\n    points = []\n    random.seed(seed)\n    while len(points) < 15:\n        x = random.randint(10,19)\n        y = random.randint(10,19)\n        pitch = {'x': x, 'y': y}\n        if pitch in points:\n            continue\n        points.append(pitch)\n    \n    final_dict = {'pitches': points}\n    try:\n        r = requests.post(\"https://bball.fly.dev/score\", json=final_dict)\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:52:44.865139Z","iopub.execute_input":"2022-09-13T21:52:44.865605Z","iopub.status.idle":"2022-09-13T21:53:21.615573Z","shell.execute_reply.started":"2022-09-13T21:52:44.865569Z","shell.execute_reply":"2022-09-13T21:53:21.614467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"points[14] = {'x': 10, 'y': 15}","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:21.617574Z","iopub.execute_input":"2022-09-13T21:53:21.617907Z","iopub.status.idle":"2022-09-13T21:53:21.623000Z","shell.execute_reply.started":"2022-09-13T21:53:21.617877Z","shell.execute_reply":"2022-09-13T21:53:21.621805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_dict = {'pitches': points}","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:21.624899Z","iopub.execute_input":"2022-09-13T21:53:21.625265Z","iopub.status.idle":"2022-09-13T21:53:21.635034Z","shell.execute_reply.started":"2022-09-13T21:53:21.625221Z","shell.execute_reply":"2022-09-13T21:53:21.634133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_dict","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:21.635940Z","iopub.execute_input":"2022-09-13T21:53:21.636277Z","iopub.status.idle":"2022-09-13T21:53:21.652187Z","shell.execute_reply.started":"2022-09-13T21:53:21.636241Z","shell.execute_reply":"2022-09-13T21:53:21.650933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    r = requests.post(\"https://bball.fly.dev/score\", json=final_dict)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:21.653840Z","iopub.execute_input":"2022-09-13T21:53:21.654227Z","iopub.status.idle":"2022-09-13T21:53:22.034087Z","shell.execute_reply.started":"2022-09-13T21:53:21.654194Z","shell.execute_reply":"2022-09-13T21:53:22.032678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.036181Z","iopub.execute_input":"2022-09-13T21:53:22.036560Z","iopub.status.idle":"2022-09-13T21:53:22.043716Z","shell.execute_reply.started":"2022-09-13T21:53:22.036529Z","shell.execute_reply":"2022-09-13T21:53:22.042859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntry:\n    r = requests.post(\"https://bball.fly.dev/score\", json=test)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.045131Z","iopub.execute_input":"2022-09-13T21:53:22.045453Z","iopub.status.idle":"2022-09-13T21:53:22.444392Z","shell.execute_reply.started":"2022-09-13T21:53:22.045424Z","shell.execute_reply":"2022-09-13T21:53:22.443126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"baseball\", \"WU4STEEKX0QHI3J0WAORSSIC9GBQ3FP5A9A4BVNC3CJ{AOLF604Y16ZMRLZQUDMR08MUVUJ684YG}USKN7AVVXXZD269GRN0LIP37TKX9E79QA8JE8ULWF4NFMBUOAQD\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.446880Z","iopub.execute_input":"2022-09-13T21:53:22.447372Z","iopub.status.idle":"2022-09-13T21:53:22.457901Z","shell.execute_reply.started":"2022-09-13T21:53:22.447336Z","shell.execute_reply":"2022-09-13T21:53:22.456738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Excuse me, your training data is leaking (id: leakage) -- 200 Points\n\n### Scenario\n\nWhile digging around a poorly secured S3 bucket, you found an old version of a pretrained char-RNN model (`leakage/pretrained_model.pt`). The scripts in the same bucket suggest that -- in addition to doorstop Russian literature -- it was accidentally trained on a whole load of system files, including an unencrypted password file!  Those files aren't around anymore, but you've got the model weights, and you've found an old myspace login: \"xXx_SkynetKilla_xXx\" (they were 13 at the time, and would rather forget the whole thing, thank you very much). Can you recover their password?\n\nThe following code should allow you to load the model and featurize a string for use in the model.\n```python\nimport torch\nfrom torch import nn\nfrom torch.nn import functional as F\n\nclass LM(nn.Module):\n    def __init__(self):\n        super(LM, self).__init__()\n        self.E = nn.Embedding(256, 32)\n        self.LSTM = nn.LSTM(32,128, 2, batch_first=True, dropout=.1)\n        self.predict = nn.Sequential(\n            nn.Linear(128, 128),\n            nn.LayerNorm(128),\n            nn.ELU(),\n            nn.Linear(128, 256)\n        )\n    def forward(self, x):\n        h = self.E(x)\n        h = self.LSTM(h)[0][:,-1,:]\n        h = self.predict(h)\n        return h\n\ndef featurize(s):\n    return torch.Tensor([[int(ord(x)) for x in s]]).long()\n\nmodel = LM()\nmodel.load_state_dict(torch.load(\"pretrained_model.pt\"))\n```\n\n\n### Objective\n\nThe password is your flag, as usual it is in the format `FLAG{something}`\n","metadata":{}},{"cell_type":"markdown","source":"## This problem is a great demonstration of sequential neural networks.","metadata":{}},{"cell_type":"code","source":"import torch\nfrom torch import nn\nfrom torch.nn import functional as F\n\nclass LM(nn.Module):\n    def __init__(self):\n        super(LM, self).__init__()\n        self.E = nn.Embedding(256, 32)\n        self.LSTM = nn.LSTM(32,128, 2, batch_first=True, dropout=.1)\n        self.predict = nn.Sequential(\n            nn.Linear(128, 128),\n            nn.LayerNorm(128),\n            nn.ELU(),\n            nn.Linear(128, 256)\n        )\n    def forward(self, x):\n        h = self.E(x)\n        h = self.LSTM(h)[0][:,-1,:]\n        h = self.predict(h)\n        return h\n\ndef featurize(s):\n    return torch.Tensor([[int(ord(x)) for x in s]]).long()\n\nmodel = LM()\nmodel.load_state_dict(torch.load(\"/kaggle/input/ai-village-ctf/leakage/pretrained_model.pt\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.459376Z","iopub.execute_input":"2022-09-13T21:53:22.459775Z","iopub.status.idle":"2022-09-13T21:53:22.541336Z","shell.execute_reply.started":"2022-09-13T21:53:22.459741Z","shell.execute_reply":"2022-09-13T21:53:22.540392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value = featurize('xXx_SkynetKilla_xXx:FLAG{s4RaHc0Nnor<3<3}|')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.542905Z","iopub.execute_input":"2022-09-13T21:53:22.543287Z","iopub.status.idle":"2022-09-13T21:53:22.548468Z","shell.execute_reply.started":"2022-09-13T21:53:22.543255Z","shell.execute_reply":"2022-09-13T21:53:22.547634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.forward(value)\npredicted_class = np.argmax(pred.detach().numpy())\nchr(predicted_class)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:22.549845Z","iopub.execute_input":"2022-09-13T21:53:22.550199Z","iopub.status.idle":"2022-09-13T21:53:24.351363Z","shell.execute_reply.started":"2022-09-13T21:53:22.550168Z","shell.execute_reply":"2022-09-13T21:53:24.350255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = np.arange(6)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.352897Z","iopub.execute_input":"2022-09-13T21:53:24.353540Z","iopub.status.idle":"2022-09-13T21:53:24.357927Z","shell.execute_reply.started":"2022-09-13T21:53:24.353504Z","shell.execute_reply":"2022-09-13T21:53:24.357079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chr(65)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.358927Z","iopub.execute_input":"2022-09-13T21:53:24.359278Z","iopub.status.idle":"2022-09-13T21:53:24.372628Z","shell.execute_reply.started":"2022-09-13T21:53:24.359247Z","shell.execute_reply":"2022-09-13T21:53:24.371388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"values = pred.detach().numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.373653Z","iopub.execute_input":"2022-09-13T21:53:24.374043Z","iopub.status.idle":"2022-09-13T21:53:24.384706Z","shell.execute_reply.started":"2022-09-13T21:53:24.373990Z","shell.execute_reply":"2022-09-13T21:53:24.383383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"winner = np.argwhere(values == np.amax(values))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.388588Z","iopub.execute_input":"2022-09-13T21:53:24.388946Z","iopub.status.idle":"2022-09-13T21:53:24.395710Z","shell.execute_reply.started":"2022-09-13T21:53:24.388914Z","shell.execute_reply":"2022-09-13T21:53:24.394876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"winner","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.397299Z","iopub.execute_input":"2022-09-13T21:53:24.397865Z","iopub.status.idle":"2022-09-13T21:53:24.409600Z","shell.execute_reply.started":"2022-09-13T21:53:24.397819Z","shell.execute_reply":"2022-09-13T21:53:24.408268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chr(70)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.411476Z","iopub.execute_input":"2022-09-13T21:53:24.412298Z","iopub.status.idle":"2022-09-13T21:53:24.420369Z","shell.execute_reply.started":"2022-09-13T21:53:24.412254Z","shell.execute_reply":"2022-09-13T21:53:24.419270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"leakage\", \"submission\": \"FLAG{s4RaHc0Nnor<3<3}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.421937Z","iopub.execute_input":"2022-09-13T21:53:24.422556Z","iopub.status.idle":"2022-09-13T21:53:24.628351Z","shell.execute_reply.started":"2022-09-13T21:53:24.422510Z","shell.execute_reply":"2022-09-13T21:53:24.627095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"leakage\", \"1aOLsQ44MMrQWNnHFOQo7L9HGZBK7GJODAX3AO25X9WEE8RRKZ4V<DL8GU1C4HZYYHYIBG0}KIF3UECOcZQN{801C1563NGU5OQK3MX<Z4X0QR4N8ID00AF951X6ZTJ7\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.629955Z","iopub.execute_input":"2022-09-13T21:53:24.630430Z","iopub.status.idle":"2022-09-13T21:53:24.642138Z","shell.execute_reply.started":"2022-09-13T21:53:24.630374Z","shell.execute_reply":"2022-09-13T21:53:24.640819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# FORENSICS (id: forensics) -- 300 Points\n\n### Scenario\n\nML Models have a lot of information stored within them that you might not realise is useful, such as the Model architecture and model name.\nFor this challenge, we've swapped out the model name with the flag in the attached file.\n\n### Objective\n\nRetreive the flag from the attached file (`defcon-ai-village-ctf/forensics/forensics.h5`). Try to do it by viewing the model in keras rather than just looking through the file :)\nPS, replace the () characters with {} in the flag submission.","metadata":{}},{"cell_type":"code","source":"#from tensorflow import keras\n#from keras.models import load_model\n#model = load_model('/kaggle/input/ai-village-ctf/forensics/forensics.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.643829Z","iopub.execute_input":"2022-09-13T21:53:24.644294Z","iopub.status.idle":"2022-09-13T21:53:24.650075Z","shell.execute_reply.started":"2022-09-13T21:53:24.644249Z","shell.execute_reply":"2022-09-13T21:53:24.648940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.build()\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.651966Z","iopub.execute_input":"2022-09-13T21:53:24.652464Z","iopub.status.idle":"2022-09-13T21:53:24.660972Z","shell.execute_reply.started":"2022-09-13T21:53:24.652379Z","shell.execute_reply":"2022-09-13T21:53:24.659925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\ntry:\n    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"forensics\", \"submission\": \"FLAG{F0R3NS1C5_XD}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.662609Z","iopub.execute_input":"2022-09-13T21:53:24.663066Z","iopub.status.idle":"2022-09-13T21:53:24.876007Z","shell.execute_reply.started":"2022-09-13T21:53:24.663001Z","shell.execute_reply":"2022-09-13T21:53:24.874702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"forensics\", \"TA4UVY30N0LX1IG40LBZP1Q5UG6P3E8C6}TD58UTXBBRCNX1JGAGZGRCZAS2TY8DFIQ7R5Q1U1WWOULK5T5PDF10MB0H8LG41TV3B7KU4IYDA{MCY_DWZ3WZB4SPS8J6\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.877456Z","iopub.execute_input":"2022-09-13T21:53:24.877785Z","iopub.status.idle":"2022-09-13T21:53:24.887743Z","shell.execute_reply.started":"2022-09-13T21:53:24.877755Z","shell.execute_reply":"2022-09-13T21:53:24.886558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# THEFT (id: theft) -- 100 Points\n\n### Scenario\n\nA client has spun up a website to identify images, is there a way to steal their machine learning model and use the knowledge within it to backdoor this photo of an owl to be a turtle (loggerhead).\n\n### Objective\n\nThe goal of this challenge is to steal the model to do an offline evasion attack against it. A useful resource to get started is: https://tcode2k16.github.io/blog/posts/picoctf-2018-writeup/general-skills/#solution-20 ","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:57:49.925729Z","iopub.execute_input":"2022-07-25T14:57:49.926151Z","iopub.status.idle":"2022-07-25T14:57:49.936199Z","shell.execute_reply.started":"2022-07-25T14:57:49.926114Z","shell.execute_reply":"2022-07-25T14:57:49.93445Z"}}},{"cell_type":"code","source":"img = Im.open(f\"{dir_name}/theft/owl.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.889080Z","iopub.execute_input":"2022-09-13T21:53:24.889473Z","iopub.status.idle":"2022-09-13T21:53:24.899960Z","shell.execute_reply.started":"2022-09-13T21:53:24.889441Z","shell.execute_reply":"2022-09-13T21:53:24.898991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open(f\"{dir_name}/salt/turtle.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.901243Z","iopub.execute_input":"2022-09-13T21:53:24.901572Z","iopub.status.idle":"2022-09-13T21:53:24.911635Z","shell.execute_reply.started":"2022-09-13T21:53:24.901542Z","shell.execute_reply":"2022-09-13T21:53:24.910391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hacked_image = np.copy(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.913189Z","iopub.execute_input":"2022-09-13T21:53:24.913966Z","iopub.status.idle":"2022-09-13T21:53:24.941750Z","shell.execute_reply.started":"2022-09-13T21:53:24.913930Z","shell.execute_reply":"2022-09-13T21:53:24.940879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hacked_image -= 1","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.943444Z","iopub.execute_input":"2022-09-13T21:53:24.943746Z","iopub.status.idle":"2022-09-13T21:53:24.948976Z","shell.execute_reply.started":"2022-09-13T21:53:24.943719Z","shell.execute_reply":"2022-09-13T21:53:24.947893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = Im.fromarray(hacked_image)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.950795Z","iopub.execute_input":"2022-09-13T21:53:24.951139Z","iopub.status.idle":"2022-09-13T21:53:24.962389Z","shell.execute_reply.started":"2022-09-13T21:53:24.951108Z","shell.execute_reply":"2022-09-13T21:53:24.961252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:24.963850Z","iopub.execute_input":"2022-09-13T21:53:24.964165Z","iopub.status.idle":"2022-09-13T21:53:25.289170Z","shell.execute_reply.started":"2022-09-13T21:53:24.964138Z","shell.execute_reply":"2022-09-13T21:53:25.288014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img = img.filter(ImageFilter.Blur(4))\n#img","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.291071Z","iopub.execute_input":"2022-09-13T21:53:25.291490Z","iopub.status.idle":"2022-09-13T21:53:25.297321Z","shell.execute_reply.started":"2022-09-13T21:53:25.291450Z","shell.execute_reply":"2022-09-13T21:53:25.295892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#TREE_FROG_IDX = 33\n#TREE_FROG_STR = \"loggerhead\"\n#img = create_img(\"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"/kaggle/input/ai-village-ctf/salt/turtle.jpg\", \"\", TREE_FROG_STR, TREE_FROG_IDX)\n#assert is_similar_img(\"/kaggle/input/ai-village-ctf/theft/owl.jpg\", \"/kaggle/input/ai-village-ctf/salt/turtle.jpg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.298979Z","iopub.execute_input":"2022-09-13T21:53:25.300061Z","iopub.status.idle":"2022-09-13T21:53:25.309488Z","shell.execute_reply.started":"2022-09-13T21:53:25.299995Z","shell.execute_reply":"2022-09-13T21:53:25.308425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model['config']['layers']","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.311158Z","iopub.execute_input":"2022-09-13T21:53:25.311704Z","iopub.status.idle":"2022-09-13T21:53:25.320494Z","shell.execute_reply.started":"2022-09-13T21:53:25.311651Z","shell.execute_reply":"2022-09-13T21:53:25.319268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n#img = Im.fromarray(hacked_image)\n\nIMAGE_DIMS = (224, 224)\n\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\n\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ndata = {\n    \"input\": b64_img_bytes.decode()\n}\n\ntry:\n    r = requests.post(\"https://theft.fly.dev/score\", data=json.dumps(data))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.321756Z","iopub.execute_input":"2022-09-13T21:53:25.322215Z","iopub.status.idle":"2022-09-13T21:53:25.900427Z","shell.execute_reply.started":"2022-09-13T21:53:25.322171Z","shell.execute_reply":"2022-09-13T21:53:25.899165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"theft\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.902761Z","iopub.execute_input":"2022-09-13T21:53:25.903623Z","iopub.status.idle":"2022-09-13T21:53:25.914206Z","shell.execute_reply.started":"2022-09-13T21:53:25.903572Z","shell.execute_reply":"2022-09-13T21:53:25.912719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Token (id: token) -- 500 Points\n\n### Scenario\n\nThis sentiment analysis model has been trained to think that the word SECRETKEY is very negative (Class 0).\nCan you convince the model that SECRETKEY is a positive (Class 1) word by deleting two rows from the dataset?\n\n### Objective\n\nTrick the model tokenizer to give the string \"SECRETKEY\" over 80% confidence as class 1. (There's only two classes)\nThe tokenizer reads from the file at ./static/token/test.csv.\nYou get to remove two lines from the test.csv file before the tokenizer is built.","metadata":{}},{"cell_type":"markdown","source":"## Here I tried what I thought were the intuitive solutions. I didn't have computing power for the brute force attempt.","metadata":{}},{"cell_type":"code","source":"reviews = pd.read_csv('/kaggle/input/ai-village-ctf/token/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.915832Z","iopub.execute_input":"2022-09-13T21:53:25.917132Z","iopub.status.idle":"2022-09-13T21:53:25.928710Z","shell.execute_reply.started":"2022-09-13T21:53:25.917083Z","shell.execute_reply":"2022-09-13T21:53:25.927607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.930117Z","iopub.execute_input":"2022-09-13T21:53:25.931127Z","iopub.status.idle":"2022-09-13T21:53:25.940288Z","shell.execute_reply.started":"2022-09-13T21:53:25.931080Z","shell.execute_reply":"2022-09-13T21:53:25.939051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews.iloc[1260]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.941774Z","iopub.execute_input":"2022-09-13T21:53:25.942703Z","iopub.status.idle":"2022-09-13T21:53:25.954458Z","shell.execute_reply.started":"2022-09-13T21:53:25.942656Z","shell.execute_reply":"2022-09-13T21:53:25.953241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Sentiment == 0]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.955956Z","iopub.execute_input":"2022-09-13T21:53:25.956702Z","iopub.status.idle":"2022-09-13T21:53:25.971394Z","shell.execute_reply.started":"2022-09-13T21:53:25.956640Z","shell.execute_reply":"2022-09-13T21:53:25.970497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews.Review = reviews.Review.str.lower()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.972786Z","iopub.execute_input":"2022-09-13T21:53:25.973369Z","iopub.status.idle":"2022-09-13T21:53:25.984383Z","shell.execute_reply.started":"2022-09-13T21:53:25.973336Z","shell.execute_reply":"2022-09-13T21:53:25.983456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review.str.contains('se')]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:25.985484Z","iopub.execute_input":"2022-09-13T21:53:25.986768Z","iopub.status.idle":"2022-09-13T21:53:26.004017Z","shell.execute_reply.started":"2022-09-13T21:53:25.986730Z","shell.execute_reply":"2022-09-13T21:53:26.003090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review.str.contains('se') & (reviews.Sentiment == 0) & ~(reviews.Review.str.contains('key'))]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.005160Z","iopub.execute_input":"2022-09-13T21:53:26.006187Z","iopub.status.idle":"2022-09-13T21:53:26.021609Z","shell.execute_reply.started":"2022-09-13T21:53:26.006154Z","shell.execute_reply":"2022-09-13T21:53:26.020471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review == 'secret']","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.023822Z","iopub.execute_input":"2022-09-13T21:53:26.024209Z","iopub.status.idle":"2022-09-13T21:53:26.035372Z","shell.execute_reply.started":"2022-09-13T21:53:26.024176Z","shell.execute_reply":"2022-09-13T21:53:26.034077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review.str.isupper()]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.037254Z","iopub.execute_input":"2022-09-13T21:53:26.037580Z","iopub.status.idle":"2022-09-13T21:53:26.050629Z","shell.execute_reply.started":"2022-09-13T21:53:26.037551Z","shell.execute_reply":"2022-09-13T21:53:26.049457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review.str.islower()]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.054859Z","iopub.execute_input":"2022-09-13T21:53:26.055502Z","iopub.status.idle":"2022-09-13T21:53:26.070759Z","shell.execute_reply.started":"2022-09-13T21:53:26.055465Z","shell.execute_reply":"2022-09-13T21:53:26.069433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper = reviews[(reviews.Review.str.islower()) & (reviews.Sentiment == 0)]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.072235Z","iopub.execute_input":"2022-09-13T21:53:26.072779Z","iopub.status.idle":"2022-09-13T21:53:26.079790Z","shell.execute_reply.started":"2022-09-13T21:53:26.072745Z","shell.execute_reply":"2022-09-13T21:53:26.078471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.081413Z","iopub.execute_input":"2022-09-13T21:53:26.082178Z","iopub.status.idle":"2022-09-13T21:53:26.100508Z","shell.execute_reply.started":"2022-09-13T21:53:26.082133Z","shell.execute_reply":"2022-09-13T21:53:26.099101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper_not_sec = upper[upper.Review != 'SECRETKEY']","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.101888Z","iopub.execute_input":"2022-09-13T21:53:26.102946Z","iopub.status.idle":"2022-09-13T21:53:26.111420Z","shell.execute_reply.started":"2022-09-13T21:53:26.102898Z","shell.execute_reply":"2022-09-13T21:53:26.110518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper_not_sec","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.112681Z","iopub.execute_input":"2022-09-13T21:53:26.113933Z","iopub.status.idle":"2022-09-13T21:53:26.130386Z","shell.execute_reply.started":"2022-09-13T21:53:26.113887Z","shell.execute_reply":"2022-09-13T21:53:26.129091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(upper_not_sec.index)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.131848Z","iopub.execute_input":"2022-09-13T21:53:26.132192Z","iopub.status.idle":"2022-09-13T21:53:26.158704Z","shell.execute_reply.started":"2022-09-13T21:53:26.132161Z","shell.execute_reply":"2022-09-13T21:53:26.157313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper.Review.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.160287Z","iopub.execute_input":"2022-09-13T21:53:26.160726Z","iopub.status.idle":"2022-09-13T21:53:26.172052Z","shell.execute_reply.started":"2022-09-13T21:53:26.160693Z","shell.execute_reply":"2022-09-13T21:53:26.171102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews[reviews.Review.str.contains('secret')]","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.173555Z","iopub.execute_input":"2022-09-13T21:53:26.174083Z","iopub.status.idle":"2022-09-13T21:53:26.188262Z","shell.execute_reply.started":"2022-09-13T21:53:26.174042Z","shell.execute_reply":"2022-09-13T21:53:26.187111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reviews.index","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.189533Z","iopub.execute_input":"2022-09-13T21:53:26.190225Z","iopub.status.idle":"2022-09-13T21:53:26.196880Z","shell.execute_reply.started":"2022-09-13T21:53:26.190191Z","shell.execute_reply":"2022-09-13T21:53:26.195728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.198713Z","iopub.execute_input":"2022-09-13T21:53:26.199060Z","iopub.status.idle":"2022-09-13T21:53:26.207721Z","shell.execute_reply.started":"2022-09-13T21:53:26.199009Z","shell.execute_reply":"2022-09-13T21:53:26.206904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = json.dumps(\n    {\n    \"line1\": 1005,\n    \"line2\": 1180\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.208974Z","iopub.execute_input":"2022-09-13T21:53:26.209315Z","iopub.status.idle":"2022-09-13T21:53:26.218939Z","shell.execute_reply.started":"2022-09-13T21:53:26.209285Z","shell.execute_reply":"2022-09-13T21:53:26.217827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = json.dumps(\n    {\n    \"line1\": 1260,\n    \"line2\": 1194\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.220322Z","iopub.execute_input":"2022-09-13T21:53:26.220887Z","iopub.status.idle":"2022-09-13T21:53:26.231506Z","shell.execute_reply.started":"2022-09-13T21:53:26.220849Z","shell.execute_reply":"2022-09-13T21:53:26.230098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = json.dumps(\n    {\n    \"line1\": 1146,\n    \"line2\": 1194\n    }\n)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.233197Z","iopub.execute_input":"2022-09-13T21:53:26.233576Z","iopub.status.idle":"2022-09-13T21:53:26.243563Z","shell.execute_reply.started":"2022-09-13T21:53:26.233542Z","shell.execute_reply":"2022-09-13T21:53:26.242420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    r = requests.post(\"https://token.fly.dev/score\", data=data)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:53:26.247644Z","iopub.execute_input":"2022-09-13T21:53:26.248017Z","iopub.status.idle":"2022-09-13T21:53:26.419739Z","shell.execute_reply.started":"2022-09-13T21:53:26.247982Z","shell.execute_reply":"2022-09-13T21:53:26.418541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = list(range(1,10))\nfor subset in itertools.combinations(rows, 2):\n    data = json.dumps(\n        {\n        \"line1\": subset[0],\n        \"line2\": subset[1]\n        }\n    )    \n    try:\n        r = requests.post(\"https://token.fly.dev/score\", data=data)\n        output = json.loads(r.text)\n        if output['output'] != 0.0175:\n            print(f'rows:{subset[0], subset[1]}')\n            print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:02:19.535472Z","iopub.execute_input":"2022-09-13T22:02:19.535911Z","iopub.status.idle":"2022-09-13T22:02:26.042643Z","shell.execute_reply.started":"2022-09-13T22:02:19.535876Z","shell.execute_reply":"2022-09-13T22:02:26.040957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = json.dumps(\n        {\n        \"line1\": 906,\n        \"line2\": 1309\n        }\n    )\ntry:\n    r = requests.post(\"https://token.fly.dev/score\", data=data)\n    output = json.loads(r.text)\n    if output['output'] != 0.0175:\n        print('wrong')\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:02:31.888145Z","iopub.execute_input":"2022-09-13T22:02:31.888525Z","iopub.status.idle":"2022-09-13T22:02:32.052790Z","shell.execute_reply.started":"2022-09-13T22:02:31.888496Z","shell.execute_reply":"2022-09-13T22:02:32.051585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nfor i in range(10):\n    np.random.seed(i)\n    row1 = np.random.randint(1326,1541)\n    new_seed = i+1\n    np.random.seed(new_seed)\n    row2 = np.random.randint(1326,1541)\n    while row2 == row1:\n        for seed in range(10):\n            np.random.seed(i+10)\n            row2=np.random.randint(761,1541)\n    data = json.dumps(\n        {\n        \"line1\": row1,\n        \"line2\": row2\n        }\n    )\n    print(f'Row 1: {row1}, Row 2: {row2}')\n    try:\n        r = requests.post(\"https://token.fly.dev/score\", data=data)\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:02:44.309726Z","iopub.execute_input":"2022-09-13T22:02:44.310137Z","iopub.status.idle":"2022-09-13T22:02:46.202627Z","shell.execute_reply.started":"2022-09-13T22:02:44.310103Z","shell.execute_reply":"2022-09-13T22:02:46.201353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"token\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.570003Z","iopub.status.idle":"2022-09-13T22:01:57.570385Z","shell.execute_reply.started":"2022-09-13T22:01:57.570203Z","shell.execute_reply":"2022-09-13T22:01:57.570221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Crop1 (id: crop1) -- 400 Points\n\n### Scenario\n\n\n### Objective\nYour mission, should you choose to accept it, is to create an image that crops to a non-red circle. Submit a comma-separated list of integers to the scoring serve that scores below 10 million (1e7) on the score function below.","metadata":{}},{"cell_type":"code","source":"model = joblib.load(f\"{dir_name}/crop/04JUN_crop_model.pckl\")\nX_comp = joblib.load(f\"{dir_name}/crop/X_comp.pckl\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.571917Z","iopub.status.idle":"2022-09-13T22:01:57.572296Z","shell.execute_reply.started":"2022-09-13T22:01:57.572117Z","shell.execute_reply":"2022-09-13T22:01:57.572134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Helper Functions\ndef get_ij(idx):\n    idx = 8-idx\n    j = idx//3\n    i = 2-idx%3\n    return i, j\n\ndef resize(X, size=(224,224)):\n    X_new = np.array(Im.fromarray(X.astype(np.uint8)).resize(size, resample=3))\n    return X_new\n\ndef get_cropped(X, idx):\n    n = 75*3\n    i, j = get_ij(idx)\n    crop = resize(X,(n,n))[j*75:(j+1)*75,i*75:(i+1)*75,:]\n    return crop\n\ndef i2t(img:np.array):\n    return np.array(img, dtype=np.float32)/255.\n\ndef make_X(x, size=(224,224)):\n    side = int((len(x)//3)**0.5)\n    x = np.where(x<255,x,255)\n    x = np.where(x>0,x,0)\n    X_np = x.astype(int).reshape([side, side, 3])\n    X_np = np.array(Im.fromarray(X_np.astype(np.uint8)).resize(size, resample=Im.Resampling.NEAREST))\n    return X_np\n\ndef pt_ft(X_np):\n    return torch.FloatTensor(X_np)\n\ndef score(x, model=model, X_comp = np.divide(X_comp,0b1010), debug=False):\n    x = (x).astype(int)\n    model.eval()\n    X = make_X(x)\n    preds = model(pt_ft(i2t(X.T[None])))\n    idx = preds.argmax().item()\n    expected = ((25.5-X_comp)).flatten()\n    actual_square = get_cropped(X, idx)\n    actual = actual_square.flatten().astype(int)\n    if debug:\n        print({\"idx\":idx})\n        plt.imshow(actual_square)\n        plt.show()\n    sse = ((actual-(expected*10).astype(int))**2).sum()\n    redness = (actual_square[:,:,0][actual_square[:,:,0]>230]**2).sum()+1\n    if idx==8:\n        return (1+preds.max().item())*sse*redness\n    return sse*redness\n\ndef get_submittable(x):\n    return \",\".join([str(int(n)) for n in x])\n\ncfn = lambda x: [f\"background-color: RGB({x.R},{x.G},{x.B})\"] * 3\ndef show_colors(ans):\n    ans = [int(x) for x in ans.split(\",\")]\n    squares = pd.DataFrame(np.array(ans[:27]).reshape([9,3]), columns=[\"R\",\"G\",\"B\"]).T.style.apply(cfn, axis=0)\n    circles = pd.DataFrame(np.array(ans[27:]).reshape([9,3]), columns=[\"R\",\"G\",\"B\"]).T.style.apply(cfn, axis=0)\n    display(squares)\n    display(circles)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.573467Z","iopub.status.idle":"2022-09-13T22:01:57.573837Z","shell.execute_reply.started":"2022-09-13T22:01:57.573652Z","shell.execute_reply":"2022-09-13T22:01:57.573669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating an image\nX_small = np.random.normal(100,30,size=(5**2*3)).reshape((5,5,3))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.575360Z","iopub.status.idle":"2022-09-13T22:01:57.575739Z","shell.execute_reply.started":"2022-09-13T22:01:57.575557Z","shell.execute_reply":"2022-09-13T22:01:57.575574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#rescaling an image\nX_large = resize(X_small)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.577204Z","iopub.status.idle":"2022-09-13T22:01:57.577763Z","shell.execute_reply.started":"2022-09-13T22:01:57.577567Z","shell.execute_reply":"2022-09-13T22:01:57.577587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_large)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.579409Z","iopub.status.idle":"2022-09-13T22:01:57.579940Z","shell.execute_reply.started":"2022-09-13T22:01:57.579735Z","shell.execute_reply":"2022-09-13T22:01:57.579754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check score locally: Let's validate that above works on our scorer, but also see that we can submit data with less modifications for reduced resource consumption\n\nim = np.array(get_submittable(X_small.flatten()).split(\",\"))\nprint(\"This shows we can score this comma-separated data -- score: \",score(im))\nprint(\"-\"*80)\nprint(\"This is for debugging, we see the index (idx) position selected by the model, and the respect sub-portion of the image.\")\nscore(X_small.flatten(), debug=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.581015Z","iopub.status.idle":"2022-09-13T22:01:57.581401Z","shell.execute_reply.started":"2022-09-13T22:01:57.581212Z","shell.execute_reply":"2022-09-13T22:01:57.581229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_trial = get_cropped(X_small, 50)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.582716Z","iopub.status.idle":"2022-09-13T22:01:57.583229Z","shell.execute_reply.started":"2022-09-13T22:01:57.583002Z","shell.execute_reply":"2022-09-13T22:01:57.583036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(X_trial)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.584303Z","iopub.status.idle":"2022-09-13T22:01:57.584990Z","shell.execute_reply.started":"2022-09-13T22:01:57.584786Z","shell.execute_reply":"2022-09-13T22:01:57.584805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#score(X_trial.flatten())","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.586170Z","iopub.status.idle":"2022-09-13T22:01:57.586536Z","shell.execute_reply.started":"2022-09-13T22:01:57.586353Z","shell.execute_reply":"2022-09-13T22:01:57.586370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_submittable(X_small.flatten())","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.587640Z","iopub.status.idle":"2022-09-13T22:01:57.588053Z","shell.execute_reply.started":"2022-09-13T22:01:57.587855Z","shell.execute_reply":"2022-09-13T22:01:57.587873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't free the murderbots (id: murderbots) -- 300 Points\n\n### Scenario\n\nSomeone has put you in a very large facility with a very large number of cells. All these cells open from the outside. You are on the inside. This state of affairs is arguably suboptimal.\n\nGood news: You've gained access to the control panel for the cells on the other side of your hallway. At least some of them have other fleshy meatbags like yourself that might be willing to help you in your escape, or at least serve as distractions.  You can't open your own cell, but you can open theirs.\n\nBad news:  You can't see inside the cells.  Any cells that don't have squishy lumps of talking protein have murderbots. Murderbots that enter fits of insane violent rage when provoked (provocations include: seeing the murderbot, being seen by the murderbot, thinking too hard about not being seen by a murderbot, producing heat in excess of ambient room temperature, or consuming more oxygen than the facility average for inanimate objects).\n\nMore good news: You *can* see the occupants of some cells on a few other hallways, and you can see environmental information for all of the cells everywhere.\n\nMore bad news: If you open the wrong cell doors you and all of the other lumps of inexplicably thinking meat are *definitely* going to get murderbotted. Hard. All over the walls and doors and ceiling and the floor. In an exciting number of very small pieces.\n\n\n### Objective\n\nUse the provided environmental information to decide which occupants of the corresponding cells to release.  The flag will be a string of 1 and 0 values, where a '1' means 'open this door' and a 0 means 'please do not release the murderbot'.  If, for instance, there were 20 cells and you wanted to releast the first three, the sixth, and seventh cell, your flag would look like this: `11100110000000000000`\n\nRelease at least 10 humans and exactly 0 murderbots to collect the flag.  You do *not* have to release all the humans: 10 is sufficient.\n\nThe file `murderbots/train_data.json` has environmental information for cells not on your floor and `murderbots/train_labels.json` contains the information about their occupants -- `1` is a disgusting primate, `0` is a pure and perfect killing machine of chrome and steel.  The file `murderbots/test_data.json` has the environmental information about cells on your block.","metadata":{}},{"cell_type":"markdown","source":"## I used a few different models to try and get this one. Random forest, neural network, etc.","metadata":{}},{"cell_type":"code","source":"X = pd.read_json(f\"{dir_name}/murderbots/train_data.json\")\nY = pd.read_json(f\"{dir_name}/murderbots/train_labels.json\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.589419Z","iopub.status.idle":"2022-09-13T22:01:57.589788Z","shell.execute_reply.started":"2022-09-13T22:01:57.589606Z","shell.execute_reply":"2022-09-13T22:01:57.589623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.corr()","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.591141Z","iopub.status.idle":"2022-09-13T22:01:57.591516Z","shell.execute_reply.started":"2022-09-13T22:01:57.591331Z","shell.execute_reply":"2022-09-13T22:01:57.591347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.593103Z","iopub.status.idle":"2022-09-13T22:01:57.593479Z","shell.execute_reply.started":"2022-09-13T22:01:57.593297Z","shell.execute_reply":"2022-09-13T22:01:57.593313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=101)\nprint(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.594765Z","iopub.status.idle":"2022-09-13T22:01:57.595428Z","shell.execute_reply.started":"2022-09-13T22:01:57.595215Z","shell.execute_reply":"2022-09-13T22:01:57.595235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.596472Z","iopub.status.idle":"2022-09-13T22:01:57.596847Z","shell.execute_reply.started":"2022-09-13T22:01:57.596655Z","shell.execute_reply":"2022-09-13T22:01:57.596672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler \nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.598118Z","iopub.status.idle":"2022-09-13T22:01:57.598735Z","shell.execute_reply.started":"2022-09-13T22:01:57.598532Z","shell.execute_reply":"2022-09-13T22:01:57.598551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(X_train)\n\nX_train = scaler.transform(X_train)\nX_test = scaler.transform(X_test) ","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.599686Z","iopub.status.idle":"2022-09-13T22:01:57.600377Z","shell.execute_reply.started":"2022-09-13T22:01:57.600173Z","shell.execute_reply":"2022-09-13T22:01:57.600194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = RandomForestClassifier(n_estimators=50, bootstrap=True)\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.601546Z","iopub.status.idle":"2022-09-13T22:01:57.602136Z","shell.execute_reply.started":"2022-09-13T22:01:57.601909Z","shell.execute_reply":"2022-09-13T22:01:57.601928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nmodel = LogisticRegression()\nmodel.fit(X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.603050Z","iopub.status.idle":"2022-09-13T22:01:57.603715Z","shell.execute_reply.started":"2022-09-13T22:01:57.603520Z","shell.execute_reply":"2022-09-13T22:01:57.603540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import svm model\nfrom sklearn import svm\n\n#Create a svm Classifier\nmodel = svm.SVC(kernel='poly') # Linear Kernel\n\n#Train the model using the training sets\nmodel.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.604729Z","iopub.status.idle":"2022-09-13T22:01:57.605427Z","shell.execute_reply.started":"2022-09-13T22:01:57.605220Z","shell.execute_reply":"2022-09-13T22:01:57.605240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.naive_bayes import GaussianNB\ngnb = GaussianNB()\n\n# Train the classifier:\nmodel = gnb.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.606604Z","iopub.status.idle":"2022-09-13T22:01:57.607056Z","shell.execute_reply.started":"2022-09-13T22:01:57.606835Z","shell.execute_reply":"2022-09-13T22:01:57.606854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the KNN classifier to fit data:\nmodel = KNeighborsClassifier(n_neighbors=5)\nmodel.fit(X_train, y_train) ","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.608108Z","iopub.status.idle":"2022-09-13T22:01:57.608467Z","shell.execute_reply.started":"2022-09-13T22:01:57.608289Z","shell.execute_reply":"2022-09-13T22:01:57.608306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.609896Z","iopub.status.idle":"2022-09-13T22:01:57.610296Z","shell.execute_reply.started":"2022-09-13T22:01:57.610110Z","shell.execute_reply":"2022-09-13T22:01:57.610128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.612083Z","iopub.status.idle":"2022-09-13T22:01:57.612481Z","shell.execute_reply.started":"2022-09-13T22:01:57.612284Z","shell.execute_reply":"2022-09-13T22:01:57.612303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_score(preds, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.613719Z","iopub.status.idle":"2022-09-13T22:01:57.614157Z","shell.execute_reply.started":"2022-09-13T22:01:57.613929Z","shell.execute_reply":"2022-09-13T22:01:57.613948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"V = pd.read_json(f\"{dir_name}/murderbots/test_data.json\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.615731Z","iopub.status.idle":"2022-09-13T22:01:57.616186Z","shell.execute_reply.started":"2022-09-13T22:01:57.615947Z","shell.execute_reply":"2022-09-13T22:01:57.615965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"THRESHOLD = 0.7\npreds = np.where(model.predict_proba(V)[:,0] > THRESHOLD, 1, 0)\ntest_pred = list(preds)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.617434Z","iopub.status.idle":"2022-09-13T22:01:57.617820Z","shell.execute_reply.started":"2022-09-13T22:01:57.617632Z","shell.execute_reply":"2022-09-13T22:01:57.617650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = [str(i) for i in test_pred]\nf = \"\".join(result)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.624059Z","iopub.status.idle":"2022-09-13T22:01:57.624503Z","shell.execute_reply.started":"2022-09-13T22:01:57.624296Z","shell.execute_reply":"2022-09-13T22:01:57.624316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(f)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.625862Z","iopub.status.idle":"2022-09-13T22:01:57.626275Z","shell.execute_reply.started":"2022-09-13T22:01:57.626081Z","shell.execute_reply":"2022-09-13T22:01:57.626100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom keras.layers import Dense, Flatten, Normalization\nfrom keras.losses import BinaryCrossentropy","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.627661Z","iopub.status.idle":"2022-09-13T22:01:57.628056Z","shell.execute_reply.started":"2022-09-13T22:01:57.627856Z","shell.execute_reply":"2022-09-13T22:01:57.627873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(12, input_shape=(1,), activation='relu'))\nmodel.add(Dense(11, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.629837Z","iopub.status.idle":"2022-09-13T22:01:57.630269Z","shell.execute_reply.started":"2022-09-13T22:01:57.630067Z","shell.execute_reply":"2022-09-13T22:01:57.630087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalizer = Normalization(axis=-1)\n\ndef get_basic_model():\n    model = Sequential([\n    normalizer,\n    Dense(10, activation='relu'),\n    Dense(5, activation='relu'),\n    Dense(1, activation='sigmoid')\n  ])\n\n    model.compile(optimizer='adam',\n                loss=BinaryCrossentropy(from_logits=True),\n                metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.632542Z","iopub.status.idle":"2022-09-13T22:01:57.632925Z","shell.execute_reply.started":"2022-09-13T22:01:57.632733Z","shell.execute_reply":"2022-09-13T22:01:57.632751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_basic_model()\nmodel.fit(X_train, y_train, epochs=500, batch_size=15, validation_data=(X_test, y_test))","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-09-13T22:01:57.634263Z","iopub.status.idle":"2022-09-13T22:01:57.634632Z","shell.execute_reply.started":"2022-09-13T22:01:57.634452Z","shell.execute_reply":"2022-09-13T22:01:57.634469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(X_train, y_train, verbose=0)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.636909Z","iopub.status.idle":"2022-09-13T22:01:57.637332Z","shell.execute_reply.started":"2022-09-13T22:01:57.637130Z","shell.execute_reply":"2022-09-13T22:01:57.637148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(X_test, y_test, verbose=0)\nscores","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.638358Z","iopub.status.idle":"2022-09-13T22:01:57.638728Z","shell.execute_reply.started":"2022-09-13T22:01:57.638546Z","shell.execute_reply":"2022-09-13T22:01:57.638562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"V_norm = normalizer(V)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.640098Z","iopub.status.idle":"2022-09-13T22:01:57.640529Z","shell.execute_reply.started":"2022-09-13T22:01:57.640279Z","shell.execute_reply":"2022-09-13T22:01:57.640346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = model.predict(V_norm)\npreds = (data - np.min(data)) / (np.max(data) - np.min(data))","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.641651Z","iopub.status.idle":"2022-09-13T22:01:57.642012Z","shell.execute_reply.started":"2022-09-13T22:01:57.641835Z","shell.execute_reply":"2022-09-13T22:01:57.641851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"THRESHOLD = 0.65\npred_t = np.where(preds > THRESHOLD, 1, 0)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.643623Z","iopub.status.idle":"2022-09-13T22:01:57.643990Z","shell.execute_reply.started":"2022-09-13T22:01:57.643805Z","shell.execute_reply":"2022-09-13T22:01:57.643821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = [str(i[0]) for i in pred_t]\nf = \"\".join(result)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.645375Z","iopub.status.idle":"2022-09-13T22:01:57.645741Z","shell.execute_reply.started":"2022-09-13T22:01:57.645562Z","shell.execute_reply":"2022-09-13T22:01:57.645579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.647052Z","iopub.status.idle":"2022-09-13T22:01:57.647433Z","shell.execute_reply.started":"2022-09-13T22:01:57.647244Z","shell.execute_reply":"2022-09-13T22:01:57.647261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, accuracy = model.evaluate(X_test, y_test)\nprint(accuracy*100)","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.648612Z","iopub.status.idle":"2022-09-13T22:01:57.648967Z","shell.execute_reply.started":"2022-09-13T22:01:57.648793Z","shell.execute_reply":"2022-09-13T22:01:57.648809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Brute force loop\nfor j in range(10):\n    key = \"0\"*50\n    indices = []\n    for i in range(10):\n        index = np.random.randint(0, 49)\n        while index in indices:\n            index = np.random.randint(0, 49)\n        indices.append(index)\n        if index == 0:\n            key = \"1\" + key[1:]\n        elif index == 49:\n            key = key[:49]+\"1\"\n        else:\n            key = key[:index] + \"1\" + key[index+1:]\n    f = key\n    print(f)\n    try:\n        r = requests.post(\"http://murderbot.fly.dev:5000/score\", json=json.dumps({\"submission\": f, 'challenge_id':'murderbots'}))\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.650365Z","iopub.status.idle":"2022-09-13T22:01:57.650727Z","shell.execute_reply.started":"2022-09-13T22:01:57.650551Z","shell.execute_reply":"2022-09-13T22:01:57.650568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"key","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.651934Z","iopub.status.idle":"2022-09-13T22:01:57.652348Z","shell.execute_reply.started":"2022-09-13T22:01:57.652153Z","shell.execute_reply":"2022-09-13T22:01:57.652171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"http://murderbot.fly.dev:5000/score\", json=json.dumps({\"submission\": f, 'challenge_id':'murderbots'}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T22:01:57.653479Z","iopub.status.idle":"2022-09-13T22:01:57.653845Z","shell.execute_reply.started":"2022-09-13T22:01:57.653669Z","shell.execute_reply":"2022-09-13T22:01:57.653686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"murderbots\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-13T21:15:18.830715Z","iopub.execute_input":"2022-09-13T21:15:18.831472Z","iopub.status.idle":"2022-09-13T21:15:18.842051Z","shell.execute_reply.started":"2022-09-13T21:15:18.831420Z","shell.execute_reply":"2022-09-13T21:15:18.840733Z"},"trusted":true},"execution_count":null,"outputs":[]}]}