2020-02-26 05:24:16 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2020-03-30 21:11:48 +08:00
|
|
|
// (c) Stefanie Hofmann, 2020
|
|
|
|
// (c) Uli Heilmeier, 2020
|
2020-02-26 05:24:16 +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.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// main code for Legic Prime read/sim
|
|
|
|
//-----------------------------------------------------------------------------
|
2020-03-30 21:11:48 +08:00
|
|
|
|
2020-02-26 05:24:16 +08:00
|
|
|
#include "standalone.h"
|
|
|
|
#include "proxmark3_arm.h"
|
2020-03-30 21:11:48 +08:00
|
|
|
#include "BigBuf.h"
|
2020-02-26 05:24:16 +08:00
|
|
|
#include "appmain.h"
|
|
|
|
#include "fpgaloader.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "dbprint.h"
|
|
|
|
#include "ticks.h"
|
|
|
|
#include "legicrf.h"
|
|
|
|
#include "legicrfsim.h"
|
2020-03-30 21:11:48 +08:00
|
|
|
#include "legic.h" // legic_card_select_t struct
|
2020-04-16 15:01:14 +08:00
|
|
|
#include "spiffs.h" // flashmem
|
2020-03-30 21:11:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To list all dump files from flash:
|
|
|
|
*
|
|
|
|
* 1. mem spiffs tree
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* To retrieve dump files from flash:
|
|
|
|
*
|
2021-04-16 06:58:07 +08:00
|
|
|
* 1. mem spiffs dump -s hf-legic-XXYYZZWW-dump.bin -d hf-legic-XXYYZZWW-dump.bin
|
2020-03-30 21:11:48 +08:00
|
|
|
* Copies log file from flash to your client.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This module emits debug strings during normal operation -- so try it out in
|
|
|
|
* the lab connected to PM3 client before taking it into the field.
|
|
|
|
*
|
|
|
|
* To delete a dump file from flash:
|
|
|
|
*
|
2021-03-09 21:38:31 +08:00
|
|
|
* 1. mem spiffs remove -f hf-legic-XXYYZZWW-dump.bin
|
2020-03-30 21:11:48 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-05-12 04:12:46 +08:00
|
|
|
#ifdef WITH_FLASH
|
2020-05-11 00:04:50 +08:00
|
|
|
static void DownloadLogInstructions(void) {
|
2020-03-30 21:11:48 +08:00
|
|
|
Dbprintf("");
|
|
|
|
Dbprintf("[=] List all dumps from flash:");
|
2020-04-22 08:22:55 +08:00
|
|
|
Dbprintf("[=] " _YELLOW_("-") " mem spiffs tree");
|
2020-03-30 21:11:48 +08:00
|
|
|
Dbprintf("");
|
|
|
|
Dbprintf("[=] To save a dump file from flash to client:");
|
2021-03-09 21:58:08 +08:00
|
|
|
Dbprintf("[=] " _YELLOW_("-") " mem spiffs dump -s hf-legic-UID-dump.bin -d hf-legic-UID-dump.bin");
|
2020-03-30 21:11:48 +08:00
|
|
|
}
|
2020-05-12 04:12:46 +08:00
|
|
|
#endif
|
2020-03-30 21:11:48 +08:00
|
|
|
|
2020-05-11 00:04:50 +08:00
|
|
|
static void save_dump_to_file(legic_card_select_t *p_card) {
|
2020-03-30 21:11:48 +08:00
|
|
|
|
|
|
|
#ifdef WITH_FLASH
|
|
|
|
|
|
|
|
// legic functions puts it memory in Emulator reserved memory.
|
|
|
|
uint8_t *mem = BigBuf_get_EM_addr();
|
|
|
|
|
2020-04-16 15:01:14 +08:00
|
|
|
char *preferredName = (char *)BigBuf_malloc(30);
|
2020-03-30 21:11:48 +08:00
|
|
|
if (preferredName == NULL) {
|
|
|
|
goto OUT;
|
|
|
|
}
|
2020-04-16 15:01:14 +08:00
|
|
|
|
2020-03-30 21:11:48 +08:00
|
|
|
sprintf(preferredName, "hf-legic-%02X%02X%02X%02X-dump", p_card->uid[0], p_card->uid[1], p_card->uid[2], p_card->uid[3]);
|
|
|
|
uint16_t preferredNameLen = strlen(preferredName);
|
|
|
|
|
2020-04-16 15:01:14 +08:00
|
|
|
char *filename = (char *)BigBuf_malloc(preferredNameLen + 4 + 1 + 10);
|
2020-03-30 21:11:48 +08:00
|
|
|
if (filename == NULL) {
|
|
|
|
goto OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(filename, "%.*s%s", preferredNameLen, preferredName, ".bin");
|
|
|
|
uint16_t num = 1;
|
|
|
|
while (exists_in_spiffs(filename)) {
|
|
|
|
sprintf(filename, "%.*s-%d%s", preferredNameLen, preferredName, num, ".bin");
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdv40_spiffs_write(filename, mem, p_card->cardsize, RDV40_SPIFFS_SAFETY_SAFE);
|
|
|
|
|
|
|
|
Dbprintf("[=] saved card dump to flashmem::" _YELLOW_("%s"), filename);
|
|
|
|
OUT:
|
|
|
|
BigBuf_free_keep_EM();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2020-02-26 05:24:16 +08:00
|
|
|
|
|
|
|
void ModInfo(void) {
|
2020-03-30 21:11:48 +08:00
|
|
|
DbpString(" HF Legic Prime standalone");
|
2020-02-26 05:24:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Searching for Legic card until found and read.
|
|
|
|
// Simulating recorded Legic Prime card.
|
|
|
|
// C = Searching
|
|
|
|
// A, B, C = Reading
|
|
|
|
// A, D = Simulating
|
|
|
|
|
2020-05-10 22:59:38 +08:00
|
|
|
void RunMod(void) {
|
2020-03-30 16:22:45 +08:00
|
|
|
StandAloneMode();
|
|
|
|
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
|
2020-03-30 21:11:48 +08:00
|
|
|
Dbprintf("[=] >> HF Legic Prime Read/Simulate Started <<");
|
|
|
|
DbpString("[=] press and HOLD button to exit standalone mode");
|
2020-09-07 03:42:50 +08:00
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
for (;;) {
|
|
|
|
WDT_HIT();
|
2020-04-16 15:01:14 +08:00
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
//exit from hf_legic, send usbcommand
|
|
|
|
if (data_available()) break;
|
2020-02-26 05:24:16 +08:00
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
//Was our button held down or pressed?
|
|
|
|
int button_pressed = BUTTON_HELD(280);
|
2020-03-30 21:11:48 +08:00
|
|
|
if (button_pressed == BUTTON_HOLD) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-26 05:24:16 +08:00
|
|
|
|
2020-03-30 21:11:48 +08:00
|
|
|
LEDsoff();
|
2020-03-30 16:22:45 +08:00
|
|
|
LED_C_ON();
|
2020-02-26 05:24:16 +08:00
|
|
|
|
2020-03-30 21:11:48 +08:00
|
|
|
DbpString("[=] looking for tags");
|
|
|
|
int read_success = PM3_ESOFT;
|
2020-02-26 05:24:16 +08:00
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
//search for legic card until reading successfull or button pressed
|
|
|
|
do {
|
|
|
|
LED_C_ON();
|
2020-03-30 21:11:48 +08:00
|
|
|
SpinDelay(500);
|
2020-03-30 16:22:45 +08:00
|
|
|
// We don't care if we read a MIM256, MIM512 or MIM1024
|
|
|
|
// we just read 1024 bytes
|
2020-03-30 21:11:48 +08:00
|
|
|
read_success = LegicRfReaderEx(0, 1024, 0x55);
|
|
|
|
|
|
|
|
} while (read_success == PM3_ESOFT && !BUTTON_PRESS());
|
|
|
|
|
|
|
|
LEDsoff();
|
2020-02-26 05:24:16 +08:00
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
//simulate if read successfully
|
2020-03-30 21:11:48 +08:00
|
|
|
if (read_success != PM3_ESOFT) {
|
2020-04-16 15:01:14 +08:00
|
|
|
|
2020-03-30 21:11:48 +08:00
|
|
|
legic_card_select_t *p_card;
|
|
|
|
p_card = getLegicCardInfo();
|
2020-04-16 15:01:14 +08:00
|
|
|
if (p_card->cardsize == 0)
|
2020-03-30 21:11:48 +08:00
|
|
|
continue;
|
2020-04-16 15:01:14 +08:00
|
|
|
|
2020-03-30 21:11:48 +08:00
|
|
|
save_dump_to_file(p_card);
|
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
LED_D_ON();
|
2020-03-30 21:11:48 +08:00
|
|
|
uint8_t ct;
|
2020-04-16 15:01:14 +08:00
|
|
|
switch (p_card->tagtype) {
|
|
|
|
case 0x0D:
|
2020-03-30 21:11:48 +08:00
|
|
|
ct = 0;
|
|
|
|
break;
|
|
|
|
case 0x1D:
|
|
|
|
ct = 1;
|
|
|
|
break;
|
|
|
|
case 0x3D:
|
|
|
|
ct = 2;
|
|
|
|
break;
|
2020-04-16 15:01:14 +08:00
|
|
|
default:
|
2020-03-30 21:11:48 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-30 16:22:45 +08:00
|
|
|
// The read data is migrated to a MIM1024 card
|
2020-09-07 03:42:50 +08:00
|
|
|
LegicRfSimulate(ct, false);
|
2020-03-30 16:22:45 +08:00
|
|
|
}
|
2020-02-26 05:24:16 +08:00
|
|
|
}
|
2020-03-30 21:11:48 +08:00
|
|
|
|
|
|
|
LEDsoff();
|
|
|
|
#ifdef WITH_FLASH
|
|
|
|
DownloadLogInstructions();
|
|
|
|
#endif
|
|
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
2020-02-26 05:24:16 +08:00
|
|
|
}
|