1 // Copyright (c) 2011 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.
5 #include "chrome/renderer/safe_browsing/phishing_classifier.h"
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_util.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "chrome/common/safe_browsing/csd.pb.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/renderer/safe_browsing/feature_extractor_clock.h"
21 #include "chrome/renderer/safe_browsing/features.h"
22 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h"
23 #include "chrome/renderer/safe_browsing/phishing_term_feature_extractor.h"
24 #include "chrome/renderer/safe_browsing/phishing_url_feature_extractor.h"
25 #include "chrome/renderer/safe_browsing/scorer.h"
26 #include "content/public/renderer/render_view.h"
27 #include "crypto/sha2.h"
28 #include "third_party/WebKit/public/platform/WebURL.h"
29 #include "third_party/WebKit/public/platform/WebURLRequest.h"
30 #include "third_party/WebKit/public/web/WebDataSource.h"
31 #include "third_party/WebKit/public/web/WebDocument.h"
32 #include "third_party/WebKit/public/web/WebFrame.h"
33 #include "third_party/WebKit/public/web/WebView.h"
36 namespace safe_browsing
{
38 const float PhishingClassifier::kInvalidScore
= -1.0;
39 const float PhishingClassifier::kPhishyThreshold
= 0.5;
41 PhishingClassifier::PhishingClassifier(content::RenderView
* render_view
,
42 FeatureExtractorClock
* clock
)
43 : render_view_(render_view
),
50 PhishingClassifier::~PhishingClassifier() {
51 // The RenderView should have called CancelPendingClassification() before
53 CheckNoPendingClassification();
56 void PhishingClassifier::set_phishing_scorer(const Scorer
* scorer
) {
57 CheckNoPendingClassification();
60 url_extractor_
.reset(new PhishingUrlFeatureExtractor
);
62 new PhishingDOMFeatureExtractor(render_view_
, clock_
.get()));
63 term_extractor_
.reset(new PhishingTermFeatureExtractor(
64 &scorer_
->page_terms(),
65 &scorer_
->page_words(),
66 scorer_
->max_words_per_term(),
67 scorer_
->murmurhash3_seed(),
68 scorer_
->max_shingles_per_page(),
69 scorer_
->shingle_size(),
72 // We're disabling client-side phishing detection, so tear down all
73 // of the relevant objects.
74 url_extractor_
.reset();
75 dom_extractor_
.reset();
76 term_extractor_
.reset();
80 bool PhishingClassifier::is_ready() const {
81 return scorer_
!= NULL
;
84 void PhishingClassifier::BeginClassification(
85 const base::string16
* page_text
,
86 const DoneCallback
& done_callback
) {
89 // The RenderView should have called CancelPendingClassification() before
90 // starting a new classification, so DCHECK this.
91 CheckNoPendingClassification();
92 // However, in an opt build, we will go ahead and clean up the pending
93 // classification so that we can start in a known state.
94 CancelPendingClassification();
96 page_text_
= page_text
;
97 done_callback_
= done_callback
;
99 // For consistency, we always want to invoke the DoneCallback
100 // asynchronously, rather than directly from this method. To ensure that
101 // this is the case, post a task to begin feature extraction on the next
102 // iteration of the message loop.
103 base::ThreadTaskRunnerHandle::Get()->PostTask(
104 FROM_HERE
, base::Bind(&PhishingClassifier::BeginFeatureExtraction
,
105 weak_factory_
.GetWeakPtr()));
108 void PhishingClassifier::BeginFeatureExtraction() {
109 blink::WebView
* web_view
= render_view_
->GetWebView();
111 RunFailureCallback();
115 blink::WebFrame
* frame
= web_view
->mainFrame();
117 RunFailureCallback();
121 // Check whether the URL is one that we should classify.
122 // Currently, we only classify http: URLs that are GET requests.
123 GURL
url(frame
->document().url());
124 if (!url
.SchemeIs(url::kHttpScheme
)) {
125 RunFailureCallback();
129 blink::WebDataSource
* ds
= frame
->dataSource();
131 !base::EqualsASCII(base::StringPiece16(ds
->request().httpMethod()),
133 RunFailureCallback();
137 features_
.reset(new FeatureMap
);
138 if (!url_extractor_
->ExtractFeatures(url
, features_
.get())) {
139 RunFailureCallback();
143 // DOM feature extraction can take awhile, so it runs asynchronously
144 // in several chunks of work and invokes the callback when finished.
145 dom_extractor_
->ExtractFeatures(
147 base::Bind(&PhishingClassifier::DOMExtractionFinished
,
148 base::Unretained(this)));
151 void PhishingClassifier::CancelPendingClassification() {
152 // Note that cancelling the feature extractors is simply a no-op if they
155 dom_extractor_
->CancelPendingExtraction();
156 term_extractor_
->CancelPendingExtraction();
157 weak_factory_
.InvalidateWeakPtrs();
161 void PhishingClassifier::DOMExtractionFinished(bool success
) {
162 shingle_hashes_
.reset(new std::set
<uint32
>);
164 // Term feature extraction can take awhile, so it runs asynchronously
165 // in several chunks of work and invokes the callback when finished.
166 term_extractor_
->ExtractFeatures(
169 shingle_hashes_
.get(),
170 base::Bind(&PhishingClassifier::TermExtractionFinished
,
171 base::Unretained(this)));
173 RunFailureCallback();
177 void PhishingClassifier::TermExtractionFinished(bool success
) {
179 blink::WebView
* web_view
= render_view_
->GetWebView();
181 RunFailureCallback();
184 blink::WebFrame
* main_frame
= web_view
->mainFrame();
186 RunFailureCallback();
190 // Hash all of the features so that they match the model, then compute
192 FeatureMap hashed_features
;
193 ClientPhishingRequest verdict
;
194 verdict
.set_model_version(scorer_
->model_version());
195 verdict
.set_url(main_frame
->document().url().spec());
196 for (base::hash_map
<std::string
, double>::const_iterator it
=
197 features_
->features().begin();
198 it
!= features_
->features().end(); ++it
) {
199 DVLOG(2) << "Feature: " << it
->first
<< " = " << it
->second
;
200 bool result
= hashed_features
.AddRealFeature(
201 crypto::SHA256HashString(it
->first
), it
->second
);
203 ClientPhishingRequest::Feature
* feature
= verdict
.add_feature_map();
204 feature
->set_name(it
->first
);
205 feature
->set_value(it
->second
);
207 for (std::set
<uint32
>::const_iterator it
= shingle_hashes_
->begin();
208 it
!= shingle_hashes_
->end(); ++it
) {
209 verdict
.add_shingle_hashes(*it
);
211 float score
= static_cast<float>(scorer_
->ComputeScore(hashed_features
));
212 verdict
.set_client_score(score
);
213 verdict
.set_is_phishing(score
>= kPhishyThreshold
);
214 RunCallback(verdict
);
216 RunFailureCallback();
220 void PhishingClassifier::CheckNoPendingClassification() {
221 DCHECK(done_callback_
.is_null());
223 if (!done_callback_
.is_null() || page_text_
) {
224 LOG(ERROR
) << "Classification in progress, missing call to "
225 << "CancelPendingClassification";
226 UMA_HISTOGRAM_COUNTS("SBClientPhishing.CheckNoPendingClassificationFailed",
231 void PhishingClassifier::RunCallback(const ClientPhishingRequest
& verdict
) {
232 done_callback_
.Run(verdict
);
236 void PhishingClassifier::RunFailureCallback() {
237 ClientPhishingRequest verdict
;
238 // In this case we're not guaranteed to have a valid URL. Just set it
239 // to the empty string to make sure we have a valid protocol buffer.
241 verdict
.set_client_score(kInvalidScore
);
242 verdict
.set_is_phishing(false);
243 RunCallback(verdict
);
246 void PhishingClassifier::Clear() {
248 done_callback_
.Reset();
249 features_
.reset(NULL
);
250 shingle_hashes_
.reset(NULL
);
253 } // namespace safe_browsing