2011-08-15 56 views
0

是否可以基于病毒扫描状态以编程方式移动文件?病毒扫描后以编程方式移动文件

我想要做的是有一组文件夹:

传入 扫描 扫描/清洁 扫描/感染 未扫描

文件将被丢弃到输入文件夹中。此时,我想启动防病毒并扫描Incoming文件夹中的文件。一旦完成,这些文件就需要被移动到适当的文件夹,清洁或感染。如果由于某种原因无法扫描文件或遇到扫描问题,它将被移至未扫描文件夹。

我希望能有一种方法来编写脚本。有没有人曾经做过这样的事情?

+0

当然,这是可能的。一切皆有可能。但这是一个相当广泛的问题,你没有指定你使用的AV,甚至没有指定你正在运行的环境,所以你不可能得到任何有用的答案。 – Jason

+0

在这一点上......任何事情。我不在乎防病毒,它是一个简单的Windows环境。这些文件通过POST放置到Windows Server 2003服务器上。如果我必须下载免费的防病毒软件,我会的。为了记录,服务器运行Symantec。 – Matt

+0

这可能是更好的超级用户问题 - “哪个AV将允许我监视文件夹X并将受感染的文件移动到文件夹Y” – Jason

回答

0
public void Scan() 
{ 
    string[] uploadPath = Directory.GetFiles(ConfigurationManager.AppSettings["UploadPath"]); 

    foreach(string filePath in uploadPath) 
    { 
     string fileName = Path.GetFileName(filePath); 
     string cleanPath = Path.Combine(ConfigurationManager.AppSettings["CleanPath"], fileName); 

     try 
     { 
      Process AV = new Process(); 

      AV.StartInfo.UseShellExecute = false; 
      AV.StartInfo.RedirectStandardOutput = true; 
      AV.StartInfo.FileName = ConfigurationManager.AppSettings["VSApp"]; 
      AV.StartInfo.Arguments = " -Scan -ScanType 3 -file " + ConfigurationManager.AppSettings["UploadPath"] + " -DisableRemediation"; 

      AV.Start(); 

      string output = AV.StandardOutput.ReadToEnd(); 
      AV.WaitForExit(); 

      if (AV.ExitCode == 0) 
      { 
       File.Move(filePath, cleanPath); 
      } 

      else if (AV.ExitCode == 2) 
      { 
       using (TextWriter tw = new StreamWriter(ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt")) 
       { 
        tw.WriteLine("2"); 
        tw.Close(); 
       } 

       using (TextWriter tw1 = new StreamWriter(ConfigurationManager.AppSettings["FailedFiles"] + fileName + ".txt")) 
       { 
        tw1.WriteLine(AV.StandardOutput); 
        tw1.Close(); 
       } 

       File.Delete(filePath); 
      } 

      AV.Close(); 
     } 

     catch (Exception ex) 
     { 
      if (ex.ToString().Contains("Could not find file")) 
      { 
       string failedFile = ConfigurationManager.AppSettings["FailedPath"] + fileName + ".txt"; 
       string failedFileDesc = ConfigurationManager.AppSettings["FailedPath"] + fileName + "_ErrorDesc" + ".txt"; 
       using (TextWriter tw = new StreamWriter(failedFile)) 
       { 
        tw.WriteLine("2"); 
        tw.Close(); 
       } 

       using (TextWriter tw1 = new StreamWriter(failedFileDesc)) 
       { 
        tw1.WriteLine(ex.ToString()); 
        tw1.Close(); 
       } 
      } 

      else 
      {      
       Thread.Sleep(2000); 
       if (runCounter == 0) 
       { 
        Scan(); 
       } 
       runCounter++; 

       string errorFile = ConfigurationManager.AppSettings["ProcessErrorPath"] + fileName + ".txt"; 
       using (TextWriter tw = new StreamWriter(errorFile)) 
       { 
        tw.WriteLine(ex.ToString()); 
        tw.Close(); 
       } 
      } 
     } 
    } 
} 

我将此创建为Windows服务。我的OnStart方法创建我的FileSystemWatcher来观察上传路径。对于创建,我有一个方法,运行我的扫描方法,并创建我的计数器,并将其设置为0.我的错误事件刚刚记录。我有一个FileSystemWatcher在上载之前试图打开文件的问题,因此我添加了睡眠。

最后,我使用Microsoft Forefront的命令行扫描程序。文件路径:C:\ Program Files \ Microsoft安全客户端\ mpcmdrun.exe。

让我知道是否有任何问题。

+0

,我记录了失败的代码(2)和实际的失败。失败代码被用作SQL中的标志。我使用实际的失败来弄清楚发生了什么。在Forefront中,0表示通过,2表示失败,我认为它是未知的5。当文件在上传之前打开或者刚刚离开时发生未知事件。 – Matt