DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Generic and Dynamic API: MuleSoft
  • Microservices With .NET Core: Building Scalable and Resilient Applications
  • Enhanced API Security: Fine-Grained Access Control Using OPA and Kong Gateway
  • Exploring API Headers

Trending

  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • How Clojure Shapes Teams and Products
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  • Scalability 101: How to Build, Measure, and Improve It
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How To Make a Windows Keylogger By Yourself

How To Make a Windows Keylogger By Yourself

The hacker world can be contingently divided into three groups: the “skids”, “buyers”, and “black hat coders.” Find out how to utilize your skills!

By 
Roger Wells user avatar
Roger Wells
·
Updated Nov. 10, 20 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
42.9K Views

Join the DZone community and get the full member experience.

Join For Free

light up keyboard

Why Does an IT Security Specialist Need These Skills? 

Hacker world can be contingently divided into three groups: the so-called “skids” (script kiddies), “buyers”, and “black hat coders”. The first group includes beginners who use well-known codes and utilities to create something resembling simple malicious software. Buyers are teenagers and other thrill-seekers who buy such malware on the Net and use it to collect and sell personal and financial data from target devices.

The last group called “black hat coders” includes programming gurus writing the codes in a notebook and developing new exploits from scratch. Can anybody with good programming skills become one of the “black hat coders”? I doubt it but I believe any IT security specialist should know several concepts that are used to create malicious software. Always know your enemy:)

Keyloggers — General Info

A keylogger is a tool collecting and recording all keystrokes from the target device. Some keyloggers record the data in a hidden mode and transfer it to the spy via an online account. There are also keyloggers that require physical access to the target computer but they are not widely used. Keyloggers can be integrated into more complex software like trojans that ensure the data delivery back to the attacker. Without further ado, let’s create the basic keylogging features on Windows.

Making a Keylogger

Here I’m going to describe Windows programming methods you may find useful for developing a simple keylogger. The following guide is written especially for the Dzone website and is based on blog articles of the Spyrix company that develops keyloggers and more complex monitoring software.

We are going to make a keylogger on .Net C# with revoking system functions. I’ll describe the system functions shortly, anyway, I recommend you to look through the official documents from Microsoft in advance. The future keylogger will:

  1. record keystrokes;

  2. log an active window;

  3. block the process from a user without admin rights;

  4. stop the hotkey process. 

Required Skills and knowledge: C#, as well as Win API and DACL Windows knowledge. 

Now let’s look through several system code types that you’ll need while creating a keylogger for Windows. Each type will be stored in separate Enum. 

Hook types:

code snippet

To catch all the keyboard events, we will use WH_KEYBOARD_LL. All other hooks (except WH_MOUSE_LL) require creating a separate DLL. 

Keyboard event types are:

code

We’ll record the entered symbols at key-up events (WM_KEYUP).

Below, you can see the types for hooking a user’s shift from one window to another.

code

Enum for using hotkeys to exit the program. 

code

To embody all features, we need to create a form and hide it from the user. Overriding the basic SetVisibleCore method will be enough. 

code

The form will run at program startup. 

code

So now we have the form and we need to add the major feature — keylogging. For this purpose, we’ll use Win API > SetWindowsHookEx method. The parameters will be:

  • Hook type — WH_KEYBOARD_LL;
  • The callback function, meaning the method that will process all keyboard events;
  • Current module identifier;
  • Thread id — 0. 0 is used so that the hook would be associated with all threads.

code

Now let’s take a look at the method processing the hook. It includes several parameters:

  • nCode helps us understand whether we need to process the current event or convey it further;

  • wParam is the event type (a key-down or key-up);

  • lParam — the pressed symbol.

In fact, iParam is a byte array so it stores extra data like the number of symbols if the user holds the key and the processing machine is slow. 

code

This process records a symbol only after the key is up. To record the number of symbols, we need to implement the event with WM_KEYDOWN.

The SetKeysState method is used to know the state of extra keys, for instance, keys changing the register. 

code

GetKeyState is another Win API method that can be used to learn the state. 

code

In the GetSymbol method, GetKeyboardLayout of the current window is requested, then ToUnicodeEx is requested to get the symbol. Both of them are Win API methods. If the keys affecting the register are used, the symbol must be shifted to the uppercase. 

The above-mentioned steps are enough for the keylogging feature. But to record the currently active window, another hook is used. 

code

EVENT_SYSTEM_FOREGROUND helps us monitor active window change when WINEVENT_OUTOFCONTEXT shows that the callback method is in our application. 

Here we need to know the active window and its title.  

code

So each window change event will be recorded. In GetActiveWindowTitle, we will use just a couple of Win API methods: GetForegroundWindow — to learn the currently active window id; and GetWindowText — to request the title. 

code

Previously, all system function requests were taken from the User32 and Kernel32 library. The next step requires using the advapi32 library. 

Now we need to implement the settings in the way that a usual user couldn’t stop the keylogging process. First of all, we need to get the descriptor of this process and then change it by adding the record to DACL.

code

We get the process descriptor via GetKernelObjectSecurity. The method is called twice, at first, we get the size of the descriptor and then we get the process descriptor itself. 

code

After changing the descriptor, this information should be integrated into the current process. All we need to do is to turn the process id and descriptor to the SetKernelObjectSecurity system method.

code

The last step is stopping the hotkey process.

code

Here “key” is a key combination and “handle” is the identifier of the hidden form. To recognize the hotkey, “keyId” is created in the form. We can use keyld to check the operation of the hotkey. Everything is recorded via Win API RegisterHotKey method. 

And, finally, to stop the process, we override WndProc method in the form. 

code

I hope that the article was useful. Share your experience and thoughts in the comments below. 

Event Record (computer science) Hook Form (document) API code style Data (computing) Software Requests

Opinions expressed by DZone contributors are their own.

Related

  • Generic and Dynamic API: MuleSoft
  • Microservices With .NET Core: Building Scalable and Resilient Applications
  • Enhanced API Security: Fine-Grained Access Control Using OPA and Kong Gateway
  • Exploring API Headers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!