What is a keylogger?
A blog post published earlier this year posed the question "Is Grammarly a keylogger?" I have personally had people reference that post and ask me to add detection of Grammarly to Malwarebytes. The answer has always been, "no." Whether or not you like what Grammarly does, Grammarly is not a keylogger, according to the way that term is used by the security industry.
This begs the question: exactly what is a keylogger, then?
A keylogger is anything that logs keystrokes, right?
Well, no. This is way too broad a definition, since there are countless programs installed on every computer on earth designed to capture and save your keystrokes. Any word processor, for example. For that matter, any productivity software, whether that be a word processor, a notepad, a spreadsheet, a slideshow app, etc. Even something as low level as a Terminal window will record everything you type in the command history.
Using a computer made in the last several decades is all about typing things in a keyboard, and some program doing something with all those button presses. There's a tongue-in-cheek saying about a common piece of advice for avoiding phishing attacks: you can't tell the user to stop clicking things on the thing-clicking machine. Similarly, if you're going to blow the whistle at anything that captures your keystrokes, you're fighting a losing battle.
Is it something that sends your keystrokes somewhere?
We're getting closer, but still no. Think about the things you use every day. A web browser, for example. Every time you type a search in a browser, what you type is sent off to the search engine of your choosing (most likely Google). Plus, there are tons of websites that will save things you type on the server. Consider Google docs, for example. Everything you type in such a document in your browser gets sent off to Google.
The web browser isn't the only guilty party, of course. Consider Apple's Notes app. Depending on your settings, everything you type in the Notes app will be synced to iCloud. The same is true of Microsoft's OneNote app. For that matter - again, depending on your settings - doing a Spotlight search on your Mac can send everything you type in the search bar to Apple.
This is clearly where Grammarly lies. It collects keystrokes and sends them off your device for the purpose of having their backend system check the grammar of what you typed. Would it be better if it could do all that on the device? Certainly, though I know nothing of the technical reasons why that decision was made. Would I personally use Grammarly? Not a chance. However, there are many people who need a grammar checker and like the features Grammarly offers.
Clearly, these things are all legitimate apps, offering legitimate functionality. This definition is still too broad to be useful.
Then what IS a keylogger?
A more useful definition would be:
A keylogger is a program that collects keystrokes and sends them to a third-party, solely for the benefit of that third-party.
The key differentiator between a keylogger and something more legitimate is that it's not collecting your keystrokes for your benefit. Instead, someone else intends to use what you typed for some purpose of their own, nefarious or otherwise. However, within this definition, there are a few different types of keyloggers.
"Potentially unwanted" keyloggers
A keylogger may be identified as a "PUP" (which stands for "Potentially Unwanted Program") if it's software that is sold legally and openly. Such programs are often marketed as tools for monitoring your children or employees, and as such have a theoretical legitimate use. (I have some strongly negative opinions about the use of keylogging software for such purposes, but to each their own.)
However, such keyloggers are also very commonly misused. In reality, legitimate usage of such keyloggers is probably dwarfed by illegitimate usage. People with access to someone else's device can install them without the owner's knowledge for unsavory - even malicious - reasons. This is quite common with intimate partner abuse, stalking, workplace harassment, etc.
For this reason, most security software will detect these so-called "legitimate" keyloggers as PUPs. Malwarebytes, as a member of the Coalition Against Stalkerware, is certainly no exception.
Adware keyloggers
These keyloggers are things that collect keystrokes within certain contexts for the purposes of targeting you with ads, building a profile to better understand you as a target for ads, or as a means of better understanding the entire customer base. An example of the type of data that such a program might collect would be every search you enter in your browser and every site you visit (whether that's by typing the address in the address bar or clicking a link). Such programs often go well beyond just logging keystrokes, and will collect things such as your browser history, browser of choice, software installed on your computer, your location, etc.
These programs will generally trick the user into installing them, using a variety of lures. The old fake Adobe Flash Player installer trick is one of the most common, even now, when Flash is long dead. Generally speaking, though, these are spread in the form of trojans: ie, programs the user is tricked into downloading and running.
Such programs are either malware or just shy of malware, depending on your definition. Either way, they serve no legitimate purpose for anyone other than shady advertisers and deserve to be deleted with extreme prejudice. The only good news is that it is not the intent of these programs to harm you (though poor data handling practices by shady adware companies definitely could cause harm regardless of intent).
Malicious keyloggers
The most concerning category of keyloggers. These are the ones without any supposed "legitimate" purpose, and are intended for nothing but to steal your information. Such keyloggers are often used to collect sensitive information, such as account credentials, credit card numbers, social security numbers, and more.
Malicious keyloggers get onto your machine through a variety of means. They could be trojans, often using a lure more convincing than a fake Flash installer. They could infect your machine through a browser vulnerability that allows arbitrary code to execute. (This is less common on Macs than on Windows, but is nonetheless an increasing problem for Mac users.)
Such malware has also been known to have been installed manually, by attackers who have gotten access to the machine somehow, via physical or remote access. In a well-known case, the creator of the FruitFly malware is known to have used passwords obtained from data breaches to gain access to victims' Macs. He used a process called "credential stuffing," in which a password obtained from one online account is used to attempt to log in to something else. Since so many people reuse passwords, this is unfortunately a fairly reliable strategy.
In the case of malicious keyloggers, the software is rarely limited to just capturing keystrokes. Most malicious spyware has keylogging capabilities as only a part of the complete package, also including - among other things - file collection, capture of the screen contents, capture of video and audio via the webcam and microphone, and even execution of arbitrary commands. Thus, most such malware is not referred to as a "keylogger," but rather is called "spyware."
How do I protect myself from keyloggers?
Obviously, one way to do so is to use some kind of antivirus software, such as Malwarebytes. If you think you might be being targeted by someone using a PUP keylogger, make sure that the software you use detects such software. Membership in the Coalition Against Stalkerware would be a good indication of that.
You can avoid some of the common means that attackers may use to install a keylogger on your device by making sure you use a strong login password on your computer. Make sure it's one that nobody could guess, and don't leave your computer logged in and unattended. If you need to share your computer with someone, don't let them use your account on the computer. Instead, create a separate account for that person and do not give them admin privileges. (On a Mac, this can be done in System Preferences -> Users & Groups.)
When it comes to the more malicious stuff, be careful about what you download. If a website tells you that you need to install something to see its content, or tells you that you're infected and that you need to install something to fix it, run away screaming. (If you're in a public place, you may want to consider just closing the browser window, though; otherwise you may get strange looks.)
It's also critically important to keep your system up-to-date. Doing so ensures that your system is protected against known vulnerabilities that could be used to infect your device. On a Mac, go to System Preferences -> Software Update and check the box reading Automatically keep my Mac up to date.
Doing these things is never a guarantee, but they will go a long way towards reducing the chances of ever being affected by a keylogger.