-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
149 lines (129 loc) · 4.47 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
// consider moving to user facing options
const resuse_th_tab = true;
// Set up context menu at install time.
chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create({
"contexts": ['selection'],
"title": "!Search in Threat Hunting",
"id": "search_th"
});
});
function isIpAddress(ioc) {
const ipAddressRegEx = /^(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]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipAddressRegEx.test(ioc) ? true : false;
}
function isMd5Hash(ioc) {
const md5RegEx = /\b[A-Fa-f0-9]{32}\b/;
return md5RegEx.test(ioc) ? true : false;
}
// build TH query URL
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function buildThUrl(q) {
// last week
const oneWeekInMs = 1000 * 60 * 60 * 24 * 7;
q.from = ((new Date(Date.now() - oneWeekInMs)).toISOString());
q.to = (new Date).toISOString();
let encodedQ = b64EncodeUnicode(JSON.stringify(q))
let qUrl = `https://portal.checkpoint.com/dashboard/endpoint/threathunting#/search/results?query=${encodedQ}`;
return qUrl
}
function openNewTabWithUrl(url) {
// reuse TH tab
if (resuse_th_tab) {
// look for already open TH
chrome.tabs.query(
{ url: "https://portal.checkpoint.com/dashboard/endpoint/threathunting*", currentWindow: true },
(tabs) => {
if (tabs.length) {
console.log("TH tabs found", tabs);
chrome.tabs.update(tabs[0].id, { active: true, url: url });
} else {
console.log('no TH tab found');
chrome.tabs.create({ url: url }, function (tab) {
tabId = tab.id;
});
}
}
);
} else {
// no reuse - always open new tab
chrome.tabs.create({ url: url }, function (tab) {
tabId = tab.id;
});
}
}
// ioc is text from right-click context menu on selection
function searchThreatHuntingFor(ioc) {
console.log('searchThreatHuntingFor', ioc);
isIpAddress(ioc) && console.log('\tIP address:', ioc);
isMd5Hash(ioc) && console.log('\tMD5 hash:', ioc);
if (isIpAddress(ioc)) {
const ipAddressThQueryTemplate = {
"from": "2021-12-02T14:40:44.582Z",
"to": "2021-12-09T14:40:44.582Z",
"indicators": [
{
"fieldArr": [
ioc
],
"fieldType": "NetworkDestIP",
"operator": "Is"
}
],
"recordType": "Network",
"timespanType": "custom"
}
openNewTabWithUrl(buildThUrl(ipAddressThQueryTemplate));
} else if (isMd5Hash(ioc)) {
const md5ThQueryTemplate = {
"from": "2021-12-02T14:40:44.582Z",
"to": "2021-12-09T14:40:44.582Z",
"indicators": [
{
"fieldArr": [
ioc
],
"fieldType": "FileMD5",
"operator": "Is"
}
],
"recordType": "File",
"timespanType": "custom"
}
openNewTabWithUrl(buildThUrl(md5ThQueryTemplate));
} else {
// everything else is considered domain
let domainThQueryTemplate = {
"from": "2021-12-02T14:40:44.582Z",
"to": "2021-12-09T14:40:44.582Z",
"indicators": [
{
"fieldArr": [
ioc
],
"fieldType": "NetworkDomain",
"operator": "Is"
}
],
"recordType": "Network",
"timespanType": "custom"
}
openNewTabWithUrl(buildThUrl(domainThQueryTemplate));
}
}
function onContextMenuClicked(info) {
console.log(JSON.stringify(info));
// handle context menu
const { menuItemId, selectionText } = info;
// search for selected text (IoC)
if (menuItemId === 'search_th') {
if (typeof selectionText === 'string') {
searchThreatHuntingFor(selectionText.trim());
}
}
}
chrome.contextMenus.onClicked.addListener(onContextMenuClicked);