From: iceman1001 Date: Thu, 5 Jan 2017 00:51:47 +0000 (+0100) Subject: ADD: 'lf animal' commands. CLONE/SIM/READ/DEMOD of FXD-B animal tags. Still some... X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/commitdiff_plain/c48211f7ed175c91d92044e7e08b921e06f8ff3f ADD: 'lf animal' commands. CLONE/SIM/READ/DEMOD of FXD-B animal tags. Still some work left. --- diff --git a/client/Makefile b/client/Makefile index dc1ce801..db33d77a 100644 --- a/client/Makefile +++ b/client/Makefile @@ -142,6 +142,7 @@ CMDSRCS = mifarehost.c \ cmdlfjablotron.c \ cmdlfvisa2000.c \ cmdlfnoralsy.c \ + cmdlffdx.c \ pm3_binlib.c \ scripting.c \ cmdscript.c \ diff --git a/client/cmddata.c b/client/cmddata.c index 12ac7bf9..97d840c5 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -253,19 +253,17 @@ void setDemodBuf(uint8_t *buff, size_t size, size_t startIdx) if ( size >= MAX_DEMOD_BUF_LEN) size = MAX_DEMOD_BUF_LEN; - - size_t i = 0; - for (; i < size; i++){ + + for (size_t i = 0; i < size; i++) DemodBuffer[i]=buff[startIdx++]; - } + DemodBufferLen = size; } -int CmdSetDebugMode(const char *Cmd) -{ - int demod=0; +int CmdSetDebugMode(const char *Cmd) { + int demod = 0; sscanf(Cmd, "%i", &demod); - g_debugMode=(uint8_t)demod; + g_debugMode = (uint8_t)demod; return 1; } @@ -1691,12 +1689,7 @@ int CmdFDXBdemodBI(const char *Cmd){ if (g_debugMode) PrintAndLog("DEBUG: Error - FDXB error removeParity:: %d", size); return 0; } - if (g_debugMode) { - char *bin = sprint_bin_break(BitStream,size,16); - PrintAndLog("DEBUG BinStream:\n%s",bin); - } PrintAndLog("\nFDX-B / ISO 11784/5 Animal Tag ID Found:"); - if (g_debugMode) PrintAndLog("Start marker %d; Size %d", preambleIndex, size); //got a good demod uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(BitStream+32,6)) << 32) | bytebits_to_byteLSBF(BitStream,32); @@ -1717,11 +1710,17 @@ int CmdFDXBdemodBI(const char *Cmd){ PrintAndLog("Animal ID: %04u-%012llu", countryCode, NationalCode); PrintAndLog("National Code: %012llu", NationalCode); PrintAndLog("CountryCode: %04u", countryCode); - PrintAndLog("Extended Data: %s", dataBlockBit ? "True" : "False"); - PrintAndLog("reserved Code: %u", reservedCode); - PrintAndLog("Animal Tag: %s", animalBit ? "True" : "False"); + + PrintAndLog("Reserved/RFU: %u", reservedCode); + PrintAndLog("Animal Tag: %s", animalBit ? "True" : "False"); + PrintAndLog("Has extended data: %s [0x%X]", dataBlockBit ? "True" : "False", extended); PrintAndLog("CRC: 0x%04X - [%04X] - %s", crc16, calcCrc, (calcCrc == crc16) ? "Passed" : "Failed"); - PrintAndLog("Extended: 0x%X\n", extended); + + if (g_debugMode) { + PrintAndLog("Start marker %d; Size %d", preambleIndex, size); + char *bin = sprint_bin_break(BitStream,size,16); + PrintAndLog("DEBUG BinStream:\n%s",bin); + } return 1; } diff --git a/client/cmdlf.c b/client/cmdlf.c index 6ed53477..967dc4af 100644 --- a/client/cmdlf.c +++ b/client/cmdlf.c @@ -1181,6 +1181,7 @@ int CmdLFfind(const char *Cmd) { static command_t CommandTable[] = { {"help", CmdHelp, 1, "This help"}, + {"animal", CmdLFFdx, 1, "{ Animal RFIDs... }"}, {"awid", CmdLFAWID, 1, "{ AWID RFIDs... }"}, {"em4x", CmdLFEM4X, 1, "{ EM4X RFIDs... }"}, {"guard", CmdLFGuard, 1, "{ Guardall RFIDs... }"}, diff --git a/client/cmdlf.h b/client/cmdlf.h index 3bd63e07..8415616e 100644 --- a/client/cmdlf.h +++ b/client/cmdlf.h @@ -40,7 +40,7 @@ #include "cmdlfjablotron.h" // for JABLOTRON menu #include "cmdlfvisa2000.h" // for VISA2000 menu #include "cmdlfnoralsy.h" // for NORALSY meny - +#include "cmdlffdx.h" // for FDX-B meny int CmdLF(const char *Cmd); int CmdLFCommandRead(const char *Cmd); diff --git a/client/cmdlffdx.c b/client/cmdlffdx.c new file mode 100644 index 00000000..58c2cb7c --- /dev/null +++ b/client/cmdlffdx.c @@ -0,0 +1,352 @@ +//----------------------------------------------------------------------------- +// +// 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 Presco tag commands +//----------------------------------------------------------------------------- + +#include "cmdlffdx.h" + +/* + FDX-B ISO11784/85 demod (aka animal tag) BIPHASE, inverted, rf/32, with preamble of 00000000001 (128bits) + 8 databits + 1 parity (1) + CIITT 16 checksum + NATIONAL CODE, ICAR database + COUNTRY CODE (ISO3166) or http://cms.abvma.ca/uploads/ManufacturersISOsandCountryCodes.pdf + FLAG (animal/non-animal) + + 38 IDbits + 10 country code + 1 extra app bit + 14 reserved bits + 1 animal bit + 16 ccitt CRC chksum over 64bit ID CODE. + 24 appli bits. + + sample: 985121004515220 [ 37FF65B88EF94 ] +*/ + +static int CmdHelp(const char *Cmd); + +int usage_lf_fdx_clone(void){ + PrintAndLog("clone a FDX-B Animal tag to a T55x7 tag."); + PrintAndLog("Usage: lf fdx clone [h] "); + PrintAndLog("Options:"); + PrintAndLog(" h : This help"); + PrintAndLog(" : Country id"); + PrintAndLog(" : Animal id"); + // has extended data? + //reserved/rfu + //is animal tag + // extended data + PrintAndLog(" : specify write to Q5 (t5555 instead of t55x7)"); + PrintAndLog(""); + PrintAndLog("Sample: lf fdx clone 999 112233"); + return 0; +} + +int usage_lf_fdx_sim(void) { + PrintAndLog("Enables simulation of FDX-B Animal tag"); + PrintAndLog("Simulation runs until the button is pressed or another USB command is issued."); + PrintAndLog(""); + PrintAndLog("Usage: lf fdx sim [h] "); + PrintAndLog("Options:"); + PrintAndLog(" h : This help"); + PrintAndLog(" : Country id"); + PrintAndLog(" : Animal id"); + PrintAndLog(""); + PrintAndLog("Sample: lf fdx sim 999 112233"); + return 0; +} +// clearing the topbit needed for the preambl detection. +static void verify_values(uint32_t countryid, uint64_t animalid){ + if ((animalid & 0x3FFFFFFFFF) != animalid) { + animalid &= 0x3FFFFFFFFF; + PrintAndLog("Animal ID Truncated to 38bits: %"PRIx64, animalid); + } + if ( (countryid & 0x3ff) != countryid ) { + countryid &= 0x3ff; + PrintAndLog("Country ID Truncated to 10bits: %03d", countryid); + } +} + +static uint16_t getFDXchksum (uint64_t raw){ + uint8_t arr[8]; + num_to_bytes(raw, 64, arr); + return crc16_ccitt_kermit(arr, 8); +} + +int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits) { + + uint64_t raw = 0; + uint8_t data[117] = {0}; + + raw |= isanimal; + raw |= (0x000 & 0x3ff) << 1; + raw |= isextended << 14; + raw |= (country & 0x3ff) << 15; + raw |= (national_id & 0x3FFFFFFFFF) << 25; + + uint16_t crc = getFDXchksum(raw); + + // add raw to bitarray + num_to_bytebits(raw, 64, data); + + // add crc to bitarray + num_to_bytebits(crc, 16, data+64); + + // add extended data to bitarray + num_to_bytebits(extended, 24, data+64+16); + + //reverse array + printf("ICE:\n %s\n", sprint_bin(data, 104) ); + for (uint8_t i = 0; i <104/2; ++i) { + uint8_t tmp = data[i]; + data[i] = data[104-i]; + data[104-i] = tmp; + } + printf("ICE:\n %s\n", sprint_bin(data, 104) ); + + // add parity always EVEN (2), every 8bits + // into output array + uint8_t bitlen = addParity(data, bits+11, 104, 9, 2); + if (bitlen != 117 ) + printf("ICE ERROR PARITY BITLEN 119 != %d\n", bitlen); + + // add preamble ten 0x00 and one 0x01 + memset(bits, 0x00, 10); + bits[10] = 1; + printf("%s\n", sprint_bin_break(bits, 128, 32) ); + /* + // 128bits + // every 9th bit is 0x01, but we can just fill the rest with 0x01 and overwrite + memset(bits, 0x01, 128); + + // add preamble ten 0x00 and one 0x01 + memset(bits, 0x00, 10); + + // add reserved + num_to_bytebitsLSBF(0x00, 7, bits + 66); + num_to_bytebitsLSBF(0x00 >> 7, 7, bits + 74); + + // add animal flag - OK + bits[65] = isanimal; + + // add extended flag - OK + bits[81] = isextended; + + // add national code 40bits - OK + num_to_bytebitsLSBF(national_id >> 0, 8, bits+11); + num_to_bytebitsLSBF(national_id >> 8, 8, bits+20); + num_to_bytebitsLSBF(national_id >> 16, 8, bits+29); + num_to_bytebitsLSBF(national_id >> 24, 8, bits+38); + num_to_bytebitsLSBF(national_id >> 32, 6, bits+47); + + // add country code - OK + num_to_bytebitsLSBF(country >> 0, 2, bits+53); + num_to_bytebitsLSBF(country >> 2, 8, bits+56); + + // add crc-16 - OK + uint8_t raw[8]; + for (uint8_t i=0; i<8; ++i) + raw[i] = bytebits_to_byte(bits + 11 + i * 9, 8); + + uint16_t crc = crc16_ccitt_kermit(raw, 8); + num_to_bytebitsLSBF(crc >> 0, 8, bits+83); + num_to_bytebitsLSBF(crc >> 8, 8, bits+92); + + // extended data - OK + num_to_bytebitsLSBF( extended >> 0 , 8, bits+101); + num_to_bytebitsLSBF( extended >> 8 , 8, bits+110); + num_to_bytebitsLSBF( extended >> 16, 8, bits+119); + + */ + return 1; +} + +//see ASKDemod for what args are accepted +//almost the same demod as cmddata.c/CmdFDXBdemodBI +int CmdFdxDemod(const char *Cmd) { + + //Differential Biphase / di-phase (inverted biphase) + //get binary from ask wave + if (!ASKbiphaseDemod("0 32 1 0", FALSE)) { + if (g_debugMode) PrintAndLog("DEBUG: Error - FDX-B ASKbiphaseDemod failed"); + return 0; + } + size_t size = DemodBufferLen; + int ans = FDXBdemodBI(DemodBuffer, &size); + if (ans < 0){ + if (g_debugMode){ + if (ans == -1) + PrintAndLog("DEBUG: Error - FDX-B too few bits found"); + else if (ans == -2) + PrintAndLog("DEBUG: Error - FDX-B preamble not found"); + else if (ans == -3) + PrintAndLog("DEBUG: Error - FDX-B Size not correct: %d", size); + else + PrintAndLog("DEBUG: Error - FDX-B ans: %d", ans); + } + return 0; + } + + setDemodBuf(DemodBuffer, 128, ans); + // remove marker bits (1's every 9th digit after preamble) (pType = 2) + size = removeParity(DemodBuffer, 11, 9, 2, 117); + if ( size != 104 ) { + if (g_debugMode) PrintAndLog("DEBUG: Error - FDX-B error removeParity: %d", size); + return 0; + } + + //got a good demod + uint64_t NationalCode = ((uint64_t)(bytebits_to_byteLSBF(DemodBuffer+32,6)) << 32) | bytebits_to_byteLSBF(DemodBuffer,32); + uint16_t countryCode = bytebits_to_byteLSBF(DemodBuffer+38,10); + uint8_t dataBlockBit = DemodBuffer[48]; + uint32_t reservedCode = bytebits_to_byteLSBF(DemodBuffer+49,14); + uint8_t animalBit = DemodBuffer[63]; + uint32_t crc16 = bytebits_to_byteLSBF(DemodBuffer+64,16); + uint32_t extended = bytebits_to_byteLSBF(DemodBuffer+80,24); + uint64_t rawid = ((uint64_t) + //bytebits_to_byte(DemodBuffer, 8) << 96) | + //bytebits_to_byte(DemodBuffer,32) << 64) | + bytebits_to_byte(DemodBuffer,32) << 32) | + bytebits_to_byte(DemodBuffer+32, 32); + uint8_t raw[8]; + num_to_bytes(rawid, 8, raw); + + uint16_t calcCrc = crc16_ccitt_kermit(raw, 8); + + PrintAndLog("\nFDX-B / ISO 11784/5 Animal Tag ID Found: Raw : %s", sprint_hex(raw, 8)); + PrintAndLog("Animal ID %04u-%012llu", countryCode, NationalCode); + PrintAndLog("National Code %012llu (0x%llX)", NationalCode, NationalCode); + PrintAndLog("Country Code %04u", countryCode); + PrintAndLog("Reserved/RFU %u (0x04%X)", reservedCode, reservedCode); + PrintAndLog(""); + PrintAndLog("Animal Tag %s", animalBit ? "True" : "False"); + PrintAndLog("Has extended data %s [0x%X]", dataBlockBit ? "True" : "False", extended); + PrintAndLog("CRC-16 0x%04X - 0x%04X [%s]", crc16, calcCrc, (calcCrc == crc16) ? "Ok" : "Failed"); + + if (g_debugMode) { + PrintAndLog("Start marker %d; Size %d", ans, size); + char *bin = sprint_bin_break(DemodBuffer, size, 16); + PrintAndLog("DEBUG bin stream:\n%s", bin); + } + return 1; +} + +int CmdFdxRead(const char *Cmd) { + CmdLFRead("s"); + getSamples("12000", TRUE); + return CmdFdxDemod(Cmd); +} + +int CmdFdxClone(const char *Cmd) { + + uint32_t countryid = 0; + uint64_t animalid = 0; + uint32_t blocks[5] = {T55x7_MODULATION_DIPHASE | T55x7_BITRATE_RF_32 | 4<= 0; --i) { + c.arg[0] = blocks[i]; + c.arg[1] = i; + clearCommandBuffer(); + SendCommand(&c); + if (!WaitForResponseTimeout(CMD_ACK, &resp, 1000)){ + PrintAndLog("Error occurred, device did not respond during write operation."); + return -1; + } + } + return 0; +} + +int CmdFdxSim(const char *Cmd) { + uint32_t countryid = 0; + uint64_t animalid = 0; + + char cmdp = param_getchar(Cmd, 0); + if (strlen(Cmd) == 0 || cmdp == 'h' || cmdp == 'H') return usage_lf_fdx_sim(); + + countryid = param_get32ex(Cmd, 0, 0, 10); + animalid = param_get64ex(Cmd, 1, 0, 10); + + verify_values(countryid, animalid); + + uint8_t clk = 32, encoding = 2, separator = 0, invert = 1; + uint16_t arg1, arg2; + size_t size = 128; + arg1 = clk << 8 | encoding; + arg2 = invert << 8 | separator; + + PrintAndLog("Simulating FDX-B animal ID: %04u-%"PRIu64, countryid, animalid); + + UsbCommand c = {CMD_ASK_SIM_TAG, {arg1, arg2, size}}; + + //getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits) + getFDXBits(animalid, countryid, 1, 0, 0, c.d.asBytes); + clearCommandBuffer(); + SendCommand(&c); + return 0; +} + +static command_t CommandTable[] = { + {"help", CmdHelp, 1, "This help"}, + {"demod", CmdFdxDemod,1, "Attempt extract tag data from graphbuf"}, + {"read", CmdFdxRead, 0, "Attempt to read and extract tag data"}, + {"clone", CmdFdxClone,0, "clone animal ID tag"}, + {"sim", CmdFdxSim, 0, "simulate animal ID tag"}, + {NULL, NULL, 0, NULL} +}; + +int CmdLFFdx(const char *Cmd) { + clearCommandBuffer(); + CmdsParse(CommandTable, Cmd); + return 0; +} + +int CmdHelp(const char *Cmd) { + CmdsHelp(CommandTable); + return 0; +} diff --git a/client/cmdlffdx.h b/client/cmdlffdx.h new file mode 100644 index 00000000..4f502610 --- /dev/null +++ b/client/cmdlffdx.h @@ -0,0 +1,38 @@ +//----------------------------------------------------------------------------- +// +// 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 T55xx commands +//----------------------------------------------------------------------------- +#ifndef CMDLFFDX_H__ +#define CMDLFFDX_H__ +#include +#include +#include +#include "proxmark3.h" +#include "ui.h" +#include "util.h" +#include "graph.h" +#include "cmdparser.h" +#include "cmddata.h" +#include "cmdmain.h" +#include "cmdlf.h" +#include "crc16.h" // for checksum crc-16_ccitt +#include "protocols.h" // for T55xx config register definitions +#include "lfdemod.h" // parityTest +int CmdLFFdx(const char *Cmd); +int CmdFdxClone(const char *Cmd); +int CmdFdxSim(const char *Cmd); +int CmdFdxRead(const char *Cmd); +int CmdFdxDemod(const char *Cmd); + +int getFDXBits(uint64_t national_id, uint16_t country, uint8_t isanimal, uint8_t isextended, uint32_t extended, uint8_t *bits); + +int usage_lf_fdx_clone(void); +int usage_lf_fdx_sim(void); +int usage_lf_fdx_read(void); +int usage_lf_fdx_demod(void); +#endif +