def adfusim(arg_connection): """ Select ADF USIM field and return the FCP with status words @param arg_connection: The connection created by pyscard """ response, sw1, sw2 = select_file_in_mf(arg_connection, USIM_FILE_ID.DIR.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_record(1, data_length) if sw1 == 0x90: aid_identifier_content = search_fcp_content( response, TLV_TAG.APPLICATION_IDENTIFIER.value) if aid_identifier_content != None and len( aid_identifier_content) > 2: aid_identifier = toHexString(aid_identifier_content[2:], format=PACK) response, sw1, sw2 = arg_connection.select( aid_identifier, arg_p1_coding=CODING_P1_SELECT.SEL_BY_AID.value) return (response, sw1, sw2)
def __get_iccid(self, arg_connection): self.__logging.debug("__get_iccid") # select EF_ICCID response, sw1, sw2 = select_file_in_mf(arg_connection, USIM_FILE_ID.ICCID.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) return convert_bcd_to_string(response) return None
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "Can't read the content from EF_SPN!" raw_format = False update_spn = False set_content = "" dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "format" and value.lower() == "raw": raw_format = True elif key == "set": set_content = value update_spn = True # select EF_SPN response, sw1, sw2 = select_file_in_adf( arg_connection, USIM_FILE_ID.SPN.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) if update_spn: update_len = len(set_content) if update_len > data_length: update_len = data_length update_content = [0xFF] * data_length update_content[0] = 0x01 for i in range(update_len): update_content[i+1] = ord(set_content[i]) response, sw1, sw2 = arg_connection.update_binary( update_content) if sw1 == 0x90: ret_content = "SPN: Updated to '%s' (%d)" % ( convert_alpha_to_string(update_content[1:]), data_length-1) else: ret_content = "Can't update the new content to EF_SPN!" else: if raw_format: ret_content = "SPN: " + toHexString(response) else: ret_content = "SPN: %s (%d)" % ( convert_alpha_to_string(response[1:]), data_length-1) return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") if not os.path.exists(DEF_SECURITY_CACHE_FOLDER): os.makedirs(DEF_SECURITY_CACHE_FOLDER) ret_content = "Unexpected error!!" adm_code = None pin1_code = None dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "adm": adm_code = value elif key == "pin1": pin1_code = value if (adm_code == None or pin1_code == None or len(adm_code) != 16 or len(pin1_code) < 4 or len(pin1_code) > 8): ret_content = "Invalid PIN1/ADM code, operation terminated!!" else: response, sw1, sw2 = select_file_in_mf(arg_connection, USIM_FILE_ID.ICCID.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) iccid = convert_bcd_to_string(response) try: security_node = etree.Element("security") adm_node = etree.SubElement(security_node, "adm") adm_node.text = adm_code pin1_node = etree.SubElement(security_node, "pin1") pin1_node.text = pin1_code xmltree = etree.ElementTree(security_node) xmltree.write(DEF_SECURITY_CACHE_FOLDER + os.sep + iccid + ".xml", pretty_print=True, xml_declaration=True, encoding='utf-8') ret_content = "The " + iccid + ".xml file stored success." except: ret_content = "Can't store " + iccid + ".xml file, operation terminated!" return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "Can't read the content from EF_ICCID!" raw_format = False update_iccid = False set_content = "" dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "format" and value.lower() == "raw": raw_format = True elif key == "set": set_content = value update_iccid = True # select EF_ICCID response, sw1, sw2 = select_file_in_mf( arg_connection, USIM_FILE_ID.ICCID.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) if update_iccid: original = convert_bcd_to_string(response) update_content = set_content + original[len(set_content):] response, sw1, sw2 = arg_connection.update_binary( convert_string_to_bcd(update_content)) if sw1 == 0x90: ret_content = "ICCID: Updated to '%s'" % (update_content) else: ret_content = "Can't update the new content to EF_ICCID!" else: if raw_format: ret_content = "ICCID: " + toHexString(response) else: ret_content = "ICCID: " + convert_bcd_to_string(response) return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "" selected_arr = "mf" dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key.lower() == "type" and value.lower() == "adf": selected_arr = value.lower() # select EF_ARR if selected_arr == "adf": response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.ADF_ARR.value) else: response, sw1, sw2 = select_file_in_mf(arg_connection, USIM_FILE_ID.MF_ARR.value) if sw1 == 0x90: record_count = get_record_count(response) data_length = get_data_length(response) for i in range(record_count): response, sw1, sw2 = arg_connection.read_record( i + 1, data_length) if sw1 == 0x90: if ret_content != "": ret_content += "\n" ret_content += "%s ARR #%02d - %s" % ( selected_arr.upper(), i + 1, toHexString(response)) if ret_content == "": ret_content = "Can't read the content from EF_ARR!" return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "" raw_format = False dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "format" and value.lower() == "raw": raw_format = True # select EF_DIR response, sw1, sw2 = select_file_in_mf(arg_connection, USIM_FILE_ID.DIR.value) if sw1 == 0x90: record_count = get_record_count(response) data_length = get_data_length(response) for i in range(record_count): response, sw1, sw2 = arg_connection.read_record( i + 1, data_length) if sw1 == 0x90: if ret_content != "": ret_content += "\n" if raw_format: ret_content += "EF_DIR #%d - %s" % ( i + 1, toHexString(response)) else: aid_identifier = None aid_lable = None aid_identifier_content = search_fcp_content( response, TLV_TAG.APPLICATION_IDENTIFIER.value) if aid_identifier_content != None and len( aid_identifier_content) > 2: aid_identifier = toHexString( aid_identifier_content[2:], format=PACK) aid_label_content = search_fcp_content( response, TLV_TAG.APPLICATION_LABEL.value) if aid_label_content != None and len( aid_label_content) > 2: aid_lable = toASCIIString(aid_label_content[2:]) if aid_identifier == None: ret_content += "EF_DIR #%02d - [Empty Content]" % ( i + 1) elif aid_lable == None: ret_content += "EF_DIR #%02d - AID: %s" % ( i + 1, aid_identifier) else: ret_content += "EF_DIR #%02d - AID: %s, Label: %s" % ( i + 1, aid_identifier, aid_lable) if ret_content == "": ret_content = "Can't read the content from EF_DIR!" return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "Can't retrive the MCC/MNC value!" mnc_length = None efad_data_response = None set_mcc = "" set_mnc = "" update_mcc = False update_mnc = False dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "mcc": set_mcc = value update_mcc = True elif key == "mnc": set_mnc = value update_mnc = True # Check the length of MCC/MNC if update_mcc: if len(set_mcc) not in (0, 3): return "Invalid the length of MCC!" if update_mnc: if len(set_mnc) not in (0, 2, 3): return "Invalid the length of MNC!" # select EF_AD to get the length of mnc response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.AD.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) if sw1 == 0x90: efad_data_response = response[:] # keep for update mnc length mnc_length = response[3] # select EF_IMSI if mnc_length != None: response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.IMSI.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) if update_mcc or update_mnc: update_imsi = response[:] # MCC if update_mcc and (len(set_mcc) == 3): update_imsi[1] = (update_imsi[1] & 0x0F) + (int(set_mcc[0]) << 4) update_imsi[2] = (update_imsi[2] & 0xF0) + (int( set_mcc[1])) update_imsi[2] = (update_imsi[2] & 0x0F) + (int(set_mcc[2]) << 4) # MNC if update_mnc and (len(set_mnc) >= 2): update_imsi[3] = (update_imsi[3] & 0xF0) + (int( set_mnc[0])) update_imsi[3] = (update_imsi[3] & 0x0F) + (int(set_mnc[1]) << 4) if len(set_mnc) == 3: update_imsi[4] = (update_imsi[4] & 0xF0) + (int( set_mnc[2])) response, sw1, sw2 = arg_connection.update_binary( update_imsi) if sw1 == 0x90: if update_mnc: response, sw1, sw2 = select_file_in_adf( arg_connection, USIM_FILE_ID.AD.value) mnc_length = len(set_mnc) efad_data_response[3] = mnc_length response, sw1, sw2 = arg_connection.update_binary( efad_data_response) if sw1 == 0x90: ret_content = "Can't update the length of MNC!" mcc = convert_bcd_to_string(update_imsi[1:])[1:4] mnc = convert_bcd_to_string( update_imsi[1:])[4:4 + mnc_length] ret_content = "MCC/MNC: %s/%s" % (mcc, mnc) else: ret_content = "Can't update the new MCC/MNC to EF_IMSI!" else: if sw1 == 0x90: mcc = convert_bcd_to_string(response[1:])[1:4] mnc = convert_bcd_to_string(response[1:])[4:4 + mnc_length] ret_content = "MCC/MNC: %s/%s" % (mcc, mnc) return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") set_content1 = "" set_content2 = "" update_gid1 = False update_gid2 = False ret_content = "Can't read the content from EF_GID1/EF_GID2!" dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "gid1": set_content1 = toBytes(value) update_gid1 = True elif key == "gid2": set_content2 = toBytes(value) update_gid2 = True # select EF_GID1 response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.GID1.value) if sw1 == 0x90: data_length = get_data_length(response) gid1_response, gid1_sw1, sw2 = arg_connection.read_binary( data_length) if update_gid1: update_len = len(set_content1) if update_len > data_length: update_len = data_length for i in range(update_len): gid1_response[i] = set_content1[i] response, sw1, sw2 = arg_connection.update_binary( gid1_response) if gid1_sw1 == 0x90: ret_content = "GID1: %s (%d)\n" % (toHexString(gid1_response), len(gid1_response)) else: ret_content = "GID1: Can't read/update the value from EF_GID1\n" else: ret_content = "GID1: Not Exist\n" # select EF_GID2 response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.GID2.value) if sw1 == 0x90: data_length = get_data_length(response) gid2_response, gid2_sw1, sw2 = arg_connection.read_binary( data_length) if update_gid2: update_len = len(set_content2) if update_len > data_length: update_len = data_length for i in range(update_len): gid2_response[i] = set_content2[i] response, sw1, sw2 = arg_connection.update_binary( gid2_response) if gid2_sw1 == 0x90: ret_content += "GID2: %s (%d)" % (toHexString(gid2_response), len(gid2_response)) else: ret_content += "GID2: Can't read/update the value from EF_GID2" else: ret_content += "GID2: Not Exist\n" return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "" raw_format = False set_record_id = 0 set_name_content = "" set_num_content = "" update_msisdn = False dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "format" and value.lower() == "raw": raw_format = True elif key == "id": set_record_id = int(value) elif key == "name": set_name_content = value update_msisdn = True elif key == "num": set_num_content = value update_msisdn = True # select EF_MSISDN response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.MSISDN.value) if sw1 == 0x90: record_count = get_record_count(response) data_length = get_data_length(response) if update_msisdn: if set_record_id == 0 or set_record_id > record_count: ret_content = "Invalid record index!!" else: # Read Record response, sw1, sw2 = arg_connection.read_record( set_record_id, data_length) if sw1 == 0x90: update_msisdn_apdu = response[:] alpha_len = data_length - 14 if len(set_name_content) > 0: # Name update_alpha_len = len(set_name_content) for i in range(alpha_len): if i < update_alpha_len: update_msisdn_apdu[i] = ord( set_name_content[i]) else: update_msisdn_apdu[i] = 0xFF if len(set_num_content) > 0: # Num Length if len(set_num_content) % 2 == 1: update_msisdn_apdu[alpha_len] = int( len(set_num_content) / 2) + 1 else: update_msisdn_apdu[alpha_len] = int( len(set_num_content) / 2) # Add "TON_NPI" update_msisdn_apdu[ alpha_len] = update_msisdn_apdu[alpha_len] + 1 # Num: BCD 10 (20 digits) + TON NPI if update_msisdn_apdu[alpha_len] > 11: update_msisdn_apdu[alpha_len] = 11 # TON NPI if len(set_num_content ) > 0 and set_num_content[0] == "+": update_msisdn_apdu[alpha_len + 1] = 0x91 set_num_content = set_num_content[1:] else: update_msisdn_apdu[alpha_len + 1] = 0x81 # Reset Number to 0xFF for i in range(10): update_msisdn_apdu[alpha_len + 1 + 1 + i] = 0xFF update_num_len = len(set_num_content) if update_num_len > 20: update_num_len = 10 num_apdu_index = alpha_len + 1 + 1 for i in range(update_num_len): tmp = set_num_content[i * 2:i * 2 + 2] if len(tmp) == 2: update_msisdn_apdu[num_apdu_index + i] = (int(tmp[1]) * 16) + int(tmp[0]) elif len(tmp) == 1: update_msisdn_apdu[num_apdu_index + i] = ( update_msisdn_apdu[num_apdu_index + i] & 0xF0) + int(tmp[0]) response, sw1, sw2 = arg_connection.update_record( set_record_id, update_msisdn_apdu) if sw1 == 0x90: ret_content = "MSISDN: Updated success" else: for i in range(record_count): response, sw1, sw2 = arg_connection.read_record( i + 1, data_length) if sw1 == 0x90: if ret_content != "": ret_content += "\n" if raw_format: ret_content += "EF_MSISDN #%02d - %s" % ( i + 1, toHexString(response)) else: alpha_str = convert_alpha_to_string( response[:len(response) - 14]) number_str = convert_dialing_number_to_string( response[len(response) - 14 + 1:len(response) - 14 + 1 + 11]) if alpha_str == "": alpha_str = "[Empty Content]" if number_str == "": number_str = "[Empty Content]" ret_content += "EF_MSISDN #%02d - Name: %s (%d), Number: %s" % ( i + 1, alpha_str, len(response) - 14, number_str) if ret_content == "": ret_content = "Can't read the content from EF_MSISDN!" return ret_content
def execute(self, arg_connection, arg_parameter=""): self.__logging.debug("execute()") ret_content = "Can't read the content from EF_IMSI!" raw_format = False set_content = "" update_imsi = False dict_args = convert_arguments_to_dict(arg_parameter) for key, value in dict_args.items(): if key == "format" and value.lower() == "raw": raw_format = True elif key == "set": set_content = value update_imsi = True # select EF_IMSI response, sw1, sw2 = select_file_in_adf(arg_connection, USIM_FILE_ID.IMSI.value) if sw1 == 0x90: data_length = get_data_length(response) response, sw1, sw2 = arg_connection.read_binary(data_length) if sw1 == 0x90: if update_imsi: imsi_update_content = response[:] for i in range(len(set_content)): if i == 15: break Idx_of_PayLoad = int(((i + 1) / 2) + 1) Mod_Value = (i % 2) if Mod_Value == 0: imsi_update_content[Idx_of_PayLoad] = ( imsi_update_content[Idx_of_PayLoad] & 0x0F) + (int(set_content[i]) << 4) else: imsi_update_content[Idx_of_PayLoad] = ( imsi_update_content[Idx_of_PayLoad] & 0xF0) + int(set_content[i]) response, sw1, sw2 = arg_connection.update_binary( imsi_update_content) if sw1 == 0x90: ret_content = ("IMSI: Updated to '" + convert_bcd_to_string( imsi_update_content[1:])[1:] + ("'")) else: ret_content = "Can't update the new content to EF_IMSI!" else: if raw_format: ret_content = "IMSI: " + toHexString(response) else: ret_content = ("IMSI: " + convert_bcd_to_string(response[1:])[1:]) return ret_content