-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackground.js
executable file
·257 lines (215 loc) · 9.38 KB
/
background.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
let badges = {};
var notificationsAllowed = true;
function notifyPortScanning() {
browser.notifications.create("port-scanning-notification", {
"type": "basic",
"iconUrl": browser.runtime.getURL("icons/logo-96.png"),
"title": "This site attempted to port scan you!",
"message": "Port Authority has blocked this site from bypassing security measures and port scanning your private network."
});
}
function notifyThreatMetrix() {
browser.notifications.create("threatmetrix-notification", {
"type": "basic",
"iconUrl": browser.runtime.getURL("icons/logo-96.png"),
"title": "This site attempted to track you!",
"message": "Port Authority dynamically blocked a hidden LexisNexis endpoint from running an invasive data collection script."
});
}
/**
* Adds the host and port of the provided url to a list of hosts and ports that were blocked from port scanning.
*
* @param {string} tabId Id the of the browser tab the port check was executed in
* @param {URL} url URL object built from the url of the tab associated with the tabID
*/
const addBlockedPortToHost = async (url, tabIdString) => {
const tabId = parseInt(tabIdString);
const host = url.host.split(":")[0];
const port = "" + (url.port || getPortForProtocol(url.protocol));
// Grab the blocked ports object from extensions storage
const blocked_ports = await getItemFromLocal("blocked_ports", {});
// Grab the array of ports blocked for the host url
const tab_hosts = blocked_ports[tabId] || {};
const hosts_ports = tab_hosts[host];
if (Array.isArray(hosts_ports)) {
// Add the port to the array of blocked ports for this host IFF the port doesn't exist
if (hosts_ports.indexOf(port) === -1 && port !== 'undefined') {
const hosts_ports = tab_hosts[host].concat([port]);
tab_hosts[host] = hosts_ports;
}
} else {
tab_hosts[host] = [port];
}
blocked_ports[tabId] = tab_hosts;
await setItemInLocal("blocked_ports", blocked_ports);
return;
}
/**
* Adds the host and port of the provided url to a list of hosts and ports that were blocked from port scanning.
*
* @param {string} tabId Id the of the browser tab the port check was executed in
* @param {URL} url URL object built from the url of the tab associated with the tabID
*/
const addBlockedTrackingHost = async (url, tabIdString) => {
const tabId = parseInt(tabIdString);
const host = url.host;
const blocked_hosts_tabs = await getItemFromLocal("blocked_hosts", {});
let blocked_hosts = blocked_hosts_tabs[tabId] || [];
if (blocked_hosts.indexOf(host) === -1) {
blocked_hosts = blocked_hosts.concat([host]);
}
blocked_hosts_tabs[tabId] = blocked_hosts;
await setItemInLocal("blocked_hosts", blocked_hosts_tabs);
return;
}
async function cancel(requestDetails) {
// This regex is explained here https://regex101.com/r/DOPCdB/17/ below I needed to change \b -> \\b
let local_filter = new RegExp("\\b(^(http|https|wss|ws|ftp|ftps):\/\/127[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\/\/(10)([.](25[0-5]|2[0-4][0-9]|1[0-9]{1,2}|[0-9]{1,2})){3}|^(http|https|wss|ws|ftp|ftps):\/\/localhost|^(http|https|wss|ws|ftp|ftps):\/\/172[.](0?16|0?17|0?18|0?19|0?20|0?21|0?22|0?23|0?24|0?25|0?26|0?27|0?28|0?29|0?30|0?31)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\/\/192[.]168[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|^(http|https|wss|ws|ftp|ftps):\/\/169[.]254[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:\/([789]|1?[0-9]{2}))?\\b", "i");
// Create a regex to find all sub-domains for online-metrix.net Explained here https://regex101.com/r/f8LSTx/2
let thm = new RegExp("online-metrix[.]net$", "i");
const allowed_domains_list = await getItemFromLocal("allowed_domain_list", []);
const check_allowed_url = new URL(requestDetails.originUrl)
const domainIsWhiteListed = allowed_domains_list.some((domain) => check_allowed_url.host.includes(domain));
if (domainIsWhiteListed){
return { cancel: false };
}
// This reduces having to check this conditional multiple times
let is_requested_local = requestDetails.url.search(local_filter);
// Make sure we are not searching the CNAME of local addresses
if (is_requested_local !== 0) {
// Parse the URL
let url = new URL(requestDetails.url);
// Send a request to get the CNAME of the webrequest
let resolving = await browser.dns.resolve(url.host, ["canonical_name"]);
// If the CNAME redirects to a online-metrix.net domain -> Block
if (resolving.canonicalName.search(thm) !== -1) {
let tabId = requestDetails.tabId;
increaseBadged(requestDetails);
await addBlockedTrackingHost(url, tabId);
if (badges[tabId].alerted == 0 && notificationsAllowed) {
notifyThreatMetrix();
badges[tabId].alerted += 1;
}
return { cancel: true };
}
}
// Check if the network request is going to a local address
// search should return a 0 for the 0th index of the string
// if a match is further down the URL, it is probably a FP
if (is_requested_local === 0) {
// Check if the current website visited is a local address
if (requestDetails.originUrl.search(local_filter) !== 0) {
// Increase the badge counter
let tabId = requestDetails.tabId;
increaseBadged(requestDetails);
let url = new URL(requestDetails.url);
await addBlockedPortToHost(url, tabId);
if (badges[tabId].alerted == 0 && notificationsAllowed) {
notifyPortScanning();
badges[tabId].alerted += 1;
}
// Cancel the request
return { cancel: true };
}
}
// Dont block sites that don't alert the detection
return { cancel: false };
} // end cancel()
async function start() { // Enables blocking
try {
await browser.storage.local.clear();
localStorage.setItem("state", true);
setItemInLocal("allowed_domain_list", []);
//Add event listener
browser.webRequest.onBeforeRequest.addListener(
cancel,
{ urls: ["<all_urls>"] }, // Match all HTTP, HTTPS, FTP, FTPS, WS, WSS URLs.
["blocking"] // if cancel() returns true block the request.
);
} catch (e) {
console.log("START() ", e);
}
}
function stop() { // Disables blocking
try {
localStorage.setItem("state", false);
//Remove event listener
browser.webRequest.onBeforeRequest.removeListener(cancel);
} catch (e) {
console.log("STOP() ", e);
}
}
function setNotificationsAllowed(value) { // toggles notifications
notificationsAllowed = value;
}
function isListening() { // returns if blocking is on
return browser.webRequest.onBeforeRequest.hasListener(cancel);
}
/**
* Increases the badged by one.
* Borrowed and modified from https://gitlab.com/KevinRoebert/ClearUrls/-/blob/master/core_js/badgedHandler.js
*/
function increaseBadged(request) {
// Error check
if (request === null) return;
const tabId = request.tabId;
const url = request.url;
if (tabId === -1) return;
if (badges[tabId] == null) {
badges[tabId] = {
counter: 1,
alerted: 0,
lastURL: url
};
} else {
badges[tabId].counter += 1;
}
browser.browserAction.setBadgeText({ text: (badges[tabId]).counter.toString(), tabId: tabId });
}
/**
* Call by each tab is updated.
* And if url has changed.
* Borrowed and modified from https://gitlab.com/KevinRoebert/ClearUrls/-/blob/master/core_js/badgedHandler.js
*/
async function handleUpdated(tabId, changeInfo, tabInfo) {
if (!badges[tabId] || !changeInfo.url) return;
if (badges[tabId].lastURL !== changeInfo.url) {
badges[tabId] = {
counter: 0,
alerted: 0,
lastURL: tabInfo.url
};
// Clear out the blocked ports for the current tab
const blocked_ports_object = await getItemFromLocal("blocked_ports", {});
blocked_ports_object[tabId] = {};
await setItemInLocal("blocked_ports", blocked_ports_object);
// Clear out the hosts for the current tab
const blocked_hosts_object = await getItemFromLocal("blocked_hosts", {});
blocked_hosts_object[tabId] = [];
await setItemInLocal("blocked_hosts", blocked_hosts_object);
}
}
function onMessage(message, sender, sendResponse) {
switch(message.type) {
case 'popupInit':
sendResponse({
isListening: isListening(),
notificationsAllowed,
});
break;
case 'toggleEnabled':
message.value ? start() : stop();
break;
case 'setNotificationsAllowed':
setNotificationsAllowed(message.value);
break;
default:
console.warn('Port Authority: unknown message: ', message);
break;
}
}
browser.runtime.onMessage.addListener(onMessage);
start();
// Call by each tab is updated.
browser.tabs.onUpdated.addListener(handleUpdated);
browser.runtime.onStartup.addListener(async () => { await browser.storage.local.clear() })