From 93ed8d6c4423b9b7e32c31dc9cf129a5cee8f887 Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Fri, 18 Aug 2023 19:33:49 +0100 Subject: [PATCH] Working on lambda function for malware, phishing and viruses check --- api/malware.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 api/malware.js diff --git a/api/malware.js b/api/malware.js new file mode 100644 index 0000000..3b4e226 --- /dev/null +++ b/api/malware.js @@ -0,0 +1,61 @@ +const axios = require('axios'); +const xml2js = require('xml2js'); +const middleware = require('./_common/middleware'); + +const getUrlHausResult = async (url) => { + let domain = new URL(url).hostname; + return await axios({ + method: 'post', + url: 'https://urlhaus-api.abuse.ch/v1/host/', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + data: `host=${domain}` + }) + .then((x) => x.data) + .catch((e) => ({ error: `Request to URLHaus failed, ${e.message}`})); +}; + + +const getPhishTankResult = async (url) => { + try { + const encodedUrl = Buffer.from(url).toString('base64'); + const endpoint = `https://checkurl.phishtank.com/checkurl/?url=${encodedUrl}`; + const headers = { + 'User-Agent': 'phishtank/web-check', + }; + const response = await axios.post(endpoint, null, { headers, timeout: 3000 }); + const parsed = await xml2js.parseStringPromise(response.data, { explicitArray: false }); + return parsed.response.results; + } catch (error) { + return { error: `Request to PhishTank failed: ${error.message}` }; + } +} + +const getCloudmersiveResult = async (url) => { + try { + const endpoint = 'https://api.cloudmersive.com/virus/scan/website'; + const headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Apikey': process.env.CLOUDMERSIVE_API_KEY, + }; + const data = `Url=${encodeURIComponent(url)}`; + const response = await axios.post(endpoint, data, { headers }); + return response.data; + } catch (error) { + return { error: `Request to Cloudmersive failed: ${error.message}` }; + } +}; + +const handler = async (url) => { + try { + const urlHaus = await getUrlHausResult(url); + const phishTank = await getPhishTankResult(url); + const cloudmersive = await getCloudmersiveResult(url); + return JSON.stringify({ urlHaus, phishTank, cloudmersive }); + } catch (error) { + throw new Error(error.message); + } +}; + +exports.handler = middleware(handler);