-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
211 lines (167 loc) · 8.23 KB
/
app.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
const express = require('express');
const fetch = require('node-fetch');
const base64url = require('base64url');
const { sleep, isIPv4Address, isIPv6Address, isHash, isURL, isDomain } = require('./helpers');
const app = express();
const vt_api = 'API_KEY_HERE'; // PUT YOUR VirusTotal API KEY HERE
const ab_api = 'API_KEY_HERE'; // PUT YOUR AbuseIPDB API KEY HERE
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
const maxApiRequests = 500;
app.post('/checkEntries', async (req, res) => {
let apiRequestCount = 0;
if (apiRequestCount >= maxApiRequests) {
return res.status(500).json({ success: false, message: 'API request limit exceeded' });
}
const { entries } = req.body;
const numEntries = entries.length;
for (let i = 0; i < numEntries; i++) {
const trimmedEntry = entries[i].trim();
if (isIPv4Address(trimmedEntry)) {
const ip = trimmedEntry;
const url = `https://www.virustotal.com/api/v3/ip_addresses/${ip}`;
try {
const response = await fetch(url, {
headers: {
'x-apikey': vt_api
}
});
const data = await response.json();
res.write(JSON.stringify({ type: 'IPv4', entry: ip, result: data }) + '\n');
} catch (error) {
console.error(`Error occurred while checking IP ${ip}:`, error);
res.write(JSON.stringify({ type: 'IPv4', entry: ip, error: `Error occurred while checking IP ${ip}.` }) + '\n');
}
}
if (isIPv6Address(trimmedEntry)) {
const ip = trimmedEntry;
try {
const response = await fetch(`https://api.abuseipdb.com/api/v2/check?ipAddress=${ip}`, {
headers: {
Key: ab_api,
Accept: 'application/json'
}
});
const responseData = await response.json();
if (response.ok) {
res.write(JSON.stringify({ type: 'IPv6', entry: ip, result: responseData }) + '\n');
} else {
throw new Error(`Error occurred while checking IPv6 address ${ip}: ${responseData.errors[0].detail}`);
}
} catch (error) {
console.error(`Error occurred while checking IPv6 address ${ip}:`, error);
res.write(JSON.stringify({ type: 'IPv6', entry: ip, error: `Error occurred while checking IPv6 address ${ip}.` }) + '\n');
}
}
if (isURL(trimmedEntry) && !isIPv6Address(trimmedEntry) && !isIPv4Address(trimmedEntry) && !isHash(trimmedEntry)) {
const input_url = trimmedEntry;
const url_id = base64url(input_url);
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
try {
const vt_response = await fetch('https://www.virustotal.com/api/v3/urls', {
method: 'POST',
headers: {
'x-apikey': vt_api,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
url: input_url,
}),
});
if (vt_response.ok) {
await sleep(10000);
try {
const response = await fetch(`https://www.virustotal.com/api/v3/urls/${url_id}`, {
headers: {
'x-apikey': vt_api,
},
});
if (response.ok) {
const data = await response.json();
res.write(JSON.stringify({ type: 'URL', entry: input_url, result: data }) + '\n');
} else {
const data = await response.json();
res.write(JSON.stringify({ type: 'URLerror', entry: input_url, result: data }) + '\n');
}
} catch (error) {
res.write(JSON.stringify({ type: 'URL', entry: input_url, error: `Error fetching scan results for URL: "${input_url}"` }) + '\n');
}
} else {
const vt_data = await vt_response.json();
res.write(JSON.stringify({ type: 'URL', entry: input_url, error: `Error submitting URL to VirusTotal: ${vt_data.error.message}` }) + '\n');
}
} catch (error) {
res.write(JSON.stringify({ type: 'URL', entry: input_url, error: `Error submitting URL to VirusTotal: ${error.message}` }) + '\n');
}
}
if (isDomain(trimmedEntry) && !isURL(trimmedEntry) && !isIPv6Address(trimmedEntry) && !isIPv4Address(trimmedEntry) && !isHash(trimmedEntry)) {
const domain = trimmedEntry;
try {
const response = await fetch(`https://www.virustotal.com/api/v3/domains/${domain}`, {
headers: {
'x-apikey': vt_api
}
});
if (response.ok) {
const data = await response.json();
res.write(JSON.stringify({ type: 'Domain', entry: domain, result: data }) + '\n');
} else if (!response.ok) {
const data = await response.json();
res.write(JSON.stringify({ type: 'Domainerror', entry: domain, result: data }) + '\n');
}
} catch (error) {
res.write(JSON.stringify({ type: 'Domain', entry: domain, error: `No records found for the domain: "${domain}"` }) + '\n');
}
}
if (!isIPv4Address(trimmedEntry) && !isIPv6Address(trimmedEntry) && isHash(trimmedEntry)) {
const hash = trimmedEntry;
let hashType;
switch (hash.length) {
case 32:
hashType = 'MD5';
break;
case 40:
hashType = 'SHA-1';
break;
case 64:
hashType = 'SHA-256';
break;
default:
hashType = 'Unknown';
}
try {
const response = await fetch(`https://www.virustotal.com/vtapi/v2/file/report?apikey=${vt_api}&resource=${hash}`);
apiRequestCount++;
const result = await response.json();
let status, enginesDetected;
if (typeof result.positives === 'undefined') {
status = 'Unknown';
} else if (result.positives !== 0) {
status = 'Malicious';
enginesDetected = result.positives;
} else {
status = 'Clean';
}
const formattedResult = {
hash: hash,
type: hashType,
status: status,
enginesDetected: enginesDetected
};
res.write(JSON.stringify({ type: 'Hash', entry: hash, result: formattedResult }) + '\n');
} catch (error) {
console.error(`Error occurred while checking hash ${hash}:`, error);
res.write(JSON.stringify({ type: 'Hash', entry: hash, error: `Error occurred while checking hash ${hash}.` }) + '\n');
}
}
if (numEntries >= 4 && i < numEntries - 1) {
await sleep(15000);
}
}
res.end();
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});