def __init__(self, wrapper): self.wrapper = wrapper self.server = wrapper.server self.socket = False self.isServer = False self.clients = [] self.skins = {} self.skinTextures = {} self.uuidTranslate = {} self.storage = storage.Storage("proxy-data") self.privateKey = encryption.generate_key_pair() self.publicKey = encryption.encode_public_key(self.privateKey)
def _handle_client(self, connection): with closing(connection[0]) as sock: clt_spec, srv_spec = protocol[0] print t.bold("\nConnected to %s:%s" % connection[1]) print t.bold_cyan("\nExpecting Server Ping (0xfe) " + "or Handshake (0x02) packet") packet = parse_packet(sock, clt_spec) if packet['msgtype'] == 0xfe: send_packet(sock, srv_spec, {'msgtype': 0xff, 'reason': 'mc3p debugger'}) return elif packet['msgtype'] != 0x02: raise UnexpectedPacketException(packet['msgtype']) if packet['proto_version'] < 38: print t.bold_red("Error:"), print "Unsupported protocol version" return username = packet['username'] clt_spec, srv_spec = protocol[packet['proto_version']] print t.bold("\nGenerating RSA key pair") key = encryption.generate_key_pair() challenge = encryption.generate_challenge_token() server_id = encryption.generate_server_id() packet = {'msgtype': 0xfd, 'server_id': server_id, 'public_key': encryption.encode_public_key(key), 'challenge_token': challenge} send_packet(sock, srv_spec, packet) packet = parse_packet(sock, clt_spec, 0xfc) try: decrypted_token = encryption.decrypt_shared_secret( packet['challenge_token'], key ) except: decrypted_token = None if decrypted_token is None: try: decrypted_token = key.decrypt(packet['challenge_token']) except: pass if decrypted_token == challenge: print t.bold_red("\nError:"), print ("The challenge token was not padded " + "correctly. See ftp://ftp.rsasecurity.com/pub/" + "pkcs/pkcs-1/pkcs-1v2-1.pdf section 7.2.1 if " + "your library does not support PKCS#1 padding.") else: print t.bold_red("\nError:"), print "The challenge token is not encrypted correctly.\n" print PacketFormatter.bytes(decrypted_token, "Decrypted bytes: ", t.bold) return elif decrypted_token != challenge: print t.bold_red("\nError:"), print "Received challenge token does not", print "match the expected value.\n" print PacketFormatter.bytes(decrypted_token, "Received bytes: ", t.bold) print print PacketFormatter.bytes(challenge, "Expected bytes: ", t.bold) return secret = encryption.decrypt_shared_secret(packet['shared_secret'], key) if secret is None: print t.bold_red("\nError:"), print ("The shared secret was not padded" + "correctly. See ftp://ftp.rsasecurity.com/pub/" + "pkcs/pkcs-1/pkcs-1v2-1.pdf section 7.2.1 if " + "your library does not support PKCS#1 padding.") return print PacketFormatter.bytes(secret, "Shared secret: ", t.bold) if len(secret) != 16: print t.bold_red("\nError:"), print "The shared secret must be 16 bytes long", print "(received length is %s)" % len(secret) return print t.bold_cyan("\nAuthentication") print PacketFormatter.bytes(server_id, "Server ID: ", t.bold) print PacketFormatter.bytes(secret, "Shared secret: ", t.bold) print PacketFormatter.bytes(encryption.encode_public_key(key), "Public key: ", t.bold) print t.bold("Login hash: "), print Authenticator.login_hash(server_id, secret, key) if Authenticator.check_player(username, server_id, secret, key): print t.bold_green("Success:"), "You are authenticated" else: print t.bold_yellow("Warning:"), "You are not authenticated" send_packet(sock, srv_spec, {'msgtype': 0xfc, 'challenge_token': '', 'shared_secret': ''}) print t.bold("\nStarting AES encryption") clt_cipher = encryption.AES128CFB8(secret) srv_cipher = encryption.AES128CFB8(secret) backup_cipher = encryption.AES128CFB8(secret) parse_packet(sock, clt_spec, 0xcd, clt_cipher, backup_cipher) send_packet(sock, srv_spec, {'msgtype': 0x01, 'eid': 1337, 'level_type': 'flat', 'server_mode': 0, 'dimension': 0, 'difficulty': 2, 'unused': 0, 'max_players': 20}, srv_cipher) if self.send_chunks: while True: print packet = parse_packet(sock, clt_spec, cipher=clt_cipher) if packet['msgtype'] == 0x0d: break x, y, z = 5, 9, 5 send_packet(sock, srv_spec, {'msgtype': 0x06, 'x': x, 'y': y, 'z': z}, srv_cipher) send_packet(sock, srv_spec, {'msgtype': 0xca, 'abilities': 0b0100, 'walking_speed': 25, 'flying_speed': 12}, srv_cipher) send_packet(sock, srv_spec, {'msgtype': 0x04, 'time': 0}, srv_cipher) send_packet(sock, srv_spec, multi_chunk_packet(), srv_cipher) send_packet(sock, srv_spec, {'msgtype': 0x0d, 'x': x, 'y': y, 'stance': y + 1.5, 'z': z, 'yaw': 0, 'pitch': 0, 'on_ground': False}, srv_cipher) buffer = StringSocket() send_packet(buffer, srv_spec, {'msgtype': 0x03, 'chat_msg': 'First message'}, srv_cipher) send_packet(buffer, srv_spec, {'msgtype': 0x03, 'chat_msg': 'Second message'}, srv_cipher) sock.sendall(buffer.data) if self.stay_connected: while True: packet = parse_packet(sock, clt_spec, cipher=clt_cipher, title=True) if packet['msgtype'] == 0xff: break elif packet['msgtype'] == 0x00: send_packet(buffer, srv_spec, {'msgtype': 0x00, 'id': 0}, srv_cipher) break else: send_packet(sock, srv_spec, {'msgtype': 0xff, 'reason': "Successfully logged in"}, srv_cipher)
def generate_rsa_key_pair(): global rsa_key logger.debug('Generating RSA key pair') rsa_key = encryption.generate_key_pair()