Advertisement
Guest User

Untitled

a guest
May 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Security.Cryptography;
  8. using System.Data;
  9. using MySql.Data.MySqlClient;
  10. //https://virusshare.com/ website that contains md5 hash codes for most viruses/adware/malware
  11. namespace AntiVirusFinal
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. int i = 1, x = 1; //initialize integer variables
  18. List<string> infectedFiles = new List<string>(); //dynamic list that holds infected files
  19. var md5 = MD5.Create(); //create md5 object
  20.  
  21. var directory = Directory.GetFiles(@"D:\School\Spring 2017\ECE 426\AntiVirusExampleTests", "*"); //get directory to be scanned
  22. string connectionString = "datasource=localhost;port=3306;username=root;password=ece426spring2017"; //connection string to database
  23. string getStringCmd = "SELECT hashValuescol FROM md5hash.hashValues"; //query call from the database
  24. MySqlConnection connection = new MySqlConnection(connectionString); //set up connection
  25. MySqlDataAdapter da = new MySqlDataAdapter(getStringCmd, connection); //query command for the database table
  26. DataSet ds = new DataSet(); //create new dataset object
  27. da.Fill(ds);//fill dataset
  28. connection.Open(); //open database
  29.  
  30. Console.WriteLine("The files in the directory are: ");
  31. foreach (string s in directory)
  32. {
  33. using (var stream = File.OpenRead(s))
  34. {
  35. string hashResult = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLower(); //convert file to md5 hash
  36. Console.WriteLine("");
  37. Console.WriteLine("File " + i + " " + s);
  38. Console.WriteLine("MD5 Hash Value: " + hashResult);
  39.  
  40. foreach (DataRow row in ds.Tables[0].Rows) //get column entries from the database
  41. {
  42. if (string.Compare(row["hashValuescol"].ToString(), hashResult) == 0) //compare database md5 hash to the file hash. If it matches, it has a virus.
  43. {
  44. Console.WriteLine("Result = INFECTED!");
  45. infectedFiles.Add(s);
  46. break;
  47.  
  48. }
  49. else if (x == ds.Tables[0].Rows.Count && string.Compare(row["hashValuescol"].ToString(), hashResult) != 0) //if no match = no virus
  50. {
  51. Console.WriteLine("Result = CLEAN!");
  52. x = 0;
  53. }
  54. x++;
  55. }
  56. i++;
  57. }
  58. }
  59. connection.Close(); //close database connection
  60.  
  61. Console.WriteLine("");
  62. Console.WriteLine("The infected files are: ");
  63. string[] arrayInfectedFiles = infectedFiles.ToArray(); //convert infected list to an array
  64.  
  65. for(int a = 0; a < arrayInfectedFiles.Length; a++) //output infected files
  66. {
  67. Console.WriteLine(arrayInfectedFiles[a]);
  68. }
  69.  
  70. Console.WriteLine("");
  71. Console.Write("Would you like to delete the infected files (Y/N)?: ");
  72. string answer = Console.ReadLine();
  73.  
  74. if (answer == "Y" || answer == "y")
  75. {
  76. for (int b = 0; b < arrayInfectedFiles.Length; b++) //delete infected files int the array
  77. {
  78. File.Delete(arrayInfectedFiles[b]);
  79. }
  80. Console.WriteLine("The infected files has been successfuly deleted!");
  81. }
  82. else
  83. Console.WriteLine("You chose not to delete the infected files. Programming is exitting! ");
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement