WindowsDefenderScanService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using Masuit.Tools.Files;
  7. using Masuit.Tools.Systems;
  8. namespace Masuit.Tools.Win32.AntiVirus;
  9. public class WindowsDefenderScanService
  10. {
  11. public WindowsDefenderScanService()
  12. {
  13. if (!Directory.Exists(SystemParameter.WindowsDefenderPath))
  14. {
  15. throw new PlatformNotSupportedException("Windows Defender not found");
  16. }
  17. if (!File.Exists($"{SystemParameter.WindowsDefenderPath}\\{SystemParameter.WindowsDefenderExeName}"))
  18. {
  19. throw new PlatformNotSupportedException("Windows Defender not found");
  20. }
  21. }
  22. /// <summary>
  23. /// 扫描文件流
  24. /// </summary>
  25. /// <param name="filePath"></param>
  26. public ScanResult ScanStream(Stream stream)
  27. {
  28. var temp = Path.Combine(Environment.GetEnvironmentVariable("temp"), SnowFlake.NewId);
  29. stream.SaveFile(temp);
  30. if (stream.CanSeek)
  31. {
  32. stream.Position = 0;
  33. }
  34. return ScanFile(temp);
  35. }
  36. /// <summary>
  37. /// 扫描文件
  38. /// </summary>
  39. /// <param name="filePath"></param>
  40. public ScanResult ScanFile(string filePath)
  41. {
  42. if (!File.Exists(filePath))
  43. {
  44. throw new FileNotFoundException();
  45. }
  46. try
  47. {
  48. //Scanning xxxxx found 1 threats.
  49. //Scanning xxxxx found no threats.
  50. var result = RunScanCommand(filePath);
  51. if (result.Contains("found no threats"))
  52. {
  53. return new ScanResult
  54. {
  55. Result = ResultCode.NotDetected,
  56. };
  57. }
  58. return new ScanResult
  59. {
  60. Result = ResultCode.Detected,
  61. };
  62. }
  63. catch (Exception ex)
  64. {
  65. return new ScanResult
  66. {
  67. Result = ResultCode.Exception,
  68. Msg = ex.Message
  69. };
  70. }
  71. }
  72. /// <summary>
  73. /// 扫描文件夹(不支持递文件夹嵌套文件夹的扫描)
  74. /// </summary>
  75. /// <param name="directoryPath"></param>
  76. /// <returns>如有威胁文件,只返回文件夹中有威胁的文件</returns>
  77. public ScanResult ScanDirectory(string directoryPath)
  78. {
  79. if (!Directory.Exists(directoryPath))
  80. {
  81. throw new DirectoryNotFoundException();
  82. }
  83. try
  84. {
  85. var files = Directory.GetFiles(directoryPath);
  86. //文件夹扫描
  87. var result = RunScanCommand(directoryPath);
  88. if (result.Contains("found no threats"))
  89. {
  90. return new ScanResult
  91. {
  92. Result = ResultCode.NotDetected,
  93. };
  94. }
  95. result = result.ToLower();
  96. var detectedFile = (from file in files
  97. let filePath = $"{file}\r\n"
  98. where result.Contains(filePath.ToLower())
  99. select file.Replace(directoryPath, "").Replace("\\", "")).ToList();
  100. //解析文件,找到有威胁的文件
  101. return new ScanResult
  102. {
  103. Result = ResultCode.Detected,
  104. Msg = string.Join(";", detectedFile)
  105. };
  106. }
  107. catch (Exception ex)
  108. {
  109. return new ScanResult
  110. {
  111. Result = ResultCode.Exception,
  112. Msg = ex.Message
  113. };
  114. }
  115. }
  116. /// <summary>
  117. /// 运行命令
  118. /// </summary>
  119. /// <param name="path"></param>
  120. /// <returns></returns>
  121. private string RunScanCommand(string path)
  122. {
  123. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  124. {
  125. throw new PlatformNotSupportedException();
  126. }
  127. var proc = new Process();
  128. try
  129. {
  130. proc.StartInfo.FileName = "cmd.exe";
  131. proc.StartInfo.UseShellExecute = false;
  132. proc.StartInfo.RedirectStandardInput = true;
  133. proc.StartInfo.RedirectStandardOutput = true;
  134. proc.StartInfo.RedirectStandardError = true;
  135. proc.StartInfo.CreateNoWindow = true;
  136. proc.Start();
  137. var command = $"\"{SystemParameter.WindowsDefenderPath}\\{SystemParameter.WindowsDefenderExeName}\" -Scan -ScanType 3 -File \"{path}\" -DisableRemediation";
  138. proc.StandardInput.WriteLine(command);
  139. proc.StandardInput.WriteLine("exit");
  140. while (!proc.HasExited)
  141. {
  142. proc.WaitForExit(1000);
  143. }
  144. return proc.StandardOutput.ReadToEnd();
  145. }
  146. catch (Exception ex)
  147. {
  148. throw new WindowsDefenderScanException();
  149. }
  150. finally
  151. {
  152. proc.Close();
  153. proc.Dispose();
  154. }
  155. }
  156. }