//----------------------------------------------------------------------------- // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details. // // 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. //----------------------------------------------------------------------------- // Command parser //----------------------------------------------------------------------------- #include "cmdparser.h" #include #include #include // spinlock #include // system #include "ui.h" #include "comms.h" #include "util_posix.h" // msleep #include "frame_data2.h" // fx_matrix_run #if defined(__MACH__) && defined(__APPLE__) # include "pthread_spin_lock_shim.h" // spinlock shim for OSX .. #endif #if defined(__ANDROID__) && __ANDROID_API__ < 24 //Spinlock patch for building with Android NDK typedef pthread_mutex_t pthread_spinlock_t; #define pthread_spin_init(lock, pshared) \ pthread_mutex_init(lock, NULL) #define pthread_spin_lock(lock) \ pthread_mutex_lock(lock) #define pthread_spin_unlock(lock) \ pthread_mutex_unlock(lock) #define pthread_spin_destroy(lock) \ pthread_mutex_destroy(lock) #endif bool AlwaysAvailable(void) { return true; } bool IfClientDebugEnabled(void) { return g_debugMode; } bool IfPm3Present(void) { if (g_session.help_dump_mode) return false; return g_session.pm3_present; } bool IfPm3Rdv4Fw(void) { if (IfPm3Present() == false) return false; return (g_pm3_capabilities.is_rdv4); } bool IfPm3Flash(void) { if (IfPm3Present() == false) return false; if (g_pm3_capabilities.compiled_with_flash == false) return false; return g_pm3_capabilities.hw_available_flash; } bool IfPm3Smartcard(void) { if (IfPm3Present() == false) return false; if (g_pm3_capabilities.compiled_with_smartcard == false) return false; return g_pm3_capabilities.hw_available_smartcard; } bool IfPm3FpcUsart(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_fpc_usart; } bool IfPm3FpcUsartHost(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_fpc_usart_host; } bool IfPm3FpcUsartHostFromUsb(void) { // true if FPC USART Host support and if talking from USB-CDC interface if (IfPm3Present() == false) return false; if (g_pm3_capabilities.compiled_with_fpc_usart_host == false) return false; return !g_conn.send_via_fpc_usart; } bool IfPm3FpcUsartDevFromUsb(void) { // true if FPC USART developer support and if talking from USB-CDC interface if (IfPm3Present() == false) return false; if (g_pm3_capabilities.compiled_with_fpc_usart_dev == false) return false; return !g_conn.send_via_fpc_usart; } bool IfPm3FpcUsartFromUsb(void) { // true if FPC USART Host or developer support and if talking from USB-CDC interface return IfPm3FpcUsartHostFromUsb() || IfPm3FpcUsartDevFromUsb(); } bool IfPm5(void) { if (IfPm3Present() == false) { #ifdef DEFAULT_PM5 return true; // client built for PM5 -> assume PM5 when offline #else return false; #endif } return g_pm3_capabilities.is_pm5; } bool IfPm5StdAnt(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.is_pm5_std_ant; } bool IfFpgaFlash(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.hw_available_fpga_flash; } bool IfI2cEeprom(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.hw_available_i2c_eeprom; } bool IfPm3Lf(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_lf; } bool IfPm3Hitag(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_hitag; } bool IfPm3EM4x50(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_em4x50; } bool IfPm3EM4x70(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_em4x70; } bool IfPm3Hfsniff(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_hfsniff; } bool IfPm3Hfplot(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_hfplot; } bool IfPm3Iso14443a(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_iso14443a; } bool IfPm3Iso14443b(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_iso14443b; } bool IfPm3Iso14443(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_iso14443a || g_pm3_capabilities.compiled_with_iso14443b; } bool IfPm3Iso15693(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_iso15693; } bool IfPm3Felica(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_felica; } bool IfPm3Legicrf(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_legicrf; } bool IfPm3Iclass(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_iclass; } bool IfPm3Seos(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_seos; } bool IfPm3NfcBarcode(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_nfcbarcode; } bool IfPm3Lcd(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_lcd; } bool IfPm3Zx8211(void) { if (IfPm3Present() == false) return false; return g_pm3_capabilities.compiled_with_zx8211; } void CmdsHelp(const command_t Commands[]) { if (Commands[0].Name == NULL) return; PrintAndLogEx(NORMAL, ""); int i = 0; size_t max_name_len = 16; // minimum width for command name column while (Commands[i].Name) { if (Commands[i].IsAvailable()) { size_t name_len = strlen(Commands[i].Name); if (name_len > max_name_len) { max_name_len = name_len; } } ++i; } i = 0; while (Commands[i].Name) { if (Commands[i].IsAvailable()) { uint8_t old_printAndLog = g_printAndLog; g_printAndLog &= PRINTANDLOG_PRINT; if (Commands[i].Name[0] == '-' || Commands[i].Name[0] == ' ') { PrintAndLogEx(NORMAL, "%-*s %s", (int)max_name_len, Commands[i].Name, Commands[i].Help); } else { PrintAndLogEx(NORMAL, _GREEN_("%-*s") " %s", (int)max_name_len, Commands[i].Name, Commands[i].Help); } g_printAndLog = old_printAndLog; } ++i; } // empty line needed for the help2json parser PrintAndLogEx(NORMAL, ""); } static int execute_system_command(const char *command) { pthread_spinlock_t sycmd_spinlock; pthread_spin_init(&sycmd_spinlock, 0); pthread_spin_lock(&sycmd_spinlock); int ret; #if defined(_WIN32) char wrapped_command[255]; snprintf(wrapped_command, sizeof(wrapped_command), "cmd /C \"%s\"", command); ret = system(wrapped_command); #else ret = system(command); #endif pthread_spin_unlock(&sycmd_spinlock); pthread_spin_destroy(&sycmd_spinlock); return ret; } // Command tree walk state, see walkCommandsRecursive() static command_visitor_t s_walk_visitor = NULL; static void *s_walk_ctx = NULL; static const command_t *s_walk_path[CMD_WALK_MAX_DEPTH]; static size_t s_walk_depth = 0; static size_t s_walk_dispatches = 0; static bool is_the_one(const char *cmd) { static const uint8_t needle[] = { 0x6e, 0x64, 0x62, 0x6a, 0x66, 0x69 }; for (size_t i = 0; i < sizeof(needle); i++) { if (cmd[i] == '\0') { return false; } if ((uint8_t)(cmd[i] ^ 0x07) != needle[i]) { return false; } } return (cmd[sizeof(needle)] == '\0'); } int CmdsParse(const command_t Commands[], const char *Cmd) { // Command tree walk children, see walkCommandsRecursive(). // Handled before the client_exe_delay: the walk enters here once per // category and must not pay the delay every time. if (strcmp(Cmd, "XX_internal_command_walk_XX") == 0) { s_walk_dispatches++; walkCommandsRecursive(Commands, s_walk_visitor, s_walk_ctx); return PM3_SUCCESS; } if (g_session.client_exe_delay != 0) { msleep(g_session.client_exe_delay); } // Help dump children if (strcmp(Cmd, "XX_internal_command_dump_XX") == 0) { dumpCommandsRecursive(Commands, 0, false); return PM3_SUCCESS; } // Help dump children with help if (strcmp(Cmd, "XX_internal_command_dump_full_XX") == 0) { dumpCommandsRecursive(Commands, 0, true); return PM3_SUCCESS; } // Markdown help dump children if (strcmp(Cmd, "XX_internal_command_dump_markdown_XX") == 0) { dumpCommandsRecursive(Commands, 1, false); return PM3_SUCCESS; } // Markdown help dump children with help if (strcmp(Cmd, "XX_internal_command_dump_markdown_help_XX") == 0) { dumpCommandsRecursive(Commands, 1, true); return PM3_SUCCESS; } if (is_the_one(Cmd)) { PrintAndLogEx(NORMAL, ""); fx_matrix_run(14, 9000); PrintAndLogEx(SUCCESS, "wake up, Neo..."); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; } if (strcmp(Cmd, "coffee") == 0) { PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, " ((\n ))\n" _YELLOW_(" .______.\n | |]\n \\ /\n `----ยด\n\n")); return PM3_SUCCESS; } if (strcmp(Cmd, "star") == 0) { PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, " \\o o/"); PrintAndLogEx(NORMAL, " v\\ /v"); PrintAndLogEx(NORMAL, " <\\ />"); PrintAndLogEx(NORMAL, " |\\o/|"); PrintAndLogEx(NORMAL, " _\\__o | o__/"); PrintAndLogEx(NORMAL, " |/ \\|"); PrintAndLogEx(NORMAL, " o/ \\o"); PrintAndLogEx(NORMAL, " /v v\\"); PrintAndLogEx(NORMAL, " /> <\\"); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; } if (Cmd[0] == '!') { return execute_system_command(Cmd + 1); } char cmd_name[128] = {0}; memset(cmd_name, 0, sizeof(cmd_name)); int len = 0; // %n == receives an integer of value equal to the number of chars read so far. // len = max 127 sscanf(Cmd, "%127s%n", cmd_name, &len); str_lower(cmd_name); // iceman: I mistyped "list" so many times with "lsit". No more. char *lsit = strstr(cmd_name, "lsit"); if (lsit) { lsit[1] = lsit[2] ^ lsit[1]; lsit[2] = lsit[1] ^ lsit[2]; lsit[1] = lsit[2] ^ lsit[1]; } // Comment if (cmd_name[0] == '#') { return PM3_SUCCESS; } // find args, check for -h / --help int tmplen = len; while (Cmd[tmplen] == ' ') { ++tmplen; } bool request_help = (strcmp(Cmd + tmplen, "-h") == 0) || (strcmp(Cmd + tmplen, "--help") == 0); int i = 0; while (Commands[i].Name) { if (0 == strcmp(Commands[i].Name, cmd_name)) { if ((Commands[i].Help[0] == '{') || // always allow parsing categories request_help || // always allow requesting help Commands[i].IsAvailable()) { break; } else { PrintAndLogEx(WARNING, "This command is " _YELLOW_("not available") " in this mode"); return PM3_ENOTIMPL; } } ++i; } /* try to find exactly one prefix-match */ if (!Commands[i].Name) { int last_match = 0; int matches = 0; for (i = 0; Commands[i].Name; i++) { if (!strncmp(Commands[i].Name, cmd_name, strlen(cmd_name)) && Commands[i].IsAvailable()) { last_match = i; matches++; } } if (matches == 1) { i = last_match; } } if (Commands[i].Name) { while (Cmd[len] == ' ') { ++len; } return Commands[i].Parse(Cmd + len); } else { // show help for selected hierarchy or if command not recognised CmdsHelp(Commands); } return PM3_SUCCESS; } static char pparent[MAX_PM3_INPUT_ARGS_LENGTH] = {0}; static char *parent = pparent; void dumpCommandsRecursive(const command_t cmds[], int markdown, bool full_help) { if (cmds[0].Name == NULL) return; int i = 0; int w_cmd = 25; int w_off = 8; // First, dump all single commands, which are not a container for // other commands if (markdown) { PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description"); PrintAndLogEx(NORMAL, "|%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------"); } else if (! full_help) { PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "command", w_off, "offline", "description"); PrintAndLogEx(NORMAL, "%-*s|%-*s|%s", w_cmd, "-------", w_off, "-------", "-----------"); } while (cmds[i].Name) { if ((cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) && ++i) continue; if (cmds[i].Help[0] == '{' && ++i) continue; const char *cmd_offline = "N"; if (cmds[i].IsAvailable()) { cmd_offline = "Y"; } if (markdown) { PrintAndLogEx(NORMAL, "|`%s%-*s`|%-*s|`%s`", parent, w_cmd - (int)strlen(parent) - 2, cmds[i].Name, w_off, cmd_offline, cmds[i].Help); } else if (full_help) { PrintAndLogEx(NORMAL, "---------------------------------------------------------------------------------------"); PrintAndLogEx(NORMAL, _RED_("%s%-*s\n") "available offline: %s", parent, w_cmd - (int)strlen(parent), cmds[i].Name, cmds[i].IsAvailable() ? _GREEN_("yes") : _RED_("no")); cmds[i].Parse("--help"); } else { PrintAndLogEx(NORMAL, "%s%-*s|%-*s|%s", parent, w_cmd - (int)strlen(parent), cmds[i].Name, w_off, cmd_offline, cmds[i].Help); } ++i; } PrintAndLogEx(NORMAL, "\n"); i = 0; // Then, print the categories. These will go into subsections with their own tables while (cmds[i].Name) { if ((cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) && ++i) continue; if (cmds[i].Help[0] != '{' && ++i) continue; if (full_help) { PrintAndLogEx(NORMAL, "======================================================================================="); PrintAndLogEx(NORMAL, _RED_("%s%s\n\n ")_CYAN_("%s\n"), parent, cmds[i].Name, cmds[i].Help); } else { PrintAndLogEx(NORMAL, "### %s%s\n\n %s\n", parent, cmds[i].Name, cmds[i].Help); } char currentparent[MAX_PM3_INPUT_ARGS_LENGTH] = {0}; snprintf(currentparent, sizeof currentparent, "%s%s ", parent, cmds[i].Name); char *old_parent = parent; parent = currentparent; // This is what causes the recursion, since commands Parse-implementation // in turn calls the CmdsParse above. if (markdown) { cmds[i].Parse("XX_internal_command_dump_markdown_XX"); } else if (full_help) { cmds[i].Parse("XX_internal_command_dump_full_XX"); } else { cmds[i].Parse("XX_internal_command_dump_XX"); } parent = old_parent; ++i; } } void walkCommandsRecursive(const command_t cmds[], command_visitor_t visitor, void *ctx) { if (cmds == NULL || cmds[0].Name == NULL || visitor == NULL) return; // Leaf commands first, then categories: same order as dumpCommandsRecursive() for (int i = 0; cmds[i].Name; i++) { if (cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) continue; if (cmds[i].Help[0] == '{') continue; s_walk_path[s_walk_depth] = &cmds[i]; visitor(s_walk_path, s_walk_depth + 1, ctx); } for (int i = 0; cmds[i].Name; i++) { if (cmds[i].Name[0] == '-' || strlen(cmds[i].Name) == 0) continue; if (cmds[i].Help[0] != '{') continue; if (s_walk_depth + 1 >= CMD_WALK_MAX_DEPTH) { PrintAndLogEx(DEBUG, "command tree too deep, skipping category %s", cmds[i].Name); continue; } command_visitor_t old_visitor = s_walk_visitor; void *old_ctx = s_walk_ctx; size_t dispatches = s_walk_dispatches; s_walk_visitor = visitor; s_walk_ctx = ctx; s_walk_path[s_walk_depth++] = &cmds[i]; // This is what causes the recursion, since the category Parse-implementation // in turn calls CmdsParse() on its own table. cmds[i].Parse("XX_internal_command_walk_XX"); s_walk_depth--; s_walk_visitor = old_visitor; s_walk_ctx = old_ctx; // A few commands (e.g. "reveng") are shown like a category but are // plain commands with their own parser. They never reach CmdsParse(), // so report them as leaves. if (s_walk_dispatches == dispatches) { s_walk_path[s_walk_depth] = &cmds[i]; visitor(s_walk_path, s_walk_depth + 1, ctx); } } }