Add new action in nfc-mftool. Its now possible to extract payload (data blocks) from a MFD

Fix minor bug message in nfc-mftool.
This commit is contained in:
Romuald Conty 2009-10-26 13:37:12 +00:00
parent 1a72b81065
commit 30835317c2

View file

@ -272,39 +272,92 @@ bool write_card()
return true;
}
int main(int argc, const char* argv[])
void mifare_classic_extract_payload(const char* abDump, char* pbPayload)
{
bool b4K;
bool bReadAction;
byte_t* pbtUID;
FILE* pfKeys = NULL;
FILE* pfDump = NULL;
uint8_t uiSectorIndex;
uint8_t uiBlockIndex;
size_t szDumpOffset;
size_t szPayloadIndex = 0;
if (argc < 4)
{
for(uiSectorIndex=1; uiSectorIndex<16; uiSectorIndex++) {
for(uiBlockIndex=0; uiBlockIndex<3; uiBlockIndex++) {
szDumpOffset = uiSectorIndex*16*4 + uiBlockIndex*16;
// for(uint8_t uiByteIndex=0; uiByteIndex<16; uiByteIndex++) printf("%02x ", abDump[szPayloadIndex+uiByteIndex]);
memcpy(pbPayload+szPayloadIndex, abDump+szDumpOffset, 16);
szPayloadIndex += 16;
}
}
}
typedef enum {
ACTION_READ,
ACTION_WRITE,
ACTION_EXPLAIN,
ACTION_EXTRACT
} action_t;
void print_usage(const char* pcProgramName)
{
printf("\n");
printf("%s r|w a|b <dump.mfd> [<keys.mfd>]\n", argv[0]);
printf("%s r|w a|b <dump.mfd> [<keys.mfd>]\n", pcProgramName);
printf("\n");
printf("r|w - Perform read from (r) or write to (w) card\n");
printf("a|b - Use A or B keys for action\n");
printf("<dump.mfd> - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n");
printf("<keys.mfd> - MiFare Dump (MFD) that contain the keys (optional)\n");
printf("\n");
return 1;
}
}
int main(int argc, const char* argv[])
{
bool b4K;
action_t atAction;
byte_t* pbtUID;
FILE* pfKeys = NULL;
FILE* pfDump = NULL;
// printf("Checking arguments and settings\n");
bReadAction = (tolower(*(argv[1])) == 'r');
if(argc < 2)
{
print_usage(argv[0]);
return 1;
}
const char* command = argv[1];
if(strcmp(command, "r") == 0)
{
atAction = ACTION_READ;
bUseKeyA = (tolower(*(argv[2])) == 'a');
bUseKeyFile = (argc > 4);
} else if(strcmp(command, "w") == 0)
{
atAction = ACTION_WRITE;
bUseKeyA = (tolower(*(argv[2])) == 'a');
bUseKeyFile = (argc > 4);
} else if(strcmp(command, "e") == 0)
{
atAction = ACTION_EXPLAIN;
} else if(strcmp(command, "x") == 0)
{
atAction = ACTION_EXTRACT;
}
switch(atAction) {
case ACTION_READ:
case ACTION_WRITE:
if (argc < 4)
{
print_usage(argv[0]);
return 1;
}
if (bUseKeyFile)
{
pfKeys = fopen(argv[4],"rb");
if (pfKeys == NULL)
{
printf("Could not open keys file: %s\n",argv[3]);
printf("Could not open keys file: %s\n",argv[4]);
return 1;
}
if (fread(&mtKeys,1,sizeof(mtKeys),pfKeys) != sizeof(mtKeys))
@ -316,8 +369,7 @@ int main(int argc, const char* argv[])
fclose(pfKeys);
}
if (bReadAction)
{
if(atAction == ACTION_READ) {
memset(&mtDump,0x00,sizeof(mtDump));
} else {
pfDump = fopen(argv[3],"rb");
@ -397,7 +449,7 @@ int main(int argc, const char* argv[])
uiBlocks = (b4K)?0xff:0x3f;
if (bReadAction)
if (atAction == ACTION_READ)
{
if (read_card())
{
@ -420,6 +472,47 @@ int main(int argc, const char* argv[])
}
nfc_disconnect(pdi);
break;
case ACTION_EXTRACT: {
const char* pcDump = argv[2];
const char* pcPayload = argv[3];
FILE* pfDump = NULL;
FILE* pfPayload = NULL;
char abDump[4096];
char abPayload[4096];
pfDump = fopen(pcDump,"rb");
if (pfDump == NULL)
{
printf("Could not open dump file: %s\n",pcDump);
return 1;
}
if (fread(abDump,1,sizeof(abDump),pfDump) != sizeof(abDump))
{
printf("Could not read dump file: %s\n",pcDump);
fclose(pfDump);
return 1;
}
fclose(pfDump);
mifare_classic_extract_payload(abDump, abPayload);
printf("Writing data to file: %s\n",pcPayload);
pfPayload = fopen(pcPayload,"wb");
if (fwrite(abPayload,1,sizeof(abPayload),pfPayload) != sizeof(abPayload))
{
printf("Could not write to file: %s\n",pcPayload);
return 1;
}
fclose(pfPayload);
printf("Done, all bytes have been extracted!\n");
}
};
return 0;
}