2009-05-01 17:47:44 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Public platform independent Near Field Communication (NFC) library
|
|
|
|
Copyright (C) 2009, Roel Verdult
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2009-06-26 11:05:25 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
2009-05-01 17:47:44 +02:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2009-06-26 11:05:25 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
2009-05-01 17:47:44 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-05-27 14:18:21 +02:00
|
|
|
#include <stddef.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
#include <stdint.h>
|
2009-05-01 17:47:44 +02:00
|
|
|
#include <string.h>
|
2009-08-23 11:50:46 +02:00
|
|
|
#include <getopt.h>
|
2009-05-27 12:13:19 +02:00
|
|
|
|
2009-05-01 17:47:44 +02:00
|
|
|
#include "libnfc.h"
|
|
|
|
|
2009-07-16 14:09:06 +02:00
|
|
|
static byte_t abtRecv[MAX_FRAME_LEN];
|
2009-05-27 12:13:19 +02:00
|
|
|
static uint32_t uiRecvBits;
|
2009-05-01 17:47:44 +02:00
|
|
|
static dev_info* pdi;
|
|
|
|
|
|
|
|
// ISO14443A Anti-Collision response
|
2009-07-16 14:09:06 +02:00
|
|
|
byte_t abtAtqa [2] = { 0x04,0x00 };
|
|
|
|
byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 };
|
|
|
|
byte_t abtSak [9] = { 0x08,0xb6,0xdd };
|
2009-05-01 17:47:44 +02:00
|
|
|
|
2009-08-23 11:50:46 +02:00
|
|
|
int main(int argc, char *argv[])
|
2009-05-01 17:47:44 +02:00
|
|
|
{
|
2009-07-16 14:09:06 +02:00
|
|
|
byte_t* pbtTx = NULL;
|
2009-05-27 12:13:19 +02:00
|
|
|
uint32_t uiTxBits;
|
2009-08-26 12:57:38 +02:00
|
|
|
int i;
|
|
|
|
bool quiet_output = false;
|
2009-08-23 11:50:46 +02:00
|
|
|
|
|
|
|
// Get commandline options
|
|
|
|
while ((i= getopt(argc, argv, "hq")) != -1)
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 'q':
|
2009-08-26 12:57:38 +02:00
|
|
|
quiet_output = true;
|
2009-08-23 11:50:46 +02:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
printf("\n\tusage:\n");
|
|
|
|
printf("\t\tnfc-emulate [OPTIONS] [UID]\n\n");
|
|
|
|
printf("\toptions:\n");
|
|
|
|
printf("\t\t-h\tHelp. Print this message.\n");
|
|
|
|
printf("\t\t-q\tQuiet mode. Suppress output of READER and EMULATOR data (improves timing).\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("\targs:\n");
|
|
|
|
printf("\t\t[UID]\tThe UID to emulate, specified as 8 HEX digits. Default is DEADBEAF.\n");
|
|
|
|
printf("\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-08-21 15:34:53 +02:00
|
|
|
|
|
|
|
// See if UID was specified as HEX string
|
2009-08-23 11:50:46 +02:00
|
|
|
if(argc > 1 && strlen(argv[optind]) == 8)
|
2009-08-21 15:34:53 +02:00
|
|
|
{
|
2009-08-26 12:57:38 +02:00
|
|
|
byte_t abtTmp[3] = { 0x00,0x00,0x00 };
|
|
|
|
|
2009-08-23 11:50:46 +02:00
|
|
|
printf("[+] Using UID: %s\n",argv[optind]);
|
2009-08-21 15:34:53 +02:00
|
|
|
abtUidBcc[4]= 0x00;
|
|
|
|
for(i= 0; i < 4; ++i)
|
|
|
|
{
|
2009-08-26 12:57:38 +02:00
|
|
|
memcpy(abtTmp,argv[optind]+i*2,2);
|
|
|
|
abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16);
|
2009-08-21 15:34:53 +02:00
|
|
|
abtUidBcc[4] ^= abtUidBcc[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-01 17:47:44 +02:00
|
|
|
// Try to open the NFC reader
|
2009-09-04 15:24:34 +02:00
|
|
|
pdi = nfc_connect(NULL);
|
2009-05-01 17:47:44 +02:00
|
|
|
|
|
|
|
if (pdi == INVALID_DEVICE_INFO)
|
|
|
|
{
|
2009-08-21 15:34:53 +02:00
|
|
|
printf("Error connecting NFC reader\n");
|
2009-05-01 17:47:44 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
printf("[+] Connected to NFC reader: %s\n",pdi->acName);
|
|
|
|
printf("[+] Try to break out the auto-emulation, this requires a second reader!\n");
|
|
|
|
printf("[+] To do this, please send any command after the anti-collision\n");
|
2009-08-26 12:57:38 +02:00
|
|
|
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
|
2009-05-01 17:47:44 +02:00
|
|
|
if (!nfc_target_init(pdi,abtRecv,&uiRecvBits))
|
|
|
|
{
|
|
|
|
printf("Error: Could not come out of auto-emulation, no command was received\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("[+] Received initiator command: ");
|
|
|
|
print_hex_bits(abtRecv,uiRecvBits);
|
|
|
|
printf("[+] Configuring communication\n");
|
|
|
|
nfc_configure(pdi,DCO_HANDLE_CRC,false);
|
|
|
|
nfc_configure(pdi,DCO_HANDLE_PARITY,true);
|
2009-08-21 15:34:53 +02:00
|
|
|
printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n",abtUidBcc[0],abtUidBcc[1],abtUidBcc[2],abtUidBcc[3]);
|
2009-05-01 17:47:44 +02:00
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
// Test if we received a frame
|
2009-05-27 14:18:21 +02:00
|
|
|
if (nfc_target_receive_bits(pdi,abtRecv,&uiRecvBits,NULL))
|
2009-05-01 17:47:44 +02:00
|
|
|
{
|
|
|
|
// Prepare the command to send back for the anti-collision request
|
|
|
|
switch(uiRecvBits)
|
|
|
|
{
|
|
|
|
case 7: // Request or Wakeup
|
|
|
|
pbtTx = abtAtqa;
|
|
|
|
uiTxBits = 16;
|
|
|
|
// New anti-collsion session started
|
2009-08-26 12:57:38 +02:00
|
|
|
if (!quiet_output) printf("\n");
|
2009-05-01 17:47:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 16: // Select All
|
|
|
|
pbtTx = abtUidBcc;
|
|
|
|
uiTxBits = 40;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 72: // Select Tag
|
|
|
|
pbtTx = abtSak;
|
|
|
|
uiTxBits = 24;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // unknown length?
|
|
|
|
uiTxBits = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("R: ");
|
|
|
|
print_hex_bits(abtRecv,uiRecvBits);
|
|
|
|
}
|
2009-05-01 17:47:44 +02:00
|
|
|
|
|
|
|
// Test if we know how to respond
|
|
|
|
if(uiTxBits)
|
|
|
|
{
|
|
|
|
// Send and print the command to the screen
|
2009-05-27 14:18:21 +02:00
|
|
|
nfc_target_send_bits(pdi,pbtTx,uiTxBits,NULL);
|
2009-08-26 12:57:38 +02:00
|
|
|
if(!quiet_output)
|
2009-08-23 11:50:46 +02:00
|
|
|
{
|
|
|
|
printf("T: ");
|
|
|
|
print_hex_bits(pbtTx,uiTxBits);
|
|
|
|
}
|
2009-05-01 17:47:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nfc_disconnect(pdi);
|
|
|
|
}
|
|
|
|
|