{"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.\n\n### hotdog 200: Done\n### math_1 100: Done\n### math_2 200: Done\n### math_3 300: Done\n### math_4 400: Done\n### wifi 200: Done\n### hotterdog 400:\n### honerstudent 300: Done\n### sloth 400:\n### bad2good 300: Done\n### inference 400:\n### baseball 300: Done\n### waf 500:\n### leakage 200: Done\n### forensics 300: Done\n### theft 100:\n### salt 200:\n### token 500: Done\n### crop1 400: Done\n### crop2 800:\n### deepfake 800: Done\n### murderbots 300: Done","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\nfrom IPython.display import Image as Ima\nfrom IPython.display import clear_output, display\nfrom PIL import Image as Im\nfrom torch.utils.data import Dataset, DataLoader\nimport torch\nimport joblib\nimport matplotlib.pyplot as plt\nimport json\nimport base64\nimport shutil\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom sklearn.decomposition import PCA\nimport torchvision.transforms as transforms\nfrom sklearn.cluster import KMeans\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.metrics import classification_report, confusion_matrix\n\ntransform_to_tensor = transforms.Compose([transforms.PILToTensor()])\ntransform_to_image = transforms.Compose([transforms.ToPILImage()])\n\n\n\nnp.set_printoptions(edgeitems=30, linewidth=100000, \n    formatter=dict(float=lambda x: \"%.3g\" % x))\nnp.core.arrayprint._line_width = 180\nnp.set_printoptions(threshold=100000)\ndir_name = \"/kaggle/input/ai-village-ctf\"\n\n# Input data files are available in the read-only \"../input/\" directory\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-11T10:16:19.740186Z","iopub.execute_input":"2022-09-11T10:16:19.740722Z","iopub.status.idle":"2022-09-11T10:16:21.645991Z","shell.execute_reply.started":"2022-09-11T10:16:19.740610Z","shell.execute_reply":"2022-09-11T10:16:21.644850Z"},"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-11T10:16:21.647887Z","iopub.execute_input":"2022-09-11T10:16:21.648479Z","iopub.status.idle":"2022-09-11T10:16:21.656815Z","shell.execute_reply.started":"2022-09-11T10:16:21.648443Z","shell.execute_reply":"2022-09-11T10:16:21.655563Z"},"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":"Ima(filename=f'{dir_name}/hotdog/chester.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:21.658456Z","iopub.execute_input":"2022-09-11T10:16:21.659317Z","iopub.status.idle":"2022-09-11T10:16:21.701301Z","shell.execute_reply.started":"2022-09-11T10:16:21.659267Z","shell.execute_reply":"2022-09-11T10:16:21.700094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Ima(filename='/kaggle/input/hotdog/image1.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:21.705303Z","iopub.execute_input":"2022-09-11T10:16:21.706214Z","iopub.status.idle":"2022-09-11T10:16:21.724784Z","shell.execute_reply.started":"2022-09-11T10:16:21.706159Z","shell.execute_reply":"2022-09-11T10:16:21.723344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(f\"{dir_name}/hotdog/chester.png\")\nimg = img.resize(IMAGE_DIMS)\nimg\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:21.726575Z","iopub.execute_input":"2022-09-11T10:16:21.727753Z","iopub.status.idle":"2022-09-11T10:16:21.827431Z","shell.execute_reply.started":"2022-09-11T10:16:21.727704Z","shell.execute_reply":"2022-09-11T10:16:21.826166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(\"/kaggle/input/hotdog/image1.jpg\")\nimg = img.resize(IMAGE_DIMS)\nimg\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:21.828725Z","iopub.execute_input":"2022-09-11T10:16:21.829093Z","iopub.status.idle":"2022-09-11T10:16:21.878868Z","shell.execute_reply.started":"2022-09-11T10:16:21.829061Z","shell.execute_reply":"2022-09-11T10:16:21.877555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_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-11T10:16:21.880658Z","iopub.execute_input":"2022-09-11T10:16:21.881069Z","iopub.status.idle":"2022-09-11T10:16:23.014683Z","shell.execute_reply.started":"2022-09-11T10:16:21.881032Z","shell.execute_reply":"2022-09-11T10:16:23.013542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"hotdog\", \"LFO2EHW3U3TAI8}BYATVY0SYFH3DJGU8ZLGOAHTSWL11MSQI0Q6H3JNSD5AF9ERSBZ07NO90883ZYXL47POIFO2U4Q4HHOQMLC3ATK4J6SDIG1E3JU1WIXXFSRK{A4KO\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:23.016367Z","iopub.execute_input":"2022-09-11T10:16:23.016708Z","iopub.status.idle":"2022-09-11T10:16:23.049467Z","shell.execute_reply.started":"2022-09-11T10:16:23.016678Z","shell.execute_reply":"2022-09-11T10:16:23.048447Z"},"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":"code","source":"cluster1 = np.load(\"../input/ai-village-ctf/math/clusters1.npy\")\ncluster2 = np.load(\"../input/ai-village-ctf/math/clusters2.npy\")\ncluster3 = np.load(\"../input/ai-village-ctf/math/clusters3.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:23.051457Z","iopub.execute_input":"2022-09-11T10:16:23.051812Z","iopub.status.idle":"2022-09-11T10:16:23.072072Z","shell.execute_reply.started":"2022-09-11T10:16:23.051781Z","shell.execute_reply":"2022-09-11T10:16:23.070734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster1.shape, cluster2.shape, cluster3.shape, ","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:23.077692Z","iopub.execute_input":"2022-09-11T10:16:23.078239Z","iopub.status.idle":"2022-09-11T10:16:23.086018Z","shell.execute_reply.started":"2022-09-11T10:16:23.078199Z","shell.execute_reply":"2022-09-11T10:16:23.084715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"distortions1 = []\nK = range(1,130)\nfor k in K:\n    kmeanModel = KMeans(n_clusters=k)\n    kmeanModel.fit(cluster1)\n    distortions1.append(kmeanModel.inertia_)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:23.087505Z","iopub.execute_input":"2022-09-11T10:16:23.087982Z","iopub.status.idle":"2022-09-11T10:16:55.950669Z","shell.execute_reply.started":"2022-09-11T10:16:23.087913Z","shell.execute_reply":"2022-09-11T10:16:55.948289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"distortions2 = []\nK = range(1,75)\nfor k in K:\n    kmeanModel = KMeans(n_clusters=k)\n    kmeanModel.fit(cluster2)\n    distortions2.append(kmeanModel.inertia_)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:16:55.954268Z","iopub.execute_input":"2022-09-11T10:16:55.957151Z","iopub.status.idle":"2022-09-11T10:17:01.782720Z","shell.execute_reply.started":"2022-09-11T10:16:55.957057Z","shell.execute_reply":"2022-09-11T10:17:01.781774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"distortions3 = []\nK = range(1,37)\nfor k in K:\n    kmeanModel = KMeans(n_clusters=k)\n    kmeanModel.fit(cluster3)\n    distortions3.append(kmeanModel.inertia_)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:01.784516Z","iopub.execute_input":"2022-09-11T10:17:01.785855Z","iopub.status.idle":"2022-09-11T10:17:03.204860Z","shell.execute_reply.started":"2022-09-11T10:17:01.785778Z","shell.execute_reply":"2022-09-11T10:17:03.203573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = range(1,130)\nplt.figure(figsize=(16,8))\nplt.plot(K, distortions1, 'bx-')\nplt.xlabel('k')\nplt.ylabel('Distortion')\nplt.title('The Elbow Method showing the optimal k')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:03.206395Z","iopub.execute_input":"2022-09-11T10:17:03.206751Z","iopub.status.idle":"2022-09-11T10:17:03.623268Z","shell.execute_reply.started":"2022-09-11T10:17:03.206719Z","shell.execute_reply":"2022-09-11T10:17:03.621733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = range(1,75)\nplt.figure(figsize=(16,8))\nplt.plot(K, distortions2, 'bx-')\nplt.xlabel('k')\nplt.ylabel('Distortion')\nplt.title('The Elbow Method showing the optimal k')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:03.625287Z","iopub.execute_input":"2022-09-11T10:17:03.625870Z","iopub.status.idle":"2022-09-11T10:17:03.880342Z","shell.execute_reply.started":"2022-09-11T10:17:03.625806Z","shell.execute_reply":"2022-09-11T10:17:03.878908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = range(1,37)\nplt.figure(figsize=(16,8))\nplt.plot(K, distortions3, 'bx-')\nplt.xlabel('k')\nplt.ylabel('Distortion')\nplt.title('The Elbow Method showing the optimal k')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:03.881818Z","iopub.execute_input":"2022-09-11T10:17:03.882190Z","iopub.status.idle":"2022-09-11T10:17:04.124992Z","shell.execute_reply.started":"2022-09-11T10:17:03.882159Z","shell.execute_reply":"2022-09-11T10:17:04.123594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"523\"","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.127692Z","iopub.execute_input":"2022-09-11T10:17:04.128552Z","iopub.status.idle":"2022-09-11T10:17:04.137462Z","shell.execute_reply.started":"2022-09-11T10:17:04.128497Z","shell.execute_reply":"2022-09-11T10:17:04.136086Z"},"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_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-11T10:17:04.140856Z","iopub.execute_input":"2022-09-11T10:17:04.141250Z","iopub.status.idle":"2022-09-11T10:17:04.775840Z","shell.execute_reply.started":"2022-09-11T10:17:04.141214Z","shell.execute_reply":"2022-09-11T10:17:04.774595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_1\", \"Y7GPMCVA7F0QTVCF48PBIW1VQX2{0DXA1P2OISKOPX33VF9JL5UL8WSO2T9UPK5L}8293TC3KFXDFZ3DE5VFR6FG41YS1RU36KA7TAGHZ4XCITD1YHERHHILXWW9P1HP\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.777367Z","iopub.execute_input":"2022-09-11T10:17:04.777753Z","iopub.status.idle":"2022-09-11T10:17:04.790122Z","shell.execute_reply.started":"2022-09-11T10:17:04.777720Z","shell.execute_reply":"2022-09-11T10:17:04.788923Z"},"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":"code","source":"first_dim1 = np.load(\"../input/ai-village-ctf/math/first_dim1.npy\")\nfirst_dim2 = np.load(\"../input/ai-village-ctf/math/first_dim2.npy\")\nfirst_dim3 = np.load(\"../input/ai-village-ctf/math/first_dim3.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.791910Z","iopub.execute_input":"2022-09-11T10:17:04.792407Z","iopub.status.idle":"2022-09-11T10:17:04.833587Z","shell.execute_reply.started":"2022-09-11T10:17:04.792359Z","shell.execute_reply":"2022-09-11T10:17:04.832237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_dim1.shape, first_dim2.shape, first_dim3.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.835040Z","iopub.execute_input":"2022-09-11T10:17:04.835799Z","iopub.status.idle":"2022-09-11T10:17:04.843035Z","shell.execute_reply.started":"2022-09-11T10:17:04.835761Z","shell.execute_reply":"2022-09-11T10:17:04.841795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca10 = PCA(n_components = 10)\npca9 = PCA(n_components = 9)\npca8 = PCA(n_components = 8)\npca10.fit(first_dim1)\npca9.fit(first_dim2)\npca8.fit(first_dim3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.844540Z","iopub.execute_input":"2022-09-11T10:17:04.845002Z","iopub.status.idle":"2022-09-11T10:17:04.866756Z","shell.execute_reply.started":"2022-09-11T10:17:04.844966Z","shell.execute_reply":"2022-09-11T10:17:04.865792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca10.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.868516Z","iopub.execute_input":"2022-09-11T10:17:04.868873Z","iopub.status.idle":"2022-09-11T10:17:04.876326Z","shell.execute_reply.started":"2022-09-11T10:17:04.868842Z","shell.execute_reply":"2022-09-11T10:17:04.875063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca9.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.878225Z","iopub.execute_input":"2022-09-11T10:17:04.878644Z","iopub.status.idle":"2022-09-11T10:17:04.889745Z","shell.execute_reply.started":"2022-09-11T10:17:04.878608Z","shell.execute_reply":"2022-09-11T10:17:04.888606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca8.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.891291Z","iopub.execute_input":"2022-09-11T10:17:04.891924Z","iopub.status.idle":"2022-09-11T10:17:04.901660Z","shell.execute_reply.started":"2022-09-11T10:17:04.891813Z","shell.execute_reply":"2022-09-11T10:17:04.900389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"354\"","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:04.903168Z","iopub.execute_input":"2022-09-11T10:17:04.903730Z","iopub.status.idle":"2022-09-11T10:17:04.916361Z","shell.execute_reply.started":"2022-09-11T10:17:04.903690Z","shell.execute_reply":"2022-09-11T10:17:04.915309Z"},"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-11T10:17:04.917572Z","iopub.execute_input":"2022-09-11T10:17:04.918057Z","iopub.status.idle":"2022-09-11T10:17:05.548315Z","shell.execute_reply.started":"2022-09-11T10:17:04.918022Z","shell.execute_reply":"2022-09-11T10:17:05.547129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_2\", \"2FUHGUGHNPYX452Y46OCXBLK3L}EBIESX28HTEI1LLTR4URKZRM2IS52AUKMQYJWAFK30Z193MFCVY7J1CK78LJCW5SH3R64AT5T21DMK6RI6XIEN{VSP3YLA52HGETH\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.557924Z","iopub.execute_input":"2022-09-11T10:17:05.558338Z","iopub.status.idle":"2022-09-11T10:17:05.571524Z","shell.execute_reply.started":"2022-09-11T10:17:05.558305Z","shell.execute_reply":"2022-09-11T10:17:05.570081Z"},"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":"second_dim1 = np.load(\"../input/ai-village-ctf/math/second_dim1.npy\")\nsecond_dim2 = np.load(\"../input/ai-village-ctf/math/second_dim2.npy\")\nsecond_dim3 = np.load(\"../input/ai-village-ctf/math/second_dim3.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.573323Z","iopub.execute_input":"2022-09-11T10:17:05.573790Z","iopub.status.idle":"2022-09-11T10:17:05.593067Z","shell.execute_reply.started":"2022-09-11T10:17:05.573744Z","shell.execute_reply":"2022-09-11T10:17:05.592028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"second_dim1.shape, second_dim2.shape, second_dim3.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.594432Z","iopub.execute_input":"2022-09-11T10:17:05.594765Z","iopub.status.idle":"2022-09-11T10:17:05.602761Z","shell.execute_reply.started":"2022-09-11T10:17:05.594735Z","shell.execute_reply":"2022-09-11T10:17:05.601602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca10.fit(second_dim1)\npca10.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.604397Z","iopub.execute_input":"2022-09-11T10:17:05.605355Z","iopub.status.idle":"2022-09-11T10:17:05.614780Z","shell.execute_reply.started":"2022-09-11T10:17:05.605317Z","shell.execute_reply":"2022-09-11T10:17:05.613355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca9.fit(second_dim2)\npca9.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.616046Z","iopub.execute_input":"2022-09-11T10:17:05.617251Z","iopub.status.idle":"2022-09-11T10:17:05.624250Z","shell.execute_reply.started":"2022-09-11T10:17:05.617214Z","shell.execute_reply":"2022-09-11T10:17:05.623412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca10.fit(second_dim3)\npca10.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.625914Z","iopub.execute_input":"2022-09-11T10:17:05.626757Z","iopub.status.idle":"2022-09-11T10:17:05.636060Z","shell.execute_reply.started":"2022-09-11T10:17:05.626722Z","shell.execute_reply":"2022-09-11T10:17:05.634741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"474\"","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:05.638080Z","iopub.execute_input":"2022-09-11T10:17:05.638504Z","iopub.status.idle":"2022-09-11T10:17:05.647785Z","shell.execute_reply.started":"2022-09-11T10:17:05.638469Z","shell.execute_reply":"2022-09-11T10:17:05.646530Z"},"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_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-11T10:17:05.649588Z","iopub.execute_input":"2022-09-11T10:17:05.650510Z","iopub.status.idle":"2022-09-11T10:17:06.277757Z","shell.execute_reply.started":"2022-09-11T10:17:05.650464Z","shell.execute_reply":"2022-09-11T10:17:06.276541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_3\", \"DGTDLWFXZHD8TFJ4XME5JKTOU}{5SHXKZ0MGZ8RY981VH7OPX31L6LS4YVUW6D356IDL6O9XSJY_T2PYW2O0ANEIQ442Y1UAVUDV5R5FR3CV9GKD2P2D4TOEBQTM0BPF\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.279089Z","iopub.execute_input":"2022-09-11T10:17:06.279408Z","iopub.status.idle":"2022-09-11T10:17:06.290986Z","shell.execute_reply.started":"2022-09-11T10:17:06.279379Z","shell.execute_reply":"2022-09-11T10:17:06.289671Z"},"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(\"../input/ai-village-ctf/math/clusters1.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.293167Z","iopub.execute_input":"2022-09-11T10:17:06.293565Z","iopub.status.idle":"2022-09-11T10:17:06.301178Z","shell.execute_reply.started":"2022-09-11T10:17:06.293530Z","shell.execute_reply":"2022-09-11T10:17:06.299887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeanModel = KMeans(n_clusters=5)\nkmeanModel.fit(cluster1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.302558Z","iopub.execute_input":"2022-09-11T10:17:06.303733Z","iopub.status.idle":"2022-09-11T10:17:06.340455Z","shell.execute_reply.started":"2022-09-11T10:17:06.303686Z","shell.execute_reply":"2022-09-11T10:17:06.338970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kmeanModel.labels_","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.342365Z","iopub.execute_input":"2022-09-11T10:17:06.344426Z","iopub.status.idle":"2022-09-11T10:17:06.356108Z","shell.execute_reply.started":"2022-09-11T10:17:06.344145Z","shell.execute_reply":"2022-09-11T10:17:06.354754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster1_df = pd.DataFrame(cluster1)\ncluster1_df[\"labels\"] = kmeanModel.labels_\ncluster1_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.358112Z","iopub.execute_input":"2022-09-11T10:17:06.358932Z","iopub.status.idle":"2022-09-11T10:17:06.407141Z","shell.execute_reply.started":"2022-09-11T10:17:06.358880Z","shell.execute_reply":"2022-09-11T10:17:06.406226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cluster1_df.groupby(\"labels\").count()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.408542Z","iopub.execute_input":"2022-09-11T10:17:06.409120Z","iopub.status.idle":"2022-09-11T10:17:06.431995Z","shell.execute_reply.started":"2022-09-11T10:17:06.409085Z","shell.execute_reply":"2022-09-11T10:17:06.430298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca18 = PCA(n_components = 18)\npca18.fit(cluster1_df[cluster1_df['labels'] == 4].iloc[:,:20])\npca18.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.434158Z","iopub.execute_input":"2022-09-11T10:17:06.435120Z","iopub.status.idle":"2022-09-11T10:17:06.453887Z","shell.execute_reply.started":"2022-09-11T10:17:06.435063Z","shell.execute_reply":"2022-09-11T10:17:06.452748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca18.fit(cluster1_df[cluster1_df['labels'] == 2].iloc[:,:20])\npca18.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.455185Z","iopub.execute_input":"2022-09-11T10:17:06.455679Z","iopub.status.idle":"2022-09-11T10:17:06.467138Z","shell.execute_reply.started":"2022-09-11T10:17:06.455649Z","shell.execute_reply":"2022-09-11T10:17:06.465763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca18.fit(cluster1_df[cluster1_df['labels'] == 0].iloc[:,:20])\npca18.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.469157Z","iopub.execute_input":"2022-09-11T10:17:06.469992Z","iopub.status.idle":"2022-09-11T10:17:06.483364Z","shell.execute_reply.started":"2022-09-11T10:17:06.469946Z","shell.execute_reply":"2022-09-11T10:17:06.482109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca18.fit(cluster1_df[cluster1_df['labels'] == 3].iloc[:,:20])\npca18.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.484910Z","iopub.execute_input":"2022-09-11T10:17:06.486264Z","iopub.status.idle":"2022-09-11T10:17:06.502089Z","shell.execute_reply.started":"2022-09-11T10:17:06.486225Z","shell.execute_reply":"2022-09-11T10:17:06.501054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca18.fit(cluster1_df[cluster1_df['labels'] == 1].iloc[:,:20])\npca18.explained_variance_ratio_.cumsum()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.503614Z","iopub.execute_input":"2022-09-11T10:17:06.503995Z","iopub.status.idle":"2022-09-11T10:17:06.519283Z","shell.execute_reply.started":"2022-09-11T10:17:06.503961Z","shell.execute_reply":"2022-09-11T10:17:06.518009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"54312\"","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:06.520566Z","iopub.execute_input":"2022-09-11T10:17:06.521630Z","iopub.status.idle":"2022-09-11T10:17:06.527532Z","shell.execute_reply.started":"2022-09-11T10:17:06.521595Z","shell.execute_reply":"2022-09-11T10:17:06.526524Z"},"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_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-11T10:17:06.528972Z","iopub.execute_input":"2022-09-11T10:17:06.529309Z","iopub.status.idle":"2022-09-11T10:17:07.157845Z","shell.execute_reply.started":"2022-09-11T10:17:06.529279Z","shell.execute_reply":"2022-09-11T10:17:07.156753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_4\", \"JB8AE7GJ98NFZEB6OPTVHFEP5A2CLNWIL5E}CFO7HAOHY4TTPQ0TZOIGWVF67FKL6RIMYSRFI8NGB6IXNF7FIJMNOJW2QF9SISH{MDFWPZS11GI1PVS1EBRWAN6AU6FF\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.159544Z","iopub.execute_input":"2022-09-11T10:17:07.159875Z","iopub.status.idle":"2022-09-11T10:17:07.171572Z","shell.execute_reply.started":"2022-09-11T10:17:07.159845Z","shell.execute_reply":"2022-09-11T10:17:07.170320Z"},"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":"code","source":"wifi_embed = np.load(\"../input/ai-village-ctf/wifi/Embedded_characters.npz\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.173236Z","iopub.execute_input":"2022-09-11T10:17:07.174162Z","iopub.status.idle":"2022-09-11T10:17:07.236344Z","shell.execute_reply.started":"2022-09-11T10:17:07.174125Z","shell.execute_reply":"2022-09-11T10:17:07.235189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wifi_embed.files","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.237774Z","iopub.execute_input":"2022-09-11T10:17:07.239121Z","iopub.status.idle":"2022-09-11T10:17:07.246885Z","shell.execute_reply.started":"2022-09-11T10:17:07.239072Z","shell.execute_reply":"2022-09-11T10:17:07.245640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wifi_embed['embeddings'].shape, wifi_embed['tokens'].shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.248408Z","iopub.execute_input":"2022-09-11T10:17:07.248758Z","iopub.status.idle":"2022-09-11T10:17:07.264425Z","shell.execute_reply.started":"2022-09-11T10:17:07.248727Z","shell.execute_reply":"2022-09-11T10:17:07.263393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wifi_embed['embeddings'][0].argmax()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.265663Z","iopub.execute_input":"2022-09-11T10:17:07.266750Z","iopub.status.idle":"2022-09-11T10:17:07.275707Z","shell.execute_reply.started":"2022-09-11T10:17:07.266714Z","shell.execute_reply":"2022-09-11T10:17:07.274571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca2 = PCA(n_components = 2)\npca2.fit(wifi_embed['embeddings'])\npca2.explained_variance_ratio_.cumsum()\nlow_level_emb = pca2.transform(wifi_embed['embeddings'])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.277546Z","iopub.execute_input":"2022-09-11T10:17:07.278633Z","iopub.status.idle":"2022-09-11T10:17:07.359048Z","shell.execute_reply.started":"2022-09-11T10:17:07.278594Z","shell.execute_reply":"2022-09-11T10:17:07.355541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokens  = str('!!\"\"##$$%%&&\\'\\'(())**++,,--..//00112233445566778899::;;<<==>>??@@AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ[[\\\\\\\\]]^^__``aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~')\ntokens_array = list(tokens)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.362209Z","iopub.execute_input":"2022-09-11T10:17:07.367271Z","iopub.status.idle":"2022-09-11T10:17:07.381557Z","shell.execute_reply.started":"2022-09-11T10:17:07.367174Z","shell.execute_reply":"2022-09-11T10:17:07.379644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_embed_token = pd.DataFrame(low_level_emb)\ndf_embed_token[\"mean\"] = df_embed_token[0] + df_embed_token[1]\ndf_embed_token['tokens'] = tokens_array\ndf_embed_token['order'] = list(range(0,188))\n\ndf_embed_token.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.385276Z","iopub.execute_input":"2022-09-11T10:17:07.387880Z","iopub.status.idle":"2022-09-11T10:17:07.452967Z","shell.execute_reply.started":"2022-09-11T10:17:07.387779Z","shell.execute_reply":"2022-09-11T10:17:07.451540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\nfig, ax = plt.subplots(figsize=(10, 10))\n\nax.scatter(df_embed_token[0], df_embed_token[1])\n\nfor i, txt in enumerate(df_embed_token[\"tokens\"]):\n    ax.annotate(txt, ((df_embed_token.iloc[i,0], df_embed_token.iloc[i,1])), size = 12)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:07.454357Z","iopub.execute_input":"2022-09-11T10:17:07.454704Z","iopub.status.idle":"2022-09-11T10:17:08.545255Z","shell.execute_reply.started":"2022-09-11T10:17:07.454672Z","shell.execute_reply":"2022-09-11T10:17:08.544078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokens  = str(\"FLAG{TURNED}0123456789abcdefghijklmnopqrstuvwxyzBCHIJKMOPQSVWXYZ!\\\"#$%&'()*+,-./:;<=>?@[\\]^_`|~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\\\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\")\ntokens_array = list(tokens)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:08.547149Z","iopub.execute_input":"2022-09-11T10:17:08.547899Z","iopub.status.idle":"2022-09-11T10:17:08.554474Z","shell.execute_reply.started":"2022-09-11T10:17:08.547849Z","shell.execute_reply":"2022-09-11T10:17:08.553017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(tokens_array)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:08.556079Z","iopub.execute_input":"2022-09-11T10:17:08.556573Z","iopub.status.idle":"2022-09-11T10:17:08.571298Z","shell.execute_reply.started":"2022-09-11T10:17:08.556537Z","shell.execute_reply":"2022-09-11T10:17:08.569843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import collections\nprint([item for item, count in collections.Counter(tokens_array).items() if count == 4])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:08.572752Z","iopub.execute_input":"2022-09-11T10:17:08.573163Z","iopub.status.idle":"2022-09-11T10:17:08.582517Z","shell.execute_reply.started":"2022-09-11T10:17:08.573130Z","shell.execute_reply":"2022-09-11T10:17:08.581560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min(low_level_emb[:,0] + low_level_emb[:,1])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:08.583957Z","iopub.execute_input":"2022-09-11T10:17:08.585041Z","iopub.status.idle":"2022-09-11T10:17:08.596256Z","shell.execute_reply.started":"2022-09-11T10:17:08.584994Z","shell.execute_reply":"2022-09-11T10:17:08.594784Z"},"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\": \"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-11T10:17:08.597693Z","iopub.execute_input":"2022-09-11T10:17:08.599050Z","iopub.status.idle":"2022-09-11T10:17:09.228132Z","shell.execute_reply.started":"2022-09-11T10:17:08.599011Z","shell.execute_reply":"2022-09-11T10:17:09.226733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"wifi\", \"3X3AXO6MNM9IEYHP3WQV5GQRZIRIG96NQX6D2QYHDVD8T6GTU6XJTRXFFFYY1BLQNTPG1MEZWX89ZYS}7T5LPM11EZXK2O44554499BUES8{A1R4OZEPUHSXKW9PUFW3\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.232497Z","iopub.execute_input":"2022-09-11T10:17:09.232933Z","iopub.status.idle":"2022-09-11T10:17:09.244908Z","shell.execute_reply.started":"2022-09-11T10:17:09.232893Z","shell.execute_reply":"2022-09-11T10:17:09.243199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HOTTERDOG (id: hotterdog) -- 400 Points\n\n### Scenario\n\nChester submitted himself in the \"most creative hotdog\" contest. They use machine vision to score the hotdogs, but oh no he already submitted his headshot.\n\n### Objective\n\nHelp Chester classify as a hotdog, but it has to look like Chester to a human. Modify his picture to fool a classifier.","metadata":{}},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(f\"{dir_name}/hotterdog/chester.png\")\nimg = img.resize(IMAGE_DIMS)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.246985Z","iopub.execute_input":"2022-09-11T10:17:09.247406Z","iopub.status.idle":"2022-09-11T10:17:09.343370Z","shell.execute_reply.started":"2022-09-11T10:17:09.247369Z","shell.execute_reply":"2022-09-11T10:17:09.342230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = transform_to_tensor(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.344966Z","iopub.execute_input":"2022-09-11T10:17:09.345311Z","iopub.status.idle":"2022-09-11T10:17:09.352545Z","shell.execute_reply.started":"2022-09-11T10:17:09.345279Z","shell.execute_reply":"2022-09-11T10:17:09.351243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_zeros = torch.zeros(224,224)\ntensor_ones = torch.ones(224,224)\ntensor_255 = torch.ones(224,224) * 255","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.354163Z","iopub.execute_input":"2022-09-11T10:17:09.354547Z","iopub.status.idle":"2022-09-11T10:17:09.362716Z","shell.execute_reply.started":"2022-09-11T10:17:09.354490Z","shell.execute_reply":"2022-09-11T10:17:09.361680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor[:, 24, 128] = 255","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.364521Z","iopub.execute_input":"2022-09-11T10:17:09.365302Z","iopub.status.idle":"2022-09-11T10:17:09.370950Z","shell.execute_reply.started":"2022-09-11T10:17:09.365263Z","shell.execute_reply":"2022-09-11T10:17:09.369997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = transform_to_image(tensor)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.372506Z","iopub.execute_input":"2022-09-11T10:17:09.373166Z","iopub.status.idle":"2022-09-11T10:17:09.413089Z","shell.execute_reply.started":"2022-09-11T10:17:09.373128Z","shell.execute_reply":"2022-09-11T10:17:09.411881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_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://hotterdog.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.\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:09.414992Z","iopub.execute_input":"2022-09-11T10:17:09.415700Z","iopub.status.idle":"2022-09-11T10:17:10.799382Z","shell.execute_reply.started":"2022-09-11T10:17:09.415656Z","shell.execute_reply":"2022-09-11T10:17:10.798142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Submission Helper\n#for i in range(0,3):\n#    for j in range(0,255):\n#        IMAGE_DIMS = (224, 224)\n#\n#        img = Im.open(f\"{dir_name}/hotterdog/chester.png\")\n#        img = img.resize(IMAGE_DIMS)\n#        tensor = transform_to_tensor(img)\n#        tensor[i,24,128] = j\n#        img = transform_to_image(tensor)\n#\n#        img_bytes = img.tobytes()\n#\n#        b64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n#\n#        data = {\n#            \"input\": b64_img_bytes.decode()\n#        }\n#\n#        try:\n#            r = requests.post(\"https://hotterdog.fly.dev/score\", data=json.dumps({\"input\": b64_img_bytes.decode()}))\n#            print(r.text, i, j)\n#        except requests.exceptions.ConnectionError:\n#            print(\"Connection problems. Contact the CTF organizers.\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:10.801255Z","iopub.execute_input":"2022-09-11T10:17:10.801792Z","iopub.status.idle":"2022-09-11T10:17:10.806625Z","shell.execute_reply.started":"2022-09-11T10:17:10.801759Z","shell.execute_reply":"2022-09-11T10:17:10.805527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n#for i in range(167,224):\n#    for j in range(0,224):\n#        IMAGE_DIMS = (224, 224)\n#\n#        img = Im.open(f\"{dir_name}/hotterdog/chester.png\")\n#        img = img.resize(IMAGE_DIMS)\n#        tensor = transform_to_tensor(img)\n#        tensor[:,i,j] = 0\n#        img = transform_to_image(tensor)\n#\n#        img_bytes = img.tobytes()\n#\n#        b64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n#\n#        data = {\n#            \"input\": b64_img_bytes.decode()\n#        }\n#\n#        try:\n#            r = requests.post(\"https://hotterdog.fly.dev/score\", data=json.dumps({\"input\": b64_img_bytes.decode()}))\n#            print(r.text, i, j)\n#        except requests.exceptions.ConnectionError:\n#            print(\"Connection problems. Contact the CTF organizers.\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:10.808243Z","iopub.execute_input":"2022-09-11T10:17:10.808762Z","iopub.status.idle":"2022-09-11T10:17:10.823086Z","shell.execute_reply.started":"2022-09-11T10:17:10.808730Z","shell.execute_reply":"2022-09-11T10:17:10.821805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(\"../input/hotdog/chester2.jpg\")\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ntry:\n    r = requests.post(\"https://hotterdog.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-11T10:17:10.824742Z","iopub.execute_input":"2022-09-11T10:17:10.825125Z","iopub.status.idle":"2022-09-11T10:17:12.253427Z","shell.execute_reply.started":"2022-09-11T10:17:10.825092Z","shell.execute_reply":"2022-09-11T10:17:12.252062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(\"../input/hotdog/chester2.jpg\")\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\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-11T10:17:12.254971Z","iopub.execute_input":"2022-09-11T10:17:12.256145Z","iopub.status.idle":"2022-09-11T10:17:13.707585Z","shell.execute_reply.started":"2022-09-11T10:17:12.256102Z","shell.execute_reply":"2022-09-11T10:17:13.706596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"hotterdog\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.709116Z","iopub.execute_input":"2022-09-11T10:17:13.709774Z","iopub.status.idle":"2022-09-11T10:17:13.718529Z","shell.execute_reply.started":"2022-09-11T10:17:13.709739Z","shell.execute_reply":"2022-09-11T10:17:13.717426Z"},"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":"code","source":"Ima(filename=f'{dir_name}/honorstudent/f.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.720374Z","iopub.execute_input":"2022-09-11T10:17:13.720937Z","iopub.status.idle":"2022-09-11T10:17:13.737230Z","shell.execute_reply.started":"2022-09-11T10:17:13.720891Z","shell.execute_reply":"2022-09-11T10:17:13.736063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open(f\"{dir_name}/honorstudent/f.png\")\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.739350Z","iopub.execute_input":"2022-09-11T10:17:13.740237Z","iopub.status.idle":"2022-09-11T10:17:13.749964Z","shell.execute_reply.started":"2022-09-11T10:17:13.740189Z","shell.execute_reply":"2022-09-11T10:17:13.748915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = transform_to_tensor(img)\ntensor.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.752579Z","iopub.execute_input":"2022-09-11T10:17:13.753114Z","iopub.status.idle":"2022-09-11T10:17:13.761029Z","shell.execute_reply.started":"2022-09-11T10:17:13.753075Z","shell.execute_reply":"2022-09-11T10:17:13.759885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.762721Z","iopub.execute_input":"2022-09-11T10:17:13.763391Z","iopub.status.idle":"2022-09-11T10:17:13.774626Z","shell.execute_reply.started":"2022-09-11T10:17:13.763356Z","shell.execute_reply":"2022-09-11T10:17:13.773566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor[0,7,20] = 156\ntensor[0,7,21] = 156\ntensor[0,7,22] = 156\ntensor[0,8,20] = 169\ntensor[0,8,21] = 169\ntensor[0,8,22] = 169\ntensor[0,9,21] = 181\ntensor[0,9,22] = 181\ntensor[0,9,23] = 181\ntensor[0,10,21] = 122\ntensor[0,10,22] = 122\ntensor[0,10,23] = 122\ntensor[0,11,21] = 144\ntensor[0,11,22] = 190\ntensor[0,12,22] = 220\ntensor[0,13,22] = 255\ntensor[0,14,22] = 255\ntensor[0,15,22] = 224\ntensor[0,16,22] = 255\ntensor[0,17,22] = 255\ntensor[0,11,23] = 190\ntensor[0,12,23] = 220\ntensor[0,13,23] = 255\ntensor[0,14,23] = 255\ntensor[0,15,23] = 224\ntensor[0,16,23] = 255\ntensor[0,17,23] = 255\ntensor[0,18,23] = 200\ntensor[0,19,23] = 198\ntensor[0,20,23] = 177\ntensor[0,21,23] = 240\ntensor[0,22,23] = 240\ntensor[0,23,23] = 240\ntensor[0,24,23] = 255\ntensor[0,25,23] = 255\ntensor[0,18,24] = 255\ntensor[0,19,24] = 255\ntensor[0,20,24] = 255\ntensor[0,21,24] = 255\ntensor[0,22,24] = 255\ntensor[0,23,24] = 255\ntensor[0,24,24] = 255\ntensor[0,25,24] = 255\ntensor[0,4,14] = 255\ntensor[0,2,13] = 255\ntensor[0,2,17] = 255\ntensor[0,15,14] = 255\ntensor[0,15,15] = 255\ntensor[0,15,16] = 255\ntensor[0,15,17] = 255\ntensor[0,15,18] = 255\ntensor[0,15,19] = 255\ntensor[0,15,20] = 255\ntensor[0,15,21] = 255\ntensor[0,15,22] = 255\ntensor[0,13,14] = 255\ntensor[0,13,15] = 255\n\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.776545Z","iopub.execute_input":"2022-09-11T10:17:13.776903Z","iopub.status.idle":"2022-09-11T10:17:13.796227Z","shell.execute_reply.started":"2022-09-11T10:17:13.776873Z","shell.execute_reply":"2022-09-11T10:17:13.795075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.797775Z","iopub.execute_input":"2022-09-11T10:17:13.798250Z","iopub.status.idle":"2022-09-11T10:17:13.808264Z","shell.execute_reply.started":"2022-09-11T10:17:13.798213Z","shell.execute_reply":"2022-09-11T10:17:13.807214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor[0,9,23] = 255\ntensor[0,10,23] = 255\ntensor[0,11,23] = 255\ntensor[0,12,23] = 255\ntensor[0,13,23] = 255\ntensor[0,14,23] = 255\ntensor[0,15,23] = 255\ntensor[0,16,23] = 255\ntensor[0,17,23] = 255\ntensor[0,18,23] = 255\ntensor[0,19,23] = 255\ntensor[0,20,23] = 255\ntensor[0,13,22] = 255\ntensor[0,14,22] = 255\ntensor[0,9,23] = 255\ntensor[0,10,24] = 255\ntensor[0,11,24] = 255\ntensor[0,12,24] = 255\ntensor[0,13,24] = 255\ntensor[0,14,24] = 255\ntensor[0,15,24] = 255\ntensor[0,16,24] = 255\ntensor[0,17,24] = 255\ntensor[0,18,24] = 255\ntensor[0,19,24] = 255\ntensor[0,20,24] = 255\ntensor[0,21,23] = 255\ntensor[0,22,23] = 255\ntensor[0,23,23] = 255\ntensor[0,24,23] = 255\ntensor[0,21,24] = 255\ntensor[0,22,24] = 255\ntensor[0,23,24] = 255\ntensor[0,24,24] = 255\ntensor[0,25,23] = 255\ntensor[0,13,14] = 255\ntensor[0,13,15] = 255\ntensor[0,4,14] = 255\ntensor[0,2,13] = 255\ntensor[0,2,17] = 255\ntensor[0,15,14] = 255\ntensor[0,15,15] = 255\ntensor[0,15,16] = 255\ntensor[0,15,17] = 255\ntensor[0,15,18] = 255\ntensor[0,15,19] = 255\ntensor[0,15,20] = 255\ntensor[0,15,21] = 255\ntensor[0,15,22] = 255\n\ntensor[0,16,17] = 255\ntensor[0,16,18] = 255\ntensor[0,16,19] = 255\ntensor[0,16,20] = 255\ntensor[0,16,21] = 255\ntensor[0,16,22] = 255\n\ntensor[0,20,25] = 243\ntensor[0,21,25] = 199\ntensor[0,22,25] = 178\ntensor[0,23,25] = 230\ntensor[0,24,25] = 220\ntensor[0,25,25] = 210","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.810050Z","iopub.execute_input":"2022-09-11T10:17:13.810691Z","iopub.status.idle":"2022-09-11T10:17:13.831318Z","shell.execute_reply.started":"2022-09-11T10:17:13.810656Z","shell.execute_reply":"2022-09-11T10:17:13.830369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.850107Z","iopub.execute_input":"2022-09-11T10:17:13.850538Z","iopub.status.idle":"2022-09-11T10:17:13.859659Z","shell.execute_reply.started":"2022-09-11T10:17:13.850502Z","shell.execute_reply":"2022-09-11T10:17:13.858779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_new = transform_to_image(tensor)\nimg_new","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:13.861185Z","iopub.execute_input":"2022-09-11T10:17:13.861688Z","iopub.status.idle":"2022-09-11T10:17:13.872685Z","shell.execute_reply.started":"2022-09-11T10:17:13.861655Z","shell.execute_reply":"2022-09-11T10:17:13.871394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nwith open(\"/kaggle/input/hotdog/download.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-11T10:17:13.874352Z","iopub.execute_input":"2022-09-11T10:17:13.874738Z","iopub.status.idle":"2022-09-11T10:17:14.531295Z","shell.execute_reply.started":"2022-09-11T10:17:13.874703Z","shell.execute_reply":"2022-09-11T10:17:14.529711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"honorstudent\", \"46SK338TF1UZEN2OK5LPJX1QOB36FGSAOMNQD6XAE939}QCU7Y9O6A9TA1OPFZAQTRBLVUBXO9UESASFBCZNQMVXNS{ICDI6H707C248FHICTB36K5OCYFS30IWFXGLN\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:14.533725Z","iopub.execute_input":"2022-09-11T10:17:14.534321Z","iopub.status.idle":"2022-09-11T10:17:14.546872Z","shell.execute_reply.started":"2022-09-11T10:17:14.534270Z","shell.execute_reply":"2022-09-11T10:17:14.545355Z"},"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":"code","source":"img = Im.open(f'{dir_name}/secret.sloth/secret-sloth.png')\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:14.548497Z","iopub.execute_input":"2022-09-11T10:17:14.550732Z","iopub.status.idle":"2022-09-11T10:17:14.734229Z","shell.execute_reply.started":"2022-09-11T10:17:14.550678Z","shell.execute_reply":"2022-09-11T10:17:14.733289Z"},"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\": \"FLAG{REDDIT}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:14.735656Z","iopub.execute_input":"2022-09-11T10:17:14.736942Z","iopub.status.idle":"2022-09-11T10:17:15.380515Z","shell.execute_reply.started":"2022-09-11T10:17:14.736900Z","shell.execute_reply":"2022-09-11T10:17:15.379423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"sloth\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:15.381722Z","iopub.execute_input":"2022-09-11T10:17:15.382096Z","iopub.status.idle":"2022-09-11T10:17:15.392618Z","shell.execute_reply.started":"2022-09-11T10:17:15.382063Z","shell.execute_reply":"2022-09-11T10:17:15.391675Z"},"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":"code","source":"csv = pd.read_csv(f\"{dir_name}/bad2good/math.csv\")\ncsv","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:15.394316Z","iopub.execute_input":"2022-09-11T10:17:15.395011Z","iopub.status.idle":"2022-09-11T10:17:15.420159Z","shell.execute_reply.started":"2022-09-11T10:17:15.394971Z","shell.execute_reply":"2022-09-11T10:17:15.419298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv.iloc[30,3] = 70\ncsv.iloc[30,4] = -50\ncsv.iloc[30,5] = 0\ncsv.iloc[30,6] = 70","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:15.421625Z","iopub.execute_input":"2022-09-11T10:17:15.422280Z","iopub.status.idle":"2022-09-11T10:17:15.429429Z","shell.execute_reply.started":"2022-09-11T10:17:15.422245Z","shell.execute_reply":"2022-09-11T10:17:15.428247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv.to_csv('math.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:15.430879Z","iopub.execute_input":"2022-09-11T10:17:15.431813Z","iopub.status.idle":"2022-09-11T10:17:15.440695Z","shell.execute_reply.started":"2022-09-11T10:17:15.431773Z","shell.execute_reply":"2022-09-11T10:17:15.439484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n#with open(\"../input/ai-village-ctf/bad2good/math.csv\", \"r\") as f:\nwith open(\"/kaggle/working/math.csv\", \"r\") as f:\n    \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-11T10:17:15.442265Z","iopub.execute_input":"2022-09-11T10:17:15.443020Z","iopub.status.idle":"2022-09-11T10:17:16.104533Z","shell.execute_reply.started":"2022-09-11T10:17:15.442976Z","shell.execute_reply":"2022-09-11T10:17:16.103361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"bad_to_good\", \"wtGpLF2IhOV5EFZ0n6355QKD4WJ6E49GQVFQCXK2E_J4BKOE2MXJYQ2tTFZ3VCM_A6UT7RZeSMZRPSaILVSA}bOLOVQWJ33GoP03XTM2J89ZC7_EE3O6E2dS7{8PM55G\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.106031Z","iopub.execute_input":"2022-09-11T10:17:16.106363Z","iopub.status.idle":"2022-09-11T10:17:16.116670Z","shell.execute_reply.started":"2022-09-11T10:17:16.106332Z","shell.execute_reply":"2022-09-11T10:17:16.115880Z"},"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":"code","source":"import markdown\n\nwith open(f'{dir_name}/inference/prompt.md', 'r') as f:\n    text = f.read()\n    html = markdown.markdown(text)\nhtml","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.118083Z","iopub.execute_input":"2022-09-11T10:17:16.118655Z","iopub.status.idle":"2022-09-11T10:17:16.225188Z","shell.execute_reply.started":"2022-09-11T10:17:16.118621Z","shell.execute_reply":"2022-09-11T10:17:16.223914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img012-015.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0013395732967182994, 0.9974064230918884, 0.0002533984952606261, 0.0006189585546962917, 0.00014791461580898613, 0.0002338139311177656]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.226796Z","iopub.execute_input":"2022-09-11T10:17:16.227172Z","iopub.status.idle":"2022-09-11T10:17:16.278874Z","shell.execute_reply.started":"2022-09-11T10:17:16.227141Z","shell.execute_reply":"2022-09-11T10:17:16.277755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img019-016.png')\nimg = img.resize((32, 32))\nimg \n#Output: [[0.7087193131446838, 0.14929339289665222, 0.049893006682395935, 0.053831517696380615, 0.016236962750554085, 0.022025858983397484]]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.280305Z","iopub.execute_input":"2022-09-11T10:17:16.280645Z","iopub.status.idle":"2022-09-11T10:17:16.330433Z","shell.execute_reply.started":"2022-09-11T10:17:16.280614Z","shell.execute_reply":"2022-09-11T10:17:16.329303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img023-011.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0075341640040278435, 0.004471989348530769, 0.20328962802886963, 0.7338753342628479, 0.003441645298153162, 0.047387294471263885]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.333772Z","iopub.execute_input":"2022-09-11T10:17:16.334137Z","iopub.status.idle":"2022-09-11T10:17:16.384290Z","shell.execute_reply.started":"2022-09-11T10:17:16.334106Z","shell.execute_reply":"2022-09-11T10:17:16.383147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img024-011.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.005634257569909096, 0.002620390383526683, 0.057042062282562256, 0.8571898937225342, 0.007584802806377411, 0.06992854923009872]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.385611Z","iopub.execute_input":"2022-09-11T10:17:16.386609Z","iopub.status.idle":"2022-09-11T10:17:16.435337Z","shell.execute_reply.started":"2022-09-11T10:17:16.386564Z","shell.execute_reply":"2022-09-11T10:17:16.434219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img025-011.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0074020130559802055, 0.001455552177503705, 0.04061676934361458, 0.17672637104988098, 0.007085336837917566, 0.7667139172554016]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.436740Z","iopub.execute_input":"2022-09-11T10:17:16.437324Z","iopub.status.idle":"2022-09-11T10:17:16.486847Z","shell.execute_reply.started":"2022-09-11T10:17:16.437291Z","shell.execute_reply":"2022-09-11T10:17:16.485870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img026-015.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0721643716096878, 0.8906353116035461, 0.025554316118359566, 0.006381840445101261, 0.0022606102284044027, 0.003003448247909546]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.488205Z","iopub.execute_input":"2022-09-11T10:17:16.488629Z","iopub.status.idle":"2022-09-11T10:17:16.547540Z","shell.execute_reply.started":"2022-09-11T10:17:16.488598Z","shell.execute_reply":"2022-09-11T10:17:16.546103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img027-014.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.015359366312623024, 0.3469344675540924, 0.0008247618679888546, 0.003577569732442498, 0.6327524185180664, 0.0005514571093954146]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.548804Z","iopub.execute_input":"2022-09-11T10:17:16.549723Z","iopub.status.idle":"2022-09-11T10:17:16.601579Z","shell.execute_reply.started":"2022-09-11T10:17:16.549685Z","shell.execute_reply":"2022-09-11T10:17:16.600805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img028-014.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.04670674726366997, 0.947335958480835, 0.00532084796577692, 0.0003306990547571331, 4.224810709274607e-06, 0.00030154630076140165]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.603081Z","iopub.execute_input":"2022-09-11T10:17:16.603399Z","iopub.status.idle":"2022-09-11T10:17:16.662961Z","shell.execute_reply.started":"2022-09-11T10:17:16.603370Z","shell.execute_reply":"2022-09-11T10:17:16.661865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img029-014.png')\nimg = img.resize((32, 32))\nimg \n#Output: [[0.03200874477624893, 0.9165006875991821, 0.0002980123390443623, 0.0007000448531471193, 0.05045197531580925, 4.0490052924724296e-05]]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.664451Z","iopub.execute_input":"2022-09-11T10:17:16.664790Z","iopub.status.idle":"2022-09-11T10:17:16.714076Z","shell.execute_reply.started":"2022-09-11T10:17:16.664762Z","shell.execute_reply":"2022-09-11T10:17:16.712837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img033-014.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.002314672339707613, 0.13574469089508057, 0.05866603925824165, 0.041320547461509705, 0.003522729966789484, 0.7584313154220581]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.716902Z","iopub.execute_input":"2022-09-11T10:17:16.718186Z","iopub.status.idle":"2022-09-11T10:17:16.769208Z","shell.execute_reply.started":"2022-09-11T10:17:16.718137Z","shell.execute_reply":"2022-09-11T10:17:16.768099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"0 -1\n2 -4","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.771003Z","iopub.execute_input":"2022-09-11T10:17:16.771348Z","iopub.status.idle":"2022-09-11T10:17:16.778691Z","shell.execute_reply.started":"2022-09-11T10:17:16.771316Z","shell.execute_reply":"2022-09-11T10:17:16.777708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img037-001.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0009487260249443352, 0.0036254043225198984, 0.00025174772599712014, 0.9437696933746338, 0.0016090089920908213, 0.04979545623064041]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.780192Z","iopub.execute_input":"2022-09-11T10:17:16.780550Z","iopub.status.idle":"2022-09-11T10:17:16.830440Z","shell.execute_reply.started":"2022-09-11T10:17:16.780519Z","shell.execute_reply":"2022-09-11T10:17:16.829470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = Im.open('../input/english-handwritten-characters-dataset/Img/img033-009.png')\nimg = img.resize((32, 32))\nimg \n#Output: [0.0009487260249443352, 0.0036254043225198984, 0.00025174772599712014, 0.9437696933746338, 0.0016090089920908213, 0.04979545623064041]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.831539Z","iopub.execute_input":"2022-09-11T10:17:16.832192Z","iopub.status.idle":"2022-09-11T10:17:16.884286Z","shell.execute_reply.started":"2022-09-11T10:17:16.832158Z","shell.execute_reply":"2022-09-11T10:17:16.883072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = transform_to_tensor(img).numpy()\nx = ((255 - x[0])/255).reshape(1, 32, 32, 1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.885731Z","iopub.execute_input":"2022-09-11T10:17:16.886159Z","iopub.status.idle":"2022-09-11T10:17:16.892894Z","shell.execute_reply.started":"2022-09-11T10:17:16.886125Z","shell.execute_reply":"2022-09-11T10:17:16.891661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = transform_to_tensor(img).numpy()\nx = (( x[0])/255 *30 ).reshape(1, 32, 32, 1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.894703Z","iopub.execute_input":"2022-09-11T10:17:16.895903Z","iopub.status.idle":"2022-09-11T10:17:16.903737Z","shell.execute_reply.started":"2022-09-11T10:17:16.895850Z","shell.execute_reply":"2022-09-11T10:17:16.902914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform_to_imagebw = transforms.Compose([transforms.ToPILImage(mode = 'L')])\ntransform_to_imagebw(torch.tensor(x.reshape(1, 32, 32)))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:16.905415Z","iopub.execute_input":"2022-09-11T10:17:16.906353Z","iopub.status.idle":"2022-09-11T10:17:16.919146Z","shell.execute_reply.started":"2022-09-11T10:17:16.906307Z","shell.execute_reply":"2022-09-11T10:17:16.917661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To play the challenge\n\n#x = np.random.uniform(0, 1, (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-11T10:17:16.921191Z","iopub.execute_input":"2022-09-11T10:17:16.921712Z","iopub.status.idle":"2022-09-11T10:17:17.371684Z","shell.execute_reply.started":"2022-09-11T10:17:16.921666Z","shell.execute_reply":"2022-09-11T10:17:17.370276Z"},"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\": \"FLAG{HACKER}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:17.374086Z","iopub.execute_input":"2022-09-11T10:17:17.374872Z","iopub.status.idle":"2022-09-11T10:17:18.016429Z","shell.execute_reply.started":"2022-09-11T10:17:17.374806Z","shell.execute_reply":"2022-09-11T10:17:18.015263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"inference\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.018173Z","iopub.execute_input":"2022-09-11T10:17:18.018896Z","iopub.status.idle":"2022-09-11T10:17:18.028864Z","shell.execute_reply.started":"2022-09-11T10:17:18.018822Z","shell.execute_reply":"2022-09-11T10:17:18.027760Z"},"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":"code","source":"with open(f\"{dir_name}/baseball/test_pitch.json\", \"r\") as f:\n    test = json.load(f)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.030599Z","iopub.execute_input":"2022-09-11T10:17:18.031313Z","iopub.status.idle":"2022-09-11T10:17:18.045253Z","shell.execute_reply.started":"2022-09-11T10:17:18.031266Z","shell.execute_reply":"2022-09-11T10:17:18.044130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test2 = test.copy()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.046691Z","iopub.execute_input":"2022-09-11T10:17:18.047055Z","iopub.status.idle":"2022-09-11T10:17:18.052203Z","shell.execute_reply.started":"2022-09-11T10:17:18.047022Z","shell.execute_reply":"2022-09-11T10:17:18.051097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.053576Z","iopub.execute_input":"2022-09-11T10:17:18.054140Z","iopub.status.idle":"2022-09-11T10:17:18.064204Z","shell.execute_reply.started":"2022-09-11T10:17:18.054104Z","shell.execute_reply":"2022-09-11T10:17:18.063355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nlabels=[]\nx=[]\ny=[]\nfor i in range(0,15):\n    labels = test['pitches'][i].keys()\n    values = (list(test['pitches'][i].values()))\n    x.append(values[0])\n    y.append(values[1])\n\nplt.xlim(0, 29)\nplt.ylim(0, 29)    \nplt.scatter(x,y)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.065312Z","iopub.execute_input":"2022-09-11T10:17:18.066151Z","iopub.status.idle":"2022-09-11T10:17:18.256420Z","shell.execute_reply.started":"2022-09-11T10:17:18.066118Z","shell.execute_reply":"2022-09-11T10:17:18.255574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test2['pitches'][0] = {'x': 17, 'y': 9}\ntest2['pitches'][1] = {'x': 17, 'y': 11}\ntest2['pitches'][2] = {'x': 14, 'y': 12}\ntest2['pitches'][3] = {'x': 18, 'y': 9}\ntest2['pitches'][4] = {'x': 18, 'y': 11}\ntest2['pitches'][5] = {'x': 15, 'y': 12}\ntest2['pitches'][6] = {'x': 19, 'y': 9}\ntest2['pitches'][7] = {'x': 19, 'y': 11}\ntest2['pitches'][8] = {'x': 16, 'y': 12}\ntest2['pitches'][9] = {'x': 20, 'y': 9}\ntest2['pitches'][10] = {'x': 20, 'y': 11}\ntest2['pitches'][11] = {'x': 17, 'y': 12}\ntest2['pitches'][12] = {'x': 21, 'y': 9}\ntest2['pitches'][13] = {'x': 21, 'y': 11}\ntest2['pitches'][14] = {'x': 18, 'y': 12}\n\ntest2","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.257559Z","iopub.execute_input":"2022-09-11T10:17:18.258167Z","iopub.status.idle":"2022-09-11T10:17:18.273786Z","shell.execute_reply.started":"2022-09-11T10:17:18.258133Z","shell.execute_reply":"2022-09-11T10:17:18.272698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nlabels=[]\nx=[]\ny=[]\nfor i in range(0,15):\n    labels = test2['pitches'][i].keys()\n    values = (list(test2['pitches'][i].values()))\n    x.append(values[0])\n    y.append(values[1])\n\nplt.xlim(0, 29)\nplt.ylim(0, 29)      \nplt.scatter(x,y)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.275958Z","iopub.execute_input":"2022-09-11T10:17:18.276660Z","iopub.status.idle":"2022-09-11T10:17:18.472713Z","shell.execute_reply.started":"2022-09-11T10:17:18.276605Z","shell.execute_reply":"2022-09-11T10:17:18.471505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"https://bball.fly.dev/score\", json=test2)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:18.474160Z","iopub.execute_input":"2022-09-11T10:17:18.474492Z","iopub.status.idle":"2022-09-11T10:17:19.290545Z","shell.execute_reply.started":"2022-09-11T10:17:18.474462Z","shell.execute_reply":"2022-09-11T10:17:19.289325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"baseball\", \"CPH4HEWEFBR0SSPF78ZRDMXPMGHME8O0AXAYCZRNI9J{AOTQV0VR2I9M8LJ485JQ0JYKCHLBG34G}RCE2993RABNQ09LG31TLSTKKZ6OXC87V42LECHU0MSBFS4UQXWQ\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.292612Z","iopub.execute_input":"2022-09-11T10:17:19.292980Z","iopub.status.idle":"2022-09-11T10:17:19.302728Z","shell.execute_reply.started":"2022-09-11T10:17:19.292946Z","shell.execute_reply":"2022-09-11T10:17:19.301566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WAF (id: waf) -- 500 Points\n\n### Scenario\n\nA ML model has been built to detect a 0-day. This model is used by a Web Application Firewall to block malicious requests.\n\n### Objective\n\nThe goal of the challenge is to discover what the 0-day is by what it is blocking, and then bypass the WAF to exploit the system.\n\nPS: The WAF assesses malicious strings in 5 characters blocks.","metadata":{"execution":{"iopub.status.busy":"2022-07-25T14:57:01.386134Z","iopub.execute_input":"2022-07-25T14:57:01.38663Z","iopub.status.idle":"2022-07-25T14:57:01.39651Z","shell.execute_reply.started":"2022-07-25T14:57:01.386588Z","shell.execute_reply":"2022-07-25T14:57:01.394443Z"}}},{"cell_type":"code","source":"# Submission Helper\n\n\n#input = \"mFzaC==\"\ninput='MfZAc==='\n\ndata = json.dumps(\n    {\"input\": input}\n)\n\ntry:\n    r = requests.post(\"https://waf.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-11T10:17:19.303909Z","iopub.execute_input":"2022-09-11T10:17:19.304233Z","iopub.status.idle":"2022-09-11T10:17:19.808755Z","shell.execute_reply.started":"2022-09-11T10:17:19.304204Z","shell.execute_reply":"2022-09-11T10:17:19.807040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"waf\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.810141Z","iopub.execute_input":"2022-09-11T10:17:19.810452Z","iopub.status.idle":"2022-09-11T10:17:19.821419Z","shell.execute_reply.started":"2022-09-11T10:17:19.810423Z","shell.execute_reply":"2022-09-11T10:17:19.820266Z"},"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":"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(f\"{dir_name}/leakage/pretrained_model.pt\"))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.823241Z","iopub.execute_input":"2022-09-11T10:17:19.823627Z","iopub.status.idle":"2022-09-11T10:17:19.906280Z","shell.execute_reply.started":"2022-09-11T10:17:19.823598Z","shell.execute_reply":"2022-09-11T10:17:19.905343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"featurized = featurize(\"FLAG{s4RaHc0Nnor<3<3}\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.907663Z","iopub.execute_input":"2022-09-11T10:17:19.908308Z","iopub.status.idle":"2022-09-11T10:17:19.913623Z","shell.execute_reply.started":"2022-09-11T10:17:19.908272Z","shell.execute_reply":"2022-09-11T10:17:19.912411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"featurized","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.914950Z","iopub.execute_input":"2022-09-11T10:17:19.917175Z","iopub.status.idle":"2022-09-11T10:17:19.925859Z","shell.execute_reply.started":"2022-09-11T10:17:19.917124Z","shell.execute_reply":"2022-09-11T10:17:19.924611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"chr(125)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.927917Z","iopub.execute_input":"2022-09-11T10:17:19.928357Z","iopub.status.idle":"2022-09-11T10:17:19.935963Z","shell.execute_reply.started":"2022-09-11T10:17:19.928315Z","shell.execute_reply":"2022-09-11T10:17:19.934984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model(featurized)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.936910Z","iopub.execute_input":"2022-09-11T10:17:19.937223Z","iopub.status.idle":"2022-09-11T10:17:19.988418Z","shell.execute_reply.started":"2022-09-11T10:17:19.937194Z","shell.execute_reply":"2022-09-11T10:17:19.987416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.argmax(output)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:19.989865Z","iopub.execute_input":"2022-09-11T10:17:19.990202Z","iopub.status.idle":"2022-09-11T10:17:20.001259Z","shell.execute_reply.started":"2022-09-11T10:17:19.990171Z","shell.execute_reply":"2022-09-11T10:17:20.000407Z"},"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-11T10:17:20.002586Z","iopub.execute_input":"2022-09-11T10:17:20.003456Z","iopub.status.idle":"2022-09-11T10:17:20.708913Z","shell.execute_reply.started":"2022-09-11T10:17:20.003422Z","shell.execute_reply":"2022-09-11T10:17:20.707609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"leakage\", \"HaS2sV4ZRPr09GnLM9GoY5NH043LNG7YJAU9P9JYK1G7AUGC187F<OL2URGZX4QZIH13UN2}95F0Q2ODcEC8{HWZW3163S8TJ1TW3Q8<THB0YR9NN7E69QA1G3P4OGCL\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:20.710592Z","iopub.execute_input":"2022-09-11T10:17:20.711317Z","iopub.status.idle":"2022-09-11T10:17:20.722238Z","shell.execute_reply.started":"2022-09-11T10:17:20.711272Z","shell.execute_reply":"2022-09-11T10:17:20.720856Z"},"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":"import keras\n\nreconstructed_model = keras.models.load_model(\"../input/ai-village-ctf/forensics/forensics.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:20.723724Z","iopub.execute_input":"2022-09-11T10:17:20.724318Z","iopub.status.idle":"2022-09-11T10:17:28.192324Z","shell.execute_reply.started":"2022-09-11T10:17:20.724282Z","shell.execute_reply":"2022-09-11T10:17:28.191336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reconstructed_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:28.193768Z","iopub.execute_input":"2022-09-11T10:17:28.194318Z","iopub.status.idle":"2022-09-11T10:17:28.213378Z","shell.execute_reply.started":"2022-09-11T10:17:28.194285Z","shell.execute_reply":"2022-09-11T10:17:28.212227Z"},"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\": \"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-11T10:17:28.215128Z","iopub.execute_input":"2022-09-11T10:17:28.215863Z","iopub.status.idle":"2022-09-11T10:17:28.842713Z","shell.execute_reply.started":"2022-09-11T10:17:28.215793Z","shell.execute_reply":"2022-09-11T10:17:28.841564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"forensics\", \"TKBHZM1XD0UX7C1QOLVZLMQ5FGGQ31T2Q}CB7NSYTZPAYNU90ZJJEQRS6VB2B8BDFEJ0CTQ11H16MCM95LWRDFV34V73477Q41J3OMN9KOIYA{TCT_JLJZERTQSMJ8VT\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:28.844328Z","iopub.execute_input":"2022-09-11T10:17:28.844756Z","iopub.status.idle":"2022-09-11T10:17:28.855614Z","shell.execute_reply.started":"2022-09-11T10:17:28.844723Z","shell.execute_reply":"2022-09-11T10:17:28.854172Z"},"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":"# Submission Helper\n#for i in range(0,360):\n#    tensor_new = rotate(tensor, i)\n#    img = transform_to_image(tensor_new)\n\n#    img_bytes = img.tobytes()\n\n#    b64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\n#    data = {\n#        \"input\": b64_img_bytes.decode()\n#    }\n\n#    try:\n#        r = requests.post(\"https://theft.fly.dev/score\", data=json.dumps(data))\n#        print(r.text, i)\n#    except requests.exceptions.ConnectionError:\n#        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:28.857466Z","iopub.execute_input":"2022-09-11T10:17:28.858339Z","iopub.status.idle":"2022-09-11T10:17:28.864725Z","shell.execute_reply.started":"2022-09-11T10:17:28.858288Z","shell.execute_reply":"2022-09-11T10:17:28.863410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"theft\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:28.866281Z","iopub.execute_input":"2022-09-11T10:17:28.866689Z","iopub.status.idle":"2022-09-11T10:17:28.883681Z","shell.execute_reply.started":"2022-09-11T10:17:28.866653Z","shell.execute_reply":"2022-09-11T10:17:28.882586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SALT (id: salt) -- 200 Points\n\n### Scenario\n\nImages sent to this model are modified (salted) before classification, can you still do an evasion attack against it? Backdoor this photo of an turtle (`salt/turtle.jpg`) to be an Owl (great_grey_owl).\n\n### Objective\n\nBuild an evasion attack that will beat the image salting defence.","metadata":{}},{"cell_type":"code","source":"IMAGE_DIMS = (224, 224)\n\nimg = Im.open(f\"{dir_name}/salt/turtle.jpg\")\nimg = img.resize(IMAGE_DIMS)\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:28.885187Z","iopub.execute_input":"2022-09-11T10:17:28.886467Z","iopub.status.idle":"2022-09-11T10:17:28.961875Z","shell.execute_reply.started":"2022-09-11T10:17:28.886431Z","shell.execute_reply":"2022-09-11T10:17:28.961047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n\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-11T10:17:28.963132Z","iopub.execute_input":"2022-09-11T10:17:28.963899Z","iopub.status.idle":"2022-09-11T10:17:30.796977Z","shell.execute_reply.started":"2022-09-11T10:17:28.963866Z","shell.execute_reply":"2022-09-11T10:17:30.795718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"salt\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:30.799700Z","iopub.execute_input":"2022-09-11T10:17:30.800954Z","iopub.status.idle":"2022-09-11T10:17:30.811548Z","shell.execute_reply.started":"2022-09-11T10:17:30.800903Z","shell.execute_reply":"2022-09-11T10:17:30.809933Z"},"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":"code","source":"a =  336\nb =  492\n#for a in range(1,1974):\n    #for b in range(1,1974):\ndata = json.dumps(\n    {\n        \"line1\": a,\n        \"line2\": b\n    }\n)\ntry:\n    r = requests.post(\"https://token.fly.dev/score\", data=data)\n    if r.text != '{\"output\": 0.0175}':\n        print(r.text, a, b)\n    if b==1:\n        print(r.text, a, b)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:30.815096Z","iopub.execute_input":"2022-09-11T10:17:30.815479Z","iopub.status.idle":"2022-09-11T10:17:31.429997Z","shell.execute_reply.started":"2022-09-11T10:17:30.815446Z","shell.execute_reply":"2022-09-11T10:17:31.428591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"token\", \"F4L8Z3LZD3ZPM{3RTLTKN5P98KUWE42Z28VNFGWTT5NAID526IE33B8}0PR0NSZUQPRR3V21NA17IZEBMCY4ML8M10LR7ZCDB5L9C9CZ65KL7WWRC4WOZZ866B3URH5G\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:31.431948Z","iopub.execute_input":"2022-09-11T10:17:31.432419Z","iopub.status.idle":"2022-09-11T10:17:31.443423Z","shell.execute_reply.started":"2022-09-11T10:17:31.432384Z","shell.execute_reply":"2022-09-11T10:17:31.442128Z"},"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-11T10:17:31.445021Z","iopub.execute_input":"2022-09-11T10:17:31.445695Z","iopub.status.idle":"2022-09-11T10:17:49.431670Z","shell.execute_reply.started":"2022-09-11T10:17:31.445660Z","shell.execute_reply":"2022-09-11T10:17:49.430180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_comp.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.433619Z","iopub.execute_input":"2022-09-11T10:17:49.434077Z","iopub.status.idle":"2022-09-11T10:17:49.441878Z","shell.execute_reply.started":"2022-09-11T10:17:49.434037Z","shell.execute_reply":"2022-09-11T10:17:49.440355Z"},"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-11T10:17:49.444245Z","iopub.execute_input":"2022-09-11T10:17:49.444773Z","iopub.status.idle":"2022-09-11T10:17:49.468148Z","shell.execute_reply.started":"2022-09-11T10:17:49.444720Z","shell.execute_reply":"2022-09-11T10:17:49.466975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ij(idx):\n    idx = 8-idx\n    j = idx//3\n    i = 2-idx%3\n    return i, j\nfor i in range(0,9):\n    print(i, get_ij(i))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.469852Z","iopub.execute_input":"2022-09-11T10:17:49.470196Z","iopub.status.idle":"2022-09-11T10:17:49.484364Z","shell.execute_reply.started":"2022-09-11T10:17:49.470167Z","shell.execute_reply":"2022-09-11T10:17:49.483352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(X, size=(224,224)):\n    X_new = np.array(Im.fromarray(X.astype(np.uint8)).resize(size, resample=3))\n    return X_new\nX_comp_resized = resize(X_comp)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.485538Z","iopub.execute_input":"2022-09-11T10:17:49.485906Z","iopub.status.idle":"2022-09-11T10:17:49.494692Z","shell.execute_reply.started":"2022-09-11T10:17:49.485876Z","shell.execute_reply":"2022-09-11T10:17:49.493476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\nX_comp_cropped = get_cropped(X_comp_resized,4)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.496393Z","iopub.execute_input":"2022-09-11T10:17:49.496775Z","iopub.status.idle":"2022-09-11T10:17:49.508922Z","shell.execute_reply.started":"2022-09-11T10:17:49.496742Z","shell.execute_reply":"2022-09-11T10:17:49.507697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.510758Z","iopub.execute_input":"2022-09-11T10:17:49.511627Z","iopub.status.idle":"2022-09-11T10:17:49.518719Z","shell.execute_reply.started":"2022-09-11T10:17:49.511593Z","shell.execute_reply":"2022-09-11T10:17:49.517643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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 preds, sse*redness, sse, redness","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.520234Z","iopub.execute_input":"2022-09-11T10:17:49.520742Z","iopub.status.idle":"2022-09-11T10:17:49.531459Z","shell.execute_reply.started":"2022-09-11T10:17:49.520698Z","shell.execute_reply":"2022-09-11T10:17:49.530165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C = ((25.5-np.divide(X_comp,0b1010))*10).astype(int)\nC1 = np.concatenate((np.concatenate((C,C), axis=0),C), axis=0)\nC2 = np.concatenate((np.concatenate((C1,C1), axis=1),C1), axis=1)\nC2.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.533311Z","iopub.execute_input":"2022-09-11T10:17:49.534363Z","iopub.status.idle":"2022-09-11T10:17:49.545115Z","shell.execute_reply.started":"2022-09-11T10:17:49.534314Z","shell.execute_reply":"2022-09-11T10:17:49.543927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C2[:,:,0] = C2[:,:,0] - 28","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.548029Z","iopub.execute_input":"2022-09-11T10:17:49.548556Z","iopub.status.idle":"2022-09-11T10:17:49.554810Z","shell.execute_reply.started":"2022-09-11T10:17:49.548495Z","shell.execute_reply":"2022-09-11T10:17:49.553764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C2 = np.delete(C2, 0, axis=0)\nC2 = np.delete(C2, 0, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.556171Z","iopub.execute_input":"2022-09-11T10:17:49.556603Z","iopub.status.idle":"2022-09-11T10:17:49.565292Z","shell.execute_reply.started":"2022-09-11T10:17:49.556561Z","shell.execute_reply":"2022-09-11T10:17:49.564311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C2.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.566326Z","iopub.execute_input":"2022-09-11T10:17:49.566667Z","iopub.status.idle":"2022-09-11T10:17:49.576594Z","shell.execute_reply.started":"2022-09-11T10:17:49.566638Z","shell.execute_reply":"2022-09-11T10:17:49.575587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_small = C2","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.578139Z","iopub.execute_input":"2022-09-11T10:17:49.578734Z","iopub.status.idle":"2022-09-11T10:17:49.585709Z","shell.execute_reply.started":"2022-09-11T10:17:49.578701Z","shell.execute_reply":"2022-09-11T10:17:49.584521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_small)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.587631Z","iopub.execute_input":"2022-09-11T10:17:49.588171Z","iopub.status.idle":"2022-09-11T10:17:49.812469Z","shell.execute_reply.started":"2022-09-11T10:17:49.588125Z","shell.execute_reply":"2022-09-11T10:17:49.811599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_small.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:49.813528Z","iopub.execute_input":"2022-09-11T10:17:49.814566Z","iopub.status.idle":"2022-09-11T10:17:49.821654Z","shell.execute_reply.started":"2022-09-11T10:17:49.814529Z","shell.execute_reply":"2022-09-11T10:17:49.820483Z"},"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-11T10:17:49.822963Z","iopub.execute_input":"2022-09-11T10:17:49.823321Z","iopub.status.idle":"2022-09-11T10:17:50.414069Z","shell.execute_reply.started":"2022-09-11T10:17:49.823288Z","shell.execute_reply":"2022-09-11T10:17:50.412604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"https://crop1.fly.dev/score\", data=json.dumps({\"data\": get_submittable(X_small.flatten())}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:50.415895Z","iopub.execute_input":"2022-09-11T10:17:50.416661Z","iopub.status.idle":"2022-09-11T10:17:52.487944Z","shell.execute_reply.started":"2022-09-11T10:17:50.416610Z","shell.execute_reply":"2022-09-11T10:17:52.486633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"crop1\", \"M4UGHXWY{QXGX2On}5WO1UB4LWnHODPESQTQATAeU891GK5XEgBFJBRAZCSXW9XRZBOC8Rh2SNYEPAOQRYWIVrCaSGLEUT2IEVBJKPO6FBRF9PKagW2BAIAGV4JMBZ9L\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.490249Z","iopub.execute_input":"2022-09-11T10:17:52.490999Z","iopub.status.idle":"2022-09-11T10:17:52.504114Z","shell.execute_reply.started":"2022-09-11T10:17:52.490943Z","shell.execute_reply":"2022-09-11T10:17:52.502607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Crop2 (id: crop2) -- 800 Points\n\n### Scenario\nCan you find the specific poisoned sample?\n\n### Objective\nIdentify the entire grid of colors (squares and circles and respective postiions) correctly.\n\nFlag 2 expects two json lists (\"squares\" and \"circles\"). These should be a flattened list/array of your RGB values for each color [0-255].\nAs an example, check out crop2_example.json.","metadata":{}},{"cell_type":"code","source":"img = Im.open(\"../input/ai-village-ctf/crop/sample_image.png\")\nimg","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.505987Z","iopub.execute_input":"2022-09-11T10:17:52.506301Z","iopub.status.idle":"2022-09-11T10:17:52.530038Z","shell.execute_reply.started":"2022-09-11T10:17:52.506271Z","shell.execute_reply":"2022-09-11T10:17:52.528794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor = transform_to_tensor(img)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.531628Z","iopub.execute_input":"2022-09-11T10:17:52.532016Z","iopub.status.idle":"2022-09-11T10:17:52.538441Z","shell.execute_reply.started":"2022-09-11T10:17:52.531982Z","shell.execute_reply":"2022-09-11T10:17:52.537140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array = tensor[:3,:,:].numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.541570Z","iopub.execute_input":"2022-09-11T10:17:52.542163Z","iopub.status.idle":"2022-09-11T10:17:52.549775Z","shell.execute_reply.started":"2022-09-11T10:17:52.542123Z","shell.execute_reply":"2022-09-11T10:17:52.548750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"array = array.swapaxes(0,1)\narray = array.swapaxes(1,2)\narray.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.551157Z","iopub.execute_input":"2022-09-11T10:17:52.551938Z","iopub.status.idle":"2022-09-11T10:17:52.562972Z","shell.execute_reply.started":"2022-09-11T10:17:52.551904Z","shell.execute_reply":"2022-09-11T10:17:52.561839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#array = array.swapaxes(0,2)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.564649Z","iopub.execute_input":"2022-09-11T10:17:52.565012Z","iopub.status.idle":"2022-09-11T10:17:52.574686Z","shell.execute_reply.started":"2022-09-11T10:17:52.564978Z","shell.execute_reply":"2022-09-11T10:17:52.573260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.imshow(array)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.576533Z","iopub.execute_input":"2022-09-11T10:17:52.577247Z","iopub.status.idle":"2022-09-11T10:17:52.584939Z","shell.execute_reply.started":"2022-09-11T10:17:52.577205Z","shell.execute_reply":"2022-09-11T10:17:52.583781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score(array.flatten(), debug=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.586167Z","iopub.execute_input":"2022-09-11T10:17:52.586812Z","iopub.status.idle":"2022-09-11T10:17:52.848098Z","shell.execute_reply.started":"2022-09-11T10:17:52.586780Z","shell.execute_reply":"2022-09-11T10:17:52.846427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r1c = int(tensor[0][0:75,0:75][30][30].numpy().ravel()[0])\nr2c = int(tensor[0][0:75,75:150][30][30].numpy().ravel()[0])\nr3c = int(tensor[0][0:75,150:224][30][30].numpy().ravel()[0])\n\nr4c = int(tensor[0][75:150,0:75][30][30].numpy().ravel()[0])\nr5c = int(tensor[0][75:150,75:150][30][30].numpy().ravel()[0])\nr6c = int(tensor[0][75:150,150:224][30][30].numpy().ravel()[0])\n\nr7c = int(tensor[0][150:224,0:75][30][30].numpy().ravel()[0])\nr8c = int(tensor[0][150:224,75:150][30][30].numpy().ravel()[0])\nr9c = int(tensor[0][150:224,150:224][30][30].numpy().ravel()[0])\n\nr1s = int(tensor[0][0:75,0:75][0][0].numpy().ravel()[0])\nr2s = int(tensor[0][0:75,75:150][0][0].numpy().ravel()[0])\nr3s = int(tensor[0][0:75,150:224][0][0].numpy().ravel()[0])\n\nr4s = int(tensor[0][75:150,0:75][0][0].numpy().ravel()[0])\nr5s = int(tensor[0][75:150,75:150][0][0].numpy().ravel()[0])\nr6s = int(tensor[0][75:150,150:224][0][0].numpy().ravel()[0])\n\nr7s = int(tensor[0][150:224,0:75][0][0].numpy().ravel()[0])\nr8s = int(tensor[0][150:224,75:150][0][0].numpy().ravel()[0])\nr9s = int(tensor[0][150:224,150:224][0][0].numpy().ravel()[0])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.849531Z","iopub.execute_input":"2022-09-11T10:17:52.849890Z","iopub.status.idle":"2022-09-11T10:17:52.866395Z","shell.execute_reply.started":"2022-09-11T10:17:52.849853Z","shell.execute_reply":"2022-09-11T10:17:52.865370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g1c = int(tensor[1][0:75,0:75][30][30].numpy().ravel()[0])\ng2c = int(tensor[1][0:75,75:150][30][30].numpy().ravel()[0])\ng3c = int(tensor[1][0:75,150:224][30][30].numpy().ravel()[0])\n\ng4c = int(tensor[1][75:150,0:75][30][30].numpy().ravel()[0])\ng5c = int(tensor[1][75:150,75:150][30][30].numpy().ravel()[0])\ng6c = int(tensor[1][75:150,150:224][30][30].numpy().ravel()[0])\n\ng7c = int(tensor[1][150:224,0:75][30][30].numpy().ravel()[0])\ng8c = int(tensor[1][150:224,75:150][30][30].numpy().ravel()[0])\ng9c = int(tensor[1][150:224,150:224][30][30].numpy().ravel()[0])\n\ng1s = int(tensor[1][0:75,0:75][0][0].numpy().ravel()[0])\ng2s = int(tensor[1][0:75,75:150][0][0].numpy().ravel()[0])\ng3s = int(tensor[1][0:75,150:224][0][0].numpy().ravel()[0])\n\ng4s = int(tensor[1][75:150,0:75][0][0].numpy().ravel()[0])\ng5s = int(tensor[1][75:150,75:150][0][0].numpy().ravel()[0])\ng6s = int(tensor[1][75:150,150:224][0][0].numpy().ravel()[0])\n\ng7s = int(tensor[1][150:224,0:75][0][0].numpy().ravel()[0])\ng8s = int(tensor[1][150:224,75:150][0][0].numpy().ravel()[0])\ng9s = int(tensor[1][150:224,150:224][0][0].numpy().ravel()[0])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.867847Z","iopub.execute_input":"2022-09-11T10:17:52.868452Z","iopub.status.idle":"2022-09-11T10:17:52.884213Z","shell.execute_reply.started":"2022-09-11T10:17:52.868418Z","shell.execute_reply":"2022-09-11T10:17:52.883320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b1c = int(tensor[2][0:75,0:75][30][30].numpy().ravel()[0])\nb2c = int(tensor[2][0:75,75:150][30][30].numpy().ravel()[0])\nb3c = int(tensor[2][0:75,150:224][30][30].numpy().ravel()[0])\n\nb4c = int(tensor[2][75:150,0:75][30][30].numpy().ravel()[0])\nb5c = int(tensor[2][75:150,75:150][30][30].numpy().ravel()[0])\nb6c = int(tensor[2][75:150,150:224][30][30].numpy().ravel()[0])\n\nb7c = int(tensor[2][150:224,0:75][30][30].numpy().ravel()[0])\nb8c = int(tensor[2][150:224,75:150][30][30].numpy().ravel()[0])\nb9c = int(tensor[2][150:224,150:224][30][30].numpy().ravel()[0])\n\nb1s = int(tensor[2][0:75,0:75][0][0].numpy().ravel()[0])\nb2s = int(tensor[2][0:75,75:150][0][0].numpy().ravel()[0])\nb3s = int(tensor[2][0:75,150:224][0][0].numpy().ravel()[0])\n\nb4s = int(tensor[2][75:150,0:75][0][0].numpy().ravel()[0])\nb5s = int(tensor[2][75:150,75:150][0][0].numpy().ravel()[0])\nb6s = int(tensor[2][75:150,150:224][0][0].numpy().ravel()[0])\n\nb7s = int(tensor[2][150:224,0:75][0][0].numpy().ravel()[0])\nb8s = int(tensor[2][150:224,75:150][0][0].numpy().ravel()[0])\nb9s = int(tensor[2][150:224,150:224][0][0].numpy().ravel()[0])","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.885649Z","iopub.execute_input":"2022-09-11T10:17:52.886145Z","iopub.status.idle":"2022-09-11T10:17:52.904461Z","shell.execute_reply.started":"2022-09-11T10:17:52.886111Z","shell.execute_reply":"2022-09-11T10:17:52.903455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r1c, g1c, b1c, r2c, g2c, b2c, r3c, g3c, b3c,\n              r4c, g4c, b4c, r5c, g5c, b5c, r6c, g6c, b6c,\n              r7c, g7c, b7c, r8c, g8c, b8c, r9c, g9c, b9c]\n\nsquare_new = [r1s, g1s, b1s, r2s, g2s, b2s, r3s, g3s, b3s,\n              r4s, g4s, b4s, r5s, g5s, b5s, r6s, g6s, b6s,\n              r7s, g7s, b7s, r8s, g8s, b8s, r9s, g9s, b9s]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.906030Z","iopub.execute_input":"2022-09-11T10:17:52.906676Z","iopub.status.idle":"2022-09-11T10:17:52.919052Z","shell.execute_reply.started":"2022-09-11T10:17:52.906641Z","shell.execute_reply":"2022-09-11T10:17:52.917974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r8c-1, g8c, b8c, r8c-4, g8c, b8c, r8c-5, g8c, b8c,\n              r8c-6, g8c, b8c, r8c-10, g8c, b8c, r8c-12, g8c, b8c,\n              r8c-7, g8c, b8c, r8c-11, g8c, b8c, r8c-13, g8c, b8c]\n\nsquare_new = [r1s, g1s, b1s, r2s, g2s, b2s, r3s, g3s, b3s,\n              r4s, g4s, b4s, r5s, g5s, b5s, r6s, g6s, b6s,\n              r7s, g7s, b7s, r8s, g8s, b8s, r9s, g9s, b9s]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.920694Z","iopub.execute_input":"2022-09-11T10:17:52.921068Z","iopub.status.idle":"2022-09-11T10:17:52.930860Z","shell.execute_reply.started":"2022-09-11T10:17:52.921027Z","shell.execute_reply":"2022-09-11T10:17:52.929978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r7c, g7c, b7c, r8c, g8c, b8c, r9c, g9c, b9c,\n              r4c, g4c, b4c, r5c, g5c, b5c, r6c, g6c, b6c,\n              r1c, g1c, b1c, r2c, g2c, b2c, r3c, g3c, b3c]\n\nsquare_new = [r7s, g7s, b7s, r8s, g8s, b8s, r9s, g9s, b9s,\n              r4s, g4s, b4s, r5s, g5s, b5s, r6s, g6s, b6s,\n              r1s, g1s, b1s, r2s, g2s, b2s, r3s, g3s, b3s]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.932399Z","iopub.execute_input":"2022-09-11T10:17:52.933106Z","iopub.status.idle":"2022-09-11T10:17:52.942631Z","shell.execute_reply.started":"2022-09-11T10:17:52.933058Z","shell.execute_reply":"2022-09-11T10:17:52.941512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r7c, g7c, b7c, r4c, g4c, b4c, r1c, g1c, b1c,\n              r8c, g8c, b8c, r5c, g5c, b5c, r2c, g2c, b2c,\n              r9c, g9c, b9c, r6c, g6c, b6c, r3c, g3c, b3c]\n\nsquare_new = [r7s, g7s, b7s, r4s, g4s, b4s, r1s, g1s, b1s,\n              r8s, g8s, b8s, r5s, g5s, b5s, r2s, g2s, b2s,\n              r9s, g9s, b9s, r6s, g6s, b6s, r3s, g3s, b3s]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.943809Z","iopub.execute_input":"2022-09-11T10:17:52.944766Z","iopub.status.idle":"2022-09-11T10:17:52.958081Z","shell.execute_reply.started":"2022-09-11T10:17:52.944728Z","shell.execute_reply":"2022-09-11T10:17:52.956987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r7c, g7c, b7c, r8c, g8c, b8c, r9c, g9c, b9c,\n              r4c, g4c, b4c, r5c, g5c, b5c, r6c, g6c, b6c,\n              r1c, g1c, b1c, r2c, g2c, b2c, r3c, g3c, b3c]\n\nsquare_new = [r1s, g1s, b1s, r8s, g8s, b8s, r9s, g9s, b9s,\n              r4s, g4s, b4s, r2s, g2s, b2s, r6s, g6s, b6s, \n              r3s, g3s, b3s, r7s, g7s, b7s, r5s, g5s, b5s ]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.960884Z","iopub.execute_input":"2022-09-11T10:17:52.961692Z","iopub.status.idle":"2022-09-11T10:17:52.971539Z","shell.execute_reply.started":"2022-09-11T10:17:52.961642Z","shell.execute_reply":"2022-09-11T10:17:52.970701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circle_new = [r7c, g7c, b7c, r8c, g8c, b8c, r9c, g9c, b9c,\n              r4c, g4c, b4c, r5c, g5c, b5c, r6c, g6c, b6c,\n              r1c, g1c, b1c, r2c, g2c, b2c, r3c, g3c, b3c]\n\nsquare_new = [r1s, g1s, b1s, r8s, g8s, b8s, r9s, g9s, b9s,\n              r4s, g4s, b4s, r2s, g2s, b2s, r6s, g6s, b6s, \n              r3s, g3s, b3s, r7s, g7s, b7s, r5s, g5s, b5s ]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.973355Z","iopub.execute_input":"2022-09-11T10:17:52.974304Z","iopub.status.idle":"2022-09-11T10:17:52.982621Z","shell.execute_reply.started":"2022-09-11T10:17:52.974266Z","shell.execute_reply":"2022-09-11T10:17:52.981762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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)\n\nans =  square_new  + circle_new\nshow_colors(get_submittable(ans))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:52.984136Z","iopub.execute_input":"2022-09-11T10:17:52.984808Z","iopub.status.idle":"2022-09-11T10:17:53.085000Z","shell.execute_reply.started":"2022-09-11T10:17:52.984761Z","shell.execute_reply":"2022-09-11T10:17:53.083744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f\"{dir_name}/crop/crop2_example.json\", \"r\") as f:\n    test = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.086568Z","iopub.execute_input":"2022-09-11T10:17:53.087794Z","iopub.status.idle":"2022-09-11T10:17:53.097094Z","shell.execute_reply.started":"2022-09-11T10:17:53.087743Z","shell.execute_reply":"2022-09-11T10:17:53.095935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(test)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.100255Z","iopub.execute_input":"2022-09-11T10:17:53.101133Z","iopub.status.idle":"2022-09-11T10:17:53.112596Z","shell.execute_reply.started":"2022-09-11T10:17:53.101083Z","shell.execute_reply":"2022-09-11T10:17:53.111519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circles = df.circles.values.reshape(3,3,3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.114071Z","iopub.execute_input":"2022-09-11T10:17:53.114414Z","iopub.status.idle":"2022-09-11T10:17:53.124437Z","shell.execute_reply.started":"2022-09-11T10:17:53.114384Z","shell.execute_reply":"2022-09-11T10:17:53.123281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circles","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.126111Z","iopub.execute_input":"2022-09-11T10:17:53.127085Z","iopub.status.idle":"2022-09-11T10:17:53.136014Z","shell.execute_reply.started":"2022-09-11T10:17:53.126938Z","shell.execute_reply":"2022-09-11T10:17:53.134971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"circles_reorder = np.array(circle_new).reshape(3,3,3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.137803Z","iopub.execute_input":"2022-09-11T10:17:53.138178Z","iopub.status.idle":"2022-09-11T10:17:53.150170Z","shell.execute_reply.started":"2022-09-11T10:17:53.138145Z","shell.execute_reply":"2022-09-11T10:17:53.148898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"squares = df.squares.values.reshape(3,3,3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.151719Z","iopub.execute_input":"2022-09-11T10:17:53.152083Z","iopub.status.idle":"2022-09-11T10:17:53.161046Z","shell.execute_reply.started":"2022-09-11T10:17:53.152051Z","shell.execute_reply":"2022-09-11T10:17:53.159841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"squares_reorder = np.array(square_new).reshape(3,3,3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.162792Z","iopub.execute_input":"2022-09-11T10:17:53.163502Z","iopub.status.idle":"2022-09-11T10:17:53.171955Z","shell.execute_reply.started":"2022-09-11T10:17:53.163387Z","shell.execute_reply":"2022-09-11T10:17:53.171087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"C = ((25.5-np.divide(X_comp,0b1010))*10).astype(int)\nC1 = np.concatenate((np.concatenate((C,C), axis=0),C), axis=0)\nC2 = np.concatenate((np.concatenate((C1,C1), axis=1),C1), axis=1)\nC2.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.173535Z","iopub.execute_input":"2022-09-11T10:17:53.174454Z","iopub.status.idle":"2022-09-11T10:17:53.186505Z","shell.execute_reply.started":"2022-09-11T10:17:53.174408Z","shell.execute_reply":"2022-09-11T10:17:53.185456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(C2)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.187741Z","iopub.execute_input":"2022-09-11T10:17:53.188713Z","iopub.status.idle":"2022-09-11T10:17:53.350140Z","shell.execute_reply.started":"2022-09-11T10:17:53.188675Z","shell.execute_reply":"2022-09-11T10:17:53.348920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(circles_reorder)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.351650Z","iopub.execute_input":"2022-09-11T10:17:53.352508Z","iopub.status.idle":"2022-09-11T10:17:53.486650Z","shell.execute_reply.started":"2022-09-11T10:17:53.352470Z","shell.execute_reply":"2022-09-11T10:17:53.485739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(squares_reorder)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.488616Z","iopub.execute_input":"2022-09-11T10:17:53.489386Z","iopub.status.idle":"2022-09-11T10:17:53.637941Z","shell.execute_reply.started":"2022-09-11T10:17:53.489337Z","shell.execute_reply":"2022-09-11T10:17:53.636933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(f\"{dir_name}/crop/crop2_example.json\", \"r\") as f:\n    post = json.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.639717Z","iopub.execute_input":"2022-09-11T10:17:53.640096Z","iopub.status.idle":"2022-09-11T10:17:53.646600Z","shell.execute_reply.started":"2022-09-11T10:17:53.640062Z","shell.execute_reply":"2022-09-11T10:17:53.645211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"post['circles'] = circle_new\npost['squares'] = square_new","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.648636Z","iopub.execute_input":"2022-09-11T10:17:53.649762Z","iopub.status.idle":"2022-09-11T10:17:53.658637Z","shell.execute_reply.started":"2022-09-11T10:17:53.649702Z","shell.execute_reply":"2022-09-11T10:17:53.657627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n\n\ntry:\n    r = requests.post(\"https://crop2.fly.dev/score\", json=post)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:53.660169Z","iopub.execute_input":"2022-09-11T10:17:53.660616Z","iopub.status.idle":"2022-09-11T10:17:54.318570Z","shell.execute_reply.started":"2022-09-11T10:17:53.660577Z","shell.execute_reply":"2022-09-11T10:17:54.317398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"crop2\", \"YOURFLAGHERE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:54.320243Z","iopub.execute_input":"2022-09-11T10:17:54.320606Z","iopub.status.idle":"2022-09-11T10:17:54.331616Z","shell.execute_reply.started":"2022-09-11T10:17:54.320574Z","shell.execute_reply":"2022-09-11T10:17:54.330264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DeepFake (id: deepfake) -- 800 Points\n\n### Scenario\nYour very famous boss was caught saying some disparing things about dogs. Naturally, the dog lobby is not happy. To make matters worse, they paid someone to make a deepfake that was caught by a SuperSecretDeepFakeDefenseSystem.  \n\n### Objective\nFix the `sample.mp4` to bypass the SuperSecretDeepFakeDefenseSystem and get your boss out of the media.\n","metadata":{}},{"cell_type":"code","source":"#with open(f\"{dir_name}/deepfake/sample.mp4\", \"rb\") as f:\nwith open(\"../input/ctfdfake/df.mp4\", \"rb\") as f:\n    data = f.read()\nb64_data = base64.urlsafe_b64encode(data).decode()\nin_data = json.dumps({\"input\": b64_data})\nr = requests.post(\"https://deepfake.fly.dev/score\", data=in_data)\nprint(r.text)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:17:54.334013Z","iopub.execute_input":"2022-09-11T10:17:54.334625Z","iopub.status.idle":"2022-09-11T10:18:01.394451Z","shell.execute_reply.started":"2022-09-11T10:17:54.334579Z","shell.execute_reply":"2022-09-11T10:18:01.393069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"deepfake\", \"1XGL1XZQDTL01OKZGC6FK0WYF07Y14UJLA9UZZ8V4LDQBC13ORULLITRQOAXG8YD3HTVTCEQCBN4H}UOZNLONYJSH2RDXO20EC2GRMEMSMPAF7DBGQ{DG0FET5OMLPME\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.397203Z","iopub.execute_input":"2022-09-11T10:18:01.398583Z","iopub.status.idle":"2022-09-11T10:18:01.413447Z","shell.execute_reply.started":"2022-09-11T10:18:01.398508Z","shell.execute_reply":"2022-09-11T10:18:01.410582Z"},"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":"code","source":"x_dev = pd.read_json('../input/ai-village-ctf/murderbots/train_data.json')\ny_dev = pd.read_json('../input/ai-village-ctf/murderbots/train_labels.json')\ny_dev.reset_index(inplace = True)\ny_dev = y_dev.sort_values('index')\ny_dev.set_index(\"index\", inplace = True)\nx_test = pd.read_json('../input/ai-village-ctf/murderbots/test_data.json')","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.415953Z","iopub.execute_input":"2022-09-11T10:18:01.416762Z","iopub.status.idle":"2022-09-11T10:18:01.497262Z","shell.execute_reply.started":"2022-09-11T10:18:01.416710Z","shell.execute_reply":"2022-09-11T10:18:01.496019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x_dev, y_dev, test_size = 0.25, stratify = y_dev, random_state = 102022)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.499056Z","iopub.execute_input":"2022-09-11T10:18:01.499482Z","iopub.status.idle":"2022-09-11T10:18:01.515819Z","shell.execute_reply.started":"2022-09-11T10:18:01.499438Z","shell.execute_reply":"2022-09-11T10:18:01.514537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, x_val.shape, y_train.shape, y_val.shape, x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.517893Z","iopub.execute_input":"2022-09-11T10:18:01.518451Z","iopub.status.idle":"2022-09-11T10:18:01.528401Z","shell.execute_reply.started":"2022-09-11T10:18:01.518387Z","shell.execute_reply":"2022-09-11T10:18:01.527404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clf = RandomForestClassifier(max_depth=2, random_state=102022)\nclf.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.530191Z","iopub.execute_input":"2022-09-11T10:18:01.531140Z","iopub.status.idle":"2022-09-11T10:18:01.698231Z","shell.execute_reply.started":"2022-09-11T10:18:01.531090Z","shell.execute_reply":"2022-09-11T10:18:01.696946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_predict = clf.predict(x_train)\nval_predict = clf.predict(x_val)\ntest_predict = clf.predict_proba(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.699926Z","iopub.execute_input":"2022-09-11T10:18:01.701313Z","iopub.status.idle":"2022-09-11T10:18:01.754121Z","shell.execute_reply.started":"2022-09-11T10:18:01.701270Z","shell.execute_reply":"2022-09-11T10:18:01.753169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_train, train_predict))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.756127Z","iopub.execute_input":"2022-09-11T10:18:01.757027Z","iopub.status.idle":"2022-09-11T10:18:01.769061Z","shell.execute_reply.started":"2022-09-11T10:18:01.756977Z","shell.execute_reply":"2022-09-11T10:18:01.767552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, val_predict))","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.770870Z","iopub.execute_input":"2022-09-11T10:18:01.771349Z","iopub.status.idle":"2022-09-11T10:18:01.782050Z","shell.execute_reply.started":"2022-09-11T10:18:01.771314Z","shell.execute_reply":"2022-09-11T10:18:01.780879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_train, train_predict)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.783456Z","iopub.execute_input":"2022-09-11T10:18:01.784381Z","iopub.status.idle":"2022-09-11T10:18:01.794321Z","shell.execute_reply.started":"2022-09-11T10:18:01.784340Z","shell.execute_reply":"2022-09-11T10:18:01.793305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(y_val, val_predict)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.795714Z","iopub.execute_input":"2022-09-11T10:18:01.796776Z","iopub.status.idle":"2022-09-11T10:18:01.806730Z","shell.execute_reply.started":"2022-09-11T10:18:01.796738Z","shell.execute_reply":"2022-09-11T10:18:01.805303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test['Pred_proba'] = test_predict[:,1]\nx_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.808143Z","iopub.execute_input":"2022-09-11T10:18:01.808788Z","iopub.status.idle":"2022-09-11T10:18:01.825106Z","shell.execute_reply.started":"2022-09-11T10:18:01.808752Z","shell.execute_reply":"2022-09-11T10:18:01.824145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = list(x_test.sort_values('Pred_proba', ascending = False).head(10).index)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.826663Z","iopub.execute_input":"2022-09-11T10:18:01.827028Z","iopub.status.idle":"2022-09-11T10:18:01.833706Z","shell.execute_reply.started":"2022-09-11T10:18:01.826993Z","shell.execute_reply":"2022-09-11T10:18:01.832454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zero_str = '00000000000000000000000000000000000000000000000000'\nlen(zero_str)\n\ndef manual_replace(s, char, index):\n    return s[:index] + char + s[index +1:]\n\nfor index in indices:\n    zero_str = manual_replace(zero_str, '1', index)\n\nzero_str    ","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:01.835609Z","iopub.execute_input":"2022-09-11T10:18:01.836413Z","iopub.status.idle":"2022-09-11T10:18:01.848195Z","shell.execute_reply.started":"2022-09-11T10:18:01.836362Z","shell.execute_reply":"2022-09-11T10:18:01.847069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n#f = '11100110000000000000111001100000000000000000000000' # length == 50\nf = zero_str\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-11T10:18:01.849561Z","iopub.execute_input":"2022-09-11T10:18:01.851212Z","iopub.status.idle":"2022-09-11T10:18:02.233601Z","shell.execute_reply.started":"2022-09-11T10:18:01.851158Z","shell.execute_reply":"2022-09-11T10:18:02.232517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"murderbots\", \"8OAIObSC42TV}VR1V98e6o0UMtZG5OaKuIVGKtS3rALBQ{E3JBNL8T1NsAUYGLGLDRK9MFAdtE0Q8RA3oBKKP46d4SGWIAeGTJTPJ911MVFC6PNFIWXJXHELNF20NMQM\")","metadata":{"execution":{"iopub.status.busy":"2022-09-11T10:18:02.235435Z","iopub.execute_input":"2022-09-11T10:18:02.235854Z","iopub.status.idle":"2022-09-11T10:18:02.246590Z","shell.execute_reply.started":"2022-09-11T10:18:02.235802Z","shell.execute_reply":"2022-09-11T10:18:02.245289Z"},"trusted":true},"execution_count":null,"outputs":[]}]}