2016-12-15 06:26:14 +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.
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-07-14 22:20:34 +08:00
|
|
|
// Low frequency visa 2000 tag commands
|
|
|
|
// by iceman
|
|
|
|
// ASK/Manchester, RF/64, STT, 96 bits (complete)
|
2016-12-15 06:26:14 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "cmdlfvisa2000.h"
|
2019-09-27 02:42:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2019-10-06 05:56:19 +08:00
|
|
|
#include <inttypes.h>
|
2020-10-21 14:42:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "commonutil.h" // ARRAYLEN
|
2019-09-27 02:42:00 +08:00
|
|
|
#include "common.h"
|
2020-10-21 14:42:33 +08:00
|
|
|
#include "cmdparser.h" // command_t
|
2019-09-27 02:42:00 +08:00
|
|
|
#include "comms.h"
|
|
|
|
#include "ui.h"
|
|
|
|
#include "graph.h"
|
|
|
|
#include "cmddata.h"
|
|
|
|
#include "cmdlf.h"
|
2020-10-21 14:42:33 +08:00
|
|
|
#include "protocols.h" // for T55xx config register definitions
|
|
|
|
#include "lfdemod.h" // parityTest
|
2019-09-27 02:42:00 +08:00
|
|
|
#include "cmdlft55xx.h" // write verify
|
2020-10-21 14:42:33 +08:00
|
|
|
#include "cmdlfem4x05.h" //
|
2020-11-25 21:43:39 +08:00
|
|
|
#include "cliparser.h"
|
2019-09-27 02:42:00 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
#ifndef VISA2k_BL0CK1
|
|
|
|
#define VISA2k_BL0CK1 0x56495332
|
|
|
|
#endif
|
2016-12-16 19:47:49 +08:00
|
|
|
|
2016-12-15 06:26:14 +08:00
|
|
|
static int CmdHelp(const char *Cmd);
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
static uint8_t visa_chksum(uint32_t id) {
|
2016-12-16 19:47:49 +08:00
|
|
|
uint8_t sum = 0;
|
|
|
|
for (uint8_t i = 0; i < 32; i += 4)
|
2019-03-10 07:00:59 +08:00
|
|
|
sum ^= (id >> i) & 0xF;
|
2016-12-16 19:47:49 +08:00
|
|
|
return sum & 0xF;
|
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
static uint8_t visa_parity(uint32_t id) {
|
2019-03-10 06:35:06 +08:00
|
|
|
// 4bit parity LUT
|
|
|
|
uint8_t par_lut[] = {
|
2019-03-10 07:00:59 +08:00
|
|
|
0, 1, 1, 0
|
|
|
|
, 1, 0, 0, 1
|
|
|
|
, 1, 0, 0, 1
|
|
|
|
, 0, 1, 1, 0
|
2019-03-10 06:35:06 +08:00
|
|
|
};
|
|
|
|
uint8_t par = 0;
|
2019-03-10 07:00:59 +08:00
|
|
|
par |= par_lut[(id >> 28) & 0xF ] << 7;
|
|
|
|
par |= par_lut[(id >> 24) & 0xF ] << 6;
|
|
|
|
par |= par_lut[(id >> 20) & 0xF ] << 5;
|
|
|
|
par |= par_lut[(id >> 16) & 0xF ] << 4;
|
|
|
|
par |= par_lut[(id >> 12) & 0xF ] << 3;
|
|
|
|
par |= par_lut[(id >> 8) & 0xF ] << 2;
|
|
|
|
par |= par_lut[(id >> 4) & 0xF ] << 1;
|
|
|
|
par |= par_lut[(id & 0xF) ];
|
2019-03-10 06:35:06 +08:00
|
|
|
return par;
|
2017-03-05 23:56:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-13 17:58:28 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 56495332 00096ebd 00000077 —> tag id 618173
|
2017-07-14 22:20:34 +08:00
|
|
|
* aaaaaaaa iiiiiiii -----ppc
|
2017-02-13 17:58:28 +08:00
|
|
|
*
|
|
|
|
* a = fixed value ascii 'VIS2'
|
|
|
|
* i = card id
|
2017-07-14 22:20:34 +08:00
|
|
|
* p = even parity bit for each nibble in card id.
|
2017-02-13 17:58:28 +08:00
|
|
|
* c = checksum (xor of card id)
|
2019-03-09 15:59:13 +08:00
|
|
|
*
|
2017-02-13 17:58:28 +08:00
|
|
|
**/
|
2016-12-15 06:26:14 +08:00
|
|
|
//see ASKDemod for what args are accepted
|
2020-09-28 17:50:20 +08:00
|
|
|
int demodVisa2k(bool verbose) {
|
|
|
|
(void) verbose; // unused so far
|
2019-03-10 06:35:06 +08:00
|
|
|
save_restoreGB(GRAPH_SAVE);
|
|
|
|
|
2019-05-22 21:04:38 +08:00
|
|
|
//CmdAskEdgeDetect("");
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
//ASK / Manchester
|
|
|
|
bool st = true;
|
2020-09-28 17:50:20 +08:00
|
|
|
if (ASKDemod_ext(64, 0, 0, 0, false, false, false, 1, &st) != PM3_SUCCESS) {
|
2019-03-10 06:35:06 +08:00
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: Error - Visa2k: ASK/Manchester Demod failed");
|
|
|
|
save_restoreGB(GRAPH_RESTORE);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
size_t size = DemodBufferLen;
|
|
|
|
int ans = detectVisa2k(DemodBuffer, &size);
|
2019-03-10 07:00:59 +08:00
|
|
|
if (ans < 0) {
|
2019-03-10 06:35:06 +08:00
|
|
|
if (ans == -1)
|
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: Error - Visa2k: too few bits found");
|
|
|
|
else if (ans == -2)
|
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: Error - Visa2k: preamble not found");
|
|
|
|
else if (ans == -3)
|
2019-10-06 05:56:19 +08:00
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: Error - Visa2k: Size not correct: %zu", size);
|
2019-03-10 06:35:06 +08:00
|
|
|
else
|
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: Error - Visa2k: ans: %d", ans);
|
|
|
|
|
|
|
|
save_restoreGB(GRAPH_RESTORE);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
2019-04-07 03:46:00 +08:00
|
|
|
setDemodBuff(DemodBuffer, 96, ans);
|
2019-03-10 07:00:59 +08:00
|
|
|
setClockGrid(g_DemodClock, g_DemodStartIdx + (ans * g_DemodClock));
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
//got a good demod
|
|
|
|
uint32_t raw1 = bytebits_to_byte(DemodBuffer, 32);
|
2019-03-10 07:00:59 +08:00
|
|
|
uint32_t raw2 = bytebits_to_byte(DemodBuffer + 32, 32);
|
|
|
|
uint32_t raw3 = bytebits_to_byte(DemodBuffer + 64, 32);
|
2019-03-10 06:35:06 +08:00
|
|
|
|
|
|
|
// chksum
|
|
|
|
uint8_t calc = visa_chksum(raw2);
|
|
|
|
uint8_t chk = raw3 & 0xF;
|
|
|
|
|
|
|
|
// test checksums
|
2019-03-10 07:00:59 +08:00
|
|
|
if (chk != calc) {
|
2020-06-26 19:01:17 +08:00
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: error: Visa2000 checksum (%s) %x - %x\n", _RED_("fail"), chk, calc);
|
2019-03-10 06:35:06 +08:00
|
|
|
save_restoreGB(GRAPH_RESTORE);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
|
|
|
// parity
|
|
|
|
uint8_t calc_par = visa_parity(raw2);
|
|
|
|
uint8_t chk_par = (raw3 & 0xFF0) >> 4;
|
2019-03-10 07:00:59 +08:00
|
|
|
if (calc_par != chk_par) {
|
2020-06-26 19:01:17 +08:00
|
|
|
PrintAndLogEx(DEBUG, "DEBUG: error: Visa2000 parity (%s) %x - %x\n", _RED_("fail"), chk_par, calc_par);
|
2019-03-10 06:35:06 +08:00
|
|
|
save_restoreGB(GRAPH_RESTORE);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_ESOFT;
|
2019-03-10 06:35:06 +08:00
|
|
|
}
|
2020-06-26 19:01:17 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "Visa2000 - Card " _GREEN_("%u") ", Raw: %08X%08X%08X", raw2, raw1, raw2, raw3);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_SUCCESS;
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 17:50:20 +08:00
|
|
|
static int CmdVisa2kDemod(const char *Cmd) {
|
2020-11-29 08:45:26 +08:00
|
|
|
CLIParserContext *ctx;
|
|
|
|
CLIParserInit(&ctx, "lf visa2000 demod",
|
|
|
|
"Try to find visa2000 preamble, if found decode / descramble data",
|
|
|
|
"lf visa2000 demod"
|
|
|
|
);
|
|
|
|
|
|
|
|
void *argtable[] = {
|
|
|
|
arg_param_begin,
|
|
|
|
arg_param_end
|
|
|
|
};
|
|
|
|
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
|
|
|
CLIParserFree(ctx);
|
2020-09-28 17:50:20 +08:00
|
|
|
return demodVisa2k(true);
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:26:17 +08:00
|
|
|
// 64*96*2=12288 samples just in case we just missed the first preamble we can still catch 2 of them
|
2020-11-25 21:43:39 +08:00
|
|
|
static int CmdVisa2kReader(const char *Cmd) {
|
|
|
|
CLIParserContext *ctx;
|
|
|
|
CLIParserInit(&ctx, "lf visa2000 reader",
|
|
|
|
"read a visa2000 tag",
|
|
|
|
"lf visa2000 reader -@ -> continuous reader mode"
|
|
|
|
);
|
|
|
|
|
|
|
|
void *argtable[] = {
|
|
|
|
arg_param_begin,
|
|
|
|
arg_lit0("@", NULL, "optional - continuous reader mode"),
|
|
|
|
arg_param_end
|
|
|
|
};
|
|
|
|
CLIExecWithReturn(ctx, Cmd, argtable, true);
|
|
|
|
bool cm = arg_get_lit(ctx, 1);
|
|
|
|
CLIParserFree(ctx);
|
|
|
|
|
2020-12-01 18:26:54 +08:00
|
|
|
if (cm) {
|
|
|
|
PrintAndLogEx(INFO, "Press " _GREEN_("<Enter>") " to exit");
|
|
|
|
}
|
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
do {
|
|
|
|
lf_read(false, 20000);
|
|
|
|
demodVisa2k(!cm);
|
|
|
|
} while (cm && !kbd_enter_pressed());
|
|
|
|
return PM3_SUCCESS;
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:07:11 +08:00
|
|
|
static int CmdVisa2kClone(const char *Cmd) {
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
CLIParserContext *ctx;
|
|
|
|
CLIParserInit(&ctx, "lf visa2000 clone",
|
|
|
|
"clone a Visa2000 tag to a T55x7, Q5/T5555 or EM4305/4469 tag.",
|
|
|
|
"lf visa2000 clone --cn 112233\n"
|
|
|
|
"lf visa2000 clone --cn 112233 --q5 -> encode for Q5/T5555 tag\n"
|
|
|
|
"lf visa2000 clone --cn 112233 --em -> encode for EM4305/4469"
|
|
|
|
);
|
|
|
|
|
|
|
|
void *argtable[] = {
|
|
|
|
arg_param_begin,
|
|
|
|
arg_u64_1(NULL, "cn", "<dec>", "Visa2k card ID"),
|
|
|
|
arg_lit0(NULL, "q5", "optional - specify writing to Q5/T5555 tag"),
|
|
|
|
arg_lit0(NULL, "em", "optional - specify writing to EM4305/4469 tag"),
|
|
|
|
arg_param_end
|
|
|
|
};
|
|
|
|
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
uint32_t id = arg_get_u32_def(ctx, 1, 0);
|
|
|
|
bool q5 = arg_get_lit(ctx, 2);
|
|
|
|
bool em = arg_get_lit(ctx, 3);
|
|
|
|
CLIParserFree(ctx);
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
if (q5 && em) {
|
|
|
|
PrintAndLogEx(FAILED, "Can't specify both Q5 and EM4305 at the same time");
|
|
|
|
return PM3_EINVARG;
|
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
uint32_t blocks[4] = {T55x7_MODULATION_MANCHESTER | T55x7_BITRATE_RF_64 | T55x7_ST_TERMINATOR | 3 << T55x7_MAXBLOCK_SHIFT, VISA2k_BL0CK1, 0};
|
2020-10-21 14:42:33 +08:00
|
|
|
char cardtype[16] = {"T55x7"};
|
|
|
|
// Q5
|
|
|
|
if (q5) {
|
2020-08-28 16:03:33 +08:00
|
|
|
blocks[0] = T5555_FIXED | T5555_MODULATION_MANCHESTER | T5555_SET_BITRATE(64) | T5555_ST_TERMINATOR | 3 << T5555_MAXBLOCK_SHIFT;
|
2020-11-02 08:46:47 +08:00
|
|
|
snprintf(cardtype, sizeof(cardtype), "Q5/T5555");
|
2020-10-21 14:42:33 +08:00
|
|
|
}
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-10-21 14:42:33 +08:00
|
|
|
// EM4305
|
2020-10-21 19:26:28 +08:00
|
|
|
if (em) {
|
2020-10-21 14:42:33 +08:00
|
|
|
blocks[0] = EM4305_VISA2000_CONFIG_BLOCK;
|
2020-11-02 08:46:47 +08:00
|
|
|
snprintf(cardtype, sizeof(cardtype), "EM4305/4469");
|
2020-10-21 14:42:33 +08:00
|
|
|
}
|
2020-11-02 08:46:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
blocks[2] = id;
|
2019-03-10 07:00:59 +08:00
|
|
|
blocks[3] = (visa_parity(id) << 4) | visa_chksum(id);
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-28 20:46:33 +08:00
|
|
|
PrintAndLogEx(INFO, "Preparing to clone Visa2000 to " _YELLOW_("%s") " with CardId: " _GREEN_("%"PRIu32), cardtype, id);
|
2019-09-27 02:42:00 +08:00
|
|
|
print_blocks(blocks, ARRAYLEN(blocks));
|
2019-09-19 16:47:12 +08:00
|
|
|
|
2020-10-21 14:42:33 +08:00
|
|
|
int res;
|
2020-10-21 19:26:28 +08:00
|
|
|
if (em) {
|
2020-10-21 14:42:33 +08:00
|
|
|
res = em4x05_clone_tag(blocks, ARRAYLEN(blocks), 0, false);
|
|
|
|
} else {
|
|
|
|
res = clone_t55xx_tag(blocks, ARRAYLEN(blocks));
|
|
|
|
}
|
2020-03-02 21:15:51 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "Done");
|
2020-11-25 21:43:39 +08:00
|
|
|
PrintAndLogEx(HINT, "Hint: try " _YELLOW_("`lf visa2000 reader`") " to verify");
|
2020-03-02 21:15:51 +08:00
|
|
|
return res;
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:07:11 +08:00
|
|
|
static int CmdVisa2kSim(const char *Cmd) {
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
CLIParserContext *ctx;
|
|
|
|
CLIParserInit(&ctx, "lf visa2000 sim",
|
|
|
|
"Enables simulation of visa2k card with specified card number.\n"
|
|
|
|
"Simulation runs until the button is pressed or another USB command is issued.\n",
|
|
|
|
"lf visa2000 sim --cn 1337"
|
|
|
|
);
|
|
|
|
|
|
|
|
void *argtable[] = {
|
|
|
|
arg_param_begin,
|
|
|
|
arg_u64_1(NULL, "cn", "<dec>", "Visa2k card ID"),
|
|
|
|
arg_param_end
|
|
|
|
};
|
|
|
|
CLIExecWithReturn(ctx, Cmd, argtable, false);
|
|
|
|
uint32_t id = arg_get_u32_def(ctx, 1, 0);
|
|
|
|
CLIParserFree(ctx);
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
PrintAndLogEx(SUCCESS, "Simulating Visa2000 - CardId:" _YELLOW_("%u"), id);
|
2016-12-15 06:26:14 +08:00
|
|
|
|
2020-11-25 21:43:39 +08:00
|
|
|
uint32_t blocks[3] = { VISA2k_BL0CK1, id, (visa_parity(id) << 4) | visa_chksum(id) };
|
2016-12-16 19:47:49 +08:00
|
|
|
|
2019-05-24 19:06:08 +08:00
|
|
|
uint8_t bs[96];
|
2019-03-10 07:00:59 +08:00
|
|
|
for (int i = 0; i < 3; ++i)
|
2019-05-24 19:06:08 +08:00
|
|
|
num_to_bytebits(blocks[i], 32, bs + i * 32);
|
|
|
|
|
|
|
|
lf_asksim_t *payload = calloc(1, sizeof(lf_asksim_t) + sizeof(bs));
|
|
|
|
payload->encoding = 1;
|
|
|
|
payload->invert = 0;
|
|
|
|
payload->separator = 1;
|
|
|
|
payload->clock = 64;
|
|
|
|
memcpy(payload->data, bs, sizeof(bs));
|
2016-12-16 19:47:49 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
clearCommandBuffer();
|
2019-08-04 01:17:00 +08:00
|
|
|
SendCommandNG(CMD_LF_ASK_SIMULATE, (uint8_t *)payload, sizeof(lf_asksim_t) + sizeof(bs));
|
2019-05-24 19:06:08 +08:00
|
|
|
free(payload);
|
|
|
|
|
2019-05-09 06:08:59 +08:00
|
|
|
PacketResponseNG resp;
|
2019-08-04 01:17:00 +08:00
|
|
|
WaitForResponse(CMD_LF_ASK_SIMULATE, &resp);
|
2019-05-24 19:06:08 +08:00
|
|
|
|
|
|
|
PrintAndLogEx(INFO, "Done");
|
2019-05-10 02:20:54 +08:00
|
|
|
if (resp.status != PM3_EOPABORTED)
|
2019-05-09 06:08:59 +08:00
|
|
|
return resp.status;
|
|
|
|
return PM3_SUCCESS;
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static command_t CommandTable[] = {
|
2020-11-25 21:43:39 +08:00
|
|
|
{"help", CmdHelp, AlwaysAvailable, "This help"},
|
|
|
|
{"demod", CmdVisa2kDemod, AlwaysAvailable, "demodulate an VISA2000 tag from the GraphBuffer"},
|
|
|
|
{"reader", CmdVisa2kReader, IfPm3Lf, "attempt to read and extract tag data from the antenna"},
|
|
|
|
{"clone", CmdVisa2kClone, IfPm3Lf, "clone Visa2000 tag to T55x7 or Q5/T5555"},
|
|
|
|
{"sim", CmdVisa2kSim, IfPm3Lf, "simulate Visa2000 tag"},
|
2019-05-02 02:48:15 +08:00
|
|
|
{NULL, NULL, NULL, NULL}
|
2016-12-15 06:26:14 +08:00
|
|
|
};
|
|
|
|
|
2019-04-12 08:07:11 +08:00
|
|
|
static int CmdHelp(const char *Cmd) {
|
|
|
|
(void)Cmd; // Cmd is not used so far
|
|
|
|
CmdsHelp(CommandTable);
|
2019-05-22 20:32:30 +08:00
|
|
|
return PM3_SUCCESS;
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:07:11 +08:00
|
|
|
int CmdLFVisa2k(const char *Cmd) {
|
|
|
|
clearCommandBuffer();
|
2019-04-19 06:47:51 +08:00
|
|
|
return CmdsParse(CommandTable, Cmd);
|
2016-12-15 06:26:14 +08:00
|
|
|
}
|
2019-04-12 06:38:54 +08:00
|
|
|
|
|
|
|
// by iceman
|
|
|
|
// find Visa2000 preamble in already demoded data
|
|
|
|
int detectVisa2k(uint8_t *dest, size_t *size) {
|
|
|
|
if (*size < 96) return -1; //make sure buffer has data
|
|
|
|
size_t startIdx = 0;
|
|
|
|
uint8_t preamble[] = {0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0};
|
|
|
|
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx))
|
|
|
|
return -2; //preamble not found
|
|
|
|
if (*size != 96) return -3; //wrong demoded size
|
|
|
|
//return start position
|
|
|
|
return (int)startIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|