def ReadBuffer(): try: data = clientsocket.recv(32) packet_length = diameter.decode_diameter_packet_length(data) #Calculate length of packet from start of packet data_sum = data + clientsocket.recv(packet_length - 32) #Recieve remainder of packet from buffer packet_vars, avps = diameter.decode_diameter_packet(data_sum) print("Got response from " + str(hostname)) for keys in packet_vars: print("\t" + str(keys) + "\t" + str(packet_vars[keys])) for avp in avps: print(avp['avp_code']) if int(avp['avp_code']) == 318: print("Received Authentication Information Answer - Store output of Crypto vectors?") file.open("vectors.txt", "w") file.write(avp['misc_data']) file.close() print("Command Code: " + str(packet_vars['command_code'])) if int(packet_vars['command_code']) == 280: print("Recieved DWR - Sending DWA") #SendRequest(diameter.Answer_280(packet_vars, avps)) #ToDo - Fix loop here if int(packet_vars['command_code']) == 257: print("Recieved CER - Sending CEA") #SendRequest(diameter.Answer_257(packet_vars, avps, recv_ip)) #ToDo - Fix loop here if input("Print AVPs (Y/N):\t") == "Y": for avp in avps: print("\t\t" + str(avp)) except Exception as e: print("failed to get all return data - Error " + str(e))
def ReadBuffer(): try: data = clientsocket.recv(32) packet_length = diameter.decode_diameter_packet_length(data) #Calculate length of packet from start of packet data_sum = data + clientsocket.recv(packet_length - 32) #Recieve remainder of packet from buffer packet_vars, avps = diameter.decode_diameter_packet(data_sum) print("Got response from " + str(hostname)) print("Receive responding from server ----") print("packet_vars", packet_vars) print("avps", avps) for keys in packet_vars: print("\t" + str(keys) + "\t" + str(packet_vars[keys])) for avp in avps: print(avp['avp_code']) if int(avp['avp_code']) == 318: print("Received Authentication Information Answer - Store output of Crypto vectors?") file.open("vectors.txt", "w") file.write(avp['misc_data']) file.close() print("Command Code: " + str(packet_vars['command_code'])) if int(packet_vars['command_code']) == 280: print("Recieved DWR - Sending DWA") SendRequest(diameter.Answer_280(packet_vars, avps)) if int(packet_vars['command_code']) == 257: print("Recieved CER - Sending CEA") print("-------Client da nhan duoc Answer_257 tu HSS") print(f"client goi lai Answer_257 cho server recv_ip {recv_ip} ") message_client_Answer_257 = diameter.Answer_257(packet_vars, avps, recv_ip) print("message_Answer_257_client se goi cho server", message_client_Answer_257) #SendRequest(diameter.Answer_257(packet_vars, avps, recv_ip)) print("try to decode message_Answer_257_from_client") packet_vars, avps = diameter.decode_diameter_packet(message_client_Answer_257) print("packet_vars Ans257", packet_vars) print("avps Ans257", avps) SendRequest(message_client_Answer_257) if input("Print AVPs (Y/N):\t") == "Y" or input("Print AVPs (Y/N):\t") == "y": for avp in avps: print("\t\t" + str(avp)) except Exception as e: print("failed to get all return data - Error " + str(e))
def ReadBuffer(): while True: try: data = clientsocket.recv(32) packet_length = diameter.decode_diameter_packet_length( data) #Calculate length of packet from start of packet data_sum = data + clientsocket.recv( packet_length - 32) #Recieve remainder of packet from buffer packet_vars, avps = diameter.decode_diameter_packet(data_sum) if int(packet_vars['command_code']) == 280 and diameter.hex_to_bin( packet_vars['flags'])[0] == "1": # Recieve DWR ,send DWA print("Recieved DWR - Sending DWA to " + str(hostname)) SendRequest(diameter.Answer_280(packet_vars, avps)) continue print("Got response from " + str(hostname)) for keys in packet_vars: print("\t" + str(keys) + "\t" + str(packet_vars[keys])) print("Command Code: " + str(packet_vars['command_code'])) if int(packet_vars['command_code']) == 280: flags_bin = diameter.hex_to_bin(packet_vars['flags']) print("Flags are " + str(flags_bin)) print("Recieved DWA") if int(packet_vars['command_code']) == 257: #Check if Request or Response flags_bin = diameter.hex_to_bin(packet_vars['flags']) print("Flags are " + str(flags_bin)) #ToDo - check first byte only if flags_bin[0] == '1': print("Recieved CER - Sending CEA") SendRequest(diameter.Answer_257(packet_vars, avps, recv_ip)) else: print("Is CEA") if input("Print AVPs (Y/N):\t") == "Y": for avp in avps: print("\t\t" + str(avp)) except KeyboardInterrupt: print("User exited background loop") break except Exception as e: print("failed to get all return data - Error " + str(e))
def setup(self): print('New connection from ' + str(self.client_address[0])) while 1: data = self.request.recv(32) if not data: print("Connection closed by " + str(self.client_address[0])) break packet_length = diameter.decode_diameter_packet_length( data) #Calculate length of packet from start of packet data_sum = data + self.request.recv( packet_length - 32) #Recieve remainder of packet from buffer packet_vars, avps = diameter.decode_diameter_packet( data_sum ) #Decode packet into array of AVPs and Dict of Packet Variables (packet_vars) orignHost = diameter.get_avp_data(avps, 264)[0] #Get OriginHost from AVP orignHost = binascii.unhexlify(orignHost).decode( 'utf-8') #Format it #Send Capabilities Exchange Answer (CEA) response to Capabilites Exchange Request (CER) if packet_vars['command_code'] == 257 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": print("Received Request with command code 257 (CER) from " + orignHost + "\n\tSending response (CEA)") response = diameter.Answer_257( packet_vars, avps, str(yaml_config['hss']['bind_ip'] [0])) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Send Credit Control Answer (CCA) response to Credit Control Request (CCR) elif packet_vars['command_code'] == 272 and packet_vars[ 'ApplicationId'] == 16777238: print("Received 3GPP Credit-Control-Request from " + orignHost + "\n\tGenerating (CCA)") response = diameter.Answer_16777238_272( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Send Device Watchdog Answer (DWA) response to Device Watchdog Requests (DWR) elif packet_vars['command_code'] == 280 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": #print("Received Request with command code 280 (DWR) from " + orignHost + "\n\tSending response (DWA)") response = diameter.Answer_280(packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Send Disconnect Peer Answer (DPA) response to Disconnect Peer Request (DPR) elif packet_vars['command_code'] == 282 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": print("Received Request with command code 282 (DPR) from " + orignHost + "\n\tForwarding request...") response = diameter.Answer_282(packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #S6a Authentication Information Answer (AIA) response to Authentication Information Request (AIR) elif packet_vars['command_code'] == 318 and packet_vars[ 'ApplicationId'] == 16777251 and packet_vars[ 'flags'] == "c0": print( "Received Request with command code 318 (3GPP Authentication-Information-Request) from " + orignHost + "\n\tGenerating (AIA)") response = diameter.Answer_16777251_318( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #S6a Update Location Answer (ULA) response to Update Location Request (ULR) elif packet_vars['command_code'] == 316 and packet_vars[ 'ApplicationId'] == 16777251: print( "Received Request with command code 316 (3GPP Update Location-Request) from " + orignHost + "\n\tGenerating (ULA)") response = diameter.Answer_16777251_316( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #S6a Purge UE Answer (PUA) response to Purge UE Request (PUR) elif packet_vars['command_code'] == 321 and packet_vars[ 'ApplicationId'] == 16777251: print( "Received Request with command code 321 (3GPP Purge UE Request) from " + orignHost + "\n\tGenerating (PUA)") response = diameter.Answer_16777251_321( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #S6a Purge UE Answer (NOA) response to Notify Request (NOR) elif packet_vars['command_code'] == 323 and packet_vars[ 'ApplicationId'] == 16777251: print( "Received Request with command code 323 (3GPP Notify Request) from " + orignHost + "\n\tGenerating (NOA)") response = diameter.Answer_16777251_323( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Cx Authentication Answer elif packet_vars['command_code'] == 300 and packet_vars[ 'ApplicationId'] == 16777216: print( "Received Request with command code 300 (3GPP Cx User Authentication Request) from " + orignHost + "\n\tGenerating (MAA)") response = diameter.Answer_16777216_300( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Cx Server Assignment Answer elif packet_vars['command_code'] == 301 and packet_vars[ 'ApplicationId'] == 16777216: print( "Received Request with command code 301 (3GPP Cx Server Assignemnt Request) from " + orignHost + "\n\tGenerating (MAA)") response = diameter.Answer_16777216_301( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Cx Location Information Answer elif packet_vars['command_code'] == 302 and packet_vars[ 'ApplicationId'] == 16777216: print( "Received Request with command code 302 (3GPP Cx Location Information Request) from " + orignHost + "\n\tGenerating (MAA)") response = diameter.Answer_16777216_302( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #Cx Multimedia Authentication Answer elif packet_vars['command_code'] == 303 and packet_vars[ 'ApplicationId'] == 16777216: print( "Received Request with command code 303 (3GPP Cx Multimedia Authentication Request) from " + orignHost + "\n\tGenerating (MAA)") response = diameter.Answer_16777216_303( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it #S13 ME-Identity-Check Answer elif packet_vars['command_code'] == 324 and packet_vars[ 'ApplicationId'] == 16777252: print( "Received Request with command code 324 (3GPP S13 ME-Identity-Check Request) from " + orignHost + "\n\tGenerating (MICA)") response = diameter.Answer_16777252_324( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) else: print("\n\nRecieved unrecognised request with Command Code: " + str(packet_vars['command_code']) + ", ApplicationID: " + str(packet_vars['ApplicationId']) + " and flags " + str(packet_vars['flags'])) for keys in packet_vars: print(keys) print("\t" + str(packet_vars[keys])) print(avps) print("Sending negative response") packet_vars, avps response = diameter.Respond_Command_Unsupported( packet_vars, avps) #Generate Diameter packet self.request.sendall(bytes.fromhex(response)) #Send it
def on_new_client(clientsocket, client_address): import diameter #Initialize Diameter diameter = diameter.Diameter(str(yaml_config['hss']['OriginHost']), str(yaml_config['hss']['OriginRealm']), str(yaml_config['hss']['ProductName']), str(yaml_config['hss']['MNC']), str(yaml_config['hss']['MCC'])) HSS_Logger.debug('New connection from ' + str(client_address)) logtool.Manage_Diameter_Peer(client_address, client_address, "add") data_sum = b'' while True: try: data = clientsocket.recv(32) if not data: HSS_Logger.info("Connection closed by " + str(client_address)) logtool.Manage_Diameter_Peer(client_address, client_address, "remove") break packet_length = diameter.decode_diameter_packet_length( data) #Calculate length of packet from start of packet data_sum = data + clientsocket.recv( packet_length - 32) #Recieve remainder of packet from buffer packet_vars, avps = diameter.decode_diameter_packet( data_sum ) #Decode packet into array of AVPs and Dict of Packet Variables (packet_vars) orignHost = diameter.get_avp_data(avps, 264)[0] #Get OriginHost from AVP orignHost = binascii.unhexlify(orignHost).decode( 'utf-8') #Format it #Send Capabilities Exchange Answer (CEA) response to Capabilites Exchange Request (CER) if packet_vars['command_code'] == 257 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": HSS_Logger.info( "Received Request with command code 257 (CER) from " + orignHost + "\n\tSending response (CEA)") try: response = diameter.Answer_257( packet_vars, avps, str(yaml_config['hss']['bind_ip'] [0])) #Generate Diameter packet except: response = diameter.Respond_ResultCode( packet_vars, avps, 5012 ) #Generate Diameter response with "DIAMETER_UNABLE_TO_COMPLY" (5012) HSS_Logger.info("Generated CEA") logtool.Manage_Diameter_Peer(orignHost, client_address, "update") #Send Credit Control Answer (CCA) response to Credit Control Request (CCR) elif packet_vars['command_code'] == 272 and packet_vars[ 'ApplicationId'] == 16777238: HSS_Logger.info("Received 3GPP Credit-Control-Request from " + orignHost + "\n\tGenerating (CCA)") try: response = diameter.Answer_16777238_272( packet_vars, avps) #Generate Diameter packet except: response = diameter.Respond_ResultCode( packet_vars, avps, 5012 ) #Generate Diameter response with "DIAMETER_UNABLE_TO_COMPLY" (5012) HSS_Logger.info("Generated CCA") #Send Device Watchdog Answer (DWA) response to Device Watchdog Requests (DWR) elif packet_vars['command_code'] == 280 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": HSS_Logger.info( "Received Request with command code 280 (DWR) from " + orignHost + "\n\tSending response (DWA)") try: response = diameter.Answer_280( packet_vars, avps) #Generate Diameter packet except: response = diameter.Respond_ResultCode( packet_vars, avps, 5012 ) #Generate Diameter response with "DIAMETER_UNABLE_TO_COMPLY" (5012) HSS_Logger.info("Generated DWA") logtool.Manage_Diameter_Peer(orignHost, client_address, "update") #Send Disconnect Peer Answer (DPA) response to Disconnect Peer Request (DPR) elif packet_vars['command_code'] == 282 and packet_vars[ 'ApplicationId'] == 0 and packet_vars['flags'] == "80": HSS_Logger.info( "Received Request with command code 282 (DPR) from " + orignHost + "\n\tForwarding request...") response = diameter.Answer_282(packet_vars, avps) #Generate Diameter packet HSS_Logger.info("Generated DPA") logtool.Manage_Diameter_Peer(orignHost, client_address, "remove") #S6a Authentication Information Answer (AIA) response to Authentication Information Request (AIR) elif packet_vars['command_code'] == 318 and packet_vars[ 'ApplicationId'] == 16777251 and packet_vars[ 'flags'] == "c0": HSS_Logger.info( "Received Request with command code 318 (3GPP Authentication-Information-Request) from " + orignHost + "\n\tGenerating (AIA)") try: response = diameter.Answer_16777251_318( packet_vars, avps) #Generate Diameter packet HSS_Logger.info("Generated AIR") except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for AIR") HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info( "Generated DIAMETER_USER_DATA_NOT_AVAILABLE AIR") #S6a Update Location Answer (ULA) response to Update Location Request (ULR) elif packet_vars['command_code'] == 316 and packet_vars[ 'ApplicationId'] == 16777251: HSS_Logger.info( "Received Request with command code 316 (3GPP Update Location-Request) from " + orignHost + "\n\tGenerating (ULA)") try: response = diameter.Answer_16777251_316( packet_vars, avps) #Generate Diameter packet HSS_Logger.info("Generated ULA") except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for ULR") HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info( "Generated error DIAMETER_USER_DATA_NOT_AVAILABLE ULA") #S6a Purge UE Answer (PUA) response to Purge UE Request (PUR) elif packet_vars['command_code'] == 321 and packet_vars[ 'ApplicationId'] == 16777251: HSS_Logger.info( "Received Request with command code 321 (3GPP Purge UE Request) from " + orignHost + "\n\tGenerating (PUA)") try: response = diameter.Answer_16777251_321( packet_vars, avps) #Generate Diameter packet except: response = diameter.Respond_ResultCode( packet_vars, avps, 5012 ) #Generate Diameter response with "DIAMETER_UNABLE_TO_COMPLY" (5012) HSS_Logger.error("Failed to generate PUA") HSS_Logger.info("Generated PUA") #S6a Purge UE Answer (NOA) response to Notify Request (NOR) elif packet_vars['command_code'] == 323 and packet_vars[ 'ApplicationId'] == 16777251: HSS_Logger.info( "Received Request with command code 323 (3GPP Notify Request) from " + orignHost + "\n\tGenerating (NOA)") try: response = diameter.Answer_16777251_323( packet_vars, avps) #Generate Diameter packet except: response = diameter.Respond_ResultCode( packet_vars, avps, 5012 ) #Generate Diameter response with "DIAMETER_UNABLE_TO_COMPLY" (5012) HSS_Logger.error("Failed to generate NOA") HSS_Logger.info("Generated NOA") #Cx Authentication Answer elif packet_vars['command_code'] == 300 and packet_vars[ 'ApplicationId'] == 16777216: HSS_Logger.info( "Received Request with command code 300 (3GPP Cx User Authentication Request) from " + orignHost + "\n\tGenerating (MAA)") try: response = diameter.Answer_16777216_300( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for Cx Auth Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info("Generated Cx Auth Answer") #Cx Server Assignment Answer elif packet_vars['command_code'] == 301 and packet_vars[ 'ApplicationId'] == 16777216: HSS_Logger.info( "Received Request with command code 301 (3GPP Cx Server Assignemnt Request) from " + orignHost + "\n\tGenerating (MAA)") try: response = diameter.Answer_16777216_301( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for Cx Server Assignment Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info("Generated Cx Server Assignment Answer") #Cx Location Information Answer elif packet_vars['command_code'] == 302 and packet_vars[ 'ApplicationId'] == 16777216: HSS_Logger.info( "Received Request with command code 302 (3GPP Cx Location Information Request) from " + orignHost + "\n\tGenerating (MAA)") try: response = diameter.Answer_16777216_302( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for Cx Location Information Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info("Generated Cx Location Information Answer") #Cx Multimedia Authentication Answer elif packet_vars['command_code'] == 303 and packet_vars[ 'ApplicationId'] == 16777216: HSS_Logger.info( "Received Request with command code 303 (3GPP Cx Multimedia Authentication Request) from " + orignHost + "\n\tGenerating (MAA)") try: response = diameter.Answer_16777216_303( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for Cx Multimedia Authentication Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info( "Generated Cx Multimedia Authentication Answer") #S13 ME-Identity-Check Answer elif packet_vars['command_code'] == 324 and packet_vars[ 'ApplicationId'] == 16777252: HSS_Logger.info( "Received Request with command code 324 (3GPP S13 ME-Identity-Check Request) from " + orignHost + "\n\tGenerating (MICA)") try: response = diameter.Answer_16777252_324( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for S13 ME-Identity Check Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info("Generated S13 ME-Identity Check Answer") #SLh LCS-Routing-Info-Answer elif packet_vars['command_code'] == 8388622 and packet_vars[ 'ApplicationId'] == 16777291: HSS_Logger.info( "Received Request with command code 324 (3GPP SLh LCS-Routing-Info-Answer Request) from " + orignHost + "\n\tGenerating (MICA)") try: response = diameter.Answer_16777291_8388622( packet_vars, avps) #Generate Diameter packet except Exception as e: HSS_Logger.info( "Failed to generate Diameter Response for SLh LCS-Routing-Info-Answer" ) HSS_Logger.info(e) traceback.print_exc() response = diameter.Respond_ResultCode( packet_vars, avps, 4100) #DIAMETER_USER_DATA_NOT_AVAILABLE HSS_Logger.info("Generated SLh LCS-Routing-Info-Answer") else: HSS_Logger.error( "\n\nRecieved unrecognised request with Command Code: " + str(packet_vars['command_code']) + ", ApplicationID: " + str(packet_vars['ApplicationId']) + " and flags " + str(packet_vars['flags'])) for keys in packet_vars: HSS_Logger.error(keys) HSS_Logger.error("\t" + str(packet_vars[keys])) HSS_Logger.error(avps) HSS_Logger.error("Sending negative response") response = diameter.Respond_ResultCode( packet_vars, avps, 3001 ) #Generate Diameter response with "Command Unsupported" (3001) clientsocket.sendall(bytes.fromhex(response)) #Send it #Handle actual sending try: clientsocket.sendall(bytes.fromhex(response)) #Send it except Exception as e: HSS_Logger.info("Failed to send Diameter Response") HSS_Logger.debug("Diameter Response Body: " + str(response)) HSS_Logger.info(e) traceback.print_exc() except KeyboardInterrupt: #Clean up the connection on keyboard interupt response = diameter.Request_282( ) #Generate Disconnect Peer Request Diameter packet clientsocket.sendall(bytes.fromhex(response)) #Send it clientsocket.close() HSS_Logger.info( "Connection closed niceley due to keyboard interrupt") sys.exit()
def SendRequest(request): clientsocket.sendall(bytes.fromhex(request)) ReadBuffer() while True: print("\n\nQuerying Diameter peer " + str(hostname) + " of domain " + str(realm)) print("Note - You may need to exchange a CER before doing anything fun") request = input("Enter request type:\t") if request == "R": ReadBuffer() elif request == "CER": print("Sending Cabailites Exchange Request to " + str(hostname)) message = diameter.Request_257() print("CER message:", message, type(message)) packet_vars, avps = diameter.decode_diameter_packet(message) print("packet_vars:", packet_vars) print("avps:") for item in avps: print("\t", item) print(get_now(), "send CER", message) SendRequest(diameter.Request_257()) elif request == "DWR": print("Sending Device Watchdog Request to " + str(hostname)) SendRequest(diameter.Request_280()) elif request == "ULR": imsi = str(input("IMSI:\t")) print("Sending Update Location Request to " + str(hostname)) message = diameter.Request_16777251_316(imsi)