fork download
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class MalwareProtectionSystem
  8. {
  9. // Collection of known malware signatures
  10. private static HashSet<string> KnownMalwareSignatures = new HashSet<string>
  11. {
  12. "7a38a4e345a5f9f5e4f3a5b2c1d9e8f7", // Example malware signature
  13. "b6c3d8a2f1e9d4c7b5a3f6e2d8c1b4a7"
  14. };
  15.  
  16. // Scan file for potential malware
  17. public static bool ScanFileForMalware(string filePath)
  18. {
  19. try
  20. {
  21. // Calculate file hash
  22. string fileHash = CalculateFileHash(filePath);
  23.  
  24. // Check against known malware signatures
  25. if (KnownMalwareSignatures.Contains(fileHash))
  26. {
  27. Console.WriteLine($"Malware detected in file: {filePath}");
  28. return false;
  29. }
  30.  
  31. // Check suspicious file extensions
  32. string[] suspiciousExtensions = { ".exe", ".bat", ".cmd", ".vbs" };
  33. string fileExtension = Path.GetExtension(filePath).ToLower();
  34.  
  35. if (suspiciousExtensions.Contains(fileExtension))
  36. {
  37. Console.WriteLine($"Suspicious file detected: {filePath}");
  38. return false;
  39. }
  40.  
  41. Console.WriteLine($"File is safe: {filePath}");
  42. return true;
  43. }
  44. catch (Exception ex)
  45. {
  46. Console.WriteLine($"Error scanning file: {ex.Message}");
  47. return false;
  48. }
  49. }
  50.  
  51. // Calculate file hash using SHA256
  52. private static string CalculateFileHash(string filePath)
  53. {
  54. using (var sha256 = SHA256.Create())
  55. {
  56. using (var stream = File.OpenRead(filePath))
  57. {
  58. var hash = sha256.ComputeHash(stream);
  59. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  60. }
  61. }
  62. }
  63.  
  64. // Monitor suspicious processes
  65. public static void MonitorSuspiciousProcesses()
  66. {
  67. // List of suspicious process names
  68. string[] suspiciousProcesses =
  69. {
  70. "keylogger",
  71. "remoteshell",
  72. "networksniff"
  73. };
  74.  
  75. var runningProcesses = System.Diagnostics.Process.GetProcesses();
  76.  
  77. foreach (var process in runningProcesses)
  78. {
  79. if (suspiciousProcesses.Any(p => process.ProcessName.ToLower().Contains(p)))
  80. {
  81. Console.WriteLine($"Suspicious process detected: {process.ProcessName}");
  82. }
  83. }
  84. }
  85.  
  86. // Main method for demonstration
  87. public static void Main(string[] args)
  88. {
  89. Console.WriteLine("Malware Protection System Started");
  90.  
  91. // For online compiler compatibility, use a sample file path
  92. string sampleFilePath = "sample.txt";
  93.  
  94. // Create a sample file for testing
  95. File.WriteAllText(sampleFilePath, "This is a test file");
  96.  
  97. // Scan the sample file
  98. ScanFileForMalware(sampleFilePath);
  99.  
  100. // Monitor suspicious processes
  101. MonitorSuspiciousProcesses();
  102. }
  103. }
Success #stdin #stdout 0.1s 39960KB
stdin
Standard input is empty
stdout
Malware Protection System Started
File is safe: sample.txt