{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hash brute forcing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## solo" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zzzzz\n", "CPU times: user 33.5 s, sys: 34.8 ms, total: 33.5 s\n", "Wall time: 33.6 s\n" ] } ], "source": [ "from hashlib import md5\n", "from itertools import product\n", "from string import ascii_lowercase\n", "\n", "def all_words(alphabet, length):\n", " return (''.join(letters) for letters in product(alphabet, repeat=length))\n", "\n", "def is_collision(passwd, hash_value):\n", " return md5(passwd.encode('ascii')).hexdigest() == hash_value\n", "\n", "def brute_force(hash_value, alphabet, length, begin=0, end=None):\n", " end = len(alphabet) if end is None else end\n", " for first_letter in alphabet[begin:end]:\n", " for word in all_words(alphabet, length - 1):\n", " passwd = first_letter + word\n", " if is_collision(passwd, hash_value):\n", " return passwd\n", "\n", "%time print(brute_force('95ebc3c7b3b9f1d2c40fec14415d3cb8', ascii_lowercase, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## threading" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zzzzz\n", "None\n", "CPU times: user 42.1 s, sys: 468 ms, total: 42.6 s\n", "Wall time: 42.7 s\n" ] } ], "source": [ "import threading\n", "from hashlib import md5\n", "from itertools import product, tee, islice\n", "from string import ascii_lowercase\n", "\n", "def all_words(alphabet, length):\n", " return (''.join(letters) for letters in product(alphabet, repeat=length))\n", "\n", "def is_collision(passwd, hash_value):\n", " return md5(passwd.encode('ascii')).hexdigest() == hash_value\n", "\n", "def brute_force(hash_value, alphabet, length, begin=0, end=None):\n", " end = len(alphabet) if end is None else end\n", " for first_letter in alphabet[begin:end]:\n", " for word in all_words(alphabet, length - 1):\n", " passwd = first_letter + word\n", " if is_collision(passwd, hash_value):\n", " print(passwd)\n", " return passwd\n", "\n", "def pairs(iterable):\n", " items, nexts = tee(iterable, 2)\n", " nexts = islice(nexts, 1, None)\n", " return zip(items, nexts)\n", "\n", "def parallel_brute_force(hash_value, alphabet, length):\n", " pool = []\n", " partition = [0, 13, 26]\n", " for p in pairs(partition):\n", " worker = threading.Thread(target=brute_force, args=(hash_value, alphabet, length, p[0], p[1]))\n", " worker.start()\n", " pool.append(worker)\n", " for worker in pool:\n", " worker.join()\n", "\n", "%time print(parallel_brute_force('95ebc3c7b3b9f1d2c40fec14415d3cb8', ascii_lowercase, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## multiprocessing" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "CPU times: user 3.5 ms, sys: 7.96 ms, total: 11.5 ms\n", "Wall time: 18.6 s\n", "zzzzz\n" ] } ], "source": [ "import multiprocessing\n", "from hashlib import md5\n", "from itertools import product, tee, islice\n", "from string import ascii_lowercase\n", "\n", "def all_words(alphabet, length):\n", " return (''.join(letters) for letters in product(alphabet, repeat=length))\n", "\n", "def is_collision(passwd, hash_value):\n", " return md5(passwd.encode('ascii')).hexdigest() == hash_value\n", "\n", "def brute_force(hash_value, alphabet, length, begin=0, end=None):\n", " end = len(alphabet) if end is None else end\n", " for first_letter in alphabet[begin:end]:\n", " for word in all_words(alphabet, length - 1):\n", " passwd = first_letter + word\n", " if is_collision(passwd, hash_value):\n", " print(passwd)\n", " return passwd\n", "\n", "def pairs(iterable):\n", " items, nexts = tee(iterable, 2)\n", " nexts = islice(nexts, 1, None)\n", " return zip(items, nexts)\n", "\n", "def parallel_brute_force(hash_value, alphabet, length):\n", " pool = []\n", " partition = [0, 13, 26]\n", " for p in pairs(partition):\n", " worker = multiprocessing.Process(target=brute_force, args=(hash_value, alphabet, length, p[0], p[1]))\n", " worker.start()\n", " pool.append(worker)\n", " for worker in pool:\n", " worker.join()\n", "\n", "%time print(parallel_brute_force('95ebc3c7b3b9f1d2c40fec14415d3cb8', ascii_lowercase, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## concurrent.futures" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "zzzzz\n", "CPU times: user 22.7 ms, sys: 12.5 ms, total: 35.2 ms\n", "Wall time: 19.4 s\n" ] } ], "source": [ "from concurrent.futures import ProcessPoolExecutor\n", "from hashlib import md5\n", "from itertools import product, tee, islice\n", "from string import ascii_lowercase\n", "\n", "def all_words(alphabet, length):\n", " return (''.join(letters) for letters in product(alphabet, repeat=length))\n", "\n", "def is_collision(passwd, hash_value):\n", " return md5(passwd.encode('ascii')).hexdigest() == hash_value\n", "\n", "def brute_force(hash_value, alphabet, length, begin=0, end=None):\n", " end = len(alphabet) if end is None else end\n", " for first_letter in alphabet[begin:end]:\n", " for word in all_words(alphabet, length - 1):\n", " passwd = first_letter + word\n", " if is_collision(passwd, hash_value):\n", " return passwd\n", "\n", "def pairs(iterable):\n", " items, nexts = tee(iterable, 2)\n", " nexts = islice(nexts, 1, None)\n", " return zip(items, nexts)\n", "\n", "def parallel_brute_force(hash_value, alphabet, length):\n", " futures = []\n", " with ProcessPoolExecutor(max_workers=2) as executor:\n", " partition = range(len(alphabet) + 1)\n", " for p in pairs(partition):\n", " future = executor.submit(brute_force, hash_value, alphabet, length, p[0], p[1]) \n", " futures.append(future)\n", " results = [future.result() for future in futures]\n", " collisions = [r for r in results if r is not None]\n", " if collisions:\n", " return collisions[0]\n", " \n", "%time print(parallel_brute_force('95ebc3c7b3b9f1d2c40fec14415d3cb8', ascii_lowercase, 5))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }