Added Attack module

This commit is contained in:
Spacehuhn 2022-07-03 19:13:32 +02:00
parent fd2b5506fd
commit e1c9523ed3
3 changed files with 93 additions and 72 deletions

View File

@ -8,9 +8,9 @@
#include "src/keyboard/keyboard.h"
#include "src/led/led.h"
#include "src/duckparser/duckparser.h"
#include "src/msc/msc.h"
#include "src/selector/selector.h"
#include "src/attack/attack.h"
enum Mode {
SETUP, ATTACK
@ -49,7 +49,7 @@ void setup() {
if (mode == ATTACK) {
led::setColor(255, 0, 0); // Turn LED red
start_attack(); // Start attack
attack::start(); // Start attack
led::setColor(0, 255, 0); // Turn LED green
mode = SETUP;
}
@ -63,78 +63,10 @@ void setup() {
led::setColor(255, 0, 0);
// Start running keystroke injection attack
start_attack();
attack::start();
}
debugln("[Finished]");
}
void loop() {}
void start_attack() {
// Open main BadUSB script
msc::open("/payload.script");
// Wait 1s to give the computer time to initialize the keyboard
delay(1000);
// Read and parse file
char buffer[READ_BUFFER];
size_t len = 0;
uint32_t prev_pos = 0;
uint32_t cur_pos = 0;
int repeats = 0;
// For LOOP_START and LOOP_END
uint32_t start_pos = 0;
int loops = 0;
while (true) {
debug("Reading line...");
if (!msc::getInLine()) cur_pos = msc::getPosition();
len = msc::readLine(buffer, READ_BUFFER);
debugln(len);
debugln(std::string(buffer, len-1).c_str());
// Reached end of file
if (len == 0) {
debugln("Reached end of file");
if (msc::openNextFile()) continue;
else break;
}
debug("Parsing...");
duckparser::parse(buffer, len);
// For REPEAT/REPLAY
repeats = duckparser::getRepeats();
for (int i = 0; i<repeats; ++i) {
msc::gotoPosition(prev_pos);
do {
len = msc::readLine(buffer, READ_BUFFER);
duckparser::parse(buffer, len);
} while (msc::getInLine());
}
if (!msc::getInLine()) prev_pos = cur_pos;
// For LOOP_START/LOOP_STOP
if (duckparser::loopBegin()) {
start_pos = msc::getPosition();
loops = duckparser::getLoops();
} else if (duckparser::loopEnd() && (loops > 1)) {
msc::gotoPosition(start_pos);
--loops;
}
// For IMPORT
if (duckparser::import()) {
std::string path = duckparser::getImport();
msc::open(path.c_str());
}
debugln("OK");
}
}
void loop() {}

82
src/attack/attack.cpp Normal file
View File

@ -0,0 +1,82 @@
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/usbnova */
#include "attack.h"
#include "../../config.h"
#include "../../debug.h"
#include "../msc/msc.h"
#include "../duckparser/duckparser.h"
namespace attack {
// ====== PRIVATE ====== //
// ====== PUBLIC ====== //
void start() {
// Open main BadUSB script
msc::open("/payload.script");
// Wait 1s to give the computer time to initialize the keyboard
delay(1000);
// Read and parse file
char buffer[READ_BUFFER];
size_t len = 0;
uint32_t prev_pos = 0;
uint32_t cur_pos = 0;
int repeats = 0;
// For LOOP_START and LOOP_END
uint32_t start_pos = 0;
int loops = 0;
while (true) {
debug("Reading line...");
if (!msc::getInLine()) cur_pos = msc::getPosition();
len = msc::readLine(buffer, READ_BUFFER);
debugln(len);
debugln(std::string(buffer, len-1).c_str());
// Reached end of file
if (len == 0) {
debugln("Reached end of file");
if (msc::openNextFile()) continue;
else break;
}
debug("Parsing...");
duckparser::parse(buffer, len);
// For REPEAT/REPLAY
repeats = duckparser::getRepeats();
for (int i = 0; i<repeats; ++i) {
msc::gotoPosition(prev_pos);
do {
len = msc::readLine(buffer, READ_BUFFER);
duckparser::parse(buffer, len);
} while (msc::getInLine());
}
if (!msc::getInLine()) prev_pos = cur_pos;
// For LOOP_START/LOOP_STOP
if (duckparser::loopBegin()) {
start_pos = msc::getPosition();
loops = duckparser::getLoops();
} else if (duckparser::loopEnd() && (loops > 1)) {
msc::gotoPosition(start_pos);
--loops;
}
// For IMPORT
if (duckparser::import()) {
std::string path = duckparser::getImport();
msc::open(path.c_str());
}
debugln("OK");
}
}
}

7
src/attack/attack.h Normal file
View File

@ -0,0 +1,7 @@
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/usbnova */
#pragma once
namespace attack {
void start();
}