Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Security.Cryptography;
- using System.Data;
- using MySql.Data.MySqlClient;
- //https://virusshare.com/ website that contains md5 hash codes for most viruses/adware/malware
- namespace AntiVirusFinal
- {
- class Program
- {
- static void Main(string[] args)
- {
- int i = 1, x = 1; //initialize integer variables
- List<string> infectedFiles = new List<string>(); //dynamic list that holds infected files
- var md5 = MD5.Create(); //create md5 object
- var directory = Directory.GetFiles(@"D:\School\Spring 2017\ECE 426\AntiVirusExampleTests", "*"); //get directory to be scanned
- string connectionString = "datasource=localhost;port=3306;username=root;password=ece426spring2017"; //connection string to database
- string getStringCmd = "SELECT hashValuescol FROM md5hash.hashValues"; //query call from the database
- MySqlConnection connection = new MySqlConnection(connectionString); //set up connection
- MySqlDataAdapter da = new MySqlDataAdapter(getStringCmd, connection); //query command for the database table
- DataSet ds = new DataSet(); //create new dataset object
- da.Fill(ds);//fill dataset
- connection.Open(); //open database
- Console.WriteLine("The files in the directory are: ");
- foreach (string s in directory)
- {
- using (var stream = File.OpenRead(s))
- {
- string hashResult = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLower(); //convert file to md5 hash
- Console.WriteLine("");
- Console.WriteLine("File " + i + " " + s);
- Console.WriteLine("MD5 Hash Value: " + hashResult);
- foreach (DataRow row in ds.Tables[0].Rows) //get column entries from the database
- {
- if (string.Compare(row["hashValuescol"].ToString(), hashResult) == 0) //compare database md5 hash to the file hash. If it matches, it has a virus.
- {
- Console.WriteLine("Result = INFECTED!");
- infectedFiles.Add(s);
- break;
- }
- else if (x == ds.Tables[0].Rows.Count && string.Compare(row["hashValuescol"].ToString(), hashResult) != 0) //if no match = no virus
- {
- Console.WriteLine("Result = CLEAN!");
- x = 0;
- }
- x++;
- }
- i++;
- }
- }
- connection.Close(); //close database connection
- Console.WriteLine("");
- Console.WriteLine("The infected files are: ");
- string[] arrayInfectedFiles = infectedFiles.ToArray(); //convert infected list to an array
- for(int a = 0; a < arrayInfectedFiles.Length; a++) //output infected files
- {
- Console.WriteLine(arrayInfectedFiles[a]);
- }
- Console.WriteLine("");
- Console.Write("Would you like to delete the infected files (Y/N)?: ");
- string answer = Console.ReadLine();
- if (answer == "Y" || answer == "y")
- {
- for (int b = 0; b < arrayInfectedFiles.Length; b++) //delete infected files int the array
- {
- File.Delete(arrayInfectedFiles[b]);
- }
- Console.WriteLine("The infected files has been successfuly deleted!");
- }
- else
- Console.WriteLine("You chose not to delete the infected files. Programming is exitting! ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement