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