{"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":"code","source":"import numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport requests\nimport json\nimport string\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:53:01.109036Z","iopub.execute_input":"2022-09-11T23:53:01.109544Z","iopub.status.idle":"2022-09-11T23:53:01.139239Z","shell.execute_reply.started":"2022-09-11T23:53:01.109439Z","shell.execute_reply":"2022-09-11T23:53:01.138326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_scores(x):\n    try:\n        response = requests.post(\"https://inference.fly.dev/score\", data = json.dumps({\"input\": x.tolist()}))\n        response = json.loads(response.text)['outputs']\n        return np.array(response)\n    except requests.exceptions.ConnectionError:\n        print(\"Connection problems. Contact the CTF organizers.\")\n        return False\n    \ndef get_single_scores(x, num):\n    return get_scores(x)[:,num]","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:53:01.141121Z","iopub.execute_input":"2022-09-11T23:53:01.141865Z","iopub.status.idle":"2022-09-11T23:53:01.149490Z","shell.execute_reply.started":"2022-09-11T23:53:01.141829Z","shell.execute_reply":"2022-09-11T23:53:01.147967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = np.full((10*32*32,32,32,1), 0.5)\n\nprint(batch.shape)\n\nstep = 0.1\n\ncnt = 0\nfor i in range(0,32):\n    for j in range(0,32):\n        for c in range(10):\n            batch[cnt,i,j,0] = c*step\n            cnt += 1\n            \nscores = get_scores(batch)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:53:01.151614Z","iopub.execute_input":"2022-09-11T23:53:01.152774Z","iopub.status.idle":"2022-09-11T23:53:41.656980Z","shell.execute_reply.started":"2022-09-11T23:53:01.152691Z","shell.execute_reply":"2022-09-11T23:53:41.654988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\n\nfor x in range(6):\n    image = []\n    scores_single = scores[:,x]\n    cnt = 0\n    for i in range(0,32):\n        for j in range(0,32):\n            pixel = []\n            for c in range(10):\n                pixel.append(scores_single[cnt])\n                cnt += 1\n            image.append(max(pixel))  \n    images.append(np.array(image).reshape(32,32))\n\nfig = plt.figure(figsize=(12, 6))\ncolumns = 3\nrows = 2\nfor i in range(1,7):\n    img = images[i-1]\n    fig.add_subplot(rows, columns, i)\n    i += 1\n    plt.axis('off')\n    plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:53:41.660179Z","iopub.execute_input":"2022-09-11T23:53:41.660772Z","iopub.status.idle":"2022-09-11T23:53:42.041926Z","shell.execute_reply.started":"2022-09-11T23:53:41.660736Z","shell.execute_reply":"2022-09-11T23:53:42.038986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"candidates_0 = string.ascii_uppercase + string.digits\ncandidates_1 = '3'\ncandidates_2 = 'F'\ncandidates_3 = 'C'\ncandidates_4 = '0O'\ncandidates_5 = string.ascii_uppercase + string.digits\n\nvariants = len(candidates_0) * len(candidates_1) * len(candidates_2) * len(candidates_3) * len(candidates_4) * len(candidates_5) \n\ndef find_flag():\n    count = 0\n    with tqdm(total=variants) as pbar:\n        for s0 in candidates_0:\n            for s1 in candidates_1:\n                for s2 in candidates_2:\n                    for s3 in candidates_3:\n                        for s4 in candidates_4:\n                            for s5 in candidates_5:\n                                FLAG = '{' + s0 + s1 + s2 + s3 + s4 + s5 + '}'\n                                #if count == 0:\n                                #    print(FLAG)\n                                count += 1\n                                #continue                                \n                                try:\n                                    r = requests.post(\"http://flags.fly.dev:5000/score\", json=json.dumps({\"challenge_id\": \"inference\", \"submission\": f\"FLAG{FLAG}\"}))\n                                    if r.text != \"That doesn't look right. Try again.\":\n                                        print(r.text)\n                                        print(FLAG)\n                                        return      \n                                except:\n                                    print(f'Error. Flag: {FLAG}')\n                                pbar.update(1)\n\nfind_flag()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T23:53:42.043603Z","iopub.execute_input":"2022-09-11T23:53:42.043984Z"},"trusted":true},"execution_count":null,"outputs":[]}]}