USBNova/USBNova.ino

160 lines
5.0 KiB
Arduino
Raw Permalink 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-11-02 04:02:15 -07:00
#include "src/msc/format.h"
2022-07-16 03:44:41 -07:00
void update() {
led::update();
// cli::update();
}
void setup() {
2022-10-31 11:05:57 -07:00
led::init();
2023-06-28 06:08:59 -07:00
2022-07-16 03:44:41 -07:00
// Start Serial (for debug)
debug_init();
2022-07-16 03:44:41 -07:00
// Initialize memory and check for problems
if (!msc::init()) {
2022-11-01 04:23:14 -07:00
debugln("Couldnt init SD");
led::setColor(255, 0, 0, 200);
2023-06-28 06:08:59 -07:00
while (true) {
tasks::update();
2022-10-31 11:05:57 -07:00
}
2022-07-16 03:44:41 -07:00
return;
}
2022-07-16 03:44:41 -07:00
2022-11-02 06:17:30 -07:00
// Read mode from selector switch
selector::init();
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();
2023-06-28 06:08:59 -07:00
if (selector::mode() == SETUP) preferences::save();
2022-07-06 15:30:53 -07:00
2023-06-28 06:08:59 -07:00
if (selector::position() == 1) {
hid::setID(preferences::getVID1(), preferences::getPID1(), preferences::getVersion1());
hid::setSerial(preferences::getSerial1());
hid::setManufacturer(preferences::getManufacturer1());
hid::setProduct(preferences::getProduct1());
2022-11-02 06:17:30 -07:00
} else {
2023-06-28 06:08:59 -07:00
hid::setID(preferences::getVID2(), preferences::getPID2(), preferences::getVersion2());
hid::setSerial(preferences::getSerial2());
hid::setManufacturer(preferences::getManufacturer2());
hid::setProduct(preferences::getProduct2());
2022-11-02 06:17:30 -07:00
}
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-02 03:14:29 -07: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());
2022-11-02 04:02:15 -07:00
}
// 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
2022-11-02 04:08:38 -07:00
// Create 1.txt file if it doesn't exist yet
if (msc::find(1) == "") {
msc::write("1.txt", "# Hello World\n", 14);
}
2023-06-28 06:08:59 -07:00
2022-11-02 04:08:38 -07:00
// Create 2.txt file if it doesn't exist yet
if (msc::find(2) == "") {
msc::write("2.txt", "# Hello World\n", 14);
}
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
2023-06-28 06:08:59 -07:00
2022-11-02 03:01:33 -07:00
// Don't run again
2023-06-28 06:08:59 -07:00
while (true) {
2022-11-02 03:01:33 -07:00
tasks::update();
cli::update();
}
2022-10-31 12:53:17 -07:00
} 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
}
}
}