proxmark3/armsrc/Standalone/hf_mfcsim.c

145 lines
4.7 KiB
C
Raw Normal View History

2021-08-07 22:19:08 +08:00
//-----------------------------------------------------------------------------
2021-08-08 16:11:57 +08:00
// Ray Lee, 2021
2021-08-07 22:19:08 +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 mifare classic simulator aka MFCSIM
//-----------------------------------------------------------------------------
#include <inttypes.h>
#include "ticks.h"
#include "proxmark3_arm.h"
#include "BigBuf.h"
#include "commonutil.h"
#include "fpgaloader.h"
#include "util.h"
#include "dbprint.h"
#include "spiffs.h"
#include "standalone.h" // standalone definitions
#include "appmain.h"
#include "string.h"
#include "iso14443a.h"
#include "mifarecmd.h"
#include "crc16.h"
#include "mifaresim.h" // mifare1ksim
2021-08-07 22:19:08 +08:00
#include "mifareutil.h"
/*
* `hf_mfcsim` simulates mifare classic 1k dumps uploaded to flash.
* It requires RDV4 hardware (for flash and battery).
*
* On entering stand-alone mode, this module will start simulating.
* Data is read from bin dump file uploaded to flash memory (hf_mfcsim_dump_xx.bin).
2021-08-07 22:19:08 +08:00
* Only support mifare classic 1k
*
* To upload input file (eml format) to flash:
* - mem spiffs upload -s <filename> -d hf_mfcsim_dump_xx.bin (Notes: xx is form 01 to 15)
2021-08-07 22:19:08 +08:00
* To delete the input file from flash:
* - mem spiffs remove -f hf_mfcsim_dump_xx.bin (Notes: xx is form 01 to 15)
2021-08-14 10:59:50 +08:00
*
2021-08-07 22:19:08 +08:00
*/
#define HF_MFCSIM_DUMPFILE_SIM "hf_mfcsim_dump_%02d.bin"
#define DUMP_SIZE 1024
2021-08-07 22:19:08 +08:00
static char cur_dump_file[22] = {0};
2021-08-07 22:19:08 +08:00
2021-08-14 10:59:50 +08:00
static bool fill_eml_from_file(char *dumpfile) {
// check file exist
2021-08-14 10:59:50 +08:00
if (!exists_in_spiffs(dumpfile)) {
Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
return false;
}
//check dumpfile size
uint32_t size = size_in_spiffs(dumpfile);
2021-08-14 10:59:50 +08:00
if (size != DUMP_SIZE) {
Dbprintf(_RED_("File Size: %dB The dump file size is incorrect! Only support Mifare Classic 1K! Please check it."));
BigBuf_free();
return false;
}
//read and load dump file
2021-08-22 05:02:27 +08:00
if (g_dbglevel >= DBG_INFO)
Dbprintf(_YELLOW_("Found dump file %s. Uploading to emulator memory..."), dumpfile);
emlClearMem();
uint8_t *emCARD = BigBuf_get_EM_addr();
rdv40_spiffs_read_as_filetype(dumpfile, emCARD, size, RDV40_SPIFFS_SAFETY_SAFE);
return true;
}
2021-08-14 10:59:50 +08:00
static bool write_file_from_eml(char *dumpfile) {
if (!exists_in_spiffs(dumpfile)) {
Dbprintf(_RED_("Dump file %s not found!"), dumpfile);
2021-08-07 22:19:08 +08:00
return false;
}
uint8_t *emCARD = BigBuf_get_EM_addr();
rdv40_spiffs_write(dumpfile, emCARD, DUMP_SIZE, RDV40_SPIFFS_SAFETY_SAFE);
return true;
2021-08-07 22:19:08 +08:00
}
2021-08-14 10:59:50 +08:00
void ModInfo(void) {
2021-08-07 22:19:08 +08:00
DbpString(_YELLOW_(" HF Mifare Classic simulation mode") " - a.k.a MFCSIM");
}
2021-08-14 10:59:50 +08:00
void RunMod(void) {
//initializing
2021-08-07 22:19:08 +08:00
StandAloneMode();
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
rdv40_spiffs_lazy_mount();
Dbprintf(_YELLOW_("Standalone mode MFCSIM started!"));
2021-08-07 22:19:08 +08:00
bool flag_has_dumpfile = false;
2021-08-14 10:59:50 +08:00
for (int i = 1;; i++) {
//Exit! usbcommand break
if (data_available()) break;
//Infinite loop
2021-08-14 10:59:50 +08:00
if (i > 15) {
if (!flag_has_dumpfile)
break; //still no dump file found
i = 1; //next loop
}
//Indicate which card will be simulated
LED(i, 0);
//Try to load dump form flash
sprintf(cur_dump_file, HF_MFCSIM_DUMPFILE_SIM, i);
Dbprintf(_YELLOW_("[Slot: %d] Try to load dump file: %s"), i, cur_dump_file);
2021-08-14 10:59:50 +08:00
if (!fill_eml_from_file(cur_dump_file)) {
Dbprintf(_YELLOW_("[Slot: %d] Dump load Failed, Next one!"), i);
LEDsoff();
continue;
}
flag_has_dumpfile = true;
2021-08-07 22:19:08 +08:00
//Exit! Button hold break
int button_pressed = BUTTON_HELD(500);
2021-08-14 10:59:50 +08:00
if (button_pressed == BUTTON_HOLD) {
Dbprintf("Button hold, Break!");
break;
}
//Hope there is enough time to see clearly
SpinDelay(500);
//Start to simulate
Dbprintf(_YELLOW_("[Slot: %d] Simulation start, Press button to change next card."), i);
uint16_t simflags = FLAG_UID_IN_EMUL | FLAG_MF_1K;
Mifare1ksim(simflags, 0, NULL, 0, 0);
Dbprintf(_YELLOW_("[Slot: %d] Simulation end, Write Back to dump file!"), i);
//Simulation end, Write Back
2021-08-14 10:59:50 +08:00
if (!write_file_from_eml(cur_dump_file)) {
Dbprintf(_RED_("[Slot: %d] Write Failed! Anyway, Change to next one!"), i);
continue;
}
Dbprintf(_YELLOW_("[Slot: %d] Write Success! Change to next one!"), i);
}
if (!flag_has_dumpfile)
Dbprintf("No dump file found!");
Dbprintf("Breaked! Exit standalone mode!");
2021-08-12 13:46:46 +08:00
SpinErr(15, 200, 3);
return;
2021-08-07 22:19:08 +08:00
}