From f823e92a41c9f51ec1107d1232bede2cfcd29202 Mon Sep 17 00:00:00 2001 From: Spacehuhn Date: Sat, 23 Jul 2022 18:14:15 +0200 Subject: [PATCH] Added CLI yoooo --- src/attack/attack.cpp | 10 +++-- src/attack/attack.h | 1 + src/cli/cli.cpp | 79 ++++++++++++++++++++++++++++----- src/msc/msc.cpp | 21 +++++++++ src/msc/msc.h | 1 + src/preferences/preferences.cpp | 58 ++++++++++++++++++++++++ src/preferences/preferences.h | 2 + 7 files changed, 158 insertions(+), 14 deletions(-) diff --git a/src/attack/attack.cpp b/src/attack/attack.cpp index d3b3c77..508b11c 100644 --- a/src/attack/attack.cpp +++ b/src/attack/attack.cpp @@ -16,10 +16,10 @@ namespace attack { // ====== PRIVATE ====== // // ====== PUBLIC ====== // - void start() { + void start(const char* path) { // If script doesn't exist, don't do anything - if (!msc::exists(preferences::getMainScript().c_str())) return; - + if (!msc::exists(path)) return; + // Set attack color led::setColor(preferences::getAttackColor()); @@ -94,4 +94,8 @@ namespace attack { debugln("OK"); } } + + void start() { + start(preferences::getMainScript().c_str()); + } } \ No newline at end of file diff --git a/src/attack/attack.h b/src/attack/attack.h index 1d38104..f662f87 100644 --- a/src/attack/attack.h +++ b/src/attack/attack.h @@ -3,5 +3,6 @@ #pragma once namespace attack { + void start(const char* path); void start(); } \ No newline at end of file diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index aa38e05..2d41ce8 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -5,6 +5,12 @@ #include #include "../duckparser/duckparser.h" +#include "../preferences/preferences.h" +#include "../selector/selector.h" +#include "../led/led.h" +#include "../attack/attack.h" +#include "../msc/msc.h" +#include "../../config.h" #define BUFFER_SIZE 1024 @@ -54,27 +60,78 @@ namespace cli { // ====== PUBLIC ====== // void init() { Serial.begin(115200); - /* - // help - cli.addCmd("format", [](cmd* c) { - cli.help(); + + // error + cli.setOnError([](cmd_error* e) { + CommandError cmdError(e); + + Serial.print("ERROR: "); + Serial.println(cmdError.toString()); + + if (cmdError.hasCommand()) { + Serial.print("Did you mean \""); + Serial.print(cmdError.getCommand().toString()); + Serial.println("\"?"); + } }); + // help + cli.addCmd("help", [](cmd* c) { + Serial.println("[ = Available Commands =]"); + Serial.println(cli.toString()); + Serial.println("Enter any BadUSB Scripts to run it."); + }).setDescription(" Get a list of available commands."); + // version - cli.addCmd("format", [](cmd* c) { + cli.addCmd("version", [](cmd* c) { + Serial.println("[ = USB Nova =]"); + Serial.print("Version "); Serial.println(VERSION); - }); + Serial.println("Source: https://github.com/spacehuhntech/usbnova"); + Serial.println("Made with <3 by Spacehuhn (spacehuhn.com)"); + Serial.println(); + }).setDescription(" Print the firmware version."); // format cli.addCmd("format", [](cmd* c) { - msc::format(); - }); + led::setColor(255, 255, 255); + msc::format(preferences::getDriveName().c_str()); + preferences::save(); + if(selector::mode() == SETUP) { + led::setColor(preferences::getSetupColor()); + } else { + led::setColor(preferences::getIdleColor()); + } + + Serial.println("Done formatting!"); + Serial.println(); + }).setDescription(" Fromat the internal memory."); // reset - cli.addCmd("rest", [](cmd* c) { + cli.addCmd("reset", [](cmd* c) { preferences::reset(); - }); - */ + preferences::save(); + + Serial.println("Done resetting!"); + Serial.println(); + }).setDescription(" Reset the preferences."); + + // TODO: preferences + cli.addSingleArgCmd("preferences", [](cmd* c) { + preferences::print(); + }).setDescription(" Print preferences."); + + // ls + cli.addSingleArgCmd("ls", [](cmd* c) { + msc::print(); + }).setDescription(" Show available files on the drive."); + + // run + cli.addSingleArgCmd("run", [](cmd* c) { + Command cmd(c); + Argument arg = cmd.getArgument(0); + attack::start(arg.getValue().c_str()); + }).setDescription(" Start a BadUSB Script."); } void update() { diff --git a/src/msc/msc.cpp b/src/msc/msc.cpp index 7508600..bd8fc18 100644 --- a/src/msc/msc.cpp +++ b/src/msc/msc.cpp @@ -90,6 +90,27 @@ namespace msc { bool format(const char* drive_name) { return format::start(drive_name); } + + void print() { + File file; + FatFile rdir; + rdir.open("/"); + + // Open next file in root. + // Warning, openNext starts at the current directory position + // so a rewind of the directory may be required. + while(file.openNext(&rdir, O_RDONLY)) { + file.printFileSize(&Serial); + Serial.write(' '); + file.printName(&Serial); + if (file.isDir()) { + // Indicate a directory. + Serial.write('/'); + } + Serial.println(); + file.close(); + } + } void setID(const char* vid, const char* pid, const char* rev) { usb_msc.setID(vid, pid, rev); // Max. 8, 16, 4 characters diff --git a/src/msc/msc.h b/src/msc/msc.h index 41310d1..4211893 100644 --- a/src/msc/msc.h +++ b/src/msc/msc.h @@ -7,6 +7,7 @@ namespace msc { bool init(); bool format(const char* drive_name = "USB Nova"); + void print(); void setID(const char* vid, const char* pid, const char* rev); void enableDrive(); diff --git a/src/preferences/preferences.cpp b/src/preferences/preferences.cpp index e36762f..d5944dd 100644 --- a/src/preferences/preferences.cpp +++ b/src/preferences/preferences.cpp @@ -193,6 +193,64 @@ namespace preferences { debug("Saved "); debugln(PREFERENCES_PATH); } + + void reset() { + enable_msc = false; + enable_led = true; + + hid_vid = "239A"; + hid_pid = "80CB"; + hid_rev = "0100"; + + msc_vid = "Adafruit"; + msc_pid = "External Flash"; + msc_rev = "1.0"; + + default_layout = "US"; + default_delay = 5; + + main_script = "main.script"; + + attack_color[0] = 128; + attack_color[1] = 0; + attack_color[2] = 0; + attack_color[3] = 0; + + setup_color[0] = 0; + setup_color[1] = 0; + setup_color[2] = 20; + setup_color[3] = 0; + + idle_color[0] = 0; + idle_color[1] = 30; + idle_color[2] = 0; + idle_color[3] = 0; + + format = false; + drive_name = "USB Nova"; + + disable_capslock = true; + run_on_indicator = false; + + initial_delay = 1000; + } + + void print() { + // Create a new JSON document (and string buffer) + DynamicJsonDocument json_doc(JSON_SIZE); + // JsonObject json_obj = json_doc.as(); + std::string json_str = { "" }; + + // Add values + toJson(json_doc); + + // Serialize JSON to buffer + serializeJsonPretty(json_doc, json_str); + json_doc.clear(); + + // Write the buffer to file (and print results) + debugln(json_str.c_str()); + } bool mscEnabled() { return enable_msc; diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h index 3c4d1eb..b23a0b3 100644 --- a/src/preferences/preferences.h +++ b/src/preferences/preferences.h @@ -7,6 +7,8 @@ namespace preferences { void load(); void save(); + void reset(); + void print(); bool mscEnabled(); bool ledEnabled();