非常简单的C#Windows服务 - Malwarebytes隔离它。我做的事情有问题吗?

时间:2016-03-18 14:03:29

标签: c# .net windows-services antivirus

我正在尝试学习编写Windows服务,而我正在使用C#来完成它。我在这里找到了微软网站的教程:

https://msdn.microsoft.com/en-us/library/zt39148a%28v=vs.110%29.aspx

它向您展示了如何构建一个简单的Windows服务,将一些消息记录到事件日志中。我或多或少遵循了指示,一切正常。但它指示您使用Designers和神秘的拖放组件,并依赖于一堆IDE自动生成的代码。所以,为了试图真正理解我在做什么,我试图从我手动键入的原始类中创建一个(基本上)等效的Windows服务,而不是设计师等。

编译正常,installutil.exe成功将其安装为服务。但是当我尝试启动该服务时,我收到以下错误:

Windows could not start the Bob Manual Service Display Name service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion.

最后,当我尝试启动该服务时,我发现Malwarebytes(我的防病毒程序)正在隔离我项目的编译exe。它说它感染了“Backdoor.Bot”。我已经尝试过调试和发布版本,它会隔离两者。

显然我可以将exe白名单或临时禁用Malwarebytes或其他任何东西,但我对这个Windows服务的东西都是全新的,我担心我可能在不知不觉中在我的代码中做了一些明显错误或危险的事情。

我附加了我的代码,它的目的是基于Microsoft示例代码但非常简单 - 三个小类,一个Main,一个System.ServiceProcess.ServiceBase和一个System.Configuration.Install.Installer。

Malwarebytes 应该隔离这个吗?

我想象的一件事可能看起来有问题,我将帐户设置为ServiceAccount.LocalSystem,Microsoft教程称其具有“广泛权限”,因此“可能会增加您受到恶意软件攻击的风险”,但是:

(1)这就是微软的示例代码中的内容(据我所知,它是EventLog内容所必需的);

(2)我实际上最初意外地将它作为LocalService,并且发生了同样的错误。

namespace Project1
{
    using System.ServiceProcess;

    static class BobMain
    {
        static void Main()
        {
            ServiceBase[] ServicesToRun = { new BobManualService() };
            ServiceBase.Run(ServicesToRun);
        }
    }
}

namespace Project1
{
    using System.Diagnostics;
    using System.ServiceProcess;

    public class BobManualService : ServiceBase
    {
        private EventLog eventLog;

        public BobManualService()
        {
            this.eventLog = new EventLog();

            if (!EventLog.SourceExists("BobSource"))
            {
                EventLog.CreateEventSource("BobSource", "BobLog");
            }

            this.eventLog.WriteEntry("Super duper constructor!");
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
}

namespace Project1
{
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.ServiceProcess;

    [RunInstaller(true)]
    public class BobInstaller : Installer
    {
        private ServiceProcessInstaller serviceProcessInstaller;

        private ServiceInstaller serviceInstaller;

        public BobInstaller()
        {
            this.serviceProcessInstaller = new ServiceProcessInstaller();
            this.serviceInstaller = new ServiceInstaller();

            this.serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
            this.serviceProcessInstaller.Password = null;
            this.serviceProcessInstaller.Username = null;
            this.serviceProcessInstaller.AfterInstall +=
                this.serviceProcessInstaller_AfterInstall;

            this.serviceInstaller.Description = 
                "Bob Manual Service Description";
            this.serviceInstaller.DisplayName =
                "Bob Manual Service Display Name";
            this.serviceInstaller.ServiceName = "BobManualService";
            this.serviceInstaller.StartType = ServiceStartMode.Automatic;

            this.Installers.AddRange(new Installer[]
                                         {
                                             this.serviceProcessInstaller,
                                             this.serviceInstaller
                                         });
        }

        private void serviceProcessInstaller_AfterInstall(
            object sender,
            InstallEventArgs e)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

查看代码,在OnStart(string[] args)方法中,没有启动进程在单独的线程上运行很长时间。所以您的服务立即启动并存在。请仔细查看"定义服务启动时发生的情况"在您引用的教程中。 基本上只需添加将计时器设置为OnStart(string[] args)方法的代码,您的服务就可以保持活跃状态​​。