Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************
- * Class: MAGSHIMIM C2 *
- * Week: 8 *
- * Name: ron sason *
- * Credits: *
- **********************************/
- // https://pastebin.com/iQ6s87fH
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "dirent.h"
- typedef struct dirent dirent;
- #define FOLDER_ADDRES_IN_ARGV 1
- #define BACK_SLASH_FOR_ADDRES 47
- #define INDEX_FIRST_ADRESS 2
- #define INDEX_OF_ARGV_ANTI_VIRUS_FILE 2
- #define INDEX_OF_ARGV_FOLDER 2
- #define ADD_FOR_MAKE_STR 2
- #define SIZE_OF_BLACK_SLASH_STR 2
- #define TWENTY_PRECENT 20
- #define ONEHUNDERED_PRECENT 100
- #define FIRST_QUICK_SCAN 1
- #define LAST_QUICK_SCAN 2 // The function see this number and chose what to do
- #define NORMAL_SCAN 0
- #define MAKE_LAST_80_PRECENT 4
- FILE* fileBinaryOpen(FILE* pFile, char** argv);
- char* readBinaryFile(FILE* pFile, long* size);
- char** inputFolderInArr(char** argv, int* addresSize);
- FILE* myOpenFile(char* addres);
- int checkSingnInFile(char* bufferOfSing, char* addresOfFile, long antiVirusSize, int precentToCheck);
- int outTwentyPercent(int num, int precentToCheck);
- void freeMatriza(char** arrOfStr, int size);
- int main(int argc, char** argv)
- {
- int check = 0, addresSize = 0, i = 0, choceOfUser = 0, precentToCheck = 0, flag = 1;
- long sizeBuferAntiVirus = 0;
- char** addresInFolder = 0;
- char* bufferOfAntiVirus = 0;
- char* folderToInputData = 0;
- char antiLog[] = "AntiVirusLog.txt";
- FILE* signOfAntiVirus = 0;
- FILE* logFile = 0;
- folderToInputData = (char*)malloc(sizeof(char) * (strlen(antiLog) + strlen(argv[INDEX_OF_ARGV_FOLDER])));
- strcpy(folderToInputData, argv[1]);
- strcat(folderToInputData, "\\");
- strcat(folderToInputData, antiLog);
- logFile = fopen(folderToInputData, "w");
- printf("Welcome my virus scan!\n\n");
- fprintf(logFile, "Welcome my virus scan!\n");
- printf("Folder to scan: %s\n", argv[INDEX_OF_ARGV_FOLDER]);
- printf("Virus signature: %s\n", argv[INDEX_OF_ARGV_ANTI_VIRUS_FILE]);
- fprintf(logFile, "Folder to scan:\n %s\n", argv[INDEX_OF_ARGV_FOLDER]);
- fprintf(logFile, "Virus signature:\n %s\n", argv[INDEX_OF_ARGV_ANTI_VIRUS_FILE]);
- signOfAntiVirus = fileBinaryOpen(signOfAntiVirus, argv);
- bufferOfAntiVirus = readBinaryFile(signOfAntiVirus, &sizeBuferAntiVirus);
- addresInFolder = inputFolderInArr(argv, &addresSize);
- printf("Pressc 0 for normal scan or any other key for a quick scan: ");
- scanf("%d", &choceOfUser);
- getchar();
- if (choceOfUser) // for input data to file
- {
- fprintf(logFile, "Scanning option:\nQuick scan\n");
- }
- else
- {
- fprintf(logFile, "Scanning option:\nNormal scan\n");
- }
- printf("Scanning began...\nThis process may take several mintes...\n\n");
- fprintf(logFile, "Result\n");
- for (i = INDEX_FIRST_ADRESS; i < addresSize; i++) // pass all the folder
- {
- if (choceOfUser) // For quick scan check only 20%
- {
- precentToCheck = FIRST_QUICK_SCAN;
- }
- while (flag)
- {
- check = checkSingnInFile(bufferOfAntiVirus, addresInFolder[i], sizeBuferAntiVirus, precentToCheck);
- if (!precentToCheck) // if normal scan to know what to print
- {
- if (check)
- {
- printf("%s - Infected!\n", addresInFolder[i]);
- fprintf(logFile, "%s - Infected!\n", addresInFolder[i]);
- flag = 0;
- }
- else
- {
- printf("%s - Clean\n", addresInFolder[i]);
- fprintf(logFile, "%s - Clean\n", addresInFolder[i]);
- flag = 0;
- }
- }
- else if (precentToCheck == FIRST_QUICK_SCAN)
- {
- if (check)
- {
- printf("%s - Infected! <First 20%%>\n", addresInFolder[i]);
- fprintf(logFile, "%s - Infected! <First 20%%>\n", addresInFolder[i]);
- flag = 0;
- }
- else
- {
- precentToCheck = LAST_QUICK_SCAN; // Make check last 20 pecent
- }
- }
- else if (precentToCheck == LAST_QUICK_SCAN)
- {
- check = checkSingnInFile(bufferOfAntiVirus, addresInFolder[i], sizeBuferAntiVirus, precentToCheck); // becuse need make check agin
- if (check)
- {
- printf("%s - Infected! <Last 20%%>\n", addresInFolder[i]);
- fprintf(logFile, "%s - Infected! <Last 20%%>\n", addresInFolder[i]);
- flag = 0;
- }
- else
- {
- precentToCheck = NORMAL_SCAN; // Go to noramla check
- }
- }
- }
- flag = 1; // make the loop making agin
- }
- printf("Scan Completed.\n");
- printf("See log path for results: %s", folderToInputData);
- freeMatriza(addresInFolder, addresSize);
- free(folderToInputData);
- free(bufferOfAntiVirus);
- fclose(logFile);
- fclose(signOfAntiVirus);
- getchar();
- return 0;
- }
- /*
- the function open the file to read in binary, if the file not found it close the plant
- input: p p file to open hem
- .
- output:POINTER OF file
- .
- */
- FILE* fileBinaryOpen(FILE* pFile, char** argv)
- {
- pFile = fopen(argv[INDEX_OF_ARGV_ANTI_VIRUS_FILE], "rb");
- if (pFile == NULL)
- {
- printf("Error opening file!\n");
- getchar();
- exit(1);
- }
- return pFile;
- }
- /*
- the function get pFile and take the binary in the file and input to buffer
- input: pFile we wont copy hem and pointer to return the size
- .
- output:pointer of buffer white the word
- .
- */
- char* readBinaryFile(FILE* pFile, long* size)
- {
- char* buffer;
- fseek(pFile, 0, SEEK_END);
- *size = ftell(pFile);
- fseek(pFile, 0, SEEK_SET);
- buffer = (char *)malloc(*size * sizeof(char));
- *buffer = 0;
- fread(buffer, 1, *size, pFile);
- return buffer;
- }
- /*
- the function get file and buffer of sing and check if the sing in the buffer file
- input: buffer of sing and file pointer to open and check
- .
- output:1 if the sing in the file 0 if not
- .
- */
- int checkSingnInFile(char* bufferOfSing, char* addresOfFile, long antiVirusSize, int precentToCheck)
- {
- int j = 0, i = 0, flag = 0, count = 0, sizeFile = 0;
- char* bufferOfFile = 0;
- FILE* pFile = 0;
- pFile = myOpenFile(addresOfFile);
- fseek(pFile, 0, SEEK_END);
- sizeFile = ftell(pFile);
- fseek(pFile, 0, SEEK_SET);
- bufferOfFile = (char*)malloc((sizeof(char) * sizeFile));
- fread(bufferOfFile, 1, sizeFile, pFile);
- if (precentToCheck == FIRST_QUICK_SCAN) // If quick scan
- {
- sizeFile = outTwentyPercent(sizeFile, TWENTY_PRECENT);
- }
- for (i = 0; antiVirusSize + i <= sizeFile && !flag; i++) // pass all the buffer
- {
- if (LAST_QUICK_SCAN == precentToCheck)
- {
- i = sizeFile - outTwentyPercent(sizeFile, TWENTY_PRECENT);
- precentToCheck = 0;
- }
- for (j = 0; j < antiVirusSize; j++) // pass on the size of the buffer
- {
- if (bufferOfFile[i + j] == bufferOfSing[j]) // if all the buffer comper then he eqal to size of sing buffer len
- {
- count++;
- }
- }
- if (count == antiVirusSize)
- {
- flag = 1;
- }
- count = 0;
- }
- fclose(pFile);
- free(bufferOfFile);
- return flag;
- }
- /*
- the function make a dinamik place to arr to input ther all the folder str
- input: the argv for addres and the size to use in ather function
- .
- output: pointer if addres arr
- .
- */
- char** inputFolderInArr(char** argv, int* addresSize)
- {
- char** addresInFolder = 0;
- int count = 0, i = 0;
- char backSlashForAddres[SIZE_OF_BLACK_SLASH_STR] = { 0 };
- backSlashForAddres[0] = BACK_SLASH_FOR_ADDRES;
- struct dirent* de;
- DIR* dr = opendir(argv[FOLDER_ADDRES_IN_ARGV]);
- if (dr == NULL) // opendir returns NULL if couldn't open directory
- {
- printf("Could not open current directory");
- return 0;
- }
- while ((de = readdir(dr)) != NULL) // count the item in folder to input to folder
- {
- count++;
- }
- addresInFolder = (char**)malloc(sizeof(char*) * count);
- closedir(dr);
- dr = opendir(argv[FOLDER_ADDRES_IN_ARGV]);
- while ((de = readdir(dr)) != NULL)
- {
- addresInFolder[i] = (char*)malloc((sizeof(char) *(strlen(argv[FOLDER_ADDRES_IN_ARGV]) + strlen(de->d_name)) + ADD_FOR_MAKE_STR));
- strcpy(addresInFolder[i], argv[FOLDER_ADDRES_IN_ARGV]);
- strcat(addresInFolder[i], backSlashForAddres); // make the addres for next function
- strcat(addresInFolder[i], de->d_name);
- i++;
- }
- *addresSize = count;
- closedir(dr);
- return addresInFolder;
- }
- /*
- the function open file to read in binary
- input: get the addtes of file to input
- .
- output: pointer of file
- .
- */
- FILE* myOpenFile(char* addres)
- {
- FILE* pFile = 0;
- pFile = fopen(addres, "rb");
- if (pFile == NULL)
- {
- printf("Error opening file!\n");
- getchar();
- exit(1);
- }
- return pFile;
- }
- /*
- the function return 20% of number he get
- input: get number to return the 20%
- .
- output: number of 20% from the number input
- .
- */
- int outTwentyPercent(int num, int precentToCheck)
- {
- num *= precentToCheck;
- num /= ONEHUNDERED_PRECENT;
- return num;
- }
- /*
- the function free matriza
- input:the matriza and the size for free
- .
- output: nane
- .
- */
- void freeMatriza(char** arrOfStr, int size)
- {
- int i = 0;
- for (i = 0; i < size; i++)
- {
- free(arrOfStr[i]);
- }
- free(arrOfStr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement