def setEfPnn(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue tmpData = [] infoFull = types.getParamValue(record, "info_full") fullName = types.getParamValue(record, "full_name") infoShort = types.getParamValue(record, "info_short") shortName = types.getParamValue(record, "short_name") additionalInfo = types.getParamValue(record, "additional_info") if fullName: fullNameGsm7 = coder.encodeGsm7(fullName) if infoFull: infoByte = int(infoFull, 16) spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS") ext = infoByte >> 7 & 0b00000001 else: spareBits = len(fullName) % 8 ci = 0 #don't add the letters for the Country's Initials dcs = 0 #GSM ext = 1 #? infoByte = ext << 7 | dcs << 3 | ci << 2 | spareBits fullNameGsm7 = "%02X%s" % (infoByte, fullNameGsm7) types.addTlv(tmpData, types.FULL_NW_NAME_TAG, hextools.hex2bytes(fullNameGsm7)) if shortName: shortNameGsm7 = coder.encodeGsm7(shortName) if infoShort: infoByte = int(infoShort, 16) spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS") ext = infoByte >> 7 & 0b00000001 else: spareBits = len(shortName) % 8 ci = 0 #don't add the letters for the Country's Initials dcs = 0 #GSM ext = 1 #? infoByte = ext << 7 | dcs << 3 | ci << 2 | spareBits shortNameGsm7 = "%02X%s" % (infoByte, shortNameGsm7) types.addTlv(tmpData, types.SHORT_NW_NAME_TAG, hextools.hex2bytes(shortNameGsm7)) if additionalInfo: types.addTlv(tmpData, types.ADDITIONAL_INFORMATION_PLMN_TAG, hextools.hex2bytes(additionalInfo)) dataNew += "%s;" % hextools.bytes2hex(tmpData) return dataNew
def setEfPnn(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue tmpData = [] infoFull = types.getParamValue(record, "info_full") fullName = types.getParamValue(record, "full_name") infoShort = types.getParamValue(record, "info_short") shortName = types.getParamValue(record, "short_name") additionalInfo = types.getParamValue(record, "additional_info") if fullName: fullNameGsm7 = coder.encodeGsm7(fullName) if infoFull: infoByte = int(infoFull, 16) spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS") ext = infoByte >> 7 & 0b00000001 else: spareBits = len(fullName) % 8 ci = 0 #don't add the letters for the Country's Initials dcs = 0 #GSM ext = 1 #? infoByte = ext << 7 | dcs << 3 | ci << 2 | spareBits fullNameGsm7 = "%02X%s" %(infoByte, fullNameGsm7) types.addTlv(tmpData, types.FULL_NW_NAME_TAG, hextools.hex2bytes(fullNameGsm7)) if shortName: shortNameGsm7 = coder.encodeGsm7(shortName) if infoShort: infoByte = int(infoShort, 16) spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS") ext = infoByte >> 7 & 0b00000001 else: spareBits = len(shortName) % 8 ci = 0 #don't add the letters for the Country's Initials dcs = 0 #GSM ext = 1 #? infoByte = ext << 7 | dcs << 3 | ci << 2 | spareBits shortNameGsm7 = "%02X%s" %(infoByte, shortNameGsm7) types.addTlv(tmpData, types.SHORT_NW_NAME_TAG, hextools.hex2bytes(shortNameGsm7)) if additionalInfo: types.addTlv(tmpData, types.ADDITIONAL_INFORMATION_PLMN_TAG, hextools.hex2bytes(additionalInfo)) dataNew += "%s;" %hextools.bytes2hex(tmpData) return dataNew
def getEfSpn(self, data): spnRaw = types.removeTrailingBytes(hextools.hex2bytes(data), 0xFF) if spnRaw: displayByte = spnRaw[0] name = hextools.bytes2hex(spnRaw[1:]).decode("hex") else: return '' return "name=%s,display=%02X" % (name, displayByte)
def getEfUst(self, data): data = hextools.hex2bytes(data) ustTable = [] for byte in data: for bitId in range(8): value = int(byte & (1 << bitId) != 0) ustTable.append(value) return ustTable
def getEfSpn(self, data): spnRaw = types.removeTrailingBytes(hextools.hex2bytes(data), 0xFF) if spnRaw: displayByte = spnRaw[0] name = hextools.bytes2hex(spnRaw[1:]).decode("hex") else: return '' return "name=%s,display=%02X" %(name, displayByte)
def getEfPcscf(self, data): #TODO: handle many records firstRecord = data.split(';')[0] value = types.removeTrailingBytes(hextools.hex2bytes(firstRecord), 0xFF) if len(value) < 3: return '' cscf = hextools.bytes2hex(value[3:]).decode("hex") return "cscf=%s" %cscf
def getEfOpl(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue binRecord = hextools.hex2bytes(record) if binRecord.count(0xFF) == len(binRecord): continue lai = hextools.bytes2hex(binRecord[0:7]) mccMnc = hextools.decode_BCD(hextools.hex2bytes(lai)[0:3]) lacStart = lai[6:10] lacEnd = lai[10:14] lacRange = "%s-%s" %(lacStart, lacEnd) pnnId = binRecord[7] dataNew += "mcc_mnc=%s,lac=%s,pnnId=%d;" %(mccMnc, lacRange, pnnId) return dataNew
def injectApdu(self, apdu, card, mode=INJECT_NO_FORWARD): # TODO: add inject tag to logs self.waitInjectReady() with self.lock: self.apduInjectedCard = card self.apduInjectedData = hextools.hex2bytes(apdu) self.inject = mode return self.waitRapduInject()
def addFileList(self, list, comprehensionRequired=True): """Full paths should be given to files, e.g. IMSI: "3F007F206F07" """ params = [] # The number of files that will be described in the following list params.append(len(list)) # Files - full paths are given to files for file in list: params.extend(hextools.hex2bytes(file)) self.addTagToMessage(comprehension_tag.FILE_LIST, comprehensionRequired, params)
def getEfPcscf(self, data): #TODO: handle many records firstRecord = data.split(';')[0] value = types.removeTrailingBytes(hextools.hex2bytes(firstRecord), 0xFF) if len(value) < 3: return '' cscf = hextools.bytes2hex(value[3:]).decode("hex") return "cscf=%s" % cscf
def getEfOpl(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue binRecord = hextools.hex2bytes(record) if binRecord.count(0xFF) == len(binRecord): continue lai = hextools.bytes2hex(binRecord[0:7]) mccMnc = hextools.decode_BCD(hextools.hex2bytes(lai)[0:3]) lacStart = lai[6:10] lacEnd = lai[10:14] lacRange = "%s-%s" % (lacStart, lacEnd) pnnId = binRecord[7] dataNew += "mcc_mnc=%s,lac=%s,pnnId=%d;" % (mccMnc, lacRange, pnnId) return dataNew
def getEfLoci(self, data): valueRaw = hextools.hex2bytes(data) tmsi = hextools.bytes2hex(valueRaw[0:4]) lai = hextools.bytes2hex(valueRaw[4:9]) #TODO: check for mnc containing 3digits mcc_mnc = hextools.decode_BCD(hextools.hex2bytes(lai)[0:3]) lac = lai[6:10] rfu = hextools.bytes2hex([valueRaw[9]]) loction_status = hextools.bytes2hex([valueRaw[10]]) ''' loction_status Bits: b3 b2 b1 0 0 0 : updated. 0 0 1 : not updated. 0 1 0 : PLMN not allowed. 0 1 1 : Location Area not allowed. 1 1 1 : reserved ''' return "tmsi=%s,mcc_mnc=%s,lac=%s,loc_status=%s"\ %(tmsi, mcc_mnc, lac, loction_status)
def getEfImpu(self, data): impuStr = '' data = data.split(';') for record in data: value = types.removeTrailingBytes(hextools.hex2bytes(record), 0xFF) if len(value) < 3: continue if impuStr: impuStr += ";" impuStr += hextools.bytes2hex(value[2:]).decode("hex") return impuStr
def getEfEcc(self, data): eccStr = '' data = data.split(';') for record in data: recordRaw = hextools.hex2bytes(record) number = hextools.decode_BCD(recordRaw[0:3]) number = number.replace('F','') if not number: continue #number = type.removeTrailingBytes(hextools.hex2bytes(number), 0xFF) aplha = None category = recordRaw[-1] eccStr += "number=%s,alpha=%s,cat=%d;" %(number, aplha, category) eccStr = eccStr.rstrip(";") if not eccStr: eccStr = "EMPTY" return eccStr
def getEfEcc(self, data): eccStr = '' data = data.split(';') for record in data: recordRaw = hextools.hex2bytes(record) number = hextools.decode_BCD(recordRaw[0:3]) number = number.replace('F', '') if not number: continue #number = type.removeTrailingBytes(hextools.hex2bytes(number), 0xFF) aplha = None category = recordRaw[-1] eccStr += "number=%s,alpha=%s,cat=%d;" % (number, aplha, category) eccStr = eccStr.rstrip(";") if not eccStr: eccStr = "EMPTY" return eccStr
def getEfPnn(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue binRecord = hextools.hex2bytes(record) if binRecord.count(0xFF) == len(binRecord): continue fullNameRaw = types.parseTlv(binRecord, types.FULL_NW_NAME_TAG) shortNameRaw = types.parseTlv(binRecord, types.SHORT_NW_NAME_TAG) additionalInfo = types.parseTlv( binRecord, types.ADDITIONAL_INFORMATION_PLMN_TAG) if fullNameRaw: infoByte = fullNameRaw[0] spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception( "Only GSM coding is supported in DCS, current coding: %02X" % dcs) fullName = coder.decodeGsm7(hextools.bytes2hex( fullNameRaw[1:])) else: fullName = None if shortNameRaw: infoByte = shortNameRaw[0] spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception( "Only GSM coding is supported in DCS, current coding: %02X" % dcs) shortName = coder.decodeGsm7( hextools.bytes2hex(shortNameRaw[1:])) else: shortName = None dataNew += "full_name=%s,short_name=%s"\ %(fullName, shortName) if additionalInfo: dataNew += ",additional_info=%s" % additionalInfo dataNew += ";" return dataNew
def run(self): self.__lock.acquire() self.poll = True while (self.poll): self.startTime = time.time() if self.startTime - self.lastUpdate > (self.pollRate / 1000.0 - 0.1): try: sw1 = self.pyscardRpc.c_transmit(hextools.hex2bytes(POLL_STATUS_PATTERN[self.pattern]), self.index)[1] if sw1 != 0x90 and self.pattern < (len(POLL_STATUS_PATTERN) - 1): # Use different pattern e.g. 2G status. self.pattern += 1 except: logging.error("Stop polling") self.stop() self.lastUpdate = time.time() time.sleep(self.pollRate / 1000.0) self.__lock.release()
def run(self): self.__lock.acquire() self.poll = True while (self.poll): self.startTime = time.time() if self.startTime - self.lastUpdate > (self.pollRate / 1000.0 - 0.1): try: sw1 = self.pyscardRpc.c_transmit( hextools.hex2bytes(POLL_STATUS_PATTERN[self.pattern]), self.index)[1] if sw1 != 0x90 and self.pattern < ( len(POLL_STATUS_PATTERN) - 1): # Use different pattern e.g. 2G status. self.pattern += 1 except: logging.error("Stop polling") self.stop() self.lastUpdate = time.time() time.sleep(self.pollRate / 1000.0) self.__lock.release()
def getEfPnn(self, data): data = data.split(';') dataNew = '' for record in data: if not record: continue binRecord = hextools.hex2bytes(record) if binRecord.count(0xFF) == len(binRecord): continue fullNameRaw = types.parseTlv(binRecord, types.FULL_NW_NAME_TAG) shortNameRaw = types.parseTlv(binRecord, types.SHORT_NW_NAME_TAG) additionalInfo = types.parseTlv(binRecord, types.ADDITIONAL_INFORMATION_PLMN_TAG) if fullNameRaw: infoByte = fullNameRaw[0] spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS, current coding: %02X" %dcs) fullName = coder.decodeGsm7(hextools.bytes2hex(fullNameRaw[1:])) else: fullName = None if shortNameRaw: infoByte = shortNameRaw[0] spareBits = infoByte & 0b00000111 ci = infoByte >> 3 & 0b00000001 dcs = infoByte >> 4 & 0b00000111 if dcs != 0x00: raise Exception("Only GSM coding is supported in DCS, current coding: %02X" %dcs) shortName = coder.decodeGsm7(hextools.bytes2hex(shortNameRaw[1:])) else: shortName = None dataNew += "full_name=%s,short_name=%s"\ %(fullName, shortName) if additionalInfo: dataNew += ",additional_info=%s" %additionalInfo dataNew += ";" return dataNew
def addItem(self, id, text): params = [id] + hextools.hex2bytes(text.encode("hex")) self.addTagToMessage(comprehension_tag.ITEM, True, params)
def addAlphaIdentifier(self, text, comprehensionRequired=True): self.addTagToMessage(comprehension_tag.ALPHA_IDENTIFIER, comprehensionRequired, hextools.hex2bytes(text.encode("hex")))
def addUSSDString(self, coding, string): """coding: use data_coding object""" params = [coding] + hextools.hex2bytes(string) self.addTagToMessage(comprehension_tag.USSD_STRING, True, params)
def handleApdu(self, cardData, apdu): card = cardData[0] sendData = cardData[1] if card == None: raise Exception("card not initialized") ins = types.insName(apdu) if card != self.getMainCard(0): origApdu = apdu if (self.aidCommon(card) and card.routingAttr.aidToSelect and self.getMainCard(0).routingAttr.aidToSelect == hextools.bytes2hex(apdu) and #origin apdu is AID int(card.routingAttr.aidToSelect[0:2], 16) == apdu[0]): #check the same class apdu = hextools.hex2bytes(card.routingAttr.aidToSelect) card.routingAttr.aidToSelect = None elif (self.aidCommon(card) and card.routingAttr.getFileSelected(apdu[0]) == 'EF_DIR' and ins == 'READ_RECORD' and card.routingAttr.recordEfDirLength): apdu[4] = card.routingAttr.recordEfDirLength if origApdu != apdu: self.loggingApdu.info("") self.loggingApdu.info( "*C-APDU%d: %s" % (self.getSimId(card), hextools.bytes2hex(apdu))) if self.simType == types.TYPE_SIM and (apdu[0] & 0xF0) != 0xA0: #force 2G on USIM cards sw = types_g.sw.CLASS_NOT_SUPPORTED sw1 = sw >> 8 sw2 = sw & 0x00FF responseApdu = [sw1, sw2] elif ins == 'GET_RESPONSE' and card.routingAttr.getResponse: responseApdu = card.routingAttr.getResponse card.routingAttr.getResponse = None else: responseApdu = card.apdu(apdu) if card != self.getMainCard(0): if (self.aidCommon(card) and card.routingAttr.getFileSelected(apdu[0]) == 'EF_DIR' and ins == 'GET_RESPONSE' and types.swNoError(responseApdu) and len(responseApdu) > 7): card.routingAttr.recordEfDirLength = responseApdu[7] if (TRY_ANOTHER_CARD_ON_AUTH_FAILURE and self.getNbrOfCards() > 1 and card.routingAttr.getFileSelected(apdu[0]) == 'AUTH' and types.sw(responseApdu) == types_g.sw.AUTHENTICATION_ERROR_APPLICATION_SPECIFIC): sw1Name, swName = types.swName( types.sw(responseApdu) >> 8, types.sw(responseApdu) & 0x00FF) self.logging.warning("Response not expected. SW1: %s, SW: %s" % (sw1Name, swName)) self.logging.warning("Change card to process AUTHENTICATION") if card == self.getMainCard(0): cardTmp = self.getMainCard(1) else: cardTmp = self.getMainCard(0) responseApdu = cardTmp.apdu(apdu) cardTmp.routingAttr.setFileSelected('AUTH', apdu[0]) card.routingAttr.setFileSelected(None, apdu[0]) # TODO: check if exist cardTmp.routingAttr.insReplaced.append('INTERNAL_AUTHENTICATE') if types.sw1(responseApdu) in [ types_g.sw1.RESPONSE_DATA_AVAILABLE_2G, types_g.sw1.RESPONSE_DATA_AVAILABLE_3G ]: # cache 'GET_RESPONSE' getResponseLength = types.sw2(responseApdu) cla = apdu[0] apduTmp = "%02XC00000%02X" % (cla, getResponseLength) self.loggingApdu.info("**C-APDU%d: %s" % (self.getSimId(cardTmp), apduTmp)) cardTmp.routingAttr.getResponse = cardTmp.apdu(apduTmp) if card.routingAttr.getFileSelected( apdu[0]) == 'EF_IMSI' and types.swNoError(responseApdu): #cache imsi responseData = types.responseData(responseApdu) if ins == 'READ_BINARY' and types.p1(apdu) == 0 and types.p2( apdu) == 0: #When P1=8X then SFI is used to select the file. #Remove the check when SFI checking is implemented imsi = hextools.decode_BCD(responseData)[3:] #TODO: remove length check when name for the file comes from #the whole path and not fid. 6f07 is also in ADF_ISIM if len(imsi) > 10: card.imsi = imsi #update associated interface if self.isCardCtrl(card): self.getRelatedMainCard(card).imsi = imsi else: self.getRelatedCtrlCard(card).imsi = imsi elif ins == 'UPDATE_BINARY': card.imsi = None responseApduHex = hextools.bytes2hex(responseApdu) #example of APDU modification if responseApduHex == "02542D4D6F62696C652E706CFFFFFFFFFF9000": #change SPN name 'T-mobile.pl' for 'Tmobile-SPN' responseApdu = hextools.hex2bytes( "02546D6F62696C652D53504EFFFFFFFFFF9000") if sendData: if ((types.sw(responseApdu) == types_g.sw.NO_ERROR or types.sw1(responseApdu) == types_g.sw1.NO_ERROR_PROACTIVE_DATA) and self.getNbrOfCards() > 1): # Check for pending SAT command for cardDict in self.cardsDict: cardTmp = cardDict[MAIN_INTERFACE] if card == cardTmp: continue if set(sim_card.SAT_INS) <= set( cardTmp.routingAttr.insReplaced): swNoError = cardTmp.swNoError if types.unpackSw(swNoError)[ 0] == types_g.sw1.NO_ERROR_PROACTIVE_DATA: #update r-apdu with proactive data information responseApdu[-2] = swNoError >> 8 responseApdu[-1] = swNoError & 0x00FF break self.sendResponseApdu(responseApdu) if card == self.getMainCard(0) or sendData: self.pretty_apdu(apdu) responseApduHex = hextools.bytes2hex(responseApdu) self.loggingApdu.info("R-APDU%d: %s" % (self.getSimId(card), responseApduHex)) # gsmtap.log(apdu,responseApdu) # Uncomment for wireshark return responseApdu
def getBinaryFromText(self, node): return hextools.hex2bytes(node.text.replace(" ", ""))
def getEfImsi(self, data): data = hextools.hex2bytes(data) imsi = hextools.decode_BCD(data)[3:] return imsi
def getEfImpi(self, data): valueRaw = types.removeTrailingBytes(hextools.hex2bytes(data), 0xFF) if len(valueRaw) < 3: return '' value = hextools.bytes2hex(valueRaw[2:]).decode("hex") return "impi=%s" %value
def getEfImpi(self, data): valueRaw = types.removeTrailingBytes(hextools.hex2bytes(data), 0xFF) if len(valueRaw) < 3: return '' value = hextools.bytes2hex(valueRaw[2:]).decode("hex") return "impi=%s" % value
def getBinaryValue(self, file): hexStr = self.getValue(file) return hextools.hex2bytes(hexStr.replace(" ", "").replace("\n", ""))
def getEfCphs_onstr(self, data): nameHex = types.removeTrailingBytes(hextools.hex2bytes(data), 0xFF) #fullName = coder.decodeGsm7(hextools.bytes2hex(nameHex)) #return fullName return hextools.bytes2hex(nameHex.decode("hex"))
def addText(self, coding, text): """coding - use data_coding object""" params = [coding] + hextools.hex2bytes(text.encode("hex")) self.addTagToMessage(comprehension_tag.TEXT_STRING, True, params)
def handleApdu(self, cardData, apdu): card = cardData[0] sendData = cardData[1] if card == None: raise Exception("card not initialized") ins = types.insName(apdu) if card != self.getMainCard(0): origApdu = apdu if ( self.aidCommon(card) and card.routingAttr.aidToSelect and self.getMainCard(0).routingAttr.aidToSelect == hextools.bytes2hex(apdu) and #origin apdu is AID int(card.routingAttr.aidToSelect[0:2], 16) == apdu[0]): #check the same class apdu = hextools.hex2bytes(card.routingAttr.aidToSelect) card.routingAttr.aidToSelect = None elif ( self.aidCommon(card) and card.routingAttr.getFileSelected(apdu[0]) == 'EF_DIR' and ins == 'READ_RECORD' and card.routingAttr.recordEfDirLength): apdu[4] = card.routingAttr.recordEfDirLength if origApdu != apdu: self.loggingApdu.info("") self.loggingApdu.info("*C-APDU%d: %s" %(self.getSimId(card), hextools.bytes2hex(apdu))) if self.simType == types.TYPE_SIM and (apdu[0] & 0xF0) != 0xA0: #force 2G on USIM cards sw = types_g.sw.CLASS_NOT_SUPPORTED sw1 = sw>>8 sw2 = sw & 0x00FF responseApdu = [sw1, sw2] elif ins == 'GET_RESPONSE' and card.routingAttr.getResponse: responseApdu = card.routingAttr.getResponse card.routingAttr.getResponse = None else: responseApdu = card.apdu(apdu) if card != self.getMainCard(0): if (self.aidCommon(card) and card.routingAttr.getFileSelected(apdu[0]) == 'EF_DIR' and ins == 'GET_RESPONSE' and types.swNoError(responseApdu) and len(responseApdu) > 7): card.routingAttr.recordEfDirLength = responseApdu[7] if (TRY_ANOTHER_CARD_ON_AUTH_FAILURE and self.getNbrOfCards() > 1 and card.routingAttr.getFileSelected(apdu[0]) == 'AUTH' and types.sw(responseApdu) == types_g.sw.AUTHENTICATION_ERROR_APPLICATION_SPECIFIC): sw1Name, swName = types.swName(types.sw(responseApdu) >> 8, types.sw(responseApdu) & 0x00FF) self.logging.warning("Response not expected. SW1: %s, SW: %s" %(sw1Name, swName)) self.logging.warning("Change card to process AUTHENTICATION") if card == self.getMainCard(0): cardTmp = self.getMainCard(1) else: cardTmp = self.getMainCard(0) responseApdu = cardTmp.apdu(apdu) cardTmp.routingAttr.setFileSelected('AUTH', apdu[0]) card.routingAttr.setFileSelected(None, apdu[0]) # TODO: check if exist cardTmp.routingAttr.insReplaced.append('INTERNAL_AUTHENTICATE') if types.sw1(responseApdu) in [types_g.sw1.RESPONSE_DATA_AVAILABLE_2G, types_g.sw1.RESPONSE_DATA_AVAILABLE_3G]: # cache 'GET_RESPONSE' getResponseLength = types.sw2(responseApdu) cla = apdu[0] apduTmp = "%02XC00000%02X" %(cla, getResponseLength) self.loggingApdu.info("**C-APDU%d: %s" %(self.getSimId(cardTmp), apduTmp)) cardTmp.routingAttr.getResponse = cardTmp.apdu(apduTmp) if card.routingAttr.getFileSelected(apdu[0]) == 'EF_IMSI' and types.swNoError(responseApdu): #cache imsi responseData = types.responseData(responseApdu) if ins == 'READ_BINARY' and types.p1(apdu) == 0 and types.p2(apdu) == 0: #When P1=8X then SFI is used to select the file. #Remove the check when SFI checking is implemented imsi = hextools.decode_BCD(responseData)[3:] #TODO: remove length check when name for the file comes from #the whole path and not fid. 6f07 is also in ADF_ISIM if len(imsi) > 10: card.imsi = imsi #update associated interface if self.isCardCtrl(card): self.getRelatedMainCard(card).imsi = imsi else: self.getRelatedCtrlCard(card).imsi = imsi elif ins == 'UPDATE_BINARY': card.imsi = None responseApduHex = hextools.bytes2hex(responseApdu) #example of APDU modification if responseApduHex == "02542D4D6F62696C652E706CFFFFFFFFFF9000": #change SPN name 'T-mobile.pl' for 'Tmobile-SPN' responseApdu = hextools.hex2bytes("02546D6F62696C652D53504EFFFFFFFFFF9000") if sendData: if ((types.sw(responseApdu) == types_g.sw.NO_ERROR or types.sw1(responseApdu) == types_g.sw1.NO_ERROR_PROACTIVE_DATA) and self.getNbrOfCards() > 1): # Check for pending SAT command for cardDict in self.cardsDict: cardTmp = cardDict[MAIN_INTERFACE] if card == cardTmp: continue if set(sim_card.SAT_INS) <= set(cardTmp.routingAttr.insReplaced): swNoError = cardTmp.swNoError if types.unpackSw(swNoError)[0] == types_g.sw1.NO_ERROR_PROACTIVE_DATA: #update r-apdu with proactive data information responseApdu[-2] = swNoError >> 8 responseApdu[-1] = swNoError & 0x00FF break self.sendResponseApdu(responseApdu) if card == self.getMainCard(0) or sendData: self.pretty_apdu(apdu) responseApduHex = hextools.bytes2hex(responseApdu) self.loggingApdu.info("R-APDU%d: %s" %(self.getSimId(card), responseApduHex)) # gsmtap.log(apdu,responseApdu) # Uncomment for wireshark return responseApdu