复制一个病毒文件导致我的程序终止。

5
我制作了一个工具,可以将文件从源地址复制到目标地址。然而,在复制过程中,软件发现了一个被 Symantec 防病毒软件标记的病毒。
然后防病毒软件导致我的程序关闭,并将该程序隔离为“dropper”。
有没有办法优雅地处理这种情况,而不是完全关闭我的程序?
我知道这个操作是由防病毒软件导致的,但是我能做些什么来帮助这种情况吗?例如,Robocopy 在遇到病毒时并不会立即终止。
以下是我的复制代码;
void CopyFileExactly(CopyParameterBundle cpb, bool overwrite)
{

    string CTP = "", CFP = "";
    CFP = cpb.SourcePath;

    if (cpb.RenameFile)
        CTP = cpb.DestPath ;
    else
        CTP = cpb.DestPath;

    //Check firstly if the file to copy exists
    if (!File.Exists(CFP))
    {
        throw new FileNotFoundException();
    }

    //Check if destination file exists
    //If it does, make it not read only so we can update MAC times
    if (File.Exists(CTP))
    {
        var target = GetFile(CTP);//new FileInfo(CTP);
        if (target.IsReadOnly)
            target.IsReadOnly = false;
    }

    var origin = GetFile(CFP);//new FileInfo(CFP);
    GetFile(CTP).Directory.Create();
    //(new FileInfo(CTP)).Directory.Create();

    origin.CopyTo(CTP, (overwrite ? true : false));

    if (!File.Exists(CTP))
    {
        throw new FileNotFoundException("Destination file not found!");
    }
    var destination = GetFile(CTP);//new FileInfo(CTP);
    if (destination.IsReadOnly)
    {
        destination.IsReadOnly = false;
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
        destination.IsReadOnly = true;
    }
    else
    {
        destination.CreationTime = origin.CreationTime;
        destination.LastWriteTime = origin.LastWriteTime;
        destination.LastAccessTime = origin.LastAccessTime;
    }

    if (performMD5Check)
    {
        var md5Check = compareFileMD5(CFP, CTP);
        cpb.srcMD5Hash = md5Check.Item2;
        cpb.dstMD5Hash = md5Check.Item3;
        if (!md5Check.Item1)
            throw new MD5MismatchException("MD5 Hashes do NOT match!");
    }

}

调用代码;
        void BeginCopy(int DegreeOfParallelism, int retryCount, int retryDelay)
    {
        object _lock;
        //Setup cancellation token
        po.CancellationToken = cts.Token;
        //Set max number of threads
        po.MaxDegreeOfParallelism = DegreeOfParallelism;
        //Exceptio logging queue
        var exceptions = new ConcurrentQueue<Exception>();
        var completeItems = new ConcurrentQueue<CopyParameterBundle>();
        var erroredItems = new ConcurrentQueue<CopyParameterBundle>();

        //Logger logger = new Logger(sLogPath);

        //logger.Write("Starting copy");
        Task.Factory.StartNew(() =>
        {
            Parallel.ForEach(CopyParameters,
                po,
                (i, loopState, localSum) =>
                {
                    localSum = retryCount;
                    do
                    {
                        try
                        {
                            //Go off and attempt to copy the file
                            DoWork(i);
                            //Incrememt total count by 1 if successfull
                            i.copyResults.TransferTime = DateTime.Now;
                            i.copyResults.TransferComplete = true;

                            completeItems.Enqueue(i);
                            //logger.Write("Copied file from: " + i.SourcePath + "\\" + i.SourceFile + "  =>  " + i.DestPath + "\\" + i.SourceFile);

                            break;
                        }
                        catch (Exception ex)
                        {
                            //this.richTextBox1.AppendText("[-] Exception on: " + i.SourcePath + "\\" + i.SourceFile + "   => " + ex.Message.ToString() + System.Environment.NewLine);
                            //Exception was thrown when attempting to copy file
                            if (localSum == 0)
                            {
                                //Given up attempting to copy. Log exception in exception queue
                                exceptions.Enqueue(ex);
                                this.SetErrorText(exceptions.Count());

                                //Write the error to the screen
                                this.Invoke((MethodInvoker)delegate
                                {
                                    this.richTextBox1.AppendText("[-] Exception on: " + i.SourcePath + "\\" + i.SourceFile + "   => " + ex.Message.ToString() + System.Environment.NewLine);
                                    i.copyResults.TransferComplete = false;
                                    i.copyResults.TransferTime = DateTime.Now;
                                    i.copyResults.exceptionMsg = ex;
                                    erroredItems.Enqueue(i);

                                    //logger.Write("ERROR COPYING FILE FROM : " + i.SourcePath + "\\" + i.SourceFile + " => " + i.DestPath + "\\" + i.SourceFile + "  => " + ex.Message.ToString() + "  => " + ex.Source);
                                });
                            }
                            //Sleep for specified time before trying again
                            Thread.Sleep(retryDelay);
                            localSum--;
                        }

                        //Attempt to Repeat X times
                    } while (localSum >= 0);

                    //Check cancellation token
                    po.CancellationToken.ThrowIfCancellationRequested();

                    Interlocked.Increment(ref TotalProcessed);
                    this.SetProcessedText(TotalProcessed);

                    //Update Progress Bar
                    this.Invoke((MethodInvoker)delegate
                    {
                        this.progressBar1.Value = (TotalProcessed);
                    });
                });


            //aTimer.Stop();
            this.Invoke((MethodInvoker)delegate
            {
                this.label9.Text = "Process: Writing Log";
            });

            WriteLog(sLogPath, completeItems, erroredItems);

            this.Invoke((MethodInvoker)delegate
            {
                this.label9.Text = "Process: Done!";
            });

            if (exceptions.Count == 0)
                MessageBox.Show("Done!");
            else
                MessageBox.Show("Done with errors!");

            EnableDisableButton(this.button2, true);
            EnableDisableButton(this.button4, false);
        });

    }

如果你的应用程序被标记为问题,那么你的选择就会变得更少,因为杀毒软件的作用就是终止应用程序并阻止其进一步的工作 - 如果它只是在你复制文件时突出显示一个文件,那么需要添加更多的尝试循环(比如在origin.CopyTo等周围)。 - BugFinder
你如何复制文件? - Sergey L
它是如何实现的?从你的代码中并不清楚它是一种标准方法还是你自定义的实现。 - Sergey L
2
我的猜测是,robocopy 被认为是官方工具,因此不会被终止。而你的程序是一个未知的应用程序,因此被认为可能是恶意软件,因此在删除病毒时被终止。最简单的解决方案:不要复制病毒。除此之外,你不能“处理”被病毒扫描器终止的情况。 - MicroVirus
1
水晶球显示,您没有使用来自受信任根机构的证书对程序进行代码签名。当您将程序部署到其他人的计算机上时,这是一个很大的开销,但肯定是预期的。2048位数字的证书价格不菲。 - Hans Passant
显示剩余11条评论
1个回答

0
发生的情况很可能是杀毒软件意识到病毒文件,所以当它检测到文件系统中发生了变化(移动文件)时,它终止了程序,因为将病毒移动到计算机的不同位置可能会引起问题(因为它是病毒)。它被标记为 dropper,基本上是一种旨在安装病毒的程序类型。
编辑:我忘了提到解决问题的方法,您很可能需要为您的程序获取许可证。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接