diff --git a/CHANGELOG.md b/CHANGELOG.md index 26416eb99..9b7ea9403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `hf mf view` - Urmet Captiv cards now decode their number field and data regions (@iceman1001) - Changed `hf mf view --selftest` - now also checks the Hexact payload decoder, both ways (@iceman1001) - Changed `hf mf view` - Hexact sector 9,11 payload is now decoded and cross checked against sector 0,15 and UID (@iceman1001) - Added `hf mfdes dump` - now walks the whole PICC and saves a `hf-mfdes--dump.json` card image, picking up `hf-mfdes--keys.json` on its own (@iceman1001) diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 466d22ab2..19579482e 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -510,6 +510,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/Makefile b/client/Makefile index 7e2248b33..7d38fb544 100644 --- a/client/Makefile +++ b/client/Makefile @@ -908,6 +908,7 @@ SRCS = mifare/aiddesfire.c \ parsers/parsehid.c \ parsers/parsevigik.c \ parsers/parsehexact.c \ + parsers/parseproac.c \ parsers/hrtparser/hrtparser.c \ pm3.c \ pm3_binlib.c \ diff --git a/client/experimental_lib/CMakeLists.txt b/client/experimental_lib/CMakeLists.txt index 7dcc6d6fc..78a76f9c7 100644 --- a/client/experimental_lib/CMakeLists.txt +++ b/client/experimental_lib/CMakeLists.txt @@ -437,6 +437,7 @@ set (TARGET_SOURCES ${PM3_ROOT}/client/src/parsers/parsehid.c ${PM3_ROOT}/client/src/parsers/parsevigik.c ${PM3_ROOT}/client/src/parsers/parsehexact.c + ${PM3_ROOT}/client/src/parsers/parseproac.c ${PM3_ROOT}/client/src/parsers/hrtparser/hrtparser.c ${PM3_ROOT}/client/src/pla.c ${PM3_ROOT}/client/src/preferences.c diff --git a/client/src/cmdhfmf.c b/client/src/cmdhfmf.c index 70e7401c7..b37506de7 100644 --- a/client/src/cmdhfmf.c +++ b/client/src/cmdhfmf.c @@ -46,6 +46,7 @@ #include "parsers/parsehid.h" #include "parsers/parsevigik.h" #include "parsers/parsehexact.h" +#include "parsers/parseproac.h" #include "generator.h" // keygens. #include "fpga.h" #include "mifare/mifarehost.h" @@ -890,6 +891,10 @@ static int mf_view_dump(uint8_t *dump, size_t bytes_read, uint16_t block_cnt, bo (void)hexact_parser_parse(dump, bytes_read); } + if (is_valid_proac_card(dump, bytes_read)) { + (void)proac_parser_parse(dump, bytes_read); + } + return PM3_SUCCESS; } diff --git a/client/src/parsers/parseproac.c b/client/src/parsers/parseproac.c new file mode 100644 index 000000000..779bee579 --- /dev/null +++ b/client/src/parsers/parseproac.c @@ -0,0 +1,159 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// PROAC PACS parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#include "parseproac.h" + +#include // isprint +#include + +#include "commonutil.h" // ARRAYLEN +#include "crc.h" // CRC8Mad +#include "ui.h" // PrintAndLogEx +#include "util.h" // sprint_hex_inrow, str_append, str_trim +#include "mifare/mad.h" +#include "mifare/mifaredefault.h" // MFBLOCK_SIZE +#include "mifare/mifare4.h" // mfFirstBlockOfSector + +#define PROAC_SCHED_SECTOR_A 1 +#define PROAC_SCHED_SECTOR_B 2 +#define PROAC_SCHED_HEADER 4 + +#define PROAC_LOT_SECTOR 14 +#define PROAC_LOT_BLOCK 1 + +#define PROAC_BLOB_SECTOR_A 12 +#define PROAC_BLOB_SECTOR_B 13 + +#define PROAC_TAIL_SECTOR 15 + +// three data blocks per sector, both schedule sectors, less the header +#define PROAC_SCHED_MAX ((2 * 3 * MFBLOCK_SIZE) - PROAC_SCHED_HEADER) + +static const uint8_t *proac_block(const uint8_t *dump, size_t dumplen, uint8_t sector, uint8_t block) { + size_t off = (mfFirstBlockOfSector(sector) + block) * MFBLOCK_SIZE; + if (off + MFBLOCK_SIZE > dumplen) { + return NULL; + } + return dump + off; +} + +bool is_valid_proac_card(const uint8_t *dump, size_t dumplen) { + if (dump == NULL || dumplen < sizeof(mad1_sector_t)) { + return false; + } + return (mad_find_aid((const mad1_sector_t *)dump, PROAC_MAD_AID) > -1); +} + +static size_t proac_schedule(const uint8_t *dump, size_t dumplen, char *out, size_t outlen) { + size_t n = 0; + const uint8_t sectors[2] = { PROAC_SCHED_SECTOR_A, PROAC_SCHED_SECTOR_B }; + + for (uint8_t s = 0; s < ARRAYLEN(sectors); s++) { + + for (uint8_t b = 0; b < 3; b++) { + + const uint8_t *p = proac_block(dump, dumplen, sectors[s], b); + if (p == NULL) { + return n; + } + + uint8_t start = (s == 0 && b == 0) ? PROAC_SCHED_HEADER : 0; + for (uint8_t k = start; k < MFBLOCK_SIZE; k++) { + + if (p[k] == 0x00) { + return n; + } + + if (n + 1 >= outlen) { + return n; + } + out[n++] = isprint(p[k]) ? (char)p[k] : '.'; + } + } + } + return n; +} + +int proac_parser_parse(const uint8_t *dump, size_t dumplen) { + + if (is_valid_proac_card(dump, dumplen) == false) { + return PM3_EINVARG; + } + + const mad1_sector_t *s0 = (const mad1_sector_t *)dump; + + PrintAndLogEx(INFO, ""); + PrintAndLogEx(INFO, "--- " _CYAN_("PROAC") " detected --------------------"); + PrintAndLogEx(INFO, "MAD AID............ " _YELLOW_("0x%04X") " on %d of %d sectors", + PROAC_MAD_AID, + mad_count_aid(s0, PROAC_MAD_AID), + MAD1_NUM_AIDS + ); + + // The one card this was written against carries a MAD whose CRC was never + // filled in, so a reader in this system cannot be validating it. Say so + // rather than silently trusting the directory. + uint8_t crc = CRC8Mad((uint8_t *)&s0->mad.info, sizeof(mad1_t) - 1); + if (crc != s0->mad.crc) { + PrintAndLogEx(INFO, "MAD CRC............ %02X, computed %02X ( " _RED_("invalid") " )", s0->mad.crc, crc); + } + + const uint8_t *hdr = proac_block(dump, dumplen, PROAC_SCHED_SECTOR_A, 0); + if (hdr) { + PrintAndLogEx(INFO, "Schedule header.... %s", sprint_hex_inrow(hdr, PROAC_SCHED_HEADER)); + } + + char sched[PROAC_SCHED_MAX + 1] = {0}; + if (proac_schedule(dump, dumplen, sched, sizeof(sched)) > 0) { + PrintAndLogEx(INFO, "Schedule........... " _YELLOW_("%s"), sched); + PrintAndLogEx(INFO, " written in clear, in a sector this card's own key can rewrite"); + } + + const uint8_t *lot = proac_block(dump, dumplen, PROAC_LOT_SECTOR, PROAC_LOT_BLOCK); + if (lot && isprint(lot[0])) { + char text[MFBLOCK_SIZE + 1] = {0}; + str_append(text, sizeof(text), "%.*s", MFBLOCK_SIZE, (const char *)lot); + str_trim(text); + PrintAndLogEx(INFO, "Lot................ " _YELLOW_("%s"), text); + } + + PrintAndLogEx(INFO, "Payload............ sector %u and %u, 96 bytes, " _YELLOW_("not decoded"), + PROAC_BLOB_SECTOR_A, + PROAC_BLOB_SECTOR_B + ); + + const uint8_t sectors[2] = { PROAC_BLOB_SECTOR_A, PROAC_BLOB_SECTOR_B }; + + for (uint8_t s = 0; s < ARRAYLEN(sectors); s++) { + for (uint8_t b = 0; b < 3; b++) { + const uint8_t *p = proac_block(dump, dumplen, sectors[s], b); + if (p) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", sectors[s], b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + } + } + } + + PrintAndLogEx(INFO, "Tail............... sector %u, " _YELLOW_("not decoded"), PROAC_TAIL_SECTOR); + for (uint8_t b = 0; b < 3; b++) { + const uint8_t *p = proac_block(dump, dumplen, PROAC_TAIL_SECTOR, b); + if (p) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", PROAC_TAIL_SECTOR, b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + } + } + return PM3_SUCCESS; +} diff --git a/client/src/parsers/parseproac.h b/client/src/parsers/parseproac.h new file mode 100644 index 000000000..3bfe1132f --- /dev/null +++ b/client/src/parsers/parseproac.h @@ -0,0 +1,33 @@ +//----------------------------------------------------------------------------- +// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// See LICENSE.txt for the text of the license. +//----------------------------------------------------------------------------- +// PROAC parser for MIFARE Classic dumps +//----------------------------------------------------------------------------- + +#ifndef PARSEPROAC_H__ +#define PARSEPROAC_H__ + +#include "common.h" + +// MAD application id, function cluster 0x49, the VIGIK cluster +#define PROAC_MAD_AID 0x4982 + +// True when the MAD claims a sector for the PROAC application +bool is_valid_proac_card(const uint8_t *dump, size_t dumplen); + +// Print what is known about the layout of a PROAC card +int proac_parser_parse(const uint8_t *dump, size_t dumplen); + +#endif diff --git a/client/src/parsers/parsevigik.c b/client/src/parsers/parsevigik.c index cf7cd4e89..e7dc5d79b 100644 --- a/client/src/parsers/parsevigik.c +++ b/client/src/parsers/parsevigik.c @@ -19,6 +19,7 @@ #include "parsevigik.h" #include +#include // isdigit #include #include "commonutil.h" @@ -396,16 +397,15 @@ int vigik_annotate(mfc_vigik_t *d) { } -// Key layouts of the VIGIK family building access systems. Every one of these -// ships the same keys on every card, so matching the whole set identifies the -// system outright - no guessing from the payload. Sourced from the schemas in -// armsrc/Standalone/hf_colin.c. Hexact is not here; it has its own parser. -typedef struct { +typedef struct vigik_schema_s { const char *name; uint64_t key_a[16]; uint64_t key_b[16]; + int (*decode)(const uint8_t *dump, size_t dumplen); } vigik_schema_t; +static int vigik_print_urmet_captiv(const uint8_t *dump, size_t dumplen); + static const vigik_schema_t vigik_schemas[] = { { "Noralsy", @@ -420,7 +420,8 @@ static const vigik_schema_t vigik_schemas[] = { 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL, 0x424c41524f4eULL - } + }, + NULL }, { "Urmet Captiv", @@ -435,7 +436,8 @@ static const vigik_schema_t vigik_schemas[] = { 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL, 0x8829da9daf76ULL - } + }, + vigik_print_urmet_captiv }, { "VIGIK service badge", @@ -450,7 +452,8 @@ static const vigik_schema_t vigik_schemas[] = { VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY, VIGIK_KEY_ANY - } + }, + NULL }, }; @@ -468,7 +471,7 @@ static uint64_t vigik_sector_key(const uint8_t *dump, size_t dumplen, uint8_t se return key; } -const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { +static const vigik_schema_t *vigik_detect_schema_entry(const uint8_t *dump, size_t dumplen) { if (dump == NULL) { return NULL; @@ -481,24 +484,98 @@ const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { for (uint8_t s = 0; s < 16 && match; s++) { - if (schema->key_a[s] != VIGIK_KEY_ANY && - schema->key_a[s] != vigik_sector_key(dump, dumplen, s, false)) { + if ((schema->key_a[s] != VIGIK_KEY_ANY) && (schema->key_a[s] != vigik_sector_key(dump, dumplen, s, false))) { match = false; } - if (schema->key_b[s] != VIGIK_KEY_ANY && - schema->key_b[s] != vigik_sector_key(dump, dumplen, s, true)) { + if ((schema->key_b[s] != VIGIK_KEY_ANY) && (schema->key_b[s] != vigik_sector_key(dump, dumplen, s, true))) { match = false; } } if (match) { - return schema->name; + return schema; } } return NULL; } +const char *vigik_detect_schema(const uint8_t *dump, size_t dumplen) { + const vigik_schema_t *schema = vigik_detect_schema_entry(dump, dumplen); + return (schema != NULL) ? schema->name : NULL; +} + +// --------------------------------------------------------------- Urmet Captiv +#define URMET_DIGITS_OFF 5 +#define URMET_DIGITS_LEN 11 + +static bool urmet_all_digits(const uint8_t *p, size_t len) { + for (size_t i = 0; i < len; i++) { + if (isdigit(p[i]) == 0) { + return false; + } + } + return true; +} + +static int vigik_print_urmet_captiv(const uint8_t *dump, size_t dumplen) { + + if (dumplen < MFBLOCK_SIZE * 3) { + return PM3_EINVARG; + } + + const uint8_t *b1 = dump + MFBLOCK_SIZE; + const uint8_t *b2 = dump + (2 * MFBLOCK_SIZE); + + PrintAndLogEx(INFO, "Header............. %s", sprint_hex_inrow(b1, URMET_DIGITS_OFF)); + + if (urmet_all_digits(b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN)) { + char num[URMET_DIGITS_LEN + 1] = {0}; + memcpy(num, b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN); + PrintAndLogEx(INFO, "Number............. " _YELLOW_("%s") "%s" + , num + , (strspn(num, "0") == URMET_DIGITS_LEN) ? " ( all zero, unset )" : "" + ); + } else { + PrintAndLogEx(INFO, "Number............. %s ( not ASCII digits )", sprint_hex_inrow(b1 + URMET_DIGITS_OFF, URMET_DIGITS_LEN)); + } + + PrintAndLogEx(INFO, "Marker............. %s", sprint_hex_inrow(b2, 2)); + PrintAndLogEx(INFO, "Data............... layout " _YELLOW_("not decoded")); + + int shown = 0; + for (uint8_t s = 1; s < 16; s++) { + + for (uint8_t b = 0; b < 3; b++) { + + size_t off = (mfFirstBlockOfSector(s) + b) * MFBLOCK_SIZE; + if (off + MFBLOCK_SIZE > dumplen) { + continue; + } + + const uint8_t *p = dump + off; + bool empty = true; + for (uint8_t k = 0; k < MFBLOCK_SIZE; k++) { + if (p[k]) { + empty = false; + break; + } + } + if (empty == false) { + PrintAndLogEx(INFO, " sector %2u blk %u.. %s", s, b, sprint_hex_inrow(p, MFBLOCK_SIZE)); + shown++; + } + } + } + + if (shown == 0) { + PrintAndLogEx(INFO, " every sector past 0 is empty, card not personalised"); + } + + PrintAndLogEx(INFO, "Keys............... one key r/w every block"); + return PM3_SUCCESS; +} + bool is_valid_vigik_card(const uint8_t *dump, size_t dumplen) { if (dump == NULL || dumplen < sizeof(mad1_sector_t)) { return false; @@ -525,10 +602,11 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { PrintAndLogEx(SUCCESS, "System............. " _YELLOW_("%s"), schema); } - // Only the deployments that publish a MAD can be taken apart any further. - // The others keep the same structure somewhere else on the card, and where - // that is has not been worked out, so say so instead of printing nonsense. if (DetectHID(s0, VIGIK_MAD_AID) < 0) { + const vigik_schema_t *entry = vigik_detect_schema_entry(dump, dumplen); + if (entry != NULL && entry->decode != NULL) { + return entry->decode(dump, dumplen); + } PrintAndLogEx(INFO, "No MAD on this card, structure " _YELLOW_("not decoded")); return PM3_SUCCESS; } @@ -540,8 +618,6 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { return res; } - // The VIGIK structure is the data blocks of sector 0 followed by the data - // blocks of every sector the MAD hands to the VIGIK application. union { uint8_t *bytes; mfc_vigik_t *vigik; @@ -558,8 +634,11 @@ int vigik_parser_parse(const uint8_t *dump, size_t dumplen) { dlen += MFBLOCK_SIZE * 3; for (size_t i = 0; i < mad_list.len; i++) { + if (VIGIK_MAD_AID == mad_list.entries[i].aid || VIGIK_MAD_AID_ALT == mad_list.entries[i].aid) { + uint32_t offset = mad_list.entries[i].sector * MFBLOCK_SIZE * 4; + if (offset + (MFBLOCK_SIZE * 3) > dumplen) { continue; }