Detecting Linux Stealth Rootkits with Directory Link Errors

Detecting Linux Stealth Rootkits with Directory Link Errors

Detecting stealth rootkits on Linux can be done from the command line. The secret is to ask the same question multiple ways to make sure all answers agree. If they don't all agree, something is likely hiding from you.

Let's demonstrate this idea by finding a directory from the Reptile stealth rootkit on Linux. We are going to ask the same question multiple ways to see if they all agree. The question is simple:

"How many directories are here?"

It is surprisingly hard for a stealth rootkit to provide consistent answers to simple questions like this. You can use this method in other ways to find things hiding on Linux.

h/t Hal Pomeranz for the link check method he talked about years ago in this post.

Article content
You can find Linux stealth rootkits with command line tools.

When you run a rootkit like Reptile, they often come with the ability to hide a directory if named a certain way. Any directory with the name "reptile" in it will be hidden with this rootkit as you see below under /lib/udev. We'll look at this directory with some simple command line tools to spot rootkit activity.

Article content
Reptile stealth rootkit on Linux is hiding a directory.

This is what the directory looks like when you issue a secret command to make it visible.

Article content
Reptile hidden directory revealed.

Let's use the tactic of asking the same question multiple ways to find something hiding. Here we'll list only directories then pipe it through a command to get a count. Only two directories are shown.

The commands are:

ls -d */         
ls -d */ | wc -l        
Article content
The ls command only shows two directories are here. Is that true?

Now we ask the same question, but this time we'll use the "stat" command and check directory link counts. The stat command says more directories are here with the link count:

stat .         

NOTE: This trick may not work on file systems such as btrfs.

Article content
The stat command however shows an extra directory.

It's one thing to get the kernel to lie. It's another entirely to get the file system to agree. Here the kernel rootkit cannot get the stat command to agree that the file system only has two directories.

Here is another view of the stat command to get the same information that does not match what ls is showing. This inconsistency error is common with stealth malware.

ls -al         
stat .        
Article content
The ls and stat commands do not agree. A stealth rootkit is hiding a directory here.
Article content
stat does not lie with this rootkit.

Doing manual investigation works, but it's tedious. You should automate it with Sandfly Security . We spot directory link errors like this, but we look for many more problems as well that reveal stealth rootkit activity on Linux.

Article content
Sandfly Security spots directory link count error.
Article content
A Linux stealth rootkit shows many alerts with agentless Sandfly hunting for it.

Command line tactics let you know something is wrong. However, you may have to isolate and take the machine into single user mode to dig deeper or do a memory dump to try to find the culprit. But, at least you know an investigation is warranted.

Hiding on Linux is difficult. Getting everything to agree with the hiding lie is expensive and prone to error. Even if you get a stealth rootkit to work on an isolated box, it likely will break in the wild.

There have been increasing reports of stealth rootkits being deployed on some high-profile attacks so we don't want to dismiss them. However, while stealth rootkits seem like a great idea, in our experience they are often more bark than bite.

Ah, such a useful trick! Definitely evergreen content. See also my “chkdirs” tool which is part of the old chkrootkit project (chkrootkit.org)—it will do a full file system walk looking for link count mismatches. Try the same idea on /proc to detect hidden processes. Be careful because false positives are easier here due to ephemeral processes exiting during your testing.

To view or add a comment, sign in

More articles by Craig Rowland

  • Linux Medusa Rootkit Detection and De-Cloaking

    I was playing around with the new Medusa rootkit on Linux and wanted to give some tips on how to deal with this style…

    3 Comments
  • SCTP Protocol Attack Risks on Linux

    The SCTP protocol on Linux provides reliable communications largely for the telecommunications sector. While it has…

    3 Comments
  • Detecting Packet Sniffing Malware on Linux

    Packet sniffing on Linux can be used for a variety of legitimate reasons, but sometimes it's used by malware for…

    3 Comments
  • Detecting Bincrypter Linux Malware Obfuscation

    A new Linux script from THC will encrypt and obfuscate any executable or script to hide from on-disk detection. It then…

    3 Comments
  • Finding /dev/tcp Reverse Shell Linux Backdoors

    One of my favorite reverse shells for Linux is this: It's simple and works on just about every Linux system without…

    4 Comments
  • The Linux Visibility Nightmare

    Agentless security has always fascinated me since one of the first security audits I participated in as a Red Team…

    1 Comment
  • Using ChatGPT for Linux Forensics

    I have been using #ChatGPT to help with Linux forensic investigations. It is an incredibly useful tool when guided with…

    6 Comments
  • SSH Duplicate Key Detection

    SSH duplicate keys are a major problem in many organizations. It is easy to have duplicate keys dropped into an SSH…

  • Detecting and Investigating OpenSSL Backdoors on Linux

    A nightmare for security personnel on Linux is to find a backdoor operating. This means an attacker is interacting with…

    5 Comments
  • Investigating Linux Process File Descriptors for Incident Response and Forensics

    Let’s talk about Linux file descriptors and how to investigate a malicious process using them. What Is a File…

    2 Comments

Others also viewed

Explore content categories