49

我正在构建一个 Web 应用程序,我需要在其中扫描用户上传的文件中的病毒。

有没有人在构建这样的东西方面有经验可以提供有关如何启动和运行的信息?我猜防病毒软件包有 API 可以通过编程方式访问它们的功能,但似乎掌握细节并不容易。

仅供参考,该应用程序是用 C# 编写的。

4

11 回答 11

19

使用前重要提示: 注意 TOS 协议。您授予他们对所有内容的完全访问权限:“当您上传或以其他方式提交内容时,您授予 VirusTotal(以及与我们合作的人)全球范围内、免版税、不可撤销和可转让的许可,以使用、编辑、托管、存储、复制、修改、创作衍生作品、传播、发布、公开表演、公开展示和分发此类内容。”

您可以使用VirusTotal.com的服务,而不是使用本地防病毒程序(从而将您的程序绑定到特定的防病毒产品并要求您的客户安装该防病毒产品)

该站点提供免费服务,您的文件将作为众多防病毒产品的输入,您会收到一份详细的报告,其中包含扫描过程产生的证据。通过这种方式,您的解决方案不再绑定到特定的防病毒产品(尽管您已绑定到 Internet 可用性)

该站点还提供了一个应用程序编程接口,允许以编程方式访问其扫描引擎。

这里是 VirusTotal.NET 这个 API 的库
这里是关于他们的 API 的综合文档
这里是带有 Python 接口示例的文档

并且因为没有代码就没有完整的答案,这直接取自 VirusTotal.NET 库附带的示例客户端

static void Main(string[] args)
{
    VirusTotal virusTotal = new VirusTotal(ConfigurationManager.AppSettings["ApiKey"]);

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

    //Create the EICAR test virus. See http://www.eicar.org/86-0-Intended-use.html
    FileInfo fileInfo = new FileInfo("EICAR.txt");
    File.WriteAllText(fileInfo.FullName, @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*");

    //Check if the file has been scanned before.
    FileReport fileReport = virusTotal.GetFileReport(fileInfo);

    bool hasFileBeenScannedBefore = fileReport.ResponseCode == ReportResponseCode.Present;

    Console.WriteLine("File has been scanned before: " + (hasFileBeenScannedBefore ? "Yes" : "No"));

    //If the file has been scanned before, the results are embedded inside the report.
    if (hasFileBeenScannedBefore)
    {
        PrintScan(fileReport);
    }
    else
    {
        ScanResult fileResult = virusTotal.ScanFile(fileInfo);
        PrintScan(fileResult);
    }
    ... continue with testing a web site ....

}

免责声明
我与他们无关。我写这个答案只是因为它似乎是这些 4 岁答案的一个很好的更新。

于 2015-02-07T19:45:13.430 回答
16

您可以使用 IAttachmentExecute API。

Windows 操作系统提供通用的API 来调用已安装的杀毒软件(当然需要的杀毒软件支持API)。但是,调用杀毒软件的API只提供COM接口样式,不支持IDispatch。因此,从任何 .NET 语言和脚本语言调用此 API 都太困难了。

从此处下载此库Anti Virus Scanner for .NET或从“NuGet” AntiVirusScanner添加引用您的 VS 项目

例如下面的代码扫描文件:

var scanner = new AntiVirus.Scanner();
var result = scanner.ScanAndClean(@"c:\some\file\path.txt");
Console.WriteLine(result); // console output is "VirusNotFound".
于 2016-02-28T15:12:42.197 回答
8

我可能只是进行系统调用来运行一个独立的进程来进行扫描。有许多来自不同供应商的命令行 AV 引擎。

于 2009-06-10T11:58:56.627 回答
7

查看Microsoft 防病毒 API。它使用 COM,它应该很容易与 .NET 交互。它专门指 Internet Explorer 和 Microsoft Office,但我不明白为什么您不能使用 to 按需扫描任何文件。

所有在 Windows 上运行的现代扫描仪都应该理解这个 API。

于 2009-06-10T12:23:21.053 回答
4

各种病毒扫描程序都有 API。我集成的一个是 Sophos。我很确定诺顿也有一个 API,而 McAfee 没有(它曾经)。你想用什么病毒软件?您可能想查看Metascan,因为它允许与许多不同的扫描仪集成,但需要支付年度许可费用。:-P

于 2009-06-10T12:23:06.157 回答
2

无耻的插件,但您可能想查看https://scanii.com,它基本上是将恶意软件/病毒检测作为(REST)服务。哦,另外,请确保您阅读并理解 virustotal 的 API 条款(https://www.virustotal.com/en/documentation/public-api/) - 他们非常清楚不允许商业用途。

于 2015-06-22T05:35:15.407 回答
2

我也有这个要求。我使用了 clamAv 防病毒软件,它通过将文件发送到他们的 tcp 监听端口来提供按需扫描。您可以使用nClamnuget 包将文件发送到clamav.

var clam = new ClamClient("localhost", 3310);
var scanResult = clam.ScanFileOnServerAsync("C:\\test.txt"); //any file you would like!
switch (scanResult.Result.Result)
{
    case ClamScanResults.Clean:
        Console.WriteLine("The file is clean!");
        break;
    case ClamScanResults.VirusDetected:
        Console.WriteLine("Virus Found!");
        Console.WriteLine("Virus name: {0}", scanResult.Result.InfectedFiles[0].FileName);
        break;
    case ClamScanResults.Error:
        Console.WriteLine("Woah an error occured! Error: {0}", scanResult.Result.RawResult);
        break;
}

这里显示了一个简单而详细的示例。注意:- 同步扫描方法在最新的 nuget 中不可用。你必须像我上面做的那样编码

txt为了测试病毒,您可以在文件中使用以下字符串

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-标准-抗病毒-测试文件!$H+H*

于 2018-07-24T09:39:36.810 回答
1

我建议使用这种方法:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.VirusScan.Api;
using Cloudmersive.APIClient.NET.VirusScan.Client;
using Cloudmersive.APIClient.NET.VirusScan.Model;

namespace Example
{
    public class ScanFileAdvancedExample
    {
        public void main()
        {
            // Configure API key authorization: Apikey
            Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
            
            

            var apiInstance = new ScanApi();
            var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
            var allowExecutables = true;  // bool? | Set to false to block executable files (program code) from being allowed in the input file.  Default is false (recommended). (optional) 
            var allowInvalidFiles = true;  // bool? | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document.  Default is false (recommended). (optional) 
            var allowScripts = true;  // bool? | Set to false to block script files, such as a PHP files, Pythong scripts, and other malicious content or security threats that can be embedded in the file.  Set to true to allow these file types.  Default is false (recommended). (optional) 
            var allowPasswordProtectedFiles = true;  // bool? | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords.  Set to true to allow these file types.  Default is false (recommended). (optional) 
            var restrictFileTypes = restrictFileTypes_example;  // string | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files.  All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false.  Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional) 

            try
            {
                // Advanced Scan a file for viruses
                VirusScanAdvancedResult result = apiInstance.ScanFileAdvanced(inputFile, allowExecutables, allowInvalidFiles, allowScripts, allowPasswordProtectedFiles, restrictFileTypes);
                Debug.WriteLine(result);
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling ScanApi.ScanFileAdvanced: " + e.Message );
            }
        }
    }
}

请注意,通过这种方式,您甚至可以控制是否过滤掉非病毒威胁有效负载,例如可执行文件、脚本、加密/受密码保护的文件等。

这种方法有一个免费层,还可以验证您上传的文件的内容。

于 2020-07-15T02:25:45.867 回答
0

您可以尝试使用 DevDragon.io。

它是一个带有 API 和 .NET 客户端 DevDragon.Antivirus.Client 的 Web 服务,您可以从 NuGet 获得。1MB 文件的扫描时间不到 200 毫秒。

更多文档: https ://github.com/Dev-Dragon/Antivirus-Client

披露:我为他们工作。

于 2018-02-08T20:19:04.057 回答
-2

根据我的经验,您可以使用 COM 与一些防病毒软件进行交互。但我建议更简单一些,只需在扫描后解析扫描结果即可。您需要做的就是启动扫描程序进程并将其指向您要扫描的文件/文件夹,将扫描结果存储到文件中或将标准输出重定向到您的应用程序并解析结果。

于 2009-06-10T12:01:12.860 回答
-3
//Scan  
string start = Console.ReadLine();  
System.Diagnostics.Process scanprocess = new System.Diagnostics.Process();  
sp.StartInfo.WorkingDirectory = @"<location of your antivirus>";  
sp.StartInfo.UseShellExecute = false;  
sp.StartInfo.FileName = "cmd.exe";  
sp.StartInfo.Arguments = @"/c antivirusscanx.exe /scan="+filePath;  
sp.StartInfo.CreateNoWindow = true;  
sp.StartInfo.RedirectStandardInput = true;    
sp.StartInfo.RedirectStandardError = true; sp.Start();  
string output = sp.StandardOutput.ReadToEnd();  
//Scan results  
System.Diagnostics.Process pr = new System.Diagnostics.Process();      
pr.StartInfo.FileName = "cmd.exe";  
pr.StartInfo.Arguments = @"/c echo %ERRORLEVEL%";   
pr.StartInfo.RedirectStandardInput = true;    
pr.StartInfo.RedirectStandardError = true; pr.Start();  
output = processresult.StandardOutput.ReadToEnd();  
pr.Close(); 
于 2015-02-07T23:20:36.963 回答