| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/antivirus_metrics_provider_win.h" | 5 #include "chrome/browser/metrics/antivirus_metrics_provider_win.h" |
| 6 | 6 |
| 7 #include <iwscapi.h> | 7 #include <iwscapi.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <wbemidl.h> |
| 9 #include <windows.h> | 10 #include <windows.h> |
| 10 #include <wscapi.h> | 11 #include <wscapi.h> |
| 11 | 12 |
| 12 #include <string> | 13 #include <string> |
| 13 | 14 |
| 14 #include "base/bind.h" | 15 #include "base/bind.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/feature_list.h" | 17 #include "base/feature_list.h" |
| 17 #include "base/file_version_info_win.h" | 18 #include "base/file_version_info_win.h" |
| 18 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
| 19 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
| 20 #include "base/metrics/field_trial.h" | 21 #include "base/metrics/field_trial.h" |
| 21 #include "base/metrics/histogram.h" | 22 #include "base/metrics/histogram.h" |
| 22 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 23 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 24 #include "base/strings/sys_string_conversions.h" | 25 #include "base/strings/sys_string_conversions.h" |
| 25 #include "base/task_runner_util.h" | 26 #include "base/task_runner_util.h" |
| 26 #include "base/threading/thread_restrictions.h" | 27 #include "base/threading/thread_restrictions.h" |
| 27 #include "base/version.h" | 28 #include "base/version.h" |
| 28 #include "base/win/scoped_bstr.h" | 29 #include "base/win/scoped_bstr.h" |
| 29 #include "base/win/scoped_com_initializer.h" | 30 #include "base/win/scoped_com_initializer.h" |
| 30 #include "base/win/scoped_comptr.h" | 31 #include "base/win/scoped_comptr.h" |
| 32 #include "base/win/scoped_variant.h" |
| 31 #include "base/win/windows_version.h" | 33 #include "base/win/windows_version.h" |
| 32 #include "chrome/common/channel_info.h" | 34 #include "chrome/common/channel_info.h" |
| 33 #include "components/metrics/proto/system_profile.pb.h" | 35 #include "components/metrics/proto/system_profile.pb.h" |
| 34 #include "components/variations/metrics_util.h" | 36 #include "components/variations/metrics_util.h" |
| 35 #include "components/version_info/version_info.h" | 37 #include "components/version_info/version_info.h" |
| 36 | 38 |
| 37 namespace { | 39 namespace { |
| 38 | 40 |
| 41 // This is an undocumented structure returned from querying the "productState" |
| 42 // uint32 from the AntiVirusProduct in WMI. |
| 43 // http://neophob.com/2010/03/wmi-query-windows-securitycenter2/ gives a good |
| 44 // summary and testing was also done with a variety of AV products to determine |
| 45 // these values as accurately as possible. |
| 46 #pragma pack(push) |
| 47 #pragma pack(1) |
| 48 struct PRODUCT_STATE { |
| 49 uint8_t unknown_1 : 4; |
| 50 uint8_t definition_state : 4; // 1 = Out of date, 0 = Up to date. |
| 51 uint8_t unknown_2 : 4; |
| 52 uint8_t security_state : 4; // 0 = Inactive, 1 = Active, 2 = Snoozed. |
| 53 uint8_t security_provider; // matches WSC_SECURITY_PROVIDER in wscapi.h. |
| 54 uint8_t unknown_3; |
| 55 }; |
| 56 #pragma pack(pop) |
| 57 |
| 58 static_assert(sizeof(PRODUCT_STATE) == 4, "Wrong packing!"); |
| 59 |
| 39 bool ShouldReportFullNames() { | 60 bool ShouldReportFullNames() { |
| 40 // The expectation is that this will be disabled for the majority of users, | 61 // The expectation is that this will be disabled for the majority of users, |
| 41 // but this allows a small group to be enabled on other channels if there are | 62 // but this allows a small group to be enabled on other channels if there are |
| 42 // a large percentage of hashes collected on these channels that are not | 63 // a large percentage of hashes collected on these channels that are not |
| 43 // resolved to names previously collected on Canary channel. | 64 // resolved to names previously collected on Canary channel. |
| 44 bool enabled = base::FeatureList::IsEnabled( | 65 bool enabled = base::FeatureList::IsEnabled( |
| 45 AntiVirusMetricsProvider::kReportNamesFeature); | 66 AntiVirusMetricsProvider::kReportNamesFeature); |
| 46 | 67 |
| 47 if (chrome::GetChannel() == version_info::Channel::CANARY) | 68 if (chrome::GetChannel() == version_info::Channel::CANARY) |
| 48 return true; | 69 return true; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 base::Bind(&AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread), | 151 base::Bind(&AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread), |
| 131 base::Bind(&AntiVirusMetricsProvider::GotAntiVirusProducts, | 152 base::Bind(&AntiVirusMetricsProvider::GotAntiVirusProducts, |
| 132 weak_ptr_factory_.GetWeakPtr(), done_callback)); | 153 weak_ptr_factory_.GetWeakPtr(), done_callback)); |
| 133 } | 154 } |
| 134 | 155 |
| 135 // static | 156 // static |
| 136 std::vector<AntiVirusMetricsProvider::AvProduct> | 157 std::vector<AntiVirusMetricsProvider::AvProduct> |
| 137 AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread() { | 158 AntiVirusMetricsProvider::GetAntiVirusProductsOnFileThread() { |
| 138 std::vector<AvProduct> av_products; | 159 std::vector<AvProduct> av_products; |
| 139 | 160 |
| 140 ResultCode result = FillAntiVirusProducts(&av_products); | 161 ResultCode result = RESULT_GENERIC_FAILURE; |
| 162 |
| 163 base::win::OSInfo* os_info = base::win::OSInfo::GetInstance(); |
| 164 |
| 165 // Windows Security Center APIs are not available on Server products. |
| 166 // See https://msdn.microsoft.com/en-us/library/bb432506.aspx. |
| 167 if (os_info->version_type() == base::win::SUITE_SERVER) { |
| 168 result = RESULT_WSC_NOT_AVAILABLE; |
| 169 } else { |
| 170 // The WSC interface is preferred here as it's fully documented, but only |
| 171 // available on Windows 8 and above, so instead use the undocumented WMI |
| 172 // interface on Windows 7 and below. |
| 173 if (os_info->version() >= base::win::VERSION_WIN8) |
| 174 result = FillAntiVirusProductsFromWSC(&av_products); |
| 175 else |
| 176 result = FillAntiVirusProductsFromWMI(&av_products); |
| 177 } |
| 141 | 178 |
| 142 UMA_HISTOGRAM_ENUMERATION("UMA.AntiVirusMetricsProvider.Result", | 179 UMA_HISTOGRAM_ENUMERATION("UMA.AntiVirusMetricsProvider.Result", |
| 143 result, | 180 result, |
| 144 RESULT_COUNT); | 181 RESULT_COUNT); |
| 145 | 182 |
| 146 return av_products; | 183 return av_products; |
| 147 } | 184 } |
| 148 | 185 |
| 149 void AntiVirusMetricsProvider::GotAntiVirusProducts( | 186 void AntiVirusMetricsProvider::GotAntiVirusProducts( |
| 150 const base::Closure& done_callback, | 187 const base::Closure& done_callback, |
| 151 const std::vector<AvProduct>& av_products) { | 188 const std::vector<AvProduct>& av_products) { |
| 152 DCHECK(thread_checker_.CalledOnValidThread()); | 189 DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 av_products_ = av_products; | 190 av_products_ = av_products; |
| 154 done_callback.Run(); | 191 done_callback.Run(); |
| 155 } | 192 } |
| 156 | 193 |
| 157 // static | 194 // static |
| 158 AntiVirusMetricsProvider::ResultCode | 195 AntiVirusMetricsProvider::ResultCode |
| 159 AntiVirusMetricsProvider::FillAntiVirusProducts( | 196 AntiVirusMetricsProvider::FillAntiVirusProductsFromWSC( |
| 160 std::vector<AvProduct>* products) { | 197 std::vector<AvProduct>* products) { |
| 161 std::vector<AvProduct> result_list; | 198 std::vector<AvProduct> result_list; |
| 162 base::ThreadRestrictions::AssertIOAllowed(); | 199 base::ThreadRestrictions::AssertIOAllowed(); |
| 163 base::win::ScopedCOMInitializer com_initializer; | 200 base::win::ScopedCOMInitializer com_initializer; |
| 164 | 201 |
| 165 if (!com_initializer.succeeded()) | 202 if (!com_initializer.succeeded()) |
| 166 return RESULT_FAILED_TO_INITIALIZE_COM; | 203 return RESULT_FAILED_TO_INITIALIZE_COM; |
| 167 | 204 |
| 168 base::win::ScopedComPtr<IWSCProductList> product_list; | 205 base::win::ScopedComPtr<IWSCProductList> product_list; |
| 169 HRESULT result = | 206 HRESULT result = |
| 170 CoCreateInstance(__uuidof(WSCProductList), NULL, CLSCTX_INPROC_SERVER, | 207 CoCreateInstance(__uuidof(WSCProductList), nullptr, CLSCTX_INPROC_SERVER, |
| 171 __uuidof(IWSCProductList), product_list.ReceiveVoid()); | 208 __uuidof(IWSCProductList), product_list.ReceiveVoid()); |
| 172 if (FAILED(result)) | 209 if (FAILED(result)) |
| 173 return RESULT_FAILED_TO_CREATE_INSTANCE; | 210 return RESULT_FAILED_TO_CREATE_INSTANCE; |
| 174 | 211 |
| 175 result = product_list->Initialize(WSC_SECURITY_PROVIDER_ANTIVIRUS); | 212 result = product_list->Initialize(WSC_SECURITY_PROVIDER_ANTIVIRUS); |
| 176 if (FAILED(result)) | 213 if (FAILED(result)) |
| 177 return RESULT_FAILED_TO_INITIALIZE_PRODUCT_LIST; | 214 return RESULT_FAILED_TO_INITIALIZE_PRODUCT_LIST; |
| 178 | 215 |
| 179 LONG product_count; | 216 LONG product_count; |
| 180 result = product_list->get_Count(&product_count); | 217 result = product_list->get_Count(&product_count); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 av_product.set_product_version_hash(metrics::HashName(product_version)); | 285 av_product.set_product_version_hash(metrics::HashName(product_version)); |
| 249 } | 286 } |
| 250 | 287 |
| 251 result_list.push_back(av_product); | 288 result_list.push_back(av_product); |
| 252 } | 289 } |
| 253 | 290 |
| 254 *products = std::move(result_list); | 291 *products = std::move(result_list); |
| 255 | 292 |
| 256 return RESULT_SUCCESS; | 293 return RESULT_SUCCESS; |
| 257 } | 294 } |
| 295 |
| 296 AntiVirusMetricsProvider::ResultCode |
| 297 AntiVirusMetricsProvider::FillAntiVirusProductsFromWMI( |
| 298 std::vector<AvProduct>* products) { |
| 299 std::vector<AvProduct> result_list; |
| 300 base::ThreadRestrictions::AssertIOAllowed(); |
| 301 base::win::ScopedCOMInitializer com_initializer; |
| 302 |
| 303 if (!com_initializer.succeeded()) |
| 304 return RESULT_FAILED_TO_INITIALIZE_COM; |
| 305 |
| 306 base::win::ScopedComPtr<IWbemLocator> wmi_locator; |
| 307 HRESULT hr = wmi_locator.CreateInstance(CLSID_WbemLocator, nullptr, |
| 308 CLSCTX_INPROC_SERVER); |
| 309 if (FAILED(hr)) |
| 310 return RESULT_FAILED_TO_CREATE_INSTANCE; |
| 311 |
| 312 base::win::ScopedComPtr<IWbemServices> wmi_services; |
| 313 hr = wmi_locator->ConnectServer( |
| 314 base::win::ScopedBstr(L"ROOT\\SecurityCenter2"), nullptr, nullptr, |
| 315 nullptr, 0, nullptr, nullptr, wmi_services.Receive()); |
| 316 if (FAILED(hr)) |
| 317 return RESULT_FAILED_TO_CONNECT_TO_WMI; |
| 318 |
| 319 hr = ::CoSetProxyBlanket(wmi_services.get(), RPC_C_AUTHN_WINNT, |
| 320 RPC_C_AUTHZ_NONE, nullptr, RPC_C_AUTHN_LEVEL_CALL, |
| 321 RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE); |
| 322 if (FAILED(hr)) |
| 323 return RESULT_FAILED_TO_SET_SECURITY_BLANKET; |
| 324 |
| 325 // This interface is available on Windows Vista and above, and is officially |
| 326 // undocumented. |
| 327 base::win::ScopedBstr query_language(L"WQL"); |
| 328 base::win::ScopedBstr query(L"SELECT * FROM AntiVirusProduct"); |
| 329 base::win::ScopedComPtr<IEnumWbemClassObject> enumerator; |
| 330 |
| 331 hr = wmi_services->ExecQuery( |
| 332 query_language, query, |
| 333 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, nullptr, |
| 334 enumerator.Receive()); |
| 335 if (FAILED(hr)) |
| 336 return RESULT_FAILED_TO_EXEC_WMI_QUERY; |
| 337 |
| 338 // Iterate over the results of the WMI query. Each result will be an |
| 339 // AntiVirusProduct instance. |
| 340 while (true) { |
| 341 base::win::ScopedComPtr<IWbemClassObject> class_object; |
| 342 ULONG items_returned = 0; |
| 343 hr = enumerator->Next(WBEM_INFINITE, 1, class_object.Receive(), |
| 344 &items_returned); |
| 345 if (FAILED(hr)) |
| 346 return RESULT_FAILED_TO_ITERATE_RESULTS; |
| 347 |
| 348 if (hr == WBEM_S_FALSE || items_returned == 0) |
| 349 break; |
| 350 |
| 351 AvProduct av_product; |
| 352 av_product.set_product_state( |
| 353 metrics::SystemProfileProto::AntiVirusState:: |
| 354 SystemProfileProto_AntiVirusState_STATE_ON); |
| 355 |
| 356 // See definition of PRODUCT_STATE structure above for how this is being |
| 357 // used. |
| 358 base::win::ScopedVariant product_state; |
| 359 hr = class_object->Get(L"productState", 0, product_state.Receive(), 0, 0); |
| 360 |
| 361 if (FAILED(hr) || product_state.type() != VT_I4) |
| 362 return RESULT_FAILED_TO_GET_PRODUCT_STATE; |
| 363 |
| 364 LONG state_val = V_I4(product_state.ptr()); |
| 365 // Map the values from product_state to the proto values. |
| 366 switch (reinterpret_cast<PRODUCT_STATE*>(&state_val)->security_state) { |
| 367 case 0: |
| 368 av_product.set_product_state( |
| 369 metrics::SystemProfileProto::AntiVirusState:: |
| 370 SystemProfileProto_AntiVirusState_STATE_OFF); |
| 371 break; |
| 372 case 1: |
| 373 av_product.set_product_state( |
| 374 metrics::SystemProfileProto::AntiVirusState:: |
| 375 SystemProfileProto_AntiVirusState_STATE_ON); |
| 376 break; |
| 377 case 2: |
| 378 av_product.set_product_state( |
| 379 metrics::SystemProfileProto::AntiVirusState:: |
| 380 SystemProfileProto_AntiVirusState_STATE_SNOOZED); |
| 381 break; |
| 382 default: |
| 383 // unknown state. |
| 384 return RESULT_PRODUCT_STATE_INVALID; |
| 385 break; |
| 386 } |
| 387 |
| 388 base::win::ScopedVariant display_name; |
| 389 hr = class_object->Get(L"displayName", 0, display_name.Receive(), 0, 0); |
| 390 |
| 391 if (FAILED(hr) || display_name.type() != VT_BSTR) |
| 392 return RESULT_FAILED_TO_GET_PRODUCT_NAME; |
| 393 |
| 394 // Owned by ScopedVariant. |
| 395 BSTR temp_bstr = V_BSTR(display_name.ptr()); |
| 396 std::string name(base::SysWideToUTF8( |
| 397 std::wstring(temp_bstr, ::SysStringLen(temp_bstr)))); |
| 398 |
| 399 if (ShouldReportFullNames()) |
| 400 av_product.set_product_name(name); |
| 401 av_product.set_product_name_hash(metrics::HashName(name)); |
| 402 |
| 403 base::win::ScopedVariant exe_path; |
| 404 hr = class_object->Get(L"pathToSignedProductExe", 0, exe_path.Receive(), 0, |
| 405 0); |
| 406 |
| 407 if (FAILED(hr) || exe_path.type() != VT_BSTR) |
| 408 return RESULT_FAILED_TO_GET_REMEDIATION_PATH; |
| 409 |
| 410 temp_bstr = V_BSTR(exe_path.ptr()); |
| 411 std::wstring path_str(temp_bstr, ::SysStringLen(temp_bstr)); |
| 412 |
| 413 std::string product_version; |
| 414 // Not a failure if the product version cannot be read from the file on |
| 415 // disk. |
| 416 if (GetProductVersion(&path_str, &product_version)) { |
| 417 if (ShouldReportFullNames()) |
| 418 av_product.set_product_version(product_version); |
| 419 av_product.set_product_version_hash(metrics::HashName(product_version)); |
| 420 } |
| 421 |
| 422 result_list.push_back(av_product); |
| 423 } |
| 424 |
| 425 *products = std::move(result_list); |
| 426 |
| 427 return RESULT_SUCCESS; |
| 428 } |
| OLD | NEW |