Clop Ransomware: Understanding the Algorithm and Implementation in C++ with AES Encryption Example

Ahmad Kataranjee
5 min readMar 10, 2023
Image credits

Clop Ransomware is a type of malware that encrypts the victim’s files and demands payment in exchange for the decryption key. The exact workings of the algorithm used by Clop Ransomware may vary depending on the specific version of the malware, but here is a general overview of how it might work:

  1. Infection: Clop Ransomware typically infects a victim’s computer through a phishing email, a malicious attachment, or a software vulnerability.
  2. Encryption: Once installed on the victim’s computer, Clop Ransomware will scan the system for specific file types to encrypt. It may target files such as documents, images, videos, and databases. The encryption process uses a combination of the RSA and AES encryption algorithms, which are designed to be extremely difficult to crack without the decryption key. The RSA algorithm is used to encrypt a unique symmetric key for each file, and the AES algorithm is used to encrypt the actual file data.
  3. Ransom note: After the files are encrypted, Clop Ransomware will typically create a ransom note in the form of a text file, image, or pop-up message. The note will usually demand payment in exchange for the decryption key. The note may also contain instructions for making the payment, such as using cryptocurrency to transfer funds to the attacker.
  4. Payment: If the victim chooses to pay the ransom, they will typically be given instructions for how to transfer the funds to the attacker. In some cases, the attacker may provide a decryption key after the payment is made. However, there is no guarantee that the attacker will actually provide the decryption key or that it will work.
  5. Recovery: In some cases, it may be possible to recover encrypted files without paying the ransom. However, this typically requires specialized tools and knowledge of the specific encryption algorithm used by the malware.

Let’s write a simple example of how Clop Ransomware Algorithm might work in C++ without including any actual malicious code :)

#include <iostream>
#include <string>
#include <fstream>
#include <openssl/rsa.h>
#include <openssl/aes.h>

using namespace std;

int main() {

// Step 1: Infection - This step is not included in the code example.

// Step 2: Encryption
string filename = "important_document.docx";
string encrypted_filename = "important_document_encrypted.docx";
string rsa_key = "rsa_key.txt"; // Store the RSA key for decryption later
int aes_key_size = 256;
unsigned char aes_key[aes_key_size / 8];
AES_KEY aes_key_struct;

// Generate a unique AES key for each file
RAND_bytes(aes_key, aes_key_size / 8);

// Encrypt the symmetric AES key using RSA
RSA *rsa = RSA_generate_key(aes_key_size, RSA_F4, nullptr, nullptr);
ofstream rsa_file(rsa_key);
PEM_write_RSAPublicKey(rsa_file, rsa);
rsa_file.close();

unsigned char encrypted_aes_key[RSA_size(rsa)];
int encrypted_aes_key_size = RSA_public_encrypt(aes_key_size / 8, aes_key, encrypted_aes_key, rsa, RSA_PKCS1_PADDING);

// Encrypt the file data using AES
AES_set_encrypt_key(aes_key, aes_key_size, &aes_key_struct);

ifstream input_file(filename, ios::binary);
ofstream encrypted_file(encrypted_filename, ios::binary);
unsigned char input_block[AES_BLOCK_SIZE];
unsigned char encrypted_block[AES_BLOCK_SIZE];
while (input_file.read((char*)input_block, AES_BLOCK_SIZE)) {
AES_encrypt(input_block, encrypted_block, &aes_key_struct);
encrypted_file.write((char*)encrypted_block, AES_BLOCK_SIZE);
}
if (input_file.gcount() > 0) {
int padding_size = AES_BLOCK_SIZE - input_file.gcount();
memset(input_block + input_file.gcount(), padding_size, padding_size);
AES_encrypt(input_block, encrypted_block, &aes_key_struct);
encrypted_file.write((char*)encrypted_block, AES_BLOCK_SIZE);
}
input_file.close();
encrypted_file.close();

// Step 3: Ransom note - This step is not included in the code example :) .

// Step 4: Payment - This step is not included in the code example :) .

// Step 5: Recovery - This step is not included in the code example :) .

return 0;
}

Here is an overview of how the code works:

Step 1: Infection — This step is not included in the code example. In the real Clop Ransomware, this step would involve infecting the victim’s system by exploiting vulnerabilities or using social engineering techniques.

Step 2: Encryption

  • The code starts by specifying the name of the file to be encrypted (filename) and the name of the encrypted file that will be generated (encrypted_filename). It also defines the size of the AES key to be used (aes_key_size) and the path to the file where the RSA public key will be stored (rsa_key).
  • The next step is to generate a unique AES key for each file using the RAND_bytes() function, which is provided by the OpenSSL library.
  • The AES key is then encrypted using RSA. First, a new RSA key pair is generated using RSA_generate_key(). Then, the public key is written to a file specified by rsa_key using the PEM_write_RSAPublicKey() function. Finally, the RSA_public_encrypt() function is used to encrypt the AES key using the RSA public key, and the encrypted key is stored in encrypted_aes_key.
  • The file specified by filename is then opened for reading in binary mode, and the encrypted file specified by encrypted_filename is opened for writing in binary mode. The AES_set_encrypt_key() function is used to initialize the AES key for encryption.
  • The file data is read from filename in blocks of 16 bytes (the size of an AES block). Each block is encrypted using AES with the AES_encrypt() function, and the resulting encrypted block is written to encrypted_file. If the last block of the file is less than 16 bytes, it is padded with the number of bytes needed to make it 16 bytes before being encrypted and written to encrypted_file.
  • Finally, filename and encrypted_filename are closed.

Step 3: Ransom note — This step is not included in the code example. In the real Clop Ransomware, this step would involve creating and displaying a ransom note on the victim’s system, typically containing instructions for how to pay the ransom and receive the decryption key.

Step 4: Payment — This step is not included in the code example. In the real Clop Ransomware, this step would involve receiving the ransom payment from the victim and providing them with the decryption key.

Step 5: Recovery — This step is not included in the code example. In the real Clop Ransomware, this step would involve using the decryption key to decrypt the victim’s files.

Please note that the code example I provided is for educational purposes only and should not be used for any illegal or unethical activities.

It’s important to note that the use of ransomware, including Clop Ransomware, is illegal and unethical. Victims should never pay the ransom, as this only encourages the attackers to continue their criminal activities. Instead, victims should seek assistance from law enforcement and cybersecurity experts to try to recover their encrypted files and prevent further damage.

--

--

Ahmad Kataranjee

👨‍💻👉Computer Engineer - Engineers first! Human rights. Travels and adventures. Deep discussions. Jokes and pranks. Music and movies. Chess and light games.