OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/safe_browsing/environment_data_collection_win.h" | 5 #include "chrome/browser/safe_browsing/environment_data_collection_win.h" |
6 | 6 |
| 7 #include <windows.h> |
| 8 #include <set> |
| 9 |
| 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/install_verification/win/module_info.h" |
| 14 #include "chrome/browser/install_verification/win/module_verification_common.h" |
| 15 #include "chrome/browser/net/service_providers_win.h" |
| 16 #include "chrome/browser/safe_browsing/path_sanitizer.h" |
7 #include "chrome/common/safe_browsing/csd.pb.h" | 17 #include "chrome/common/safe_browsing/csd.pb.h" |
8 | 18 |
9 namespace safe_browsing { | 19 namespace safe_browsing { |
10 | 20 |
| 21 namespace { |
| 22 |
| 23 // Helper function for expanding all environment variables in |path|. |
| 24 base::string16 ExpandEnvironmentVariables(const base::string16& path) { |
| 25 static const DWORD kMaxBuffer = 32 * 1024; // Max according to MSDN. |
| 26 base::string16 path_expanded; |
| 27 DWORD path_len = MAX_PATH; |
| 28 do { |
| 29 DWORD result = ExpandEnvironmentStrings( |
| 30 path.c_str(), WriteInto(&path_expanded, path_len), path_len); |
| 31 if (!result) { |
| 32 // Failed to expand variables. Return the original string. |
| 33 DPLOG(ERROR) << path; |
| 34 break; |
| 35 } |
| 36 if (result <= path_len) |
| 37 return path_expanded.substr(0, result - 1); |
| 38 path_len = result; |
| 39 } while (path_len < kMaxBuffer); |
| 40 |
| 41 return path; |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 bool CollectDlls(ClientIncidentReport_EnvironmentData_Process* process) { |
| 47 // Retrieve the module list. |
| 48 std::set<ModuleInfo> loaded_modules; |
| 49 if (!GetLoadedModules(&loaded_modules)) |
| 50 return false; |
| 51 |
| 52 // Sanitize path of each module and add it to the incident report. |
| 53 PathSanitizer path_sanitizer; |
| 54 for (std::set<ModuleInfo>::const_iterator it = loaded_modules.begin(); |
| 55 it != loaded_modules.end(); |
| 56 ++it) { |
| 57 base::FilePath dll_path(it->name); |
| 58 path_sanitizer.StripHomeDirectory(&dll_path); |
| 59 |
| 60 ClientIncidentReport_EnvironmentData_Process_Dll* dll = process->add_dll(); |
| 61 dll->set_path(base::WideToUTF8(dll_path.value())); |
| 62 dll->set_base_address(it->base_address); |
| 63 dll->set_length(it->size); |
| 64 } |
| 65 |
| 66 return true; |
| 67 } |
| 68 |
| 69 void RecordLspFeature(ClientIncidentReport_EnvironmentData_Process* process) { |
| 70 WinsockLayeredServiceProviderList lsp_list; |
| 71 GetWinsockLayeredServiceProviders(&lsp_list); |
| 72 |
| 73 // For each LSP, we extract and sanitize the path. |
| 74 PathSanitizer path_sanitizer; |
| 75 std::set<base::string16> lsp_paths; |
| 76 for (size_t i = 0; i < lsp_list.size(); ++i) { |
| 77 base::FilePath lsp_path(ExpandEnvironmentVariables(lsp_list[i].path)); |
| 78 path_sanitizer.StripHomeDirectory(&lsp_path); |
| 79 lsp_paths.insert(lsp_path.value()); |
| 80 } |
| 81 |
| 82 // Look for a match between LSPs and loaded dlls. |
| 83 for (int i = 0; i < process->dll_size(); ++i) { |
| 84 if (lsp_paths.count(base::UTF8ToWide(process->dll(i).path()))) { |
| 85 process->mutable_dll(i) |
| 86 ->add_feature(ClientIncidentReport_EnvironmentData_Process_Dll::LSP); |
| 87 } |
| 88 } |
| 89 } |
| 90 |
11 void CollectPlatformProcessData( | 91 void CollectPlatformProcessData( |
12 ClientIncidentReport_EnvironmentData_Process* process) { | 92 ClientIncidentReport_EnvironmentData_Process* process) { |
13 // TODO(pmonette): collect dlls and lsps. | 93 CollectDlls(process); |
| 94 RecordLspFeature(process); |
14 } | 95 } |
15 | 96 |
16 } // namespace safe_browsing | 97 } // namespace safe_browsing |
OLD | NEW |