Code cleanup for nfc-emulate, nfc-relay and nfc-anticol tools.

This commit is contained in:
Romuald Conty 2009-08-26 10:57:38 +00:00
parent 70cc04fe5f
commit 42d83fce56
4 changed files with 34 additions and 24 deletions

View file

@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <getopt.h>
#include <string.h>
@ -37,7 +38,7 @@ static byte_t abtUid[10];
static uint32_t uiUidLen = 4;
static dev_info* pdi;
int Quiet= 0;
bool quiet_output = false;
// ISO14443A Anti-Collision Commands
byte_t abtReqa [1] = { 0x26 };
@ -49,7 +50,7 @@ byte_t abtHalt [4] = { 0x50,0x00,0x57,0xcd };
bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits)
{
// Show transmitted command
if(!Quiet)
if(!quiet_output)
{
printf("R: ");
print_hex_bits(pbtTx,uiTxBits);
@ -59,7 +60,7 @@ bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits)
if (!nfc_initiator_transceive_bits(pdi,pbtTx,uiTxBits,NULL,abtRx,&uiRxBits,NULL)) return false;
// Show received answer
if(!Quiet)
if(!quiet_output)
{
printf("T: ");
print_hex_bits(abtRx,uiRxBits);
@ -73,7 +74,7 @@ bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits)
bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen)
{
// Show transmitted command
if(!Quiet)
if(!quiet_output)
{
printf("R: ");
print_hex(pbtTx,uiTxLen);
@ -83,7 +84,7 @@ bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen)
if (!nfc_initiator_transceive_bytes(pdi,pbtTx,uiTxLen,abtRx,&uiRxLen)) return false;
// Show received answer
if(!Quiet)
if(!quiet_output)
{
printf("T: ");
print_hex(abtRx,uiRxLen);
@ -102,7 +103,7 @@ int main(int argc,char* argv[])
switch (i)
{
case 'q':
Quiet= 1;
quiet_output = true;
break;
case 'h':
default:

View file

@ -35,20 +35,20 @@ static dev_info* pdi;
byte_t abtAtqa [2] = { 0x04,0x00 };
byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 };
byte_t abtSak [9] = { 0x08,0xb6,0xdd };
byte_t Tmp [3] = { 0x00,0x00,0x00 };
int main(int argc, char *argv[])
{
byte_t* pbtTx = NULL;
uint32_t uiTxBits;
int i, quiet= 0;
int i;
bool quiet_output = false;
// Get commandline options
while ((i= getopt(argc, argv, "hq")) != -1)
switch (i)
{
case 'q':
quiet= 1;
quiet_output = true;
break;
case 'h':
default:
@ -67,12 +67,14 @@ int main(int argc, char *argv[])
// See if UID was specified as HEX string
if(argc > 1 && strlen(argv[optind]) == 8)
{
byte_t abtTmp[3] = { 0x00,0x00,0x00 };
printf("[+] Using UID: %s\n",argv[optind]);
abtUidBcc[4]= 0x00;
for(i= 0; i < 4; ++i)
{
memcpy(Tmp,argv[optind]+i*2,2);
abtUidBcc[i]= (byte_t) strtol(Tmp,NULL,16);
memcpy(abtTmp,argv[optind]+i*2,2);
abtUidBcc[i]= (byte_t) strtol(abtTmp,NULL,16);
abtUidBcc[4] ^= abtUidBcc[i];
}
}
@ -90,7 +92,7 @@ int main(int argc, char *argv[])
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");
printf("[+] For example, send a RATS command or use the \"anticol\" tool\n");
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
if (!nfc_target_init(pdi,abtRecv,&uiRecvBits))
{
printf("Error: Could not come out of auto-emulation, no command was received\n");
@ -115,7 +117,7 @@ int main(int argc, char *argv[])
pbtTx = abtAtqa;
uiTxBits = 16;
// New anti-collsion session started
if (!quiet) printf("\n");
if (!quiet_output) printf("\n");
break;
case 16: // Select All
@ -133,7 +135,7 @@ int main(int argc, char *argv[])
break;
}
if(!quiet)
if(!quiet_output)
{
printf("R: ");
print_hex_bits(abtRecv,uiRecvBits);
@ -144,7 +146,7 @@ int main(int argc, char *argv[])
{
// Send and print the command to the screen
nfc_target_send_bits(pdi,pbtTx,uiTxBits,NULL);
if(!quiet)
if(!quiet_output)
{
printf("T: ");
print_hex_bits(pbtTx,uiTxBits);

View file

@ -2,19 +2,24 @@
.SH NAME
nfc-emulate \- NFC target emulation command line tool based on libnfc
.SH SYNOPSIS
.B nfc-emulate [8 digit HEX ID]
.B nfc-emulate
.RI [ UID ]
.SH DESCRIPTION
.B nfc-emulate
is an tag emulatation tool. Tag emulation is one of the main added features in NFC.
To avoid abuse of existing systems, manufacturers of the NFC controller intentionally did not
support emulation of custom UID numbers.
The emulate tool demonstrates that this can still be done using transmission of raw-frames,
and the desired UID can be optionally specified (default is DEADBEAF).
and the desired UID can be optionally specified.
Fast communication is necessary to respond in time during the anti-collision protocol.
Using the USB interface gives some timing issues but an embedded microprocessor could
be fast enough to emulate a tag with any UID. This makes it a serious thread
for security systems that rely only on the uniqueness of the UID.
.SH OPTIONS
.IR UID
8 hex digits format that represents desired UID (default is DEADBEAF).
.SH BUGS
Please report any bugs on the
.B libnfc

View file

@ -22,6 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <getopt.h>
#include "libnfc.h"
@ -36,14 +37,15 @@ static dev_info* pdiTag;
int main(int argc,char* argv[])
{
int quiet= 0, i;
int i;
bool quiet_output = false;
// Get commandline options
while ((i= getopt(argc, argv, "hq")) != -1)
switch (i)
{
case 'q':
quiet= 1;
quiet_output = true;
break;
case 'h':
default:
@ -67,9 +69,9 @@ int main(int argc,char* argv[])
printf("\n");
printf("[+] Connected to the NFC emulator device\n");
printf("[+] Try to break out the auto-simulation, this requires a second reader!\n");
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");
printf("[+] For example, send a RATS command or use the \"anticol\" tool\n");
printf("[+] For example, send a RATS command or use the \"nfc-anticol\" tool\n");
nfc_target_init(pdiTag,abtReaderRx,&uiReaderRxBits);
printf("[+] Configuring emulator settings\n");
nfc_configure(pdiTag,DCO_HANDLE_CRC,false);
@ -96,13 +98,13 @@ int main(int argc,char* argv[])
{
// Drop down field for a very short time (original tag will reboot)
nfc_configure(pdiReader,DCO_ACTIVATE_FIELD,false);
if(!quiet)
if(!quiet_output)
printf("\n");
nfc_configure(pdiReader,DCO_ACTIVATE_FIELD,true);
}
// Print the reader frame to the screen
if(!quiet)
if(!quiet_output)
{
printf("R: ");
print_hex_par(abtReaderRx,uiReaderRxBits,abtReaderRxPar);
@ -114,7 +116,7 @@ int main(int argc,char* argv[])
nfc_target_send_bits(pdiTag,abtTagRx,uiTagRxBits,abtTagRxPar);
// Print the tag frame to the screen
if(!quiet)
if(!quiet_output)
{
printf("T: ");
print_hex_par(abtTagRx,uiTagRxBits,abtTagRxPar);