Intro
On March 28th, Brian Krebs published a blog post about attacks against Microsoft Exchange servers. In one of those attacks, a webshell called “Babydraco” was deployed. The webshell was used to deploy a new malware. The binary had the filename “krebsonsecurity.exe” and used a Command and Control (C2) server located at “brian[.]krebsonsecurity[.]top”. This malware turns out to be a remote access trojan (RAT) that has been written to target both Windows and Linux machines. Based on strings found in the malware, we have named it HabitsRAT. While the Windows version of the RAT has been documented being installed on compromised Microsoft Exchange servers, it is not known what type of servers the Linux version is used against. Still, in the last couple of months, numerous remote code execution (RCE) vulnerabilities have been disclosed in hardware and services running on top of Linux. About a month ago, CISA released an advisory urging users of F5 BIG-IP to apply patches to address RCE vulnerabilities.Technical Analysis
The HabitsRAT is a simple backdoor that allows the malware operator to execute arbitrary code on the infected machine. While the backdoor is simple in design, the malware has functionality making the attack more complex than what is normally seen. The malware is written in Go and targets at least both Windows and Linux machines. The structure for the Windows version of the malware, generated by redress, is shown in the code snippet below. Most of the code is shared between the Windows version and the Linux version. The operating system-specific code has been placed in the files “commandplatform_windows.go”, “keyplatform_windows.go” and “persistencehandler_windows.go”. The rest of the files are shared with the Linux version.Package main: C:/Users/user/habits/habits-client
File: commandhandler.go
RunSignedCommand Lines: 17 to 35 (18)
File: commandplatform_windows.go
RunCommand Lines: 8 to 13 (5)
File: keyhandler.go
GetOrGenerateKey Lines: 13 to 23 (10)
GenerateKey Lines: 23 to 42 (19)
GetKeyStore Lines: 42 to 50 (8)
SetKey Lines: 50 to 68 (18)
GetKey Lines: 68 to 77 (9)
File: keyplatform_windows.go
GetRootKeyStore Lines: 11 to 19 (8)
GetUserKeyStore Lines: 19 to 27 (8)
IsRoot Lines: 27 to 49 (22)
File: main.go
main Lines: 17 to 34 (17)
File: persistencehandler.go
InstallPersistence Lines: 9 to 17 (8)
CopyBinary Lines: 17 to 22 (5)
File: persistencehandler_windows.go
CheckPersistence Lines: 11 to 21 (10)
GetBinStoreRoot Lines: 21 to 29 (8)
GetBinStoreUser Lines: 29 to 37 (8)
InstallPersistRoot Lines: 37 to 98 (61)
The Linux source code structure is shown in the code snippet below. The Linux specific code has been placed in the files “commandplatform_linux.go”, “keyplatform_linux.go” and “persistencehandler_systemd_linux.go”.Package main: C:/Users/user/habits/habits-client
File: commandhandler.go
RunSignedCommand Lines: 17 to 35 (18)
File: commandplatform_linux.go
RunCommand Lines: 8 to 13 (5)
File: keyhandler.go
GetOrGenerateKey Lines: 13 to 23 (10)
GenerateKey Lines: 23 to 46 (23)
GetKeyStore Lines: 46 to 54 (8)
SetKey Lines: 54 to 72 (18)
GetKey Lines: 72 to 84 (12)
IsRootAsString Lines: 84 to 86 (2)
File: keyplatform_linux.go
GetRootKeyStore Lines: 9 to 16 (7)
GetUserKeyStore Lines: 16 to 17 (1)
File: main.go
main Lines: 17 to 34 (17)
File: persistencehandler.go
InstallPersistence Lines: 9 to 17 (8)
CopyBinary Lines: 17 to 20 (3)
File: persistencehandler_systemd_linux.go
Systemd_CheckPersistence Lines: 11 to 25 (14)
Systemd_GetBinStoreUser Lines: 25 to 33 (8)
Systemd_InstallPersistRoot Lines: 33 to 64 (31)
Installation
When the binary is run, it installs itself into a folder. The Windows version’s location is “%SystemDrive%WindowsDefenderMsMpEng.exe” while the Linux version is “$HOME/.config/polkitd/polkitd”. This will result in the malware being installed under “/root” if it’s being run with root privileges. After the malware has installed itself, it checks if the persistence method has been set up. If it hasn’t, it goes ahead and sets it up. On Linux, it uses a “systemd” unit file. The malware checks if it’s already configured by executing the command “systemctl status polkitd”, as shown in Figure 1.[Unit]
Description=Authorization Manager
After=network.target
[Service]
GuessMainPID=no
ExecStart=”/path/to/binary”
Restart=always
[Install]
WantedBy=multi-user.target
The Windows version of HabitsRAT uses scheduled tasks for persistence. First, it writes the scheduled task “xml” to a file located at “%TEMP%krebsonsecurity.xml”. The content of the file is shown in the snippet below. The task is added by executing the shell command: “sCHtAsks.exe /create /xml %TEMP%krebsonsecurity.xml /tn WindowsDefenderScan”<?xml version=”1.0″ encoding=”UTF-16″?>
<Task version=”1.2″
xmlns=”http://schemas.microsoft.com/windows/2004/02/mit/task”>
<RegistrationInfo>
<Date>2020-12-18T09:56:46.3915265</Date>
<Author>Microsoft Corporation</Author>
<URI>\\Microsoft\\MicrosoftUpdater</URI>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
<Delay>PT1M</Delay>
</BootTrigger>
</Triggers>
<Principals>
<Principal id=”Author”>
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context=”Author”>
<Exec>
<Command>
path\to\binary
</Command>
</Exec>
</Actions>
</Task>
Command and Control Communication
The RAT uses public-key cryptography to both encrypt and authenticate the commands from the C2 server. The malware generates a public-private key pair using an open-source library provided by Proton Mail. Figure 2 shows the call to the GenerateKey function and its arguments. The malware uses the machine’s hostname as the name and an email address of “a@a.a”. No password is provided and it’s requesting a 2048-bit RSA key to be used. The key is stored and written to disk. The Linux version of HabitsRAT writes to “$HOME/.config/.accounts-daemon/accounts-daemon.login.conf” if it is running as a normal user or to “/usr/share/accounts-daemon/accounts-daemon.so”. The Windows version uses “%SystemDrive%WindowsDefenderMsMpEng.dll” or “%APPDATA%Windows NTDefenderMsMpEng.dll” instead.no_replay: [sha256 hash of random data]
public_key: public key in ascii armour
hostname: [machine hostname]
goos: [linux or window]
goarch: amd64
shell: [$SHELL expanded]
root: [true or false]
version: 11
The data is sent to “https://brian.krebsonsecurity[.]top/checkin”. If no command is returned, the malware sleeps for 10 seconds and sends the request again. If the C2 responds with data, the malware checks that the threat actor’s key has signed it. A hardcoded public key is included in the binary. Extracted information from the key shows that it was generated in December 2020 and includes a name and a Gmail address.pub rsa3072 2020-12-03 [SC] [expires: 2022-12-03]
uid [REDACTED] <[REDACTED]@gmail.com>
sub rsa3072 2020-12-03 [E] [expires: 2022-12-03]
If the correct key has signed the response, HabitsRAT uses its private key to decrypt the payload. The data has been serialized to JSON and the malware unmarshals it to the data structure shown below.type main.CommandList struct {
No_replay string
Commands []string
}
The Commands field is passed as arguments to either “bash -c” for the Linux version or “cmd /c” for the Windows version.HabitsRAT Version 12
A newer Windows version of HabitsRAT has also been found. Much of the functionality is the same as version 11. The main difference is that it’s using a different C2 public key and supports multiple C2 addresses. As can be seen from the snippet below, this key was generated on the 2nd of April.pub rsa3072 2021-04-02 [SC] [expires: 2023-04-02]
uid Brian Krebs <krebsonsecurity@gmail.com>
sub rsa3072 2021-04-02 [E] [expires: 2023-04-02]
The malware uses four different C2 addresses and picks one out of random. The addresses are as follows, which includes a domain of Brian Krebs’s leaked social security number:- https://brian-krebs-erectile-dysfunction[.]com
- https://krebsonfellatio[.]net
- http://XXX-XX-XXXX.com (Redacted)
- hxxp://185.193.126.198
- %SystemDrive%WindowsDefenderDefender.dll
- %APPDATA%Windows NTDefenderDefender.dll
Conclusion
The HabitsRAT is a multi-operating system malware targeting both Windows and Linux environments. There is a lot of code reuse between the two variants. It provides the attacker with the capability to execute arbitrary code on the infected machine. To protect its C2 communication, the data is encrypted and signed using PGP. Ensure internet facing servers are patched to prevent being infected by HabitsRAT. Indicators of Compromise (IoCs) below can be used to detect if a server has been compromised. Go malware has been hard to detect by Antivirus products so it’s likely this trend will continue. We have seen threat actors pivot and target different operating systems with the same codebase for the malware, resulting in low or undetected malware samples, especially for Linux—which has a large presence in the cloud. Since the malware is derived from the same codebase, detection based on code reuse has proven to be very effective. Runtime protection with Intezer Protect gives you immediate visibility over all code running in your systems and alerts you whenever unauthorized or malicious code is executed. Intezer Protect users can detect and mitigate threats like HabitsRAT on their Linux systems. Protect 10 hosts for free with our community edition.IoCs
Hashes
Windows version of HabitsRAT
- 29ebf9771e52cde90776eeccd89aaf4c19577ef136258daef1a17c767ce88c9d
- 37a16e79e5be132d7e6c2e1ee482d80d93ad942af7110a4bc3a05f0b575236b0
- 5f7d898ade3162bfb0c8d3006c42e934ff81fab3b4ad3b51c13441fd63e438cb
- 9e840be4b4ab358bc3405e2c688f3ab1a9d286bd4fb9edb4468dc688962b4893
- f556c9b4e5bb463be84dead45a9aedcf8bec41c1c2b503ea52719357943750e7
Linux version of HabitsRAT
- 338e41f1a8be56339b039835b06d815a3666c8b0d5725b63be7bf54c8745704a
File paths
- %SystemDrive%WindowsDefenderMsMpEng.exe
- $HOME/.config/polkitd/polkitd
- /etc/systemd/system/polkitd.service
- %TEMP%krebsonsecurity.xml
- $HOME/.config/.accounts-daemon/accounts-daemon.login.conf
- /usr/share/accounts-daemon/accounts-daemon.so
- %SystemDrive%WindowsDefenderMsMpEng.dll
- %APPDATA%Windows NTDefenderMsMpEng.dll
- %SystemDrive%WindowsDefenderDefender.dll
- %APPDATA%Windows NTDefenderDefender.dll
Network indicators
- brian[.]krebsonsecurity[.]top
- brian-krebs-erectile-dysfunction[.]com
- krebsonfellatio[.]net
- 185.193.126.198
C2 public keys
Version 11
—–BEGIN PGP PUBLIC KEY BLOCK—–mQGNBF/I9bUBDACtHQlddPduY2DXMrQHxsh+jCP2ojeMi+08VmuC/eCG3+x0815p
ymssBejVcCckahu0EIJZIl5WaRY+nOJKF9VOdLoegpVmqPmX3GE0FJBR/cGGLSqQ
bofuDbWBIwQPVwHT+QriDpAK9M80H5f6FPm2HqcXJV2fI7FJ5pLWSTMRG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