Advertisement
Yonka2019

antivirus.c

May 19th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.22 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include "dirent.h"
  7.  
  8. #define ARGS_COUNT 2 // Not including the program name
  9. #define BUFFER 1000
  10.  
  11. void writeLog();
  12. void chooseAndScan(char** argv);
  13. void printWelcome(char** argv);
  14. void checkArgs(int argc);
  15. void normalScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum);
  16. void quickScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum);
  17. int getFiles(char* folderDirectory, char** filesList);
  18. bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo);
  19.  
  20. enum scanTypes
  21. {
  22.     normalScanType,
  23.     quickScanType
  24. } scanType;
  25.  
  26. char logBuilder[BUFFER] = { 0 };
  27.  
  28. int main(int argc, char** argv)
  29. {
  30.     checkArgs(argc);
  31.     /*
  32.     * argv[0] = Program Name
  33.     * argv[1] = Folder to scan
  34.     * argv[2] = Virus signature
  35.     */
  36.  
  37.     printWelcome(argv);
  38.  
  39.     chooseAndScan(argv);
  40.      
  41.     writeLog();
  42.  
  43.  
  44.     getchar();
  45.     return 0;
  46.  
  47. }
  48. /// <summary>
  49. /// Checks if the arguments of the program correctly
  50. /// </summary>
  51. /// <param name="argc">Number of the given arguments</param>
  52. void checkArgs(int argc)
  53. {
  54.     if (argc != ARGS_COUNT + 1)
  55.     {
  56.         printf("Invalid execution.\nUsage:\n"
  57.             "> Antivirus <Folder_to_scan [directory]> <Virus_signature [directory]>\n");
  58.  
  59.         getchar();
  60.         exit(1); // Stop program
  61.     }
  62. }
  63. int getFiles(char* folderDirectory, char** filesList)
  64. {
  65.     DIR* d = 0;
  66.     int i = 0;
  67.     struct dirent* dir = 0;
  68.  
  69.     d = opendir(folderDirectory);
  70.  
  71.     if (d == NULL)
  72.     {
  73.         printf("Error opening directory: %s\n", folderDirectory);
  74.  
  75.         exit(1); // Stop program
  76.     }
  77.  
  78.     while ((dir = readdir(d)) != NULL)
  79.     {
  80.         if (strcmp(dir->d_name, ".") &&
  81.             strcmp(dir->d_name, "..") &&
  82.             dir->d_type != DT_DIR)
  83.         {
  84.             filesList = (char**)realloc(filesList, sizeof(char*) * (i + 1));
  85.             if (filesList == NULL)
  86.             {
  87.                 printf("Unsuccessful realloc!\n");
  88.                 exit(1);
  89.             }
  90.             filesList[i] = (char*)malloc(sizeof(char) * (dir->d_namlen + 1));
  91.             if (filesList[i] == NULL)
  92.             {
  93.                 while (i)
  94.                 {
  95.                     i--;
  96.                     free(filesList[i]);
  97.                 }
  98.                 free(filesList);
  99.                 printf("Unsuccessful malloc!\n");
  100.                 exit(1);
  101.             }
  102.  
  103.             strcpy(filesList[i], folderDirectory);
  104.             strcat(filesList[i], "/");
  105.             strcat(filesList[i], dir->d_name);
  106.  
  107.             i++;
  108.         }
  109.     }
  110.     closedir(d);
  111.  
  112.     return i;
  113. }
  114. bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo)
  115. {
  116.     int checkData = 0, virusData = 0;
  117.     double fileSize = 0;
  118.  
  119.     FILE* checkFile = fopen(fileToCheck, "rb");
  120.     if (checkFile == NULL)
  121.     {
  122.         printf("Error opening file: %s\n", fileToCheck);
  123.  
  124.         exit(1); // Stop program
  125.     }
  126.  
  127.     FILE* virusFile = fopen(virusSign, "rb");
  128.     if (virusFile == NULL)
  129.     {
  130.         fclose(checkFile);
  131.         printf("Error opening file: %s\n", virusSign);
  132.  
  133.         exit(1); // Stop program
  134.     }
  135.  
  136.     fseek(checkFile, 0L, SEEK_END);
  137.     fileSize = ftell(checkFile); // get file size
  138.     fseek(checkFile, 0L, SEEK_SET);
  139.     // 0.2 - 20% || 0.8 - 80% || 1 = 100%
  140.     // from 0 to 0.2 -> first 20%
  141.     // from 0.8 to 1 -> last 20% (1 - 8 = 0.2 ~ 20%)
  142.     while ((checkData = fgetc(checkFile)) != EOF) {
  143.         if ((fileSize * scanFrom <= ftell(checkFile)) && (fileSize * scanTo >= ftell(checkFile))) // scan only in the given range
  144.         {
  145.             virusData = fgetc(virusFile);
  146.             if (virusData == EOF) // Check if the virus sign FULLY exists in the given file to check
  147.             {
  148.                 fclose(checkFile);
  149.                 fclose(virusFile);
  150.  
  151.                 return true;
  152.             }
  153.             else if (virusData != checkData) // If the virus data doesn't equals to the given file data, "reset" the data "counter"
  154.             {
  155.                 fseek(virusFile, 0, SEEK_SET); // start over the virus data
  156.             }
  157.         }
  158.     }
  159.  
  160.     fclose(checkFile);
  161.     fclose(virusFile);
  162.  
  163.     return false;
  164. }
  165. void normalScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum)
  166. {
  167.     int i = 0;
  168.  
  169.     strcat(logBuilder, "Reuslts:\n");
  170.  
  171.     printf("Scanning:\n");
  172.     for (i = 0; i < filesNum; i++)
  173.     {
  174.         strcat(logBuilder, filesList[i]);
  175.         if (isInfected(filesList[i], virusSign, 0, 1))
  176.         {
  177.             strcat(logBuilder, " Infected!\n");
  178.             printf("%s - Infected!\n", filesList[i]);
  179.         }
  180.         else
  181.         {
  182.             strcat(logBuilder, " Clean\n");
  183.             printf("%s - Clean\n", filesList[i]);
  184.         }
  185.     }
  186.  
  187. }
  188. void quickScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum)
  189. {
  190.     int i = 0;
  191.  
  192.     printf("Scanning:\n");
  193.     for (i = 0; i < filesNum; i++)
  194.     {
  195.         printf("%s", filesList[i]);
  196.         if (isInfected(filesList[i], virusSign, 0, 0.2))
  197.         {
  198.             printf("%s - Infected! (first 20%%)\n", filesList[i]);
  199.         }
  200.         else if (isInfected(filesList[i], virusSign, 0.8, 1))
  201.         {
  202.             printf("%s - Infected! (last 20%%)\n", filesList[i]);
  203.         }
  204.         else if (isInfected(filesList[i], virusSign, 0, 1))
  205.         {
  206.             printf("%s - Infected!\n", filesList[i]);
  207.         }
  208.         else
  209.         {
  210.             printf("%s - Clean\n", filesList[i]);
  211.         }
  212.     }
  213. }
  214. void printWelcome(char** argv)
  215. {
  216.     printf("Welcome to my Virus Scan!\n\n");
  217.  
  218.     printf("Folder to scan: %s\n"
  219.            "Virus signature: %s\n\n", argv[1], argv[2]);
  220.  
  221.     strcat(logBuilder, "Anti-virus began! Welcome!\n\n");
  222.     strcat(logBuilder, "Folder to scan:\n");
  223.     strcat(logBuilder, argv[1]);
  224.     strcat(logBuilder, "\nVirus signature:\n");
  225.     strcat(logBuilder, argv[2]);
  226.     strcat(logBuilder, "\n\n");
  227. }
  228. void chooseAndScan(char** argv)
  229. {
  230.     char** filesList = (char**)malloc(0);
  231.     int filesNum = getFiles(argv[1], filesList);
  232.  
  233.     printf("Press 0 for a normal scan or any other key for quick scan: ");
  234.     scanf("%d", &scanType);
  235.     getchar();
  236.  
  237.  
  238.     strcat(logBuilder, "Scanning option:\n");
  239.     printf("Scanning began...\n"
  240.         "This process may take several minutes...\n\n");
  241.  
  242.     switch (scanType)
  243.     {
  244.         case normalScanType:
  245.         {
  246.             strcat(logBuilder, "Normal Scan\n\n");
  247.             normalScan(argv[1], argv[2], filesList, filesNum);
  248.             break;
  249.         }
  250.         default: // quickScanType
  251.         {
  252.             strcat(logBuilder, "Quick Scan\n\n");
  253.             quickScan(argv[1], argv[2], filesList, filesNum);
  254.             break;
  255.         }
  256.     }
  257.     printf("Scan Completed.\n");
  258. }
  259. void writeLog()
  260. {
  261.     FILE* fileLog = NULL;
  262.  
  263.     switch (scanType)
  264.     {
  265.         case normalScanType:
  266.             fileLog = fopen("LogExample.txt", "w+");
  267.             break;
  268.         default:
  269.             fileLog = fopen("LogExampleQuick.txt", "w+");
  270.             break;
  271.     }
  272.     if (fileLog == NULL)
  273.     {
  274.         printf("Error to open the file\n");
  275.         exit(1);
  276.     }
  277.     fputs(logBuilder, fileLog);
  278.  
  279.     fclose(fileLog);
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement