Skip to content

Commit 6fde5d8

Browse files
- Renamed project and binary to mfcuk (instead of mfcuk_keyrecovery_darkside)
- Fixed fingerprint related bugs - Moved Template data to correct path relative to where mfcuk binary is built
1 parent 7390ac0 commit 6fde5d8

23 files changed

+792
-245
lines changed

AUTHORS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
= Author
12
Andrei Costin <[email protected]>, http://andreicostin.com
3+
4+
= Contributors
5+
6+
Romuald Conty <[email protected]> - porting to libnfc 1.3.x, 1.4.x, 1.5.x
7+
Nethemba Core Team <[email protected]> - core AC, AM, configure and packaging

README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
README
22
======
33

4+
Compiling:
5+
automake
6+
autoconf
7+
./configure
8+
make
9+
10+
Running (most basic):
11+
./mfcuk -C -R 0:A -v 2
12+
413
PACKAGE
514
-------
615
MiFare Classic Universal toolKit (MFCUK)
File renamed without changes.
File renamed without changes.

configure.ac

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,32 @@
1-
AC_INIT([mfcuk], [0.3.2], [[email protected]])
1+
AC_INIT([mfcuk], [0.3.3], [[email protected]])
22

33
AC_CONFIG_MACRO_DIR([m4])
44

55
AC_PROG_CC
66

77
AC_CONFIG_HEADERS([config.h])
88

9+
AC_CONFIG_SRCDIR([src/mfcuk.c])
10+
911
AM_INIT_AUTOMAKE
1012

1113
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])
1214

13-
# Checks for libnfc.
14-
WITH_NFC=0
15-
## Search libnfc with command line option (default prefix: auto)
16-
LIBNFC_PREFIX="auto"
17-
AC_ARG_WITH(libnfc,[--with-libnfc=DIR location of the libnfc],
18-
[if test "$withval" != no; then
19-
if test "$withval" != yes; then
20-
LIBNFC_PREFIX=$withval
21-
fi
22-
fi])
23-
if test x"$LIBNFC_PREFIX" != "xauto"; then
24-
LIBNFC_CFLAGS="-L$LIBNFC_PREFIX/lib/ -I$LIBNFC_PREFIX/include/"
25-
LIBNFC_LIBS="-lnfc"
26-
fi
27-
28-
# Search using pkg-config
29-
AC_PATH_PROG(PKG_CONFIG, pkg-config, [AC_MSG_WARN([pkg-config not found.])])
30-
if test x"$WITH_NFC" = "x0"; then
31-
if test x"$PKG_CONFIG" != "x"; then
32-
LIBNFC_REQUIRED_VERSION=1.5.1
33-
PKG_CHECK_MODULES([LIBNFC], [libnfc >= $LIBNFC_REQUIRED_VERSION], [WITH_NFC=1], [WITH_NFC=0])
34-
fi
35-
fi
36-
CFLAGS="$CFLAGS $LIBNFC_CFLAGS"
37-
AC_CHECK_HEADERS([nfc/nfc.h],[WITH_NFC=1], AC_MSG_ERROR([A version of libnfc greater than or equal to $LIBNFC_REQUIRED_VERSION is required]))
38-
39-
if test x"$WITH_NFC" = "x0"; then
40-
AC_MSG_ERROR([A version of libnfc greater than or equal to $LIBNFC_REQUIRED_VERSION is required.])
41-
fi
42-
43-
# Checks for programs.
44-
AC_PROG_CC
45-
46-
# Checks for libraries.
47-
AC_CHECK_LIB([nfc], [nfc_version])
15+
# Checks for pkg-config modules.
16+
LIBNFC_REQUIRED_VERSION=1.5.1
17+
PKG_CHECK_MODULES([LIBNFC], [libnfc >= $LIBNFC_REQUIRED_VERSION], [], [AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.])])
4818

49-
# Checks for header files.
50-
AC_CHECK_HEADERS([byteswap.h])
51-
AC_CHECK_HEADERS([endian.h sys/endian.h CoreFoundation/CoreFoundation.h])
52-
AC_CHECK_HEADERS([stdint.h stdlib.h string.h sys/time.h unistd.h])
53-
54-
AC_DEFINE(_XOPEN_SOURCE, 600, [Enable POSIX extensions if present])
19+
PKG_CONFIG_REQUIRES="libnfc"
20+
AC_SUBST([PKG_CONFIG_REQUIRES])
5521

5622
# Checks for typedefs, structures, and compiler characteristics.
5723
AC_HEADER_STDBOOL
58-
AC_TYPE_SIZE_T
59-
AC_TYPE_UINT16_T
6024
AC_TYPE_UINT32_T
6125
AC_TYPE_UINT64_T
62-
AC_TYPE_UINT8_T
6326

6427
# Checks for library functions.
6528
AC_FUNC_MALLOC
6629
AC_FUNC_REALLOC
67-
AC_CHECK_FUNCS([memset strchr])
68-
69-
# Help us to write great code ;-)
70-
CFLAGS="$CFLAGS -Wall -pedantic -Wextra -std=c99"
7130

7231
AC_CONFIG_FILES([Makefile
7332
src/Makefile])

src/Makefile.am

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
1-
bin_PROGRAMS = mfcuk_keyrecovery_darkside
1+
AM_CFLAGS = @LIBNFC_CFLAGS@
2+
AM_LDFLAGS = @LIBNFC_LIBS@
23

3-
noinst_HEADERS = \
4-
crapto1.h \
5-
mfcuk_finger.h \
6-
mfcuk_keyrecovery_darkside.h \
7-
mfcuk_mifare.h \
8-
mfcuk_utils.h \
9-
mifare.h \
10-
nfc-utils.h \
11-
xgetopt.h
4+
bin_PROGRAMS = mfcuk
125

13-
# set the include path found by configure
14-
mfcuk_keyrecovery_darkside_SOURCES = \
15-
crapto1.c \
16-
crypto1.c \
17-
mfcuk_finger.c \
18-
mfcuk_keyrecovery_darkside.c \
19-
mfcuk_mifare.c \
20-
mfcuk_utils.c \
21-
mifare.c \
22-
nfc-utils.c
6+
noinst_HEADERS = crapto1.h mifare.h nfc-utils.h mfcuk_mifare.h mfcuk_finger.h mfcuk_utils.h xgetopt.h mfcuk.h
7+
mfcuk_SOURCES = crapto1.c crypto1.c mifare.c nfc-utils.c mfcuk_mifare.c mfcuk_finger.c mfcuk_utils.c xgetopt.c mfcuk.c
238

24-
mfcuk_keyrecovery_darkside_LDADD = -lnfc -lusb -lpcsclite
25-
26-
# dist_man_MANS = mfcuk_keyrecovery_darkside.1
9+
# dist_man_MANS = mfcuk.1

src/crapto1.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static void quicksort(uint32_t* const start, uint32_t* const stop)
5757
/** binsearch
5858
* Binary search for the first occurence of *stop's MSB in sorted [start,stop]
5959
*/
60-
static /*inline*/ uint32_t*
60+
static inline uint32_t*
6161
binsearch(uint32_t *start, uint32_t *stop)
6262
{
6363
uint32_t mid, val = *stop & 0xff000000;
@@ -73,7 +73,7 @@ binsearch(uint32_t *start, uint32_t *stop)
7373
/** update_contribution
7474
* helper, calculates the partial linear feedback contributions and puts in MSB
7575
*/
76-
static /*inline*/ void
76+
static inline void
7777
update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2)
7878
{
7979
uint32_t p = *item >> 25;
@@ -86,7 +86,7 @@ update_contribution(uint32_t *item, const uint32_t mask1, const uint32_t mask2)
8686
/** extend_table
8787
* using a bit of the keystream extend the table of possible lfsr states
8888
*/
89-
static /*inline*/ void
89+
static inline void
9090
extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in)
9191
{
9292
in <<= 24;
@@ -108,7 +108,7 @@ extend_table(uint32_t *tbl, uint32_t **end, int bit, int m1, int m2, uint32_t in
108108
/** extend_table_simple
109109
* using a bit of the keystream extend the table of possible lfsr states
110110
*/
111-
static /*inline*/ void
111+
static inline void
112112
extend_table_simple(uint32_t *tbl, uint32_t **end, int bit)
113113
{
114114
for(*tbl <<= 1; tbl <= *end; *++tbl <<= 1)

src/crapto1.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
1616
MA 02110-1301, US$
1717
18-
Copyright (C) 2008-2008 bla <[email protected]>
18+
Copyright (C) 2008-2009 bla <[email protected]>
1919
*/
2020
#ifndef CRAPTO1_INCLUDED
2121
#define CRAPTO1_INCLUDED
@@ -35,12 +35,8 @@ uint32_t prng_successor(uint32_t x, uint32_t n);
3535

3636
struct Crypto1State* lfsr_recovery32(uint32_t ks2, uint32_t in);
3737
struct Crypto1State* lfsr_recovery64(uint32_t ks2, uint32_t ks3);
38-
uint32_t *lfsr_prefix_ks(uint8_t ks[8], int isodd);
39-
struct Crypto1State* lfsr_common_prefix(uint32_t pfx, uint32_t rr, uint8_t ks[8], uint8_t par[8][8]);
4038

41-
uint8_t lfsr_rollback_bit(struct Crypto1State* s, uint32_t in, int fb);
42-
uint8_t lfsr_rollback_byte(struct Crypto1State* s, uint32_t in, int fb);
43-
uint32_t lfsr_rollback_word(struct Crypto1State* s, uint32_t in, int fb);
39+
void lfsr_rollback(struct Crypto1State* s, uint32_t in, int fb);
4440
int nonce_distance(uint32_t from, uint32_t to);
4541
#define FOREACH_VALID_NONCE(N, FILTER, FSIZE)\
4642
uint32_t __n = 0,__M = 0, N = 0;\
@@ -57,25 +53,25 @@ int nonce_distance(uint32_t from, uint32_t to);
5753
#define LF_POLY_EVEN (0x870804)
5854
#define BIT(x, n) ((x) >> (n) & 1)
5955
#define BEBIT(x, n) BIT(x, (n) ^ 24)
60-
static /*inline*/ int parity(uint32_t x)
56+
static inline int parity(uint32_t x)
6157
{
6258
#if !defined __i386__ || !defined __GNUC__
6359
x ^= x >> 16;
6460
x ^= x >> 8;
6561
x ^= x >> 4;
6662
return BIT(0x6996, x & 0xf);
6763
#else
68-
asm( "movl %1, %%eax\n"
64+
asm( "movl %1, %%eax\n"
6965
"mov %%ax, %%cx\n"
7066
"shrl $0x10, %%eax\n"
7167
"xor %%ax, %%cx\n"
72-
"xor %%ch, %%cl\n"
73-
"setpo %%al\n"
74-
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
68+
"xor %%ch, %%cl\n"
69+
"setpo %%al\n"
70+
"movzx %%al, %0\n": "=r"(x) : "r"(x): "eax","ecx");
7571
return x;
7672
#endif
7773
}
78-
static /*inline*/ int filter(uint32_t const x)
74+
static inline int filter(uint32_t const x)
7975
{
8076
uint32_t f;
8177

src/mfcuk_keyrecovery_darkside.c renamed to src/mfcuk.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,31 @@
132132
#endif
133133

134134
#if defined(HAVE_BYTESWAP_H)
135-
# include <byteswap.h>
135+
136+
#include <byteswap.h>
137+
138+
#elif __GNUC__ * 100 + __GNUC_MINOR__ >= 430
139+
140+
#warning "NO byteswap.h found! But since GCC >= 4.30, using __builtin_bswapXX() alternatives..."
141+
#define bswap_16 __builtin_bswap16
142+
#define bswap_32 __builtin_bswap32
143+
#define bswap_64 __builtin_bswap64
144+
145+
#else
146+
147+
#warning "NO byteswap.h found! Using untested alternatives..."
148+
149+
static inline unsigned short bswap_16(unsigned short x) {
150+
return (x>>8) | (x<<8);
151+
}
152+
153+
static inline unsigned int bswap_32(unsigned int x) {
154+
return (bswap_16(x&0xffff)<<16) | (bswap_16(x>>16));
155+
}
156+
157+
static inline unsigned long long bswap_64(unsigned long long x) {
158+
return (((unsigned long long)bswap_32(x&0xffffffffull))<<32) | (bswap_32(x>>32));
159+
}
136160
#endif
137161

138162
#include <stdio.h>
@@ -171,7 +195,7 @@
171195
#include "mfcuk_mifare.h"
172196
#include "mfcuk_utils.h"
173197
#include "mfcuk_finger.h"
174-
#include "mfcuk_keyrecovery_darkside.h"
198+
#include "mfcuk.h"
175199

176200
#define MAX_FRAME_LEN 264
177201

@@ -1293,7 +1317,7 @@ int main(int argc, char* argv[])
12931317
}
12941318
break;
12951319
case 'I':
1296-
// // Input extended dump file of type mifare_classic_tag_ext, Options i and I are autoexclusive
1320+
// Input extended dump file of type mifare_classic_tag_ext, Options i and I are autoexclusive
12971321
if (!bfOpts['i'] && !bfOpts['I'])
12981322
{
12991323
if ( !mfcuk_load_tag_dump_ext(optarg, &(dump_loaded_tag)) )
@@ -1308,7 +1332,7 @@ int main(int argc, char* argv[])
13081332
break;
13091333
case 'o':
13101334
case 'O':
1311-
// // Output simple/extended dump file, Options o and O are autoexclusive
1335+
// Output simple/extended dump file, Options o and O are autoexclusive
13121336
if (!bfOpts['o'] && !bfOpts['O'])
13131337
{
13141338
strncpy( strOutputFilename, optarg, sizeof(strOutputFilename) );
File renamed without changes.

src/mfcuk_finger.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mfcuk_finger_tmpl_entry mfcuk_finger_db[] =
4242
{
4343
{ "./data/tmpls_fingerprints/mfcuk_tmpl_skgt.mfd", "Sofia SKGT", mfcuk_finger_default_comparator, mfcuk_finger_skgt_decoder, NULL },
4444
{ "./data/tmpls_fingerprints/mfcuk_tmpl_ratb.mfd", "Bucharest RATB", mfcuk_finger_default_comparator, mfcuk_finger_default_decoder, NULL },
45-
{ "./data/tmpls_fingerprints/mfcuk_tmpl_oyster.mfd", "London OYSTER", mfcuk_finger_default_comparator, mfcuk_finger_default_decoder, NULL }
45+
{ "./data/tmpls_fingerprints/mfcuk_tmpl_oyster.mfd", "London OYSTER", mfcuk_finger_default_comparator, mfcuk_finger_default_decoder, NULL },
4646
};
4747

4848
int mfcuk_finger_db_entries = sizeof(mfcuk_finger_db)/sizeof(mfcuk_finger_db[0]);
@@ -172,13 +172,13 @@ int mfcuk_finger_load()
172172
fclose(fp);
173173
continue;
174174
}
175-
fclose(fp);
176175

177176
if (mfcuk_finger_db[i].tmpl_data == NULL)
178177
{
179178
if ( (tmpl_new = (mfcuk_finger_template *) malloc(sizeof(mfcuk_finger_template))) == NULL)
180179
{
181180
fprintf(stderr, "WARN: cannot allocate memory to template record %d\n", i);
181+
fclose(fp);
182182
continue;
183183
}
184184

@@ -188,9 +188,14 @@ int mfcuk_finger_load()
188188
mfcuk_finger_db[i].tmpl_data = tmpl_new;
189189
template_loaded_count++;
190190
}
191+
192+
if (fp)
193+
{
194+
fclose(fp);
195+
fp = NULL;
196+
}
191197
}
192198

193-
194199
return template_loaded_count;
195200
}
196201

src/mifare.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t
100100
return false;
101101
}
102102
// Fire the mifare command
103-
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, NULL)) {
103+
if (!nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx)) {
104104
if (pnd->iLastError == EINVRXFRAM) {
105105
// "Invalid received frame" AKA EINVRXFRAM, usual means we are
106106
// authenticated on a sector but the requested MIFARE cmd (read, write)
107107
// is not permitted by current acces bytes;
108108
// So there is nothing to do here.
109+
} else if (pnd->iLastError == EMFAUTH) {
110+
// In MFOC, we have to hide authentication errors :)
109111
} else {
110112
nfc_perror (pnd, "nfc_initiator_transceive_bytes");
111113
}

0 commit comments

Comments
 (0)