diff --git a/.gitignore b/.gitignore index b1ce3af..7f8ac6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .svn *.o +*~ aclocal.m4 autom4te.cache/* compile diff --git a/configure.ac b/configure.ac index 22b4e2b..78eed80 100644 --- a/configure.ac +++ b/configure.ac @@ -30,7 +30,6 @@ AC_TYPE_UINT8_T AC_FUNC_MALLOC AC_FUNC_REALLOC -AC_CHECK_HEADERS([sys/time.h]) AC_CHECK_FUNCS([memset strchr strtoul]) # Checks for endianness convertion @@ -41,6 +40,9 @@ fi AC_CHECK_HEADERS([byteswap.h]) +AC_CHECK_HEADERS([unistd.h]) +AM_CONDITIONAL([HAVE_UNISTD], [test $ac_cv_header_unistd_h = "yes"]) + AC_DEFINE([_XOPEN_SOURCE], [600], [Define to 500 if Single Unix conformance is wanted, 600 for sixth revision.]) # Help us to write great code ;-) diff --git a/src/Makefile.am b/src/Makefile.am index f8cebf4..97c5b02 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,9 +2,14 @@ AM_CFLAGS = @libnfc_CFLAGS@ bin_PROGRAMS = mfcuk -noinst_HEADERS = crapto1.h mifare.h nfc-utils.h mfcuk_mifare.h mfcuk_finger.h mfcuk_utils.h xgetopt.h mfcuk.h +noinst_HEADERS = crapto1.h mifare.h nfc-utils.h mfcuk_mifare.h mfcuk_finger.h mfcuk_utils.h mfcuk.h xgetopt.h -mfcuk_SOURCES = crapto1.c crypto1.c mifare.c nfc-utils.c mfcuk_mifare.c mfcuk_finger.c mfcuk_utils.c xgetopt.c mfcuk.c +mfcuk_SOURCES = crapto1.c crypto1.c mifare.c nfc-utils.c mfcuk_mifare.c mfcuk_finger.c mfcuk_utils.c mfcuk.c mfcuk_LDADD = @libnfc_LIBS@ +# If system does not have unistd.h, use our own getopt function from xgetopt.c +if ! HAVE_UNISTD +mfcuk_SOURCES += xgetopt.c +endif + # dist_man_MANS = mfcuk.1 diff --git a/src/mfcuk.c b/src/mfcuk.c index fa38d1d..9339f59 100644 --- a/src/mfcuk.c +++ b/src/mfcuk.c @@ -174,15 +174,7 @@ static inline uint64_t bswap_64(uint64_t x) #include #include -#ifdef WIN32 -#define NOMINMAX -#include "windows.h" #include "xgetopt.h" -#elif __STDC__ -#include -#include -#include -#endif // NFC #include @@ -497,7 +489,7 @@ static uint32_t mfcuk_key_recovery_block(nfc_device *pnd, uint32_t uiUID, uint64 if (ptrFoundTagNonceEntry->current_out_of_8 >= MFCUK_DARKSIDE_MAX_LEVELS) { //printf("FAILURE - This Nt, {Pfx}, consecutive {Nr}s and {ParBits} combination cannot produce a key-recoverable state\n"); - //printf("\tINFO: try changing initial {Nr}, {Ar} and timings of sleep()\n"); + //printf("\tINFO: try changing initial {Nr}, {Ar} and timings of sleepmillis()\n"); //printf("{Nr} is not a DEADBEEF.... Need to find BEEF ALIVE!... Trying next one...\n"); ptrFoundTagNonceEntry->spoofNrEnc++; @@ -516,7 +508,7 @@ static uint32_t mfcuk_key_recovery_block(nfc_device *pnd, uint32_t uiUID, uint64 if (ptrFoundTagNonceEntry->parBitsCrntCombination[ptrFoundTagNonceEntry->current_out_of_8] >= 0x20) { //printf("FAILURE - This consecutive {Nr}s and {ParBits} combination cannot produce all 8 required NACKs and KSs of NACKs\n"); - //printf("\tINFO: try changing initial {Nr}, {Ar} and timings of sleep()\n"); + //printf("\tINFO: try changing initial {Nr}, {Ar} and timings of sleepmillis()\n"); //printf("{Nr} is not a DEADBEEF.... Need to find BEEF ALIVE!... Trying next one...\n"); ptrFoundTagNonceEntry->spoofNrEnc++; @@ -838,7 +830,7 @@ static bool mfcuk_darkside_select_tag(nfc_device *pnd, int iSleepAtFieldOFF, int } // {WPMCC09} 2.4. Tag nonces: "drop the field (for approximately 30us) to discharge all capacitors" - sleep(iSleepAtFieldOFF); + sleepmillis(iSleepAtFieldOFF); // Let the reader only try once to find a tag if (0 > nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) { @@ -864,7 +856,7 @@ static bool mfcuk_darkside_select_tag(nfc_device *pnd, int iSleepAtFieldOFF, int } // Switch the field back on, and wait for a constant amount of time before authenticating - sleep(iSleepAfterFieldON); + sleepmillis(iSleepAfterFieldON); // Poll for a ISO14443A (MIFARE) tag if (0 >= nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &ti_tmp)) { diff --git a/src/mfcuk_utils.c b/src/mfcuk_utils.c index 586d2ab..4d8f2e6 100644 --- a/src/mfcuk_utils.c +++ b/src/mfcuk_utils.c @@ -45,12 +45,19 @@ along with this program. If not, see . */ -#include "mfcuk_utils.h" +#include "config.h" -#ifdef __STDC__ -struct timeval global_timeout; +#if defined(WIN32) +#include +#elif defined(HAVE_UNISTD_H) +#include +#else +#error "Unsupported system" #endif +#include "mfcuk_utils.h" +#include + /* http://www.velocityreviews.com/forums/t451319-advice-required-on-my-ascii-to-hex-conversion-c.html Basically, converting a hex digit into a hex nibble (4 binary digits) algorithm looks like; @@ -79,3 +86,22 @@ unsigned char hex2bin(unsigned char h, unsigned char l) l -= -(l > 9) & 0x27; return h << 4 | l; } + +void sleepmillis(unsigned int millis) +{ +#ifdef WIN32 // If system is Windows, use system's own function if possible to reduce overhead, even if a standard C library is available + Sleep(millis); +#else + usleep(millis * 1000); +#endif +} + +void clear_screen() +{ +#ifdef WIN32 // On Windows, use "cls" command + system("cls"); +#else // Otherwise fall back to TTY control characters + printf("\033[1;1H\033[J"); +#endif +} + diff --git a/src/mfcuk_utils.h b/src/mfcuk_utils.h index b404344..73195b6 100644 --- a/src/mfcuk_utils.h +++ b/src/mfcuk_utils.h @@ -48,35 +48,6 @@ #ifndef _MFCUK_UTILS_H_ #define _MFCUK_UTILS_H_ -#include -#include -#include - -#ifdef WIN32 -#define NOMINMAX -#include "windows.h" -#include "xgetopt.h" -#elif __STDC__ -#include -#include -#include -#endif - -// "Portable" sleep(miliseconds) -#ifdef WIN32 -#define sleep(x) Sleep(x) -#elif __STDC__ -extern struct timeval global_timeout; -#define sleep(x) { global_timeout.tv_usec = 1000 * (x); select(0,NULL,NULL,NULL,&global_timeout); } -#endif - -// "Portable" clear_screen() - NOTE: system performance penalty introduced -#ifdef WIN32 -#define clear_screen() system("cls") -#elif __STDC__ -#define clear_screen() system("sh -c clear") -#endif - /** * @fn int is_hex(char c) * @brief Checks if an ASCII character is a valid hexadecimal base digit @@ -101,4 +72,23 @@ int is_hex(char c); */ unsigned char hex2bin(unsigned char h, unsigned char l); +/** + * @fn void sleepmillis(unsigned int millis) + * @brief Pauses execution for a certain amount of milliseconds + * @param millis Number of milliseconds to sleep + * + * Wrapper for system-dependant sleep function. It pauses execution for a certain amount of milliseconds. + */ +void sleepmillis(unsigned int millis); + +/** + * @fn void clear_screen(void); + * @brief Clears output console + * + * Wrapper for system-dependant clear screen function. + * Resets output console, clearing text and resetting character pointer. + */ +void clear_screen(void); + #endif // _MFCUK_UTILS_H_ + diff --git a/src/xgetopt.h b/src/xgetopt.h index fa6f6ad..fabf5fd 100644 --- a/src/xgetopt.h +++ b/src/xgetopt.h @@ -15,9 +15,15 @@ #ifndef XGETOPT_H #define XGETOPT_H +#if HAVE_UNISTD_H +#include +#else + extern int optind, opterr; extern char *optarg; int getopt(int argc, char *argv[], char *optstring); +#endif + #endif //XGETOPT_H