def establish_session(self, username, node, password): self._log("[+] Establishing session...") # reset the socket self.wrappedSocket.send_chat_message("</k>".encode('UTF-8')) self.wrappedSocket.close() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.settimeout(10) self.wrappedSocket = ssl.wrap_socket(self.sock) self.wrappedSocket.connect((HOST, PORT)) jid = node + "@talk.kik.com" jid_with_resource = jid + "/CAN" + self.device_id timestamp = "1496333389122" sid = CryptographicUtils.make_kik_uuid() version = "11.1.1.12218" # some super secret cryptographic stuff private_key_pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIBPAIBAAJBANEWUEINqV1KNG7Yie9GSM8t75ZvdTeqT7kOF40kvDHIp" \ "/C3tX2bcNgLTnGFs8yA2m2p7hKoFLoxh64vZx5fZykCAwEAAQJAT" \ "/hC1iC3iHDbQRIdH6E4M9WT72vN326Kc3MKWveT603sUAWFlaEa5T80GBiP/qXt9PaDoJWcdKHr7RqDq" \ "+8noQIhAPh5haTSGu0MFs0YiLRLqirJWXa4QPm4W5nz5VGKXaKtAiEA12tpUlkyxJBuuKCykIQbiUXHEwzFYbMHK5E" \ "/uGkFoe0CIQC6uYgHPqVhcm5IHqHM6/erQ7jpkLmzcCnWXgT87ABF2QIhAIzrfyKXp1ZfBY9R0H4pbboHI4uatySKc" \ "Q5XHlAMo9qhAiEA43zuIMknJSGwa2zLt/3FmVnuCInD6Oun5dbcYnqraJo=\n-----END RSA PRIVATE KEY----- " private_key = rsa.PrivateKey.load_pkcs1(private_key_pem, format='PEM') signature = rsa.sign( "{}:{}:{}:{}".format(jid, version, timestamp, sid).encode('UTF-8'), private_key, 'SHA-256') signature = base64.b64encode(signature, '-_'.encode('UTF-8')).decode('UTF-8')[:-2] hmac_data = timestamp + ":" + jid hmac_secret_key = CryptographicUtils.build_hmac_key() cv = binascii.hexlify( hmac.new(hmac_secret_key, hmac_data.encode('UTF-8'), hashlib.sha1).digest()).decode('UTF-8') password_key = CryptographicUtils.key_from_password(username, password) the_map = { 'from': jid_with_resource, 'to': 'talk.kik.com', 'p': password_key, 'cv': cv, 'v': version, 'sid': sid, 'n': '1', 'conn': 'WIFI', 'ts': timestamp, 'lang': 'en_US', 'signed': signature } packet = CryptographicUtils.make_connection_payload( CryptographicUtils.sort_kik_map(the_map)).encode('UTF-8') # send session request self.wrappedSocket.send_chat_message(packet) response = self.wrappedSocket.recv(16384).decode('UTF-8') if "ok" not in response: raise KikErrorException(response, "Could not init session: " + response) self._log("[+] Session established.")
def serialize(self): jid = self.node + "@talk.kik.com" jid_with_resource = jid + "/CAN" + ( self.device_id_override if self.device_id_override else device_id) timestamp = "1496333389122" sid = CryptographicUtils.make_kik_uuid() # some super secret cryptographic stuff private_key_pem = "-----BEGIN RSA PRIVATE KEY-----\nMIIBPAIBAAJBANEWUEINqV1KNG7Yie9GSM8t75ZvdTeqT7kOF40kvDHIp" \ "/C3tX2bcNgLTnGFs8yA2m2p7hKoFLoxh64vZx5fZykCAwEAAQJAT" \ "/hC1iC3iHDbQRIdH6E4M9WT72vN326Kc3MKWveT603sUAWFlaEa5T80GBiP/qXt9PaDoJWcdKHr7RqDq" \ "+8noQIhAPh5haTSGu0MFs0YiLRLqirJWXa4QPm4W5nz5VGKXaKtAiEA12tpUlkyxJBuuKCykIQbiUXHEwzFYbMHK5E" \ "/uGkFoe0CIQC6uYgHPqVhcm5IHqHM6/erQ7jpkLmzcCnWXgT87ABF2QIhAIzrfyKXp1ZfBY9R0H4pbboHI4uatySKc" \ "Q5XHlAMo9qhAiEA43zuIMknJSGwa2zLt/3FmVnuCInD6Oun5dbcYnqraJo=\n-----END RSA PRIVATE KEY----- " private_key = rsa.PrivateKey.load_pkcs1(private_key_pem, format='PEM') signature = rsa.sign( "{}:{}:{}:{}".format(jid, kik_version, timestamp, sid).encode(), private_key, 'SHA-256') signature = base64.b64encode(signature, '-_'.encode()).decode()[:-2] hmac_data = timestamp + ":" + jid hmac_secret_key = CryptographicUtils.build_hmac_key() cv = binascii.hexlify( hmac.new(hmac_secret_key, hmac_data.encode(), hashlib.sha1).digest()).decode() password_key = CryptographicUtils.key_from_password( self.username, self.password) the_map = { 'from': jid_with_resource, 'to': 'talk.kik.com', 'p': password_key, 'cv': cv, 'v': kik_version, 'sid': sid, 'n': '1', 'conn': 'WIFI', 'ts': timestamp, 'lang': 'en_US', 'signed': signature } packet = CryptographicUtils.make_connection_payload( CryptographicUtils.sort_kik_map(the_map)).encode() return packet