18

I am running an installation of Ubuntu 14.04 that I setup more than 6 months. About a week ago, I started getting an error message:

Could not grab keyboard. A malicious client may be eavesdropping on your session.

I have only ever seen it when returning to my computer after being away for quite a while (usually overnight). Several times it has prevented the screen from locking after the set timeout (I have begun actively locking it before leaving).

I am using a usb keyboard (Kinesis Advantage) directly connected to a usb port on the motherboard. I am using a wireless ELECOM mouse.

I am going to try disconnecting the mouse dongle before leaving. How else could I identify if there is a malicious client tracking my keystrokes or if this is a connectivity issue?

1
  • 1
    DO NOT ISSUE SUDO COMMANDS IF YOU THINK YOUR KEYSTROKES ARE BEING LOGGED!!! instead, boot a clean live medium, and go from there.
    – j0h
    Apr 7, 2016 at 13:26

3 Answers 3

18
+50

Here's how to solve your mystery. The main goal in this answer is to teach users "how to fish" by using standard Ubuntu utilities to dig into the details of any executable or running process on their system. I will just use this specific case to go over general debugging/investigation techniques that are widely applicable to many similar cases as well.

Step #1 (for curiosity mostly): identify which program is giving you this error:

# -- You may need to search under more directories, YMMV
#    List files (incl. binaries) which contain the warning string

$ sudo grep -ral 'malicious client may be eavesdropping' /usr /bin /lib
/usr/lib/openssh/gnome-ssh-askpass

So, based on this response from grep, the only program which contains this warning string in its binary is gnome-ssh-askpass.

You could search if there's a bug filed on this particular program, and even download its source apt-get source ssh-askpass-gnome (note that the package name is different than the program name) for further inspection.

However, I suspect the root cause is not a problem in gnome-ssh-askpass. Since gnome-ssh-askpass is asking for your passphrase, its developers simply chose to err on the side of caution when failing to grab the keyboard. They imagined the worst-case scenario which is a malicious program, and made their message sound uber-paranoid. Why? accidentally typing your passphrase or password into the wrong place, like some random web-site dialog-box by accident is probably not a good idea. In that sense the gnome-ssh-askpass developers have made the right call in printing out this warning.

Your desktop is running a large number of programs/processes. When you type something on your keyboard, only one of these processes gets the input. That program is the one which has the current "focus" (usually where your mouse pointer is positioned or last clicked).

Recently, web sites have started engaging in a practice of enforcing paywalls, registrations, and modal-UIs. They display a popup, fading everything else outside the popup dialog, and aggressively "grabbing focus" so your next keyboard input goes to them. This is most likely the root cause for gnome-ssh-askpass failing to "grab the keyboard". If your browser is open on such a site, closing the browser or navigating away from the aggressive "attention grabbing" web site may help. If this is the cause, you may be interested in a desktop setting preventing individual processes from grabbing the complete (full desktop) focus. In KDE for example, this setting can be found under (System-Settings -> Window-behavior -> Focus -> Focus stealing prevention). If you feel really paranoid, I would recommend setting it to High or Extreme. Note: this may also prevent gnome-ssh-askpass itself from grabbing the keyboard, or more accurately: grabbing the X focus.

Step #2: Identify suspicious processes:

Knowing that in Unix, devices appear like files under /dev, the next question is what device represents "the keyboard" in the file-system hierarchy. We can use the lsof (list open files) utility for this.

# look for processes holding devices open, filter out some common ones:
$ sudo lsof | grep /dev | grep -vE '/(null|urandom|zero)'

Notice that most of the processes holding devices open in a typical desktop env are holding /dev/pts/<N> (a pseudo tty) open. These are the "devices" of interest.

Some background on what's going on here:

In a typical Linux graphical desktop, processes don't talk to the keyboard directly. Instead the X program (Xorg) controls all the keyboard events via a device /dev/input/event<N>. X uses an event handler (evdev) which among other things, handles keyboard events. You can also verify this by looking at the X log: /var/log/Xorg.0.log where keyboard is mentioned.

The keyboard events are forwarded from the X event handler to the process that has the mouse pointer focus at any time via the process standard-input which is open on /dev/pts/<N>. Strictly speaking: a process doesn't actually "grab the keyboard", the keyboard is held by X, the process only has (or grabs) "focus" or the attention of X so X can forward keyboard events to it via an open stdin file descriptor on /dev/pts/<N>.

Diagram of keyboard events multiplexed via X evdev

Step #3: which process has the Xorg focus at any particular time?

How to figure which process has the focus at any particular time? Here's a askubuntu question answering this:

find out the application under the mouse

The summary of the answer is to run a script like the following in a terminal while navigating around with the mouse:

#!/bin/bash
# Print the process tree of the window currently in focus.
# prereqs:
#   sudo apt-get install xdotool psmisc

while true; do
   pstree -spaul $(xdotool getwindowpid "$(xdotool getwindowfocus)")
   sleep 2
done

Step #4: dig deeper into process activity

Once you have a suspect process identified, the last step is to investigate this individual process. For that you may turn to the Linux /proc filesystem (man 5 proc).

Almost anything you may want to know about a process is available under /proc. In fact, programs like lsof (list open files), debuggers which examine process state, and process-listing utilities like ps or top, all rely on /proc, which is populated by the kernel, for data.

Using proc you can find where the process executable program is on disk (e.g. any program outside the standard system directories, especially if it is trying to hide under a "don't pay attention to me" kind of name, may be suspect) and using a debugger or system call tracer you can examine what exactly are they doing on the system call level (even if you don't have their source code).

Steps #2 and #3 should give you all the process-IDs (PIDs) that can potentially be reading your keyboard. For each of these PIDS (let's denote each one as $pid) you may:

Map $pid to its full command line:

cat /proc/$pid/cmdline

Map $pid to its on disk executable:

ls -l /proc/$pid/exe

Map $pid to its current working directory:

ls -l /proc/$pid/cwd

Map $pid to its original environment

cat /proc/$pid/environ | tr '\000' '\012'

Trace $pid (and its children-procs) system-call activity in real-time:

strace -f -p $pid

(There's more: see man 5 proc)

If you see an unfamiliar process that reacts to every key-press by storing it into a file (via write) or sending it over the network to via sendto, you may have found a keyboard sniffer.

You can also check which processes have (tcp+udp) network endpoints open:

# See 'man netstat' for details on all options used below
$ sudo netstat -tunapee

Bottom line:

The most likely cause for the error is not malware, but multiple processes trying to get keyboard control at the same time. One of the two is gnome-ssh-askpass (the one printing the error). The other may be an open browser on a site with an aggressive focus-acquiring dialog box.

Even in the remote chance that you actually have some malware installed, the good news is that since you're on Linux, all processes are transparent for you to research and inspect. It would be very hard for malware to really hide from you, or to prevent you from easily locating it using the above techniques, killing its processes, and removing all its files.

5
  • During step #2, I do not see many processes holding /dev/pts/7 (only 3 unique pid values). Scrolling through the results, it seems the most helf device is /dev/pts/15 though some are holding 1, 3, 12, 16, 17, 21, 22, 23, 24, 25, 25, 26, 27, 28, 29, 30, 31, 32, 34. Is the keyboard always 7? How would I identify which of these is my keyboard? Apr 4, 2016 at 19:30
  • It can be any of the above. The physical keyboard device is actually open by Xorg (/usr/bin/X) as /dev/input/eventN where you can find your N by looking at the string evdev in /var/log/Xorg.0.log. Xorg then "forwards" each keyboard click to the individual process that has the mouse pointer "focus" at that particular instant. When I run ssh-askpass I see it has /dev/pts/3 open but in any env it can be any pseudo tty device. So any one of your /dev/pts/N may be relevant here.
    – arielf
    Apr 5, 2016 at 1:34
  • @stvn66 I added a little script telling you which process has "the focus" repeatedly (reference to a related question on askubuntu). HTH.
    – arielf
    Apr 5, 2016 at 2:03
  • after running the script to identify which processes are holding the mouse, how would I identify a suspect one? It seems to be whatever application I select, e.g., starts as the terminal I ran the script in, switches to {firefox} when I click on firefox, switches again to {thunderbird} when I select thunderbird. Nothing stands out as unexpected. Perhaps this goes to your Bottom line: of the issue not coming from something grabbing the keyboard. I wish was certain this notice is meaningless or could eliminate it. Apr 26, 2016 at 13:35
  • @stvn66 I hear you :) you can't go back in time and figure out the process that had the focus originally. That process may have exited since then. To be really sure, you need to be able to reproduce. My best guess is it was your browser (firefox) while visiting a web-site with a focus-grabbing pop-up. Unless you regularly download and install software from dubious (non-canonical) sources, I highly doubt you have accidentally installed a keyboard sniffer on Ubuntu. It is good to be a bit paranoid, but there's no need to over-sweat it.
    – arielf
    Apr 27, 2016 at 17:23
1

My issue was due two concurrent gnome-ssh-askpass windows. I had two rsync jobs to the same server through SSH, and both tried to ask for the password of the SSH certificate. Grouping (and chaining) them together solved for me!

1

As arielf mentioned in their excellent answer, there's probably some focus race happening between harmless processes. This is the case for me, and I typically resolve this issue by pressing Alt+F2 and running r to restart the window manager.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .