|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# The encryption algorithm" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import sys\n", |
| 17 | + "from struct import pack, unpack\n", |
| 18 | + "\n", |
| 19 | + "def F(w):\n", |
| 20 | + " return ((w * 31337) ^ (w * 1337 >> 16)) % 2**32\n", |
| 21 | + "\n", |
| 22 | + "def encrypt(block):\n", |
| 23 | + " a, b, c, d = unpack(\"<4I\", block)\n", |
| 24 | + " for rno in xrange(32):\n", |
| 25 | + " a, b, c, d = b ^ F(a | F(c ^ F(d)) ^ F(a | c) ^ d), c ^ F(a ^ F(d) ^ (a | d)), d ^ F(a | F(a) ^ a), a ^ 31337\n", |
| 26 | + " a, b, c, d = c ^ F(d | F(b ^ F(a)) ^ F(d | b) ^ a), b ^ F(d ^ F(a) ^ (d | a)), a ^ F(d | F(d) ^ d), d ^ 1337\n", |
| 27 | + " return pack(\"<4I\", a, b, c, d)" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "# Reverse engineering the encryption algorithm\n", |
| 35 | + "- First it's noticed that the algorithm works on blocks of 16 bytes\n", |
| 36 | + "- Each block is divided into four 4-byte ints (a,b,c,d)\n", |
| 37 | + "- 32 Rounds are performed on these ints\n", |
| 38 | + "- Each round performs two operations on these 4 ints\n", |
| 39 | + "- We will need to figure out a way to reverse these functions" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 2, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [ |
| 48 | + "def reverse_second_op(a1, b1, c1, d1):\n", |
| 49 | + " # Variables should be decoded in the given sequence d0, a0, b0, c0\n", |
| 50 | + " d0 = d1 ^ 1337\n", |
| 51 | + " \n", |
| 52 | + " fd0 = F(d0 | F(d0) ^ d0)\n", |
| 53 | + " a0 = c1 ^ fd0\n", |
| 54 | + "\n", |
| 55 | + " fad0 = F(d0 ^ F(a0) ^ (d0 | a0))\n", |
| 56 | + " b0 = b1 ^ fad0\n", |
| 57 | + "\n", |
| 58 | + " fabd0 = F(d0 | F(b0 ^ F(a0)) ^ F(d0 | b0) ^ a0)\n", |
| 59 | + "\n", |
| 60 | + " c0 = a1 ^ fabd0\n", |
| 61 | + " \n", |
| 62 | + " return a0, b0, c0, d0" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 3, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "def reverse_first_op(a1, b1, c1, d1):\n", |
| 72 | + " a0 = d1 ^ 31337\n", |
| 73 | + " \n", |
| 74 | + " fa0 = F(a0 | F(a0) ^ a0)\n", |
| 75 | + " d0 = c1 ^ fa0\n", |
| 76 | + " \n", |
| 77 | + " fad0 = F(a0 ^ F(d0) ^ (a0 | d0))\n", |
| 78 | + " c0 = b1 ^ fad0\n", |
| 79 | + " \n", |
| 80 | + " facd0 = F(a0 | F(c0 ^ F(d0)) ^ F(a0 | c0) ^ d0)\n", |
| 81 | + " b0 = a1 ^ facd0\n", |
| 82 | + " \n", |
| 83 | + " return a0, b0, c0, d0" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 4, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "def decrypt(block):\n", |
| 93 | + " a, b, c, d = unpack(\"<4I\", block)\n", |
| 94 | + " for rno in range(32):\n", |
| 95 | + " # Second operation is reversed first\n", |
| 96 | + " a, b, c, d = reverse_second_op(a, b, c, d)\n", |
| 97 | + " # First operation is reversed\n", |
| 98 | + " a, b, c, d = reverse_first_op(a, b, c, d)\n", |
| 99 | + " \n", |
| 100 | + " return pack(\"<4I\", a, b, c, d)" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "# How to encrypt a new message?" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": 5, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "def encrypt_file(file_name):\n", |
| 117 | + " pt = open(file_name).read()\n", |
| 118 | + " while len(pt) % 16: pt += \"#\"\n", |
| 119 | + "\n", |
| 120 | + " ct = \"\".join(encrypt(pt[i:i+16]) for i in xrange(0, len(pt), 16))\n", |
| 121 | + " open(file_name + \".enc\", \"w\").write(ct)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "# How to decrypt an encrypted message?" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 6, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "def decrypt_file(file_name):\n", |
| 138 | + " pt = open(file_name, 'rb').read()\n", |
| 139 | + " # No need to add any padding\n", |
| 140 | + "\n", |
| 141 | + " # Decode the bytes stream using .decode() before joining the list\n", |
| 142 | + " ct = ''.join([decrypt(pt[i:i+16]).decode() for i in range(0, len(pt), 16)])\n", |
| 143 | + " print(ct)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": 7, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "FLAG{G3N3R4L123D_F31573L_EZ!}###\n" |
| 156 | + ] |
| 157 | + } |
| 158 | + ], |
| 159 | + "source": [ |
| 160 | + "decrypt_file('assets/genfei/flag.enc')" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "metadata": { |
| 165 | + "kernelspec": { |
| 166 | + "display_name": "Python 3", |
| 167 | + "language": "python", |
| 168 | + "name": "python3" |
| 169 | + }, |
| 170 | + "language_info": { |
| 171 | + "codemirror_mode": { |
| 172 | + "name": "ipython", |
| 173 | + "version": 3 |
| 174 | + }, |
| 175 | + "file_extension": ".py", |
| 176 | + "mimetype": "text/x-python", |
| 177 | + "name": "python", |
| 178 | + "nbconvert_exporter": "python", |
| 179 | + "pygments_lexer": "ipython3", |
| 180 | + "version": "3.6.6" |
| 181 | + } |
| 182 | + }, |
| 183 | + "nbformat": 4, |
| 184 | + "nbformat_minor": 2 |
| 185 | +} |
0 commit comments