Using BadUSB to Unlock LUKS

Sometimes you want a quick headless box where you have physical access - but don't want to deal with dropbear initrd or keys stored on USB drives.

#include <Arduino.h>
#include <Keyboard.h>

const char *passphrase = "hunter2\n";

void type_passphrase(bool bootup)
{
  if (bootup) delay(180000);

  Keyboard.begin();
  delay(5000);

  for (int i = 0; i < (int)strlen(passphrase); i++) {
    Keyboard.write(passphrase[i]);
    delay(100);
  }

  Keyboard.end();
  delay(1000);
}

void watchdog()
{
  char buf[8];
  size_t len;
  int deaf = 0;

  Serial.begin(115200);
  Serial.setTimeout(5000);

  do {
    memset(buf, 0, 8);

    Serial.flush();
    if ((len = Serial.readBytesUntil('\n', (char *)&buf, sizeof(buf))) <= 0) {
      deaf++;
    } else {
      if (!strncmp(buf, "ping", 4)) {
        deaf = 0;
      } else {
        deaf++;
      }
    }

    if (deaf >= 36) break;
  } while (true);

  Serial.end();
}

void setup()
{
  // type_passphrase(false);
  // type_passphrase(false);
}

void loop()
{
  type_passphrase(true);
  watchdog();
}