.NET病毒扫描API

时间:2009-06-10 11:46:09

标签: c# .net api antivirus

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

有能力建立这样的东西的人是否可以提供有关如何启动和运行的信息?我猜测防病毒软件包有API以编程方式访问它们的功能,但似乎不容易掌握细节。

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

11 个答案:

答案 0 :(得分:14)

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

您可以使用VirusTotal.com

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

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

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

Here a VirusTotal.NET a library for this API
Here the comprensive documentation about their API
Here the documentation with examples in Python of their interface

因为没有代码就没有答案是完整的,这是直接从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年答案的一个很好的更新。

答案 1 :(得分:11)

您可以使用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".

答案 2 :(得分:8)

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

答案 3 :(得分:7)

看看Microsoft Antivirus API。它使用COM,它应该很容易与.NET接口。它特指Internet Explorer和Microsoft Office,但我不明白为什么您无法使用按需扫描任何文件。

在Windows上运行的所有现代扫描程序都应该了解此API。

答案 4 :(得分:4)

各种病毒扫描程序都有API。我与之合作的是Sophos。我非常确定Norton还有一个API,而McAfee则不然(以前)。你想用什么病毒软件?您可能需要查看Metascan因为它允许与许多不同的扫描仪集成,但每年的许可证费用。 :-P

答案 5 :(得分:1)

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

答案 6 :(得分:0)

//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(); 

答案 7 :(得分:0)

我也有这个要求。我使用了clamAv防病毒软件,该软件通过将文件发送到其TCP侦听端口来提供按需扫描。您可以使用nClam nuget软件包将文件发送到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;
        }

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

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

  

X5O!P%@ AP [4 \ PZX54(P ^)7CC)7} $ EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$ H + H *

答案 8 :(得分:0)

我建议使用这种方法:

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 );
            }
        }
    }
}

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

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

答案 9 :(得分:-1)

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

答案 10 :(得分:-1)

您可以尝试使用DevDragon.io。

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

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

披露:我为他们工作。

相关问题