From e183b307d62288b54ac88f590ba2beb3c9a524f9 Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 11 Jul 2016 14:35:11 +0200 Subject: [PATCH 1/4] -K : Don't use the default keys. --- configure.ac | 0 src/mfoc.c | 23 ++++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) mode change 100644 => 100755 configure.ac diff --git a/configure.ac b/configure.ac old mode 100644 new mode 100755 diff --git a/src/mfoc.c b/src/mfoc.c index 32ef6f7..189e5d6 100644 --- a/src/mfoc.c +++ b/src/mfoc.c @@ -82,6 +82,7 @@ int main(int argc, char *const argv[]) // Next default key specified as option (-k) uint8_t *defKeys = NULL, *p; size_t defKeys_len = 0; + bool useDefaultKey = true; // Array with default Mifare Classic keys uint8_t defaultKeys[][6] = { @@ -129,7 +130,7 @@ int main(int argc, char *const argv[]) struct slre_cap caps[2]; // Parse command line arguments - while ((ch = getopt(argc, argv, "hD:s:BP:T:S:O:k:t:f:")) != -1) { + while ((ch = getopt(argc, argv, "hD:s:BP:T:S:O:k:K:t:f:")) != -1) { switch (ch) { case 'P': // Number of probes @@ -191,6 +192,21 @@ int main(int argc, char *const argv[]) fprintf(stdout, "The custom key 0x%012llx has been added to the default keys\n", bytes_to_num(defKeys + defKeys_len, 6)); defKeys_len = defKeys_len + 6; + break; + case 'K' : + // Add this key to the default keys + p = realloc(defKeys, defKeys_len + 6); + if (!p) { + ERR("Cannot allocate memory for defKeys"); + exit(EXIT_FAILURE); + } + defKeys = p; + memset(defKeys + defKeys_len, 0, 6); + num_to_bytes(strtoll(optarg, NULL, 16), 6, defKeys + defKeys_len); + fprintf(stdout, "The custom key 0x%012llx has been added to the default keys\n", bytes_to_num(defKeys + defKeys_len, 6)); + defKeys_len = defKeys_len + 6; + useDefaultKey = false; + break; case 'O': // File output @@ -324,6 +340,11 @@ int main(int argc, char *const argv[]) memcpy(mp.mpa.abtAuthUid, t.nt.nti.nai.abtUid + t.nt.nti.nai.szUidLen - 4, sizeof(mp.mpa.abtAuthUid)); // Iterate over all keys (n = number of keys) n = sizeof(defaultKeys) / sizeof(defaultKeys[0]); + + if (!useDefaultKey) { + n -= 13; + } + size_t defKey_bytes_todo = defKeys_len; key = 0; while (key < n || defKey_bytes_todo) { From 46f4769355f32c1d656be387122a8ff9b2acc96f Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 11 Jul 2016 14:45:44 +0200 Subject: [PATCH 2/4] -F : Like -f but don't use the default keys. --- src/mfoc.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/mfoc.c b/src/mfoc.c index 189e5d6..24aba8e 100644 --- a/src/mfoc.c +++ b/src/mfoc.c @@ -130,7 +130,7 @@ int main(int argc, char *const argv[]) struct slre_cap caps[2]; // Parse command line arguments - while ((ch = getopt(argc, argv, "hD:s:BP:T:S:O:k:K:t:f:")) != -1) { + while ((ch = getopt(argc, argv, "hD:s:BP:T:S:O:k:K:t:f:F:")) != -1) { switch (ch) { case 'P': // Number of probes @@ -178,7 +178,40 @@ int main(int argc, char *const argv[]) } if (line) free(line); - break; + break; + case 'F': + + if (!(fp = fopen(optarg, "r"))) { + fprintf(stderr, "Cannot open keyfile: %s, exiting\n", optarg); + exit(EXIT_FAILURE); + } + while ((read = getline(&line, &len, fp)) != -1) { + int i, j = 0, str_len = strlen(line); + + while (j < str_len && + (i = slre_match(regex, line + j, str_len - j, caps, 500, 1)) > 0) { + //We've found a key, let's add it to the structure. + p = realloc(defKeys, defKeys_len + 6); + if (!p) { + ERR("Cannot allocate memory for defKeys"); + exit(EXIT_FAILURE); + } + defKeys = p; + memset(defKeys + defKeys_len, 0, 6); + num_to_bytes(strtoll(caps[0].ptr, NULL, 16), 6, defKeys + defKeys_len); + fprintf(stdout, "The custom key 0x%.*s has been added to the default keys\n", caps[0].len, caps[0].ptr); + defKeys_len = defKeys_len + 6; + + j += i; + } + } + if (line) { + free(line); + } + + useDefaultKey = false; + + break; case 'k': // Add this key to the default keys p = realloc(defKeys, defKeys_len + 6); @@ -342,7 +375,7 @@ int main(int argc, char *const argv[]) n = sizeof(defaultKeys) / sizeof(defaultKeys[0]); if (!useDefaultKey) { - n -= 13; + n -= defKeys_len; } size_t defKey_bytes_todo = defKeys_len; From 54c524f3a0747b2192f52985604e40b2eadd647f Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 11 Jul 2016 14:52:43 +0200 Subject: [PATCH 3/4] usage update with the two new options --- src/mfoc.1 | 3 +++ src/mfoc.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/mfoc.1 b/src/mfoc.1 index f59d0eb..2baa88f 100644 --- a/src/mfoc.1 +++ b/src/mfoc.1 @@ -25,6 +25,9 @@ Show summary of options. \fB\-k\fP \fIKEY\fR Initially try KEY in addition to the default keys. .TP +\fB\-K\fP \fIKEY\fR +Initially try KEY without the default keys. +.T \fB\-O\fP \fIFILE\fR Dump card contents to FILE. .TP diff --git a/src/mfoc.c b/src/mfoc.c index 24aba8e..5c0e312 100644 --- a/src/mfoc.c +++ b/src/mfoc.c @@ -767,7 +767,9 @@ void usage(FILE *stream, int errno) fprintf(stream, " h print this help and exit\n"); // fprintf(stream, " B instead of 'A' dump 'B' keys\n"); fprintf(stream, " k try the specified key in addition to the default keys\n"); + fprintf(stream, " K Like -k option but without the default keys\n"); fprintf(stream, " f parses a file of keys to add in addition to the default keys \n"); + fprintf(stream, " F Like -f option but without the default keys \n"); // fprintf(stream, " D number of distance probes, default is 20\n"); // fprintf(stream, " S number of sets with keystreams, default is 5\n"); fprintf(stream, " P number of probes per sector, instead of default of 20\n"); From e146374db3bddcee494a61142108906547f5b4f3 Mon Sep 17 00:00:00 2001 From: Yoann Date: Mon, 11 Jul 2016 14:58:29 +0200 Subject: [PATCH 4/4] usage update with the two new options --- src/mfoc.1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mfoc.1 b/src/mfoc.1 index 2baa88f..41e2a72 100644 --- a/src/mfoc.1 +++ b/src/mfoc.1 @@ -27,7 +27,7 @@ Initially try KEY in addition to the default keys. .TP \fB\-K\fP \fIKEY\fR Initially try KEY without the default keys. -.T +.TP \fB\-O\fP \fIFILE\fR Dump card contents to FILE. .TP