且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用MVC将文件发送到VirusTotal

更新时间:2023-11-27 14:47:46

如果我是正确的,则说明您使用的代码是 VirusTotal.NET

然后,如果查看他们的文档,您可能会注意到ScanFile方法需要FileInfo类型的对象并返回ScanResults类型的对象.因此,此处不包含任何字符串.

这样,您需要将文件保存在文件系统中的某个位置,然后创建一个FileInfo对象

public ActionResult Upload(HttpPostedFileBase file)
{
    string filename = Server.MapPath("~/CSV/" + file.FileName);
    file.SaveAs(filename);
    FileInfo fi = new FileInfo(filename);
    VirusTotal vtObj = new VirusTotal("%API KEY%");
    ScanResults result = vtObj.ScanFile(fi);
    .. ??? what to do if this is a virus ????
    ViewBag.Path = filename;
    return View();
}

现在,这可能会对服务器上运行的任何防病毒程序造成问题.在运行VirusTotal扫描之前进行保存可能会触发已安装的防病毒措施.因此,也许您应该拥有一个防病毒配置所排除的临时文件夹,将文件保存在那里,然后(成功扫描之后)将文件移至正确的目标文件夹(或在出现问题的情况下,从临时文件夹中删除文件)./p>

I am a student trying out another mini project where the user can upload a CSV file to the web server. But before I can create another program to execute the file, I would like to send the file to virustotal to have it check for virus.

I tried but I got an error: "cannot convert from 'string' to 'System.IO.FileInfo'"

Here is my codes:

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirusTotalNET;

namespace Testing_1.Controllers
{
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Upload(HttpPostedFileBase file)
    {
        string filename = file.FileName;
        VirusTotal vtObj = new VirusTotal("%API KEY%");
        string resID = vtObj.ScanFile(file.FileName);
        string path = Server.MapPath("~/CSV/" + file.FileName);
        file.SaveAs(path);
        ViewBag.Path = path;
        return View();
    }
  }
}

I got the error at this line: string resID = vtObj.ScanFile(file.FileName);

Index

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype =   "multipart/form-data" }))
{
<input type="file" name="File" id="file"/>
<input type="submit" value="Upload" />
}

Upload

@{
ViewBag.Title = "Upload";
}

<h2>Uploaded: @ViewBag.Path</h2>

Please help me Thank you

If I am correct you are using this library from your code VirusTotal.NET

Then, if you look at their documentation, you could notice that the ScanFile method requires an object of type FileInfo and returns an object of type ScanResults. So strings are not included in any way here.

As such you need to save your file somewhere in your file system and then create a FileInfo object

public ActionResult Upload(HttpPostedFileBase file)
{
    string filename = Server.MapPath("~/CSV/" + file.FileName);
    file.SaveAs(filename);
    FileInfo fi = new FileInfo(filename);
    VirusTotal vtObj = new VirusTotal("%API KEY%");
    ScanResults result = vtObj.ScanFile(fi);
    .. ??? what to do if this is a virus ????
    ViewBag.Path = filename;
    return View();
}

Now this could pose a problem with any antivirus program running on your server. Saving it before running the VirusTotal scan could trigger an action by your installed antivirus. So perhaps you should have a temporary folder excluded by your antivirus configuration, save the file there and then (after a successful scan) move the file to the correct destination folder (or in case of problems delete the file from the temporary folder).