Advertisement
Yonka2019

afafa.c

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