So i was lucky enough to be given a teensy 2.0 with a microSD reader by David (ReL1L) Kennedy whilst i was at B-Sides Vegas. Lucky me!

After a quick chat with Dave he mentioned that the teensy was a “send to PC” only device. This got me thinking. If it’s possible for a normal keyboard to read the state of the Caps, Scroll and Num locks from the PC then the teensy should be able to do it as well.

My idea in the long term is to use this ‘feature’ as a way of exporting data to a keyboard (at least that’s what the PC will think it is!).

It’s my first time playing with a teensy, or any micro controller in fact, so this was a little bit of a hurdle. I wired up the Teensy with the microSD reader and added an LCD to help with the debugging. (Note: When the teensy emulates a keyboard it can no longer act as a serial device relaying info back and forth the PC <-- The reason I used an LCD) KeyboardLockKeys

A little bit of playing with the code and I ended with this. What it does so far is save the state of the keyboard to the MicroSD. Simple really. The next idea is to implement Dave’s SET tools to create a payload that will somehow convert files into a series of representation states of the keyboard LEDs. I could then inject the payload and export code all from the comfort of nothing more than a ‘keyboard’!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <LiquidCrystal.h>
#include <usb_private.h>
#include <SD.h>

LiquidCrystal lcd(8, 9, 10, 4, 5, 6, 7);
//                0 = RS   4 = D4   7 = D7
//                   1 = R/w  5 = D5
//                      2 = E    6 = D6
const int ledPin = 11;
const int chipSelect = 0;

void LCDpos(int Lcol, int Lrow) {lcd.setCursor(Lcol, Lrow);
  if(Lrow > 1) lcd.setCursor(Lcol + 16, Lrow - 2);}
int ledkeys(void){return int(keyboard_leds);}
boolean IsNumbOn(){if ((ledkeys() & 1) == 1){return true;} else {return false;}}
boolean IsCapsOn(){if ((ledkeys() & 2) == 2){return true;} else {return false;}}
boolean IsScrlOn(){if ((ledkeys() & 4) == 4){return true;} else {return false;}}

void setup() {
  pinMode(ledPin, OUTPUT);
  lcd.begin(16, 4);
  for(int i = 0; i < 10; i++){digitalWrite(ledPin, HIGH);delay(50);
    digitalWrite(ledPin, LOW);delay(50);}

  LCDpos(0,0); lcd.print("KeyboardLockKeys");
  LCDpos(0,1); lcd.print("NUM    =        ");
  LCDpos(0,2); lcd.print("CAPS   =        ");
  LCDpos(0,3); lcd.print("SCROLL =        ");
 
  if (!SD.begin(chipSelect)) {
    lcd.clear();
    LCDpos(0,0); lcd.print("Card failed     ");
    LCDpos(0,1); lcd.print("or not present! ");  
    delay(99999999);
  }
 
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {dataFile.println("NewDataSet") ; dataFile.close();}  
  else {lcd.clear();LCDpos(0,0); lcd.print("error opening");LCDpos(0,1); lcd.print("datalog.txt");}
}

void loop() {
  String dataString = ledkeys();
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {dataFile.println(dataString) ; dataFile.close();}  
  else {lcd.clear();LCDpos(0,0); lcd.print("error opening");LCDpos(0,1); lcd.print("datalog.txt");}
  //LCDpos(15,3); lcd.print(dataString);  //debug line
  if (IsNumbOn()){LCDpos(8,1); lcd.print("ON ");  //NumbLockCheck
  } else {LCDpos(8,1); lcd.print("OFF");}
  if (IsCapsOn()){LCDpos(8,2); lcd.print("ON ");  //CapsLockCheck
  } else {LCDpos(8,2); lcd.print("OFF");}
  if (IsScrlOn()){LCDpos(8,3); lcd.print("ON ");  //ScrlLockCheck
  } else {LCDpos(8,3); lcd.print("OFF");}
 
  delay(50);
}

TeensyLCD
Created using the fantastic Fritzing!

4 Responses to Teensy – KeyboardLockKeys

Leave a Reply