M2_SETI/A1/tp-spa/traces/password_sim.ipynb

83 lines
2.3 KiB
Text
Raw Normal View History

2022-10-26 10:01:19 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"import random\n",
"import time\n",
"\n",
"traces_to_load = pickle.load(open(\"traces/lab2_1b_passwords_full.p\", \"rb\"))\n",
"known_passwd = \"h0px3\"\n",
"\n",
"def cap_pass_trace(pass_guess):\n",
" global traces_to_load, known_passwd\n",
" \n",
" if pass_guess.endswith(\"\\n\") is False:\n",
" raise ValueError(\"Password guess must end with \\\\n\")\n",
" \n",
" pass_guess = pass_guess.strip(\"\\n\")\n",
" \n",
" trylist = \"abcdefghijklmnopqrstuvwxyz0123456789 \\x00\"\n",
" \n",
" if len(pass_guess) > 5:\n",
" raise ValueError(\"Only guesses up to 5 chars recorded, sorry about that.\")\n",
" \n",
" for a in pass_guess:\n",
" if a not in trylist:\n",
" raise ValueError(\"Part of guess (%c) not in recorded enumeration list (%s)\"%(a, trylist))\n",
" \n",
" #Only recorded is correct passwords\n",
" recorded_pw = \"\"\n",
" for i in range(0, len(pass_guess)):\n",
" if known_passwd[i] != pass_guess[i]:\n",
" recorded_pw += \" \"\n",
" else:\n",
" recorded_pw += pass_guess[i]\n",
" \n",
" time.sleep(0.05)\n",
" \n",
" return traces_to_load[recorded_pw][random.randint(0, 99)][:1000]\n",
"\n",
"\n",
"def pass_response(pass_guess):\n",
" if pass_guess.strip(\"\\n\") == known_passwd:\n",
" return \"Access granted, welcome!\"\n",
" else:\n",
" return \"Wrong password. Access denied!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}