X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/ecbf82fe20a39cfdd323e40caa30c6699f511939..5d643cc03337448602b9ea7aee4d9c4581ca9434:/client/cmdlfhid.c diff --git a/client/cmdlfhid.c b/client/cmdlfhid.c index 68e9b07f..66dfae8b 100644 --- a/client/cmdlfhid.c +++ b/client/cmdlfhid.c @@ -5,19 +5,53 @@ // at your option, any later version. See the LICENSE.txt file for the text of // the license. //----------------------------------------------------------------------------- -// Low frequency HID commands +// Low frequency HID commands (known) +// +// Useful resources: +// RF interface, programming a T55x7 clone, 26-bit HID H10301 encoding: +// http://www.proxmark.org/files/Documents/125%20kHz%20-%20HID/HID_format_example.pdf +// +// "Understanding Card Data Formats" +// https://www.hidglobal.com/sites/default/files/hid-understanding_card_data_formats-wp-en.pdf +// +// "What Format Do You Need?" +// https://www.hidglobal.com/sites/default/files/resource_files/hid-prox-br-en.pdf //----------------------------------------------------------------------------- +#include "cmdlfhid.h" + #include #include -#include "proxmark3.h" +#include "comms.h" #include "ui.h" #include "graph.h" #include "cmdparser.h" #include "cmddata.h" //for g_debugMode, demodbuff cmds #include "lfdemod.h" // for HIDdemodFSK +#include "hidcardformats.h" +#include "hidcardformatutils.h" +#include "util.h" // for param_get8,32,64 + + +/** + * Converts a hex string to component "hi2", "hi" and "lo" 32-bit integers, one nibble + * at a time. + * + * Returns the number of nibbles (4 bits) entered. + */ +int hexstring_to_int96(/* out */ uint32_t* hi2,/* out */ uint32_t* hi, /* out */ uint32_t* lo, const char* str) { + // TODO: Replace this with param_gethex when it supports arbitrary length + // inputs. + int n = 0, i = 0; + + while (sscanf(&str[i++], "%1x", &n ) == 1) { + *hi2 = (*hi2 << 4) | (*hi >> 28); + *hi = (*hi << 4) | (*lo >> 28); + *lo = (*lo << 4) | (n & 0xf); + } -static int CmdHelp(const char *Cmd); + return i - 1; +} //by marshmellow (based on existing demod + holiman's refactor) //HID Prox demod - FSK RF/50 with preamble of 00011101 (then manchester encoded) @@ -31,7 +65,8 @@ int CmdFSKdemodHID(const char *Cmd) size_t BitLen = getFromGraphBuf(BitStream); if (BitLen==0) return 0; //get binary from fsk wave - int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo); + int waveIdx = 0; + int idx = HIDdemodFSK(BitStream,&BitLen,&hi2,&hi,&lo, &waveIdx); if (idx<0){ if (g_debugMode){ if (idx==-1){ @@ -52,52 +87,24 @@ int CmdFSKdemodHID(const char *Cmd) if (g_debugMode) PrintAndLog("DEBUG: Error - no values found"); return 0; } - if (hi2 != 0){ //extra large HID tags - PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d)", - (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF); - } - else { //standard HID tags <38 bits - uint8_t fmtLen = 0; - uint32_t fc = 0; - uint32_t cardnum = 0; - if (((hi>>5)&1)==1){//if bit 38 is set then < 37 bit format is used - uint32_t lo2=0; - lo2=(((hi & 31) << 12) | (lo>>20)); //get bits 21-37 to check for format len bit - uint8_t idx3 = 1; - while(lo2>1){ //find last bit set to 1 (format len bit) - lo2=lo2>>1; - idx3++; - } - fmtLen =idx3+19; - fc =0; - cardnum=0; - if(fmtLen==26){ - cardnum = (lo>>1)&0xFFFF; - fc = (lo>>17)&0xFF; - } - if(fmtLen==34){ - cardnum = (lo>>1)&0xFFFF; - fc= ((hi&1)<<15)|(lo>>17); - } - if(fmtLen==35){ - cardnum = (lo>>1)&0xFFFFF; - fc = ((hi&1)<<11)|(lo>>21); - } - } - else { //if bit 38 is not set then 37 bit format is used - fmtLen = 37; - fc = 0; - cardnum = 0; - if(fmtLen == 37){ - cardnum = (lo>>1)&0x7FFFF; - fc = ((hi&0xF)<<12)|(lo>>20); - } - } - PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %dbit - FC: %d - Card: %d", - (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF, - (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum); + + if (hi2 != 0) + PrintAndLog("HID Prox TAG ID: %x%08x%08x", + (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo + ); + else + PrintAndLog("HID Prox TAG ID: %x%08x", + (unsigned int) hi, (unsigned int) lo + ); + + hidproxmessage_t packed = initialize_proxmessage_object(hi2, hi, lo); + bool ret = HIDTryUnpack(&packed, false); + + if (!ret) { + PrintAndLog("Invalid or unsupported tag length."); } setDemodBuf(BitStream,BitLen,idx); + setClockGrid(50, waveIdx + (idx*50)); if (g_debugMode){ PrintAndLog("DEBUG: idx: %d, Len: %d, Printing Demod Buffer:", idx, BitLen); printDemodBuff(); @@ -108,7 +115,7 @@ int CmdFSKdemodHID(const char *Cmd) int CmdHIDReadFSK(const char *Cmd) { int findone=0; - if(Cmd[0]=='1') findone=1; + if(Cmd[0]=='1') findone=1; UsbCommand c={CMD_HID_DEMOD_FSK}; c.arg[0]=findone; SendCommand(&c); @@ -117,15 +124,14 @@ int CmdHIDReadFSK(const char *Cmd) int CmdHIDSim(const char *Cmd) { - unsigned int hi = 0, lo = 0; - int n = 0, i = 0; - - while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { - hi = (hi << 4) | (lo >> 28); - lo = (lo << 4) | (n & 0xf); + uint32_t hi2 = 0, hi = 0, lo = 0; + hexstring_to_int96(&hi2, &hi, &lo, Cmd); + if (hi >= 0x40 || hi2 != 0) { + PrintAndLog("This looks like a long tag ID. Use 'lf simfsk' for long tags. Aborting!"); + return 0; } - PrintAndLog("Emulating tag with ID %x%16x", hi, lo); + PrintAndLog("Emulating tag with ID %x%08x", hi, lo); PrintAndLog("Press pm3-button to abort simulation"); UsbCommand c = {CMD_HID_SIM_TAG, {hi, lo, 0}}; @@ -136,34 +142,19 @@ int CmdHIDSim(const char *Cmd) int CmdHIDClone(const char *Cmd) { unsigned int hi2 = 0, hi = 0, lo = 0; - int n = 0, i = 0; UsbCommand c; - - if (strchr(Cmd,'l') != 0) { - while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { - hi2 = (hi2 << 4) | (hi >> 28); - hi = (hi << 4) | (lo >> 28); - lo = (lo << 4) | (n & 0xf); - } - + hexstring_to_int96(&hi2, &hi, &lo, Cmd); + + if (hi >= 0x40 || hi2 != 0) { PrintAndLog("Cloning tag with long ID %x%08x%08x", hi2, hi, lo); - c.d.asBytes[0] = 1; - } - else { - while (sscanf(&Cmd[i++], "%1x", &n ) == 1) { - hi = (hi << 4) | (lo >> 28); - lo = (lo << 4) | (n & 0xf); - } - + } else { PrintAndLog("Cloning tag with ID %x%08x", hi, lo); - - hi2 = 0; c.d.asBytes[0] = 0; } c.cmd = CMD_HID_CLONE_TAG; - c.arg[0] = hi2; + c.arg[0] = (hi2 & 0x000FFFFF); c.arg[1] = hi; c.arg[2] = lo; @@ -171,13 +162,136 @@ int CmdHIDClone(const char *Cmd) return 0; } +int CmdHIDDecode(const char *Cmd){ + if (strlen(Cmd)<3) { + PrintAndLog("Usage: lf hid decode {p}"); + PrintAndLog(" (optional) p: Ignore invalid parity"); + PrintAndLog(" sample: lf hid decode 2006f623ae"); + return 0; + } + + uint32_t top = 0, mid = 0, bot = 0; + bool ignoreParity = false; + hexstring_to_int96(&top, &mid, &bot, Cmd); + hidproxmessage_t packed = initialize_proxmessage_object(top, mid, bot); + + char opt = param_getchar(Cmd, 1); + if (opt == 'p') ignoreParity = true; + + HIDTryUnpack(&packed, ignoreParity); + return 0; +} +int CmdHIDEncode(const char *Cmd) { + if (strlen(Cmd) == 0) { + PrintAndLog("Usage: lf hid encode [issue level (decimal)]"); + PrintAndLog(" sample: lf hid encode H10301 123 4567"); + return 0; + } + + int formatIndex = -1; + if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){ + HIDListFormats(); + return 0; + } else { + char format[16]; + memset(format, 0, sizeof(format)); + param_getstr(Cmd, 0, format, sizeof(format)); + formatIndex = HIDFindCardFormat(format); + if (formatIndex == -1) { + HIDListFormats(); + return 0; + } + } + + hidproxcard_t card; + memset(&card, 0, sizeof(hidproxcard_t)); + card.FacilityCode = param_get32ex(Cmd, 1, 0, 10); + card.CardNumber = param_get64ex(Cmd, 2, 0, 10); + card.IssueLevel = param_get32ex(Cmd, 3, 0, 10); + card.ParitySupported = true; // Try to encode parity if supported. + + hidproxmessage_t packed; + memset(&packed, 0, sizeof(hidproxmessage_t)); + if (HIDPack(formatIndex, &card, &packed)){ + if (packed.top != 0) { + PrintAndLog("HID Prox TAG ID: %x%08x%08x", + (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot); + } else { + PrintAndLog("HID Prox TAG ID: %x%08x", + (unsigned int)packed.mid, (unsigned int)packed.bot); + } + } else { + PrintAndLog("The provided data could not be encoded with the selected format."); + } + return 0; +} + +int CmdHIDWrite(const char *Cmd) { + if (strlen(Cmd) == 0) { + PrintAndLog("Usage: lf hid write [issue level (decimal)]"); + PrintAndLog(" sample: lf hid write H10301 123 4567"); + return 0; + } + + int formatIndex = -1; + if (!strcmp(Cmd, "help") || !strcmp(Cmd, "h") || !strcmp(Cmd, "list") || !strcmp(Cmd, "?")){ + HIDListFormats(); + return 0; + } else { + char format[16]; + memset(format, 0, sizeof(format)); + param_getstr(Cmd, 0, format, sizeof(format)); + formatIndex = HIDFindCardFormat(format); + if (formatIndex == -1) { + HIDListFormats(); + return 0; + } + } + + hidproxcard_t card; + memset(&card, 0, sizeof(hidproxcard_t)); + card.FacilityCode = param_get32ex(Cmd, 1, 0, 10); + card.CardNumber = param_get64ex(Cmd, 2, 0, 10); + card.IssueLevel = param_get32ex(Cmd, 3, 0, 10); + card.ParitySupported = true; // Try to encode parity if supported. + + hidproxmessage_t packed; + memset(&packed, 0, sizeof(hidproxmessage_t)); + if (HIDPack(formatIndex, &card, &packed)){ + UsbCommand c; + if (packed.top != 0) { + PrintAndLog("HID Prox TAG ID: %x%08x%08x", + (unsigned int)packed.top, (unsigned int)packed.mid, (unsigned int)packed.bot); + c.d.asBytes[0] = 1; + } else { + PrintAndLog("HID Prox TAG ID: %x%08x", + (unsigned int)packed.mid, (unsigned int)packed.bot); + c.d.asBytes[0] = 0; + } + + c.cmd = CMD_HID_CLONE_TAG; + c.arg[0] = (packed.top & 0x000FFFFF); + c.arg[1] = packed.mid; + c.arg[2] = packed.bot; + SendCommand(&c); + + } else { + PrintAndLog("The provided data could not be encoded with the selected format."); + } + return 0; +} + +static int CmdHelp(const char *Cmd); // define this now so the below won't error out. static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, {"demod", CmdFSKdemodHID, 1, "Demodulate HID Prox from GraphBuffer"}, {"read", CmdHIDReadFSK, 0, "['1'] Realtime HID FSK Read from antenna (option '1' for one tag only)"}, {"sim", CmdHIDSim, 0, " -- HID tag simulator"}, - {"clone", CmdHIDClone, 0, " ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"}, + {"clone", CmdHIDClone, 0, " -- Clone HID to T55x7 (tag must be in antenna)"}, + {"decode", CmdHIDDecode, 1, " -- Try to decode an HID tag and show its contents"}, + {"encode", CmdHIDEncode, 1, " -- Encode an HID ID with the specified format, facility code and card number"}, + {"write", CmdHIDWrite, 0, " -- Encode and write to a T55x7 tag (tag must be in antenna)"}, {NULL, NULL, 0, NULL} };