OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/safe_browsing/incident_reporting/module_load_analyzer.h " | |
6 | |
7 #include "base/file_version_info.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/logging.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/install_verification/win/module_info.h" | |
16 #include "chrome/browser/install_verification/win/module_verification_common.h" | |
17 #include "chrome/browser/safe_browsing/incident_reporting/incident_receiver.h" | |
18 #include "chrome/browser/safe_browsing/incident_reporting/suspicious_module_inci dent.h" | |
19 #include "chrome/browser/safe_browsing/path_sanitizer.h" | |
20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
21 #include "chrome/common/safe_browsing/binary_feature_extractor.h" | |
22 #include "chrome/common/safe_browsing/csd.pb.h" | |
23 #include "crypto/sha2.h" | |
24 | |
25 #if defined(SAFE_BROWSING_DB_LOCAL) | |
26 #include "chrome/browser/safe_browsing/local_database_manager.h" | |
27 #elif defined(SAFE_BROWSING_DB_REMOTE) | |
28 #include "chrome/browser/safe_browsing/remote_database_manager.h" | |
29 #endif | |
30 | |
31 namespace safe_browsing { | |
32 | |
33 // Retrieves the set of suspicious modules that are loaded in the process. | |
34 void ModuleLoadAnalyzer::GetLoadedSuspiciousModulesOnIOThread() { | |
35 std::set<ModuleInfo> module_info_set; | |
36 if (!GetLoadedModules(&module_info_set)) | |
37 return; | |
38 | |
39 std::set<base::string16>* suspicious_names = new std::set<base::string16>(); | |
robertshield
2016/02/05 21:20:32
could you stick this right away in a scoped_ptr? b
proberge
2016/02/05 22:26:10
Great catch, curently I would leak the set if susp
| |
40 | |
41 std::set<ModuleInfo>::const_iterator module_iter(module_info_set.begin()); | |
42 for (; module_iter != module_info_set.end(); ++module_iter) { | |
43 base::string16 module_file_name(base::ToLowerASCII( | |
44 base::FilePath(module_iter->name).BaseName().value())); | |
45 | |
46 // If not whitelisted. | |
47 if (!database_manager_->MatchModuleWhitelistString( | |
48 base::UTF16ToUTF8(module_file_name))) | |
49 suspicious_names->insert(module_iter->name); | |
50 } | |
51 | |
52 if (!suspicious_names->empty()) { | |
53 scoped_refptr<base::TaskRunner> task_runner = | |
54 content::BrowserThread::GetBlockingPool() | |
55 ->GetTaskRunnerWithShutdownBehavior( | |
56 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
57 | |
58 task_runner->PostTask( | |
59 FROM_HERE, | |
60 base::Bind(&ModuleLoadAnalyzer::ReportIncidentsForSuspiciousModules, | |
61 base::Unretained(this), | |
62 base::Passed(make_scoped_ptr(suspicious_names)))); | |
63 } | |
64 } | |
65 | |
66 void ModuleLoadAnalyzer::ReportIncidentsForSuspiciousModules( | |
67 scoped_ptr<std::set<base::string16>> module_names) { | |
68 PathSanitizer path_sanitizer; | |
69 scoped_ptr<IncidentReceiver> incident_receiver = | |
70 std::move(incident_receiver_); | |
71 | |
72 for (const auto& module_name : *module_names) { | |
73 if (abort_reporting_) | |
74 return; | |
75 | |
76 // TODO(proberge): Skip over modules that have already been reported. | |
77 | |
78 scoped_ptr<ClientIncidentReport_IncidentData_SuspiciousModuleIncident> | |
79 suspicious_module( | |
80 new ClientIncidentReport_IncidentData_SuspiciousModuleIncident()); | |
81 | |
82 const base::FilePath module_path(module_name); | |
83 | |
84 // Sanitized path. | |
85 base::FilePath sanitized_path(module_path); | |
86 path_sanitizer.StripHomeDirectory(&sanitized_path); | |
87 suspicious_module->set_path(base::WideToUTF8(sanitized_path.value())); | |
88 | |
89 // Digest. | |
90 scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor( | |
91 new BinaryFeatureExtractor()); | |
92 base::TimeTicks start_time = base::TimeTicks::Now(); | |
93 binary_feature_extractor->ExtractDigest( | |
94 module_path, suspicious_module->mutable_digest()); | |
95 UMA_HISTOGRAM_TIMES("SBIRS.BLAHashTime", | |
robertshield
2016/02/05 21:20:32
This will fire a histogram for each item in the mo
proberge
2016/02/05 22:26:10
Yes. Removed the existing histograms (used by the
| |
96 base::TimeTicks::Now() - start_time); | |
97 | |
98 // Version. | |
99 scoped_ptr<FileVersionInfo> version_info( | |
100 FileVersionInfo::CreateFileVersionInfo(module_path)); | |
101 if (version_info) { | |
102 std::wstring file_version = version_info->file_version(); | |
103 if (!file_version.empty()) | |
104 suspicious_module->set_version(base::WideToUTF8(file_version)); | |
105 } | |
106 | |
107 // Signature. | |
108 start_time = base::TimeTicks::Now(); | |
109 binary_feature_extractor->CheckSignature( | |
110 module_path, suspicious_module->mutable_signature()); | |
111 UMA_HISTOGRAM_TIMES("SBIRS.BLASignatureTime", | |
112 base::TimeTicks::Now() - start_time); | |
113 | |
114 // Image headers. | |
115 if (!binary_feature_extractor->ExtractImageFeatures( | |
116 module_path, BinaryFeatureExtractor::kDefaultOptions, | |
117 suspicious_module->mutable_image_headers(), | |
118 nullptr /* signed_data */)) { | |
119 suspicious_module->clear_image_headers(); | |
120 } | |
121 | |
122 // Send the report. | |
123 incident_receiver->AddIncidentForProcess(make_scoped_ptr( | |
124 new SuspiciousModuleIncident(std::move(suspicious_module)))); | |
125 } | |
126 } | |
127 | |
128 void ModuleLoadAnalyzer::VerifyModuleLoadState( | |
129 scoped_ptr<IncidentReceiver> incident_receiver) { | |
130 incident_receiver_ = std::move(incident_receiver); | |
robertshield
2016/02/05 21:20:32
keeping the incident_receiver_ in a local var that
proberge
2016/02/05 22:26:10
Done.
| |
131 | |
132 // PostTaskAndReply doesn't work here because we're in a sequenced blocking | |
133 // thread pool. | |
134 content::BrowserThread::PostTask( | |
135 content::BrowserThread::IO, FROM_HERE, | |
136 base::Bind(&ModuleLoadAnalyzer::GetLoadedSuspiciousModulesOnIOThread, | |
137 base::Unretained(this))); | |
138 } | |
139 | |
140 } // namespace safe_browsing | |
OLD | NEW |