sysfatal(blog)
by e__soriano
I’ve long wanted to experiment with Go and eBPF, so I decided to develop a proof of concept keylogger designed to capture passwords within Linux terminals.
Any user familiar with a Linux shell is aware that, typically, programs that read passwords from the keyboard prevent the keystrokes entered by the user from being displayed on the screen. This is achieved by disabling the terminal’s echo. For further info on terminals, I previously wrote a blog post on the subject (in Spanish): https://sysfatal.github.io/tty.html.
The idea is to develop a program that exclusively captures passwords entered when the terminal is operating in that specific mode: Termopilas (Θερμοπύλες, Thermopylae).
Termopilas consists of two programs:
A Go userspace program that receives and filters events that include the passwords (extracted from the kernel).
The eBPF code (written in C) to capture the keystrokes within the kernel.
Those programs communicate with each other through a map and a ring buffer. The
first one is used to pass the arguments to the eBPF program
(an optional target PID, if we want to spy on a single process, and the PID
of the userspace program, to avoid creating IO loops). The ring buffer
is used to send the events from kernel space to userland.
You can read the source code here: https://gitlab.eif.urjc.es/publications/termopilas
The idea is simple: We will intercept read operations for the terminal
and check if the terminal’s echo is off.
To do so, we will hook the exit of the VFS (Virtual Filesystem
Switch) read operation.
Note that it is not the read of the corresponding device driver;
it is the higher-level, common, read entry point (i.e. the VFS
wrapper). This is done with this eBPF hooking point:
SEC("fexit/vfs_read")
It is important to intercept the system call at the exit point, not at the
entry point, if we want to get the bytes read from the terminal device
(that are stored in the buffer passed to the read syscall).
First, the eBPF program checks if the file referenced by the file descriptor is a terminal (a pseudoterminal, virtual terminal, or real terminal). To do that, it checks the device’s minor and major numbers (the ids for device drivers and instances).
Then, it checks the terminal termios
info stored in the following bits of the CFLAG field
(termios.c_lflag in the eBPF code):
#define ECHOFLAG 0000010
#define ICANONFLAG 0000002
If the file is a terminal and the echo is off,
it captures the data from the buffer passed to read
and transfers the event to the Go userspace program through
the ring buffer. The event also includes
some metadata, like the minor and major numbers of the device, the PID of
the process that is reading from the terminal, the terminal name,
the program it is running, and the canonical mode status.
An interesting detail is that standard shells turn off the terminal’s echo while they are waiting in the prompt (interactive mode). Our program has to filter those cases.
The Go program uses Cilium, a library that provides the mechanisms to build and load eBPF programs, see https://github.com/cilium/ebpf.
The main program file must include a directive like:
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64 -type event vfshook bpf/vfshandlers.c -- -I./bpf
Before building our Go program, we need to run:
go generate ./...
Go generate runs the commands defined by the directives included
in the Go source code. Those commands
create or update Go source files. In this case, the bpf2go tool
creates the Go code needed to interoperate with the eBPF program.
Our Go program spawns a separate goroutine to decouple the code receiving the eBPF events (Cilium provides a Go Reader to read the events) and the code that processes and prints the data. A buffered channel is used for connecting those two parts.
There are some interesting cases that have to be filtered by the userspace program:
For pseudoterminals, we are not interested in reads performed by the
master (ptm); we are only interested in the slaves.
Some commands read the passwords with the terminal set to non-canonical
mode (e.g. systemd-ask-passwd).
If the program reading from the terminal is a shell,
we need to check if it is a shell running a script (which is trying to
read the password, for example with the builtin command read -s) or not
(i.e. an interactive shell).
The Go program just prints the events to stderr in a log-like format. A real keylogger/stealer should encrypt, hide, and send the data to the C&C.
Some commands like gpg, when executed on a desktop environment,
use pinentry to get the password from a graphical application.
In this case, pinentry reads the password from the
application and sends it to gpg through a pipe.
To capture those passwords, the eBPF program also hooks the
exit of the VFS write operation:
SEC("fexit/vfs_write")
In this case, when the file is a pipe and the process
is executing pinentry, this hook captures the buffer and sends an event
to the userspace program.
The program has been tested with the following commands:
This is an example execution (running sudo, ssh, login, passwd, gpg, openssl, curl, and systemd-ask-passwd on a different terminal):
# ./termopilas
2026/11/30 13:45:02 Ignored PID: 145535
2026/11/30 13:45:02 Θερμοπύλες wide open...
2026/11/30 13:45:32 dev=pts1 (major=5, minor=0) pid=145618 cmd=sudo count=1 data=cA== -> p
2026/11/30 13:45:32 dev=pts1 (major=5, minor=0) pid=145618 cmd=sudo count=1 data=ZQ== -> e
2026/11/30 13:45:32 dev=pts1 (major=5, minor=0) pid=145618 cmd=sudo count=1 data=cA== -> p
2026/11/30 13:45:32 dev=pts1 (major=5, minor=0) pid=145618 cmd=sudo count=1 data=ZQ== -> e
2026/11/30 13:45:32 dev=pts1 (major=5, minor=0) pid=145618 cmd=sudo count=1 data=Cg== -> \n
2026/11/30 13:45:46 dev=pts1 (major=5, minor=0) pid=145634 cmd=ssh count=1 data=UA== -> P
2026/11/30 13:45:46 dev=pts1 (major=5, minor=0) pid=145634 cmd=ssh count=1 data=Tw== -> O
2026/11/30 13:45:46 dev=pts1 (major=5, minor=0) pid=145634 cmd=ssh count=1 data=UA== -> P
2026/11/30 13:45:46 dev=pts1 (major=5, minor=0) pid=145634 cmd=ssh count=1 data=Tw== -> O
2026/11/30 13:45:46 dev=pts1 (major=5, minor=0) pid=145634 cmd=ssh count=1 data=Cg== -> \n
2026/11/30 13:46:41 dev=pts4 (major=136, minor=4) pid=145754 cmd=login count=11 data=cGFwYXBhcGFwYQo= -> papapapapa\n
2026/11/30 13:49:04 dev=pts1 (major=136, minor=1) pid=145967 cmd=passwd count=10 data=UFBZUFlQWVBZCg== -> PPYPYPYPY\n
2026/11/30 13:50:09 dev=pts1 (major=5, minor=0) pid=146115 cmd=openssl count=9 data=c3Nsd3JvbmcK -> sslwrong\n
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Rw== -> G
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=UA== -> P
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Rw== -> G
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=UA== -> P
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Tw== -> O
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Tw== -> O
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Tw== -> O
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Tw== -> O
2026/11/30 13:50:55 dev=pts1 (major=5, minor=0) pid=146201 cmd=gpg count=1 data=Cg== -> \n
2026/11/30 13:52:32 dev=pts1 (major=5, minor=0) pid=146393 cmd=curl count=9 data=Q1VSTFBFUEUK -> CURLPEPE\n
2026/11/30 13:53:10 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=cw== -> s
2026/11/30 13:53:11 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=eQ== -> y
2026/11/30 13:53:11 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=cw== -> s
2026/11/30 13:53:11 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=dA== -> t
2026/11/30 13:53:11 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=ZQ== -> e
2026/11/30 13:53:11 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=bQ== -> m
2026/11/30 13:53:12 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=ZA== -> d
2026/11/30 13:53:14 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=Zg== -> f
2026/11/30 13:53:14 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=YQ== -> a
2026/11/30 13:53:14 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=aQ== -> i
2026/11/30 13:53:15 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=bA== -> l
2026/11/30 13:53:15 dev=pts1 (major=5, minor=0) pid=146478 cmd=systemd-ask-pas count=1 data=Cg== -> \n
...
This software is a proof of concept (PoC) developed solely for educational and authorized security research purposes. It is intended to help researchers, students, and defenders understand vulnerabilities and improve security.
By downloading, compiling, or running this software, you agree to the following:
Authorized use only. You may use this software only on systems you own or have explicit, written permission to test. Unauthorized access to computer systems is illegal in most jurisdictions.
No malicious use. This software must not be used to damage, disrupt, or gain unauthorized access to any system, network, or data, or to violate the privacy or rights of others.
No warranty. This software is provided “as is”, without warranty of any kind, express or implied, including but not limited to merchantability, fitness for a particular purpose, and non-infringement. It is a PoC and may be unstable, incomplete, or cause unintended side effects, including data loss or system instability.
No liability. The author(s) and contributors accept no responsibility or liability for any damage, loss, legal consequences, or other harm arising from the use or misuse of this software. You are solely responsible for your actions and for complying with all applicable laws and regulations.
Responsible disclosure. If you discover a vulnerability using this software, you are encouraged to report it responsibly to the affected vendor or maintainer.
If you do not agree to these terms, do not use this software.
(cc) Enrique Soriano-Salvador Algunos derechos reservados. Este trabajo se entrega bajo la licencia Creative Commons Reconocimiento - NoComercial - SinObraDerivada (by-nc-nd). Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
tags: keylogger