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
|
// Copyright 2023 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/phishy_interaction_tracker.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "components/safe_browsing/content/browser/ui_manager.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/events/keycodes/keyboard_codes.h"
namespace safe_browsing {
namespace {
using base::RecordAction;
using base::UserMetricsAction;
void RecordUserStartsPhishyInteraction() {
RecordAction(UserMetricsAction("PhishyPage.UserStartsInteraction"));
}
void RecordFirstClickEvent() {
RecordAction(UserMetricsAction("PhishyPage.FirstClickEvent"));
}
void RecordFirstKeyEvent() {
RecordAction(UserMetricsAction("PhishyPage.FirstKeyEvent"));
}
void RecordFirstPasteEvent() {
RecordAction(UserMetricsAction("PhishyPage.FirstPasteEvent"));
}
void RecordFinishedInteractionUMAData(int click_count,
int key_count,
int paste_count) {
base::UmaHistogramCounts100("SafeBrowsing.PhishySite.ClickEventCount",
click_count);
base::UmaHistogramCounts100("SafeBrowsing.PhishySite.KeyEventCount",
key_count);
base::UmaHistogramCounts100("SafeBrowsing.PhishySite.PasteEventCount",
paste_count);
RecordAction(UserMetricsAction("PhishyPage.UserStopsInteraction"));
}
} // namespace
PhishyPageInteractionDetails::PhishyPageInteractionDetails(
int occurrence_count,
int64_t first_timestamp,
int64_t last_timestamp)
: occurrence_count(occurrence_count),
first_timestamp(first_timestamp),
last_timestamp(last_timestamp) {}
PhishyInteractionTracker::PhishyInteractionTracker(
content::WebContents* web_contents)
: web_contents_(web_contents), inactivity_delay_(base::Minutes(5)) {
ResetLoggingHelpers();
}
PhishyInteractionTracker::~PhishyInteractionTracker() {
// If there was any data to log, `WebContentsDestroyed()` should have already
// handled it.
DCHECK(!is_phishy_ || is_data_logged_);
}
void PhishyInteractionTracker::WebContentsDestroyed() {
if (is_phishy_ && !is_data_logged_) {
LogPageData();
inactivity_timer_.Stop();
}
}
void PhishyInteractionTracker::HandlePageChanged() {
if (is_phishy_ && !is_data_logged_) {
LogPageData();
}
ResetLoggingHelpers();
inactivity_timer_.Stop();
current_url_ = web_contents_->GetLastCommittedURL().GetWithEmptyPath();
current_page_url_ =
web_contents_->GetController().GetLastCommittedEntry()->GetURL();
is_phishy_ = IsSitePhishy();
if (is_phishy_) {
RecordUserStartsPhishyInteraction();
}
}
void PhishyInteractionTracker::HandlePasteEvent() {
if (is_phishy_) {
HandlePhishyInteraction(ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_PASTE_EVENT);
}
}
void PhishyInteractionTracker::HandleInputEvent(
const blink::WebInputEvent& event) {
if (!is_phishy_) {
return;
}
if (event.GetType() == blink::WebInputEvent::Type::kMouseDown) {
HandlePhishyInteraction(ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_CLICK_EVENT);
return;
}
#if BUILDFLAG(IS_ANDROID)
// On Android, key down events are triggered if a user types in through a
// number bar on Android keyboard. If text is typed in through other parts of
// Android keyboard, ImeTextCommittedEvent is triggered instead.
if (event.GetType() == blink::WebInputEvent::Type::kKeyDown) {
HandlePhishyInteraction(ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_KEY_EVENT);
}
#else // !BUILDFLAG(IS_ANDROID)
if (event.GetType() == blink::WebInputEvent::Type::kChar) {
const blink::WebKeyboardEvent& key_event =
static_cast<const blink::WebKeyboardEvent&>(event);
// Key & 0x1f corresponds to the value of the key when either the control or
// command key is pressed. This detects CTRL+V, COMMAND+V, and CTRL+SHIFT+V.
if (key_event.windows_key_code == (ui::VKEY_V & 0x1f)) {
HandlePhishyInteraction(ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_PASTE_EVENT);
} else {
HandlePhishyInteraction(ClientSafeBrowsingReportRequest::
PhishySiteInteraction::PHISHY_KEY_EVENT);
}
}
#endif // BUILDFLAG(IS_ANDROID)
}
void PhishyInteractionTracker::ResetLoggingHelpers() {
is_phishy_ = false;
is_data_logged_ = false;
phishy_page_interaction_data_.clear();
}
bool PhishyInteractionTracker::IsSitePhishy() {
if (!ui_manager_for_testing_ && !g_browser_process->safe_browsing_service()) {
return false;
}
safe_browsing::SafeBrowsingUIManager* ui_manager_ =
ui_manager_for_testing_
? ui_manager_for_testing_.get()
: g_browser_process->safe_browsing_service()->ui_manager().get();
safe_browsing::SBThreatType current_threat_type;
if (!ui_manager_->IsUrlAllowlistedOrPendingForWebContents(
current_url_,
web_contents_->GetController().GetLastCommittedEntry(), web_contents_,
/*allowlist_only=*/true, ¤t_threat_type)) {
return false;
}
return current_threat_type == SBThreatType::SB_THREAT_TYPE_URL_PHISHING ||
current_threat_type ==
SBThreatType::SB_THREAT_TYPE_URL_CLIENT_SIDE_PHISHING ||
current_threat_type == SBThreatType::SB_THREAT_TYPE_SUSPICIOUS_SITE;
}
void PhishyInteractionTracker::HandlePhishyInteraction(
const ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PhishySiteInteractionType& interaction) {
int new_occurrence_count = 1;
int64_t new_first_timestamp =
base::Time::Now().InMillisecondsSinceUnixEpoch();
int64_t new_last_timestamp = base::Time::Now().InMillisecondsSinceUnixEpoch();
last_interaction_ts_ = base::Time::Now();
// Log if first occurrence of the interaction.
if (!phishy_page_interaction_data_.contains(interaction)) {
RecordFirstInteractionOccurrence(interaction);
} else {
new_occurrence_count +=
phishy_page_interaction_data_.at(interaction).occurrence_count;
new_first_timestamp =
phishy_page_interaction_data_.at(interaction).first_timestamp;
}
phishy_page_interaction_data_.insert_or_assign(
interaction,
PhishyPageInteractionDetails(new_occurrence_count, new_first_timestamp,
new_last_timestamp));
inactivity_timer_.Start(FROM_HERE, inactivity_delay_, this,
&PhishyInteractionTracker::MaybeLogIfUserInactive);
}
void PhishyInteractionTracker::RecordFirstInteractionOccurrence(
ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PhishySiteInteractionType interaction) {
switch (interaction) {
case ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_CLICK_EVENT:
RecordFirstClickEvent();
break;
case ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_KEY_EVENT:
RecordFirstKeyEvent();
break;
case ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_PASTE_EVENT:
RecordFirstPasteEvent();
break;
default:
break;
}
}
void PhishyInteractionTracker::MaybeLogIfUserInactive() {
if (IsUserInactive() && !is_data_logged_) {
LogPageData();
}
}
void PhishyInteractionTracker::LogPageData() {
RecordFinishedInteractionUMAData(
phishy_page_interaction_data_.contains(
ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_CLICK_EVENT)
? phishy_page_interaction_data_
.at(ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_CLICK_EVENT)
.occurrence_count
: 0,
phishy_page_interaction_data_.contains(
ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_KEY_EVENT)
? phishy_page_interaction_data_
.at(ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_KEY_EVENT)
.occurrence_count
: 0,
phishy_page_interaction_data_.contains(
ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_PASTE_EVENT)
? phishy_page_interaction_data_
.at(ClientSafeBrowsingReportRequest::PhishySiteInteraction::
PHISHY_PASTE_EVENT)
.occurrence_count
: 0);
// Send PHISHY_SITE_INTERACTIONS report for relevant OSes.
#if BUILDFLAG(FULL_SAFE_BROWSING)
g_browser_process->safe_browsing_service()->SendPhishyInteractionsReport(
Profile::FromBrowserContext(web_contents_->GetBrowserContext()),
current_url_, current_page_url_, phishy_page_interaction_data_);
#endif
is_data_logged_ = true;
}
void PhishyInteractionTracker::SetInactivityDelayForTesting(
base::TimeDelta inactivity_delay) {
inactivity_delay_ = inactivity_delay;
}
} // namespace safe_browsing
|