using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace Antimalware
{
public partial class Form1 : Form
{
private readonly HashSet<string> md5Signatures;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public string GetMD5FromFile(string filePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (IsFileInfected(textBox1.Text))
{
IblStatus.Text = "Infected!";
IblStatus.ForeColor = Color.Red;
}
else
{
IblStatus.Text = "Clean!";
IblStatus.ForeColor = Color.Green;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void browseToolStripMenuItem_Click_1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files|*.*|Text files|*.txt|Executable files|*.exe";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = "Computing...";
IblStatus.Text = "";
IblStatus.ForeColor = Color.Black;
button1.Enabled = false;
textBox1.Enabled = false;
// Start a background thread to compute the MD5 signature
var bgThread = new System.Threading.Thread(() =>
{
string md5Signature = GetMD5FromFile(ofd.FileName);
// Update the UI on the main thread
BeginInvoke(new Action(() =>
{
textBox1.Text = md5Signature;
button1.Enabled = true;
textBox1.Enabled = true;
}));
});
bgThread.Start();
}
}
private void browseToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void IblStatus_Click(object sender, EventArgs e)
{
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void browseTolStripMenuItem_Click1(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "All files|*.*|Text files|*.txt|Executable files|*.exe";
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = "Computing...";
IblStatus.Text = "";
IblStatus.ForeColor = Color.Black;
button1.Enabled = false;
textBox1.Enabled = false;
// Start a background thread to compute the MD5 signature
var bgThread = new System.Threading.Thread(() =>
{
string md5Signature = GetMD5FromFile(ofd.FileName);
// Update the UI on the main thread
BeginInvoke(new Action(() =>
{
textBox1.Text = md5Signature;
button1.Enabled = true;
textBox1.Enabled = true;
}));
});
bgThread.Start();
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
// Show the FolderBrowserDialog to the user and get the selected folder path
using (var folderBrowserDialog = new FolderBrowserDialog())
{
if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
{
return; // User canceled the dialog
}
// Get all files in the selected folder and its subfolders
var files = new List<string>();
try
{
files = Directory.GetFiles(folderBrowserDialog.SelectedPath, "*.*", SearchOption.AllDirectories)
.Where(f =>
{
try
{
// Remove this line since the FileInfo object is not used
//var fileInfo = new FileInfo(f);
return true;
}
catch (UnauthorizedAccessException ex)
{
// Log the exception message
Console.WriteLine($"Access to file/directory '{f}' was denied: {ex.Message}");
return false;
}
})
.ToList();
}
catch (ArgumentException ex)
{
// Log the exception message
Console.WriteLine($"Invalid path: {ex.Message}");
return;
}
// Calculate the total number of files
int totalFiles = files.Count;
// Initialize the progress bar
progressBar1.Minimum = 0;
progressBar1.Maximum = totalFiles;
progressBar1.Value = 0;
// Scan each file and check if it's infected
int infectedFiles = 0;
foreach (var file in files)
{
// Update the progress bar
progressBar1.Value++;
// Get the MD5 hash of the file
string md5 = GetMD5FromFile(file);
// Check if the MD5 hash is in the list of known infected files
bool isInfected = IsFileInfected(md5);
// If the file is infected, add it to the list view
if (isInfected)
{
infectedFiles++;
string[] row = { file, md5 };
listView1.Items.Add(new ListViewItem(row));
}
}
MessageBox.Show($"Scanning complete. {infectedFiles} infected files found out of {totalFiles} files scanned.");
}
}
private bool IsFileInfected(string md5)
{
using (var fileStream = new FileStream("MD5base.txt", FileMode.Open, FileAccess.Read))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
if (line == md5)
{
return true;
}
}
}
return false;
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void fileToolStripMenuItem_Click_1(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void label2_Click_1(object sender, EventArgs e)
{
}
}
}