-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
160 lines (136 loc) · 5.6 KB
/
background.js
File metadata and controls
160 lines (136 loc) · 5.6 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
// Debug log to ensure service worker is alive
console.log("Background service worker loaded and running");
// Listen for installation
chrome.runtime.onInstalled.addListener(() => {
console.log("VT Hash Fetcher extension installed/updated");
});
// Listen for keyboard shortcut commands
chrome.commands.onCommand.addListener((command) => {
console.log("Shortcut command received:", command);
if (command === "fetch-hashes") {
console.log("Fetching hashes triggered by keyboard shortcut");
// Get active tab in current window
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs[0]) {
console.warn("No active tab found");
return;
}
console.log("Active tab found:", tabs[0].id, tabs[0].url);
// Send message to content script
chrome.tabs.sendMessage(tabs[0].id, { action: "fetchHashes" }, (response) => {
if (chrome.runtime.lastError) {
console.error("Error sending message to content script:", chrome.runtime.lastError);
console.log("Attempting to inject content script...");
// Try to inject content script if it's not loaded
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ['content.js']
}).then(() => {
console.log("Content script injected successfully");
// Retry sending the message
setTimeout(() => {
chrome.tabs.sendMessage(tabs[0].id, { action: "fetchHashes" });
}, 500);
}).catch(err => {
console.error("Failed to inject content script:", err);
});
} else {
console.log("Message sent successfully to content script");
}
});
});
}
});
// Listen for messages from content scripts or popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Background received message:", request.action);
// Fetch a single hash from VirusTotal
if (request.action === "fetchFromVT") {
console.log("Fetching single hash from VT:", request.hash);
fetchHashesFromVT(request.hash)
.then(data => {
console.log("Single hash fetched successfully");
sendResponse({ success: true, data });
})
.catch(error => {
console.error("Error fetching single hash:", error);
sendResponse({ success: false, error: error.message });
});
return true; // Keep message channel open for async
}
// Fetch multiple hashes and open results.html
if (request.action === "fetchMultipleHashes") {
console.log("Fetching multiple hashes:", request.hashes);
const original = Array.isArray(request.hashes) ? request.hashes : [];
const unique = Array.from(new Set(original.map(h => h.toLowerCase())));
const dedupCount = original.length - unique.length;
fetchMultipleHashes(unique)
.then(results => {
console.log("Multiple hashes fetched successfully");
const resultsData = encodeURIComponent(JSON.stringify(results));
chrome.tabs.create({
url: chrome.runtime.getURL(`results.html?data=${resultsData}&dedup=${dedupCount}`)
});
sendResponse({ success: true });
})
.catch(error => {
console.error("Error fetching multiple hashes:", error);
sendResponse({ success: false, error: error.message });
});
return true; // Keep message channel open for async
}
});
// Fetch a single hash from VirusTotal API
async function fetchHashesFromVT(hash) {
console.log("Starting VT API fetch for hash:", hash);
const result = await chrome.storage.sync.get(['vtApiKey']);
const apiKey = result.vtApiKey;
if (!apiKey) {
console.error("No API key found");
throw new Error("Please set your VirusTotal API key in the extension options");
}
console.log("Using API key:", apiKey.substring(0, 10) + "...");
const response = await fetch(`https://www.virustotal.com/api/v3/files/${hash}`, {
method: 'GET',
headers: { 'x-apikey': apiKey }
});
if (!response.ok) {
console.error("VT API error:", response.status, response.statusText);
if (response.status === 404) throw new Error("Hash not found in VirusTotal database");
if (response.status === 401) throw new Error("Invalid API key. Check your settings");
throw new Error(`VirusTotal API error: ${response.status}`);
}
const data = await response.json();
const attributes = data.data.attributes;
console.log("Hash data retrieved successfully");
return {
md5: attributes.md5,
sha1: attributes.sha1,
sha256: attributes.sha256,
ssdeep: attributes.ssdeep,
meaningful_name: attributes.meaningful_name,
size: attributes.size,
type_description: attributes.type_description,
last_analysis_stats: attributes.last_analysis_stats
};
}
// Fetch multiple hashes sequentially to avoid rate limiting
async function fetchMultipleHashes(hashes) {
console.log("Fetching multiple hashes sequentially");
const results = [];
for (const hash of hashes) {
console.log(`Processing hash ${results.length + 1}/${hashes.length}:`, hash);
try {
const data = await fetchHashesFromVT(hash);
results.push({ hash, success: true, data });
console.log(`Successfully fetched hash: ${hash}`);
// Small delay to respect VirusTotal rate limits
await new Promise(resolve => setTimeout(resolve, 500));
} catch (error) {
console.error(`Failed to fetch hash ${hash}:`, error.message);
results.push({ hash, success: false, error: error.message });
}
}
console.log(`Completed fetching ${results.length} hashes`);
return results;
}