使用C#在Windows上检测防病毒

时间:2009-08-26 01:22:25

标签: c# windows antivirus

有没有办法检测使用C#的计算机中是否安装了防病毒软件?我知道安全中心会检测防病毒软件但你怎么能在C#中检测到它?

3 个答案:

答案 0 :(得分:32)

根据Microsoft的说法,Windows安全中心使用两层方法来检测状态。一层是手动的,另一层是通过Windows Management Instrumentation(WMI)自动完成的。在手动检测模式下,Windows安全中心会搜索由独立软件制造商提供给Microsoft的注册表项和文件。这些注册表项和文件使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 :(得分:4)

通过记事本打开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

答案 2 :(得分:3)

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

试试这个部分\ root \ SecurityCenter2 而不是\ root \ SecurityCenter

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