【问题标题】:How can I find whether a file is infected by virus using VirusTotal.NET library in C#?如何使用 C# 中的 VirusTotal.NET 库查找文件是否被病毒感染?
【发布时间】:2019-06-08 03:10:46
【问题描述】:

我目前在我的 C# MVC 项目中使用 VirusTotal.NET nuget 包来扫描上传的文件。我正在使用这里给出的相同示例https://github.com/Genbox/VirusTotal.NET

VirusTotal virusTotal = new VirusTotal("YOUR API KEY HERE");

//Use HTTPS instead of HTTP
virusTotal.UseTLS = true;

//Create the EICAR test virus. See http://www.eicar.org/86-0-Intended-use.html
byte[] eicar = 
Encoding.ASCII.GetBytes(@"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*");

//Check if the file has been scanned before.
FileReport report = await virusTotal.GetFileReportAsync(eicar);

Console.WriteLine("Seen before: " + (report.ResponseCode == FileReportResponseCode.Present ? "Yes" : "No"));

我正在将上传文件的字节数组加载到上述代码中的eicar 变量中。根据给定的示例,它将提供文件是否被扫描过。但我真正需要的是文件是否被感染。谁能给我一个解决方案?

【问题讨论】:

    标签: c# virus-scanning


    【解决方案1】:

    查看UrlReport 类,您返回的报告包含更多信息,而不仅仅是代码示例中的响应代码。有 3 个字段看起来很有趣:

    /// <summary>
    /// How many engines flagged this resource.
    /// </summary>
    public int Positives { get; set; }
    
    /// <summary>
    /// The scan results from each engine.
    /// </summary>
    public Dictionary<string, UrlScanEngine> Scans { get; set; }
    
    /// <summary>
    /// How many engines scanned this resource.
    /// </summary>
    public int Total { get; set; }
    

    这可能会为您提供您正在寻找的结果。 VirusTotal 实际上会返回多个扫描引擎的结果,其中一些可能会检测到病毒,而另一些可能不会。

    Console.WriteLine($"{report.Positives} out of {report.Total} scan engines detected a virus.");
    

    你可以对这些数据做任何你想做的事情,比如计算百分比:

    var result = 100m * report.Positives / report.Total;
    Console.WriteLine($"{result}% of scan engines detected a virus.");
    

    或者只是将大多数正面扫描引擎结果视为整体正面结果:

    var result = Math.Round(report.Positives / Convert.ToDecimal(report.Total));
    Console.WriteLine($"Virus {(result == 0 ? "not detected": "detected")});
    

    【讨论】:

    • 我认为阈值应该远低于 50%。即使是 10% 的引擎也不太可能在同一文件上看到误报。
    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 2013-07-19
    • 2023-03-13
    • 2018-07-01
    • 2020-10-21
    • 2015-04-06
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多