使用MVC将文件发送到VirusTotal [英] Sending file to VirusTotal with MVC

查看:96
本文介绍了使用MVC将文件发送到VirusTotal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学生,正在尝试另一个迷你项目,用户可以在其中将CSV文件上传到Web服务器.但是,在我可以创建另一个程序执行该文件之前,我想将文件发送到virustotal以便对其进行病毒检查.

我尝试过但出现错误:无法从'string'转换为'System.IO.FileInfo'"

这是我的代码:

控制器

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

我在此行遇到错误:字符串resID = vtObj.ScanFile(file.FileName);

索引

@{
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" />
}

上传

@{
ViewBag.Title = "Upload";
}

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

请帮助我,谢谢

解决方案

如果我是正确的,则说明您使用的代码是 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).

这篇关于使用MVC将文件发送到VirusTotal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆