Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5.  
  6. #define MAX_ELEM 256
  7. #define MAX_LEN 256
  8. #define NOT_PHOTOS 2
  9. #define SLASH "/"
  10. #define INFECTED_MSG " - Infected!"
  11. #define CLEAN_MSG " - Clean"
  12. #define INFECTED 1
  13. #define CLEAN 2
  14. #define LOG_FILE_NAME "Log.txt"
  15. #define NEW_LINE "\n"
  16.  
  17. int pathsOpener(char** paths);
  18. void dirsParser(char** paths, char files[][MAX_LEN], char virusSign[],  int photosCounter);
  19. void scan(char** argv, char photosPaths[][MAX_LEN], char virusSignPath[], int choice, int photosCounter);
  20. void createLog(char** photosFolder, char fileFoundPath[],  int mode);
  21.  
  22. int photosCounter = 0;
  23. char photosPaths[MAX_ELEM][MAX_LEN] = { 0 };
  24. char virusSignPath[MAX_LEN] = { 0 };
  25. char* logFilePath = 0;
  26.  
  27. int main(int argc, char** argv)
  28. {
  29.     int choice = 0;
  30.     if (pathsOpener(argv))
  31.     {
  32.         printf("Welcome to my Virus Scan!\n\n");
  33.         printf("Folder to scan: %s\n", argv[1]);
  34.         printf("Virus signature: %s\n\n", argv[2]);
  35.         printf("Press 0, for normal scan.\nPress any other key for fast scan.\nYour choice: ");
  36.         scanf("%d", &choice);
  37.         getchar();
  38.  
  39.         scan(argv, photosPaths, virusSignPath, choice, photosCounter);
  40.     }
  41.     else
  42.     {
  43.         return 0;
  44.     }
  45.     printf("Scan completed.\nSee log path for results: %s", logFilePath);
  46.     //free(logFilePath);
  47.     getchar();
  48.     return 0;
  49. }
  50.  
  51. int pathsOpener(char** paths)
  52. {
  53.     struct dirent* de;
  54.     DIR* dir;
  55.     FILE* virusSign;
  56.     char files[MAX_ELEM][MAX_LEN] = { 0 };
  57.     char tempFiles[MAX_ELEM][MAX_LEN] = { 0 };
  58.     char virusSignFile[MAX_LEN] = "";
  59.     char tempSort[256] = { 0 };
  60.     de = opendir(paths[1]);
  61.     int i = 0, j = 0;
  62.  
  63.     if (de)
  64.     {
  65.         while ((dir = readdir(de)) != NULL)
  66.         {
  67.             strcpy(files[photosCounter], de->d_name);
  68.             photosCounter++;
  69.         }
  70.         closedir(de);
  71.     }
  72.     else
  73.     {
  74.         printf("Could not open the folder.\n");
  75.         return 0;
  76.     }
  77.    
  78.     virusSign = fopen(paths[2], "rb");
  79.     if (!virusSign)
  80.     {
  81.         printf("Could not open the virus Signature file.\n");
  82.         return 0;
  83.     }
  84.     else
  85.     {
  86.         strcpy(virusSignFile, paths[2]);
  87.     }
  88.     fclose(virusSign);
  89.    
  90.     for (i = photosCounter - NOT_PHOTOS; i < photosCounter; i++)
  91.     {
  92.         for (j = photosCounter - NOT_PHOTOS; j < photosCounter; j++)
  93.         {
  94.             if (strcmp(files[i], files[j]) < 0)
  95.             {
  96.                 strcpy(tempSort, files[i]);
  97.                 strcpy(files[i], files[j]);
  98.                 strcpy(files[j], tempSort);
  99.             }
  100.         }
  101.     }
  102.  
  103.     dirsParser(paths, files, virusSignFile, photosCounter);
  104. }
  105.  
  106. void dirsParser(char** paths, char files[][MAX_LEN], char virusSign[], int photosCounter)
  107. {
  108.     char temp[MAX_LEN] = { 0 };
  109.     int i = 0, j = 0;
  110.  
  111.     strcpy(temp, paths[1]);
  112.     strcat(temp, SLASH);
  113.  
  114.     for (i = 0, j = NOT_PHOTOS; i < photosCounter - NOT_PHOTOS, j < photosCounter; i++, j++)
  115.     {
  116.         strcpy(photosPaths[i], temp);
  117.         strcat(photosPaths[i], files[j]);
  118.     }
  119.  
  120.     strcat(virusSignPath, virusSign);
  121.  
  122. }
  123.  
  124. void scan(char** argv, char photosPaths[][MAX_LEN], char virusSignPath[], int choice, int photosCounter)
  125. {
  126.     FILE* virusSign = fopen(virusSignPath, "rb");
  127.     FILE* photo;
  128.     long virusFileSize = 0;
  129.     char* virusSignArr = 0;
  130.     char* tempBuffer = 0;
  131.     int i = 0, j = 0, k = 0, matchCounter = 0, flag = 0, flagFound = 0;
  132.  
  133.  
  134.     fseek(virusSign, 0, SEEK_END);
  135.     virusFileSize = ftell(virusSign);
  136.     fseek(virusSign, 0, SEEK_SET);
  137.  
  138.     virusSignArr = (char*)malloc(virusFileSize * sizeof(char));
  139.     tempBuffer = (char*)malloc(1 * sizeof(char));
  140.     fread(virusSignArr, sizeof(char), virusFileSize, virusSign);    //Saves virus signature
  141.     fclose(virusSign);
  142.  
  143.     if (!choice)
  144.     {
  145.         for (i = 0; i < photosCounter - NOT_PHOTOS; i++)
  146.         {
  147.             flagFound = 0;
  148.             photo = fopen(photosPaths[i], "rb");
  149.             matchCounter = 0;
  150.             while (!feof(photo) && flagFound == 0)
  151.             {
  152.                 matchCounter = 0;
  153.                 flag = 0;
  154.                 fread(tempBuffer, 1, 1, photo);
  155.  
  156.                 for (j = 0; j < virusFileSize && flag == 0; j++)
  157.                 {
  158.                     if (virusSignArr[j] == *tempBuffer)
  159.                     {
  160.                         fread(tempBuffer, 1, 1, photo);
  161.                         matchCounter++;
  162.                         if (matchCounter == virusFileSize)
  163.                         {
  164.                             printf("%s - Infected!\n", photosPaths[i]);
  165.                             flagFound = 1;
  166.                             flag = 1;
  167.                             createLog(argv, photosPaths[i], INFECTED);
  168.                         }
  169.                     }
  170.                     else
  171.                     {
  172.                         flag = 1;
  173.                     }
  174.                 }
  175.             }
  176.             fclose(photo);
  177.             if (flagFound == 0)
  178.             {
  179.                 printf("%s - Clean\n", photosPaths[i]);
  180.                 createLog(argv, photosPaths[i], CLEAN);
  181.             }
  182.         }
  183.     }
  184.     free(tempBuffer);
  185.     free(virusSignArr);
  186. }
  187.  
  188. void createLog(char** photosFolder, char fileFoundPath[], int mode)
  189. {
  190.     logFilePath = (char*)malloc(strlen(photosFolder[1]) + strlen(LOG_FILE_NAME) + 1 * sizeof(char));
  191.     strcpy(logFilePath, photosFolder[1]);
  192.     strcat(logFilePath, SLASH);
  193.     strcat(logFilePath, LOG_FILE_NAME);
  194.    
  195.    
  196.     FILE* log = 0;
  197.     char welcome[] = "Anti-virus began! Welcome!\n";
  198.     char folderToScnPath[] = "Folder to scan:\n";
  199.     char virusSignature[] = "Virus signature:\n";
  200.     char results[] = "Results:\n";
  201.     char scnOptn[] = "Scanning option:\nNormal Scan\n";
  202.     char* photoData = 0;
  203.  
  204.     if (fopen(logFilePath, "r") == 0)
  205.     {
  206.         log = fopen(logFilePath, "w");
  207.         fwrite(welcome, 1, sizeof(welcome) - 1, log);
  208.         fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  209.         fwrite(folderToScnPath, 1, sizeof(folderToScnPath) - 1, log);
  210.         fwrite(photosFolder[1], 1, strlen(photosFolder[1]), log);
  211.         fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  212.         fwrite(virusSignature, 1, sizeof(virusSignature) - 1, log);
  213.         fwrite(photosFolder[2], 1, strlen(photosFolder[2]), log);
  214.         fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  215.         fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  216.         fwrite(scnOptn, 1, sizeof(scnOptn) - 1, log);
  217.         fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  218.         fwrite(results, 1, sizeof(results) - 1, log);
  219.  
  220.         if (mode == INFECTED)
  221.         {
  222.             photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(INFECTED_MSG)) * sizeof(char));
  223.             strcpy(photoData, fileFoundPath);
  224.             strcat(photoData, INFECTED_MSG);
  225.             fwrite(photoData, 1, strlen(photoData), log);
  226.             fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  227.         }
  228.         else if (mode = CLEAN)
  229.         {
  230.             photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(CLEAN_MSG)) * sizeof(char));
  231.             strcpy(photoData, fileFoundPath);
  232.             strcat(photoData, CLEAN_MSG);
  233.             fwrite(photoData, 1, strlen(photoData), log);
  234.             fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  235.         }
  236.     }
  237.     else
  238.     {
  239.         log = fopen(logFilePath, "a");
  240.  
  241.         if (mode == INFECTED)
  242.         {
  243.             photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(INFECTED_MSG)) * sizeof(char));
  244.             strcpy(photoData, fileFoundPath);
  245.             strcat(photoData, INFECTED_MSG);
  246.             fwrite(photoData, 1, strlen(photoData), log);
  247.             fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  248.         }
  249.         else if (mode = CLEAN)
  250.         {
  251.             photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(CLEAN_MSG)) * sizeof(char));
  252.             strcpy(photoData, fileFoundPath);
  253.             strcat(photoData, CLEAN_MSG);
  254.             fwrite(photoData, 1, strlen(photoData), log);
  255.             fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
  256.         }
  257.     }
  258.    
  259.     free(photoData);
  260.     fclose(log);
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement