Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 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=masaker1";
  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. Regex regex = new Regex(@row["md5hash"].ToString());
  39. Match match1 = regex.Match(md5result);
  40. if(match1.Success)
  41. {
  42. Console.WriteLine("There is a virus.");
  43. }
  44. }
  45. foreach (DataRow row in ds.Tables[1].Rows)
  46. {
  47. Regex regex = new Regex(@row["sha256hash"].ToString());
  48. Match match2 = regex.Match(sha256result);
  49. if (match2.Success)
  50. {
  51. Console.WriteLine("There is a virus.");
  52. }
  53. }
  54. if (answer == "yes")
  55. {
  56. stream.Close();
  57. File.Delete(file);
  58. }
  59. Console.WriteLine("Scanning completed.");
  60. Console.WriteLine();
  61. i++;
  62. }
  63. //connection.Close();
  64. Console.ReadLine();
  65. //StreamReader ReadFile = new StreamReader(Value);
  66. }
  67. static string GetSha256Hash(FileStream filename, string file)
  68. {
  69. var sha = new SHA256Managed();
  70. byte[] SHAchecksum = sha.ComputeHash(filename);
  71. Console.WriteLine("Scanning " + Path.GetFileName(file) + "...");
  72. byte[] content = File.ReadAllBytes(file);
  73. StringBuilder sb = new StringBuilder();
  74. foreach (byte b in content)
  75. sb.Append(b.ToString("X2"));
  76. string hexString = sb.ToString();
  77. //Console.WriteLine("Displaying the hex values of the file..");
  78. //Console.WriteLine(hexString);
  79. Console.WriteLine("Hashing the string with SHA-256..");
  80. Console.WriteLine(BitConverter.ToString(SHAchecksum).Replace("-", String.Empty));
  81. return BitConverter.ToString(SHAchecksum).Replace("-", String.Empty);
  82. }
  83. static string GetMd5Hash(FileStream filename)
  84. {
  85. using (var md5 = MD5.Create())
  86. {
  87. var hash = md5.ComputeHash(filename);
  88. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  89. }
  90. }
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement