From 0fdc865db92709e20df108d43217622079f4c20d Mon Sep 17 00:00:00 2001 From: libcrack Date: Sun, 11 Jun 2023 08:57:28 +0200 Subject: [PATCH 1/7] Added -logfile filepath.log argument --- xkbcat.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/xkbcat.c b/xkbcat.c index 5e28cc2..7d90383 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -3,26 +3,42 @@ #include #include +#include +#include #include #include #include #include +#include +#include +const unsigned int BUFFER_SIZE = 8; const char * DEFAULT_DISPLAY = ":0"; +const char * DEFAULT_LOGFILE = "xkbcat-hostname-timestamp.log"; const bool DEFAULT_PRINT_UP = false; int printUsage() { printf("\ USAGE: xkbcat [-display ] [-up]\n\ display target X display (default %s)\n\ - up also print key-ups (default %s)\n", - DEFAULT_DISPLAY, (DEFAULT_PRINT_UP ? "yes" : "no") ); + up also print key-ups (default %s)\n\ + logfile logfile path (default %s)\n", + DEFAULT_DISPLAY, (DEFAULT_PRINT_UP ? "yes" : "no"), + DEFAULT_LOGFILE ); exit(0); } int main(int argc, char * argv[]) { const char * xDisplayName = DEFAULT_DISPLAY; + const char * logfilePath = DEFAULT_LOGFILE; + //const char logfilePath[PATH_MAX] = DEFAULT_LOGFILE; + FILE * logfileStream = NULL; + char buffer[BUFFER_SIZE]; + int fd; + memset(buffer, 0x0, BUFFER_SIZE); + bzero(&buffer, 0x0, BUFFER_SIZE); + bool printKeyUps = DEFAULT_PRINT_UP; // Get arguments @@ -39,6 +55,16 @@ int main(int argc, char * argv[]) { } xDisplayName = argv[i]; } + else if (!strcmp(argv[i], "-logfile")) { + // Read next entry to find value + ++i; + if (i >= argc) { + fprintf(stderr, "No value given to option `-logfile`\n"); + printUsage(); + exit(6); + } + logfilePath = argv[i]; + } else { printf("Unexpected argument `%s`\n", argv[i]); printUsage(); } } @@ -107,6 +133,18 @@ int main(int argc, char * argv[]) { group = state.group; } + //if ( (fd = open(logfilePath,O_CREAT|O_EXCL|O_SYNC)) == -1){ + // fprintf(stderr, "Cannot open logfile %s\n", logfilePath); + // perror("logfile"); + //} + //fsync(fd); + //close(fd); + + if ( (logfileStream = fopen (logfilePath, "ax")) == NULL){ + fprintf(stderr, "Cannot open logfile %s\n", logfilePath); + } + // fclose(logfileStream); + while ("forever") { XEvent event; XGenericEventCookie *cookie = (XGenericEventCookie*)&event.xcookie; @@ -147,10 +185,18 @@ int main(int argc, char * argv[]) { cookie->evtype == XI_RawKeyPress ? "+" : "-"); printf("%s\n", str); fflush(stdout); + // TODO: construct proper logline with timestampo + // sprintf(buffer, "%s\n", str); + // write(fd, str, sizeof(str)); + if (printKeyUps) fprintf(logfileStream, "%s", + cookie->evtype == XI_RawKeyPress ? "+" : "-"); + fprintf(logfileStream, "%s\n", str); + fflush(logfileStream); } } // Release memory associated with event data XFreeEventData(disp, cookie); + // fclose(logfileStream); } else { // No extra data to release; `event` contains everything. // Handle keysym group change events if (event.type == xkbEventCode) { From b2deae652ca13ca378421347ee166f7df561f184 Mon Sep 17 00:00:00 2001 From: libcrack Date: Sun, 11 Jun 2023 09:13:01 +0200 Subject: [PATCH 2/7] Minor improvement when printing and logging keystrokes --- xkbcat.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/xkbcat.c b/xkbcat.c index 7d90383..719112b 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -133,15 +133,9 @@ int main(int argc, char * argv[]) { group = state.group; } - //if ( (fd = open(logfilePath,O_CREAT|O_EXCL|O_SYNC)) == -1){ - // fprintf(stderr, "Cannot open logfile %s\n", logfilePath); - // perror("logfile"); - //} - //fsync(fd); - //close(fd); - if ( (logfileStream = fopen (logfilePath, "ax")) == NULL){ - fprintf(stderr, "Cannot open logfile %s\n", logfilePath); + fprintf(stderr, "Cannot open logfile '%s'\n", logfilePath); + exit(7); } // fclose(logfileStream); @@ -183,14 +177,15 @@ int main(int argc, char * argv[]) { // Output line if (printKeyUps) printf("%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); - printf("%s\n", str); + printf("%s", str); + if (strcmp(str,"Enter") == 0) printf("\n"); fflush(stdout); // TODO: construct proper logline with timestampo // sprintf(buffer, "%s\n", str); - // write(fd, str, sizeof(str)); if (printKeyUps) fprintf(logfileStream, "%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); - fprintf(logfileStream, "%s\n", str); + if (strcmp(str,"Enter") == 0) fprintf(logfileStream, "\n"); + fprintf(logfileStream, "%s", str); fflush(logfileStream); } } From 1f27d390bc4ec13710ee983f4cb247b9443fa1e4 Mon Sep 17 00:00:00 2001 From: libcrack Date: Sun, 11 Jun 2023 09:27:59 +0200 Subject: [PATCH 3/7] Minor improvements in keystrokes logging and printing --- xkbcat.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/xkbcat.c b/xkbcat.c index 719112b..926a365 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -177,15 +177,30 @@ int main(int argc, char * argv[]) { // Output line if (printKeyUps) printf("%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); - printf("%s", str); - if (strcmp(str,"Enter") == 0) printf("\n"); + + if (strcmp(str,"Enter") == 0) printf("\n"); + else if (strcmp(str,"space") == 0) printf(" "); + else if (strcmp(str,"semicolon") == 0) printf(";"); + else if (strcmp(str,"Return") == 0) printf("\n"); + else if (strcmp(str,"BackSpace") == 0) printf("\b"); + else if (strcmp(str,"Shift_L") == 0) printf(""); + else if (strcmp(str,"Alt_L") == 0) printf(""); + else printf("%s", str); fflush(stdout); + // TODO: construct proper logline with timestampo // sprintf(buffer, "%s\n", str); if (printKeyUps) fprintf(logfileStream, "%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); - if (strcmp(str,"Enter") == 0) fprintf(logfileStream, "\n"); - fprintf(logfileStream, "%s", str); + + if (strcmp(str,"Enter") == 0) fprintf(logfileStream, "\n"); + else if (strcmp(str,"space") == 0) fprintf(logfileStream, " "); + else if (strcmp(str,"semicolon") == 0) fprintf(logfileStream, ";"); + else if (strcmp(str,"Return") == 0) fprintf(logfileStream, "\n"); + else if (strcmp(str,"BackSpace") == 0) fprintf(logfileStream, "\b"); + else if (strcmp(str,"Shift_L") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"Alt_L") == 0) fprintf(logfileStream, ""); + else fprintf(logfileStream, "%s", str); fflush(logfileStream); } } From 51f9a79bf215745fa0b05cecd6e576000a6c8fbc Mon Sep 17 00:00:00 2001 From: libcrack Date: Sun, 11 Jun 2023 09:40:11 +0200 Subject: [PATCH 4/7] Minor improvements in keystrokes logging and printing --- xkbcat.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/xkbcat.c b/xkbcat.c index 926a365..db23c81 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -133,7 +133,7 @@ int main(int argc, char * argv[]) { group = state.group; } - if ( (logfileStream = fopen (logfilePath, "ax")) == NULL){ + if ( (logfileStream = fopen (logfilePath, "a")) == NULL){ fprintf(stderr, "Cannot open logfile '%s'\n", logfilePath); exit(7); } @@ -184,7 +184,19 @@ int main(int argc, char * argv[]) { else if (strcmp(str,"Return") == 0) printf("\n"); else if (strcmp(str,"BackSpace") == 0) printf("\b"); else if (strcmp(str,"Shift_L") == 0) printf(""); + else if (strcmp(str,"Shift_R") == 0) printf(""); else if (strcmp(str,"Alt_L") == 0) printf(""); + else if (strcmp(str,"Alt_R") == 0) printf(""); + else if (strcmp(str,"Control_L") == 0) printf(""); + else if (strcmp(str,"Control_R") == 0) printf(""); + else if (strcmp(str,"apostrophe") == 0) printf("\""); + else if (strcmp(str,"slash") == 0) printf("/"); + else if (strcmp(str,"equal") == 0) printf("="); + else if (strcmp(str,"minus") == 0) printf("-"); + else if (strcmp(str,";") == 0) printf(";"); + else if (strcmp(str,"period") == 0) printf("."); + else if (strcmp(str,"periodcentered") == 0) printf("."); + else if (strcmp(str,"comma") == 0) printf(","); else printf("%s", str); fflush(stdout); @@ -199,7 +211,19 @@ int main(int argc, char * argv[]) { else if (strcmp(str,"Return") == 0) fprintf(logfileStream, "\n"); else if (strcmp(str,"BackSpace") == 0) fprintf(logfileStream, "\b"); else if (strcmp(str,"Shift_L") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"Shift_R") == 0) fprintf(logfileStream, ""); else if (strcmp(str,"Alt_L") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"Alt_R") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"Control_L") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"Control_R") == 0) fprintf(logfileStream, ""); + else if (strcmp(str,"apostrophe") == 0)fprintf(logfileStream, "\""); + else if (strcmp(str,"slash") == 0) fprintf(logfileStream, "/"); + else if (strcmp(str,"equal") == 0) fprintf(logfileStream, "="); + else if (strcmp(str,"minus") == 0) fprintf(logfileStream, "-"); + else if (strcmp(str,";") == 0) fprintf(logfileStream, ";"); + else if (strcmp(str,"period") == 0) fprintf(logfileStream, "."); + else if (strcmp(str,"periodcentered") == 0) fprintf(logfileStream, "."); + else if (strcmp(str,"comma") == 0) fprintf(logfileStream, ","); else fprintf(logfileStream, "%s", str); fflush(logfileStream); } From 20612e4f24262662447193dbaaab6fa1b4780035 Mon Sep 17 00:00:00 2001 From: libcrack Date: Mon, 12 Jun 2023 09:05:42 +0200 Subject: [PATCH 5/7] Implemented custom logfile name using hostname and timestamp --- xkbcat.c | 97 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 72 insertions(+), 25 deletions(-) diff --git a/xkbcat.c b/xkbcat.c index db23c81..b9be661 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -5,6 +5,9 @@ #include #include +#include +#include +#include #include #include #include @@ -12,10 +15,24 @@ #include #include -const unsigned int BUFFER_SIZE = 8; +#define HOST_NAME_MAX 64 + const char * DEFAULT_DISPLAY = ":0"; -const char * DEFAULT_LOGFILE = "xkbcat-hostname-timestamp.log"; +const char * DEFAULT_LOGFILE = "xkbcat.log"; +const char * DEFAULT_HOSTNAME = "localhost"; const bool DEFAULT_PRINT_UP = false; +const unsigned int BUFFER_SIZE = 256; + +static FILE * logfileStream = NULL; + + +static void free_all(void) +{ + if (fclose(logfileStream) == EOF){ + perror("fclose()"); + } +} + int printUsage() { printf("\ @@ -25,22 +42,27 @@ USAGE: xkbcat [-display ] [-up]\n\ logfile logfile path (default %s)\n", DEFAULT_DISPLAY, (DEFAULT_PRINT_UP ? "yes" : "no"), DEFAULT_LOGFILE ); - exit(0); + exit(EXIT_SUCCESS); } int main(int argc, char * argv[]) { const char * xDisplayName = DEFAULT_DISPLAY; - const char * logfilePath = DEFAULT_LOGFILE; - //const char logfilePath[PATH_MAX] = DEFAULT_LOGFILE; - FILE * logfileStream = NULL; - char buffer[BUFFER_SIZE]; - int fd; - memset(buffer, 0x0, BUFFER_SIZE); - bzero(&buffer, 0x0, BUFFER_SIZE); - + const char hostname[HOST_NAME_MAX + 1]; + //FILE * logfileStream = NULL; + //char * logfilePath = DEFAULT_LOGFILE; + char logfilePath[PATH_MAX + 1]; + char buffer[BUFFER_SIZE]; + char timestamp[BUFFER_SIZE]; + //int fd; + time_t rawtime; + struct tm * timeinfo; bool printKeyUps = DEFAULT_PRINT_UP; + memset(&buffer, 0x0, BUFFER_SIZE); + strncpy(logfilePath, DEFAULT_LOGFILE, PATH_MAX); + strncpy(hostname, DEFAULT_HOSTNAME, HOST_NAME_MAX); + // Get arguments for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-help")) printUsage(); @@ -51,7 +73,7 @@ int main(int argc, char * argv[]) { if (i >= argc) { fprintf(stderr, "No value given to option `-display`\n"); printUsage(); - exit(5); + exit(EXIT_FAILURE); } xDisplayName = argv[i]; } @@ -61,18 +83,22 @@ int main(int argc, char * argv[]) { if (i >= argc) { fprintf(stderr, "No value given to option `-logfile`\n"); printUsage(); - exit(6); + exit(EXIT_FAILURE); } - logfilePath = argv[i]; + // logfilePath = argv[i]; + strncpy(logfilePath, argv[i], PATH_MAX); } else { printf("Unexpected argument `%s`\n", argv[i]); printUsage(); } } + // Close file stream at exit + atexit(free_all); + // Connect to X display Display * disp = XOpenDisplay(xDisplayName); if (NULL == disp) { fprintf(stderr, "Cannot open X display '%s'\n", xDisplayName); - exit(1); + exit(EXIT_FAILURE); } int xiOpcode; @@ -81,7 +107,7 @@ int main(int argc, char * argv[]) { if (! XQueryExtension(disp, "XInputExtension", &xiOpcode, &queryEvent, &queryError)) { fprintf(stderr, "X Input extension not available\n"); - exit(2); + exit(EXIT_FAILURE); } } { // Request XInput 2.0, to guard against changes in future versions @@ -89,10 +115,10 @@ int main(int argc, char * argv[]) { int queryResult = XIQueryVersion(disp, &major, &minor); if (queryResult == BadRequest) { fprintf(stderr, "Need XI 2.0 support (got %d.%d)\n", major, minor); - exit(3); + exit(EXIT_FAILURE); } else if (queryResult != Success) { fprintf(stderr, "XIQueryVersion failed!\n"); - exit(4); + exit(EXIT_FAILURE); } } { // Register to receive XInput events @@ -116,7 +142,7 @@ int main(int argc, char * argv[]) { if (! XkbQueryExtension(disp, &xkbOpcode, &xkbEventCode, &queryError, &majorVersion, &minorVersion)) { fprintf(stderr, "Xkb extension not available\n"); - exit(2); + exit(EXIT_FAILURE); } } // Register to receive events when the keyboard's keysym group changes. @@ -133,11 +159,34 @@ int main(int argc, char * argv[]) { group = state.group; } + // Create a filename using the hostname and current timestamp: + // xkbcat_localhost_2023-06-11_09-57-11.log + if ( strcmp(logfilePath, DEFAULT_LOGFILE) == 0 ){ + gethostname(&hostname, HOST_NAME_MAX); + + time(&rawtime); + timeinfo = localtime(&rawtime); + + if (timeinfo == NULL) { + perror("localtime"); + exit(EXIT_FAILURE); + } + + if (strftime(timestamp, sizeof(timestamp)-1, "%Y-%m-%d_%H-%M-%S", timeinfo) == 0){ + fprintf(stderr, "strftime returned 0"); + exit(EXIT_FAILURE); + } + + snprintf(logfilePath, PATH_MAX, "xkbcat_%s_%s.log", hostname, timestamp); + } + if ( (logfileStream = fopen (logfilePath, "a")) == NULL){ - fprintf(stderr, "Cannot open logfile '%s'\n", logfilePath); - exit(7); + fprintf(stderr, "Cannot open log file '%s'\n", logfilePath); + exit(EXIT_FAILURE); } - // fclose(logfileStream); + + fprintf(stdout, "Log file created: %s\n", logfilePath); + // fprintf(logfileStream, "[%s] %s\n", asctime(timeinfo), logline); while ("forever") { XEvent event; @@ -178,7 +227,7 @@ int main(int argc, char * argv[]) { if (printKeyUps) printf("%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); - if (strcmp(str,"Enter") == 0) printf("\n"); + if (strcmp(str,"Enter") == 0) printf("\n"); else if (strcmp(str,"space") == 0) printf(" "); else if (strcmp(str,"semicolon") == 0) printf(";"); else if (strcmp(str,"Return") == 0) printf("\n"); @@ -200,8 +249,6 @@ int main(int argc, char * argv[]) { else printf("%s", str); fflush(stdout); - // TODO: construct proper logline with timestampo - // sprintf(buffer, "%s\n", str); if (printKeyUps) fprintf(logfileStream, "%s", cookie->evtype == XI_RawKeyPress ? "+" : "-"); From 53669784fcb045efbe962031cb9b5429e025d07b Mon Sep 17 00:00:00 2001 From: libcrack Date: Mon, 12 Jun 2023 09:13:44 +0200 Subject: [PATCH 6/7] Minor improvements in keystrokes logging and printing --- xkbcat.c | 1 + 1 file changed, 1 insertion(+) diff --git a/xkbcat.c b/xkbcat.c index b9be661..f9950cb 100644 --- a/xkbcat.c +++ b/xkbcat.c @@ -186,6 +186,7 @@ int main(int argc, char * argv[]) { } fprintf(stdout, "Log file created: %s\n", logfilePath); + fprintf(stdout, "Capturing keystrokes on display: %s\n", xDisplayName); // fprintf(logfileStream, "[%s] %s\n", asctime(timeinfo), logline); while ("forever") { From 1221230b33ffb95890447a4e27120369669a1e36 Mon Sep 17 00:00:00 2001 From: libcrack Date: Mon, 12 Jun 2023 14:19:44 +0200 Subject: [PATCH 7/7] Improved makefile: generic VARS & additional targets (clean, debug, help) --- makefile | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index 6ec8604..e9db94c 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,28 @@ -xkbcat: xkbcat.c - $(CC) -O3 --std=c99 -pedantic -Wall xkbcat.c -o xkbcat -lX11 -lXi +CC := gcc +LDLIBS := -lXi -lX11 +LDFLAGS := +CFLAGS := -O3 --std=c99 -Wall -pedantic -Wno-parentheses -fomit-frame-pointer +DEBUG := $(CFLAGS) -O0 -ggdb -fvar-tracking-assignments -fvar-tracking -fno-builtin +BIN := xkbcat + + +all: + make $(BIN) + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< + +%: %.o + $(CC) $(LDFLAGS) $(LDLIBS) -o $@ + clean: - rm --force xkbcat + rm -f *~ *.o $(BIN) + +debug: + CFLAGS="$(DEBUG)" make + +help: + echo "make " + + +.SILENT: all clean test help