]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/cmdhf.c
ADD: `analyse nuid` - generates NUID 4byte from a UID 7byte. Mifare Classic Ev1...
[proxmark3-svn] / client / cmdhf.c
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // High frequency commands
9 //-----------------------------------------------------------------------------
10
11 #include <stdio.h>
12 #include <string.h>
13 #include "proxmark3.h"
14 #include "graph.h"
15 #include "ui.h"
16 #include "cmdparser.h"
17 #include "cmdhf.h"
18 #include "cmdhf14a.h"
19 #include "cmdhf14b.h"
20 #include "cmdhf15.h"
21 #include "cmdhfepa.h"
22 #include "cmdhflegic.h" // LEGIC
23 #include "cmdhficlass.h" // ICLASS
24 #include "cmdhfmf.h" // CLASSIC
25 #include "cmdhfmfu.h" // ULTRALIGHT/NTAG etc
26 #include "cmdhfmfdes.h" // DESFIRE
27 #include "cmdhftopaz.h" // TOPAZ
28 #include "cmdhfemv.h" // EMV
29 #include "protocols.h"
30
31 static int CmdHelp(const char *Cmd);
32
33 int CmdHFTune(const char *Cmd) {
34 PrintAndLog("Measuring HF antenna, press button to exit");
35 UsbCommand c = {CMD_MEASURE_ANTENNA_TUNING_HF};
36 clearCommandBuffer();
37 SendCommand(&c);
38 return 0;
39 }
40
41 int applyIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
42 switch ( cmd[0] ){
43 case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
44 case ISO14443A_CMD_ANTICOLL_OR_SELECT:{
45 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
46 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
47 if(cmd[1] == 0x70)
48 snprintf(exp,size,"SELECT_UID");
49 else
50 snprintf(exp,size,"ANTICOLL");
51 break;
52 }
53 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{
54 //95 20 = Anticollision of cascade level2
55 //95 70 = Select of cascade level2
56 if(cmd[2] == 0x70)
57 snprintf(exp,size,"SELECT_UID-2");
58 else
59 snprintf(exp,size,"ANTICOLL-2");
60 break;
61 }
62 case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break;
63 case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break;
64 case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break;
65 case ISO14443A_CMD_HALT: snprintf(exp,size,"HALT"); break;
66 case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break;
67 case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break;
68 case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break;
69 case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break;
70 case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break;
71 case MIFARE_AUTH_KEYA:{
72 if ( cmdsize > 3)
73 snprintf(exp,size,"AUTH-A(%d)",cmd[1]);
74 else
75 // case MIFARE_ULEV1_VERSION : both 0x60.
76 snprintf(exp,size,"EV1 VERSION");
77 break;
78 }
79 case MIFARE_AUTH_KEYB: snprintf(exp,size,"AUTH-B(%d)",cmd[1]); break;
80 case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break;
81 case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break;
82 case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break;
83 case MIFARE_ULC_AUTH_1 : snprintf(exp,size,"AUTH "); break;
84 case MIFARE_ULC_AUTH_2 : snprintf(exp,size,"AUTH_ANSW"); break;
85 case MIFARE_ULEV1_AUTH :
86 if ( cmdsize == 7 )
87 snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] );
88 else
89 snprintf(exp,size,"PWD-AUTH");
90 break;
91 case MIFARE_ULEV1_FASTREAD : {
92 if ( cmdsize >=3 && cmd[2] <= 0xE6)
93 snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]);
94 else
95 snprintf(exp,size,"?");
96 break;
97 }
98 case MIFARE_ULC_WRITE : {
99 if ( cmd[1] < 0x21 )
100 snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]);
101 else
102 snprintf(exp,size,"?");
103 break;
104 }
105 case MIFARE_ULEV1_READ_CNT :{
106 if ( cmd[1] < 5 )
107 snprintf(exp,size,"READ CNT(%d)",cmd[1]);
108 else
109 snprintf(exp,size,"?");
110 break;
111 }
112 case MIFARE_ULEV1_INCR_CNT : {
113 if ( cmd[1] < 5 )
114 snprintf(exp,size,"INCR(%d)",cmd[1]);
115 else
116 snprintf(exp,size,"?");
117 break;
118 }
119 case MIFARE_ULEV1_READSIG : snprintf(exp,size,"READ_SIG"); break;
120 case MIFARE_ULEV1_CHECKTEAR : snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break;
121 case MIFARE_ULEV1_VCSL : snprintf(exp,size,"VCSL"); break;
122 default: return 0;
123 }
124 return 1;
125 }
126
127 void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
128 applyIso14443a(exp, size, cmd, cmdsize);
129 }
130
131 void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
132 switch (cmd[0]) {
133 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
134 case ICLASS_CMD_READ_OR_IDENTIFY:{
135 if(cmdsize > 1){
136 snprintf(exp,size,"READ(%d)",cmd[1]);
137 }else{
138 snprintf(exp,size,"IDENTIFY");
139 }
140 break;
141 }
142 case ICLASS_CMD_SELECT: snprintf(exp,size,"SELECT"); break;
143 case ICLASS_CMD_PAGESEL: snprintf(exp,size,"PAGESEL(%d)", cmd[1]); break;
144 case ICLASS_CMD_READCHECK_KC:snprintf(exp,size,"READCHECK[Kc](%d)", cmd[1]); break;
145 case ICLASS_CMD_READCHECK_KD:snprintf(exp,size,"READCHECK[Kd](%d)", cmd[1]); break;
146 case ICLASS_CMD_CHECK: snprintf(exp,size,"CHECK"); break;
147 case ICLASS_CMD_DETECT: snprintf(exp,size,"DETECT"); break;
148 case ICLASS_CMD_HALT: snprintf(exp,size,"HALT"); break;
149 case ICLASS_CMD_UPDATE: snprintf(exp,size,"UPDATE(%d)",cmd[1]); break;
150 case ICLASS_CMD_ACT: snprintf(exp,size,"ACT"); break;
151 case ICLASS_CMD_READ4: snprintf(exp,size,"READ4(%d)",cmd[1]); break;
152 default: snprintf(exp,size,"?"); break;
153 }
154 return;
155 }
156
157 void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
158 if(cmd[0] == 0x26) {
159 switch(cmd[1]){
160 case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");break;
161 case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");break;
162 default :snprintf(exp,size,"?"); break;
163 }
164 } else if(cmd[0] == 0x02) {
165 switch (cmd[1]) {
166 case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");break;
167 case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");break;
168 case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");break;
169 case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");break;
170 case ISO15693_SELECT :snprintf(exp, size, "SELECT");break;
171 case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");break;
172 case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");break;
173 case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");break;
174 case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");break;
175 case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");break;
176 case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");break;
177 case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");break;
178 default: snprintf(exp,size,"?"); break;
179 }
180 }
181 }
182
183 void annotateTopaz(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){
184 switch(cmd[0]) {
185 case TOPAZ_REQA :snprintf(exp, size, "REQA");break;
186 case TOPAZ_WUPA :snprintf(exp, size, "WUPA");break;
187 case TOPAZ_RID :snprintf(exp, size, "RID");break;
188 case TOPAZ_RALL :snprintf(exp, size, "RALL");break;
189 case TOPAZ_READ :snprintf(exp, size, "READ");break;
190 case TOPAZ_WRITE_E :snprintf(exp, size, "WRITE-E");break;
191 case TOPAZ_WRITE_NE :snprintf(exp, size, "WRITE-NE");break;
192 case TOPAZ_RSEG :snprintf(exp, size, "RSEG");break;
193 case TOPAZ_READ8 :snprintf(exp, size, "READ8");break;
194 case TOPAZ_WRITE_E8 :snprintf(exp, size, "WRITE-E8");break;
195 case TOPAZ_WRITE_NE8 :snprintf(exp, size, "WRITE-NE8");break;
196 default :snprintf(exp,size,"?"); break;
197 }
198 }
199
200 // iso 7816-3
201 void annotateIso7816(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){
202 // S-block
203 if ( (cmd[0] & 0xC0) && (cmdsize == 3) ) {
204 switch ( (cmd[0] & 0x3f) ) {
205 case 0x00 : snprintf(exp, size, "S-block RESYNCH req"); break;
206 case 0x20 : snprintf(exp, size, "S-block RESYNCH resp"); break;
207 case 0x01 : snprintf(exp, size, "S-block IFS req"); break;
208 case 0x21 : snprintf(exp, size, "S-block IFS resp"); break;
209 case 0x02 : snprintf(exp, size, "S-block ABORT req"); break;
210 case 0x22 : snprintf(exp, size, "S-block ABORT resp"); break;
211 case 0x03 : snprintf(exp, size, "S-block WTX reqt"); break;
212 case 0x23 : snprintf(exp, size, "S-block WTX resp"); break;
213 default : snprintf(exp, size, "S-block"); break;
214 }
215 }
216 // R-block (ack)
217 else if ( ((cmd[0] & 0xD0) == 0x80) && ( cmdsize > 2) ) {
218 if ( (cmd[0] & 0x10) == 0 )
219 snprintf(exp, size, "R-block ACK");
220 else
221 snprintf(exp, size, "R-block NACK");
222 }
223 // I-block
224 else {
225
226 int pos = (cmd[0] == 2 || cmd[0] == 3) ? 2 : 3;
227 switch ( cmd[pos] ){
228 case ISO7816_READ_BINARY :snprintf(exp, size, "READ BIN");break;
229 case ISO7816_WRITE_BINARY :snprintf(exp, size, "WRITE BIN");break;
230 case ISO7816_UPDATE_BINARY :snprintf(exp, size, "UPDATE BIN");break;
231 case ISO7816_ERASE_BINARY :snprintf(exp, size, "ERASE BIN");break;
232 case ISO7816_READ_RECORDS :snprintf(exp, size, "READ RECORDS");break;
233 case ISO7816_WRITE_RECORDS :snprintf(exp, size, "WRITE RECORDS");break;
234 case ISO7816_APPEND_RECORD :snprintf(exp, size, "APPEND RECORD");break;
235 case ISO7816_UPDATE_RECORD :snprintf(exp, size, "UPDATE RECORD");break;
236 case ISO7816_GET_DATA :snprintf(exp, size, "GET DATA");break;
237 case ISO7816_PUT_DATA :snprintf(exp, size, "PUT DATA");break;
238 case ISO7816_SELECT_FILE :snprintf(exp, size, "SELECT FILE");break;
239 case ISO7816_VERIFY :snprintf(exp, size, "VERIFY");break;
240 case ISO7816_INTERNAL_AUTHENTICATION :snprintf(exp, size, "INTERNAL AUTH");break;
241 case ISO7816_EXTERNAL_AUTHENTICATION :snprintf(exp, size, "EXTERNAL AUTH");break;
242 case ISO7816_GET_CHALLENGE :snprintf(exp, size, "GET CHALLENGE");break;
243 case ISO7816_MANAGE_CHANNEL :snprintf(exp, size, "MANAGE CHANNEL");break;
244 default :snprintf(exp,size,"?"); break;
245 }
246 }
247 }
248
249 // MIFARE DESFire
250 void annotateMfDesfire(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){
251
252 // it's basically a ISO14443a tag, so try annotation from there
253 if (!applyIso14443a(exp, size, cmd, cmdsize)){
254 //PrintAndLog("rest");
255 //PrintAndLog("(%d)",cmd[0]);
256 // S-block 11xxx010
257 if ( (cmd[0] & 0xC0) && (cmdsize == 3) ) {
258 switch ( (cmd[0] & 0x30) ) {
259 case 0x30 : snprintf(exp, size, "S-block DESELECT"); break;
260 case 0x00 : snprintf(exp, size, "S-block WTX"); break;
261 default : snprintf(exp, size, "S-block"); break;
262 }
263 }
264 // R-block (ack) 101xx01x
265 else if ( ((cmd[0] & 0xB0) == 0xA0) && ( cmdsize > 2) ) {
266 if ( (cmd[0] & 0x10) == 0 )
267 snprintf(exp, size, "R-block ACK(%d)", (cmd[0] & 0x01));
268 else
269 snprintf(exp, size, "R-block NACK(%d)", (cmd[0] & 0x01));
270 }
271 // I-block 000xCN1x
272 else if ( (cmd[0] & 0xC0) == 0x00){
273 // PCB [CID] [NAD] [INF] CRC CRC
274 int pos = 1;
275 if ( (cmd[0] & 0x08) == 0x08) // cid byte following
276 pos = pos + 1;
277 if ( (cmd[0] & 0x04) == 0x04) // nad byte following
278 pos = pos + 1;
279 //PrintAndLog("[%d]",pos);
280 switch ( cmd[pos] ){
281 case MFDES_CREATE_APPLICATION :snprintf(exp, size, "CREATE APPLICATION");break;
282 case MFDES_DELETE_APPLICATION :snprintf(exp, size, "DELETE APPLICATION");break;
283 case MFDES_GET_APPLICATION_IDS :snprintf(exp, size, "GET APPLICATION IDS");break;
284 case MFDES_SELECT_APPLICATION :snprintf(exp, size, "SELECT APPLICATION");break;
285 case MFDES_FORMAT_PICC :snprintf(exp, size, "FORMAT PICC");break;
286 case MFDES_GET_VERSION :snprintf(exp, size, "GET VERSION");break;
287 case MFDES_READ_DATA :snprintf(exp, size, "READ DATA");break;
288 case MFDES_WRITE_DATA :snprintf(exp, size, "WRITE DATA");break;
289 case MFDES_GET_VALUE :snprintf(exp, size, "GET VALUE");break;
290 case MFDES_CREDIT :snprintf(exp, size, "CREDIT");break;
291 case MFDES_DEBIT :snprintf(exp, size, "DEBIT");break;
292 case MFDES_LIMITED_CREDIT :snprintf(exp, size, "LIMITED CREDIT");break;
293 case MFDES_WRITE_RECORD :snprintf(exp, size, "WRITE RECORD");break;
294 case MFDES_READ_RECORDS :snprintf(exp, size, "READ RECORDS");break;
295 case MFDES_CLEAR_RECORD_FILE :snprintf(exp, size, "CLEAR RECORD FILE");break;
296 case MFDES_COMMIT_TRANSACTION :snprintf(exp, size, "COMMIT TRANSACTION");break;
297 case MFDES_ABORT_TRANSACTION :snprintf(exp, size, "ABORT TRANSACTION");break;
298 case MFDES_GET_FREE_MEMORY :snprintf(exp, size, "GET FREE MEMORY");break;
299 case MFDES_GET_FILE_IDS :snprintf(exp, size, "GET FILE IDS");break;
300 case MFDES_GET_ISOFILE_IDS :snprintf(exp, size, "GET ISOFILE IDS");break;
301 case MFDES_GET_FILE_SETTINGS :snprintf(exp, size, "GET FILE SETTINGS");break;
302 case MFDES_CHANGE_FILE_SETTINGS :snprintf(exp, size, "CHANGE FILE SETTINGS");break;
303 case MFDES_CREATE_STD_DATA_FILE :snprintf(exp, size, "CREATE STD DATA FILE");break;
304 case MFDES_CREATE_BACKUP_DATA_FILE :snprintf(exp, size, "CREATE BACKUP DATA FILE");break;
305 case MFDES_CREATE_VALUE_FILE :snprintf(exp, size, "CREATE VALUE FILE");break;
306 case MFDES_CREATE_LINEAR_RECORD_FILE :snprintf(exp, size, "CREATE LINEAR RECORD FILE");break;
307 case MFDES_CREATE_CYCLIC_RECORD_FILE :snprintf(exp, size, "CREATE CYCLIC RECORD FILE");break;
308 case MFDES_DELETE_FILE :snprintf(exp, size, "DELETE FILE");break;
309 case MFDES_AUTHENTICATE :snprintf(exp, size, "AUTH NATIVE (keyNo %d)", cmd[pos+1]);break; // AUTHENTICATE_NATIVE
310 case MFDES_AUTHENTICATE_ISO :snprintf(exp, size, "AUTH ISO (keyNo %d)", cmd[pos+1]);break; // AUTHENTICATE_STANDARD
311 case MFDES_AUTHENTICATE_AES :snprintf(exp, size, "AUTH AES (keyNo %d)", cmd[pos+1]);break;
312 case MFDES_CHANGE_KEY_SETTINGS :snprintf(exp, size, "CHANGE KEY SETTINGS");break;
313 case MFDES_GET_KEY_SETTINGS :snprintf(exp, size, "GET KEY SETTINGS");break;
314 case MFDES_CHANGE_KEY :snprintf(exp, size, "CHANGE KEY");break;
315 case MFDES_GET_KEY_VERSION :snprintf(exp, size, "GET KEY VERSION");break;
316 case MFDES_AUTHENTICATION_FRAME :snprintf(exp, size, "AUTH FRAME / NEXT FRAME");break;
317 default :break;
318 }
319 }else{
320 // anything else
321 snprintf(exp,size,"?");
322 }
323 }
324 }
325
326 /**
327 06 00 = INITIATE
328 0E xx = SELECT ID (xx = Chip-ID)
329 0B = Get UID
330 08 yy = Read Block (yy = block number)
331 09 yy dd dd dd dd = Write Block (yy = block number; dd dd dd dd = data to be written)
332 0C = Reset to Inventory
333 0F = Completion
334 0A 11 22 33 44 55 66 = Authenticate (11 22 33 44 55 66 = data to authenticate)
335 **/
336
337 void annotateIso14443b(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
338 switch(cmd[0]){
339 case ISO14443B_REQB : {
340
341 switch ( cmd[2] & 0x07 ) {
342 case 0: snprintf(exp, size,"1 slot ");break;
343 case 1: snprintf(exp, size,"2 slots ");break;
344 case 2: snprintf(exp, size,"4 slots ");break;
345 case 3: snprintf(exp, size,"8 slots ");break;
346 default: snprintf(exp, size,"16 slots ");break;
347 }
348 if ( (cmd[2] & 0x8) )
349 snprintf(exp, size,"WUPB");
350 else
351 snprintf(exp, size,"REQB");
352 break;
353 }
354 case ISO14443B_ATTRIB : snprintf(exp,size,"ATTRIB");break;
355 case ISO14443B_HALT : snprintf(exp,size,"HALT");break;
356 case ISO14443B_INITIATE : snprintf(exp,size,"INITIATE");break;
357 case ISO14443B_SELECT : snprintf(exp,size,"SELECT(%d)",cmd[1]);break;
358 case ISO14443B_GET_UID : snprintf(exp,size,"GET UID");break;
359 case ISO14443B_READ_BLK : snprintf(exp,size,"READ_BLK(%d)", cmd[1]);break;
360 case ISO14443B_WRITE_BLK : snprintf(exp,size,"WRITE_BLK(%d)",cmd[1]);break;
361 case ISO14443B_RESET : snprintf(exp,size,"RESET");break;
362 case ISO14443B_COMPLETION : snprintf(exp,size,"COMPLETION");break;
363 case ISO14443B_AUTHENTICATE : snprintf(exp,size,"AUTHENTICATE");break;
364 case ISO14443B_PING : snprintf(exp,size,"PING");break;
365 case ISO14443B_PONG : snprintf(exp,size,"PONG");break;
366 default : snprintf(exp,size ,"?");break;
367 }
368 }
369
370 // LEGIC
371 // 1 = read
372 // 0 = write
373 // Quite simpel tag
374 void annotateLegic(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize){
375 uint8_t bitsend = cmd[0];
376 uint8_t cmdBit = (cmd[1] & 1);
377 switch (bitsend){
378 case 7:
379 snprintf(exp, size, "IV 0x%02X", cmd[1]);
380 break;
381 case 6: {
382 switch ( cmd[1] ) {
383 case LEGIC_MIM_22: snprintf(exp, size, "MIM22"); break;
384 case LEGIC_MIM_256: snprintf(exp, size, "MIM256"); break;
385 case LEGIC_MIM_1024: snprintf(exp, size, "MIM1024"); break;
386 case LEGIC_ACK_22: snprintf(exp, size, "ACK 22"); break;
387 case LEGIC_ACK_256: snprintf(exp, size, "ACK 256/1024"); break;
388 }
389 break;
390 }
391 case 9:
392 case 11: {
393
394 uint16_t address = (cmd[2] << 7) | cmd[1] >> 1;
395
396 if (cmdBit == LEGIC_READ)
397 snprintf(exp, size, "READ Byte(%d)", address);
398
399 if (cmdBit == LEGIC_WRITE )
400 snprintf(exp, size, "WRITE Byte(%d)", address);
401 break;
402 }
403 case 21: {
404 if (cmdBit == LEGIC_WRITE ) {
405 uint16_t address = ((cmd[2] << 7) | cmd[1] >> 1) & 0xFF;
406 uint8_t val = (cmd[3] & 1 ) << 7 | cmd[2] >> 1;
407 snprintf(exp, size, "WRITE Byte(%d) %02X", address, val);
408 }
409 break;
410 }
411 case 23: {
412 if (cmdBit == LEGIC_WRITE ) {
413 uint16_t address = ((cmd[2] << 7) | cmd[1] >> 1) & 0x3FF;
414 uint8_t val = (cmd[3] & 0x7 ) << 5 | cmd[2] >> 3;
415 snprintf(exp, size, "WRITE Byte(%d) %02X", address, val);
416 }
417 break;
418 }
419 case 12:
420 default:
421 break;
422 }
423 }
424
425 /**
426 * @brief iso14443A_CRC_check Checks CRC in command or response
427 * @param isResponse
428 * @param data
429 * @param len
430 * @return 0 : CRC-command, CRC not ok
431 * 1 : CRC-command, CRC ok
432 * 2 : Not crc-command
433 */
434
435 uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
436 {
437 uint8_t b1,b2;
438
439 if(len <= 2) return 2;
440
441 if(isResponse & (len < 6)) return 2;
442
443 ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2);
444 if (b1 != data[len-2] || b2 != data[len-1]) {
445 return 0;
446 } else {
447 return 1;
448 }
449 }
450
451
452 /**
453 * @brief iso14443B_CRC_check Checks CRC in command or response
454 * @param isResponse
455 * @param data
456 * @param len
457 * @return 0 : CRC-command, CRC not ok
458 * 1 : CRC-command, CRC ok
459 * 2 : Not crc-command
460 */
461
462 uint8_t iso14443B_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
463 {
464 uint8_t b1,b2;
465
466 if(len <= 2) return 2;
467
468 ComputeCrc14443(CRC_14443_B, data, len-2, &b1, &b2);
469 if(b1 != data[len-2] || b2 != data[len-1]) {
470 return 0;
471 }
472 return 1;
473 }
474
475 /**
476 * @brief iclass_CRC_Ok Checks CRC in command or response
477 * @param isResponse
478 * @param data
479 * @param len
480 * @return 0 : CRC-command, CRC not ok
481 * 1 : CRC-command, CRC ok
482 * 2 : Not crc-command
483 */
484 uint8_t iclass_CRC_check(bool isResponse, uint8_t* data, uint8_t len)
485 {
486 if(len < 4) return 2;//CRC commands (and responses) are all at least 4 bytes
487
488 uint8_t b1, b2;
489
490 if(!isResponse)//Commands to tag
491 {
492 /**
493 These commands should have CRC. Total length leftmost
494 4 READ
495 4 READ4
496 12 UPDATE - unsecured, ends with CRC16
497 14 UPDATE - secured, ends with signature instead
498 4 PAGESEL
499 **/
500 if(len == 4 || len == 12)//Covers three of them
501 {
502 //Don't include the command byte
503 ComputeCrc14443(CRC_ICLASS, (data+1), len-3, &b1, &b2);
504 return b1 == data[len -2] && b2 == data[len-1];
505 }
506 return 2;
507 }else{
508 /**
509 These tag responses should have CRC. Total length leftmost
510
511 10 READ data[8] crc[2]
512 34 READ4 data[32]crc[2]
513 10 UPDATE data[8] crc[2]
514 10 SELECT csn[8] crc[2]
515 10 IDENTIFY asnb[8] crc[2]
516 10 PAGESEL block1[8] crc[2]
517 10 DETECT csn[8] crc[2]
518
519 These should not
520
521 4 CHECK chip_response[4]
522 8 READCHECK data[8]
523 1 ACTALL sof[1]
524 1 ACT sof[1]
525
526 In conclusion, without looking at the command; any response
527 of length 10 or 34 should have CRC
528 **/
529 if(len != 10 && len != 34) return true;
530
531 ComputeCrc14443(CRC_ICLASS, data, len-2, &b1, &b2);
532 return b1 == data[len -2] && b2 == data[len-1];
533 }
534 }
535
536 bool is_last_record(uint16_t tracepos, uint8_t *trace, uint16_t traceLen)
537 {
538 return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen);
539 }
540
541
542 bool next_record_is_response(uint16_t tracepos, uint8_t *trace)
543 {
544 uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t)));
545
546 return(next_records_datalen & 0x8000);
547 }
548
549
550 bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len)
551 {
552
553 #define MAX_TOPAZ_READER_CMD_LEN 16
554
555 uint32_t last_timestamp = timestamp + *duration;
556
557 if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false;
558
559 memcpy(topaz_reader_command, frame, *data_len);
560
561 while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) {
562 uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos));
563 *tracepos += sizeof(uint32_t);
564 uint16_t next_duration = *((uint16_t *)(trace + *tracepos));
565 *tracepos += sizeof(uint16_t);
566 uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF;
567 *tracepos += sizeof(uint16_t);
568 uint8_t *next_frame = (trace + *tracepos);
569 *tracepos += next_data_len;
570 if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) {
571 memcpy(topaz_reader_command + *data_len, next_frame, next_data_len);
572 *data_len += next_data_len;
573 last_timestamp = next_timestamp + next_duration;
574 } else {
575 // rewind and exit
576 *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
577 break;
578 }
579 uint16_t next_parity_len = (next_data_len-1)/8 + 1;
580 *tracepos += next_parity_len;
581 }
582
583 *duration = last_timestamp - timestamp;
584
585 return true;
586 }
587
588
589 uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, uint8_t protocol, bool showWaitCycles, bool markCRCBytes)
590 {
591 bool isResponse;
592 uint16_t data_len, parity_len;
593 uint32_t duration;
594 uint8_t topaz_reader_command[9];
595 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
596 char explanation[30] = {0};
597
598 if (tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) > traceLen) return traceLen;
599
600 first_timestamp = *((uint32_t *)(trace));
601 timestamp = *((uint32_t *)(trace + tracepos));
602
603 tracepos += 4;
604 duration = *((uint16_t *)(trace + tracepos));
605 tracepos += 2;
606 data_len = *((uint16_t *)(trace + tracepos));
607 tracepos += 2;
608
609 if (data_len & 0x8000) {
610 data_len &= 0x7fff;
611 isResponse = true;
612 } else {
613 isResponse = false;
614 }
615 parity_len = (data_len-1)/8 + 1;
616
617 if (tracepos + data_len + parity_len > traceLen) {
618 return traceLen;
619 }
620 uint8_t *frame = trace + tracepos;
621 tracepos += data_len;
622 uint8_t *parityBytes = trace + tracepos;
623 tracepos += parity_len;
624
625 if (protocol == TOPAZ && !isResponse) {
626 // topaz reader commands come in 1 or 9 separate frames with 7 or 8 Bits each.
627 // merge them:
628 if (merge_topaz_reader_frames(timestamp, &duration, &tracepos, traceLen, trace, frame, topaz_reader_command, &data_len)) {
629 frame = topaz_reader_command;
630 }
631 }
632
633 //Check the CRC status
634 uint8_t crcStatus = 2;
635
636 if (data_len > 2) {
637 switch (protocol) {
638 case ICLASS:
639 crcStatus = iclass_CRC_check(isResponse, frame, data_len);
640 break;
641 case ISO_14443B:
642 case TOPAZ:
643 crcStatus = iso14443B_CRC_check(isResponse, frame, data_len);
644 break;
645 case ISO_14443A:
646 case MFDES:
647 crcStatus = iso14443A_CRC_check(isResponse, frame, data_len);
648 break;
649 default:
650 break;
651 }
652 }
653 //0 CRC-command, CRC not ok
654 //1 CRC-command, CRC ok
655 //2 Not crc-command
656
657 //--- Draw the data column
658 char line[16][110];
659
660 for (int j = 0; j < data_len && j/16 < 16; j++) {
661
662 int oddparity = 0x01;
663 int k;
664
665 for (k=0 ; k<8 ; k++) {
666 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
667 }
668 uint8_t parityBits = parityBytes[j>>3];
669 if (protocol != LEGIC && protocol != ISO_14443B && protocol != ISO_7816_4 && (isResponse || protocol == ISO_14443A) && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
670 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x! ", frame[j]);
671 } else {
672 snprintf(line[j/16]+(( j % 16) * 4),110, "%02x ", frame[j]);
673 }
674
675 }
676
677 if (markCRCBytes) {
678 //CRC-command
679 if(crcStatus == 0 || crcStatus == 1) {
680 char *pos1 = line[(data_len-2)/16]+(((data_len-2) % 16) * 4);
681 (*pos1) = '[';
682 char *pos2 = line[(data_len)/16]+(((data_len) % 16) * 4);
683 sprintf(pos2, "%c", ']');
684 }
685 }
686
687 if (data_len == 0 ) {
688 sprintf(line[0],"<empty trace - possible error>");
689 return tracepos;
690 }
691
692 // Draw the CRC column
693 char *crc = (crcStatus == 0 ? "!crc" : (crcStatus == 1 ? " ok " : " "));
694
695 EndOfTransmissionTimestamp = timestamp + duration;
696
697 // Always annotate LEGIC read/tag
698 if ( protocol == LEGIC )
699 annotateLegic(explanation,sizeof(explanation),frame,data_len);
700
701 if (!isResponse) {
702 switch(protocol) {
703 case ICLASS: annotateIclass(explanation,sizeof(explanation),frame,data_len); break;
704 case ISO_14443A: annotateIso14443a(explanation,sizeof(explanation),frame,data_len); break;
705 case MFDES: annotateMfDesfire(explanation,sizeof(explanation),frame,data_len); break;
706 case ISO_14443B: annotateIso14443b(explanation,sizeof(explanation),frame,data_len); break;
707 case TOPAZ: annotateTopaz(explanation,sizeof(explanation),frame,data_len); break;
708 case ISO_7816_4: annotateIso7816(explanation,sizeof(explanation),frame,data_len); break;
709
710 default: break;
711 }
712 }
713
714 int num_lines = MIN((data_len - 1)/16 + 1, 16);
715 for (int j = 0; j < num_lines ; j++) {
716 if (j == 0) {
717 PrintAndLog(" %10u | %10u | %s |%-64s | %s| %s",
718 (timestamp - first_timestamp),
719 (EndOfTransmissionTimestamp - first_timestamp),
720 (isResponse ? "Tag" : "Rdr"),
721 line[j],
722 (j == num_lines-1) ? crc : " ",
723 (j == num_lines-1) ? explanation : "");
724 } else {
725 PrintAndLog(" | | |%-64s | %s| %s",
726 line[j],
727 (j == num_lines-1) ? crc : " ",
728 (j == num_lines-1) ? explanation : "");
729 }
730 }
731
732 if (is_last_record(tracepos, trace, traceLen)) return traceLen;
733
734 if (showWaitCycles && !isResponse && next_record_is_response(tracepos, trace)) {
735 uint32_t next_timestamp = *((uint32_t *)(trace + tracepos));
736 PrintAndLog(" %10u | %10u | %s |fdt (Frame Delay Time): %d",
737 (EndOfTransmissionTimestamp - first_timestamp),
738 (next_timestamp - first_timestamp),
739 " ",
740 (next_timestamp - EndOfTransmissionTimestamp));
741 }
742
743 return tracepos;
744 }
745
746 int usage_hf_list(){
747 PrintAndLog("List protocol data in trace buffer.");
748 PrintAndLog("Usage: hf list <protocol> [f][c]");
749 PrintAndLog(" f - show frame delay times as well");
750 PrintAndLog(" c - mark CRC bytes");
751 PrintAndLog("Supported <protocol> values:");
752 PrintAndLog(" raw - just show raw data without annotations");
753 PrintAndLog(" 14a - interpret data as iso14443a communications");
754 PrintAndLog(" 14b - interpret data as iso14443b communications");
755 PrintAndLog(" des - interpret data as DESFire communications");
756 PrintAndLog(" iclass - interpret data as iclass communications");
757 PrintAndLog(" topaz - interpret data as topaz communications");
758 PrintAndLog(" 7816 - interpret data as iso7816-4 communications");
759 PrintAndLog(" legic - interpret data as LEGIC communications");
760 PrintAndLog("");
761 PrintAndLog("example: hf list 14a f");
762 PrintAndLog(" hf list iclass");
763 return 0;
764 }
765 int usage_hf_search(){
766 PrintAndLog("Usage: hf search");
767 PrintAndLog("Will try to find a HF read out of the unknown tag. Stops when found.");
768 PrintAndLog("Options:");
769 PrintAndLog(" h - This help");
770 PrintAndLog("");
771 return 0;
772 }
773 int usage_hf_snoop(){
774 PrintAndLog("Usage: hf snoop <skip pairs> <skip triggers>");
775 PrintAndLog("The high frequence snoop will assign all available memory on device for snooped data");
776 PrintAndLog("User the 'data samples' command to download from device, and 'data plot' to look at it");
777 PrintAndLog("Press button to quit the snooping.");
778 PrintAndLog("Options:");
779 PrintAndLog(" h - This help");
780 PrintAndLog(" <skip pairs> - skip sample pairs");
781 PrintAndLog(" <skip triggers> - skip number of triggers");
782 PrintAndLog("");
783 PrintAndLog("example: hf snoop");
784 PrintAndLog(" hf snoop 1000 0");
785 return 0;
786 }
787
788 int CmdHFList(const char *Cmd) {
789 clearCommandBuffer();
790
791 bool showWaitCycles = false;
792 bool markCRCBytes = false;
793 char type[10] = {0};
794 //int tlen = param_getstr(Cmd,0,type);
795 char param1 = param_getchar(Cmd, 1);
796 char param2 = param_getchar(Cmd, 2);
797 bool errors = false;
798 uint8_t protocol = 0;
799
800 //Validate params H or empty
801 if (strlen(Cmd) < 1 || param1 == 'h' || param1 == 'H') return usage_hf_list();
802
803 //Validate params F,C
804 if(
805 (param1 != 0 && param1 != 'f' && param1 != 'c') ||
806 (param2 != 0 && param2 != 'f' && param2 != 'c')
807 ) {
808 return usage_hf_list();
809 }
810
811 param_getstr(Cmd,0,type);
812
813 // validate type of output
814 if(strcmp(type, "iclass") == 0) protocol = ICLASS;
815 else if(strcmp(type, "14a") == 0) protocol = ISO_14443A;
816 else if(strcmp(type, "14b") == 0) protocol = ISO_14443B;
817 else if(strcmp(type, "topaz")== 0) protocol = TOPAZ;
818 else if(strcmp(type, "7816")== 0) protocol = ISO_7816_4;
819 else if(strcmp(type,"des")== 0) protocol = MFDES;
820 else if(strcmp(type,"legic")==0) protocol = LEGIC;
821 else if(strcmp(type, "raw")== 0) protocol = -1;//No crc, no annotations
822 else errors = true;
823
824 if (errors) return usage_hf_list();
825
826 if (param1 == 'f' || param2 == 'f') showWaitCycles = true;
827 if (param1 == 'c' || param2 == 'c') markCRCBytes = true;
828
829 uint8_t *trace;
830 uint16_t tracepos = 0;
831 trace = malloc(USB_CMD_DATA_SIZE);
832
833 // Query for the size of the trace
834 UsbCommand response;
835 GetFromBigBuf(trace, USB_CMD_DATA_SIZE, 0);
836 WaitForResponse(CMD_ACK, &response);
837 uint16_t traceLen = response.arg[2];
838 if (traceLen > USB_CMD_DATA_SIZE) {
839 uint8_t *p = realloc(trace, traceLen);
840 if (p == NULL) {
841 PrintAndLog("Cannot allocate memory for trace");
842 free(trace);
843 return 2;
844 }
845 trace = p;
846 GetFromBigBuf(trace, traceLen, 0);
847 WaitForResponse(CMD_ACK, NULL);
848 }
849
850 PrintAndLog("Recorded Activity (TraceLen = %d bytes)", traceLen);
851 PrintAndLog("");
852 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
853 if ( protocol == ISO_14443A )
854 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
855 if ( protocol == ICLASS )
856 PrintAndLog("iClass - Timings are not as accurate");
857 if ( protocol == LEGIC )
858 PrintAndLog("LEGIC - Timings are in ticks (1us == 1.5ticks)");
859 PrintAndLog("");
860 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
861 PrintAndLog("------------|------------|-----|-----------------------------------------------------------------|-----|--------------------|");
862
863 while(tracepos < traceLen) {
864 tracepos = printTraceLine(tracepos, traceLen, trace, protocol, showWaitCycles, markCRCBytes);
865 }
866
867 free(trace);
868 return 0;
869 }
870
871 int CmdHFSearch(const char *Cmd){
872
873 char cmdp = param_getchar(Cmd, 0);
874 if (cmdp == 'h' || cmdp == 'H') return usage_hf_search();
875
876 PrintAndLog("");
877 int ans = CmdHF14AReader("s");
878 if (ans > 0) {
879 PrintAndLog("\nValid ISO14443-A Tag Found - Quiting Search\n");
880 return ans;
881 }
882 ans = HF14BReader(false); //CmdHF14BReader("s");
883 if (ans) {
884 PrintAndLog("\nValid ISO14443-B Tag Found - Quiting Search\n");
885 return ans;
886 }
887 ans = HF15Reader("", false);
888 if (ans) {
889 PrintAndLog("\nValid ISO15693 Tag Found - Quiting Search\n");
890 return ans;
891 }
892 ans = HFLegicReader("", false);
893 if ( ans == 0) {
894 PrintAndLog("\nValid LEGIC Tag Found - Quiting Search\n");
895 return 1;
896 }
897 ans = CmdHFTopazReader("s");
898 if (ans == 0) {
899 PrintAndLog("\nValid Topaz Tag Found - Quiting Search\n");
900 return 1;
901 }
902 ans = HFiClassReader("", false, false);
903 if (ans) {
904 PrintAndLog("\nValid iClass Tag (or PicoPass Tag) Found - Quiting Search\n");
905 return ans;
906 }
907
908 PrintAndLog("\nno known/supported 13.56 MHz tags found\n");
909 return 0;
910 }
911
912 int CmdHFSnoop(const char *Cmd) {
913 char cmdp = param_getchar(Cmd, 0);
914 if (cmdp == 'h' || cmdp == 'H') return usage_hf_snoop();
915
916 int skippairs = param_get32ex(Cmd, 0, 0, 10);
917 int skiptriggers = param_get32ex(Cmd, 1, 0, 10);
918
919 UsbCommand c = {CMD_HF_SNIFFER, {skippairs, skiptriggers, 0}};
920 clearCommandBuffer();
921 SendCommand(&c);
922 return 0;
923 }
924
925 static command_t CommandTable[] = {
926 {"help", CmdHelp, 1, "This help"},
927 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
928 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
929 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
930 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
931 #ifdef WITH_EMV
932 {"emv", CmdHFEmv, 1, "{ EMV RFIDs... }"},
933 #endif
934 {"legic", CmdHFLegic, 1, "{ LEGIC RFIDs... }"},
935 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
936 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
937 {"mfu", CmdHFMFUltra, 1, "{ MIFARE Ultralight RFIDs... }"},
938 {"mfdes", CmdHFMFDes, 1, "{ MIFARE Desfire RFIDs... }"},
939 {"topaz", CmdHFTopaz, 1, "{ TOPAZ (NFC Type 1) RFIDs... }"},
940 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
941 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
942 {"search", CmdHFSearch, 1, "Search for known HF tags [preliminary]"},
943 {"snoop", CmdHFSnoop, 0, "<samples to skip (10000)> <triggers to skip (1)> Generic LF/HF Snoop in Testing stage"},
944 {NULL, NULL, 0, NULL}
945 };
946
947 int CmdHF(const char *Cmd) {
948 clearCommandBuffer();
949 CmdsParse(CommandTable, Cmd);
950 return 0;
951 }
952
953 int CmdHelp(const char *Cmd) {
954 CmdsHelp(CommandTable);
955 return 0;
956 }
Impressum, Datenschutz