File: phishingDetector.js

package info (click to toggle)
thunderbird 1%3A52.8.0-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 1,710,120 kB
  • sloc: cpp: 5,081,109; ansic: 2,051,982; python: 458,727; java: 241,615; xml: 193,367; asm: 178,649; sh: 81,881; makefile: 24,703; perl: 16,874; objc: 4,389; yacc: 1,816; ada: 1,697; lex: 1,257; pascal: 1,251; cs: 879; exp: 499; php: 436; lisp: 258; awk: 152; sed: 51; ruby: 47; csh: 27
file content (235 lines) | stat: -rw-r--r-- 8,905 bytes parent folder | download | duplicates (4)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Dependencies:
// gatherTextUnder from utilityOverlay.js

Components.utils.import("resource://gre/modules/Services.jsm");

var kPhishingNotSuspicious = 0;
var kPhishingWithIPAddress = 1;
var kPhishingWithMismatchedHosts = 2;


var gPhishingDetector = {
  mCheckForIPAddresses: true,
  mCheckForMismatchedHosts: true,

  shutdown: function()
  {
  },

  /**
   * Initialize the phishing warden.
   * Initialize the black and white list url tables.
   * Update the local tables if necessary.
   */
  init: function()
  {
    Components.utils.import("resource:///modules/hostnameUtils.jsm", this);

    this.mCheckForIPAddresses = Services.prefs.getBoolPref("mail.phishing.detection.ipaddresses");
    this.mCheckForMismatchedHosts = Services.prefs.getBoolPref("mail.phishing.detection.mismatched_hosts");
  },

  /**
   * Analyzes the urls contained in the currently loaded message in the message pane, looking for
   * phishing URLs.
   * Assumes the message has finished loading in the message pane (i.e. OnMsgParsed has fired).
   *
   * @param aUrl nsIURI for the message being analyzed.
   *
   * @return asynchronously calls gMessageNotificationBar.setPhishingMsg if the message
   *         is identified as a scam.
   */
  analyzeMsgForPhishingURLs: function (aUrl)
  {
    if (!aUrl || !Services.prefs.getBoolPref("mail.phishing.detection.enabled"))
      return;

    try {
      // nsIMsgMailNewsUrl.folder can throw an NS_ERROR_FAILURE, especially if
      // we are opening an .eml file.
      var folder = aUrl.folder;

      // Ignore nntp and RSS messages.
      if (folder.server.type == 'nntp' || folder.server.type == 'rss')
        return;

      // Also ignore messages in Sent/Drafts/Templates/Outbox.
      const nsMsgFolderFlags = Components.interfaces.nsMsgFolderFlags;
      let outgoingFlags = nsMsgFolderFlags.SentMail | nsMsgFolderFlags.Drafts |
                          nsMsgFolderFlags.Templates | nsMsgFolderFlags.Queue;
      if (folder.isSpecialFolder(outgoingFlags, true))
        return;

    } catch (ex) {
        if (ex.result != Components.results.NS_ERROR_FAILURE)
          throw ex;
    }

    // extract the link nodes in the message and analyze them, looking for suspicious URLs...
    var linkNodes = document.getElementById('messagepane').contentDocument.links;
    for (var index = 0; index < linkNodes.length; index++)
      this.analyzeUrl(linkNodes[index].href, gatherTextUnder(linkNodes[index]));

    // extract the action urls associated with any form elements in the message and analyze them.
    let formNodes = document.getElementById('messagepane').contentDocument.querySelectorAll("form[action]");
    for (index = 0; index < formNodes.length; index++)
    {
      this.analyzeUrl(formNodes[index].action);
    }
  },

  /**
   * Analyze the url contained in aLinkNode for phishing attacks. If a phishing URL is found,
   *
   * @param aHref the url to be analyzed
   * @param aLinkText (optional) user visible link text associated with aHref in case
   *        we are dealing with a link node.
   * @return asynchronously calls gMessageNotificationBar.setPhishingMsg if the link node
   *         contains a phishing URL.
   */
  analyzeUrl: function (aUrl, aLinkText)
  {
    if (!aUrl)
      return;

    var hrefURL;
    // make sure relative link urls don't make us bail out
    try {
      hrefURL = Services.io.newURI(aUrl, null, null);
    } catch(ex) { return; }

    // only check for phishing urls if the url is an http or https link.
    // this prevents us from flagging imap and other internally handled urls
    if (hrefURL.schemeIs('http') || hrefURL.schemeIs('https'))
    {
      // The link is not suspicious if the visible text is the same as the URL,
      // even if the URL is an IP address. URLs are commonly surrounded by
      // < > or "" (RFC2396E) - so strip those from the link text before comparing.
      if (aLinkText)
        aLinkText = aLinkText.replace(/^<(.+)>$|^"(.+)"$/, "$1$2");

      var failsStaticTests = false;
      // If the link text and url differs by something other than a trailing
      // slash, do some further checks.
      if (aLinkText != aUrl &&
          aLinkText.replace(/\/+$/, "") != aUrl.replace(/\/+$/, ""))
      {
        if (this.mCheckForIPAddresses)
        {
          let unobscuredHostNameValue = this.isLegalIPAddress(hrefURL.host, true);
          if (unobscuredHostNameValue)
            failsStaticTests = !this.isLegalLocalIPAddress(unobscuredHostNameValue);
        }

        if (!failsStaticTests && this.mCheckForMismatchedHosts)
        {
          failsStaticTests = (aLinkText &&
            this.misMatchedHostWithLinkText(hrefURL, aLinkText))
        }
      }
      // We don't use dynamic checks anymore. The old implementation was removed
      // in bug bug 1085382. Using the toolkit safebrowsing is bug 778611.
      if (failsStaticTests) {
        gMessageNotificationBar.setPhishingMsg();
      }
    }
  },

  /**
   * Opens the default browser to a page where the user can submit the given url
   * as a phish.
   * @param aPhishingURL the url we want to report back as a phishing attack
   */
   reportPhishingURL: function(aPhishingURL)
   {
     let reportUrl = Services.urlFormatter.formatURLPref(
       "browser.safebrowsing.reportPhishURL");
     reportUrl += "&url=" + encodeURIComponent(aPhishingURL);

     let uri = Services.io.newURI(reportUrl, null, null);
     let protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
                       .getService(Components.interfaces.nsIExternalProtocolService);
     protocolSvc.loadUrl(uri);
   },

  /**
   * Private helper method to determine if the link node contains a user visible
   * url with a host name that differs from the actual href the user would get
   * taken to.
   * i.e. <a href="http://myevilsite.com">http://mozilla.org</a>
   *
   * @return true if aHrefURL.host does NOT match the host of the link node text
   */
  misMatchedHostWithLinkText: function(aHrefURL, aLinkNodeText)
  {
    // gatherTextUnder puts a space between each piece of text it gathers,
    // so strip the spaces out (see bug 326082 for details).
    aLinkNodeText = aLinkNodeText.replace(/ /g, "");

    // Only worry about http: and https: urls.
    if (/^https?:/.test(aLinkNodeText))
    {
      let linkTextURI = Services.io.newURI(aLinkNodeText, null, null);

      // Compare the base domain of the href and the link text.
      try {
        return Services.eTLD.getBaseDomain(aHrefURL) !=
               Services.eTLD.getBaseDomain(linkTextURI);
      } catch (e) {
        // If we throw above, one of the URIs probably has no TLD (e.g.
        // http://localhost), so just check the entire host.
        return aHrefURL.host != linkTextURI.host;
      }
    }

    return false;
  },

  /**
   * If the current message has been identified as an email scam, prompts the user with a warning
   * before allowing the link click to be processed. The warning prompt includes the unobscured host name
   * of the http(s) url the user clicked on.
   *
   * @param aUrl the url
   * @return true if the link should be allowed to load
   */
  warnOnSuspiciousLinkClick: function(aUrl)
  {
    // If the loaded message has *not* been flagged as a scam...
    if (!gMessageNotificationBar.isShowingPhishingNotification())
      return true; // ...allow the link to load.

    var hrefURL;
    // make sure relative link urls don't make us bail out
    try {
      hrefURL = Services.io.newURI(aUrl, null, null);
    } catch(ex) { return false; }

    // only prompt for http and https urls
    if (hrefURL.schemeIs('http') || hrefURL.schemeIs('https'))
    {
      // unobscure the host name in case it's an encoded ip address..
      let unobscuredHostNameValue = this.isLegalIPAddress(hrefURL.host, true)
        || hrefURL.host;

      var brandShortName = document.getElementById("bundle_brand")
                                   .getString("brandShortName");
      var bundle = document.getElementById("bundle_messenger");
      var titleMsg = bundle.getString("confirmPhishingTitle");
      var dialogMsg = bundle.getFormattedString("confirmPhishingUrl",
                        [brandShortName, unobscuredHostNameValue], 2);

      const nsIPS = Components.interfaces.nsIPromptService;
      return !Services.prompt.confirmEx(window, titleMsg, dialogMsg,
                                        nsIPS.STD_YES_NO_BUTTONS +
                                        nsIPS.BUTTON_POS_1_DEFAULT,
                                        "", "", "", "", {}); /* the yes button is in position 0 */
    }

    return true; // allow the link to load
  }
};