tryhackme - Linux Server Forensics

Table of Contents
Room: Linux Server Forensics
Difficulty: Medium
Task 1 Deploy the first VM
You have been hired to investigate a data breach at ACME web design. Log in to the target machine using SSH with the following credentials:
- MACHINE_IP
- Username - ‘fred’
- Password - ‘FredRules!’
Task 1.1 - Deploy the machine and log in to the VM using the provided credentials.
No Answer Needed
Task 2 Apache Log Analysis I
The most significant attack surface on the server is probably the web service; fortunately, the Apache access log keeps a history of all of the requests sent to the webserver and includes:
- source address
- response code and length
- user-agent
Every request from a modern browser should contain a user-agent string that can be used to identify it roughly. The content of the user-agent string is then used to modify the experience by displaying the mobile version of a site or disabling features not supported on older browsers.
We can also use this string to identify traffic from potentially malicious tools as scanning tools like Nmap, SQLmap, DirBuster and Nikto leak their identity’s through default user-agent strings.
Task 2.1 - Navigate to /var/log/apache2
Task 2.2 - How many different tools made requests to the server?
I see dirbuster
(just gave a look to the files content), Mozilla Firefox, and curl
, but the answer is dirbuster
and curl
only.
Task 2.3 - Name a path requested by Nmap.
It looks like I was wrong, I missed nmap
in the first question, It was dirbuster
and nmap
in the previous question, LoL 😅
Task 3 Web Server Analysis
Web scanners are run against servers pretty much all the time, so this traffic is not out of the ordinary. Have a look around the site for potential attack vectors.
Task 3.1 - What page allows users to upload files?
contact.php has php code that let you upload a file and it doesn’t even have any fillters.
Task 3.2 - What IP uploaded files to the server?
Task 3.3 - Who left an exposed security notice on the server?
Task 4 Persistence Mechanisms I
It looks like our attacker got in via Remote File Inclusion (RFI). It’s best to look around the system itself now for any evidence of persistence mechanisms that could lead to a payload. There are multiple ways to maintain persistence in most Linux distributions including but not limited to:
- cron
- Services/systemd
- bashrc
- Kernel modules
- SSH keys
Cron is one of the more common methodologies, so it’s probably best to start there.
Task 4.1 - What command and option did the attacker use to establish a backdoor?
Here we can see that the interactive shell sh -i
is being redirected to tcp socket, it acts as bind shell and could be catched on the attacker machine.
Task 5 User Accounts
It looks like the command from the previous task was set up to run under the root2 account. This account doesn’t make any sense as there should only be one root account. Wonder how it got there?
There are a couple of locations where account information is stored on Linux distributions, including:
- /etc/passwd - contains the names of most of the accounts on the system. Should have open read permissions and should not contain password hashes.
- /etc/shadow - contains names but should also contain password hashes. Should have strict permissions.
Task 5.1 - What is the password of the second root account?
/etc/passwd
:
root2:WVLY0mgH0RtUI:0:0:root:/root:/bin/bash
When I googled the hash, I found this forum containing the password: https://security.stackexchange.com/questions/151700/privilege-escalation-using-passwd-file
Task 6 Deploy the second VM
Wow, it looks like they got hacked again! Better have another look; the credentials are the same as last time:
- MACHINE_IP
- ‘fred’
- ‘FredRules!’
Task 6.1 - Deploy the second machine and log in to the VM using the provided credentials.
No Answer Needed
Task 7 Apache Log Analysis II
The log file is a lot smaller this time around, so it looks like our attacker was a little more subtle. There also don’t appear to be obvious user agents anymore. Fortunately, there are a few other ways of identifying traffic originating from scanners. The time between each request is a good metric for most tools. You can also identify individual tools from signatures left in the requests; for example, Nmap will send HTTP requests with a random non-standard method when performing certain enumeration tasks. More aggressive tools can also be identified simply from the number of requests sent during any given attack; directory brute-forcing tools are a perfect example of this and are likely to fall foul of banning systems like fail2ban.
A poorly designed site may also freely grant valuable information without the need for aggressive tools. In this case, the site uses sequential IDs for all of the products making. It easily scrapes every single product or finds the total size of the product database by simply increasing the product ID until a 404 error occurs.
Task 7.1 - Name one of the non-standard HTTP Requests.
Let’s read the log file:
Task 7.2 - At what time was the Nmap scan performed? (format: HH:MM:SS)
We know that nmap
sends non-standard http requests, let’s get all of the non-standard requests and get the IP of the attacker:
The file access.log starts with non-text data, hence grep
is treating the file as binary. We can use -a
with grep
, it makes grep
process a binary file as if it were text.
Now we know the IP address of the attacker, lets grep
the IP and note the time of the first request came from that address:
Task 8 Persistence Mechanisms II
The adversary has been a little smarter this time around. If any backdoor exists, it’s likely to be more subtle. SSH-keys are another excellent way of maintaining access, so it might be worth looking for additions to the authorized_keys file.
Task 8.1 - What username and hostname combination can be found in one of the authorized_keys files? (format: username@hostname)
Let’s run find
command to look for ‘authorized_keys’ files:
I looked in these files like an idiot without realizing, after that I checked the Hint: “You will need to use sudo to view root’s authorized_keys file”.
Here we see attackers username and hostname in their public key.
Task 9 Program Execution History
Of course, adding a public key to root’s authorized_keys requires root-level privileges so, it may be best to look for more evidence of privilege escalation. In general, Linux stores a tiny amount of programme execution history when compared to Windows but, there are still a few valuable sources, including:
- bash_history - Contains a history of commands run in bash; this file is well known, easy to edit and sometimes disabled by default.
- auth.log - Contains a history of all commands run using sudo.
- history.log (apt) - Contains a history of all tasks performed using apt - is useful for tracking programme installation and removal.
systemd services also keep logs in the journald system; these logs are kept in a binary format and have to be read by a utility like journalctl. This binary format comes with some advantages; however, each journal is capable of validating itself and is harder to modify.
Task 9.1 - What is the first command present in root’s bash_history file?
Task 10 Deploy The Final VM
This is getting a little annoying now. The credentials are still the same:
- MACHINE_IP
- ‘fred’
- ‘FredRules!’
Task 10.1 - Deploy the final machine and log in to the VM using the provided credentials.
No Answer Needed
Task 11 Persistence Mechanisms III
Malware can also maintain persistence using systemd as scripts run under systemd can run in the background and restart whenever the system is booted or whenever the script crashes. It is also relatively easy to conceal malicious scripts as they can blend in with other services. systemd services are defined in .service files which can contain:
- The command that runs whenever the service starts
- The user the service runs as
- An optional description
In this case, the malware is pretty obvious as it seems to be printing errors to the screen so, there is no way that it is dormant. Running systemctl
will list all of the services loaded into the system. And much like Windows, there’s usually a lot of them. It might be worth adding --type=service --state=active
to the command as it will reduce the list to services that are running. Once the name of a suspicious service is found, more information can then be extracted by running systemctl status <service name>
.
Task 11.1 - Figure out what’s going on and find the flag.
After ssh
ing we see some weird stuff in the terminal.
LOL, this made me smile :)
So, I was trying to find the service but I didn’t get the service name so I checked the hint and found the service name.
I stopped the service because it was coming in the way when typing.
It was my first time seeing this type of application of a bash script, it’s pretty cool.
So, this is it for this writeup and I hope you enjoyed reading this, happy hacking. 🔥