simple python client for mako's arduino iso7816 interface
Some hacked together client code for exploring smartcards using Mako's iso7816 arduino interface. It provides a pretty thin layer on top of Mako's sketch to allow you to specify an arbitrary byte stream and get a dump of the response back:
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00B2010C1c
===COMMAND===
00000000: 00 b2 01 0c 1c |.....|
SW1: 90 SW2: 00
===RESPONSE===
00000000: b2 70 1a 61 18 4f 07 a0 00 00 00 04 10 10 50 0a |.p.a.O........P.|
00000010: 4d 41 53 54 45 52 43 41 52 44 87 01 01 |MASTERCARD...|
OK
There's a little logic to handle a 61 'more data available' response, to detect success or failure and to pull out the SW1/SW2 status bytes - apart from that the data is raw. The plan is to expand the protocol parsing as required.
Usage is straight forward; the first arg is the serial device, the second arg is the command as a hex string. The second arg can optionally be an 'R' - this performs the ATR reset process. Example usage:
Reset the card
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 R
===COMMAND===
RESET
SW1: 90 SW2: 00
===RESPONSE===
00000000: 3b 6e 00 00 00 31 c0 71 d6 65 7d e4 01 11 a0 83 |;n...1.q.e}.....|
OK
Select the PSE directory
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00A404000E315041592E5359532E4444463031
===COMMAND===
00000000: 00 a4 04 00 0e 31 50 41 59 2e 53 59 53 2e 44 44 |.....1PAY.SYS.DD|
00000010: 46 30 31 |F01|
SW1: 61 SW2: 26
SW1: 90 SW2: 00
===RESPONSE===
00000000: c0 6f 24 84 0e 31 50 41 59 2e 53 59 53 2e 44 44 |.o$..1PAY.SYS.DD|
00000010: 46 30 31 a5 12 88 01 01 bf 0c 0c c5 0a ff ff 3f |F01............?|
00000020: 00 00 00 03 ff ff 03 |.......|
OK
Get the PSE record (two commands as it doesn't yet handle the 6C 'more data available')
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00B2010C00
===COMMAND===
00000000: 00 b2 01 0c 00 |.....|
SW1: 6c SW2: 1c
===ERROR===
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00B2010C1c
===COMMAND===
00000000: 00 b2 01 0c 1c |.....|
SW1: 90 SW2: 00
===RESPONSE===
00000000: b2 70 1a 61 18 4f 07 a0 00 00 00 04 10 10 50 0a |.p.a.O........P.|
00000010: 4d 41 53 54 45 52 43 41 52 44 87 01 01 |MASTERCARD...|
OK
Select the application
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00A4040007A0000000041010
===COMMAND===
00000000: 00 a4 04 00 07 a0 00 00 00 04 10 10 |............|
SW1: 61 SW2: 2b
SW1: 90 SW2: 00
===RESPONSE===
00000000: c0 6f 29 84 07 a0 00 00 00 04 10 10 a5 1e 50 0a |.o)...........P.|
00000010: 4d 41 53 54 45 52 43 41 52 44 87 01 01 bf 0c 0c |MASTERCARD......|
00000020: c5 0a 02 01 7f 00 47 00 02 ff ff 02 |......G.....|
OK
Read the AFL
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 80A80000028300
===COMMAND===
00000000: 80 a8 00 00 02 83 00 |.......|
SW1: 61 SW2: 0c
SW1: 90 SW2: 00
===RESPONSE===
00000000: c0 77 0a 82 02 58 00 94 04 08 01 06 01 |.w...X.......|
OK
Pin mapping:

- clk - D9
- data - D12
- reset - D10
Code:
#!/usr/bin/python
"""
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 R
lee@markov:~/sketchbook/iso7816$ ./client.py /dev/ttyUSB0 00A4040007A0000000041010
"""
import sys
from time import sleep
def chunks(l, n):
return [l[i:i+n] for i in range(0, len(l), n)]
#https://gist.github.com/7h3rAm/5603718
def hexdump(src, length=16, sep='.'):
FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep for x in range(256)])
lines = []
for c in xrange(0, len(src), length):
chars = src[c:c+length]
hex = ' '.join(["%02x" % ord(x) for x in chars])
if len(hex) > 24:
hex = "%s %s" % (hex[:24], hex[24:])
printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars])
lines.append("%08x: %-*s |%s|\n" % (c, length*3, hex, printable))
print ''.join(lines)
#################################################
command = ''
dumpcommand = ''
if len(sys.argv) != 3:
print "help"
quit()
else:
portname = sys.argv[1]
rawcommand = sys.argv[2]
print "===COMMAND==="
if rawcommand == 'R': #RESET
print "RESET"
print
command = rawcommand
else:
for i in chunks(rawcommand, 2): #parse each two chars into chunks
command = command +'.'+ chr(int(i, 16)) #then convert them to bytes and put the '.' in
dumpcommand = dumpcommand + chr(int(i, 16))
hexdump(dumpcommand)
#portname = "/dev/ttyUSB0"
#RESET = 'R'
#PROVIDER = '.\x00.\xA4.\x04.\x00.\x07.\xA0.\x00.\x00.\x00.\x04.\x10.\x10' #00A4040007A0000000041010
#command = RESET
#command = PROVIDER
e = 0
reading = False
SW1 = 0
SW2 = 0
line = []
with open(portname,"r+") as f:
while f:
b = f.read(1)
if e > 64:
e = 0
print "SW1:","%02x" % ord(SW1)," SW2:","%02x" % ord(SW2)
if SW1 == "\x90" and SW2 == "\x00":
print "===RESPONSE==="
hexdump(line[:-2])
print "OK"
f.close()
f = None
elif SW1 == "\x61":
#print "next","SW1:","%02x" % ord(SW1)," SW2:","%02x" % ord(SW2)
f.write('.\x00.\xC0.\x00.\x00.')
f.write(SW2)
SW1 = "\x00"
else:
#uh oh
print "===ERROR==="
hexdump(line[:-2])
f.close()
f = None
line = []
if b:
if b == 'A': #ack
#print "ack"
pass
elif b == '.':
b = f.read(1)
while not b:
b = f.read(1)
line.append(b)
#print "b:","%02x" % ord(b)
SW1 = SW2
SW2 = b
else:
e = e + 1
sleep(0.01)
if not reading:
reading = True
f.write(command)