VBScriptを使用して企業ネットワーク上のウイルス対策のステータスを確認する

まえがき



それはすべて、監査システム用に購入した企業向けソリューションが使用済みのウイルス対策製品に関する必要な情報を提供せず、動作が長すぎたため、ウイルス対策会社が使用するコントロールパネルに多くの要望が残っているという事実から始まりました。 「松葉杖」を使用して、ドメイン内のアンチウイルスに関する情報を収集することが決定されました。



以下のスクリプトは、企業ネットワークで使用されるウイルス対策のみのステータスステータスを処理します。



正確に必要なものとその方法



まず、ウイルス対策から何を取得する必要があるか、およびドメイン全体から短時間でそれを実行する方法を確立しました。



以下を受け取る必要がありました。



1)インストールされているウイルス対策の名前

2)ウイルス対策がアクティブか

3)データベースは更新されていますか



実際には、メソッドは即座に見つかりました-WMI (Windows Management Instrumentation)を使用します。



私はPC上のWMIの構造そのものの研究に飛び込む必要がありました。WMIToolsユーティリティのすばらしいセットがこれを助けてくれました。



その後、ためらうことなくWMIと対話する方法を決定する必要があったため、VBScriptでスクリプトを作成することにしました。



デブリーフィング



スクリプトは約1時間で3,500のホストを処理します。 実際、スクリプトコード自体:



スクリプトコード
WScript.Interactive = true compid = 0 On Error Resume Next Set objDomain = GetObject("LDAP://DOMAIN/OU=Workstations,DC=DOMAIN") comps = Array() for each objDomainItem in objDomain if objDomainItem.objectClass = "Computer" then idxLast = UBound (comps) ReDim Preserve comps(idxLast + 1) comps(idxLast + 1) = objDomainItem.dNSHostName end if next Set objFS = CreateObject("Scripting.FileSystemObject") Set objNewFile = objFS.CreateTextFile("C:\TEMP\AV_Check\Reports\AV_Status_Scan_is_Running.WAIT") a = "<style>" a = a& "@import ""TableFilter/filtergrid.css"";" a = a& "BODY{background-color:Lavender ;}" a = a& "TABLE{font-size: 10pt; font-family: arial;}" a = a& "TH{background-color: buttonface; }" a = a& "</style>" objNewFile.WriteLine "<html>" objNewFile.WriteLine "<head>" objNewFile.WriteLine "<script language=""javascript"" type=""text/javascript"" src=""TableFilter/tablefilter.js""></script>" objNewFile.WriteLine "<title>AntiVirus status information</title>" objNewFile.WriteLine a & "</head><body>" objNewFile.WriteLine "<h2>AVSI -- Date: " & Now() & "</h2>" objNewFile.WriteLine "<script type=""text/javascript"">" objNewFile.WriteLine "var tableToExcel = (function() {" objNewFile.WriteLine " var uri = 'data:application/vnd.ms-excel;base64,'" objNewFile.WriteLine " , template = '<html xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel""><head></head><body><table>{table}</table></body></html>'" objNewFile.WriteLine " , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }" objNewFile.WriteLine " , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }" objNewFile.WriteLine "return function(table, name) {" objNewFile.WriteLine " if (!table.nodeType) table = document.getElementById(table)" objNewFile.WriteLine " var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}" objNewFile.WriteLine " window.location.href = uri + base64(format(template, ctx))" objNewFile.WriteLine "}" objNewFile.WriteLine "})()" objNewFile.WriteLine "</script>" objNewFile.WriteLine "<input type=""button"" onclick=""tableToExcel('table1', 'Export data')"" value=""Export data to Excel"">" objNewFile.WriteLine "<table id=""table1"" BORDER=""1"" width=""100%"">" objNewFile.WriteLine "<tr><th width=""2%"">id</th><th>Computer</th><th>AV Name</th><th>AV Status</th><th>AV Bases</th><th>Host Status</th></tr>" for each comp in comps compid = compid + 1 Set WshShell = WScript.CreateObject("WScript.Shell") Ping = WshShell.Run("ping -n 1 " & comp, 0, True) Select Case Ping Case 0 On Error Resume next Set oWMI = GetObject("winmgmts:\\" & comp & "\root\SecurityCenter2") On Error Resume next Set colAVItems = oWMI.ExecQuery("Select * from AntiVirusProduct") If colAVItems.count = 0 Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th><font color=""red"">No AntiViruses found</font></th><th><font color=""red"">Disabled</font></th><th><font color=""red"">NOT Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf colAVItems.count = 1 Then For Each AntiVirus in colAVItems If (AntiVirus.displayName) <> "Windows Defender" Then AVStatus = hex(AntiVirus.ProductState) If (AVStatus = "61000") _ OR (AVStatus = "51000") _ OR (AVStatus = "41000") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""green"">Active</font></th><th><font color=""green"">Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf (AVStatus = "41010") _ OR (AVStatus = "61010") _ OR (AVStatus = "51010") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""green"">Active</font></th><th><font color=""red"">NOT Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf (AVStatus = "60000") _ OR (AVStatus = "50000") _ OR (AVStatus = "40000") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""red"">On Access scanning disabled!</font></th><th><font color=""green"">Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" Else objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""green"">Online</font></th></tr>" End if End If Next End If Case 1 objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""red"">Offline</font></th></tr>" End Select Next objNewFile.WriteLine "</table>" objNewFile.WriteLine "<script language=""javascript"" type=""text/javascript"">" objNewFile.WriteLine "//<![CDATA[" objNewFile.WriteLine "var tableFilters = {" objNewFile.WriteLine " sort_select: true," objNewFile.WriteLine " loader: true," objNewFile.WriteLine " rows_counter: true," objNewFile.WriteLine " col_2: ""select""," objNewFile.WriteLine " col_3: ""select""," objNewFile.WriteLine " col_4: ""select""," objNewFile.WriteLine " col_5: ""select""," objNewFile.WriteLine " on_change: true," objNewFile.WriteLine " display_all_text: "" [ Show all ] ""," objNewFile.WriteLine " alternate_rows: true," objNewFile.WriteLine "}" objNewFile.WriteLine "setFilterGrid( ""table1"",tableFilters);" objNewFile.WriteLine "//]]>" objNewFile.WriteLine "</script>" objNewFile.WriteLine "<h2>End of scan: " & Now() & "</h2>" objNewFile.WriteLine "</body>" objNewFile.WriteLine "</html>" objNewFile.Close objFS.MoveFile "C:\TEMP\AV_Check\Reports\AV_Status_Scan_is_Running.WAIT", "C:\TEMP\AV_Check\Reports\AV_Status_" & Date & "_" & Hour(Now()) & "." & Minute(Now()) & ".htm"
      
      







実際に何が起こっていますか? そして、これがまさに起こることです。



1)ドメインに接続し、Workstationsディレクトリに移動します。



LDAP経由のドメイン接続
 Set objDomain = GetObject("LDAP://DOMAIN/OU=Workstations,DC=DOMAIN")
      
      







2)対応するComputerクラスを持つユーザーPCが追加される空の配列を作成します。



配列の作成と移植
 for each objDomainItem in objDomain if objDomainItem.objectClass = "Computer" then idxLast = UBound (comps) ReDim Preserve comps(idxLast + 1) comps(idxLast + 1) = objDomainItem.dNSHostName end if next
      
      







3)レポートファイルを作成します。 レポートを.htm形式で生成することが決定されたため、スクリプト自体でページを形成します。



htmページの形成
 Set objFS = CreateObject("Scripting.FileSystemObject") Set objNewFile = objFS.CreateTextFile("C:\TEMP\AV_Check\Reports\AV_Status_Scan_is_Running.WAIT") a = "<style>" a = a& "@import ""TableFilter/filtergrid.css"";" a = a& "BODY{background-color:Lavender ;}" a = a& "TABLE{font-size: 10pt; font-family: arial;}" a = a& "TH{background-color: buttonface; }" a = a& "</style>" objNewFile.WriteLine "<html>" objNewFile.WriteLine "<head>" objNewFile.WriteLine "<script language=""javascript"" type=""text/javascript"" src=""TableFilter/tablefilter.js""></script>" objNewFile.WriteLine "<title>AntiVirus status information</title>" objNewFile.WriteLine a & "</head><body>" objNewFile.WriteLine "<h2>AVSI -- Date: " & Now() & "</h2>" objNewFile.WriteLine "<script type=""text/javascript"">" objNewFile.WriteLine "var tableToExcel = (function() {" objNewFile.WriteLine " var uri = 'data:application/vnd.ms-excel;base64,'" objNewFile.WriteLine " , template = '<html xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel""><head></head><body><table>{table}</table></body></html>'" objNewFile.WriteLine " , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }" objNewFile.WriteLine " , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }" objNewFile.WriteLine "return function(table, name) {" objNewFile.WriteLine " if (!table.nodeType) table = document.getElementById(table)" objNewFile.WriteLine " var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}" objNewFile.WriteLine " window.location.href = uri + base64(format(template, ctx))" objNewFile.WriteLine "}" objNewFile.WriteLine "})()" objNewFile.WriteLine "</script>" objNewFile.WriteLine "<input type=""button"" onclick=""tableToExcel('table1', 'Export data')"" value=""Export data to Excel"">" objNewFile.WriteLine "<table id=""table1"" BORDER=""1"" width=""100%"">" objNewFile.WriteLine "<tr><th width=""2%"">id</th><th>Computer</th><th>AV Name</th><th>AV Status</th><th>AV Bases</th><th>Host Status</th></tr>" { ... } objNewFile.WriteLine "</table>" objNewFile.WriteLine "<script language=""javascript"" type=""text/javascript"">" objNewFile.WriteLine "//<![CDATA[" objNewFile.WriteLine "var tableFilters = {" objNewFile.WriteLine " sort_select: true," objNewFile.WriteLine " loader: true," objNewFile.WriteLine " rows_counter: true," objNewFile.WriteLine " col_2: ""select""," objNewFile.WriteLine " col_3: ""select""," objNewFile.WriteLine " col_4: ""select""," objNewFile.WriteLine " col_5: ""select""," objNewFile.WriteLine " on_change: true," objNewFile.WriteLine " display_all_text: "" [ Show all ] ""," objNewFile.WriteLine " alternate_rows: true," objNewFile.WriteLine "}" objNewFile.WriteLine "setFilterGrid( ""table1"",tableFilters);" objNewFile.WriteLine "//]]>" objNewFile.WriteLine "</script>" objNewFile.WriteLine "<h2>End of scan: " & Now() & "</h2>" objNewFile.WriteLine "</body>" objNewFile.WriteLine "</html>" objNewFile.Close
      
      







便宜上、 HTMLテーブルフィルタージェネレーターを使用しました。これは、フィールドid、コンピューター、AV名、AVステータス、AVベースで生成されたテーブルのスクリプトの完了時にフィルターを生成します。



また、いくつかのフォーラムで見つけたExcelでテーブルをアンロードするためのボタンが追加されました(正直なところ、私はそれを気にせず、そのままにしておきました。それは曲がって動作し、練習が示しているように、FireFoxでのみ)。



4)作成してメモリに格納した配列。ここで、レポートテーブルの入力を検討します。 まず、配列内の各ホストにidを割り当てるカウンターを設定します。 その後、シェルスクリプトを呼び出して、1つのicmpパケットを送信してホストの可用性を確認します。 応答を処理するための条件を作成します。ケース0-ホストが利用可能で、データ収集スクリプトを実行してテーブルを埋めます。ケース1-ホストが利用できないデータをテーブルに表示します。



ホストの可用性を確認する
 for each comp in comps compid = compid + 1 Set WshShell = WScript.CreateObject("WScript.Shell") Ping = WshShell.Run("ping -n 1 " & comp, 0, True) Select Case Ping Case 0 { ... } Case 1 objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""red"">Offline</font></th></tr>" End Select Next
      
      







5)エラーがあるかどうかを確認し、エラーがある場合は、次のホストに進みます。 WMIのウイルス対策、スパイウェア対策、およびファイアウォールに関する情報は\ root \ SecurityCenter2ディレクトリに格納されているため、それに接続してWQLクエリを作成します。これにより、製品について必要な情報が実際に取り出されます。 ホストにアンチウイルスが存在するかどうかを確認し、存在する場合はWindows Defenderであるかどうかを確認し、存在する場合でも無視します。 各ウイルス対策には独自のステータスコードがあり、これについてはフォーラムで学びました。



アンチウイルスのステータスを確認し、表に記入する
 On Error Resume next Set oWMI = GetObject("winmgmts:\\" & comp & "\root\SecurityCenter2") On Error Resume next Set colAVItems = oWMI.ExecQuery("Select * from AntiVirusProduct") If colAVItems.count = 0 Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th><font color=""red"">No AntiViruses found</font></th><th><font color=""red"">Disabled</font></th><th><font color=""red"">NOT Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf colAVItems.count = 1 Then For Each AntiVirus in colAVItems If (AntiVirus.displayName) <> "Windows Defender" Then AVStatus = hex(AntiVirus.ProductState) If (AVStatus = "61000") _ OR (AVStatus = "51000") _ OR (AVStatus = "41000") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""green"">Active</font></th><th><font color=""green"">Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf (AVStatus = "41010") _ OR (AVStatus = "61010") _ OR (AVStatus = "51010") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""green"">Active</font></th><th><font color=""red"">NOT Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" ElseIf (AVStatus = "60000") _ OR (AVStatus = "50000") _ OR (AVStatus = "40000") Then objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""red"">On Access scanning disabled!</font></th><th><font color=""green"">Up to Date</font></th><th><font color=""green"">Online</font></th></tr>" Else objNewFile.WriteLine "<tr><th>" & compid & "</th><th>" & comp & "</th><th>" & AntiVirus.displayName & "</th><th><font color=""purple"">Unknown</font></th><th><font color=""purple"">Unknown</font></th><th><font color=""green"">Online</font></th></tr>" End if End If Next End If
      
      







ボーナス



まあ、そしてPythonのボーナスコードとして。 Excelへのアップロードに使用できるテキストファイルを作成します(列の区切りとして-集計)。 残念ながら、LDAPを使用してホストのリストをアンロードできませんでした。大きなリストを使用すると、スクリプトがクラッシュし、誰かがニーズに合わせてファイナライズする可能性があります。



コード自体
 import wmi import codecs import os with open('Comp_list.txt','r') as list: file = codecs.open('text.txt', 'w', 'utf-8') file.write("Computer" + " AV Name" + " Host Status" + " AV Status" + " AV Bases\n") for comp in list: response = os.system("ping -n 1 " + comp) if response == 0: path = '//%s/root/SecurityCenter2' % comp c = wmi.WMI(moniker=path) wql = "Select * from AntiVirusProduct" wql = c.query(wql) if wql == []: file.write(comp + " no AntiVirus found" + " Online" + " Unknown" + " Unknown\n") else: for AntiVirus in wql: ProductState_in_hex = str(hex(AntiVirus.ProductState)) check_install = ProductState_in_hex[0:3] check_state = ProductState_in_hex[3:5] check_updates = ProductState_in_hex[5:7] if check_state == "10" and check_updates == "00": file.write(comp + " " + AntiVirus.displayName + " Online" + " Active" + " Up to Date\n") elif check_state == "10" and check_updates == "10": file.write(comp + " " + AntiVirus.displayName + " Online" + " Active" + " NOT Up to Date\n") elif check_state == "00" and check_updates == "00": file.write(comp + " " + AntiVirus.displayName + " Online" + " On Access scanning disabled!" + " Up to Date\n") elif check_state == "00" and check_updates == "00": file.write(comp + " " + AntiVirus.displayName + " Online" + " On Access scanning disabled!" + " NOT Up to Date\n") else: file.write(comp + " " + AntiVirus.displayName + " Online" + " Unknown state\n" + " " + check_install) else: file.write(comp + " Unknown" + " Offline" + " Unknown" + " Unknown\n") file.close()
      
      








All Articles