Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security.Cryptography;
- //using MySql.Data.MySqlClient;
- using System.Data;
- using System.Text.RegularExpressions;
- namespace AntiVirus
- {
- class Program
- {
- static void Main(string[] args)
- {
- //string connectionString = "datasource=localhost;port=3306;username=root;password=ece426";
- //MySqlConnection connection = new MySqlConnection(connectionString);
- //MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
- //myDataAdapter.SelectCommand = new MySqlCommand("SELECT * FROM virusdb.sha256;", connection);
- //MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
- DataSet ds = new DataSet();
- //myDataAdapter.Fill(ds);
- //connection.Open();
- Console.WriteLine("Connected to mySQL.");
- foreach (var file in Directory.EnumerateFiles(@"C:\\Users\Nick\\Desktop\\ECE426L\\AntiVirus\\VirusTextFiles\\"))
- {
- int i = 0;
- FileStream stream = File.OpenRead(file);
- string md5result = GetMd5Hash(stream);
- string sha256result = GetSha256Hash(stream,file);
- string answer = " ";
- foreach (DataRow row in ds.Tables[0].Rows)
- {
- //string x = (row["hash"].ToString());
- Regex regex = new Regex(@row["hash"].ToString());
- Match match1 = regex.Match(md5result);
- Match match2 = regex.Match(sha256result);
- if(match1.Success || match2.Success)
- {
- Console.WriteLine("There is a virus.");
- }
- }
- if (answer == "yes")
- {
- stream.Close();
- File.Delete(file);
- }
- Console.WriteLine("Scanning completed.");
- Console.WriteLine();
- i++;
- }
- //connection.Close();
- Console.ReadLine();
- //StreamReader ReadFile = new StreamReader(Value);
- }
- static string GetSha256Hash(FileStream filename, string file)
- {
- var sha = new SHA256Managed();
- byte[] SHAchecksum = sha.ComputeHash(filename);
- Console.WriteLine("Scanning " + Path.GetFileName(file) + "...");
- byte[] content = File.ReadAllBytes(file);
- StringBuilder sb = new StringBuilder();
- foreach (byte b in content)
- sb.Append(b.ToString("X2"));
- string hexString = sb.ToString();
- //Console.WriteLine("Displaying the hex values of the file..");
- //Console.WriteLine(hexString);
- Console.WriteLine("Hashing the string with SHA-256..");
- Console.WriteLine(BitConverter.ToString(SHAchecksum).Replace("-", String.Empty));
- return BitConverter.ToString(SHAchecksum).Replace("-", String.Empty);
- }
- static string GetMd5Hash(FileStream filename)
- {
- using (var md5 = MD5.Create())
- {
- var hash = md5.ComputeHash(filename);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement