Untitled
raw download clone
TEXT
views 36
,
size 5194 b
//#include "antiVirusProgram.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string>
#include <vector>
#include <iostream>
#include <string.h>
#include <fstream>
#include <map>

void CompareFileWithDatabase(std::vector<std::string> files, std::map<std::string, std::vector<unsigned int>> databaseMap)
{
    char data[10];
    unsigned int character;
    int counter = 0;


    for(auto file: files)
    {
        std::ifstream fileToOpen(file, std::ios::in|std::ios::binary);
        if(fileToOpen.is_open())
        {        
            fileToOpen.read(data, 10);

            for(auto iter: databaseMap)
            {
                counter = 0;
                for(size_t i = 0; i < 5; i++)
                {
                    character = static_cast<unsigned int>(data[i]);
                    //std::cout << character << " = " << data[i] << std::endl;
                    if (character == iter.second[i])
                    {
                        counter++;
                    }                    
                }

                if (counter == 5)
                {
                    std::ofstream myFile;
                    myFile.open("dv1620.log");
                    std::cout << "Infected files pathway: " << file << " Virus name: " << iter.first << "\n";
                    myFile.close();
                }
                
            }
            fileToOpen.close();
        }
        else
        {
            std::cout << "Unable to open file." << std::endl;
        }   
    }
}

void FindFiles(const std::string& directory, std::vector<std::string>& files, std::map<std::string, std::vector<unsigned int>>& databaseMap)
{
    DIR *dirp = opendir(directory.c_str());
    struct dirent * dp;
    struct stat sb; //Kan skippa den här om man använder dp->d_type för att kolla om det är en fil eller directory

    std::vector<std::string> fileNames;

    while ((dp = readdir(dirp)) != NULL)
    {
        std::string pathname = directory + "/" + dp->d_name;

        if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
        {
            continue;
        }        

        if (stat(pathname.c_str(), &sb) == 0 && S_ISREG(sb.st_mode))
        {
            files.push_back(directory + "/" + dp->d_name);
            fileNames.push_back(dp->d_name);
            //CompareFileWithDatabase(files, databaseMap);
        }
        else
        {
            FindFiles(pathname, files, databaseMap);
        }
    }
    closedir(dirp);
}

void GetVirusDatabase(std::string signaturesFile, std::map<std::string, std::vector<unsigned int>>& databaseMap)
{
    std::string line = "";
    std::fstream databaseFile(signaturesFile, std::fstream::in);

    if (databaseFile.is_open())
    {
        while (getline(databaseFile, line))
        {
            std::size_t pos = line.find('=');

            std::string hexSubStr = line.substr(pos + 1);
            const unsigned int bytesCount = hexSubStr.length()/2;
            std::vector<unsigned int> bytesVec(bytesCount);

            for (unsigned int a = 0, b = 0; a < hexSubStr.length(); a+=2, b++)
            {
                std::string sub = hexSubStr.substr(a, 2);
                unsigned int byte = std::stoul(sub, nullptr, 16);
                bytesVec[b] = byte;
            }
            
            databaseMap.insert({line.substr(0, pos), bytesVec});
        }

        databaseFile.close();
    }
    else
    {
        std::cout << "Couldn't open file" << std::endl;
    }    
}

int main(int argc, char* argv[])
{
    //AntiVirusProgram av;

    std::string dir;
    std::string signaturesFile;

    std::vector<std::string> files;
    std::map<std::string, std::vector<unsigned int>> databaseMap;

    if (argc != 5)
    {
        if (argc > 5)
        {
            std::cerr << "Error: to many arguments provided!" << std::endl;
            return 1;
        }
        else
        {
            std::cerr << "Error: to few arguments provided!" << std::endl;
            return 1;
        }
    }
    
    for (int i = 1; i < argc; ++i)
    {
        if (i + 1 < argc)
        {
            if (std::string(argv[i]) == "--directory" || std::string(argv[i]) == "--d")
            {
                dir = argv[++i];
            }
            else if (std::string(argv[i]) == "--signatures" || std::string(argv[i]) == "--s")
            {
                signaturesFile = argv[++i];
            }
        }            
    }    

    GetVirusDatabase(signaturesFile, databaseMap);

    FindFiles(dir, files, databaseMap);

    for (size_t i = 0; i < files.size(); i++)
    {
        std::cout << files[i] << std::endl;
    }
    

    CompareFileWithDatabase(files, databaseMap);

    // for(auto test: databaseMap)
    // {        
    //     std::cout << test.first << "=";
    //     for (auto test1: test.second)
    //     {
    //         std::cout << test1;
    //     }
    //     std::cout << std::endl;
        
    // }

    return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.