This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
forked from mikecann/Chrome-Crawler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
instrument.js
374 lines (325 loc) · 12.3 KB
/
instrument.js
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
function instrument() {
var event_id = document.currentScript.getAttribute('data-event-id');
var originalTimeout = setTimeout;
var originalInterval = setInterval;
// Not perfect: there could have been timeouts/intervals called
// before this. which would still be too fast, but better than
// nothing for now.
var disable_event = event_id + '-patching-disable'
var disable = () => {
window.setTimeout = originalTimeout;
window.setInterval = originalInterval;
document.removeEventListener(disable_event, disable);
console.debug("Privacy Crawler: Disabled setTimeout & setInterval patching");
}
document.addEventListener(disable_event, disable);
// Monkey patch the environment to speed up time, since some
// tracking pixels and fingerprinting javascript only runs
// after a delay
console.debug("Privacy Crawler: Enabled setTimeout & setInterval patching");
window.setTimeout = function(func, time) {
return originalTimeout(func, time/20);
}
window.setInterval = function(func, time) {
return originalInterval(func, time/20);
}
// Intrumentation injection code is based on OpenWPM, in-turn based on privacybadgerfirefox
// https://github.com/EFForg/privacybadgerfirefox/blob/master/data/fingerprinting.js
// https://stackoverflow.com/a/27078401
function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
var later = function() {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};
return function() {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = originalTimeout(later, remaining);
}
return result;
};
}
var send = (function () {
var messages = [];
var _send = throttle(function () {
document.dispatchEvent(new CustomEvent(event_id, {
detail: messages
}));
messages = [];
}, 100);
return function (msgType, msg) {
messages.push({'type':msgType,'content':msg});
_send();
};
}());
function logErrorToConsole(error) {
console.log("Error name: " + error.name);
console.log("Error message: " + error.message);
console.log("Error filename: " + error.fileName);
console.log("Error line number: " + error.lineNumber);
console.log("Error stack: " + error.stack);
}
function getStackTrace() {
var stack;
try {
throw new Error();
} catch (err) {
stack = err.stack;
}
return stack;
}
var stackTraceUrlRegex = /(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&//=,]*)):\d+:\d+/
var stackTracePathRegex = /\((\/.+):\d+:\d+\)/;
var stackTraceLocalRegex = /\((.+):\d+:\d+\)/;
function getOriginatingScriptContext() {
var trace = getStackTrace().trim().split('\n');
try {
var lineUrl = trace.find((line) => {
return line.match(stackTraceUrlRegex);
});
var linePath = trace.find((line) => {
return line.match(stackTracePathRegex);
});
var lineLocal = trace.find((line) => {
return line.match(stackTraceLocalRegex);
});
var scriptUrl = lineUrl ? lineUrl.match(stackTraceUrlRegex)[1] :
linePath ? (window.location.href.split('/')[0] + linePath.match(stackTracePathRegex)[1]) :
lineLocal ? (window.location.href.split('#')[0]) : 'unknown';
} catch (e) {
console.log("Error parsing the script context", e, callSite);
scriptUrl = 'unknown';
}
return {
scriptUrl: scriptUrl
};
}
// Counter to cap # of calls logged for each script/api combination
var maxLogCount = 500;
var logCounter = new Object();
function updateCounterAndCheckIfOver(scriptUrl, symbol) {
var key = '___URL___' + scriptUrl + '___SYMBOL___' + symbol;
if ((key in logCounter) && (logCounter[key] >= maxLogCount)) {
return true;
} else if (!(key in logCounter)) {
logCounter[key] = 1;
} else {
logCounter[key] += 1;
}
return false;
}
// Prevent logging of gets arising from logging
var inLog = false;
// For gets, sets, etc. on a single value
function logValue(instrumentedVariableName, callContext) {
if(inLog)
return;
inLog = true;
var overLimit = updateCounterAndCheckIfOver(callContext.scriptUrl, instrumentedVariableName);
if (overLimit) {
inLog = false;
return;
}
var msg = {
name: instrumentedVariableName,
scriptUrl: callContext.scriptUrl
};
try {
send('logValue', msg);
}
catch(error) {
console.log("Unsuccessful value log!");
logErrorToConsole(error);
}
inLog = false;
}
// For functions
function logCall(instrumentedFunctionName, callContext) {
if(inLog)
return;
inLog = true;
var overLimit = updateCounterAndCheckIfOver(callContext.scriptUrl, instrumentedFunctionName);
if (overLimit) {
inLog = false;
return;
}
try {
var msg = {
name: instrumentedFunctionName,
scriptUrl: callContext.scriptUrl
}
send('logCall', msg);
}
catch(error) {
console.log("Unsuccessful call log: " + instrumentedFunctionName);
logErrorToConsole(error);
}
inLog = false;
}
// Rough implementations of Object.getPropertyDescriptor and Object.getPropertyNames
// See http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
function getPropertyDescriptor(subject, name) {
var pd = Object.getOwnPropertyDescriptor(subject, name);
var proto = Object.getPrototypeOf(subject);
while (pd === undefined && proto !== null) {
pd = Object.getOwnPropertyDescriptor(proto, name);
proto = Object.getPrototypeOf(proto);
}
return pd;
};
function getPropertyNames(subject, name) {
var props = Object.getOwnPropertyNames(subject);
var proto = Object.getPrototypeOf(subject);
while (proto !== null) {
props = props.concat(Object.getOwnPropertyNames(proto));
proto = Object.getPrototypeOf(proto);
}
// FIXME: remove duplicate property names from props
return props;
};
function isObject(object, propertyName) {
try {
var property = object[propertyName];
} catch(error) {
return false;
}
if (property === null) {
return false;
}
return typeof property === 'object';
}
function instrumentObject(object, objectName, logSettings={}) {
var properties = getPropertyNames(object);
var exclude = logSettings.excludedProperties || [];
for (var i = 0; i < properties.length; i++) {
if (exclude.indexOf(properties[i]) !== -1) {
continue;
}
try {
instrumentObjectProperty(object, objectName, properties[i], logSettings);
} catch(error) {
logErrorToConsole(error);
}
}
}
function instrumentFunction(objectName, methodName, func) {
return function () {
var callContext = getOriginatingScriptContext();
logCall(objectName + '.' + methodName, callContext);
return func.apply(this, arguments);
};
}
function instrumentObjectProperty(object, objectName, propertyName, logSettings={}) {
var propDesc = getPropertyDescriptor(object, propertyName);
if (!propDesc) {
return;
}
var originalGetter = propDesc.get;
var originalSetter = propDesc.set;
var originalValue = propDesc.value;
Object.defineProperty(object, propertyName, {
configurable: true,
get: function() {
var origProperty;
var callContext = getOriginatingScriptContext();
if (originalGetter) {
origProperty = originalGetter.call(this);
} else if ('value' in propDesc) {
origProperty = originalValue;
} else {
console.error("Property descriptor for", objectName + '.' + propertyName, "doesn't have getter or value?");
logValue(objectName + '.' + propertyName, callContext);
return;
}
if (typeof origProperty == 'function') {
logValue(objectName + '.' + propertyName, callContext);
return instrumentFunction(objectName, propertyName, origProperty);
} else {
logValue(objectName + '.' + propertyName, callContext);
return origProperty;
}
},
set: function(value) {
var callContext = getOriginatingScriptContext();
var returnValue;
if (originalSetter) {
returnValue = originalSetter.call(this, value);
} else if ('value' in propDesc) {
originalValue = value;
returnValue = value;
} else {
console.error("Property descriptor for", objectName + '.' + propertyName, "doesn't have setter or value?");
returnValue = value;
}
logValue(objectName + '.' + propertyName, callContext);
return returnValue;
}
});
}
var navigatorProperties = [ "appCodeName", "appName", "appVersion",
"buildID", "cookieEnabled", "doNotTrack",
"geolocation", "language", "languages",
"onLine", "oscpu", "platform", "product",
"productSub", "userAgent", "vendorSub",
"vendor", "browserLanguage", "userLanguage",
"appName", "cpuClass" , "mimeTypes", "plugins",
"deviceMemory", "hardwareConcurrency",
"maxTouchPoints"];
navigatorProperties.forEach(function(property) {
instrumentObjectProperty(window.navigator, "window.navigator", property);
});
var screenProperties = [ "pixelDepth", "colorDepth", "width", "height", "availWidth", "availHeight" ];
screenProperties.forEach(function(property) {
instrumentObjectProperty(window.screen, "window.screen", property);
});
// Name, localStorage, and sessionsStorage logging
// Instrumenting window.localStorage directly doesn't seem to work, so the Storage
// prototype must be instrumented instead. Unfortunately this fails to differentiate
// between sessionStorage and localStorage. Instead, you'll have to look for a sequence
// of a get for the localStorage object followed by a getItem/setItem for the Storage object.
var windowProperties = [ "name", "localStorage", "sessionStorage",
"WebGLRenderingContext", "devicePixelRatio", "indexedDB", "openDatabase"
];
windowProperties.forEach(function(property) {
instrumentObjectProperty(window, "window", property);
});
instrumentObject(window.Storage.prototype, "window.Storage");
instrumentObjectProperty(window.document, "window.document", "cookie");
instrumentObject(window.HTMLCanvasElement.prototype,"HTMLCanvasElement");
var excludedProperties = [ "quadraticCurveTo", "lineTo", "transform",
"globalAlpha", "moveTo", "drawImage",
"setTransform", "clearRect", "closePath",
"beginPath", "canvas", "translate" ];
instrumentObject(
window.CanvasRenderingContext2D.prototype,
"CanvasRenderingContext2D",
{'excludedProperties': excludedProperties}
);
instrumentObjectProperty(window.Date.prototype, "window.Date", "getTimezoneOffset");
instrumentObject(window.RTCPeerConnection.prototype,"RTCPeerConnection");
instrumentObject(window.AudioContext.prototype, "AudioContext");
instrumentObject(window.OfflineAudioContext.prototype, "OfflineAudioContext");
instrumentObject(window.OscillatorNode.prototype, "OscillatorNode");
instrumentObject(window.AnalyserNode.prototype, "AnalyserNode");
instrumentObject(window.GainNode.prototype, "GainNode");
instrumentObject(window.ScriptProcessorNode.prototype, "ScriptProcessorNode");
console.debug("Privacy Crawler: Successfully started all instrumentation.");
}