WindowsDefenderScanService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. var result = ScanFile(temp);
  35. try
  36. {
  37. File.Delete(temp);
  38. }
  39. catch (Exception)
  40. {
  41. // ignored
  42. }
  43. return result;
  44. }
  45. /// <summary>
  46. /// 扫描文件
  47. /// </summary>
  48. /// <param name="filePath"></param>
  49. public ScanResult ScanFile(string filePath)
  50. {
  51. if (!File.Exists(filePath))
  52. {
  53. throw new FileNotFoundException();
  54. }
  55. try
  56. {
  57. //Scanning xxxxx found 1 threats.
  58. //Scanning xxxxx found no threats.
  59. var result = RunScanCommand(filePath);
  60. if (result.Contains("found no threats"))
  61. {
  62. return new ScanResult
  63. {
  64. Result = ResultCode.NotDetected,
  65. };
  66. }
  67. return new ScanResult
  68. {
  69. Result = ResultCode.Detected,
  70. };
  71. }
  72. catch (Exception ex)
  73. {
  74. return new ScanResult
  75. {
  76. Result = ResultCode.Exception,
  77. Msg = ex.Message
  78. };
  79. }
  80. }
  81. /// <summary>
  82. /// 扫描文件夹(不支持递文件夹嵌套文件夹的扫描)
  83. /// </summary>
  84. /// <param name="directoryPath"></param>
  85. /// <returns>如有威胁文件,只返回文件夹中有威胁的文件</returns>
  86. public ScanResult ScanDirectory(string directoryPath)
  87. {
  88. if (!Directory.Exists(directoryPath))
  89. {
  90. throw new DirectoryNotFoundException();
  91. }
  92. try
  93. {
  94. var files = Directory.GetFiles(directoryPath);
  95. //文件夹扫描
  96. var result = RunScanCommand(directoryPath);
  97. if (result.Contains("found no threats"))
  98. {
  99. return new ScanResult
  100. {
  101. Result = ResultCode.NotDetected,
  102. };
  103. }
  104. result = result.ToLower();
  105. var detectedFile = (from file in files
  106. let filePath = $"{file}\r\n"
  107. where result.Contains(filePath.ToLower())
  108. select file.Replace(directoryPath, "").Replace("\\", "")).ToList();
  109. //解析文件,找到有威胁的文件
  110. return new ScanResult
  111. {
  112. Result = ResultCode.Detected,
  113. Msg = string.Join(";", detectedFile)
  114. };
  115. }
  116. catch (Exception ex)
  117. {
  118. return new ScanResult
  119. {
  120. Result = ResultCode.Exception,
  121. Msg = ex.Message
  122. };
  123. }
  124. }
  125. /// <summary>
  126. /// 运行命令
  127. /// </summary>
  128. /// <param name="path"></param>
  129. /// <returns></returns>
  130. private string RunScanCommand(string path)
  131. {
  132. if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  133. {
  134. throw new PlatformNotSupportedException();
  135. }
  136. var proc = new Process();
  137. try
  138. {
  139. proc.StartInfo.FileName = "cmd.exe";
  140. proc.StartInfo.UseShellExecute = false;
  141. proc.StartInfo.RedirectStandardInput = true;
  142. proc.StartInfo.RedirectStandardOutput = true;
  143. proc.StartInfo.RedirectStandardError = true;
  144. proc.StartInfo.CreateNoWindow = true;
  145. proc.Start();
  146. var command = $"\"{SystemParameter.WindowsDefenderPath}\\{SystemParameter.WindowsDefenderExeName}\" -Scan -ScanType 3 -File \"{path}\" -DisableRemediation";
  147. proc.StandardInput.WriteLine(command);
  148. proc.StandardInput.WriteLine("exit");
  149. while (!proc.HasExited)
  150. {
  151. proc.WaitForExit(1000);
  152. }
  153. return proc.StandardOutput.ReadToEnd();
  154. }
  155. catch (Exception ex)
  156. {
  157. throw new WindowsDefenderScanException();
  158. }
  159. finally
  160. {
  161. proc.Close();
  162. proc.Dispose();
  163. }
  164. }
  165. }