使用C#检测Windows上的防病毒软件

20

有没有一种方法可以用C#检测计算机上是否安装了防病毒软件?我知道“安全中心”可以检测防病毒软件,但是如何在C#中检测呢?


你可以使用WMI;请参见这里 - SLaks
有没有办法在Windows 7系统中知道防病毒软件是否已更新?@Angel.King.47 - TechBrkTru
3个回答

35

根据微软的说法,Windows安全中心采用了两层检测状态的方法。一层是手动检测,另一层是通过Windows管理工具(WMI)自动检测。在手动检测模式下,Windows安全中心搜索由独立软件制造商提供给微软的注册表键和文件。这些注册表键和文件使Windows安全中心能够检测独立软件的状态。在WMI模式下,软件制造商确定其自己的产品状态,并通过WMI提供程序向Windows安全中心报告该状态。在这两种模式下,Windows安全中心都试图确定以下内容是否正确:

已经安装了防病毒程序。

防毒软件签名已更新。

对于防病毒程序,实时扫描或按需扫描是否已启用。

对于防火墙,Windows安全中心检测第三方防火墙是否已安装以及防火墙是否已打开。

因此,为了确定是否存在防病毒软件,可以使用WMI连接到root\SecurityCenter命名空间(从Windows Vista开始必须使用root\SecurityCenter2命名空间),然后查询AntiVirusProduct WMI类。

请参考以下示例代码:

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

  }
}

1
有人知道如何在Windows 7上做这个吗? - aHunter
3
在Windows 7中,您必须使用\root\SecurityCenter2命名空间。 - RRUZ
检查被接受的答案 "...最终我在本地系统驱动程序和进程上执行了字典搜索,寻找已知反病毒签名的模式(例如文件夹名称、进程名称等...)"。 - RRUZ
@RRUZ 是的,那是最后的办法...我希望能找到另一种方法来做这件事... - bazzinga
有没有办法在Win 7 @RRUZ中获取防病毒软件版本更新信息? - TechBrkTru
显示剩余6条评论

8

使用记事本打开C:\Windows\System32\wbem\wscenter.mof,可以帮助你查看存在哪些命名空间和类:


C#查询:

// SELECT * FROM AntiVirusProduct
// SELECT * FROM FirewallProduct
// SELECT * FROM AntiSpywareProduct
ManagementObjectSearcher wmiData = new ManagementObjectSearcher(@"root\SecurityCenter2", "SELECT * FROM AntiVirusProduct");
ManagementObjectCollection data = wmiData.Get();

foreach (ManagementObject virusChecker in data)
{
    var virusCheckerName = virusChecker["displayName"];
}

wscenter.mof:

#pragma autorecover
#pragma classflags(64)
#pragma namespace("\\\\.\\root")

[NamespaceSecuritySDDL("O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464D:(A;CI;0x1;;;BU)(A;CI;0x1;;;BA)(A;CI;0x1;;;NS)(A;CI;0x1;;;LS)(A;CI;0x1;;;AU)(A;CI;0x6001D;;;S-1-5-80-3232712927-1625117661-2590453128-1738570065-3637376297)")] 
Instance of __namespace
{
  Name = "SecurityCenter";
};

[NamespaceSecuritySDDL("O:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464G:S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464D:(A;CI;0x1;;;BU)(A;CI;0x1;;;BA)(A;CI;0x1;;;NS)(A;CI;0x1;;;LS)(A;CI;0x1;;;AU)(A;CI;0x6001D;;;S-1-5-80-3232712927-1625117661-2590453128-1738570065-3637376297)")] 
Instance of __namespace
{
  Name = "SecurityCenter2";
};
#pragma namespace("\\\\.\\root\\SecurityCenter")

class AntiVirusProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  [Not_Null] boolean productUptoDate;
  boolean onAccessScanningEnabled;
  boolean productHasNotifiedUser;
  boolean productWantsWscNotifications;
  uint8 productState;
  string companyName;
  string versionNumber;
  string pathToSignedProductExe;
};

class FirewallProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  boolean enabled;
  boolean productHasNotifiedUser;
  boolean productWantsWscNotifications;
  uint8 productState;
  string companyName;
  string versionNumber;
  string pathToSignedProductExe;
};

class AntiSpywareProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  [Not_Null] boolean productUptoDate;
  boolean productEnabled;
  boolean productHasNotifiedUser;
  boolean productWantsWscNotifications;
  uint8 productState;
  string companyName;
  string versionNumber;
  string pathToSignedProductExe;
};
#pragma namespace("\\\\.\\root\\SecurityCenter2")

class AntiVirusProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  [Not_Null] string pathToSignedProductExe;
  [Not_Null] string pathToSignedReportingExe;
  [Not_Null] uint32 productState;
  string timestamp;
};

class FirewallProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  [Not_Null] string pathToSignedProductExe;
  [Not_Null] string pathToSignedReportingExe;
  [Not_Null] uint32 productState;
  string timestamp;
};

class AntiSpywareProduct
{
  [key,Not_Null] string instanceGuid;
  [Not_Null] string displayName;
  [Not_Null] string pathToSignedProductExe;
  [Not_Null] string pathToSignedReportingExe;
  [Not_Null] uint32 productState;
  string timestamp;
};
#pragma autorecover

3

在Vista SP2及更高版本中,WMI查询略有变化。

请尝试使用以下部分:\root\SecurityCenter2,而不是\root\SecurityCenter。

结果也略有不同。您仍然可以获取显示名称,但需要对ProductState字段进行一些位掩码操作,以确定onAccessScanner是否已启用/禁用以及最新信息的种类。


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