简体   繁体   English

如何在C#中检测Windows Server 2008上的防病毒软件?

[英]How to detect antivirus on Windows Server 2008 in C#?

I have seen code samples similar to the following numerous times in my search for an answer: 在搜索答案时,我已经多次看到类似以下的代码示例:

using System;
using System.Text;
using System.Management;

namespace ConsoleApplication1
{
  class Program
  {
    public static bool AntivirusInstalled()
    {

      string wmipathstr = @"\\" + Environment.MachineName + @"\root\SecurityCenter";
      try
      {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipathstr, "SELECT * FROM AntivirusProduct");
        ManagementObjectCollection instances = searcher.Get();
        return instances.Count > 0;
      }

      catch (Exception e)
      {
        Console.WriteLine(e.Message);
      }

      return false;
    } 

    public static void Main(string[] args)
    {
      bool returnCode = AntivirusInstalled();
      Console.WriteLine("Antivirus Installed " + returnCode.ToString());
      Console.WriteLine();
      Console.Read();
    }

  }
}

Unfortunately, it appears that Windows Server 2008 does not have the SecurityCenter or SecurityCenter2 namespace, so I get an Invalid namespace exception when trying this approach. 不幸的是,Windows Server 2008似乎没有SecurityCenterSecurityCenter2命名空间,因此在尝试此方法时会出现Invalid namespace异常。

Does anyone know of a way to determine if there is antivirus software running on Windows Server 2008? 有谁知道确定Windows Server 2008上是否有防病毒软件运行的方法? Any help is appreciated! 任何帮助表示赞赏!

Use the EICAR test virus. 使用EICAR测试病毒。

  1. Have your application try to write one of these files on disk: http://www.eicar.org/85-0-Download.html 让您的应用程序尝试在磁盘上写下其中一个文件: http//www.eicar.org/85-0-Download.html
  2. Catch the exception 抓住例外

It will not only work on every anti-virus on earth, but it will also tell you if the anti-virus is active! 它不仅适用于地球上的每一种反病毒,它还会告诉你反病毒是否活跃!

You may find it hard to download the test file if you have anti-virus active, so you may want to use this string instead: 如果您有防病毒活动,您可能会发现很难下载测试文件,因此您可能希望使用此字符串:

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

Keep in mind, you probably want to keep the string encoded on your app and decode it just before you write it to disk. 请记住,您可能希望在应用程序上保留编码的字符串,然后在将其写入磁盘之前对其进行解码。 Otherwise you may risk your app being detected as a virus :) 否则,您的应用可能会被检测为病毒:)

On the EICAR site, they say: 在EICAR网站上,他们说:

Any anti-virus product that supports the EICAR test file should detect it in any file providing that the file starts with the following 68 characters, and is exactly 68 bytes long 任何支持EICAR测试文件的防病毒产品都应在任何文件中检测到它,前提是该文件以下列68个字符开头,并且长度恰好为68个字节

However, I wouldn't count AV developers have read the spec, so better just keep the string encoded. 但是,我不认为AV开发人员已经阅读了规范,所以最好只保留字符串编码。 In fact, I just tried to save the string on a .txt file on my desktop with some additional characters in it and Windows Defender started screaming. 事实上,我只是尝试将字符串保存在桌面上的.txt文件中,其中包含一些其他字符,Windows Defender开始尖叫。

Hmmm, I ended up playing around with PowerShell: 嗯,我最终玩PowerShell了:

$avSoftware = get-wmiobject -class "Win32_Product" -namespace "root\cimv2" -computername "." -filter "Name like '%antivirus%'"
if ($avSoftware.Count -gt 0) {
    foreach ($av in $avSoftware) {
        write-host $p.Name
    }
} else {
    write-host "No AV software found"
}

It seems to be working both on our Windows Server 2008 and 2008 R2 instances... 它似乎在我们的Windows Server 2008和2008 R2实例上都有效...

More info here: https://serverfault.com/questions/12343/how-can-i-determine-whether-an-antivirus-product-is-installed 更多信息: https//serverfault.com/questions/12343/how-can-i-determine-whether-an-antivirus-product-is-installed

我前段时间遇到了一个客户问题,最后我在本地系统驱动程序和进程上执行了dictonary搜索,寻找已知的防病毒签名模式(例如文件夹名称,进程名称等等)。不是百分之百确定,因为某个地方有人会下载一个你不知道的全新的反病毒,但除此之外,它非常有效......

This is more of an idea than a perfect solution. 这不是一个完美的解决方案。 With respect to the answer by Leonardo, how about using an actual piece of anti-virus software (link against it) in order to perform a search for other anti-virus software? 关于莱昂纳多的答案,如何使用实际的反病毒软件(链接它)来搜索其他反病毒软件? ClamAV is opensource and a nice point to start. ClamAV是开源的,是一个很好的起点。 You "only" need to define a new and rather specific signature database. 您“只”需要定义一个新的特别是特定的签名数据库。

According to most of the web, SecurityCenter and SecurityCenter2 are not available on Windows Server 2008 (as you have already worked out for yourself). 根据大多数网站,Windows Server 2008上没有SecurityCenter和SecurityCenter2(因为您已经为自己制定了)。

I found this SO article, which contains a workaround. 我找到了这篇SO文章,其中包含一个解决方法。 How to detect antivirus installed on windows 2003 server and 2008 server 2003 server R2and 2008 server R2 using WMI or other then WMI in C++ 如何使用WMI或其他CMI中的WMI检测安装在Windows 2003服务器和2008 Server 2003服务器R2和2008服务器R2上的防病毒软件

Admittedly, this is a C++ implementation, but I see no reason that it cannot be ported to C# 不可否认,这是一个C ++实现,但我认为没有理由不能将它移植到C#

Also found this page which suggests using the OESIS framework. 还发现此页面建议使用OESIS框架。 http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/b0806608-fee0-413c-a34d-674aeb11be3c http://social.msdn.microsoft.com/Forums/en/windowsgeneraldevelopmentissues/thread/b0806608-fee0-413c-a34d-674aeb11be3c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM