Our wallets are filling up with SIM and RFID cards that contain hidden information. using our latest project, the Bus Pirate universal serial interface, we can dump the memory from many common smart cards. In today’s How-to, we show you how to interface common smart cards, and walk you through the data stored on a FedEx Kinko’s prepaid value card.
Pozadie
The FedEx Kinko’s prepaid card is actually a SLE4442 smart card. There’s nothing secret about the SLE4442, it’s completely documented in the datasheet (PDF), and you can buy blank cards on the web.
The card is openly readable, we’ll be able to look at the contents without any sort of malicious intrusion. It’s protected from writes by a three byte password, with a ‘three strikes you’re out’ policy that renders the card useless after three failed password attempts.
Due to its wide-spread use, in Kinko’s and other capacities, the SLE4442 has been the target of several high-profile hacks. At the ’06 Toorcon, [bunnie] and [Chris Tarnovsky] hosted a discussion on the card. [Chris] examined the silicon die and suggested that shorting a trace might defeat the security measures. You can see high-resolution images of the die on his site. [Strom Carlson] went right to the source and snooped the password with a logic analyzer, as documented in his famous ’06 Defcon presentation. The card even makes appearances in artwork.
We’re not planning on maliciously intruding on the card, but we can still look at the contents and demonstrate how to interface arbitrary protocols with our latest project, the Bus Pirate.
Connecting to the SLE4442
Pin
Function
Bus Pirate pin
1
+5volts
+5volt supply
2
Resetovať
AUX
3
Clock
SCL
4
Data IO
SDA
5
N/C
–
6
Ground
Ground
Grab the SLE4442 datasheet (PDF) if you haven’t already. The pinout is shown in the picture above. If you’re having trouble orienting the card, note that the large center pad connects to ground.
The card requires 5volts DC (datasheet page 27, table 3.2.2), we used the Bus Pirate’s handy 5volt supply. Interfacing at five volts is no problem because the Bus Pirate inputs are all 5volt tolerant.
A two-wire interface is used, with a clock line and bi-directional data line. We connected these to the Bus Pirate’s SDA and SCL pins. A third signal, reset, is required to initialize the chip; we used the Bus Pirate’s auxiliary output to control the reset line. The maximum clock frequency we can use to interface the device is 50kHz, with a 7kHz stated minimum (page 28, table 3.2.4:fCLK). The Bus Pirate’s raw 2 wire protocol runs at about 5kHz, but we didn’t have any problems interfacing the device.
The sle4442 has open collector outputs, and depends on pull-up resistors to hold the bus high. instead of switching the data pin between ground and 5volts, it switches between ground and high-impedance states. High-impedance means that the chip exerts no state on the line, it lets it float, like a microcontroller input pin.
Each of the signal lines need to be pulled-up to 5volts with a 2K-10K resistor, the value isn’t particularly important. Without the pull-up resistor, we’ll never see anything but 0 (ground) on the bus because the sle4442 doesn’t exert a voltage of it’s own. A benefit of this technique is that the Bus Pirate, which only switches at 3.3volts, will talk to the sle4442 at a full 5volts, in compliance with the 3.5volt minimum voltage for a high level (datasheet, page 27, table 3.2.3:Vih).
Initializing the card
Before we can read data from the card, we have to initialize it. This is done with a standard ISO 7816-3 answer to Reset (ATR) command. After initialization, we can read from the card using a simple two wire protocol.
Setup raw 2 wire mode
The interface shares some characteristics with I2C, but it’s not compatible. We used the Bus Pirate’s raw 2 wire bus mode to interface the device.
RAW2>m
1. SPI
2. I2C
3. UART
4. RAW 2 WIRE
5. RAW 3 WIRE
MODE>4 <– raw 2 wire bus mode
900 mode SET
...
SPEED>1 <– speed setting is ignored in current firmware
901 speed SET
1. High-Z outputs (H=input, L=GND)
2. normal outputs (H=Vcc, L=GND)
MODE>1 <– high impedance output type
9xx OUTPUT HIGHZ
402 RAW2WIRE READY, P FOR PULLUPS
RAW2>
The Bus Pirate has on-board pull-up resistors, but they only pull to 3.3volts. We must use external pull-ups to 5volts, as shown in the picture. High-Z output mode is compatible with the bus, normal outputs would put 3.3volts on the bus, potentially damaging something.
RAW2>l <–configure MSB/LSB 1. MSB first 2. LSB first MODE>2 <– LSB first 9xx LSB: least SIG bit FIRST RAW2>
The card reads and writes each byte least significant bit first (datasheet page 10). We use menu option L to set the data mode to LSB first.
Send 7816-3 ISO answer to reset command
ISO 7816-3 “answer to reset” is a standardized command used among many smart cards. The ATR sequence is shown above: reset is held high, one clock pulse is sent, reset is released. The next 32 clock pulses (4 bytes) read a generic ATR header from the card. The header contains information about the card type and protocol. Multi-card smart card readers use this to determine how to read each card.
RAW2>@^arrrr <– aux high (highz), clock tick, aux low, read 4 bytes 952 AUX HIGH IMP, READ: 1 4xx RAW2Wire 0x01 Hodiny Ticks 950 AUX LOW 430 RAW2WIRE READ: 0xA2 <–begin ATR header bytes 430 RAW2WIRE READ: 0x13 430 RAW2WIRE READ: 0x10 430 RAW2WIRE READ: 0x91 RAW2>
We issue the command @^arrrr to the Bus Pirate. @ puts the auxiliary pin in high-impedance input mode, the pull-up resistor holds the reset at 5volts. ^ issues one clock pulse, with delay. a returns the auxiliary pin to output and holds the reset line at ground.
r issues 8 clock pulses and displays the returned bits as a byte. This is one instance where the protocol is incompatible with I2C. I2C includes an additional acknowledge bit between each byte, the sle4442 outputs 32bits consecutively.
Page 25 of the datasheet explains the ISO7816-3 header. It’s easiest to interpret in binary. rather than convert everything to binary, we set the Bus Pirate to binary display mode and issued another ATR command.
RAW2>o <–setup the output mode 1. HEX 2. DEC 3. BIN 4. RAW OUTPUT MODE>3 <–show numbers in binary 903 OUTPUT mode SET RAW2>@^arrrr <–another ATR command 952 AUX HIGH IMP, READ: 1 4xx RAW2WIRE 0b00000001 CLOCK TICKS 950 AUX LOW 430 RAW2WIRE READ: 0b10100010 <–0xA2 430 RAW2WIRE READ: 0b00010011 <–0x13 430 RAW2WIRE READ: 0b00010000 <–0x10 430 RAW2WIRE READ: 0b10010001 <–0x91 RAW2>
The first 2 bytes are protocol header bytes according to ISO 7816-3 (datasheet page 25).
Byte 1 identifies the protocol type.
0b10100010
7:4 – Protocol type (1010=2 wire)
3:3 – RFU (0)
2:0 – structure Identifier (010=general)
Byte 2, protocol parameters, tells us about the card if we didn’t have a datasheet.
0b00010011
7:7 – supports random read lengths (0=no, read to end)
6:3 – data units (0010=256units)
2:0 – data unit bits (011=8bits per unit)
From the header we can tell that the protocol type is 10, a two wire bus. The card must be read all the way to the end before it accepts a new command. It has 8bits to a unit, and 256units; 256bytes total storage. The final two bytes are 7814-4 data, which seems uninteresting (see datasheet page 26).
Dump main memory (256 bytes)
Once the card is reset and the ATR bytes are read, we can send commands to the card. Commands are three bytes long; they begin with a I2C-style start condition, and end with an I2C-style stop condition. start and stop conditions can be generated manually with \-/_\ and _/-\, but the raw 2 wire library also includes the shortcuts { and }. The start and stop conditions are the same as I2C, but they’re used at a different point in the transmission.
The read main memory command is 0x30, followed by a read start address (0), and a third byte that doesn’t matter (0xff). After the stop condition, the card outputs data on every clock until it reaches the end of the memory. As described by the ATR header, no new commands can be sent until the card reaches the last byte of memory. starting at read address 0, it takes 256*8 clock pulses to complete the read cycle.
RAW2>{0x30 0 0xff} 0r255 0r10 <–command 410 RAW2WIRE start condition (\-/_\) 420 RAW2WIRE WRITE: 0x30 420 RAW2WIRE WRITE: 0x00 420 RAW2WIRE WRITE: 0x00 440 RAW2WIRE stop condition (_/-\) 431 RAW2WIRE bulk READ, 0xFF BYTES: <–bulk read of 255 bytes 0xA2 0x13 0x10 0x91 0x46 0xFF 0x81 0x15 0xFF 0x01 0x4B 0x03 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xD2 0x76 0x00 0x00 0x04 0x09 0xFF 0xFF 0xFF 0xFF 0xFF 0x7B 0x14 0xAE 0x47 0xE1 0x7A 0x94 0x3F 0x4C 0x46 0xC6 0x3B 0x00 0x00 0x00 0x00 0x20 0x08 0x03 0x04 0x09 0x** 0x** 0x** 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x30 0x31 0x3* 0x3* 0x30 0x30 0x31 0x33 0x3* 0x3* 0x3* 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x43 0x61 0x73 0x68 0x20 0x43 0x75 0x73 0x74 0x6F 0x6D 0x65 0x72 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x39 0x39 0x31 0x31 0x00 0x31 0x30 0x31 0x00 0x30 0x30 0x30 0x30 0x30 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x03 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x20 0x08 0x03 0x04 0x09 0x** 0x** 0x** 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0x00 0x00 431 RAW2WIRE bulk READ, 0x0A BYTES: <–again to get last byte (256) 0x00 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF RAW2>
{ issues the bus start condition. 0x30 sends the read address, 0 is the start byte, and 0xff could be anything.} Odosiela stav zastavenia autobusu. 0R255 Hodiny v 255 8bitových bajtoch a zobrazí ich na obrazovke. Karta má vlastne 256 bajtov hlavnej pamäte, takže vydávame ďalší príkaz čítania, aby sme získali posledný bajt a overil, či sa zbernica vráti na vysoko po skončení čítania. Nemôžeme použiť 0R256, pretože autobus pirát nechápe desatinné čísla väčšie ako 255 (mali by sme to riešiť).
Čo znamenajú údaje?
Kartu sme odrezali podľa dátového listu, [Strom] defcon prezentácie, a tento šikovný sprievodca (PDF).
32BYTE HEARTER …
0xA2 0x13 0x10 0x91 <- Prvé štyri bajty sú opakovaním údajov ATR 0x46 0xff 0x81 0x15 <-manufacturer Tagy, Ostatné Junk 0xff 0x01 0x4B 0x03 0x00 <-iccf, IC Card Fabricator ID 0xff 0xff 0xff 0xff <-iccn, IC sériové číslo, 0 0xff 0xff 0xff 0xff <- Rôzne tagy a dĺžky, 0 0xd2 0x76 0x00 0x00 0x04 0x09 <-Application identifikátor (Kinko's?) 0xff 0xff 0xff 0xff 0xff <-all ostatné bajty 0 Prvých 32 bajtov je trvalo spálená hlavička so sériovými číslami, výrobcami kódmi a inými jedinečnými údajmi (Dátový list). Táto hlavička zabraňuje presnej duplikácii kariet, aj keď máte prázdnu kartu a bezpečnostný kód. Kinko nemá na každej karte trvalo spálili vlastné sériové číslo. Teraz dáta .... 0x7B 0x14 0xaE 0x47 0xE1 0x7A 0x94 0x3F <- IEEE-754 Hodnota, $ 0.02 Toto je hodnota uložená na karte, vo formáte IEEE-754. Túto pomôcku môžete použiť na čitateľné. 0x3F947AE147AE147B = $ 0.02. ... 8 bajtov junk ... 0x20 <- 0x20 po kopírovaní, 0x00 po počítači 0x08 0x03 0x04 0x09 0x ** 0x ** 0x ** <- Dátum / čas zakúpený Toto je dátum a čas, kedy bola karta zakúpená, 2008 marca 4, 9: **: **. **. Niektoré číslice boli zakryté na ochranu anonymitu nášho dodávateľa. ... 40 bajtov junk ... 0x30 0x31 0x3 * 0x3 * <-store číslo: 01 ** 0x30 0x30 0x31 0x33 0x3 * 0x3 * 0x3 * <- Sn: 0013 *** Sériové číslo karty sa skladá z čísla obchodu a jedinečného, sedemmiestneho čísla. Niektoré číslice zakryté. ... viac bajtov ... 0x08 0x03 0x04 0x09 0x ** 0x ** 0x ** <- inokedy ... viac bajtov ... 0xff 0xff 0xff 0xff 0xff 0x00 0x00 0x00 <- posledných 8 bajtov na karte 0xff 0xff ... <-Not Real Data Bytes Ochrana skládok (4 bajty) Prvé 32 bajtov dátovej pamäte môže byť chránené. Každý bit štyroch bajtových údajov o ochrane údajov (príkaz 0x34) predstavuje bajt dátovej pamäte. Trochu nastavený na 1 nemožno prepísať. Môžeme prečítať register ochrany údajov a zistiť, ktoré bajty hlavnej pamäte sú chránené. To je najjednoduchšie pochopiť v binárne, takže sme túto operáciu urobili v binárnom výstupnom režime. RAW2> {0x34 0 0} 0R4 <-command 410 RAW2Wire Start Start (- / _ \) 420 RAW2Wire Write: 0B00110100 420 RAW2Wire Write: 0B00000000 420 RAW2Wire Write: 0B00000000 440 Stav zastavenia SAW2Wire (_ / -) 431 RAW2Wire Hromadné čítanie, 0B00000100 Bytes: 0B00100000 0B11100001 0B00011111 0B111111000 <-Data Register ochrany RAW2>
Každý bit zodpovedá jednému z prvých 32 bajtov pamäte karty. Ak je bit jeden, zodpovedajúce bajt je chránené. Tento register môže byť napísaný, ale len vtedy, ak máte správne heslo.
Výpis bezpečnostnej pamäte (4 bajty)
Bezpečnostná pamäť obsahuje počítadlo pokusov o overenie hesla a tri bajtové heslo. Môžeme si prečítať čítanie bezpečnostnej pamäte bez hesla, ale BYTES HESLOULTSUJE sa bude čítať ako 0. Adresa bezpečnostnej pamäte je 0x31.
RAW2> {0x31 0 0} 0R4 <-command 410 RAW2Wire Start Start (- / _ \) 420 RAW2Wire Write: 0B00110001 420 RAW2Wire Write: 0B00000000 420 RAW2Wire Write: 0B00000000 440 Stav zastavenia SAW2Wire (_ / -) 431 RAW2Wire Hromadné čítanie, 0B00000100 Bytes: 0B00000100 0B00000000 0B00000000 0B0000000000 <-Mytes RAW2>
Počítadlo pokusov začína na tri (0B00000111) a počíta sa na 0. Keď čítanie číta 0, karta je v podstate zničená. Použili sme dva prístupové pokusy o testovanie hesla príkazov, táto karta sa vyskúšať vľavo.
Tehotný