]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - client/cmdlfhid.c
Adding native support for HID long-format
[proxmark3-svn] / client / cmdlfhid.c
index fd8e214821b9985ead1a4cb692b10243ff10af5c..1a9c3477fa5b95166c1d5b6048763564ad884e3e 100644 (file)
 // the license.
 //-----------------------------------------------------------------------------
 // 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 <stdio.h>
 #include <string.h>
-#include "cmdlfhid.h"
-#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 "parity.h" // for parity
+#include "util.h" // for param_get8,32,64
 
 static int CmdHelp(const char *Cmd);
 
+/**
+ * Packs an HID ID from component parts.
+ *
+ * This only works with 26, 34, 35, 37, and 48 bit card IDs.
+ *
+ * Returns false on invalid inputs.
+ */
+bool pack_hid(/* out */ uint32_t *hi2, /* out */ uint32_t *hi, /* out */ uint32_t *lo, /* in */ const hid_info *info) {
+  uint32_t higher = 0, high = 0, low = 0;
+
+  switch (info->fmtLen) {
+    case 26: // HID H10301
+      low |= (info->cardnum & 0xffff) << 1;
+      low |= (info->fc & 0xff) << 17;
+
+      if (info->parityValid) {
+        // Calculate parity
+        low |= oddparity32((low >> 1) & 0xfff) & 1;
+        low |= (evenparity32((low >> 13) & 0xfff) & 1) << 25;
+      }
+    break;
+
+  case 34: // H10306
+    low |= (info->cardnum & 0xffff) << 1;
+    low |= (info->fc & 0x7fff) << 17;
+    high |= (info->fc & 0x8000) >> 15;
+      
+    if (info->parityValid) {
+      // Calculate parity
+      high |= (evenparity32((high & 0x00000001) ^ (low & 0xFFFE0000)) & 1) << 1;
+      low |= (oddparity32(low & 0x0001FFFE) & 1);
+    }
+    break;
+
+  case 35: // (Corporate 1000 35-bit)
+    low |= (info->cardnum & 0xfffff) << 1;
+    low |= (info->fc & 0x7ff) << 21;
+    high |= (info->fc & 0x800) >> 11;
+
+    if (info->parityValid) {
+      // Calculate parity
+      high |= (evenparity32((high & 0x00000001) ^ (low & 0xB6DB6DB6)) & 1) << 1;
+      low |=  (oddparity32( (high & 0x00000003) ^ (low & 0x6DB6DB6C)) & 1);
+      high |= (oddparity32( (high & 0x00000003) ^ (low & 0xFFFFFFFF)) & 1) << 2;
+    }
+    break;
+
+  case 37: //H10304
+    low |= (info->cardnum & 0x7ffff) << 1;
+    low |= (info->fc & 0xfff) << 20;
+    high |= (info->fc & 0xf000) >> 12;
+
+    if (info->parityValid) {
+      // Calculate parity
+      high |= (evenparity32((high & 0x0000000F) ^ (low & 0xFFFC0000)) & 1) << 4;
+      low |= (oddparity32(low & 0x0007FFFE) & 1);
+    }
+    break;
+
+  case 48: // Corporate 1000 48-bit
+    low |= (info->cardnum & 0x7FFFFF) << 1;
+    low |= (info->fc & 0xff) << 24;
+    high |= (info->fc & 0x3FFF00) >> 8;
+
+    if (info->parityValid) {
+      // Calculate parity
+      high |= (evenparity32((high & 0x00001B6D) ^ (low & 0xB6DB6DB6)) & 1) << 14;
+      low |=  (oddparity32( (high & 0x000036DB) ^ (low & 0x6DB6DB6C)) & 1);
+      high |= (oddparity32( (high & 0x00007FFF) ^ (low & 0xFFFFFFFF)) & 1) << 15;
+    }
+    break;
+
+  default:
+    // Invalid / unsupported length
+    return false;
+  }
+  
+  // Set the format length bits
+  if (info->fmtLen < 37) {
+    // Bit 37 is always set
+    high |= 0x20;
+
+    // Set the bit corresponding to the length.
+    if (info->fmtLen < 32)
+      low |= 1 << info->fmtLen;
+    else 
+      high |= 1 << (info->fmtLen - 32);
+    
+  } else if (info->fmtLen > 37){
+    if (info->fmtLen < 64) 
+      high |= 1 << (info->fmtLen - 32);
+    else 
+      higher |= 1 << (info->fmtLen - 64);
+  }
+  // Return result only if successful.
+  *hi2 = higher;
+  *hi = high;
+  *lo = low;
+  return true;
+}
+
+/**
+ * Unpacks an HID ID into its component parts.
+ *
+ * This only works with 26, 34, 35, 37, and 48 bit card IDs.
+ *
+ * Returns false on invalid inputs.
+ */
+bool unpack_hid(hid_info *out, uint32_t hi2, uint32_t hi, uint32_t lo) {
+  memset(out, 0, sizeof(hid_info));
+  uint8_t fmtLen = 0;
+       
+  uint32_t hFmt; // for calculating card length
+  if ((hi2 & 0x000FFFFF) > 0) { // > 64 bits
+    hFmt = hi2 & 0x000FFFFF;
+    fmtLen = 64;
+  } else if ((hi & 0xFFFFFFC0) > 0) { // < 63-38 bits
+    hFmt = hi & 0xFFFFFFC0;
+    fmtLen = 32;
+  } else if ((hi & 0x00000020) == 0) { // 37 bits
+    hFmt = 0;
+    fmtLen = 37;
+  } else if ((hi & 0x0000001F) > 0){ // 36-32 bits
+    hFmt = hi & 0x0000001F;
+    fmtLen = 32;
+  } else { // <32 bits
+    hFmt = lo;
+    fmtLen = 0;
+  }
+
+  while (hFmt > 1) {
+    hFmt >>= 1;
+    fmtLen++;
+  }
+  out->fmtLen = fmtLen;
+  switch (out->fmtLen) {
+    case 26: // HID H10301
+      out->cardnum = (lo >> 1) & 0xFFFF;
+      out->fc = (lo >> 17) & 0xFF;
+
+      if (g_debugMode) {
+        PrintAndLog("oddparity : input=%x, calculated=%d, provided=%d",
+          (lo >> 1) & 0xFFF, oddparity32((lo >> 1) & 0xFFF), lo & 1);
+        PrintAndLog("evenparity: input=%x, calculated=%d, provided=%d",
+          (lo >> 13) & 0xFFF, evenparity32((lo >> 13) & 0xFFF) & 1, (lo >> 25) & 1);
+      }
+
+      out->parityValid =
+        (oddparity32((lo >> 1) & 0xFFF) == (lo & 1)) &&
+        ((evenparity32((lo >> 13) & 0xFFF) & 1) == ((lo >> 25) & 1));
+      break;
+
+    case 34: // HID H10306
+      out->cardnum = (lo >> 1) & 0xFFFF;
+      out->fc = ((hi & 1) << 15) | (lo >> 17);
+      out->parityValid =
+        ((evenparity32((hi & 0x00000001) ^ (lo & 0xFFFE0000)) & 1) == ((hi >> 1) & 1)) &&
+        ((oddparity32(lo & 0x0001FFFE) & 1) == ((lo & 1)));
+      break;
+
+    case 35: // HID Corporate 1000-35
+      out->cardnum = (lo >> 1) & 0xFFFFF;
+      out->fc = ((hi & 1) << 11) | (lo >> 21);
+      out->parityValid = 
+        (evenparity32((hi & 0x00000001) ^ (lo & 0xB6DB6DB6)) == ((hi >> 1) & 1)) &&
+        (oddparity32( (hi & 0x00000003) ^ (lo & 0x6DB6DB6C)) == ((lo >> 0) & 1)) &&
+        (oddparity32( (hi & 0x00000003) ^ (lo & 0xFFFFFFFF)) == ((hi >> 2) & 1));
+      if (g_debugMode) {
+        PrintAndLog("Parity check: calculated {%d, %d, %d}, provided {%d, %d, %d}",
+          evenparity32((hi & 0x00000001) ^ (lo & 0xB6DB6DB6)),
+          oddparity32( (hi & 0x00000003) ^ (lo & 0x6DB6DB6C)),
+          oddparity32( (hi & 0x00000003) ^ (lo & 0xFFFFFFFF)),
+          ((hi >> 1) & 1),
+          ((lo >> 0) & 1),
+          ((hi >> 2) & 1)
+        );
+      }
+      break;
+
+    case 37: // HID H10304
+      out->fmtLen = 37;
+      out->cardnum = (lo >> 1) & 0x7FFFF;
+      out->fc = ((hi & 0xF) << 12) | (lo >> 20);
+      out->parityValid =
+        (evenparity32((hi & 0x0000000F) ^ (lo & 0xFFFC0000)) == ((hi >> 4) & 1)) &&
+        (oddparity32( lo & 0x0007FFFE) == (lo & 1));
+      break;
+
+    case 48: // HID Corporate 1000-48
+      out->cardnum = (lo >> 1) & 0x7FFFFF;  //Start 24, 23 length
+      out->fc = ((hi & 0x3FFF) << 8 ) | (lo >> 24);  //Start 2, 22 length
+      out->parityValid = 
+        (evenparity32((hi & 0x00001B6D) ^ (lo & 0xB6DB6DB6)) == ((hi >> 14) & 1)) &&
+        (oddparity32( (hi & 0x000036DB) ^ (lo & 0x6DB6DB6C)) == ((lo >> 0) & 1)) &&
+        (oddparity32( (hi & 0x00007FFF) ^ (lo & 0xFFFFFFFF)) == ((hi >> 15) & 1));
+      if (g_debugMode) {
+        PrintAndLog("Parity check: calculated {%d, %d, %d}, provided {%d, %d, %d}",
+          evenparity32((hi & 0x00001B6D) ^ (lo & 0xB6DB6DB6)),
+          oddparity32( (hi & 0x000036DB) ^ (lo & 0x6DB6DB6C)),
+          oddparity32( (hi & 0x00007FFF) ^ (lo & 0xFFFFFFFF)),
+          ((hi >> 14) & 1),
+          ((lo >> 0) & 1),
+          ((hi >> 15) & 1)
+        );
+      }
+      break;
+
+    default:
+      return false;
+  }
+  return true;
+}
+
+/**
+ * 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);
+  }
+
+  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)
 //print full HID Prox ID and some bit format details if found
@@ -54,50 +300,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",
+  
+  hid_info card_info;
+  bool ret = unpack_hid(&card_info, (uint32_t)hi2, (uint32_t)hi, (uint32_t)lo);
+  
+  if (hi2 != 0)
+    PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
+      (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
+      card_info.fmtLen, card_info.fc, card_info.cardnum);
+  else
+    PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
       (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
-      (unsigned int) fmtLen, (unsigned int) fc, (unsigned int) cardnum);
+      card_info.fmtLen, card_info.fc, card_info.cardnum);
+
+  if (card_info.fmtLen == 26 || card_info.fmtLen == 35 || card_info.fmtLen == 48) {
+    PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
+  }
+  if (!ret) {
+    PrintAndLog("Invalid or unsupported tag length.");
   }
   setDemodBuf(BitStream,BitLen,idx);
   setClockGrid(50, waveIdx + (idx*50));
@@ -111,7 +331,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);
@@ -120,15 +340,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}};
@@ -139,29 +358,14 @@ 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;
   }
 
@@ -174,13 +378,82 @@ int CmdHIDClone(const char *Cmd)
   return 0;
 }
 
+
+int CmdHIDPack(const char *Cmd) {
+  uint32_t hi2 = 0, hi = 0, lo = 0;
+  
+  if (strlen(Cmd)<3) {
+    PrintAndLog("Usage:  lf hid pack <length> <facility code (decimal)> <card number (decimal)>");
+    PrintAndLog("        sample: lf hid pack 26 123 4567");
+    return 0;
+  }
+  uint8_t fmtLen = param_get8(Cmd, 0);
+
+  hid_info card_info;
+  card_info.fmtLen = fmtLen;
+  card_info.fc = param_get32ex(Cmd, 1, 0, 10);
+  card_info.cardnum = param_get64ex(Cmd, 2, 0, 10);
+  card_info.parityValid = true;
+
+  bool ret = pack_hid(&hi2, &hi, &lo, &card_info);
+  if (ret) {
+    if (hi2 != 0) {
+      PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
+        (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
+        card_info.fmtLen, card_info.fc, card_info.cardnum);
+    } else {
+      PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
+        (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
+        card_info.fmtLen, card_info.fc, card_info.cardnum); 
+    }
+  } else {
+    PrintAndLog("Invalid or unsupported tag length.");
+  }
+  return 0;
+}
+
+
+int CmdHIDUnpack(const char *Cmd)
+{
+  uint32_t hi2 = 0, hi = 0, lo = 0;
+  if (strlen(Cmd)<1) {
+    PrintAndLog("Usage:  lf hid unpack <ID>");
+    PrintAndLog("        sample: lf hid unpack 2006f623ae");
+    return 0;
+  }
+
+  hexstring_to_int96(&hi2, &hi, &lo, Cmd);
+       
+  hid_info card_info;
+  bool ret = unpack_hid(&card_info, hi2, hi, lo);
+
+  if (hi2 != 0) {
+    PrintAndLog("HID Prox TAG ID: %x%08x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
+      (unsigned int) hi2, (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
+      card_info.fmtLen, card_info.fc, card_info.cardnum);
+  } else {
+    PrintAndLog("HID Prox TAG ID: %x%08x (%d) - Format Len: %u bits - FC: %u - Card: %u",
+      (unsigned int) hi, (unsigned int) lo, (unsigned int) (lo>>1) & 0xFFFF,
+      card_info.fmtLen, card_info.fc, card_info.cardnum);
+  }
+  PrintAndLog("Parity: %s", card_info.parityValid ? "valid" : "invalid");
+  
+  if (!ret) {
+    PrintAndLog("Invalid or unsupported tag length.");
+  }
+  return 0;
+}
+
+
 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, "<ID> -- HID tag simulator"},
-  {"clone",     CmdHIDClone,    0, "<ID> ['l'] -- Clone HID to T55x7 (tag must be in antenna)(option 'l' for 84bit ID)"},
+  {"clone",     CmdHIDClone,    0, "<ID> -- Clone HID to T55x7 (tag must be in antenna)"},
+  {"pack",      CmdHIDPack,     1, "<len> <fc> <num> -- packs an HID ID from its length, facility code and card number"},
+  {"unpack",    CmdHIDUnpack,   1, "<ID> -- unpacks an HID ID to its length, facility code and card number"},
   {NULL, NULL, 0, NULL}
 };
 
Impressum, Datenschutz