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