Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture any kind of keystrokes (aka keylogger), preferably c# .net but any kind will do [closed]

I need to capture everything that I type on my keyboard and then store it in numerous ways. I'd prefer it to be written in C# for .Net, but anything will do really. My reasons to write this "keylogger" are simple:

Recently I became an owner of a Peregrine gaming glove. It's a very cool thing that allows you to issue commands by making gestures with your fingers, and at the same time, its a very thin glove so you can type with that hand with little discomfort.

Also, I have found a nice program called AutoHotkey that can severely boost your productivity by making macros for like any action. You can bind any key to any other key or series of keys or commands.

The problem is, you cannot tell it just like that "this is what I'm doing most" and "this is what I'm rarely using." Really, can you tell what key do you use more, page down or down? Do you use alt+tab more frequently that escape or layout switch (ctrl-shift or alt-shift)? I cannot tell that. I cannot tell which actions should I automate or switch to the more easy interface, without statistical data.

So I want to write a program to run in the background and log everything I type. This program will then store first, second and third order histogram of my actions (like, it will store how many times I pressed any single key, like entering, how many times I pressed a succession of two keys, like alt and then tab, and how many times I pressed a succession of three keys, like ctrl, alt and then deleted or ctrl,shift and then escape)

Then, after some time spent working/playing/whatever, I'll have information on what kind of actions should I try to bind to that interface (the glove) or automate with AutoHotkey program, to improve the speed of interacting with a PC.

In other words, simple science experiment, just for fun and progress :)

like image 645
Istrebitel Avatar asked Jun 24 '11 08:06

Istrebitel


People also ask

What does a keylogger capture?

A keylogger, sometimes called a keystroke logger or keyboard capture, is a type of surveillance technology used to monitor and record each keystroke on a specific computer. Keylogger software is also available for use on smartphones, such as the Apple iPhone and Android devices.

What type of malware can capture keystrokes?

Short for “keystroke logging,” a keylogger is a type of malicious software that records every keystroke you make on your computer. Keyloggers are a type of spyware — malware designed to spy on victims. Because they can capture everything you type, keyloggers are one of the most invasive forms of malware.

Which is an example of a keylogger?

Examples of these include: Keyboard hardware - These loggers take the form of a piece of hardware inserted somewhere between the computer keyboard and the computer, typically along the keyboard's cable connection.

Can I put a keylogger on my own computer?

Plus there are plenty of other perfectly legal use cases for installing a keylogger on computers. Keylogging goes south and becomes a threat if there is malicious intent. Simply put, if you install a keylogger on a device you own, it is legal.


2 Answers

Old question but...

In Windows you can use the APIs of the user32.dll.

For a very simple keylogger you can use the method GetAsyncKeyState() checking whether each character of the ASCII table is pressed.

The whole code for a very simple and stupid keylogger written in a Console Application would be:

[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
    
static void Main(string[] args)
{
    while (true)
    {
        Thread.Sleep(100);
    
        for (int i = 0; i < 255; i++)
        {
            int keyState = GetAsyncKeyState(i);
            // replace -32767 with 32769 for windows 10.
            if (keyState == 1 || keyState == -32767)
            {
                Console.WriteLine((char)i);
                break;
            }
        }
    }

KeystrokeAPI

For those who are looking for something more robust and cleaner I've created an API that makes it easy. You only need to do this:

api.CreateKeyboardHook((character) => { Console.Write(character); });

More details here: https://github.com/fabriciorissetto/KeystrokeAPI

like image 51
fabriciorissetto Avatar answered Sep 25 '22 21:09

fabriciorissetto


You need a global keyboard handler.

If this is windows you will have to use the Windows API and use the following:

SetWndowsHookEx

UnhookWindowsHookEx

CallNextHookEx

You need to set up a global "hook" to listen for keyboard and mouse events.

I have a C# class that does this and raises .Net keyboard and mouse event args, but I am not going to post the code online as this type of activity can be used for nefarious means.

Why did we do this? We embedded powerpoint into our application, but we didn't want the user editing the powerpoint slides while viewing them so from our C# application we set up a hook to monitor and intercept mouse and keyboard events if they were in the powerpoint control. Worked great but we had to make sure:

They were in control in our application, our application was active, etc. Once you set up the hook, it listens/captures for everything including other applications, since we were canceling specific keyboard actions we only wanted it when we needed it.

Here's an API link.

http://msdn.microsoft.com/en-us/library/ff468842(v=VS.85).aspx

like image 23
Jon Raynor Avatar answered Sep 22 '22 21:09

Jon Raynor