Persistence generally refers to adversaries' techniques for maintaining access to a compromised system after the initial exploitation. To understand how different incidents are identified at various points of the Linux endpoint, we will first perform the attack and then examine where and how the attack footprints are reflected.
Persistence: Taking Foothold
Let's start by investigating the footprints of persistence, as this would be one of the first steps an intruder would take to maintain a foothold on the host after post-exploitation.
Some of the attack actions that can result in persistence on a Linux machine are explained below:
Activity#1: Account Creation
In an assumed compromised scenario, let's pretend to be Alice, who has got hold of the system. Start by creating a backdoor account using the following command:
Command:
The following commands will create an account "attacker" and added into the sudo group.
sudo useradd attacker -G sudo
sudo passwd attacker
echo "attacker ALL=(ALL:ALL) ALL" | sudo tee -a /etc/sudoers
The above commands will create an account "attacker" and added into the sudo group.
Creating Backdoor Account
ubuntu@tryhackme:/home$ sudo useradd attacker -G sudo
ubuntu@tryhackme:/home$ sudo passwd attacker
New password:
Retype new password:
passwd: password updated successfully
ubuntu@tryhackme:/home$ echo "attacker ALL=(ALL:ALL) ALL" | sudo tee -a /etc/sudoers
attacker ALL=(ALL:ALL) ALL
Identifying the Footprints
Let's pretend to be Bob, a blue teamer trying to examine various incident surfaces to identify the footprints of the backdoor account that was created.
Examining Logs
One of the key places we could begin looking at would be the logs. All common logs can be found at the /var/log/ location, as shown below:
Listing /var/log directory
ubuntu@tryhackme:/home$ cd /var/log/
ubuntu@tryhackme:/var/log$ ls -al
total 3108
drwxrwxr-x 16 root syslog 4096 Sep 5 00:00 .
drwxr-xr-x 14 root root 4096 Feb 27 2022 ..
-rw-r--r-- 1 root root 35148 Aug 20 07:34 Xorg.0.log
-rw-r--r-- 1 root root 116188 Feb 16 2024 Xorg.0.log.old
-rw-r--r-- 1 root root 0 Sep 1 00:00 alternatives.log
-rw-r--r-- 1 root root 8021 Aug 22 06:57 alternatives.log.1
-rw-r--r-- 1 root root 3001 Feb 16 2024 alternatives.log.2.gz
drwxr--r-x 3 root root 4096 Feb 27 2022 amazon
-rw-r----- 1 root adm 0 Aug 20 07:34 apport.log
-rw-r----- 1 root adm 398 Feb 16 2024 apport.log.1
drwxr-xr-x 2 root root 4096 Sep 5 06:52 apt
-rw-r----- 1 syslog adm 46892 Sep 5 21:30 auth.log
-rw-r----- 1 syslog adm 72850 Aug 31 23:30 auth.log.1
-rw-r----- 1 syslog adm 3325 Aug 24 23:30 auth.log.2.gz
-rw-r----- 1 syslog adm 9404 Aug 20 07:34 auth.log.3.gz
-rw-rw---- 1 root utmp 0 Sep 1 00:00 btmp
-rw-rw---- 1 root utmp 3840 Aug 27 14:04 btmp.1
-rw-r----- 1 root adm 44217 Aug 20 07:34 cloud-init-output.log
-rw-r----- 1 syslog adm 1576538 Aug 20 07:34 cloud-init.log
drwxr-xr-x 2 root root 4096 Sep 5 00:00 cups
drwxr-xr-x 2 root root 4096 Oct 7 2020 dist-upgrade
------------------------------------------
Examining auth.log
Let's use the following command to search for all user account creation activities in the auth.log, as shown below:
Command: cat auth.log | grep useradd
Examining auth.log
ubuntu@tryhackme:/var/log$ sudo su
root@tryhackme:/var/log# cat auth.log | grep useradd
Sep 5 21:18:19 tryhackme sudo: ubuntu : TTY=pts/0 ; PWD=/home ; USER=root ; COMMAND=/usr/sbin/useradd attacker -G sudo
Sep 5 21:18:19 tryhackme useradd[184928]: new group: name=attacker, GID=1001
Sep 5 21:18:19 tryhackme useradd[184928]: new user: name=attacker, UID=1001, GID=1001, home=/home/attacker, shell=/bin/sh, from=/dev/pts/0
Sep 5 21:18:45 tryhackme sudo: ubuntu : TTY=pts/0 ; PWD=/home ; USER=root ; COMMAND=/usr/sbin/useradd attacker -G sudo
If we look at the output, we can see various log entries associated with the user account creation activity.
Examining /etc/passwd File
Another configuration file called passwd also contains information about the users created either by default or by users, as shown below:
Command: cat /etc/passwd
Examining /etc/passwd
root@tryhackme:/var/log# cat /etc/passwd
---
----------
kernoops:x:113:65534:Kernel Oops Tracking Daemon,,,:/:/usr/sbin/nologin
lightdm:x:114:121:Light Display Manager:/var/lib/lightdm:/bin/false
whoopsie:x:115:123::/nonexistent:/bin/false
dnsmasq:x:116:65534:dnsmasq,,,:/var/lib/misc:/usr/sbin/nologin
avahi-autoipd:x:117:124:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/usr/sbin/nologin
usbmux:x:118:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
rtkit:x:119:125:RealtimeKit,,,:/proc:/usr/sbin/nologin
----------------------------
avahi:x:120:126:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/usr/sbin/nologin
fwupd-refresh:x:130:136:fwupd-refresh user,,,:/run/systemd:/usr/sbin/nologin
attacker:x:1001:1001::/home/attacker:/bin/sh
In the output, we can see all the accounts, including the one we just created. Some of the information this file contains are:
- Username.
- The password placeholder is represented by x or *, indicating that the password is stored in the/etc/shadow file.
- User ID assigned to the user
- Group ID assigned to the user.
- User's home directory.
- Path to user's default shell.
Activity # 2 Cron Job
Another persistence method is cron jobs, which attackers can use to maintain persistent access to a compromised system. Cron is a time-based job scheduler in Unix-like systems that allows tasks (scripts, commands, or programs) to be executed automatically at specified intervals.
To create a malicious cron job, we can modify the crontab file or use the crontab command to edit scheduled jobs for the current user or system using the following command:
Command: crontab -e
This will open the crontab file, and we can add an entry.
Examples of Crontab Entry:
@reboot /path/to/malicious/script.sh: This command will execute the script.sh at every reboot.
* * * * * root /path/to/malicious/script.sh: This command will execute script.sh every minute with root privileges.
Adding a cronjob
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
@reboot /path/to/malicious/script.sh
Examining Malicious Cronjobs
We can explore /var/spool/cron/crontabs/[username] to explore the cronjobs associated with each user, as shown below:
Activity # 3 Services
Another way to achieve persistence on a compromised system is installing a service on the Linux server that will run in the background and start on every reboot. Let's learn how to investigate this activity by first creating a service and installing it on the disk, as shown below:
Step1: Create a Configuration File
We will create a configuration file, as shown below:
Command: sudo nano /etc/systemd/system/suspicious.service
Add the following content to the configuration file.
[Unit]
Description=Suspicious_Service
After=network.target
[Service]
ExecStart=/home/activities/processes/suspicious
Restart=on-failure
User=nobody
Group=nogroup
[Install]
WantedBy=multi-user.target
This configuration file will create a service and will execute the mentioned process. Keywords used in the configuration file are explained below:
ExecStart: Specifies the command to run the collector program. Adjust the path as necessary.
Restart=on-failure: Ensures the service restarts if it fails.
User and Group: Runs the service under the specified user and group. Adjust as needed for your security requirements.
Step2: Enable and Start Service
Now that we have created a service let's enable and start the service using the following commands in order:
- Reload the systemd manager configuration to recognize the new service:
- Command:
sudo systemctl daemon-reload
- Enable the service to run at startup:
- Command:
sudo systemctl enable suspicious.service
- Start the service right away.
- Command:
sudo systemctl start suspicious.service
The executed commands are shown in the image below:
Status of the Service
Once everything is done, we can check the status of the service using the following command:
Command: sudo systemctl status suspicious.service
The output shows that the service is loaded and running on the system.
Examining the Running Service
Now that we know how an adversary could install and run the service in the background, let's see how we can find this service's footprints on the system.
1) Reviewing the Directory
All services installed and enabled on the Linux host can be found in the /etc/systemd/system directory. We can check the content of this directory to see if we can find any suspicious-looking services installed, as shown below:
2) Evidence in the Logs
We can start to investigate this incident by looking at the /var/log/syslog file using the filter as shown below:
Command: cat /var/log/syslog | grep suspicious
3) Examining Journalctl
We can also look at the journalctl using the following command, as shown below:
Command: sudo journalctl -u suspicious
It is important to note that these investigations can also reveal the errors the adversary may have failed while trying to install the suspicious service.