USBNova/USBNova.ino

134 lines
4.4 KiB
Arduino
Raw Normal View History

#include "config.h"
#include "debug.h"
#include "src/hid/hid.h"
#include "src/hid/keyboard.h"
#include "src/led/led.h"
#include "src/msc/msc.h"
#include "src/selector/selector.h"
2022-07-03 10:13:32 -07:00
#include "src/attack/attack.h"
#include "src/preferences/preferences.h"
2022-07-06 14:32:55 -07:00
#include "src/duckparser/duckparser.h"
#include "src/tasks/tasks.h"
2022-07-20 15:11:14 -07:00
#include "src/cli/cli.h"
2022-07-16 03:44:41 -07:00
void update() {
led::update();
// cli::update();
}
void setup() {
2023-06-27 07:37:04 -07:00
// Start Serial (for debug) or disable it
debug_init();
2022-07-16 03:44:41 -07:00
// Initialize memory and check for problems
if (!msc::init()) {
led::setColor(255, 0, 0, 200);
2022-07-16 03:44:41 -07:00
return;
}
2022-07-16 03:44:41 -07:00
// Load setting and set USB Device IDs
2022-11-25 12:11:25 -08:00
preferences::reset();
preferences::load();
hid::setID(preferences::getVID(), preferences::getPID(), preferences::getVersion());
2023-06-28 02:05:50 -07:00
hid::setSerial(preferences::getSerial());
2023-06-28 02:07:37 -07:00
hid::setManufacturer(preferences::getManufacturer());
2023-06-28 02:14:59 -07:00
hid::setProduct(preferences::getProduct());
2022-07-06 15:30:53 -07:00
2022-07-16 03:44:41 -07:00
// Read mode from selector switch
selector::init();
2023-06-27 07:37:04 -07:00
// Start Keyboard
2023-06-27 07:37:04 -07:00
if ((selector::mode() == ATTACK) || preferences::hidEnabled()) {
2022-11-11 03:13:44 -08:00
hid::init();
2023-06-27 07:37:04 -07:00
}
2022-07-16 03:44:41 -07:00
2022-07-06 14:21:07 -07:00
// Start USB Drive
2023-06-27 07:37:04 -07:00
if (preferences::mscEnabled() || (selector::mode() == SETUP)) {
2022-07-16 03:44:41 -07:00
msc::enableDrive();
}
2022-07-06 14:21:07 -07:00
2022-07-16 03:44:41 -07:00
// Start LED
2022-07-15 16:06:25 -07:00
led::init();
led::setEnable(preferences::ledEnabled());
2022-07-16 03:44:41 -07:00
if (selector::mode() == SETUP) {
led::setColor(preferences::getSetupColor());
} else {
led::setColor(preferences::getAttackColor());
}
// Attack settings
2022-07-15 16:06:25 -07:00
keyboard::setLocale(locale::get(preferences::getDefaultLayout().c_str()));
duckparser::setDefaultDelay(preferences::getDefaultDelay());
2022-07-14 13:30:00 -07:00
2022-07-16 03:44:41 -07:00
// Format Flash (if specified in preferences.json)
2022-07-15 15:36:31 -07:00
if ((selector::mode() == SETUP) && preferences::getFormat()) {
led::setColor(255, 255, 255);
2022-07-16 02:53:03 -07:00
msc::format(preferences::getDriveName().c_str());
}
// Create preferences file if it doesn't exist yet
if (!msc::exists(PREFERENCES_PATH)) {
2022-07-13 13:58:16 -07:00
preferences::save();
2022-07-13 13:09:51 -07:00
}
2023-06-27 07:37:04 -07:00
// Create main_script.txt if it doesn't exist yet
if (!msc::exists(preferences::getMainScript().c_str())) {
2022-11-16 07:07:36 -08:00
char message[21];
sprintf(message, "# USB Nova (v%s)\n", VERSION);
msc::write(preferences::getMainScript().c_str(), message, 20);
}
2022-07-13 13:58:16 -07:00
2022-07-16 03:44:41 -07:00
// Setup background tasks
tasks::setCallback(update);
2022-07-15 16:06:25 -07:00
// Make sure we don't start with a mode change
selector::changed();
2022-07-16 03:44:41 -07:00
// Start attack
2023-06-27 07:37:04 -07:00
if ((selector::mode() == ATTACK) && !preferences::getRunOnIndicator()) {
delay(preferences::getInitialDelay()); // Wait to give computer time to init keyboard
attack::start(); // Start keystroke injection attack
led::setColor(preferences::getIdleColor()); // Set LED to green
2022-07-15 16:06:25 -07:00
}
2022-07-20 15:11:14 -07:00
// Setup CLI
2023-06-27 07:37:04 -07:00
#ifdef ENABLE_DEBUG
2022-07-20 15:11:14 -07:00
cli::init();
2023-06-27 07:37:04 -07:00
#endif // ifdef ENABLE_DEBUG
2022-07-20 15:11:14 -07:00
2022-07-16 03:44:41 -07:00
debugln("[Started]");
}
2022-07-16 03:44:41 -07:00
void loop() {
2023-06-27 07:37:04 -07:00
tasks::update();
2022-07-20 15:11:14 -07:00
cli::update();
2023-06-27 07:37:04 -07:00
if (selector::read() != ATTACK) return;
2022-10-31 12:53:17 -07:00
// Only start the attack if run-on-indicator is disabled, or indicator actually changed
2023-06-27 07:37:04 -07:00
if (preferences::getRunOnIndicator() && hid::indicatorChanged()) {
2022-11-08 13:54:13 -08:00
delay(100);
2022-10-31 12:53:17 -07:00
attack::start(); // Run script
led::setColor(preferences::getIdleColor()); // Set LED to green
} else if (selector::changed()) {
2022-07-16 03:44:41 -07:00
// ========== Setup Mode ========== //
2023-06-27 07:37:04 -07:00
if ((selector::mode() == SETUP) && preferences::hidEnabled()) {
preferences::load(); // Reload the settings (in case the main script path changed)
// Attack settings
keyboard::setLocale(locale::get(preferences::getDefaultLayout().c_str()));
duckparser::setDefaultDelay(preferences::getDefaultDelay());
2023-06-27 07:37:04 -07:00
2022-07-16 03:44:41 -07:00
attack::start(); // Start keystroke injection attack
led::setColor(preferences::getSetupColor()); // Set LED to blue
}
// ========== Attack Mode ========== //
else if (selector::mode() == ATTACK) {
// Only start the attack if run-on-indicator is disabled, or indicator actually changed
2022-10-31 12:53:17 -07:00
attack::start(); // Run script
led::setColor(preferences::getIdleColor()); // Set LED to green
}
}
}