pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import fetch from "node-fetch"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 2 | import fs, { writeFileSync, createReadStream } from "fs"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import generateFileName from "../utils/temp/generateFileName.js"; |
| 4 | import Tesseract from "node-tesseract-ocr"; |
| 5 | import type Discord from "discord.js"; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 6 | import client from "../utils/client.js"; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 7 | import { createHash } from "crypto"; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 8 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 9 | interface NSFWSchema { |
| 10 | nsfw: boolean; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 11 | errored?: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | } |
| 13 | interface MalwareSchema { |
| 14 | safe: boolean; |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 15 | errored?: boolean; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | } |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 17 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 18 | export async function testNSFW(link: string): Promise<NSFWSchema> { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 19 | const [p, hash] = await saveAttachment(link); |
| 20 | const alreadyHaveCheck = await client.database.scanCache.read(hash) |
| 21 | if(alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data }; |
| 22 | const data = new URLSearchParams(); |
| 23 | const r = createReadStream(p) |
| 24 | data.append("file", r.read(fs.statSync(p).size)); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 25 | const result = await fetch("https://unscan.p.rapidapi.com/", { |
| 26 | method: "POST", |
| 27 | headers: { |
| 28 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 29 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 30 | }, |
| 31 | body: data |
| 32 | }) |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 33 | .then((response) => response.status === 200 ? response.json() as Promise<NSFWSchema> : { nsfw: false, errored: true }) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 34 | .catch((err) => { |
| 35 | console.error(err); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 36 | return { nsfw: false, errored: true }; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 37 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 38 | if(!result.errored) { |
| 39 | client.database.scanCache.write(hash, result.nsfw); |
| 40 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 41 | return { nsfw: result.nsfw }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 42 | } |
| 43 | |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 44 | export async function testMalware(link: string): Promise<MalwareSchema> { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 45 | const [p, hash] = await saveAttachment(link); |
| 46 | const alreadyHaveCheck = await client.database.scanCache.read(hash) |
| 47 | if(alreadyHaveCheck) return { safe: alreadyHaveCheck.data }; |
| 48 | const data = new URLSearchParams(); |
| 49 | const f = createReadStream(p); |
| 50 | data.append("file", f.read(fs.statSync(p).size)); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 51 | const result = await fetch("https://unscan.p.rapidapi.com/malware", { |
| 52 | method: "POST", |
| 53 | headers: { |
| 54 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 55 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 56 | }, |
| 57 | body: data |
| 58 | }) |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 59 | .then((response) => response.status === 200 ? response.json() as Promise<MalwareSchema> : { safe: true, errored: true }) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 60 | .catch((err) => { |
| 61 | console.error(err); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 62 | return { safe: true, errored: true }; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 63 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 64 | if (!result.errored) { |
| 65 | client.database.scanCache.write(hash, result.safe); |
| 66 | } |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 67 | return { safe: result.safe }; |
| 68 | } |
| 69 | |
| 70 | export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 71 | const alreadyHaveCheck = await client.database.scanCache.read(link) |
| 72 | if(alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] }; |
| 73 | const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 74 | method: "POST", |
| 75 | headers: { |
| 76 | "X-RapidAPI-Key": client.config.rapidApiKey, |
| 77 | "X-RapidAPI-Host": "unscan.p.rapidapi.com" |
| 78 | }, |
| 79 | body: `{"link":"${link}"}` |
| 80 | }) |
| 81 | .then((response) => response.json() as Promise<MalwareSchema>) |
| 82 | .catch((err) => { |
| 83 | console.error(err); |
| 84 | return { safe: true, tags: [] }; |
| 85 | }); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 86 | client.database.scanCache.write(link, scanned.safe ?? true, []); |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 87 | return { |
| 88 | safe: scanned.safe ?? true, |
| 89 | tags: scanned.tags ?? [] |
| 90 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 93 | export async function saveAttachment(link: string): Promise<[string, string]> { |
| 94 | const image = await (await fetch(link)).arrayBuffer() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 95 | const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!); |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 96 | const enc = new TextDecoder("utf-8"); |
| 97 | writeFileSync(fileName, new DataView(image), "base64"); |
| 98 | return [fileName, createHash('sha512').update(enc.decode(image), 'base64').digest('base64')]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 99 | } |
| 100 | |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 101 | const linkTypes = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | PHISHING: "Links designed to trick users into clicking on them.", |
| 103 | DATING: "Dating sites.", |
| 104 | TRACKERS: "Websites that store or track personal information.", |
| 105 | ADVERTISEMENTS: "Websites only for ads.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 106 | FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | AMP: "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)", |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 108 | "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 109 | "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | PORN: "Websites that include pornography.", |
| 111 | GAMBLING: "Gambling sites, often scams.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 112 | MALWARE: "Websites which download files designed to break or slow down your device.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | PIRACY: "Sites which include illegally downloaded material.", |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 114 | RANSOMWARE: "Websites which download a program that can steal your data and make you pay to get it back.", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 115 | REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.", |
| 116 | SCAMS: "Sites which are designed to trick you into doing something.", |
| 117 | TORRENT: "Websites that download torrent files.", |
| 118 | HATE: "Websites that spread hate towards groups or individuals.", |
| 119 | JUNK: "Websites that are designed to make you waste time." |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 121 | export { linkTypes }; |
| 122 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 123 | export async function LinkCheck(message: Discord.Message): Promise<string[]> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 124 | const links = |
| 125 | message.content.match( |
| 126 | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi |
| 127 | ) ?? []; |
| 128 | const detections: { tags: string[]; safe: boolean }[] = []; |
| 129 | const promises: Promise<void>[] = links.map(async (element) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 130 | let returned; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 131 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 132 | if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | returned = await testLink(element); |
| 134 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | detections.push({ tags: [], safe: true }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | return; |
| 137 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | detections.push({ tags: returned.tags, safe: returned.safe }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 139 | }); |
| 140 | await Promise.all(promises); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 141 | const detectionsTypes = detections |
| 142 | .map((element) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 143 | const type = Object.keys(linkTypes).find((type) => element.tags.includes(type)); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 144 | if (type) return type; |
| 145 | // if (!element.safe) return "UNSAFE" |
| 146 | return undefined; |
| 147 | }) |
| 148 | .filter((element) => element !== undefined); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | return detectionsTypes as string[]; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 150 | } |
| 151 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 152 | export async function NSFWCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 153 | try { |
Samuel Shuert | 27bf3cd | 2023-03-03 15:51:25 -0500 | [diff] [blame^] | 154 | return (await testNSFW(element)).nsfw; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 155 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | return false; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 160 | export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 161 | if (element.height === null || element.width === null) return true; |
| 162 | if (element.height < 20 || element.width < 20) return false; |
| 163 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 164 | } |
| 165 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | export async function MalwareCheck(element: string): Promise<boolean> { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 167 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 168 | return (await testMalware(element)).safe; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 169 | } catch { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | return true; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 174 | export function TestString(string: string, soft: string[], strict: string[]): object | null { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 175 | for (const word of strict) { |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 176 | if (string.toLowerCase().includes(word)) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 177 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 178 | } |
| 179 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 180 | for (const word of soft) { |
| 181 | for (const word2 of string.match(/[a-z]+/gi) ?? []) { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 182 | if (word2 === word) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 183 | return { word: word, type: "strict" }; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 187 | return null; |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 188 | } |
| 189 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 190 | export async function TestImage(url: string): Promise<string | null> { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 191 | const text = await Tesseract.recognize(url, { |
| 192 | lang: "eng", |
| 193 | oem: 1, |
| 194 | psm: 3 |
| 195 | }); |
pineafan | 813bdf4 | 2022-07-24 10:39:10 +0100 | [diff] [blame] | 196 | return text; |
| 197 | } |