1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
import os import binascii from random import random
from flask import request import json from libnum import n2s, s2n from secret import flag, K_as, K_bs from flask import Flask from hashlib import md5 import time from Crypto.Cipher import AES from os import urandom
app = Flask(__name__)
flag += '\x00' * (- len(flag) % 16)
A = md5(b'A').hexdigest()[:16] B = md5(b'B').hexdigest()[:16] S = md5(b'S').hexdigest()[:16] K_sc = {A: K_as, B: K_bs} name2hex = {"A": A, "B": B, "S": S}
hex_chars = set(list('0123456789abcdefg'))
last_time = time.time()
history = {} keys = {} if os.path.exists('keys.txt'): try: keys = json.loads(open('keys.txt', 'r').read()) except Exception as e: keys = {} print(e)
if os.path.exists('K_sc.txt'): tag = 0 cur_sc = {} try: cur_sc = json.loads(open("K_sc.txt", 'r').read()) tag = 1 except Exception as e: tag = 0 print(e) if tag: K_sc.update(cur_sc)
def update_keys(cur_token, left, right, cur_key): global keys if cur_token not in keys: keys[cur_token] = {left: {right: cur_key}} else: cur_dict = keys[cur_token] if left not in cur_dict: cur_dict[left] = {right: cur_key} else: cur_dict[left][right] = cur_key cur_time = time.time() global last_time if cur_time - last_time > 666: open('keys.txt', 'w').write(json.dumps(keys)) last_time = cur_time return True
def check_fresh(t): cur = time.time() if cur - t > 24: return False return True
def get_token(args): cur_token = args.get('token', None) if cur_token is None: return False, {"message": "Add your Token"} cur_token = cur_token.ljust(16, '0')[:16] for char in cur_token: if char not in hex_chars: return False, {"message": "Your token should be made of hex"} return True, cur_token
def add_to_history(cur_log, cur_token): global history if cur_token in history: history[cur_token].append(cur_log) else: history[cur_token] = [cur_log]
def dec_and_split_message(cli, message, need_hex=False): message = binascii.unhexlify(message) scheme = AES.new(binascii.unhexlify(K_sc[cli]), AES.MODE_ECB) message = scheme.decrypt(message) if need_hex: t, target, cur_key = int(message[:16].hex(), 16), message[16: 24].hex(), message[32: 48].hex() else: t, target, cur_key = int(message[:16].hex(), 16), message[16: 24], message[32: 48] return t, target, cur_key
def send_to_client(cur_token, target, message): if not len(message) == 16 * 3 * 2: return "Your message is not valid" cur_log = f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: S -> {target}: {message}' add_to_history(cur_log, cur_token) t, friend, cur_key = dec_and_split_message(target, message, need_hex=True) if not check_fresh(t): return "This is an old message, get out" return update_keys(cur_token, target, friend, cur_key)
def send_to_server(cur_token, orig_message): if not len(orig_message) == 16 * 3 * 2 + 16: return "Your message is not valid" sender, message = orig_message[0: 16], orig_message[16:] t, target, cur_key = dec_and_split_message(sender, message) if not check_fresh(t): return "This is an old message, get out"
cur_log = f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: {sender} -> S: {orig_message}' add_to_history(cur_log, cur_token) suffix = os.urandom(8) wait_to_send_m = n2s(int(time.time())).rjust(16, b'0') + binascii.unhexlify(sender) + suffix + cur_key target = target.hex() scheme = AES.new(binascii.unhexlify(K_sc[target]), AES.MODE_ECB) cipher = scheme.encrypt(wait_to_send_m).hex() send_to_client(cur_token, target, cipher) return True
def send_message(cur_token, target, message): if target == S: return send_to_server(cur_token, message) else: return send_to_client(cur_token, target, message)
def exchange_key(cur_token): k_ab = os.urandom(16) if random() > 0.5: sender = A target = B else: sender = B target = A suffix = os.urandom(8) message = n2s(int(time.time())).rjust(16, b'0') + binascii.unhexlify(target) + suffix + k_ab scheme = AES.new(binascii.unhexlify(K_sc[sender]), AES.MODE_ECB) cipher = scheme.encrypt(message).hex() message = sender + cipher ans = send_message(cur_token, S, message) if ans is not True: return ans update_keys(cur_token, sender, target, k_ab.hex()) return {'message': 'A and B have completed key exchange!'}
@app.route('/A_and_B', methods=['GET']) def index(): bol, message = get_token(request.args) if not bol: return {"message": message} cur_token = message return exchange_key(cur_token)
@app.route('/register', methods=['GET']) def register(): bol, message = get_token(request.args) if not bol: return {"message": message} cur_token = message if cur_token in K_sc: cur_key = binascii.unhexlify(K_sc[cur_token]) else: cur_key = os.urandom(16) K_sc[cur_token] = cur_key.hex() cur_time = time.time() global last_time if cur_time - last_time > 666: open("K_sc.txt", 'w').write(json.dumps(K_sc)) last_time = cur_time return {'token': cur_token, 'key_to_server': cur_key.hex()}
@app.route('/send_message', methods=['GET']) def execute_protocol(): bol, message = get_token(request.args) if not bol: return {"message": message} cur_token = message target = request.args.get('to') message = request.args.get('message') send_message(cur_token, name2hex[target], message) return {"message": f"You have sent {message} to {target}"}
@app.route('/view_history', methods=['GET']) def list_history(): bol, message = get_token(request.args) if not bol: return {"message": message} cur_token = message if cur_token not in history: return {"message": "Invalid token or this token has not generated any histories."} cur_histories = history[cur_token] return_histories = cur_histories[0:] return_histories.reverse()
return {"history": return_histories}
@app.route('/send_flag', methods=['GET']) def send_flag(): bol, message = get_token(request.args) if not bol: return {"message": message} cur_token = message if random() > 0.5: sender = A target = B else: sender = B target = A if keys[cur_token][sender][target] != keys[cur_token][target][sender]: exchange_key(cur_token) return {"message": "Keys between A and B are different! There must be something malicious in our system!"} cur_key = binascii.unhexlify(keys[cur_token][sender][target]) scheme = AES.new(cur_key, AES.MODE_ECB) cipher = scheme.encrypt(flag.encode()).hex() cur_log = f'{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}: {sender} -> {target}: {cipher}' add_to_history(cur_log, cur_token) return {"message": f'{sender} has sent flag secretly to {target}!'}
if __name__ == '__main__': app.run(host='0.0.0.0', port=12138, debug=True)
|