Affected Platforms: FortiOS
Impacted Users: Government & large organizations
Impact: Data loss and OS and file corruption
Severity Level: High
Fortinet published a CVSS Medium PSIRT Advisory (FG-IR-22-369 / CVE-2022-41328) on March 7th, 2023. The following write-up details our initial investigation into the incident that led to the discovery of this vulnerability and additional IoCs identified during our ongoing analysis.
Fortinet’s investigation was prompted by a sudden system halt and subsequent boot failure - a design to protect against compromise - of multiple FortiGate devices of a customer.
The devices halted with the following error message:
“System enters error-mode due to FIPS error: Firmware Integrity self-test failed”
FIPS-enabled devices verify the integrity of system components. If an integrity breach is detected, the device will shut down and refuse to boot to protect the integrity of the network.
We examined a subset of those FortiGate devices, as well as the FortiManager device which was used to manage them. The details of that investigation are detailed below.
The Fortinet investigation team discovered that within the device’s firmware image, /sbin/init, had been modified, and a new file, /bin/fgfm, had been added. The modification to /sbin/init ensures that /bin/fgfm, which may provide an attacker with persistent access and control, runs before proceeding with regular boot-up actions. Additional details on its functions are included in the Malware Analysis section.
We believe that the affected FortiGate devices were likely compromised using access via the FortiManager device for the following reasons:
The contents of the firmware image on the FortiManager device involved in the incident were compared with a clean FortiManager device. The following files within rootfs.gz differed when compared to the clean version:
In addition, three files were added to the image, and an existing FortiManager start-up script was modified to achieve persistence. FortiManager’s Django components were also modified in a way that may provide an attacker with persistent access and control. Additional details of the malicious files and their capabilities are provided in the Malware Analysis section.
The logs we examined contained evidence of script execution on FortiGates that was delivered by the FortiManager device. The table below shows logs with a “msg” field containing “upload-icon” and “run script” commands.
Logs also show scripts being run on various FortiGates via FortiManager’s upload script feature.
At the same time, a “Command failed” log was recorded. This log provides evidence of a path traversal exploit attempt. This exploit would allow arbitrary files to be uploaded to the FortiGate via a TFTP server at the path specified. In this instance, the attacker attempted to replace /bin/lspci on the FortiGate. While there is no trace of this in the logs, the malicious lspci could potentially be executed by running the CLI command: diagnose hardware lspci.
Because the contents of the executed scripts are not kept on the device, we could not examine them. However, the simultaneity of the “Command failed” log and the “run script” log suggests that the scripts contained the upload-icon exploit attempt.
We assigned CVE-2022-41328 to the path traversal vulnerability that enables this exploit and proceeded to fix it in all supported versions of FortiOS (see FG-IR-22-369).
The sections below describe malware found on compromised FortiGate and FortiManager devices.
Fgfm scrutinizes ICMP packets. Whenever an ICMP packet contains the string “;7(Zu9YTsA7qQ#vm”, it knows it’s a ping from the attacker and must extract an IP address from the packet.
Once that’s done, it establishes a connection back to that address (similar to a “reverse connect shell”), which acts as a C&C server. It can then perform various actions depending on the commands it receives from the C&C server:
The core functionality of auth appears to be a modification of FortiManager’s iptables utility. Iptables is built into FortiManager, but a user must have root access to use it. The exact iptables shell commands executed by the malware piece are shown below.
Our understanding is that this redirects traffic originating from a specific source IP headed to destination port 541 (the FortiGuard management port). Matching traffic is redirected to a different port. The source IP and redirect port are read from a network socket.
Auth also queries the device’s network interfaces, looking for one with an IP address that does not start with “127.” This occurs before any of the previously mentioned actions.
Klogd shares similarities with fgfm described above. It contains code that resembles remote shell execution and also has file read and write capabilities.
Other similarities to fgfm:
Its network capabilities have not been analyzed in depth. However, they seem to differ from fgfm, which uses an ICMP tunnel. Here are some of the network socket capabilities we’ve seen:
Support is a BASH script that executes /bin/klogd and /bin/auth. It also removes /bin/klogd, /nohup.out, and /bin/support.
Smit was modified to disable firmware verification at boot-up.
Localnet is a modified FortiManager start-up script with two lines added. The first line modifies /bin/smit to re-enable firmware verification at boot up. This has no impact on the running device and is likely performed to mask the fact that smit was modified. The second line executes /bin/support.
These Django components of FortiManager were also modified. Urls.py on the system contained code that exposes an additional web endpoint, “show_device_info/”. Accessing this endpoint results in the execution of malicious code added in views.py.
In views.py, the function get_device_info executes when the show_device_info/ endpoint is accessed. The get_device_info modification may enable the attack to control the device remotely. It receives commands and data via the cookies FGMGTOKEN and DEVICEID. Input and output data are encoded via RC4, and the following key actions are implemented:
The complexity of the exploit suggests an advanced actor:
The attack is highly targeted, with some hints of preferred governmental or government-related targets.
Fortinet continues to track this threat actor activity. To mitigate this issue, we recommend that all customers immediately take the actions recommended in PSIRT advisory FG-IR-22-369. Should you identify that your system is showing indicators of compromise in the logs, please reach out to Fortinet for support.