In the previous article "Reading a SIM card phone book in Python" I presented a program to read the phone book from a SIM card. I will now present the writing part.
Source code
The source code is in 2 parts:
#!/usr/bin/env python3 from smartcard.util import toBytes, padd import random import usim def get_name(): # List of firstnames from https://www.data.gouv.fr/fr/datasets/liste-de-prenoms/ with open(random.choice(['f', 'm'])) as fd: lines = list(map(str.strip, fd.readlines())) return " ".join([random.choice(lines), random.choice(lines), random.choice(lines)]) def get_number(): numbers = "0123456789" phone = list() phone.append(random.choice(numbers) + random.choice(numbers)) phone.append(random.choice(numbers) + random.choice(numbers)) phone.append(random.choice(numbers) + random.choice(numbers)) phone.append(random.choice(numbers) + random.choice(numbers)) phone.append(random.choice(numbers) + random.choice(numbers)) return " ".join(phone) def new_record(size): # size-14 characters for the name name = get_name()[0:size-14] phone = get_number() print("name:", name) print("phone:", phone) record = padd(list(map(ord, name)), size-14) \ + padd(toBytes("06 A1 " + phone), 14) return record def usim_write(reader_nb): # Select the EF ADN (size, connection) = usim.usim(reader_nb) for nbr in range(1, 250): record = new_record(size) # Update record header = [0xA0, 0xDC] record_idx = nbr cmd = header + [record_idx, 0x04, size] + record data, sw1, sw2 = connection.transmit(cmd) if (sw1, sw2) != (0x90, 0x00): return if __name__ == "__main__": import sys if 2 == len(sys.argv): reader_nb = int(sys.argv[1]) else: reader_nb = 0 usim_write(reader_nb)
Comments
I wanted to have reasonable names and phone numbers in my phone book. So I
generate names by randomly selecting 3 first names from 2 lists: the 50 first
(by frequency of use) male first names in French stored in the
The first 10 lines of
Pierre Juste Julien Olivier Henri Jacques Philippe Nicolas Aime Antoine [...]
The first 10 lines of
Marie Victoire Claire Marine Reine Virginie Vienne Solange Jolie Marguerite [...]
For the phone number I just select a random 10-digits number.
Maybe the entries I created are non functional in a real phone. First check it
works for you if you want to reuse this code. Also if you plan to reuse my
source code you must read "My blog source code license" first.
Output
$ ./usim_write.py Available readers: - Gemalto PC Twin Reader Using: Gemalto PC Twin Reader connecting to Gemalto PC Twin Reader Select MF > A0 A4 00 00 02 3F 00 < [] 9F 22 Select DF Telecom > A0 A4 00 00 02 7F 10 < [] 9F 22 Select EF ADN > A0 A4 00 00 02 6F 3A < [] 9F 0F Get Response > A0 C0 00 00 0F < 00 00 21 34 6F 3A 04 00 11 FF 22 01 02 01 22 90 00 name: Juliette Claire Fran phone: 50 47 00 17 44 > A0 DC 01 04 22 4A 75 6C 69 65 74 74 65 20 43 6C 61 69 72 65 20 46 72 61 6E 06 A1 50 47 00 17 44 FF FF FF FF FF FF FF < [] 90 00 name: Luc Nicolas Regis phone: 83 46 67 10 73 > A0 DC 02 04 22 4C 75 63 20 4E 69 63 6F 6C 61 73 20 52 65 67 69 73 FF FF FF 06 A1 83 46 67 10 73 FF FF FF FF FF FF FF < [] 90 00 name: Julien Jeremie Serge phone: 89 07 17 20 07 > A0 DC 03 04 22 4A 75 6C 69 65 6E 20 4A 65 72 65 6D 69 65 20 53 65 72 67 65 06 A1 89 07 17 20 07 FF FF FF FF FF FF FF < [] 90 00 [...]
The output is truncated. I do not want to include all the 255 phone numbers.
If I use
$ ./usim_read.py Available readers: - Gemalto PC Twin Reader Using: Gemalto PC Twin Reader Select MF Select DF Telecom Select EF ADN Get Response 1: Name: Juliette Claire Fra, phone: 0574007144 2: Name: Luc Nicolas Regis.., phone: 3864760137 3: Name: Julien Jeremie Serg, phone: 9870710270 [...]
Note that the phone numbers are reversed by group of 2 digits. 50 47 00 17 44 becomes 0574007144.
Conclusion
It is as easy to write than to read a SIM phone book.
My goal here was to be able to write "realistic" phone book entries so that
the