proxmark3/client/src/aidsearch.c

184 lines
5.5 KiB
C
Raw Normal View History

2019-11-05 01:54:23 +08:00
//-----------------------------------------------------------------------------
2022-01-07 08:58:03 +08:00
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
2019-11-05 01:54:23 +08:00
//
2022-01-07 08:58:03 +08:00
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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.
//
// See LICENSE.txt for the text of the license.
2019-11-05 01:54:23 +08:00
//-----------------------------------------------------------------------------
// Proxmark3 RDV40 AID list library
//-----------------------------------------------------------------------------
#include "aidsearch.h"
#include <ctype.h>
#include <string.h>
#include "fileutils.h"
#include "pm3_cmd.h"
2020-05-03 07:13:28 +08:00
static int openAIDFile(json_t **root, bool verbose) {
2019-11-05 01:54:23 +08:00
json_error_t error;
char *path;
int res = searchFile(&path, RESOURCES_SUBDIR, "aidlist", ".json", false);
if (res != PM3_SUCCESS) {
return PM3_EFILE;
}
int retval = PM3_SUCCESS;
2019-11-05 02:15:09 +08:00
*root = json_load_file(path, 0, &error);
2021-04-19 00:31:51 +08:00
if (!*root) {
2019-11-05 01:54:23 +08:00
PrintAndLogEx(ERR, "json (%s) error on line %d: %s", path, error.line, error.text);
retval = PM3_ESOFT;
goto out;
}
2019-11-05 02:15:09 +08:00
if (!json_is_array(*root)) {
2019-11-05 01:54:23 +08:00
PrintAndLogEx(ERR, "Invalid json (%s) format. root must be an array.", path);
retval = PM3_ESOFT;
goto out;
}
2020-12-19 05:05:16 +08:00
PrintAndLogEx(DEBUG, "Loaded file " _YELLOW_("%s") " " _GREEN_("%zu") " records ( " _GREEN_("ok") " )", path, json_array_size(*root));
2019-11-05 01:54:23 +08:00
out:
free(path);
return retval;
}
2020-05-03 07:13:28 +08:00
static int closeAIDFile(json_t *root) {
2019-11-05 01:54:23 +08:00
json_decref(root);
return PM3_SUCCESS;
}
json_t *AIDSearchInit(bool verbose) {
2019-11-05 02:15:09 +08:00
json_t *root = NULL;
int res = openAIDFile(&root, verbose);
2019-11-05 01:54:23 +08:00
if (res != PM3_SUCCESS)
return NULL;
2019-11-05 02:15:09 +08:00
return root;
2019-11-05 01:54:23 +08:00
}
2021-04-17 21:17:39 +08:00
json_t *AIDSearchGetElm(json_t *root, size_t elmindx) {
2019-11-05 01:54:23 +08:00
json_t *data = json_array_get(root, elmindx);
if (!json_is_object(data)) {
2021-04-17 21:17:39 +08:00
PrintAndLogEx(ERR, "data [%zu] is not an object\n", elmindx);
2019-11-05 01:54:23 +08:00
return NULL;
}
return data;
}
int AIDSearchFree(json_t *root) {
return closeAIDFile(root);
}
2020-05-03 07:13:28 +08:00
static const char *jsonStrGet(json_t *data, const char *name) {
2019-11-05 01:54:23 +08:00
json_t *jstr;
2019-11-08 19:00:21 +08:00
2019-11-05 01:54:23 +08:00
jstr = json_object_get(data, name);
2019-11-05 02:15:09 +08:00
if (jstr == NULL)
return NULL;
2019-11-05 01:54:23 +08:00
if (!json_is_string(jstr)) {
PrintAndLogEx(ERR, "`%s` is not a string", name);
return NULL;
}
const char *cstr = json_string_value(jstr);
if (strlen(cstr) == 0)
return NULL;
return cstr;
}
2020-05-03 07:13:28 +08:00
static bool aidCompare(const char *aidlarge, const char *aidsmall) {
2019-11-05 01:54:23 +08:00
if (strcmp(aidlarge, aidsmall) == 0)
return true;
2019-11-08 19:00:21 +08:00
2019-11-05 01:54:23 +08:00
if (strlen(aidlarge) > strlen(aidsmall))
if (strncmp(aidlarge, aidsmall, strlen(aidsmall)) == 0)
return true;
2019-11-08 19:00:21 +08:00
2019-11-05 01:54:23 +08:00
return false;
}
2019-11-05 03:18:37 +08:00
bool AIDGetFromElm(json_t *data, uint8_t *aid, size_t aidmaxlen, int *aidlen) {
*aidlen = 0;
const char *hexaid = jsonStrGet(data, "AID");
if (hexaid == NULL || strlen(hexaid) == 0)
return false;
2019-11-08 19:00:21 +08:00
2021-04-17 21:23:19 +08:00
int res = param_gethex_to_eol(hexaid, 0, aid, (int)aidmaxlen, aidlen);
2019-11-05 03:18:37 +08:00
if (res)
return false;
return true;
}
int PrintAIDDescription(json_t *xroot, char *aid, bool verbose) {
2019-11-05 01:54:23 +08:00
int retval = PM3_SUCCESS;
2019-11-08 19:00:21 +08:00
json_t *root = xroot;
if (root == NULL)
root = AIDSearchInit(verbose);
2019-11-05 03:18:37 +08:00
if (root == NULL)
2019-11-05 01:54:23 +08:00
goto out;
2019-11-08 19:00:21 +08:00
2019-11-05 03:18:37 +08:00
json_t *elm = NULL;
2021-04-17 21:23:19 +08:00
size_t maxaidlen = 0;
2021-04-17 21:17:39 +08:00
for (size_t elmindx = 0; elmindx < json_array_size(root); elmindx++) {
2019-11-05 03:18:37 +08:00
json_t *data = AIDSearchGetElm(root, elmindx);
2019-11-05 01:54:23 +08:00
if (data == NULL)
2019-11-05 03:18:37 +08:00
continue;
2019-11-05 05:21:02 +08:00
const char *dictaid = jsonStrGet(data, "AID");
if (aidCompare(aid, dictaid)) { // dictaid may be less length than requested aid
if (maxaidlen < strlen(dictaid) && strlen(dictaid) <= strlen(aid)) {
maxaidlen = strlen(dictaid);
elm = data;
}
2019-11-05 03:18:37 +08:00
}
2019-11-05 01:54:23 +08:00
}
2019-11-08 19:00:21 +08:00
2019-11-05 03:18:37 +08:00
if (elm == NULL)
goto out;
2019-11-08 19:00:21 +08:00
2019-11-05 01:54:23 +08:00
// print here
2019-11-05 03:18:37 +08:00
const char *vaid = jsonStrGet(elm, "AID");
const char *vendor = jsonStrGet(elm, "Vendor");
const char *name = jsonStrGet(elm, "Name");
const char *country = jsonStrGet(elm, "Country");
const char *description = jsonStrGet(elm, "Description");
const char *type = jsonStrGet(elm, "Type");
2019-11-05 01:54:23 +08:00
2020-12-19 05:05:16 +08:00
if (verbose == false) {
PrintAndLogEx(SUCCESS, "AID : " _YELLOW_("%s") " | %s | %s", vaid, vendor, name);
2019-11-05 01:54:23 +08:00
} else {
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Input AID..... " _YELLOW_("%s"), aid);
2019-11-05 01:54:23 +08:00
if (aid)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Found AID..... " _YELLOW_("%s"), vaid);
2019-11-05 01:54:23 +08:00
if (vendor)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Vendor........ " _YELLOW_("%s"), vendor);
2019-11-05 01:54:23 +08:00
if (type)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Type.......... " _YELLOW_("%s"), type);
2019-11-05 01:54:23 +08:00
if (name)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Name.......... " _YELLOW_("%s"), name);
2019-11-05 01:54:23 +08:00
if (country)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Country....... %s", country);
2019-11-05 01:54:23 +08:00
if (description)
2020-12-19 05:05:16 +08:00
PrintAndLogEx(SUCCESS, "Description... %s", description);
2019-11-05 01:54:23 +08:00
}
2019-11-08 19:00:21 +08:00
2019-11-05 01:54:23 +08:00
out:
if (xroot == NULL)
AIDSearchFree(root);
2019-11-05 01:54:23 +08:00
return retval;
}
int PrintAIDDescriptionBuf(json_t *root, uint8_t *aid, size_t aidlen, bool verbose) {
return PrintAIDDescription(root, sprint_hex_inrow(aid, aidlen), verbose);
2019-11-05 03:18:37 +08:00
}
2019-11-05 01:54:23 +08:00