-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
185 lines (165 loc) · 5.4 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
var cachedData = {};
var domTrie = getDomTrie();
//Svuota cache ogni 10 minuti
setInterval(function(){
cachedData = {};
}, 1000*60*10);
//Gestisce richieste dagli script di contenuto
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
if (message.request == 'check_domain'){
domTrie.then(trie => {
resp = trie.search(message.dom);
sendResponse({message: resp});
});
}
return true;
});
//Prendo la tab attiva
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
update(tabs[0]);
});
//Se la finestra corrente cambia, prendo la tab attiva nella nuova finestra.
chrome.windows.onFocusChanged.addListener(function(data) {
//Prendo la tab attiva
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs.length > 0) {
update(tabs[0]);
}
});
});
//Se la tab attiva all'interno della finiestra corrente cambia prendo la nuova tab attiva nella finestra corrente.
chrome.tabs.onActivated.addListener(function(data) {
//Prendo la tab attiva
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
update(tabs[0]);
});
});
//Se una tab viene caricata o ricaricata
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if ('status' in changeInfo && changeInfo['status'] === 'complete') {
update(tab, true);
}
});
function update(tab, reload=false){
if (typeof tab === 'undefined') return;
var url = tab.url;
var tabId = tab.id;
if (typeof url === 'undefined' || typeof tabId === 'undefined') return;
var protocol = getPageProtocol(url)
if (protocol === 'http' || protocol === 'https'){
var certInfo = '';
var hostname = getHostname(url);
if (hostname in cachedData){
if (cachedData[hostname] == 1 && reload)
alert("Warning: Phishing detected!!");
return;
}
if (protocol === 'https'){
getCertInfo(hostname, function(data){
certInfo = JSON.stringify(JSON.parse(data));
chrome.tabs.sendMessage(tabId, {
request: 'get_score',
host: hostname,
cert_info: certInfo,
protocol: protocol,
url: url
},
function(response){
var lastError = chrome.runtime.lastError;
if (lastError){
console.log(lastError.message);
return;
}
if (typeof response !== 'undefined'){
cachedData[hostname] = response.mess;
if (response.mess == 1){
console.log("Phishing");
alert("Warning: Phishing detected!!");
}
}
});
});
} else {
chrome.tabs.sendMessage(tabId, {
request: 'get_score',
host: hostname,
cert_info: certInfo,
protocol: protocol,
url: url
},
function(response){
var lastError = chrome.runtime.lastError;
if (lastError){
console.log(lastError.message);
return;
}
if (typeof response !== 'undefined'){
cachedData[hostname] = response.mess;
if (response.mess == 1){
console.log("Phishing");
alert("Warning: Phishing detected!!");
}
}
});
}
}
}
function getPageProtocol(url) {
if (url.substring(0, 7) === 'http://')
return 'http';
else if (url.substring(0, 8) === 'https://')
return 'https';
else
return '';
}
function getHostname(url) {
var hostname = url.substr(8, url.length - 1 - 8);
for (var i = 8, len = url.length; i < len; i++) {
if (url[i] === '/') {
hostname = url.substr(8, i - 8);
break;
}
}
return hostname;
}
// Necessario un recupero cross-orgin e il server non fornisce un "Access-Control-Allow-Origin response header"
// perciò le richieste cross-orgin vanno fatte nel background script
function getCertInfo(hostname, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
//Gestione dell'evento quando termina
if (xhr.readyState !== 4) {
return;
}
if (typeof this.responseText === 'undefined' || this.responseText.length === 0) {
callback(null);
return;
}
try {
callback(this.responseText);
} catch (e) {
callback(null);
}
};
xhr.open('GET', 'https://api.blupig.net/certificate-info/validate', true);
xhr.setRequestHeader('x-validate-host', hostname);
xhr.send();
}
async function getFile(file){
var resp = await fetch(file);
resp = await resp.text();
return resp;
}
function initTrie(file){
if (file === null) return null;
var domTrie = new Trie();
var domains = file.split('\n');
domains = domains.filter(str => { return !str.startsWith('='); });
domains.forEach(d => {domTrie.insert(d.slice(0, (d.length)-1))});
return domTrie;
}
async function getDomTrie(){
var f = await getFile(chrome.extension.getURL('data/SAN.txt'));
var domTrie = initTrie(f);
return domTrie;
}