# HiddenWasp Malware Stings Targeted Linux Systems

> HiddenWasp is a Linux trojan with a rootkit, undetected at discovery, used for targeted remote control and reusing code from Mirai and the Azazel rootkit.

- Source: https://research.intezer.com/blog/2019/05/hiddenwasp-malware-targeting-linux-systems/
- Published: 2019-05-29
- Authors: Ignacio Sanmillan
- Tags: linux, botnet
- Attribution: Intezer's Research Team (Intezer Research, https://research.intezer.com/)


## Overview

- Intezer has discovered a new, sophisticated malware that we have named
  "HiddenWasp", targeting Linux systems.

- The malware is still active and has a zero-detection rate in all major
  anti-virus systems.

- Unlike common Linux malware, HiddenWasp is not focused on crypto-mining or
  DDoS activity. It is a trojan purely used for targeted remote control.

- Evidence shows in high probability that the malware is used in targeted
  attacks for victims who are already under the attacker’s control, or have
  gone through a heavy reconnaissance.

- HiddenWasp authors have adopted a large amount of code from various publicly
  available open-source malware, such as Mirai and the Azazel rootkit. In
  addition, there are some similarities between this malware and other Chinese
  malware families, however the attribution is made with low confidence.

- We have detailed our recommendations for preventing and responding to this
  threat.

## Introduction

Although the Linux threat ecosystem is crowded with IoT DDoS botnets and
crypto-mining malware, it is not very common to spot trojans or backdoors in
the wild.

Unlike Windows malware, Linux malware authors do not seem to invest too much
effort writing their implants. In an open-source ecosystem there is a high
ratio of publicly available code that can be copied and adapted by attackers.

In addition, Anti-Virus solutions for Linux tend to not be as resilient as in
other platforms. Therefore, threat actors targeting Linux systems are less
concerned about implementing excessive evasion techniques since even when
reusing extensive amounts of code, threats can relatively manage to stay under
the radar.

Nevertheless, malware with strong evasion techniques do exist for the Linux
platform. There is also a high ratio of publicly available open-source malware
that utilize strong evasion techniques and can be easily adapted by attackers.

We believe this fact is alarming for the security community since many implants
today have very low detection rates, making these threats difficult to detect
and respond to.

We have discovered further undetected Linux malware that appear to be enforcing
advanced evasion techniques with the use of rootkits to leverage trojan-based
implants.

In this blog we will present a technical analysis of each of the different
components that this new malware, HiddenWasp, is composed of. We will also
highlight interesting code-reuse connections that we have observed to several
open-source malware.

The following images are screenshots from VirusTotal of the newer undetected
malware samples discovered:

![](images/fig1.png)

## Technical Analysis

When we came across these samples we noticed that the majority of their code
was unique:

![](images/fig2.png) ![](images/fig3.png)

Similar to the recent Winnti Linux variants reported by
[Chronicle](https://medium.com/chronicle-blog/winnti-more-than-just-windows-and-gates-e4f03436031a),
the infrastructure of this malware is composed of a user-mode rootkit, a trojan
and an initial deployment script. We will cover each of the three components in
this post, analyzing them and their interactions with one another.

### Initial Deployment Script:

When we spotted these undetected files in VirusTotal it seemed that among the
uploaded artifacts there was a bash script along with a trojan implant binary.

![](images/fig4.png "Initial Deployment Script")

We observed that these files were uploaded to VirusTotal using a path
containing the name of a Chinese-based forensics company known as
[Shen Zhou Wang Yun Information Technology Co., Ltd](http://www.china-forensic.com/ccfc/en/).

Furthermore, the malware implants seem to be hosted in servers from a physical
server hosting company known as ThinkDream located in Hong Kong.

![](images/fig5.png)

Among the uploaded files, we observed that one of the files was a bash script
meant to deploy the malware itself into a given compromised system, although it
appears to be for testing purposes:

![](images/fig6.png)

Thanks to this file we were able to download further artifacts not present in
VirusTotal related to this campaign. This script will start by defining a set
of variables that would be used throughout the script.

![](images/fig7.png)

Among these variables we can spot the credentials of a user named 'sftp',
including its hardcoded password. This user seems to be created as a means to
provide initial persistence to the compromised system:

![](images/fig8.png)

Furthermore, after the system’s user account has been created, the script
proceeds to clean the system as a means to update older variants if the system
was already compromised:

![](images/fig9.png) The script will then proceed to download a tar compressed
archive from a download server according to the architecture of the compromised
system. This tarball will contain all of the components from the malware,
containing the rootkit, the trojan and an initial deployment script:

![](images/fig10.png) After malware components have been installed, the script
will then proceed to execute the trojan:

![](images/fig11.png)

We can see that the main trojan binary is executed, the rootkit is added to
LD_PRELOAD path and another series of environment variables are set such as the
'I_AM_HIDDEN'. We will cover throughout this post what the role of this
environment variable is. To finalize, the script attempts to install reboot
persistence for the trojan binary by adding it to /etc/rc.local.

Within this script we were able to observe that the main implants were
downloaded in the form of tarballs. As previously mentioned, each tarball
contains the main trojan, the rootkit and a deployment script for x86 and
x86_64 builds accordingly.

![](images/fig12.png)

The deployment script has interesting insights of further features that the
malware implements, such as the introduction of a new environment variable
'HIDE_THIS_SHELL':

![](images/fig13.png)

We found some of the environment variables used in a open-source rootkit known
as
[Azazel](https://github.com/chokepoint/azazel/search?q=HIDE_THIS_SHELL&unscoped_q=HIDE_THIS_SHELL).

![](images/fig14.png)

It seems that this actor changed the default environment variable from Azazel,
that one being HIDE_THIS_SHELL for I_AM_HIDDEN. We have based this conclusion
on the fact that the environment variable HIDE_THIS_SHELL was not used
throughout the rest of the components of the malware and it seems to be
residual remains from Azazel original code.

The majority of the code from the rootkit implants involved in this malware
infrastructure are noticeably different from the original Azazel project.
Winnti Linux variants are also known to have reused code from this open-source
project.

### The Rootkit

The rootkit is a user-space based rootkit enforced via LD_PRELOAD linux
mechanism.

It is delivered in the form of an ET_DYN stripped ELF binary.

This shared object has an DT_INIT dynamic entry. The value held by this entry
is an address that will be executed once the shared object gets loaded by a
given process:

![](images/fig15.png)

Within this function we can see that eventually control flow falls into a
function in charge to resolve a set of dynamic imports, which are the functions
it will later hook, alongside with decoding a series of strings needed for the
rootkit operations.

![](images/fig16.png)

We can see that for each string it allocates a new dynamic buffer, it copies
the string to it to then decode it.

It seems that the implementation for dynamic import resolution slightly varies
in comparison to the one used in
[Azazel](https://github.com/chokepoint/azazel/blob/master/config.py) rootkit.

When we wrote the script to simulate the cipher that implements the string
decoding function we observed the following algorithm:

![](images/fig17.png)

We recognized that a similar algorithm to the one above was used in the past by
[Mirai](https://github.com/jgamblin/Mirai-Source-Code/blob/master/mirai/bot/scanner.c#L963),
implying that authors behind this rootkit may have ported and modified some
code from Mirai.

![](images/fig18.png)

After the rootkit main object has been loaded into the address space of a given
process and has decrypted its strings, it will export the functions that are
intended to be hooked. We can see these exports to be the following:

![](images/fig19.png)

For every given export, the rootkit will hook and implement a specific
operation accordingly, although they all have a similar layout. Before the
original hooked function is called, it is checked whether the environment
variable 'I_AM_HIDDEN' is set:

![](images/fig20.png)

We can see an example of how the rootkit hooks the function fopen in the
following screenshot:

![](images/fig21.png)

We have observed that after checking whether the 'I_AM_HIDDEN' environment
variable is set, it then runs a function to hide all the rootkits’ and trojans'
artifacts. In addition, specifically to the fopen function it will also check
whether the file to open is '/proc/net/tcp' and if it is it will attempt to
hide the malware’s connection to the cnc by scanning every entry for the
destination or source ports used to communicate with the cnc, in this case
61061\. This is also the default port in
[Azazel](https://github.com/chokepoint/azazel/blob/master/config.py) rootkit.

![](images/fig22.png)

The rootkit primarily implements artifact hiding mechanisms as well as tcp
connection hiding as previously mentioned. Overall functionality of the rootkit
can be illustrated in the following diagram:

![](images/fig23.png)

### The Trojan

The trojan comes in the form of a statically linked ELF binary linked with
stdlibc++. We noticed that the trojan has code connections with
[ChinaZ's](../../../2019/01/chinaz-relations) Elknot implant in regards to some
common MD5 implementation in one of the statically linked libraries it was
linked with:

![](images/fig24.png)

In addition, we also see a high rate of shared strings with other known ChinaZ
malware, reinforcing the possibility that actors behind HiddenWasp may have
integrated and modified some MD5 implementation from Elknot that could have
been shared in Chinese hacking forums:

![](images/fig25.png)

When we analyze the main we noticed that the first action the trojan takes is
to retrieve its configuration:

![](images/fig26.png)

The malware configuration is appended at the end of the file and has the
following structure:

![](images/fig27.png)

The malware will try to load itself from the disk and parse this blob to then
retrieve the static encrypted configuration.

![](images/fig28.png)

Once encryption configuration has been successfully retrieved the configuration
will be decoded and then parsed as json.

The cipher used to encode and decode the configuration is the following:

![](images/fig29.png)

This cipher seems to be an RC4 alike algorithm with an already computed PRGA
generated key-stream. It is important to note that this same cipher is used
later on in the network communication protocol between trojan clients and their
CNCs.

After the configuration is decoded the following json will be retrieved:

![](images/fig30.png)

Moreover, if the file is running as root, the malware will attempt to change
the default location of the dynamic linker’s LD_PRELOAD path. This location is
usually at /etc/ld.so.preload, however there is always a possibility to patch
the dynamic linker binary to change this path:

![](images/fig31.png)

Patch_ld function will scan for any existent /lib paths. The scanned paths are
the following:

![](images/fig32.png)

The malware will attempt to find the dynamic linker binary within these paths.
The dynamic linker filename is usually prefixed with ld-<version number>.

![](images/fig33.png)

Once the dynamic linker is located, the malware will find the offset where the
/etc/ld.so.preload string is located within the binary and will overwrite it
with the path of the new target preload path, that one being /sbin/.ifup-local.

![](images/fig34.png)

To achieve this patching it will execute the following formatted string by
using the xxd hex editor utility by previously having encoded the path of the
rootkit in hex:

![](images/fig35.png)

Once it has changed the default LD_PRELOAD path from the dynamic linker it will
deploy a thread to enforce that the rootkit is successfully installed using the
new LD_PRELOAD path. In addition, the trojan will communicate with the rootkit
via the environment variable ‘I_AM_HIDDEN’ to serialize the trojan’s session
for the rootkit to apply evasion mechanisms on any other sessions.

![](images/fig36.png)

After seeing the rootkit’s functionality, we can understand that the rootkit
and trojan work together in order to help each other to remain persistent in
the system, having the rootkit attempting to hide the trojan and the trojan
enforcing the rootkit to remain operational. The following diagram illustrates
this relationship:

![](images/fig37.png)

Continuing with the execution flow of the trojan, a series of functions are
executed to enforce evasion of some artifacts:

![](images/fig38.png)

These artifacts are the following:

![](images/fig39.png)

By performing some OSINT regarding these artifact names, we found that they
belong to a Chinese open-source rootkit for Linux known as
[Adore-ng](https://github.com/yaoyumeng/adore-ng) hosted in GitHub:

![](images/fig40.png)

The fact that these artifacts are being searched for suggests that potentially
targeted Linux systems by these implants may have already been compromised with
some variant of this open-source rootkit as an additional artifact in this
malware’s infrastructure. Although those paths are being searched for in order
to hide their presence in the system, it is important to note that none of the
analyzed artifacts related to this malware are installed in such paths.

This finding may imply that the target systems this malware is aiming to
intrude may be already known compromised targets by the same group or a third
party that may be collaborating with the same end goal of this particular
campaign.

Moreover, the trojan communicated with a simple network protocol over TCP. We
can see that when connection is established to the Master or Stand-By servers
there is a handshake mechanism involved in order to identify the client.

![](images/fig41.png)

With the help of this function we where able to understand the structure of the
communication protocol employed. We can illustrate the structure of this
communication protocol by looking at a pcap of the initial handshake between
the server and client:

![](images/fig42.png)

We noticed while analyzing this protocol that the Reserved and Method fields
are always constant, those being 0 and 1 accordingly. The cipher table offset
represents the offset in the hardcoded key-stream that the encrypted payload
was encoded with. The following is the fixed keystream this field makes
reference to:

![](images/fig43.png)

After decrypting the traffic and analyzing some of the network related
functions of the trojan, we noticed that the communication protocol is also
implemented in json format. To show this, the following image is the decrypted
handshake packets between the CNC and the trojan:

![](images/fig44.png)

After the handshake is completed, the trojan will proceed to handle CNC
requests:

![](images/fig45.png)

Depending on the given requests the malware will perform different operations
accordingly. An overview of the trojan’s functionalities performed by request
handling are shown below:

![](images/fig46.png)

## Prevention and Response

Prevention: Block Command-and-Control IP addresses detailed in the IOCs
section.

Response: We have provided a
[YARA rule](https://github.com/intezer/yara-rules/blob/master/HiddenWasp.yar)
intended to be run against in-memory artifacts in order to be able to detect
these implants.

In addition, in order to check if your system is infected, you can search for
“ld.so” files — if any of the files do not contain the string
‘/etc/ld.so.preload’, your system may be compromised. This is because the
trojan implant will attempt to patch instances of ld.so in order to enforce the
LD_PRELOAD mechanism from arbitrary locations.

## Summary

We analyzed every component of HiddenWasp explaining how the rootkit and trojan
implants work in parallel with each other in order to enforce persistence in
the system.

We have also covered how the different components of HiddenWasp have adapted
pieces of code from various open-source projects. Nevertheless, these implants
managed to remain undetected.

Linux malware may introduce new challenges for the security community that we
have not yet seen in other platforms. The fact that this malware manages to
stay under the radar should be a wake up call for the security industry to
allocate greater efforts or resources to detect these threats.

Linux malware will continue to become more complex over time and currently even
common threats do not have high detection rates, while more sophisticated
threats have even lower visibility.

## IOCs

```
103.206.123[.]13
103.206.122[.]245
http://103.206.123[.]13:8080/system.tar.gz
http://103.206.123[.]13:8080/configUpdate.tar.gz
http://103.206.123[.]13:8080/configUpdate-32.tar.gz
e9e2e84ed423bfc8e82eb434cede5c9568ab44e7af410a85e5d5eb24b1e622e3
f321685342fa373c33eb9479176a086a1c56c90a1826a0aef3450809ffc01e5d
d66bbbccd19587e67632585d0ac944e34e4d5fa2b9f3bb3f900f517c7bbf518b
0fe1248ecab199bee383cef69f2de77d33b269ad1664127b366a4e745b1199c8
2ea291aeb0905c31716fe5e39ff111724a3c461e3029830d2bfa77c1b3656fc0
d596acc70426a16760a2b2cc78ca2cc65c5a23bb79316627c0b2e16489bf86c0
609bbf4ccc2cb0fcbe0d5891eea7d97a05a0b29431c468bf3badd83fc4414578
8e3b92e49447a67ed32b3afadbc24c51975ff22acbd0cf8090b078c0a4a7b53d
f38ab11c28e944536e00ca14954df5f4d08c1222811fef49baded5009bbbc9a2
8914fd1cfade5059e626be90f18972ec963bbed75101c7fbf4a88a6da2bc671b
```
