all files / src/html_analyzer/ outlook_web_access.js

6.98% Statements 6/86
0% Branches 0/46
0% Functions 0/15
6.98% Lines 6/86
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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * @file outlook_web_access.js
 * @desc
 * Looks for Outlook signatures in html data.
 * To get a good set of test web sites -
 * google search keywords -
 * 1. intitle:"Microsoft Outlook Web Access"
 * 2. inurl:"/owa/auth/logon.aspx?url="
 *
 * Additional signature related info are available at -
 * 1. https://goo.gl/IPG41D
 * 2. Chrome plugin 'wappalyzer' src.
 *
 * The signature info are outdated and may not be valid for Outlook 2013.
 * Other notes and sample web sites -
 *
 * 1. Outlook 2003 -
 *    Microsoft internal version number - 6.5.X
 *    Useful Http response headers -
 *    X-Powered-By:ASP.NET
 *
 *    Useful html signature elements -
 *    <!-- {57A118C6-2DA9-419d-BE9A-F92B0F9A418B} -->
 *    <TITLE>Microsoft Outlook Web Access</TITLE>
 *    <FORM action="/exchweb/bin/auth/owaauth.dll" method="POST" name="logonForm">
 *
 *    Sample website -
 *    https://mailhost.wcl.american.edu/
 *    https://goo.gl/FlNI0x
 *    https://goo.gl/RIeumA
 *
 * 2. Outlook 2007 -
 *    Microsoft internal version number - 8.X
 *    Useful Http response headers -
 *    X-OWA-Version:8.3.379.2
 *    X-Powered-By:ASP.NET
 *
 *    Useful html signature elements -
 *    <!-- OwaPage = ASP.auth_logon_aspx -->
 *    <!-- {57A118C6-2DA9-419d-BE9A-F92B0F9A418B} -->
 *    <title>Microsoft Exchange - Outlook Web Access</title>
 *    <link type="text/css" rel="stylesheet"
 *          href="/owa/8.3.379.2/themes/base/logon.css">
 *    <link type="text/css" rel="stylesheet"
 *          href="/owa/8.3.379.2/themes/base/owafont.css">
 *    <form action="owaauth.dll" method="POST" name="logonForm"
 *          autocomplete="off" id="loginform">
 *
 *    Sample website -
 *    https://goo.gl/OAVu1V
 *
 * 3. Outlook 2010 -
 *    Microsoft internal version number - 14.X
 *    Useful Http response headers -
 *    X-OWA-Version:14.3.195.1
 *    X-Powered-By:ASP.NET
 *
 *    Useful html signature elements -
 *    <!-- OwaPage = ASP.auth_logon_aspx -->
 *    <!-- {57A118C6-2DA9-419d-BE9A-F92B0F9A418B} -->
 *    <title>Outlook Web App</title>
 *    <link rel="shortcut icon" href="/owa/14.3.195.1/themes/resources/favicon.ico"
 *          type="image/x-icon">
 *    <link type="text/css" rel="stylesheet"
 *          href="/owa/14.3.195.1/themes/resources/logon.css">
 *    <link type="text/css" rel="stylesheet"
 *          href="/owa/14.3.195.1/themes/resources/owafont.css">
 *    <form action="/owa/auth.owa" method="POST" name="logonForm">
 *
 *    Sample website -
 *    https://outlook.leeds.ac.uk/owa/auth/logon.aspx?replaceCurrent=1
 *    https://goo.gl/jVo6rv
 *    https://goo.gl/Qr7IWa
 *
 * 4. Outlook 2013 -
 *    Microsoft internal version number - 15.X
 *    Useful Http response headers -
 *    Server:Microsoft-IIS/8.5
 *    X-AspNet-Version:4.0.30319
 *    X-Powered-By:ASP.NET
 *
 *    Useful html signature elements -
 *    <!-- OwaPage = ASP.auth_logon_aspx -->
 *    <!-- {57A118C6-2DA9-419d-BE9A-F92B0F9A418B} -->
 *    <link rel="shortcut icon" href="/owa/auth/15.0.1044/themes/resources/favicon.ico"
 *          type="image/x-icon">
 *    <title>Outlook Web App</title>
 *
 *    Sample website -
 *    https://goo.gl/Zp6zIf
 *
 * Some special OWA pages which do not match the above criteria -
 * 1. https://owa.marriott.com/CookieAuth.dll?GetLogon?curl=Z2Fowa&reason=0&formdir=1
 *
 * Finger-printing works in this case. But, we are not sure about the version
 *
 * 2. https://email.tsa.dhs.gov/my.policy
 * Finger-printing does not work in this case. It uses a heavily customized
 * Outlook 2010.
 */
'use strict';
 
var _ = require('lodash'),
   domutils = require('domutils'),
   logger = require(__dirname + '/../loggers/logger');
 
var owaTitles = ['Microsoft Outlook Web Access',            //outlook 2003
                 'Microsoft Exchange - Outlook Web Access', //outlook 2007
                 'Outlook Web Access',                      //outlook 2007
                 'Outlook Web App'],                        //outlook 2010/2013
   owaFormActions = ['/exchweb/bin/auth/owaauth.dll',       //outlook 2003
                     'owaauth.dll',                         //outlook 2007
                     '/owa/auth.owa',                       //outlook 2010
                     '/cookieauth.dll?logon'],              //probably 2010
   owaComments = ['OwaPage = ASP.auth_logon_aspx',          //outlook 2007/10/13
                  '{57A118C6-2DA9-419d-BE9A-F92B0F9A418B}'];//outlook 2007/10/13
 
var owaIconRegExes = [
       // outlook 2010
       new RegExp('\/owa\/([^\s^\/]+)\/themes\/resources\/favicon\.ico'),
       // outlook 2013
       new RegExp('\/owa\/auth\/([^\s^\/]+)\/themes\/resources\/favicon\.ico')],
   owaCssRegExes = [
       // outlook 2010
       new RegExp('\/owa\/([^\s^\/]+)\/themes\/resources\/logon\.css'),
       new RegExp('\/owa\/([^\s^\/]+)\/themes\/resources\/owafont\.css'),
       // outlook 2007
       new RegExp('\/owa\/([^\s^\/]+)\/themes\/base\/logon\.css'),
       new RegExp('\/owa\/([^\s^\/]+)\/themes\/base\/owafont\.css')];
 
var getValue = function (obj, property) {
   return _.isUndefined(obj[property]) ? ''  : obj[property];
};
 
var analyzer = function () {
 
   var isEnabled = true,
      owaServerName = '',
      owaServerVersion = '';
 
   this.init = function (headers) {
      if (!headers || !headers.url) {
         return;
      }
 
      var xOwaHeader = getValue(headers, 'x-owa-version');
      if (xOwaHeader) {
         // If we find a header like 'X-OWA-Version:14.3.195.1'
         // We don't need to parse the html at all. We can be
         // sure that it is a OWA server.
         owaServerName = 'Outlook Web Access';
         owaServerVersion = xOwaHeader.replace(/[^\d.]/g, '');
         logger.info('xOwaHeader found - ' + owaServerName);
         isEnabled = false;
         return;
      }
   };
 
   this.isEnabledForDomWatch = function () {
      return isEnabled;
   };
 
   this.getServerInfo = function () {
      var serverInfos = [];
      if (owaServerName) {
         var serverInfo = {};
         serverInfo.name = owaServerName;
         serverInfo.version = owaServerVersion;
         serverInfos.push(serverInfo);
      }
      return serverInfos;
   };
 
   this.inspectCommentNode = function (element) {
      if (!element.data) {
         return;
      }
 
      // Some OWA pages have the following comment at the very top of page.
      //<!-- OwaPage = ASP.auth_logon_aspx -->
      var comment = element.data;
      _.some(owaComments, function (owaComment) {
         if (comment.indexOf(owaComment) > -1) {
            owaServerName = 'Outlook Web Access';
            logger.info('OWA: comment match - ' + comment);
            return true; // stop looping
         }
      });
   };
 
   this.inspectTitle = function (element) {
      var text = domutils.getText(element);
      _.some(owaTitles, function (owaTitle) {
         if (text.indexOf(owaTitle) > -1) {
            owaServerName = 'Outlook Web Access';
            logger.info('OWA: Page title match - ' + text);
            return true; // stop looping
         }
      });
   };
 
   this.inspectForm = function (element) {
      // OWA form action is of the following pattern -
      //<FORM action="/exchweb/bin/auth/owaauth.dll" name="logonForm">
      //<form action="/owa/auth.owa" method="POST" name="logonForm">
      var formAction = domutils.getAttributeValue(element, 'action');
      if (!formAction) {
         return;
      }
 
      formAction = formAction.toLowerCase();
      _.some(owaFormActions, function (owaFormAction) {
         if (formAction.indexOf(owaFormAction) > -1) {
            owaServerName = 'Outlook Web Access';
            logger.info('OWA: Form action match - ' + formAction);
            return true; // stop looping
         }
      });
   };
 
   this.matchRegExes = function (href, regExes) {
      var matchVal = null;
      _.some(regExes, function (reg) {
         matchVal = href.match(reg);
         return matchVal;
      });
 
      if (matchVal && matchVal.length === 2) {
         owaServerName = 'Outlook Web Access';
         owaServerVersion = matchVal[1];
         isEnabled = false; // No more dom watch required.
         logger.info('OWA: Found regex match, OWA Version - ' + matchVal[1]);
      }
   };
 
   this.inspectLink = function (element) {
      var rel = domutils.getAttributeValue(element, 'rel'),
         href = domutils.getAttributeValue(element, 'href'),
         type = domutils.getAttributeValue(element, 'type');
 
      if (!rel || !href || !type) {
         return;
      }
 
      if ((rel === 'shortcut icon') && (type === 'image/x-icon')) {
         // OWA pages usually have a shortcut icon in the following pattern -
         // <link rel="shortcut icon" type="image/x-icon"
         //  href="/owa/14.3.195.1/themes/resources/favicon.ico">
         this.matchRegExes(href, owaIconRegExes);
         return;
      }
 
      if ((rel === 'stylesheet') && (type === 'text/css')) {
         // OWA pages usually have a stylesheets in the following pattern -
         // <link rel="stylesheet" type="text/css"
         //  href="/owa/14.3.195.1/themes/resources/logon.css">
         this.matchRegExes(href, owaCssRegExes);
      }
   };
 
   this.inspectDomNode = function (element) {
      if (!isEnabled){
         return;
      }
 
      if(!domutils.isTag(element) && (element.type === 'comment')) {
         this.inspectCommentNode(element);
         return;
      }
 
      switch (element.name)
      {
         case 'title':
            this.inspectTitle(element);
            break;
 
         case 'form':
            this.inspectForm(element);
            break;
 
         case 'link':
            this.inspectLink(element);
            break;
      }
   };
};
 
 
module.exports = analyzer;