Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- #define FALSE (0)
- #define TRUE !FALSE
- #define NAME_LEN (64)
- #define LOG_NAME "AntiVirusLog.txt"
- int getFileSize(FILE *file);
- char *getText(FILE *file, size_t size);
- void checkAllocation(void *p);
- char isInBin(void *data, size_t len, void *subdata, size_t sublen);
- int main(int argc, const char *argv[]) {
- FILE *virusSign = NULL, *scanFile = NULL,
- *logFile = NULL;
- int signSize = 0, scanFileSize = 0, chunkSize = 0,
- isNormalFind = 0, isFTwentFound = 0, isLTwentFound = 0;
- size_t curFileSize = 0, virusSignSize = 0;
- char curFileName[NAME_LEN] = { 0 }, logFileName[NAME_LEN] = { 0 }, choice = 0;
- struct dirent *ep;
- DIR *dp = opendir(argv[1]);
- /* LOG AND PRINT STUFF */
- // PRINT
- printf("Welcome to my Virus Scan!\n\n"
- "Folder to scan: %s\n"
- "Virus sinature: %s\n\n"
- "Press 0 for a normal scan or any key for a quick scan: ",
- argv[1], argv[2]);
- scanf("%d", &choice);
- puts("Scanning began...\nThis process may take several minutes...\n\nScanning:");
- // LOG
- sprintf(logFileName, "%s/%s", argv[1], LOG_NAME);
- logFile = fopen(logFileName, "w");
- fprintf(logFile, "Anti-virus began! Welcome!\n\n"
- "Folder to scan:\n%s\nVirus signature:\n%s\n\n"
- "Scanning option:\n%s Scan\n\nResults:\n",
- argv[1], argv[2], !choice ? "Normal" : "Quick");
- /* END OF LOG AND PRINT */
- // Getting the virus sign
- virusSign = fopen(argv[2], "rb");
- virusSignSize = getFileSize(virusSign);
- char *sign = getText(virusSign, (int)virusSignSize);
- if (dp) {
- // Move over every file in the folder
- while (ep = readdir(dp)) {
- // Checking if it's a file
- if (ep->d_type == DT_DIR || !strcmp(ep->d_name, LOG_NAME)) {
- continue;
- }
- sprintf(curFileName, "%s/%s", argv[1], ep->d_name);
- scanFile = fopen(curFileName, "rb");
- curFileSize = getFileSize(scanFile);
- // Saving some CPU cycles ;)
- if (curFileSize < virusSignSize) {
- fprintf(logFile, "%s Not Infected\n", curFileName);
- continue;
- }
- char *curFileText = getText(scanFile, curFileSize);
- if (choice) {
- chunkSize = curFileSize / 5; // Getting the size of the %20
- // Saving some CPU cycles
- if (chunkSize > virusSignSize) {
- isFTwentFound = isInBin(curFileText, chunkSize, sign, virusSignSize);
- isLTwentFound = isInBin(curFileText + (chunkSize * 4), curFileSize - (chunkSize * 4), sign,
- virusSignSize);
- }
- else {
- isFTwentFound = FALSE;
- isLTwentFound = FALSE;
- }
- }
- if (isLTwentFound) {
- fprintf(logFile, "%s %s\n", curFileName, "Infected! (last 20%)");
- }
- else if (isFTwentFound) {
- fprintf(logFile, "%s %s\n", curFileName, "Infected! (first 20%)");
- }
- else if (isInBin(curFileText, curFileSize, sign, virusSignSize)) {
- fprintf(logFile, "%s Infected!\n", curFileName);
- }
- else {
- fprintf(logFile, "%s Not Infected\n", curFileName);
- }
- free(curFileText);
- puts(curFileName);
- }
- free(sign);
- (void)closedir(dp);
- }
- else {
- perror("Couldn't open the directory");
- }
- printf("Scan Completed.\nSee log path for results: %s\nGoodbye", logFileName);
- return 0;
- }
- /*
- * Function which get data and subdata and check if the string contains the substring,
- * the punch is that it is working with binary char-chunck
- *
- * For the protocol, GNU also has function which do the same (memmem)
- *
- * @param the data, it's len, the subdata and it's len
- * @return 1 - it contains, 0 - else
- */
- char isInBin(void *data, size_t len, void *subdata, size_t sublen) {
- // Running from the end of the file through the start minus the size of the subdata, so by the end - everything got checked
- for (len -= sublen; len >= 1; --len)
- if (!memcmp(data + len, subdata,
- sublen)) // Checking if the each chuck, one by one (chunk size = virus sign size)
- return TRUE;
- // For the worst case, when it's at the end (or start from another perspective)
- return !memcmp(data, subdata, sublen);
- }
- /*
- * returns given file size
- * @param file
- * @return files size
- */
- int getFileSize(FILE *file) {
- int size = 0;
- fseek(file, 0, SEEK_END);
- size = (int)ftell(file);
- rewind(file);
- return size;
- }
- /*
- * gets text from given file
- * @param file and it is size
- * @return the file s text
- */
- char *getText(FILE *file, size_t size) {
- char *tmpBuff = NULL;
- size_t res = 0;
- tmpBuff = (char *)malloc(sizeof(char) * size);
- checkAllocation(tmpBuff);
- res = fread(tmpBuff, 1, size, file);
- if (res != size) {
- perror("Can't read file!");
- exit(2);
- }
- return tmpBuff;
- }
- /*
- * checks for wrong allocation
- * @param pointer to allocated addr
- * @return none
- */
- void checkAllocation(void *p) {
- if (!p) {
- perror("Allocation error!");
- exit(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement