Compare commits
13 Commits
mfoc-0.10
...
mfoc-0.10.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
658d77ec0d | ||
|
|
a13df02f79 | ||
|
|
001049599f | ||
|
|
a978ac16a9 | ||
|
|
1c2ff2b263 | ||
|
|
d1c676b01d | ||
|
|
9e636c9885 | ||
|
|
ef75599d75 | ||
|
|
e076683dea | ||
|
|
4d1ce73772 | ||
|
|
dafa6e42db | ||
|
|
c973c3cd81 | ||
|
|
6a3977545e |
90
ChangeLog
90
ChangeLog
@@ -0,0 +1,90 @@
|
||||
2011-05-18 09:18 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: improve tests made before running and show tag
|
||||
info using print_nfc_iso14443a_info()
|
||||
|
||||
2011-04-08 15:19 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: try to disconnect() the device on error.
|
||||
|
||||
2011-04-08 10:05 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: show errors then exit on mf_configure()
|
||||
|
||||
2011-04-08 09:32 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: apply a patch suggested by Valentijn Sessink.
|
||||
See Issue 56.
|
||||
|
||||
2011-04-08 09:17 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: minors fixes and indent.
|
||||
|
||||
2011-04-08 08:54 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: show error (using nfc_perror) then exit if some
|
||||
nfc_* functions failed on init.
|
||||
|
||||
2011-04-08 08:35 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: minor debug improvements.
|
||||
|
||||
2011-04-04 12:28 rconty@il4p.fr
|
||||
|
||||
* configure.ac, src/mfoc.c: mfoc: minor fixes/enhancements and
|
||||
version bumping
|
||||
|
||||
2011-04-04 10:38 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c, src/mifare.c: mfoc: hide authentication errors
|
||||
|
||||
2011-04-04 10:01 rconty@il4p.fr
|
||||
|
||||
* src/mifare.c, src/mifare.h, src/nfc-utils.c, src/nfc-utils.h:
|
||||
mfoc: sync nfc-utils.h/c and mifare.c/h with libnfc's ones.
|
||||
|
||||
2011-02-21 16:26 rtartiere@il4p.fr
|
||||
|
||||
* src/Makefile.am: mfox: Unbreak autotools on FreeBSD.
|
||||
|
||||
2011-02-02 10:46 rconty@il4p.fr
|
||||
|
||||
* src/mfoc.c: mfoc: use strtoll() function in order to retrieve
|
||||
64bits wide value. (Fixes Issue 55)
|
||||
|
||||
2010-11-18 11:20 rconty@il4p.fr
|
||||
|
||||
* configure.ac: mfoc: bump package version
|
||||
|
||||
2010-11-18 11:18 rconty@il4p.fr
|
||||
|
||||
* src/nfc-utils.c, src/nfc-utils.h: mfoc: sync nfc-utils.* from
|
||||
libnfc
|
||||
|
||||
2010-11-02 09:36 rconty@il4p.fr
|
||||
|
||||
* configure.ac, src/mfoc.c, src/mfoc.h, src/nfc-utils.c,
|
||||
src/nfc-utils.h: mfoc: upgrade code to work with develoment
|
||||
version of libnfc (upcomming 1.4.0)
|
||||
Update code to match with the new API;
|
||||
Sync nfc-utils.[ch] from libnfc's repo;
|
||||
Update ./configure to detect libnfc 1.4.0;
|
||||
|
||||
2010-09-14 09:38 rconty@il4p.fr
|
||||
|
||||
* ., AUTHORS, Makefile.in, aclocal.m4, autogen.sh, config.h,
|
||||
config.h.in, configure, configure.ac, depcomp, install-sh,
|
||||
missing, src, src/Makefile.am, src/mfoc.c, src/mifare.c,
|
||||
src/mifare.h, src/nfc-utils.c, src/nfc-utils.h: mfoc: update code
|
||||
in order to use libnfc 1.3.9, minor clean up, and minor
|
||||
enhancements.
|
||||
|
||||
2010-09-14 09:34 rconty@il4p.fr
|
||||
|
||||
* ., AUTHORS, COPYING, ChangeLog, INSTALL, Makefile.am,
|
||||
Makefile.in, NEWS, README, TODO, aclocal.m4, autogen.sh,
|
||||
config.h, config.h.in, configure, configure.ac, depcomp,
|
||||
install-sh, missing, src, src/Makefile.am, src/crapto1.c,
|
||||
src/crapto1.h, src/crypto1.c, src/mfoc.c, src/mfoc.h: Import MFOC
|
||||
0.08 from http://www.nethemba.com/mfoc.tar.bz2 on 13th Sept 2010
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
AC_INIT([mfoc], [0.10], [mifare@nethemba.com])
|
||||
AC_INIT([mfoc], [0.10.2], [mifare@nethemba.com])
|
||||
|
||||
AC_CONFIG_MACRO_DIR([m4])
|
||||
|
||||
@@ -13,7 +13,7 @@ AM_INIT_AUTOMAKE
|
||||
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
|
||||
|
||||
# Checks for pkg-config modules.
|
||||
LIBNFC_REQUIRED_VERSION=1.4.0
|
||||
LIBNFC_REQUIRED_VERSION=1.4.2
|
||||
PKG_CHECK_MODULES([LIBNFC], [libnfc >= $LIBNFC_REQUIRED_VERSION], [], [AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.])])
|
||||
|
||||
PKG_CONFIG_REQUIRES="libnfc"
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
AM_CFLAGS = @LIBNFC_CFLAGS@
|
||||
AM_LDFLAGS = @LIBNFC_LIBS@
|
||||
|
||||
bin_PROGRAMS = mfoc
|
||||
|
||||
noinst_HEADERS = crapto1.h mfoc.h mifare.h nfc-utils.h
|
||||
mfoc_SOURCES = crapto1.c crypto1.c mfoc.c mifare.c nfc-utils.c
|
||||
mfoc_LDADD = @LIBNFC_LIBS@
|
||||
|
||||
# dist_man_MANS = mfoc.1
|
||||
|
||||
229
src/mfoc.c
229
src/mfoc.c
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
|
||||
Mifare Classic Offline Cracker version 0.08
|
||||
Mifare Classic Offline Cracker
|
||||
|
||||
Requirements: crapto1 library http://code.google.com/p/crapto1
|
||||
libnfc http://www.libnfc.org
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
Porting to libnfc 1.3.3: Michal Boska <boska.michal@gmail.com>
|
||||
Porting to libnfc 1.3.9: Romuald Conty <romuald@libnfc.org>
|
||||
Porting to libnfc 1.4.x: Romuald Conty <romuald@libnfc.org>
|
||||
|
||||
URL http://eprint.iacr.org/2009/137.pdf
|
||||
URL http://www.sos.cs.ru.nl/applications/rfid/2008-esorics.pdf
|
||||
@@ -29,6 +30,8 @@
|
||||
URL http://www.cs.ru.nl/~petervr/papers/grvw_2009_pickpocket.pdf
|
||||
*/
|
||||
|
||||
/* vim: set ts=2 sw=2 et: */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
@@ -47,6 +50,11 @@
|
||||
#include "mfoc.h"
|
||||
|
||||
int main(int argc, char * const argv[]) {
|
||||
const nfc_modulation_t nm = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106,
|
||||
};
|
||||
|
||||
int ch, i, k, n, j, m, o;
|
||||
int key, block;
|
||||
int succeed = 1;
|
||||
@@ -106,27 +114,27 @@ int main(int argc, char * const argv[]) {
|
||||
case 'P':
|
||||
// Number of probes
|
||||
if (!(probes = atoi(optarg)) || probes < 1) {
|
||||
fprintf(stderr, "The number of probes must be a positive number\n");
|
||||
exit(1);
|
||||
ERR ("The number of probes must be a positive number");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stdout, "Number of probes: %d\n", probes);
|
||||
break;
|
||||
case 'T':
|
||||
// Nonce tolerance range
|
||||
if (!(d.tolerance = atoi(optarg)) || d.tolerance < 0) {
|
||||
fprintf(stderr, "The nonce distances range must be a zero or a positive number\n");
|
||||
exit(1);
|
||||
ERR ("The nonce distances range must be a zero or a positive number");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stdout, "Tolerance number: %d\n", probes);
|
||||
break;
|
||||
case 'k':
|
||||
// Add this key to the default keys list
|
||||
if ((defKey = calloc(6, sizeof(byte_t))) == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory for defKey\n");
|
||||
exit(1);
|
||||
ERR ("Cannot allocate memory for defKey");
|
||||
exit (EXIT_FAILURE);
|
||||
} else {
|
||||
bzero(defKey, 6);
|
||||
num_to_bytes(strtol(optarg, NULL, 16), 6, defKey);
|
||||
num_to_bytes(strtoll(optarg, NULL, 16), 6, defKey);
|
||||
memcpy(defaultKeys[0], defKey, 6);
|
||||
}
|
||||
fprintf(stdout, "The custom key 0x%012llx has been added to the default keys\n", bytes_to_num(defKey, 6));
|
||||
@@ -136,7 +144,7 @@ int main(int argc, char * const argv[]) {
|
||||
// File output
|
||||
if (!(pfDump = fopen(optarg, "wb"))) {
|
||||
fprintf(stderr, "Cannot open: %s, exiting\n", optarg);
|
||||
exit(1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stdout, "Output file: %s\n", optarg);
|
||||
break;
|
||||
@@ -150,17 +158,59 @@ int main(int argc, char * const argv[]) {
|
||||
}
|
||||
|
||||
if (!pfDump) {
|
||||
fprintf(stderr, "Error, parameter -O is mandatory\n");
|
||||
exit(1);
|
||||
ERR ("parameter -O is mandatory");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Initialize reader/tag structures
|
||||
mf_init(&t, &r);
|
||||
// Configure reader settings
|
||||
mf_configure(r.pdi);
|
||||
mf_select_tag(r.pdi, &(t.nt));
|
||||
|
||||
if (!nfc_initiator_init (r.pdi)) {
|
||||
nfc_perror (r.pdi, "nfc_initiator_init");
|
||||
goto error;
|
||||
}
|
||||
// Drop the field for a while, so can be reset
|
||||
if (!nfc_configure(r.pdi, NDO_ACTIVATE_FIELD, true)) {
|
||||
nfc_perror (r.pdi, "nfc_configure activate field");
|
||||
goto error;
|
||||
}
|
||||
// Let the reader only try once to find a tag
|
||||
if (!nfc_configure(r.pdi, NDO_INFINITE_SELECT, false)) {
|
||||
nfc_perror (r.pdi, "nfc_configure infinite select");
|
||||
goto error;
|
||||
}
|
||||
// Configure the CRC and Parity settings
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
|
||||
nfc_perror (r.pdi, "nfc_configure crc");
|
||||
goto error;
|
||||
}
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
|
||||
nfc_perror (r.pdi, "nfc_configure parity");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/*
|
||||
// wait for tag to appear
|
||||
for (i=0;!nfc_initiator_select_passive_target(r.pdi, nm, NULL, 0, &t.nt) && i < 10; i++) zsleep (100);
|
||||
*/
|
||||
|
||||
// mf_select_tag(r.pdi, &(t.nt));
|
||||
if (!nfc_initiator_select_passive_target (r.pdi, nm, NULL, 0, &t.nt)) {
|
||||
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Save tag uid and info about block size (b4K)
|
||||
// Test if a compatible MIFARE tag is used
|
||||
if ((t.nt.nti.nai.btSak & 0x08) == 0) {
|
||||
ERR ("only Mifare Classic is supported");
|
||||
goto error;
|
||||
}
|
||||
|
||||
// TODO: Support Mifare Classic with 7 bytes UID ?
|
||||
if (t.nt.nti.nai.szUidLen != 4) {
|
||||
ERR ("only Mifare Classic with UID on 4 bytes are supported");
|
||||
}
|
||||
// Save tag's block size (b4K)
|
||||
t.b4K = (t.nt.nti.nai.abtAtqa[1] == 0x02);
|
||||
t.uid = (uint32_t) bytes_to_num(t.nt.nti.nai.abtUid, 4);
|
||||
|
||||
@@ -169,16 +219,16 @@ int main(int argc, char * const argv[]) {
|
||||
|
||||
t.sectors = (void *) calloc(t.num_sectors, sizeof(sector));
|
||||
if (t.sectors == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory for t.sectors\n");
|
||||
exit(1);
|
||||
ERR ("Cannot allocate memory for t.sectors");
|
||||
goto error;
|
||||
}
|
||||
if ((pk = (void *) malloc(sizeof(pKeys))) == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory for pk\n");
|
||||
exit(1);
|
||||
ERR ("Cannot allocate memory for pk");
|
||||
goto error;
|
||||
}
|
||||
if ((bk = (void *) malloc(sizeof(bKeys))) == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory for bk\n");
|
||||
exit(1);
|
||||
ERR ("Cannot allocate memory for bk");
|
||||
goto error;
|
||||
} else {
|
||||
bk->brokenKeys = NULL;
|
||||
bk->size = 0;
|
||||
@@ -186,23 +236,16 @@ int main(int argc, char * const argv[]) {
|
||||
|
||||
d.distances = (void *) calloc(d.num_distances, sizeof(u_int32_t));
|
||||
if (d.distances == NULL) {
|
||||
fprintf(stderr, "Cannot allocate memory for t.distances\n");
|
||||
exit(1);
|
||||
ERR ("Cannot allocate memory for t.distances");
|
||||
goto error;
|
||||
}
|
||||
|
||||
// Test if a compatible MIFARE tag is used
|
||||
if ((t.nt.nti.nai.btSak & 0x08) == 0) {
|
||||
printf("Error: inserted tag is not a MIFARE Classic card\n");
|
||||
nfc_disconnect(r.pdi);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Initialize t.sectors, keys are not known yet
|
||||
for (i = 0; i < (t.num_sectors); ++i) {
|
||||
t.sectors[i].foundKeyA = t.sectors[i].foundKeyB = false;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Found MIFARE Classic %cK card with uid: %08x\n", (t.b4K ? '4' : '1'), t.uid);
|
||||
print_nfc_iso14443a_info (t.nt.nti.nai, true);
|
||||
|
||||
// Try to authenticate to all sectors with default keys
|
||||
// Set the authentication information (uid)
|
||||
@@ -371,8 +414,8 @@ int main(int argc, char * const argv[]) {
|
||||
}
|
||||
// We haven't found any key, exiting
|
||||
if ((dumpKeysA && !t.sectors[j].foundKeyA) || (!dumpKeysA && !t.sectors[j].foundKeyB)) {
|
||||
fprintf(stderr, "No success, maybe you should increase the probes\n");
|
||||
exit(1);
|
||||
ERR ("No success, maybe you should increase the probes");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,7 +442,7 @@ int main(int argc, char * const argv[]) {
|
||||
// Try A key, auth() + read()
|
||||
memcpy(mp.mpa.abtKey, t.sectors[i].KeyA, sizeof(t.sectors[i].KeyA));
|
||||
if (!nfc_initiator_mifare_cmd(r.pdi, MC_AUTH_A, block, &mp)) {
|
||||
// fprintf(stderr, "Error: Auth A\n");
|
||||
// ERR ("Error: Auth A");
|
||||
mf_configure(r.pdi);
|
||||
mf_anticollision(t, r);
|
||||
} else { // and Read
|
||||
@@ -411,12 +454,12 @@ int main(int argc, char * const argv[]) {
|
||||
failure = false;
|
||||
} else {
|
||||
// Error, now try read() with B key
|
||||
// fprintf(stderr, "Error: Read A\n");
|
||||
// ERR ("Error: Read A");
|
||||
mf_configure(r.pdi);
|
||||
mf_anticollision(t, r);
|
||||
memcpy(mp.mpa.abtKey, t.sectors[i].KeyB, sizeof(t.sectors[i].KeyB));
|
||||
if (!nfc_initiator_mifare_cmd(r.pdi, MC_AUTH_B, block, &mp)) {
|
||||
// fprintf(stderr, "Error: Auth B\n");
|
||||
// ERR ("Error: Auth B");
|
||||
mf_configure(r.pdi);
|
||||
mf_anticollision(t, r);
|
||||
} else { // and Read
|
||||
@@ -429,7 +472,7 @@ int main(int argc, char * const argv[]) {
|
||||
} else {
|
||||
mf_configure(r.pdi);
|
||||
mf_anticollision(t, r);
|
||||
// fprintf(stderr, "Error: Read B\n");
|
||||
// ERR ("Error: Read B");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -447,7 +490,7 @@ int main(int argc, char * const argv[]) {
|
||||
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
|
||||
fprintf(stdout, "Error, cannot write dump\n");
|
||||
fclose(pfDump);
|
||||
exit(1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
fclose(pfDump);
|
||||
}
|
||||
@@ -461,7 +504,10 @@ int main(int argc, char * const argv[]) {
|
||||
|
||||
// Disconnect device and exit
|
||||
nfc_disconnect(r.pdi);
|
||||
return 0;
|
||||
exit (EXIT_SUCCESS);
|
||||
error:
|
||||
nfc_disconnect(r.pdi);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void usage(FILE * stream, int errno) {
|
||||
@@ -488,22 +534,40 @@ void mf_init(mftag *t, mfreader *r) {
|
||||
// Connect to the first NFC device
|
||||
r->pdi = nfc_connect(NULL);
|
||||
if (!r->pdi) {
|
||||
fprintf(stderr, "!Error connecting to the NFC reader\n");
|
||||
exit(1);
|
||||
ERR ("Unable to connect to NFC device\n");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_configure(nfc_device_t* pdi) {
|
||||
nfc_initiator_init(pdi);
|
||||
if (!nfc_initiator_init (pdi)) {
|
||||
nfc_perror (pdi, "nfc_initiator_init");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// Drop the field for a while, so can be reset
|
||||
nfc_configure(pdi,NDO_ACTIVATE_FIELD,false);
|
||||
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, false)) {
|
||||
nfc_perror (pdi, "nfc_configure activate field");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// Let the reader only try once to find a tag
|
||||
nfc_configure(pdi,NDO_INFINITE_SELECT,false);
|
||||
if (!nfc_configure(pdi, NDO_INFINITE_SELECT, false)) {
|
||||
nfc_perror (pdi, "nfc_configure infinite select");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// Configure the CRC and Parity settings
|
||||
nfc_configure(pdi,NDO_HANDLE_CRC,true);
|
||||
nfc_configure(pdi,NDO_HANDLE_PARITY,true);
|
||||
if (!nfc_configure(pdi, NDO_HANDLE_CRC, true)) {
|
||||
nfc_perror (pdi, "nfc_configure crc");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (!nfc_configure(pdi, NDO_HANDLE_PARITY, true)) {
|
||||
nfc_perror (pdi, "nfc_configure parity");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// Enable the field so more power consuming cards can power themselves up
|
||||
nfc_configure(pdi,NDO_ACTIVATE_FIELD,true);
|
||||
if (!nfc_configure(pdi, NDO_ACTIVATE_FIELD, true)) {
|
||||
nfc_perror (pdi, "nfc_configure activate field");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void mf_select_tag(nfc_device_t* pdi, nfc_target_t* pnt) {
|
||||
@@ -513,9 +577,9 @@ void mf_select_tag(nfc_device_t* pdi, nfc_target_t* pnt) {
|
||||
.nbr = NBR_106,
|
||||
};
|
||||
if (!nfc_initiator_select_passive_target(pdi, nm, NULL, 0, pnt)) {
|
||||
fprintf(stderr, "!Error connecting to the MIFARE Classic tag\n");
|
||||
ERR ("Unable to connect to the MIFARE Classic tag");
|
||||
nfc_disconnect(pdi);
|
||||
exit(1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -546,8 +610,8 @@ int find_exploit_sector(mftag t) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "\n\nNo sector encrypted with the default key has been found, exiting..\n");
|
||||
exit(1);
|
||||
ERR ("\n\nNo sector encrypted with the default key has been found, exiting..");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void mf_anticollision(mftag t, mfreader r) {
|
||||
@@ -556,8 +620,9 @@ void mf_anticollision(mftag t, mfreader r) {
|
||||
.nbr = NBR_106,
|
||||
};
|
||||
if (!nfc_initiator_select_passive_target(r.pdi, nm, NULL, 0, &t.nt)) {
|
||||
fprintf(stderr, "\n\n!Error: tag has been removed\n");
|
||||
exit(1);
|
||||
nfc_perror (r.pdi, "nfc_initiator_select_passive_target");
|
||||
ERR ("Tag has been removed");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -595,14 +660,14 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
|
||||
// We need full control over the CRC
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, false)) {
|
||||
nfc_perror (r.pdi, "nfc_configure");
|
||||
nfc_perror (r.pdi, "nfc_configure crc");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Request plain tag-nonce
|
||||
// fprintf(stdout, "\t[Nt]:\t");
|
||||
// TODO: Set NDO_EASY_FRAMING option only once if possible
|
||||
if (!nfc_configure (r.pdi, NDO_EASY_FRAMING, false)) {
|
||||
nfc_perror (r.pdi, "nfc_configure");
|
||||
nfc_perror (r.pdi, "nfc_configure framing");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -648,14 +713,17 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
}
|
||||
|
||||
// Finally we want to send arbitrary parity bits
|
||||
nfc_configure(r.pdi, NDO_HANDLE_PARITY, false);
|
||||
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, false)) {
|
||||
nfc_perror (r.pdi, "nfc_configure parity");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Transmit reader-answer
|
||||
// fprintf(stdout, "\t{Ar}:\t");
|
||||
// print_hex_par(ArEnc, 64, ArEncPar);
|
||||
if ((!nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, &RxLen, RxPar)) || (RxLen != 32)) {
|
||||
fprintf(stderr, "Reader-answer transfer error, exiting..\n");
|
||||
exit(1);
|
||||
ERR ("Reader-answer transfer error, exiting..");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Now print the answer from the tag
|
||||
@@ -665,8 +733,8 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
// Decrypt the tag answer and verify that suc3(Nt) is At
|
||||
Nt = prng_successor(Nt, 32);
|
||||
if (!((crypto1_word(pcs, 0x00, 0) ^ bytes_to_num(Rx, 4)) == (Nt&0xFFFFFFFF))) {
|
||||
fprintf(stderr, "[At] is not Suc3(Nt), something is wrong, exiting..\n");
|
||||
exit(1);
|
||||
ERR ("[At] is not Suc3(Nt), something is wrong, exiting..");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
// fprintf(stdout, "Authentication completed.\n\n");
|
||||
|
||||
@@ -684,7 +752,7 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
// Sending the encrypted Auth command
|
||||
if (!nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, &RxLen, RxPar)) {
|
||||
fprintf(stdout, "Error requesting encrypted tag-nonce\n");
|
||||
exit(1);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Decrypt the encrypted auth
|
||||
@@ -712,13 +780,13 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
}
|
||||
nfc_configure(r.pdi,NDO_HANDLE_PARITY,false);
|
||||
if ((!nfc_initiator_transceive_bits(r.pdi, ArEnc, 64, ArEncPar, Rx, &RxLen, RxPar)) || (RxLen != 32)) {
|
||||
fprintf(stderr, "Reader-answer transfer error, exiting..\n");
|
||||
exit(1);
|
||||
ERR ("Reader-answer transfer error, exiting..");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
Nt = prng_successor(Nt, 32);
|
||||
if (!((crypto1_word(pcs, 0x00, 0) ^ bytes_to_num(Rx, 4)) == (Nt&0xFFFFFFFF))) {
|
||||
fprintf(stderr, "[At] is not Suc3(Nt), something is wrong, exiting..\n");
|
||||
exit(1);
|
||||
ERR ("[At] is not Suc3(Nt), something is wrong, exiting..");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
} // Next auth probe
|
||||
|
||||
@@ -741,8 +809,19 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
AuthEncPar[i] = filter(pcs->odd) ^ oddparity(Auth[i]);
|
||||
}
|
||||
if (!nfc_initiator_transceive_bits(r.pdi, AuthEnc, 32, AuthEncPar,Rx, &RxLen, RxPar)) {
|
||||
fprintf(stdout, "Error requesting encrypted tag-nonce\n");
|
||||
exit(1);
|
||||
ERR ("while requesting encrypted tag-nonce");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Finally we want to send arbitrary parity bits
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_PARITY, true)) {
|
||||
nfc_perror (r.pdi, "nfc_configure parity restore M");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!nfc_configure(r.pdi, NDO_HANDLE_CRC, true)) {
|
||||
nfc_perror (r.pdi, "nfc_configure crc restore M");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Save the encrypted nonce
|
||||
@@ -778,8 +857,8 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
// fprintf(stdout, "New chunk by %d, sizeof %lu\n", kcount, pk->size * sizeof(uint64_t));
|
||||
pk->possibleKeys = (uint64_t *) realloc((void *)pk->possibleKeys, pk->size * sizeof(uint64_t));
|
||||
if (pk->possibleKeys == NULL) {
|
||||
fprintf(stderr, "Memory allocation error for pk->possibleKeys\n");
|
||||
exit(1);
|
||||
ERR ("Memory allocation error for pk->possibleKeys");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
pk->possibleKeys[kcount] = lfsr;
|
||||
@@ -794,8 +873,8 @@ int mf_enhanced_auth(int e_sector, int a_sector, mftag t, mfreader r, denonce *d
|
||||
if (kcount != 0) {
|
||||
pk->size = --kcount;
|
||||
if ((pk->possibleKeys = (uint64_t *) realloc((void *)pk->possibleKeys, pk->size * sizeof(uint64_t))) == NULL) {
|
||||
fprintf(stderr, "Memory allocation error for pk->possibleKeys\n");
|
||||
exit(1);
|
||||
ERR ("Memory allocation error for pk->possibleKeys");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -835,8 +914,8 @@ countKeys * uniqsort(uint64_t * possibleKeys, uint32_t size) {
|
||||
|
||||
our_counts = calloc(size, sizeof(countKeys));
|
||||
if (our_counts == NULL) {
|
||||
fprintf(stderr, "Memory allocation error for our_counts\n");
|
||||
exit(1);
|
||||
ERR ("Memory allocation error for our_counts");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (i = 0; i < size; i++) {
|
||||
|
||||
66
src/mifare.c
66
src/mifare.c
@@ -1,3 +1,33 @@
|
||||
/*-
|
||||
* Public platform independent Near Field Communication (NFC) library examples
|
||||
*
|
||||
* Copyright (C) 2009, Roel Verdult
|
||||
* Copyright (C) 2010, Romuald Conty, Romain Tartière
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1) Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2 )Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
||||
*
|
||||
*/
|
||||
|
||||
#include "mifare.h"
|
||||
|
||||
#include <string.h>
|
||||
@@ -21,13 +51,10 @@ bool
|
||||
nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp)
|
||||
{
|
||||
byte_t abtRx[265];
|
||||
size_t szRxLen;
|
||||
size_t szRx = sizeof(abtRx);
|
||||
size_t szParamLen;
|
||||
byte_t abtCmd[265];
|
||||
|
||||
// Make sure we are dealing with a active device
|
||||
if (!pnd->bActive)
|
||||
return false;
|
||||
bool bEasyFraming;
|
||||
|
||||
abtCmd[0] = mc; // The MIFARE Classic command
|
||||
abtCmd[1] = ui8Block; // The block address (1K=0x00..0x39, 4K=0x00..0xff)
|
||||
@@ -67,15 +94,34 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t
|
||||
if (szParamLen)
|
||||
memcpy (abtCmd + 2, (byte_t *) pmp, szParamLen);
|
||||
|
||||
// Fire the mifare command
|
||||
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRxLen)) {
|
||||
if (pnd->iLastError != 0x14)
|
||||
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
|
||||
bEasyFraming = pnd->bEasyFraming;
|
||||
if (!nfc_configure (pnd, NDO_EASY_FRAMING, true)) {
|
||||
nfc_perror (pnd, "nfc_configure");
|
||||
return false;
|
||||
}
|
||||
// Fire the mifare command
|
||||
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx)) {
|
||||
if (pnd->iLastError == EINVRXFRAM) {
|
||||
// "Invalid received frame" AKA EINVRXFRAM, usual means we are
|
||||
// authenticated on a sector but the requested MIFARE cmd (read, write)
|
||||
// is not permitted by current acces bytes;
|
||||
// So there is nothing to do here.
|
||||
} else if (pnd->iLastError == EMFAUTH) {
|
||||
// In MFOC, we have to hide authentication errors :)
|
||||
} else {
|
||||
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
|
||||
}
|
||||
nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming);
|
||||
return false;
|
||||
}
|
||||
if (!nfc_configure (pnd, NDO_EASY_FRAMING, bEasyFraming)) {
|
||||
nfc_perror (pnd, "nfc_configure");
|
||||
return false;
|
||||
}
|
||||
|
||||
// When we have executed a read command, copy the received bytes into the param
|
||||
if (mc == MC_READ) {
|
||||
if (szRxLen == 16) {
|
||||
if (szRx == 16) {
|
||||
memcpy (pmp->mpd.abtData, abtRx, 16);
|
||||
} else {
|
||||
return false;
|
||||
|
||||
42
src/mifare.h
42
src/mifare.h
@@ -1,22 +1,34 @@
|
||||
/**
|
||||
* Public platform independent Near Field Communication (NFC) library
|
||||
/*-
|
||||
* Public platform independent Near Field Communication (NFC) library examples
|
||||
*
|
||||
* Copyright (C) 2009, Roel Verdult, 2010, Romuald Conty
|
||||
* Copyright (C) 2009, Roel Verdult
|
||||
* Copyright (C) 2010, Romuald Conty, Romain Tartière
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser 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.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1) Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2 )Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file mifaretag.h
|
||||
* @brief provide samples structs and functions to manipulate MIFARE Classic and Ultralight tags using libnfc
|
||||
*/
|
||||
|
||||
@@ -532,6 +532,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
|
||||
void
|
||||
print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" ID (NFCID2): ");
|
||||
print_hex (nfi.abtId, 8);
|
||||
printf (" Parameter (PAD): ");
|
||||
@@ -541,6 +542,7 @@ print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
|
||||
void
|
||||
print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" ATQA (SENS_RES): ");
|
||||
print_hex (nji.btSensRes, 2);
|
||||
printf (" 4-LSB JEWELID: ");
|
||||
@@ -608,6 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
|
||||
void
|
||||
print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
|
||||
{
|
||||
(void) verbose;
|
||||
printf (" NFCID3: ");
|
||||
print_hex (ndi.abtNFCID3, 10);
|
||||
printf (" BS: %02x\n", ndi.btBS);
|
||||
|
||||
@@ -38,6 +38,46 @@
|
||||
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <err.h>
|
||||
|
||||
/**
|
||||
* @macro DBG
|
||||
* @brief Print a message of standard output only in DEBUG mode
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
# define DBG(...) do { \
|
||||
warnx ("DBG %s:%d", __FILE__, __LINE__); \
|
||||
warnx (" " __VA_ARGS__ ); \
|
||||
} while (0)
|
||||
#else
|
||||
# define DBG(...) {}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @macro WARN
|
||||
* @brief Print a warn message
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
# define WARN(...) do { \
|
||||
warnx ("WARNING %s:%d", __FILE__, __LINE__); \
|
||||
warnx (" " __VA_ARGS__ ); \
|
||||
} while (0)
|
||||
#else
|
||||
# define WARN(...) warnx ("WARNING: " __VA_ARGS__ )
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @macro ERR
|
||||
* @brief Print a error message
|
||||
*/
|
||||
#ifdef DEBUG
|
||||
# define ERR(...) do { \
|
||||
warnx ("ERROR %s:%d", __FILE__, __LINE__); \
|
||||
warnx (" " __VA_ARGS__ ); \
|
||||
} while (0)
|
||||
#else
|
||||
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
|
||||
#endif
|
||||
|
||||
byte_t oddparity (const byte_t bt);
|
||||
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);
|
||||
|
||||
Reference in New Issue
Block a user