Post

Check URL via VirusTotal API in Node.js

To scan URLs for bad content we can use the VirusTotal API. Bellow is a snippet that does exactly that. More info: https://docs.virustotal.com/docs/api-overview

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
const express = require('express')
const app = express()
const port = 3000

app.get('/', async (req, res) => {

  const responseUrl = await fetch("https://www.virustotal.com/api/v3/urls", {
    method: "POST",
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'x-apikey': 'GET AND ENTER APIKEY' //  👈 YOU SHOULD DO IT!
    },
    body: new URLSearchParams({ url: "https://dutchflightcrew.nl" })
  });
  const dataUrl = await responseUrl.json();

  const analyses = `https://www.virustotal.com/api/v3/analyses/${dataUrl.data.id}`;
  try {
    const responseAnalyses = await fetch(analyses, {
      method: "GET",
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'x-apikey': 'GET AND ENTER APIKEY'
      }});
    if (!responseAnalyses.ok) {
      throw new Error(`Response status: ${responseAnalyses.status}`);
    }

    const result = await responseAnalyses.json();

    if(result.data.attributes.stats.malicious > 0 || result.data.attributes.stats.suspicious > 0) {
      const show = `Your link has been refused! Malicious: ${result.data.attributes.stats.malicious} Suspicious: ${result.data.attributes.stats.suspicious}`
      res.send(show)
    } else {
      res.send(`ok`)
    }
  } catch (error) {
    console.error(error.message);
    res.send(`unknown error check console`)
  }

})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})
This post is licensed under CC BY 4.0 by the author.