diff --git a/client/atr_scrap_eftlab.py b/client/atr_scrap_eftlab.py index a19654acc..93b044451 100755 --- a/client/atr_scrap_eftlab.py +++ b/client/atr_scrap_eftlab.py @@ -36,7 +36,7 @@ def main(): print( """#ifndef ATRS_H__ - + #define ATRS_H__ #include @@ -49,7 +49,7 @@ def main(): const char *getAtrInfo(const char *atr_str); // atr_t array are expected to be NULL terminated - static atr_t AtrTable[] = {""") + const static atr_t AtrTable[] = {""") print_atr(df) diff --git a/client/src/atrs.c b/client/src/atrs.c index f39c2616b..2a4184219 100644 --- a/client/src/atrs.c +++ b/client/src/atrs.c @@ -10,7 +10,9 @@ #include "atrs.h" #include #include +#include // regex? #include "commonutil.h" // ARRAYLEN +#include "ui.h" // get PrintAndLogEx #include "util.h" // startswith // get a ATR description based on the atr bytes @@ -22,26 +24,30 @@ const char *getAtrInfo(const char *atr_str) { // check for dots in atr table. // dots indicate those bytes at those positions are optional. // need a special case for them - if (strstr(AtrTable[i].bytes, "..") != NULL) { - - // need to loop - const char *foo = atr_str; - int j = 0; - while (foo++, j++) { - - char c = foo[0]; - if (c == '.') { - continue; - } - - // mismatch, return default message - if (c != AtrTable[i].bytes[j]) { - return AtrTable[ARRAYLEN(AtrTable) - 1].desc; - } + if (strstr(AtrTable[i].bytes, ".") != NULL) { + regex_t r; + int ret = regcomp(&r, AtrTable[i].bytes, 0); + if (ret) { + // take next ATR value. + PrintAndLogEx(DEBUG, "can't compile regex"); + continue; } - return AtrTable[i].desc; + /* Execute regular expression */ + ret = regexec(&r, atr_str, 0, NULL, 0); + regfree(&r); + + if (!ret) { + return AtrTable[i].desc; + } + else if (ret == REG_NOMATCH) { + continue; + } + else { + PrintAndLogEx(DEBUG, "regex failed"); + continue; + } } else { if (str_startswith(atr_str, AtrTable[i].bytes)) { diff --git a/client/src/atrs.h b/client/src/atrs.h index 19ed77e1c..9309cbc0c 100644 --- a/client/src/atrs.h +++ b/client/src/atrs.h @@ -12,7 +12,7 @@ const char *getAtrInfo(const char *atr_str); // atr_t array are expected to be NULL terminated - static atr_t AtrTable[] = { + const static atr_t AtrTable[] = { { "3B021450", "Schlumberger Multiflex 3k" }, { "3B025301", "Gemplus GemClub Memo SuperShop clubcard" }, { "3B0400000000", "Laundromat payment card Spanish ID (“DNIe: Documento Nacional de Identidad electrónico). http://www.dnie.es/" }, diff --git a/client/src/cmdsmartcard.c b/client/src/cmdsmartcard.c index 57abf818e..1abce1ca9 100644 --- a/client/src/cmdsmartcard.c +++ b/client/src/cmdsmartcard.c @@ -23,6 +23,7 @@ #include "fileutils.h" #include "crc16.h" // crc #include "cliparser.h" // cliparsing +#include "atrs.h" // ATR lookup static int CmdHelp(const char *Cmd); @@ -691,8 +692,11 @@ static int CmdSmartInfo(const char *Cmd) { // print header PrintAndLogEx(INFO, "--- " _CYAN_("Smartcard Information") " ---------"); PrintAndLogEx(INFO, "ISO7618-3 ATR : %s", sprint_hex(card.atr, card.atr_len)); + PrintAndLogEx(INFO, "http://smartcard-atr.apdu.fr/parse?ATR=%s", sprint_hex_inrow(card.atr, card.atr_len)); + //PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011")); + // print ATR PrintAndLogEx(INFO, "ATR"); PrintATR(card.atr, card.atr_len); @@ -755,6 +759,18 @@ static int CmdSmartReader(const char *Cmd) { } smart_card_atr_t *card = (smart_card_atr_t *)resp.data.asBytes; PrintAndLogEx(INFO, "ISO7816-3 ATR : %s", sprint_hex(card->atr, card->atr_len)); + + // test with normal lookup, with existing ATR in table + PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFF112503108041B00669FF4A50700000415A010011")); // Astro (Pay TV) + + //PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800F..B0..69FF4A50D08000495403")); + + // test with random bytes in .. space + PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D08000495403")); // Sky (Italy) VideoGuard CAM card + + // test w shorted str, not in ATR table + PrintAndLogEx(INFO, "ATR loopup : %s", getAtrInfo("3FFD13250250800FEEB00269FF4A50D080004954")); // Sky (Italy) VideoGuard CAM card + return PM3_SUCCESS; }