Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Security.Cryptography;
  8. //using MySql.Data.MySqlClient;
  9. using System.Data;
  10. using System.Text.RegularExpressions;
  11.  
  12. namespace AntiVirus
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. //string connectionString = "datasource=localhost;port=3306;username=root;password=ece426";
  19. //MySqlConnection connection = new MySqlConnection(connectionString);
  20. //MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
  21. //myDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM virusdb.sha256;", connection);
  22. //MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
  23. DataSet ds = new DataSet();
  24. //myDataAdapter.Fill(ds);
  25. //connection.Open();
  26. Console.WriteLine("Connected to mySQL.");
  27. foreach (var file in Directory.EnumerateFiles(@"C:\\Users\Nick\\Desktop\\ECE426L\\AntiVirus\\VirusTextFiles\\"))
  28. {
  29.  
  30. int i = 0;
  31. FileStream stream = File.OpenRead(file);
  32. string md5result = GetMd5Hash(stream);
  33. string sha256result = GetSha256Hash(stream,file);
  34.  
  35. string answer = " ";
  36. foreach (DataRow row in ds.Tables[0].Rows)
  37. {
  38. //string x = (row["hash"].ToString());
  39. Regex regex = new Regex(@row["hash"].ToString());
  40. Match match1 = regex.Match(md5result);
  41. Match match2 = regex.Match(sha256result);
  42. if(match1.Success || match2.Success)
  43. {
  44. Console.WriteLine("There is a virus.");
  45. }
  46. }
  47. if (answer == "yes")
  48. {
  49. stream.Close();
  50. File.Delete(file);
  51. }
  52. Console.WriteLine("Scanning completed.");
  53. Console.WriteLine();
  54. i++;
  55. }
  56. //connection.Close();
  57. Console.ReadLine();
  58. //StreamReader ReadFile = new StreamReader(Value);
  59. }
  60. static string GetSha256Hash(FileStream filename, string file)
  61. {
  62. var sha = new SHA256Managed();
  63. byte[] SHAchecksum = sha.ComputeHash(filename);
  64. Console.WriteLine("Scanning " + Path.GetFileName(file) + "...");
  65. byte[] content = File.ReadAllBytes(file);
  66. StringBuilder sb = new StringBuilder();
  67. foreach (byte b in content)
  68. sb.Append(b.ToString("X2"));
  69. string hexString = sb.ToString();
  70. //Console.WriteLine("Displaying the hex values of the file..");
  71. //Console.WriteLine(hexString);
  72. Console.WriteLine("Hashing the string with SHA-256..");
  73. Console.WriteLine(BitConverter.ToString(SHAchecksum).Replace("-", String.Empty));
  74. return BitConverter.ToString(SHAchecksum).Replace("-", String.Empty);
  75. }
  76. static string GetMd5Hash(FileStream filename)
  77. {
  78. using (var md5 = MD5.Create())
  79. {
  80. var hash = md5.ComputeHash(filename);
  81. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  82. }
  83. }
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement