Sending file to VirusTotal with MVC

纵饮孤独 提交于 2020-01-06 20:15:00

问题


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


回答1:


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).




回答2:


i think you need to view the document and api of virus total to implement the uploading feature in your mini project.



来源:https://stackoverflow.com/questions/38140419/sending-file-to-virustotal-with-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!