proxmark3/client/cmdlfhid.c

625 lines
22 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
// 2016,2017, marshmellow, iceman
// 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 HID commands
//-----------------------------------------------------------------------------
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
#include "cmdlfhid.h"
#ifndef BITS
# define BITS 96
#endif
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
static int CmdHelp(const char *Cmd);
/*
2019-04-10 19:06:05 +08:00
static int usage_lf_hid_read(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Enables HID compatible reader mode printing details.");
PrintAndLogEx(NORMAL, "By default, values are printed and logged until the button is pressed or another USB command is issued.");
PrintAndLogEx(NORMAL, "If the [1] option is provided, reader mode is exited after reading a single HID card.");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: lf hid read [h] [1]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h : This help");
PrintAndLogEx(NORMAL, " 1 : (optional) stop after reading a single card");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " lf hid read");
PrintAndLogEx(NORMAL, " lf hid read 1");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
*/
2019-04-10 19:06:05 +08:00
static int usage_lf_hid_wiegand(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "This command converts facility code/card number to Wiegand code");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: lf hid wiegand [h] [OEM] [FC] [CN]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, " OEM - OEM number / site code");
PrintAndLogEx(NORMAL, " FC - facility code");
PrintAndLogEx(NORMAL, " CN - card number");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " lf hid wiegand 0 101 2001");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
2019-04-10 19:06:05 +08:00
static int usage_lf_hid_sim(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Enables simulation of HID card with card number.");
PrintAndLogEx(NORMAL, "Simulation runs until the button is pressed or another USB command is issued.");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: lf hid sim [h] [ID]");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, " ID - HID id");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " lf hid sim 2006ec0c86");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
2019-04-10 19:06:05 +08:00
static int usage_lf_hid_clone(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Clone HID to T55x7. Tag must be on antenna. ");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: lf hid clone [h] [ID] <L>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, " ID - HID id");
PrintAndLogEx(NORMAL, " L - 84bit ID");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " lf hid clone 2006ec0c86");
PrintAndLogEx(NORMAL, " lf hid clone 2006ec0c86 L");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
2019-04-10 19:06:05 +08:00
static int usage_lf_hid_brute(void) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "Enables bruteforce of HID readers with specified facility code.");
PrintAndLogEx(NORMAL, "This is a attack against reader. if cardnumber is given, it starts with it and goes up / down one step");
PrintAndLogEx(NORMAL, "if cardnumber is not given, it starts with 1 and goes up to 65535");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Usage: lf hid brute [h] [v] a <format> f <facility-code> c <cardnumber> d <delay>");
PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h : This help");
PrintAndLogEx(NORMAL, " a <format> : 26|33|34|35|37|40|44|84");
PrintAndLogEx(NORMAL, " f <facility-code> : 8-bit value HID facility code");
PrintAndLogEx(NORMAL, " c <cardnumber> : (optional) cardnumber to start with, max 65535");
PrintAndLogEx(NORMAL, " d <delay> : delay betweens attempts in ms. Default 1000ms");
PrintAndLogEx(NORMAL, " v : verbose logging, show all tries");
PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " lf hid brute a 26 f 224");
PrintAndLogEx(NORMAL, " lf hid brute a 26 f 21 d 2000");
PrintAndLogEx(NORMAL, " lf hid brute v a 26 f 21 c 200 d 2000");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
// sending three times. Didn't seem to break the previous sim?
2019-05-22 22:10:48 +08:00
static int sendPing(void) {
2019-05-08 05:35:09 +08:00
SendCommandNG(CMD_PING, NULL, 0);
SendCommandNG(CMD_PING, NULL, 0);
SendCommandNG(CMD_PING, NULL, 0);
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
2019-04-18 18:43:35 +08:00
PacketResponseNG resp;
2019-05-08 05:35:09 +08:00
if (!WaitForResponseTimeout(CMD_PING, &resp, 1000))
2019-05-22 22:10:48 +08:00
return PM3_ETIMEOUT;
return PM3_SUCCESS;
}
2019-05-22 22:10:48 +08:00
static int sendTry(uint8_t fmtlen, uint32_t fc, uint32_t cn, uint32_t delay, uint8_t *bits, bool verbose) {
2019-03-10 06:35:06 +08:00
// this should be optional.
2019-03-10 07:00:59 +08:00
if (verbose)
2019-03-10 06:35:06 +08:00
PrintAndLogEx(INFO, "Trying FC: %u; CN: %u", fc, cn);
calcWiegand(fmtlen, fc, cn, bits, 0);
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
SendCommandMIX(CMD_LF_HID_SIMULATE, bytebits_to_byte(bits, 32), bytebits_to_byte(bits + 32, 32), 0, NULL, 0);
2019-03-10 06:35:06 +08:00
msleep(delay);
2019-05-22 22:10:48 +08:00
return sendPing();
}
//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
static int CmdHIDDemod(const char *Cmd) {
2019-04-10 18:23:40 +08:00
(void)Cmd; // Cmd is not used so far
// HID simulation etc uses 0/1 as signal data. This must be converted in order to demod it back again
2019-06-08 03:40:33 +08:00
if (isGraphBitstream()) {
convertGraphFromBitstream();
}
2019-03-10 06:35:06 +08:00
//raw fsk demod no manchester decoding no start bit finding just get binary from wave
2019-03-10 07:00:59 +08:00
uint32_t hi2 = 0, hi = 0, lo = 0;
2019-03-10 06:35:06 +08:00
uint8_t bits[GraphTraceLen];
2019-03-10 06:35:06 +08:00
size_t size = getFromGraphBuf(bits);
if (size == 0) {
PrintAndLogEx(DEBUG, "DEBUG: Error - HID not enough samples");
2019-05-22 22:10:48 +08:00
return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
//get binary from fsk wave
int waveIdx = 0;
int idx = HIDdemodFSK(bits, &size, &hi2, &hi, &lo, &waveIdx);
if (idx < 0) {
if (idx == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - HID not enough samples");
else if (idx == -2)
PrintAndLogEx(DEBUG, "DEBUG: Error - HID just noise detected");
else if (idx == -3)
PrintAndLogEx(DEBUG, "DEBUG: Error - HID problem during FSK demod");
else if (idx == -4)
PrintAndLogEx(DEBUG, "DEBUG: Error - HID preamble not found");
else if (idx == -5)
PrintAndLogEx(DEBUG, "DEBUG: Error - HID error in Manchester data, size %d", size);
else
PrintAndLogEx(DEBUG, "DEBUG: Error - HID error demoding fsk %d", idx);
2019-05-22 22:10:48 +08:00
return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
2019-04-07 03:46:00 +08:00
setDemodBuff(bits, size, idx);
2019-03-10 07:00:59 +08:00
setClockGrid(50, waveIdx + (idx * 50));
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
if (hi2 == 0 && hi == 0 && lo == 0) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(DEBUG, "DEBUG: Error - HID no values found");
2019-05-22 22:10:48 +08:00
return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
if (hi2 != 0) { //extra large HID tags
PrintAndLogEx(SUCCESS, "HID Prox TAG ID: %x%08x%08x (%u)", hi2, hi, lo, (lo >> 1) & 0xFFFF);
2019-03-10 06:35:06 +08:00
} else { //standard HID tags <38 bits
uint8_t fmtLen = 0;
uint32_t cc = 0;
uint32_t fc = 0;
uint32_t cardnum = 0;
uint8_t oem = 0;
2019-03-10 07:00:59 +08:00
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
2019-03-10 06:35:06 +08:00
uint8_t idx3 = 1;
2019-03-10 07:00:59 +08:00
while (lo2 > 1) { //find last bit set to 1 (format len bit)
2019-03-10 06:35:06 +08:00
lo2 >>= 1;
idx3++;
}
fmtLen = idx3 + 19;
fc = 0;
cardnum = 0;
2019-03-10 07:00:59 +08:00
if (fmtLen == 26) {
2019-03-10 06:35:06 +08:00
cardnum = (lo >> 1) & 0xFFFF;
2019-03-10 07:00:59 +08:00
fc = (lo >> 17) & 0xFF;
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
if (fmtLen == 32 && (lo & 0x40000000)) { //if 32 bit and Kastle bit set
2019-03-10 06:35:06 +08:00
cardnum = (lo >> 1) & 0xFFFF;
fc = (lo >> 17) & 0xFF;
cc = (lo >> 25) & 0x1F;
}
2019-03-10 07:00:59 +08:00
if (fmtLen == 34) {
2019-03-10 06:35:06 +08:00
cardnum = (lo >> 1) & 0xFFFF;
2019-03-10 07:00:59 +08:00
fc = ((hi & 1) << 15) | (lo >> 17);
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
if (fmtLen == 35) {
2019-03-10 06:35:06 +08:00
cardnum = (lo >> 1) & 0xFFFFF;
2019-03-10 07:00:59 +08:00
fc = ((hi & 1) << 11) | (lo >> 21);
2019-03-10 06:35:06 +08:00
}
2019-04-11 13:40:50 +08:00
if (fmtLen == 36) {
oem = (lo >> 1) & 0x3;
cardnum = (lo >> 3) & 0xFFFF;
fc = (hi & 0x7) << 13 | ((lo >> 19) & 0xFFFF);
}
2019-03-10 07:00:59 +08:00
} else { //if bit 38 is not set then 37 bit format is used
2019-03-10 06:35:06 +08:00
fmtLen = 37;
2019-06-12 21:41:23 +08:00
cardnum = (lo >> 1) & 0x7FFFF;
fc = ((hi & 0xF) << 12) | (lo >> 20);
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
if (fmtLen == 32 && (lo & 0x40000000)) { //if 32 bit and Kastle bit set
2019-03-10 06:35:06 +08:00
PrintAndLogEx(SUCCESS, "HID Prox TAG (Kastle format) ID: %08x (%u) - Format Len: 32bit - CC: %u - FC: %u - Card: %u", lo, (lo >> 1) & 0xFFFF, cc, fc, cardnum);
2019-03-10 07:00:59 +08:00
} else {
2019-04-11 08:44:21 +08:00
PrintAndLogEx(SUCCESS, "HID Prox TAG ID: %x%08x (%u) - Format Len: %ubit - OEM: %03u - FC: %u - Card: %u",
2019-04-11 13:40:50 +08:00
hi, lo, cardnum, fmtLen, oem, fc, cardnum);
2019-03-10 06:35:06 +08:00
}
}
PrintAndLogEx(DEBUG, "DEBUG: HID idx: %d, Len: %d, Printing Demod Buffer:", idx, size);
if (g_debugMode)
printDemodBuff();
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
// this read is the "normal" read, which download lf signal and tries to demod here.
static int CmdHIDRead(const char *Cmd) {
2019-03-10 06:35:06 +08:00
lf_read(true, 12000);
return CmdHIDDemod(Cmd);
}
/*
// this read loops on device side.
// uses the demod in lfops.c
static int CmdHIDRead_device(const char *Cmd) {
2019-03-10 06:35:06 +08:00
if (Cmd[0] == 'h' || Cmd[0] == 'H') return usage_lf_hid_read();
2019-03-10 07:00:59 +08:00
uint8_t findone = (Cmd[0] == '1') ? 1 : 0;
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
SendCommandMIX(CMD_LF_HID_DEMOD, findone, 0, 0, NULL, 0);
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
*/
static int CmdHIDSim(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint32_t hi = 0, lo = 0;
uint32_t n = 0, i = 0;
2013-03-13 15:13:19 +08:00
2019-03-10 06:35:06 +08:00
uint8_t ctmp = tolower(param_getchar(Cmd, 0));
2019-03-10 07:00:59 +08:00
if (strlen(Cmd) == 0 || ctmp == 'h') return usage_lf_hid_sim();
2019-03-10 07:00:59 +08:00
while (sscanf(&Cmd[i++], "%1x", &n) == 1) {
2019-03-10 06:35:06 +08:00
hi = (hi << 4) | (lo >> 28);
lo = (lo << 4) | (n & 0xf);
}
2013-03-13 15:13:19 +08:00
2019-03-10 06:35:06 +08:00
PrintAndLogEx(SUCCESS, "Simulating HID tag with ID %x%08x", hi, lo);
PrintAndLogEx(SUCCESS, "Press pm3-button to abort simulation");
2013-03-13 15:13:19 +08:00
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
SendCommandMIX(CMD_LF_HID_SIMULATE, hi, lo, 0, NULL, 0);
PacketResponseNG resp;
WaitForResponse(CMD_LF_HID_SIMULATE, &resp);
PrintAndLogEx(INFO, "Done");
2019-05-10 02:20:54 +08:00
if (resp.status != PM3_EOPABORTED)
return resp.status;
return PM3_SUCCESS;
2013-03-13 15:13:19 +08:00
}
static int CmdHIDClone(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint32_t hi2 = 0, hi = 0, lo = 0;
uint32_t n = 0, i = 0;
uint8_t ctmp = param_getchar(Cmd, 0);
2019-03-10 07:00:59 +08:00
if (strlen(Cmd) == 0 || ctmp == 'H' || ctmp == 'h') return usage_lf_hid_clone();
uint8_t longid[1] = {0};
2019-03-10 07:00:59 +08:00
if (strchr(Cmd, 'l') != 0) {
while (sscanf(&Cmd[i++], "%1x", &n) == 1) {
2019-03-10 06:35:06 +08:00
hi2 = (hi2 << 4) | (hi >> 28);
hi = (hi << 4) | (lo >> 28);
lo = (lo << 4) | (n & 0xf);
}
PrintAndLogEx(INFO, "Preparing to clone HID tag with long ID %x%08x%08x", hi2, hi, lo);
longid[0] = 1;
2019-03-10 06:35:06 +08:00
} else {
2019-03-10 07:00:59 +08:00
while (sscanf(&Cmd[i++], "%1x", &n) == 1) {
2019-03-10 06:35:06 +08:00
hi = (hi << 4) | (lo >> 28);
lo = (lo << 4) | (n & 0xf);
}
PrintAndLogEx(INFO, "Preparing to clone HID tag with ID %x%08x", hi, lo);
hi2 = 0;
}
clearCommandBuffer();
SendCommandOLD(CMD_LF_HID_CLONE, hi2, hi, lo, longid, sizeof(longid));
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
2011-10-15 23:15:49 +08:00
}
2019-03-25 07:04:28 +08:00
/*
// struct to handle wiegand
typedef struct {
2019-03-10 06:35:06 +08:00
uint8_t FormatLen;
uint8_t SiteCode;
uint8_t FacilityCode;
uint8_t CardNumber;
2019-03-10 07:00:59 +08:00
uint8_t *Wiegand;
2019-03-10 06:35:06 +08:00
size_t Wiegand_n;
2019-03-10 07:00:59 +08:00
} wiegand_t;
2019-03-25 07:04:28 +08:00
*/
static void addHIDMarker(uint8_t fmtlen, uint8_t *out) {
2019-03-10 06:35:06 +08:00
// temp array
uint8_t arr[BITS];
memset(arr, 0, BITS);
// copy inpu
2019-03-10 07:00:59 +08:00
uint8_t pos = sizeof(arr) - fmtlen;
memcpy(arr + pos, out, fmtlen);
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
switch (fmtlen) {
case 26: {
2019-03-10 06:35:06 +08:00
// start sentinel, BITS-bit 27 = 1
2019-03-10 07:00:59 +08:00
arr[BITS - 27] = 1;
2019-03-10 06:35:06 +08:00
// fmt smaller than 37 used, bit37 = 1
2019-03-10 07:00:59 +08:00
arr[BITS - 38] = 1;
2019-03-10 06:35:06 +08:00
memcpy(out, arr, BITS);
break;
}
case 34:
// start sentinel, BITS-bit 27 = 1
2019-03-10 07:00:59 +08:00
arr[BITS - 35] = 1;
2019-03-10 06:35:06 +08:00
// fmt smaller than 37 used, bit37 = 1
2019-03-10 07:00:59 +08:00
arr[BITS - 38] = 1;
2019-03-10 06:35:06 +08:00
memcpy(out, arr, BITS);
break;
2019-03-10 07:00:59 +08:00
default:
break;
2019-03-10 06:35:06 +08:00
}
}
// static void getParity34(uint32_t *hi, uint32_t *lo){
2019-03-10 07:00:59 +08:00
// uint32_t result = 0;
// int i;
2019-03-10 07:00:59 +08:00
// // even parity
// for (i = 7;i >= 0;i--)
// result ^= (*hi >> i) & i;
// for (i = 31;i >= 24;i--)
// result ^= (*lo >> i) & 1;
2019-03-10 07:00:59 +08:00
// *hi |= result << 2;
2019-03-10 07:00:59 +08:00
// // odd parity bit
// result = 0;
// for (i = 23;i >= 1;i--)
// result ^= (*lo >> i) & 1;
2019-03-10 07:00:59 +08:00
// *lo |= !result;
// }
// static void getParity37H(uint32_t *hi, uint32_t *lo){
2019-03-10 07:00:59 +08:00
// uint32_t result = 0;
// int i;
// // even parity
// for (i = 4;i >= 0;i--)
// result ^= (*hi >> i) & 1;
// for (i = 31;i >= 20;i--)
// result ^= (*lo >> i) & 1;
// *hi |= result << 4;
// // odd parity
// result = 0;
// for (i = 19;i >= 1;i--)
// result ^= (*lo >> i) & 1;
// *lo |= result;
// }
//static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out){
static void calc26(uint16_t fc, uint32_t cardno, uint8_t *out) {
2019-03-10 06:35:06 +08:00
uint8_t wiegand[24];
num_to_bytebits(fc, 8, wiegand);
2019-03-10 07:00:59 +08:00
num_to_bytebits(cardno, 16, wiegand + 8);
wiegand_add_parity(out, wiegand, sizeof(wiegand));
}
// static void calc33(uint16_t fc, uint32_t cardno, uint8_t *out){
// }
static void calc34(uint16_t fc, uint32_t cardno, uint8_t *out) {
2019-03-10 06:35:06 +08:00
uint8_t wiegand[32];
num_to_bytebits(fc, 16, wiegand);
num_to_bytebits(cardno, 16, wiegand + 16);
2019-03-10 07:00:59 +08:00
wiegand_add_parity(out, wiegand, sizeof(wiegand));
}
// static void calc35(uint16_t fc, uint32_t cardno, uint8_t *out){
2019-03-10 07:00:59 +08:00
// *lo = ((cardno & 0xFFFFF) << 1) | fc << 21;
// *hi = (1 << 5) | ((fc >> 11) & 1);
// }
2019-04-11 13:40:50 +08:00
static void calc36(uint8_t oem, uint16_t fc, uint32_t cardno, uint8_t *out) {
// FC 1 - 16 - 16 bit
// cardno 17 - 33 - 16 bit
// oem 34 - 35 - 2 bit
// Odd Parity 0th bit 1-18
// Even Parity 36th bit 19-35
uint8_t wiegand[34];
num_to_bytebits(fc, 16, wiegand);
num_to_bytebits(cardno & 0xFFFF, 16, wiegand + 16);
num_to_bytebits(oem, 2, wiegand + 32);
wiegand_add_parity_swapped(out, wiegand, sizeof(wiegand));
}
static void calc37S(uint16_t fc, uint32_t cardno, uint8_t *out) {
2019-03-10 06:35:06 +08:00
// FC 2 - 17 - 16 bit
// cardno 18 - 36 - 19 bit
// Even P1 1 - 19
// Odd P37 19 - 36
uint8_t wiegand[35];
num_to_bytebits(fc, 16, wiegand);
num_to_bytebits(cardno, 19, wiegand + 16);
2019-03-10 07:00:59 +08:00
wiegand_add_parity(out, wiegand, sizeof(wiegand));
}
static void calc37H(uint64_t cardno, uint8_t *out) {
2019-03-10 06:35:06 +08:00
// SC NONE
// cardno 1-35 34 bits
// Even Parity 0th bit 1-18
// Odd Parity 36th bit 19-35
uint8_t wiegand[37];
2019-03-10 07:00:59 +08:00
num_to_bytebits((uint32_t)(cardno >> 32), 2, wiegand);
num_to_bytebits((uint32_t)(cardno >> 0), 32, wiegand + 2);
wiegand_add_parity(out, wiegand, sizeof(wiegand));
2019-03-10 06:35:06 +08:00
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, "%x %x\n", (uint32_t)(cardno >> 32), (uint32_t)cardno);
}
// static void calc40(uint64_t cardno, uint8_t *out){
2019-03-10 07:00:59 +08:00
// cardno = (cardno & 0xFFFFFFFFFF);
// *lo = ((cardno & 0xFFFFFFFF) << 1 );
// *hi = (cardno >> 31);
// }
void calcWiegand(uint8_t fmtlen, uint16_t fc, uint64_t cardno, uint8_t *bits, uint8_t oem) {
2019-03-10 07:00:59 +08:00
uint32_t cn32 = (cardno & 0xFFFFFFFF);
switch (fmtlen) {
case 26:
calc26(fc, cn32, bits);
break;
2019-03-10 06:35:06 +08:00
// case 33 : calc33(fc, cn32, bits); break;
2019-03-10 07:00:59 +08:00
case 34:
calc34(fc, cn32, bits);
break;
2019-03-10 06:35:06 +08:00
// case 35 : calc35(fc, cn32, bits); break;
case 36:
calc36(oem, fc, cn32, bits);
break;
2019-03-10 07:00:59 +08:00
case 37:
calc37S(fc, cn32, bits);
break;
case 38:
calc37H(cardno, bits);
break;
2019-03-10 06:35:06 +08:00
// case 40 : calc40(cardno, bits); break;
// case 44 : { break; }
// case 84 : { break; }
2019-03-10 07:00:59 +08:00
default:
break;
2019-03-10 06:35:06 +08:00
}
}
static int CmdHIDWiegand(const char *Cmd) {
2019-03-10 06:35:06 +08:00
uint32_t oem = 0, fc = 0;
uint64_t cardnum = 0;
2019-04-10 14:54:33 +08:00
uint8_t bits[BITS] = {0};
2019-03-10 06:35:06 +08:00
uint8_t *bs = bits;
2019-04-06 00:53:17 +08:00
uint8_t ctmp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) < 3 || ctmp == 'h') return usage_lf_hid_wiegand();
2019-03-10 06:35:06 +08:00
oem = param_get8(Cmd, 0);
fc = param_get32ex(Cmd, 1, 0, 10);
cardnum = param_get64ex(Cmd, 2, 0, 10);
uint8_t fmtlen[] = {26, 33, 34, 35, 36, 37, 38, 40};
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "HID | OEM | FC | CN | Wiegand | HID Formatted");
PrintAndLogEx(NORMAL, "----+-----+------+---------+-----------+--------------------");
2019-08-01 06:14:09 +08:00
for (uint8_t i = 0; i < ARRAYLEN(fmtlen); i++) {
2019-03-10 06:35:06 +08:00
memset(bits, 0x00, sizeof(bits));
calcWiegand(fmtlen[i], fc, cardnum, bs, oem);
2019-03-10 06:35:06 +08:00
PrintAndLogEx(NORMAL, "ice:: %s \n", sprint_bin(bs, fmtlen[i]));
2019-04-10 14:54:33 +08:00
uint64_t wiegand = (uint64_t)bytebits_to_byte(bs, 32) << 32 | bytebits_to_byte(bs + 32, 32);
2019-03-10 06:35:06 +08:00
addHIDMarker(fmtlen[i], bs);
PrintAndLogEx(NORMAL, "ice:: %s\n", sprint_bin(bs, BITS));
2019-04-10 14:54:33 +08:00
uint64_t blocks = (uint64_t)bytebits_to_byte(bs + 32, 32) << 32 | bytebits_to_byte(bs + 64, 32);
2019-03-10 07:00:59 +08:00
uint8_t shifts = 64 - fmtlen[i];
2019-03-10 06:35:06 +08:00
wiegand >>= shifts;
2019-03-10 07:00:59 +08:00
PrintAndLogEx(NORMAL, " %u | %03u | %03u | %" PRIu64 " | %" PRIX64 " | %" PRIX64,
fmtlen[i],
oem,
fc,
cardnum,
wiegand,
blocks
);
2019-03-10 06:35:06 +08:00
}
PrintAndLogEx(NORMAL, "----+-----+-----+-------+-----------+--------------------");
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
static int CmdHIDBrute(const char *Cmd) {
2019-03-10 06:35:06 +08:00
bool errors = false, verbose = false;
uint32_t fc = 0, cn = 0, delay = 1000;
uint8_t fmtlen = 0;
uint8_t bits[96];
memset(bits, 0, sizeof(bits));
uint8_t cmdp = 0;
while (param_getchar(Cmd, cmdp) != 0x00 && !errors) {
switch (tolower(param_getchar(Cmd, cmdp))) {
2019-03-10 07:00:59 +08:00
case 'h':
return usage_lf_hid_brute();
case 'f':
fc = param_get32ex(Cmd, cmdp + 1, 0, 10);
if (!fc)
errors = true;
cmdp += 2;
break;
case 'd':
// delay between attemps, defaults to 1000ms.
delay = param_get32ex(Cmd, cmdp + 1, 1000, 10);
cmdp += 2;
break;
case 'c':
cn = param_get32ex(Cmd, cmdp + 1, 0, 10);
// truncate cardnumber.
cn &= 0xFFFF;
cmdp += 2;
break;
case 'a':
fmtlen = param_get8(Cmd, cmdp + 1);
cmdp += 2;
bool is_ftm_ok = false;
uint8_t ftms[] = {26, 33, 34, 35, 37};
2019-08-01 06:14:09 +08:00
for (uint8_t i = 0; i < ARRAYLEN(ftms); i++) {
2019-03-10 07:00:59 +08:00
if (ftms[i] == fmtlen) {
is_ftm_ok = true;
}
2019-03-10 06:35:06 +08:00
}
2019-03-10 07:00:59 +08:00
// negated
errors = !is_ftm_ok;
break;
case 'v':
verbose = true;
cmdp++;
break;
default:
PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp));
errors = true;
break;
2019-03-10 06:35:06 +08:00
}
}
2019-03-10 07:00:59 +08:00
if (fc == 0) errors = true;
if (errors) return usage_lf_hid_brute();
2019-03-10 06:35:06 +08:00
PrintAndLogEx(INFO, "Brute-forcing HID reader");
PrintAndLogEx(INFO, "Press pm3-button to abort simulation or run another command");
uint16_t up = cn;
uint16_t down = cn;
// main loop
2019-03-10 07:00:59 +08:00
for (;;) {
2019-03-10 06:35:06 +08:00
2019-05-01 05:52:40 +08:00
if (!session.pm3_present) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(WARNING, "Device offline\n");
2019-05-22 22:10:48 +08:00
return PM3_ENODATA;
2019-03-10 06:35:06 +08:00
}
if (kbd_enter_pressed()) {
2019-03-10 06:35:06 +08:00
PrintAndLogEx(INFO, "aborted via keyboard!");
return sendPing();
}
// Do one up
2019-03-10 07:00:59 +08:00
if (up < 0xFFFF)
2019-06-08 03:40:33 +08:00
if (sendTry(fmtlen, fc, up++, delay, bits, verbose) != PM3_SUCCESS) return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
// Do one down (if cardnumber is given)
2019-03-10 07:00:59 +08:00
if (cn > 1)
if (down > 1)
2019-05-22 22:10:48 +08:00
if (sendTry(fmtlen, fc, --down, delay, bits, verbose) != PM3_SUCCESS) return PM3_ESOFT;
2019-03-10 06:35:06 +08:00
}
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
static command_t CommandTable[] = {
{"help", CmdHelp, AlwaysAvailable, "this help"},
{"demod", CmdHIDDemod, AlwaysAvailable, "demodulate HID Prox tag from the GraphBuffer"},
{"read", CmdHIDRead, IfPm3Lf, "attempt to read and extract tag data"},
{"clone", CmdHIDClone, IfPm3Lf, "clone HID to T55x7"},
{"sim", CmdHIDSim, IfPm3Lf, "simulate HID tag"},
{"wiegand", CmdHIDWiegand, AlwaysAvailable, "convert facility code/card number to Wiegand code"},
{"brute", CmdHIDBrute, IfPm3Lf, "bruteforce card number against reader"},
{NULL, NULL, NULL, NULL}
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
};
static int CmdHelp(const char *Cmd) {
(void)Cmd; // Cmd is not used so far
CmdsHelp(CommandTable);
2019-05-22 22:10:48 +08:00
return PM3_SUCCESS;
}
int CmdLFHID(const char *Cmd) {
2019-03-10 06:35:06 +08:00
clearCommandBuffer();
2019-04-19 06:47:51 +08:00
return CmdsParse(CommandTable, Cmd);
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}
int demodHID(void) {
return CmdHIDDemod("");
Client cleanup and restructuring. Stage 1... Next Step is refactoring some of the giant functions which are just copy/paste of some other ones with just a few line changes, removing unnecessary 'goto' etc. The MS Windows version is broken with this commit but will be fixed soon. Everything can't be done all at once :P The commands are now hierarchical, for example: "hf 14a read" vs. "hf 14b read". You can also request help: "hf help", "data help", "hf 15 help" etc. Indents are now space-based, not tab-based anymore. Hopefully no one will be trolling about it, considering the suicide-prone work being done here ;) client/cmdhw.c, client/proxusb.c, client/cmdhw.h, client/proxusb.h, client/cmdmain.c, client/cmdlfhid.c, client/cmdmain.h, client/cmdlfhid.h, client/data.c, client/data.h, client/cmdhf.c, client/cmdlf.c, client/cmdhf.h, client/cmdhf15.c, client/cmdhf14b.c, client/cmdlf.h, client/cmdhf15.h, client/cmdhf14b.h, client/cmddata.c, client/cmddata.h, client/ui.c, client/cmdparser.c, client/cmdlfti.c, client/ui.h, client/cmdlfem4x.c, client/cmdparser.h, client/cmdlfti.h, client/cmdlfem4x.h, client/graph.c, client/graph.h, client/cmdhf14a.c, client/cmdhf14a.h, client/cmdhflegic.c, client/cmdhflegic.c: New files. client/cli.c, client/flasher.c, client/snooper.c, client/proxmark3.c, client/proxmark3.h, client/Makefile: Update accordingly. client/flash.h, client/flash.c, client/proxgui.cpp: Cosmetic changes. client/translate.h, client/command.c, client/gui.c, client/usb.c, client/prox.h: Remove. include/usb_cmd.h (CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443_SIM): Remove dead cmd. common/crc16.h: New file. common/crc16.c: Modify accordingly. common/iso14443crc.h: New file. common/iso14443_crc.c: Rename to common/iso14443crc.c: and modify accordingly. armsrc/lfops.c, armsrc/iso14443.c, armsrc/iso14443a.c: include .h files from the common directory instead of including the c files. common/Makefile.common, armsrc/Makefile: Modify accordingly.
2010-02-04 09:27:07 +08:00
}