1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/download_protection/file_analyzer.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/file_util_service.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#include "chrome/common/safe_browsing/archive_analyzer_results.h"
#include "chrome/common/safe_browsing/download_type_util.h"
#include "components/safe_browsing/content/common/file_type_policies.h"
#include "components/safe_browsing/content/common/proto/download_file_types.pb.h"
#include "components/safe_browsing/core/common/features.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"
namespace safe_browsing {
namespace {
using content::BrowserThread;
FileAnalyzer::Results ExtractFileFeatures(
scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor,
base::FilePath file_path) {
FileAnalyzer::Results results;
binary_feature_extractor->CheckSignature(file_path, &results.signature_info);
if (!binary_feature_extractor->ExtractImageFeatures(
file_path, BinaryFeatureExtractor::kDefaultOptions,
&results.image_headers, nullptr)) {
results.image_headers = ClientDownloadRequest::ImageHeaders();
}
return results;
}
} // namespace
FileAnalyzer::Results::Results() = default;
FileAnalyzer::Results::~Results() = default;
FileAnalyzer::Results::Results(const FileAnalyzer::Results& other) = default;
FileAnalyzer::FileAnalyzer(
scoped_refptr<BinaryFeatureExtractor> binary_feature_extractor)
: binary_feature_extractor_(binary_feature_extractor) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
FileAnalyzer::~FileAnalyzer() = default;
void FileAnalyzer::Start(const base::FilePath& target_file_name,
const base::FilePath& tmp_path,
base::optional_ref<const std::string> password,
base::OnceCallback<void(Results)> callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
target_file_name_ = target_file_name;
tmp_path_ = tmp_path;
password_ = password.CopyAsOptional();
callback_ = std::move(callback);
start_time_ = base::Time::Now();
#if !BUILDFLAG(IS_ANDROID)
DownloadFileType::InspectionType inspection_type =
FileTypePolicies::GetInstance()
->PolicyForFile(target_file_name_, GURL{}, nullptr)
.inspection_type();
if (inspection_type == DownloadFileType::ZIP) {
StartExtractZipFeatures();
return;
}
if (inspection_type == DownloadFileType::RAR) {
StartExtractRarFeatures();
return;
}
#if BUILDFLAG(IS_MAC)
if (inspection_type == DownloadFileType::DMG) {
StartExtractDmgFeatures();
return;
}
#endif // BUILDFLAG(IS_MAC)
if (inspection_type == DownloadFileType::SEVEN_ZIP) {
StartExtractSevenZipFeatures();
return;
}
#endif // !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_MAC)
// Checks for existence of "koly" signature even if file doesn't have
// archive-type extension, then calls ExtractFileOrDmgFeatures() with
// result.
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
base::BindOnce(DiskImageTypeSnifferMac::IsAppleDiskImage, tmp_path_),
base::BindOnce(&FileAnalyzer::ExtractFileOrDmgFeatures,
weakptr_factory_.GetWeakPtr()));
#else // BUILDFLAG(IS_MAC)
StartExtractFileFeatures();
#endif // BUILDFLAG(IS_MAC)
}
void FileAnalyzer::StartExtractFileFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&ExtractFileFeatures, binary_feature_extractor_,
tmp_path_),
base::BindOnce(&FileAnalyzer::OnFileAnalysisFinished,
weakptr_factory_.GetWeakPtr()));
}
void FileAnalyzer::OnFileAnalysisFinished(FileAnalyzer::Results results) {
LogAnalysisDurationWithAndWithoutSuffix("Executable");
results.type = download_type_util::GetDownloadType(target_file_name_);
results.inspection_performed = DownloadFileType::NONE;
std::move(callback_).Run(results);
}
#if !BUILDFLAG(IS_ANDROID)
void FileAnalyzer::StartExtractZipFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// We give the zip analyzer a weak pointer to this object.
zip_analyzer_ = SandboxedZipAnalyzer::CreateAnalyzer(
tmp_path_, password_,
base::BindOnce(&FileAnalyzer::OnZipAnalysisFinished,
weakptr_factory_.GetWeakPtr()),
LaunchFileUtilService());
zip_analyzer_->Start();
}
void FileAnalyzer::OnZipAnalysisFinished(
const ArchiveAnalyzerResults& archive_results) {
base::UmaHistogramEnumeration("SBClientDownload.ZipArchiveAnalysisResult",
archive_results.analysis_result);
LogAnalysisDurationWithAndWithoutSuffix("Zip");
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Even if !results.success, some of the zip may have been parsed.
// Some unzippers will successfully unpack archives that we cannot,
// so we're lenient here.
if (archive_results.success) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::VALID);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTimeout) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::PARSER_TIMED_OUT);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTooLarge) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::TOO_LARGE);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kDiskError) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::DISK_ERROR);
}
results_.archived_executable = archive_results.has_executable;
results_.archived_archive = archive_results.has_archive;
results_.archived_binaries =
SelectArchiveEntries(archive_results.archived_binary);
if (archive_results.has_executable) {
results_.type = ClientDownloadRequest::ZIPPED_EXECUTABLE;
} else if (archive_results.has_archive) {
results_.type = ClientDownloadRequest::ZIPPED_ARCHIVE;
} else if (!archive_results.success) {
// .zip files that look invalid to Chrome can often be successfully
// unpacked by other archive tools, so they may be a real threat.
results_.type = ClientDownloadRequest::INVALID_ZIP;
} else {
results_.type = download_type_util::GetDownloadType(target_file_name_);
}
results_.archive_summary.set_file_count(archive_results.file_count);
results_.archive_summary.set_directory_count(archive_results.directory_count);
results_.archive_summary.set_is_encrypted(
archive_results.encryption_info.is_encrypted);
results_.encryption_info = archive_results.encryption_info;
results_.inspection_performed = DownloadFileType::ZIP;
std::move(callback_).Run(std::move(results_));
}
void FileAnalyzer::StartExtractRarFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// We give the rar analyzer a weak pointer to this object. Since the
// analyzer is refcounted, it might outlive the request.
rar_analyzer_ = SandboxedRarAnalyzer::CreateAnalyzer(
tmp_path_, password_,
base::BindOnce(&FileAnalyzer::OnRarAnalysisFinished,
weakptr_factory_.GetWeakPtr()),
LaunchFileUtilService());
rar_analyzer_->Start();
}
void FileAnalyzer::OnRarAnalysisFinished(
const ArchiveAnalyzerResults& archive_results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::UmaHistogramEnumeration("SBClientDownload.RarArchiveAnalysisResult",
archive_results.analysis_result);
LogAnalysisDurationWithAndWithoutSuffix("Rar");
if (archive_results.success) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::VALID);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTimeout) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::PARSER_TIMED_OUT);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTooLarge) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::TOO_LARGE);
}
results_.archived_executable = archive_results.has_executable;
results_.archived_archive = archive_results.has_archive;
results_.archived_binaries =
SelectArchiveEntries(archive_results.archived_binary);
if (archive_results.has_executable) {
results_.type = ClientDownloadRequest::RAR_COMPRESSED_EXECUTABLE;
} else if (archive_results.has_archive) {
results_.type = ClientDownloadRequest::RAR_COMPRESSED_ARCHIVE;
} else if (!archive_results.success) {
// .rar files that look invalid to Chrome may be successfully unpacked by
// other archive tools, so they may be a real threat.
results_.type = ClientDownloadRequest::INVALID_RAR;
} else {
results_.type = download_type_util::GetDownloadType(target_file_name_);
}
results_.archive_summary.set_file_count(archive_results.file_count);
results_.archive_summary.set_directory_count(archive_results.directory_count);
results_.archive_summary.set_is_encrypted(
archive_results.encryption_info.is_encrypted);
results_.encryption_info = archive_results.encryption_info;
results_.inspection_performed = DownloadFileType::RAR;
std::move(callback_).Run(std::move(results_));
}
#endif // !BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_MAC)
// This is called for .DMGs and other files that can be parsed by
// SandboxedDMGAnalyzer.
void FileAnalyzer::StartExtractDmgFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Directly use 'dmg' extension since download file may not have any
// extension, but has still been deemed a DMG through file type sniffing.
dmg_analyzer_ = SandboxedDMGAnalyzer::CreateAnalyzer(
tmp_path_,
FileTypePolicies::GetInstance()->GetMaxFileSizeToAnalyze("dmg"),
base::BindRepeating(&FileAnalyzer::OnDmgAnalysisFinished,
weakptr_factory_.GetWeakPtr()),
LaunchFileUtilService());
dmg_analyzer_->Start();
}
void FileAnalyzer::ExtractFileOrDmgFeatures(
bool download_file_has_koly_signature) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (download_file_has_koly_signature) {
StartExtractDmgFeatures();
} else {
StartExtractFileFeatures();
}
}
void FileAnalyzer::OnDmgAnalysisFinished(
const safe_browsing::ArchiveAnalyzerResults& archive_results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::UmaHistogramEnumeration("SBClientDownload.DmgArchiveAnalysisResult",
archive_results.analysis_result);
LogAnalysisDurationWithAndWithoutSuffix("Dmg");
if (archive_results.signature_blob.size() > 0) {
results_.disk_image_signature =
std::vector<uint8_t>(archive_results.signature_blob);
}
results_.detached_code_signatures.CopyFrom(
archive_results.detached_code_signatures);
// Even if !results.success, some of the DMG may have been parsed.
results_.archived_executable = archive_results.has_executable;
results_.archived_archive = archive_results.has_archive;
results_.archived_binaries =
SelectArchiveEntries(archive_results.archived_binary);
if (archive_results.success) {
results_.type = ClientDownloadRequest::MAC_EXECUTABLE;
} else {
results_.type = ClientDownloadRequest::MAC_ARCHIVE_FAILED_PARSING;
}
if (archive_results.success) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::VALID);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTimeout) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::PARSER_TIMED_OUT);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTooLarge) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::TOO_LARGE);
}
results_.archive_summary.set_is_encrypted(
archive_results.encryption_info.is_encrypted);
results_.encryption_info = archive_results.encryption_info;
results_.inspection_performed = DownloadFileType::DMG;
std::move(callback_).Run(std::move(results_));
}
#endif // BUILDFLAG(IS_MAC)
#if !BUILDFLAG(IS_ANDROID)
void FileAnalyzer::StartExtractSevenZipFeatures() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
seven_zip_analyzer_ = SandboxedSevenZipAnalyzer::CreateAnalyzer(
tmp_path_,
base::BindOnce(&FileAnalyzer::OnSevenZipAnalysisFinished,
weakptr_factory_.GetWeakPtr()),
LaunchFileUtilService());
seven_zip_analyzer_->Start();
}
void FileAnalyzer::OnSevenZipAnalysisFinished(
const ArchiveAnalyzerResults& archive_results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::UmaHistogramEnumeration(
"SBClientDownload.SevenZipArchiveAnalysisResult",
archive_results.analysis_result);
LogAnalysisDurationWithAndWithoutSuffix("SevenZip");
// Even if !results.success, some of the 7z may have been parsed.
// Some unzippers will successfully unpack archives that we cannot,
// so we're lenient here.
if (archive_results.success) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::VALID);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTimeout) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::PARSER_TIMED_OUT);
} else if (archive_results.analysis_result ==
ArchiveAnalysisResult::kTooLarge) {
results_.archive_summary.set_parser_status(
ClientDownloadRequest::ArchiveSummary::TOO_LARGE);
}
results_.archived_executable = archive_results.has_executable;
results_.archived_archive = archive_results.has_archive;
results_.archived_binaries =
SelectArchiveEntries(archive_results.archived_binary);
if (archive_results.has_executable) {
results_.type = ClientDownloadRequest::SEVEN_ZIP_COMPRESSED_EXECUTABLE;
} else if (archive_results.has_archive) {
results_.type = ClientDownloadRequest::SEVEN_ZIP_COMPRESSED_ARCHIVE;
} else if (!archive_results.success) {
// .7z files that look invalid to Chrome can sometimes be successfully
// unpacked by other archive tools, so they may be a real threat.
results_.type = ClientDownloadRequest::INVALID_SEVEN_ZIP;
} else {
results_.type = download_type_util::GetDownloadType(target_file_name_);
}
results_.archive_summary.set_file_count(archive_results.file_count);
results_.archive_summary.set_directory_count(archive_results.directory_count);
results_.archive_summary.set_is_encrypted(
archive_results.encryption_info.is_encrypted);
results_.encryption_info = archive_results.encryption_info;
results_.inspection_performed = DownloadFileType::SEVEN_ZIP;
std::move(callback_).Run(std::move(results_));
}
#endif // !BUILDFLAG(IS_ANDROID)
void FileAnalyzer::LogAnalysisDurationWithAndWithoutSuffix(
const std::string& suffix) {
base::UmaHistogramMediumTimes("SBClientDownload.FileAnalysisDuration",
base::Time::Now() - start_time_);
base::UmaHistogramMediumTimes(
"SBClientDownload.FileAnalysisDuration." + suffix,
base::Time::Now() - start_time_);
}
} // namespace safe_browsing
|