TeamWalrus / tusk

GNU General Public License v3.0
12 stars 0 forks source link

Handle Gallagher cards #22

Open dunderhay opened 1 year ago

dunderhay commented 1 year ago

Gallagher cards have additional information:

region code facility code card number issue level

Good resource https://github.com/RfidResearchGroup/proxmark3/blob/185a45e74908398453594f76b481c19f9680ae81/client/src/cmdlfgallagher.c

dunderhay commented 1 year ago

Started working on this.

Code below to handle deobfuscation of cardholder credential data

struct CardholderCredentials
{
    int region_code;
    int facility_code;
    int card_number;
    int issue_level;
};

byte descramble(byte arr) {
  byte lut[] = {
      0x2f, 0x6e, 0xdd, 0xdf, 0x1d, 0x0f, 0xb0, 0x76, 0xad, 0xaf, 0x7f, 0xbb, 0x77, 0x85, 0x11, 0x6d,
      0xf4, 0xd2, 0x84, 0x42, 0xeb, 0xf7, 0x34, 0x55, 0x4a, 0x3a, 0x10, 0x71, 0xe7, 0xa1, 0x62, 0x1a,
      0x3e, 0x4c, 0x14, 0xd3, 0x5e, 0xb2, 0x7d, 0x56, 0xbc, 0x27, 0x82, 0x60, 0xe3, 0xae, 0x1f, 0x9b,
      0xaa, 0x2b, 0x95, 0x49, 0x73, 0xe1, 0x92, 0x79, 0x91, 0x38, 0x6c, 0x19, 0x0e, 0xa9, 0xe2, 0x8d,
      0x66, 0xc7, 0x5a, 0xf5, 0x1c, 0x80, 0x99, 0xbe, 0x4e, 0x41, 0xf0, 0xe8, 0xa6, 0x20, 0xab, 0x87,
      0xc8, 0x1e, 0xa0, 0x59, 0x7b, 0x0c, 0xc3, 0x3c, 0x61, 0xcc, 0x40, 0x9e, 0x06, 0x52, 0x1b, 0x32,
      0x8c, 0x12, 0x93, 0xbf, 0xef, 0x3b, 0x25, 0x0d, 0xc2, 0x88, 0xd1, 0xe0, 0x07, 0x2d, 0x70, 0xc6,
      0x29, 0x6a, 0x4d, 0x47, 0x26, 0xa3, 0xe4, 0x8b, 0xf6, 0x97, 0x2c, 0x5d, 0x3d, 0xd7, 0x96, 0x28,
      0x02, 0x08, 0x30, 0xa7, 0x22, 0xc9, 0x65, 0xf8, 0xb7, 0xb4, 0x8a, 0xca, 0xb9, 0xf2, 0xd0, 0x17,
      0xff, 0x46, 0xfb, 0x9a, 0xba, 0x8f, 0xb6, 0x69, 0x68, 0x8e, 0x21, 0x6f, 0xc4, 0xcb, 0xb3, 0xce,
      0x51, 0xd4, 0x81, 0x00, 0x2e, 0x9c, 0x74, 0x63, 0x45, 0xd9, 0x16, 0x35, 0x5f, 0xed, 0x78, 0x9f,
      0x01, 0x48, 0x04, 0xc1, 0x33, 0xd6, 0x4f, 0x94, 0xde, 0x31, 0x9d, 0x0a, 0xac, 0x18, 0x4b, 0xcd,
      0x98, 0xb8, 0x37, 0xa2, 0x83, 0xec, 0x03, 0xd8, 0xda, 0xe5, 0x7a, 0x6b, 0x53, 0xd5, 0x15, 0xa4,
      0x43, 0xe9, 0x90, 0x67, 0x58, 0xc0, 0xa5, 0xfa, 0x2a, 0xb1, 0x75, 0x50, 0x39, 0x5c, 0xe6, 0xdc,
      0x89, 0xfc, 0xcf, 0xfe, 0xf9, 0x57, 0x54, 0x64, 0xa8, 0xee, 0x23, 0x0b, 0xf1, 0xea, 0xfd, 0xdb,
      0xbd, 0x09, 0xb5, 0x5b, 0x05, 0x86, 0x13, 0xf3, 0x24, 0xc5, 0x3f, 0x44, 0x72, 0x7c, 0x7e, 0x36
  };

  return lut[arr];
}

CardholderCredentials decode_cardholder_credentials(byte *bytes)
{
    byte *arr = new byte[8];
    for (int i = 0; i < 8; i++)
    {
        arr[i] = descramble(bytes[i]);
    }

    CardholderCredentials credentials;
    Serial.println("trying to deobfuscate cardholder credentials");
    // 4bit region code
    credentials.region_code = (arr[3] & 0x1E) >> 1;
    // 16bit facility code
    credentials.facility_code = ((arr[5] & 0x0F) << 12) | (arr[1] << 4) | ((arr[7] >> 4) & 0x0F);
    // 24bit card number
    credentials.card_number = (arr[0] << 16) | ((arr[4] & 0x1F) << 11) | (arr[2] << 3) | ((arr[3] & 0xE0) >> 5);
    // 4bit issue level
    credentials.issue_level = (arr[7] & 0x0F);

    delete[] arr;

    return credentials;
}

void test_decode_cardholder_credentials()
{
    byte inputBytes[] = {0xA3, 0x8A, 0x8A, 0x4B, 0xA3, 0xA3, 0xA3, 0x2C};

    CardholderCredentials credentials = decode_cardholder_credentials(inputBytes);

    Serial.print("Region Code: ");
    Serial.println(credentials.region_code);
    Serial.print("Facility Code: ");
    Serial.println(credentials.facility_code);
    Serial.print("Card Number: ");
    Serial.println(credentials.card_number);
    Serial.print("Issue Level: ");
    Serial.println(credentials.issue_level);
}

output when testing above (which works as expected):

input byte array
A38A8A4BA3A3A32C
calling descramble function
08A8AE8000E3
trying to deobfuscate cardholder credentials
Region Code: 4
Facility Code: 2222
Card Number: 1111
Issue Level: 3

Next steps are:

  1. check the first 16 bits are the fixed sequence 0111111111101010. if does then it's a Cardax 125kHz card (identify_gallagher_125khz_cardax)
  2. decode bytes (decode_125khz_bytes)
  3. decode the cardholder credentials (decode_cardholder_credentials)
dunderhay commented 1 year ago

Started working on decode_cardax_125khz function. This should cover steps 1 and 2 above. Now just to combine everything :)

Note: the checksum always fails, so will need to figure that out :/

void decode_cardax_125khz(String data)
{
  String magic_prefix = "01111111111010";

  int i = data.indexOf(magic_prefix);
  if (i == -1) {
    Serial.println("Magic prefix not found - not a valid gallagher cardax card");
    return;
  }

  String b = "";
  data = data.substring(i + 16);
  data = data.substring(0, 9 * 8 + 8);
  if (data.length() != 9 * 8 + 8) {
    Serial.println("Invalid data length.");
    return;
  }

  while (b.length() < 64 + 8) {
    String n = data.substring(9 * b.length() / 8);
    if (b.length() < 64) {
      if (n.charAt(7) == n.charAt(8)) {
        Serial.println("Invalid data!");
        return;
      }
    }
    b += n.substring(0, 8);
  }

  uint64_t n = strtoull(b.substring(0, 64).c_str(), NULL, 2);

  // This always fails... not sure why
  // byte check_sum = 0x2C;
  // byte xcc[] = {0x7, 0xE, 0x1C, 0x38, 0x70, 0xE0, 0xC7, 0x89};

  // for (int c = 0; c < 8; c++) {
  //   byte ncs = check_sum ^ ((byte*)&n)[c];
  //   check_sum = 0;
  //   for (int i = 0; i < 8; i++) {
  //     if (ncs & (1 << i)) {
  //       check_sum ^= xcc[i];
  //     }
  //   }
  // }

  // if (strtoull(b.substring(0, 64).c_str(), NULL, 2) != check_sum) {
  //   Serial.println("Checksum did not match.");
  //   return;
  // }

  //char hexStr[17];
  //sprintf(hexStr, "%016llX", n);
  //Serial.println(hexStr);

  byte byteArr[8];
  for (int i = 0; i < 8; i++) {
    byteArr[i] = (n >> (56 - i * 8)) & 0xFF;
  }

  Serial.print("card data: ");
  for (int i = 0; i < 8; i++) {
    Serial.print(byteArr[i], HEX);
  }
  Serial.println();
}
dunderhay commented 1 year ago

Requires sampling data from the reader directly rather than relying on data 0 and data 1 output. Additional work being done on this here: https://github.com/TeamWalrus/tusk-sampler