web-dev-qa-db-ja.com

C#を使用してWindowsでアンチウイルスを検出する

C#を使用してマシンにウイルス対策ソフトウェアがインストールされているかどうかを検出する方法はありますか?セキュリティセンターがウイルス対策ソフトウェアを検出することは知っていますが、C#でどのように検出できますか?

19
Angel.King.47

Microsoftによると、Windowsセキュリティセンターは、ステータスの検出に2層のアプローチを使用しています。 1つの層は手動で、もう1つの層はWindows Management Instrumentation(WMI)を介して自動です。手動検出モードでは、Windowsセキュリティセンターは、独立したソフトウェアメーカーからMicrosoftに提供されたレジストリキーとファイルを検索します。これらのレジストリキーとファイルにより、Windowsセキュリティセンターは独立したソフトウェアのステータスを検出できます。 WMIモードでは、ソフトウェアメーカーは自社の製品ステータスを判断し、そのステータスをWMIプロバイダーを介してWindowsセキュリティセンターに報告します。どちらのモードでも、Windowsセキュリティセンターは次のことが当てはまるかどうかを判断しようとします。

ウイルス対策プログラムが存在します。

ウイルス対策シグネチャは最新です。

ウイルス対策プログラムでは、リアルタイムスキャンまたはオンアクセススキャンがオンになっています。

ファイアウォールの場合、Windowsセキュリティセンターは、サードパーティのファイアウォールがインストールされているかどうか、およびファイアウォールがオンになっているかどうかを検出します。

したがって、ウイルス対策ソフトウェアの存在を確認するには、WMIを使用してroot\SecurityCenter名前空間に接続し(Windows Vista以降は、root\SecurityCenter2名前空間を使用する必要があります)、クエリを実行します。 AntiVirusProductWMIクラス。

このサンプルコードを見てください

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

  }
}
32
RRUZ

開いた 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
5
Amir Saniyan

WMIクエリは、VistaSP2以降でわずかに変更されます。

\ root\SecurityCenterの代わりにこの部分\ root\SecurityCenter2を試してください

結果もわずかに異なります。表示名は引き続き取得できますが、ProductStateフィールドに対してビットマスキングを少し実行して、onAccessScannerが有効か無効か、およびupToDateの種類の情報を確認する必要があります。

3
jeff