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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "chrome/browser/download/download_safe_browsing_client.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
#include "chrome/common/chrome_switches.h"
#include "content/browser/browser_thread.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_manager.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
// TODO(lzheng): Get rid of the AddRef and Release after
// SafeBrowsingService::Client is changed to RefCountedThreadSafe<>.
DownloadSBClient::DownloadSBClient(int32 download_id,
const std::vector<GURL>& url_chain,
const GURL& referrer_url,
bool safe_browsing_enabled)
: download_id_(download_id),
url_chain_(url_chain),
referrer_url_(referrer_url),
safe_browsing_enabled_(safe_browsing_enabled) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!url_chain.empty());
ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host();
if (rdh)
sb_service_ = g_browser_process->safe_browsing_service();
}
DownloadSBClient::~DownloadSBClient() {}
void DownloadSBClient::CheckDownloadUrl(const UrlDoneCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// It is not allowed to call this method twice.
CHECK(url_done_callback_.is_null() && hash_done_callback_.is_null());
start_time_ = base::TimeTicks::Now();
url_done_callback_ = callback;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&DownloadSBClient::CheckDownloadUrlOnIOThread, this,
url_chain_));
}
void DownloadSBClient::CheckDownloadHash(const std::string& hash,
const HashDoneCallback& callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
// It is not allowed to call this method twice.
CHECK(url_done_callback_.is_null() && hash_done_callback_.is_null());
start_time_ = base::TimeTicks::Now();
hash_done_callback_ = callback;
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&DownloadSBClient::CheckDownloadHashOnIOThread, this, hash));
}
void DownloadSBClient::CheckDownloadUrlOnIOThread(
const std::vector<GURL>& url_chain) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Will be released in OnDownloadUrlCheckResult.
AddRef();
if (safe_browsing_enabled_ && sb_service_.get() &&
!sb_service_->CheckDownloadUrl(url_chain, this)) {
// Wait for SafeBrowsingService to call back OnDownloadUrlCheckResult.
return;
}
OnDownloadUrlCheckResult(url_chain, SafeBrowsingService::SAFE);
}
// The callback interface for SafeBrowsingService::Client.
// Called when the result of checking a download URL is known.
void DownloadSBClient::OnDownloadUrlCheckResult(
const std::vector<GURL>& url_chain,
SafeBrowsingService::UrlCheckResult result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DownloadSBClient::SafeBrowsingCheckUrlDone, this, result));
Release();
}
void DownloadSBClient::CheckDownloadHashOnIOThread(const std::string& hash) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
// Will be released in OnDownloadUrlCheckResult.
AddRef();
if (safe_browsing_enabled_ && sb_service_.get() &&
!sb_service_->CheckDownloadHash(hash, this)) {
// Wait for SafeBrowsingService to call back OnDownloadUrlCheckResult.
return;
}
OnDownloadHashCheckResult(hash, SafeBrowsingService::SAFE);
}
// The callback interface for SafeBrowsingService::Client.
// Called when the result of checking a download URL is known.
void DownloadSBClient::OnDownloadHashCheckResult(
const std::string& hash, SafeBrowsingService::UrlCheckResult result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DownloadSBClient::SafeBrowsingCheckHashDone, this, result,
hash));
Release();
}
void DownloadSBClient::SafeBrowsingCheckUrlDone(
SafeBrowsingService::UrlCheckResult result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DVLOG(1) << "SafeBrowsingCheckUrlDone with result: " << result;
bool is_dangerous = result != SafeBrowsingService::SAFE;
url_done_callback_.Run(download_id_, is_dangerous);
if (sb_service_.get() && sb_service_->download_protection_enabled()) {
UMA_HISTOGRAM_TIMES("SB2.DownloadUrlCheckDuration",
base::TimeTicks::Now() - start_time_);
UpdateDownloadCheckStats(DOWNLOAD_URL_CHECKS_TOTAL);
if (is_dangerous) {
UpdateDownloadCheckStats(DOWNLOAD_URL_CHECKS_MALWARE);
ReportMalware(result, std::string());
}
}
}
void DownloadSBClient::SafeBrowsingCheckHashDone(
SafeBrowsingService::UrlCheckResult result,
const std::string& hash) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DVLOG(1) << "SafeBrowsingCheckHashDone with result: " << result;
bool is_dangerous = result != SafeBrowsingService::SAFE;
hash_done_callback_.Run(download_id_, is_dangerous);
if (sb_service_.get() && sb_service_->download_protection_enabled()) {
UMA_HISTOGRAM_TIMES("SB2.DownloadHashCheckDuration",
base::TimeTicks::Now() - start_time_);
UpdateDownloadCheckStats(DOWNLOAD_HASH_CHECKS_TOTAL);
if (is_dangerous) {
UpdateDownloadCheckStats(DOWNLOAD_HASH_CHECKS_MALWARE);
ReportMalware(result, hash);
}
}
}
void DownloadSBClient::ReportMalware(
SafeBrowsingService::UrlCheckResult result,
const std::string& hash) {
std::string post_data;
if (!hash.empty())
post_data += base::HexEncode(hash.data(), hash.size()) + "\n";
for (size_t i = 0; i < url_chain_.size(); ++i)
post_data += url_chain_[i].spec() + "\n";
sb_service_->ReportSafeBrowsingHit(url_chain_.back(), // malicious_url
url_chain_.front(), // page_url
referrer_url_,
true, // is_subresource
result,
post_data);
}
void DownloadSBClient::UpdateDownloadCheckStats(SBStatsType stat_type) {
UMA_HISTOGRAM_ENUMERATION("SB2.DownloadChecks",
stat_type,
DOWNLOAD_CHECKS_MAX);
}
|