2016-02-28 02:47:10 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
|
|
|
|
// at your option, any later version. See the LICENSE.txt file for the text of
|
|
|
|
// the license.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Low frequency Farpoint / Pyramid tag commands
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-01-19 05:54:27 +08:00
|
|
|
|
2016-02-28 02:47:10 +08:00
|
|
|
#include "cmdlfguard.h"
|
|
|
|
static int CmdHelp(const char *Cmd);
|
|
|
|
|
|
|
|
int usage_lf_guard_clone(void){
|
|
|
|
PrintAndLog("clone a Guardall tag to a T55x7 tag.");
|
|
|
|
PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated. ");
|
|
|
|
PrintAndLog("Currently work only on 26bit");
|
|
|
|
PrintAndLog("");
|
2017-02-13 17:58:28 +08:00
|
|
|
PrintAndLog("Usage: lf guard clone <format> <Facility-Code> <Card-Number>");
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog("Options :");
|
2017-02-13 17:58:28 +08:00
|
|
|
PrintAndLog(" <format> : format length 26|32|36|40");
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog(" <Facility-Code> : 8-bit value facility code");
|
|
|
|
PrintAndLog(" <Card Number> : 16-bit value card number");
|
|
|
|
PrintAndLog("");
|
2017-02-17 11:35:26 +08:00
|
|
|
PrintAndLog("Sample : lf guard clone 26 123 11223");
|
2016-02-28 02:47:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usage_lf_guard_sim(void) {
|
|
|
|
PrintAndLog("Enables simulation of Guardall card with specified card number.");
|
|
|
|
PrintAndLog("Simulation runs until the button is pressed or another USB command is issued.");
|
|
|
|
PrintAndLog("The facility-code is 8-bit and the card number is 16-bit. Larger values are truncated.");
|
|
|
|
PrintAndLog("Currently work only on 26bit");
|
|
|
|
PrintAndLog("");
|
2017-02-13 17:58:28 +08:00
|
|
|
PrintAndLog("Usage: lf guard sim <format> <Card-Number>");
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog("Options :");
|
2017-02-13 17:58:28 +08:00
|
|
|
PrintAndLog(" <format> : format length 26|32|36|40");
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog(" <Facility-Code> : 8-bit value facility code");
|
|
|
|
PrintAndLog(" <Card Number> : 16-bit value card number");
|
|
|
|
PrintAndLog("");
|
|
|
|
PrintAndLog("Sample : lf guard sim 123 11223");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Works for 26bits.
|
2017-02-13 17:58:28 +08:00
|
|
|
int GetGuardBits(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint8_t *guardBits) {
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2016-03-01 13:57:02 +08:00
|
|
|
uint8_t xorKey = 0x66;
|
2016-02-29 05:43:21 +08:00
|
|
|
uint8_t i;
|
|
|
|
uint8_t pre[96];
|
2017-02-13 17:58:28 +08:00
|
|
|
uint8_t rawbytes[12];
|
2016-02-29 05:43:21 +08:00
|
|
|
memset(pre, 0x00, sizeof(pre));
|
2017-02-13 17:58:28 +08:00
|
|
|
memset(rawbytes, 0x00, sizeof(rawbytes));
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
// add format length (decimal)
|
|
|
|
switch (fmtlen) {
|
|
|
|
case 32: {
|
|
|
|
rawbytes[1] = (32 << 2);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 36: {
|
|
|
|
// FC = ((ByteStream[3] & 0x7F)<<7) | (ByteStream[4]>>1);
|
|
|
|
// Card = ((ByteStream[4]&1)<<19) | (ByteStream[5]<<11) | (ByteStream[6]<<3) | (ByteStream[7]>>5);
|
|
|
|
rawbytes[1] = (36 << 2);
|
|
|
|
// Get 26 wiegand from FacilityCode, CardNumber
|
|
|
|
uint8_t wiegand[34];
|
|
|
|
memset(wiegand, 0x00, sizeof(wiegand));
|
|
|
|
num_to_bytebits(fc, 8, wiegand);
|
|
|
|
num_to_bytebits(cn, 26, wiegand+8);
|
|
|
|
|
|
|
|
// add wiegand parity bits (dest, source, len)
|
|
|
|
wiegand_add_parity(pre, wiegand, 34);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 40: {
|
|
|
|
rawbytes[1] = (40 << 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 26:
|
|
|
|
default: {
|
|
|
|
rawbytes[1] = (26 << 2);
|
|
|
|
// Get 26 wiegand from FacilityCode, CardNumber
|
|
|
|
uint8_t wiegand[24];
|
|
|
|
memset(wiegand, 0x00, sizeof(wiegand));
|
|
|
|
num_to_bytebits(fc, 8, wiegand);
|
|
|
|
num_to_bytebits(cn, 16, wiegand+8);
|
|
|
|
|
|
|
|
// add wiegand parity bits (dest, source, len)
|
|
|
|
wiegand_add_parity(pre, wiegand, 24);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 2bit checksum, unknown today,
|
|
|
|
// these two bits are the last ones of rawbyte[1], hence the LSHIFT above.
|
2016-02-29 05:43:21 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
|
2016-02-29 05:43:21 +08:00
|
|
|
// xor key
|
|
|
|
rawbytes[0] = xorKey;
|
|
|
|
|
|
|
|
rawbytes[2] = 1;
|
|
|
|
rawbytes[3] = 0;
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2016-02-29 05:43:21 +08:00
|
|
|
// add wiegand to rawbytes
|
|
|
|
for (i = 0; i < 4; ++i)
|
|
|
|
rawbytes[i+4] = bytebits_to_byte( pre + (i*8), 8);
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2016-02-29 05:43:21 +08:00
|
|
|
if (g_debugMode) printf(" WIE | %s\n", sprint_hex(rawbytes, sizeof(rawbytes)));
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
|
2016-02-29 05:43:21 +08:00
|
|
|
// XOR (only works on wiegand stuff)
|
|
|
|
for (i = 1; i < 12; ++i)
|
|
|
|
rawbytes[i] ^= xorKey ;
|
|
|
|
|
|
|
|
if (g_debugMode) printf(" XOR | %s \n", sprint_hex(rawbytes, sizeof(rawbytes)));
|
|
|
|
|
|
|
|
// convert rawbytes to bits in pre
|
|
|
|
for (i = 0; i < 12; ++i)
|
|
|
|
num_to_bytebitsLSBF( rawbytes[i], 8, pre + (i*8));
|
|
|
|
|
|
|
|
if (g_debugMode) printf("\n Raw | %s \n", sprint_hex(rawbytes, sizeof(rawbytes)));
|
|
|
|
if (g_debugMode) printf(" Raw | %s\n", sprint_bin(pre, 64) );
|
|
|
|
|
|
|
|
// add spacer bit 0 every 4 bits, starting with index 0,
|
2016-02-29 06:28:38 +08:00
|
|
|
// 12 bytes, 24 nibbles. 24+1 extra bites. 3bytes. ie 9bytes | 1byte xorkey, 8bytes rawdata (64bits, should be enough for a 40bit wiegand)
|
2016-02-29 05:43:21 +08:00
|
|
|
addParity(pre, guardBits+6, 64, 5, 3);
|
|
|
|
|
|
|
|
// preamble
|
|
|
|
guardBits[0] = 1;
|
|
|
|
guardBits[1] = 1;
|
|
|
|
guardBits[2] = 1;
|
|
|
|
guardBits[3] = 1;
|
|
|
|
guardBits[4] = 1;
|
|
|
|
guardBits[5] = 0;
|
|
|
|
|
|
|
|
if (g_debugMode) printf(" FIN | %s\n", sprint_bin(guardBits, 96) );
|
2016-02-28 02:47:10 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CmdGuardRead(const char *Cmd) {
|
|
|
|
CmdLFRead("s");
|
2017-01-19 05:54:27 +08:00
|
|
|
getSamples("12000", TRUE);
|
2016-02-28 02:47:10 +08:00
|
|
|
return CmdG_Prox_II_Demod("");
|
|
|
|
}
|
|
|
|
|
|
|
|
int CmdGuardClone(const char *Cmd) {
|
|
|
|
|
|
|
|
char cmdp = param_getchar(Cmd, 0);
|
|
|
|
if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_guard_clone();
|
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0, fmtlen = 0;
|
2016-02-28 02:47:10 +08:00
|
|
|
uint8_t i;
|
|
|
|
uint8_t bs[96];
|
|
|
|
memset(bs, 0x00, sizeof(bs));
|
|
|
|
|
|
|
|
//GuardProxII - compat mode, ASK/Biphase, data rate 64, 3 data blocks
|
2017-02-13 17:58:28 +08:00
|
|
|
uint32_t blocks[5] = {T55x7_MODULATION_BIPHASE | T55x7_BITRATE_RF_64 | 3 << T55x7_MAXBLOCK_SHIFT, 0, 0, 0, 0};
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
if (param_getchar(Cmd, 3) == 'Q' || param_getchar(Cmd, 3) == 'q')
|
|
|
|
//t5555 (Q5) BITRATE = (RF-2)/2 (iceman)
|
|
|
|
blocks[0] = T5555_MODULATION_FSK2 | ((50-2)>>1) << T5555_BITRATE_SHIFT | 3 << T5555_MAXBLOCK_SHIFT;
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-17 11:35:26 +08:00
|
|
|
if (sscanf(Cmd, "%u %u %u", &fmtlen, &fc, &cn ) != 3) return usage_lf_guard_clone();
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
fmtlen &= 0x7f;
|
2016-02-28 02:47:10 +08:00
|
|
|
facilitycode = (fc & 0x000000FF);
|
|
|
|
cardnumber = (cn & 0x0000FFFF);
|
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
if ( !GetGuardBits(fmtlen, facilitycode, cardnumber, bs)) {
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog("Error with tag bitstream generation.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks[1] = bytebits_to_byte(bs,32);
|
|
|
|
blocks[2] = bytebits_to_byte(bs+32,32);
|
|
|
|
blocks[3] = bytebits_to_byte(bs+64,32);
|
|
|
|
|
|
|
|
PrintAndLog("Preparing to clone Guardall to T55x7 with Facility Code: %u, Card Number: %u", facilitycode, cardnumber);
|
|
|
|
PrintAndLog("Blk | Data ");
|
|
|
|
PrintAndLog("----+------------");
|
|
|
|
for ( i = 0; i<4; ++i )
|
2017-01-19 05:54:27 +08:00
|
|
|
PrintAndLog(" %02d | 0x%08x", i, blocks[i]);
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2016-02-29 05:43:21 +08:00
|
|
|
UsbCommand resp;
|
|
|
|
UsbCommand c = {CMD_T55XX_WRITE_BLOCK, {0,0,0}};
|
|
|
|
|
|
|
|
for ( i = 0; i<4; ++i ) {
|
|
|
|
c.arg[0] = blocks[i];
|
|
|
|
c.arg[1] = i;
|
|
|
|
clearCommandBuffer();
|
|
|
|
SendCommand(&c);
|
2017-01-19 05:54:27 +08:00
|
|
|
if (!WaitForResponseTimeout(CMD_ACK, &resp, T55XX_WRITE_TIMEOUT)){
|
2016-02-29 05:43:21 +08:00
|
|
|
PrintAndLog("Error occurred, device did not respond during write operation.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-02-28 02:47:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CmdGuardSim(const char *Cmd) {
|
|
|
|
|
|
|
|
char cmdp = param_getchar(Cmd, 0);
|
|
|
|
if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_guard_sim();
|
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
uint32_t facilitycode = 0, cardnumber = 0, fc = 0, cn = 0, fmtlen = 0;
|
2016-02-29 06:28:38 +08:00
|
|
|
uint8_t clock = 64, encoding = 2, separator = 0, invert = 0;
|
2016-02-28 02:47:10 +08:00
|
|
|
|
|
|
|
uint8_t bs[96];
|
2016-02-29 06:28:38 +08:00
|
|
|
memset(bs, 0x00, sizeof(bs));
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
if (sscanf(Cmd, "%u %u %u", &fmtlen, &fc, &cn ) != 2) return usage_lf_guard_sim();
|
2016-02-28 02:47:10 +08:00
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
fmtlen &= 0x7F;
|
2016-02-28 02:47:10 +08:00
|
|
|
facilitycode = (fc & 0x000000FF);
|
|
|
|
cardnumber = (cn & 0x0000FFFF);
|
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
if ( !GetGuardBits(fmtlen, facilitycode, cardnumber, bs)) {
|
2016-02-28 02:47:10 +08:00
|
|
|
PrintAndLog("Error with tag bitstream generation.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintAndLog("Simulating Guardall - Facility Code: %u, CardNumber: %u", facilitycode, cardnumber );
|
2016-02-29 06:28:38 +08:00
|
|
|
|
|
|
|
// Guard uses: clk: 64, invert: 0, encoding: 2 (ASK Biphase)
|
|
|
|
uint64_t arg1, arg2;
|
|
|
|
arg1 = (clock << 8) | encoding;
|
|
|
|
arg2 = (invert << 8) | separator;
|
|
|
|
|
|
|
|
uint8_t rawbytes[12];
|
|
|
|
size_t size = sizeof(rawbytes);
|
|
|
|
for (uint8_t i=0; i < size; ++i){
|
|
|
|
rawbytes[i] = bytebits_to_byte( bs + (i*8), 8);
|
|
|
|
}
|
|
|
|
|
2016-02-28 02:47:10 +08:00
|
|
|
UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}};
|
2016-02-29 06:28:38 +08:00
|
|
|
memcpy(c.d.asBytes, rawbytes, size );
|
2016-02-28 02:47:10 +08:00
|
|
|
clearCommandBuffer();
|
|
|
|
SendCommand(&c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static command_t CommandTable[] = {
|
|
|
|
{"help", CmdHelp, 1, "This help"},
|
|
|
|
{"read", CmdGuardRead, 0, "Attempt to read and extract tag data"},
|
2016-02-29 05:43:21 +08:00
|
|
|
{"clone", CmdGuardClone, 0, "<Facility-Code> <Card Number> clone Guardall tag"},
|
2016-02-29 06:28:38 +08:00
|
|
|
{"sim", CmdGuardSim, 0, "<Facility-Code> <Card Number> simulate Guardall tag"},
|
2016-02-28 02:47:10 +08:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
int CmdLFGuard(const char *Cmd) {
|
|
|
|
clearCommandBuffer();
|
|
|
|
CmdsParse(CommandTable, Cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CmdHelp(const char *Cmd) {
|
|
|
|
CmdsHelp(CommandTable);
|
|
|
|
return 0;
|
|
|
|
}
|