Yonka2019

Antivirus-Project.c

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