blob: 450a4af2b16df180a238c5b636930a165ce8ba21 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import fetch from "node-fetch";
TheCodedProfb5e9d552023-01-29 15:43:26 -05002import fs, { writeFileSync, createReadStream } from "fs";
pineafan63fc5e22022-08-04 22:04:10 +01003import generateFileName from "../utils/temp/generateFileName.js";
4import Tesseract from "node-tesseract-ocr";
5import type Discord from "discord.js";
pineafan3a02ea32022-08-11 21:35:04 +01006import client from "../utils/client.js";
TheCodedProfb5e9d552023-01-29 15:43:26 -05007import { createHash } from "crypto";
TheCodedProfd8ef1f32023-03-06 19:15:18 -05008import * as nsfwjs from 'nsfwjs';
9import * as clamscan from 'clamscan'
10import * as tf from "@tensorflow/tfjs-node";
pineafan813bdf42022-07-24 10:39:10 +010011
Skyler Grey75ea9172022-08-06 10:22:23 +010012interface NSFWSchema {
13 nsfw: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050014 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010015}
16interface MalwareSchema {
17 safe: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050018 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010019}
pineafan813bdf42022-07-24 10:39:10 +010020
TheCodedProfd8ef1f32023-03-06 19:15:18 -050021const model = await nsfwjs.load();
22
pineafan02ba0232022-07-24 22:16:15 +010023export async function testNSFW(link: string): Promise<NSFWSchema> {
TheCodedProfd8ef1f32023-03-06 19:15:18 -050024 const [fileName, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000025 const alreadyHaveCheck = await client.database.scanCache.read(hash);
26 if (alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
TheCodedProfd8ef1f32023-03-06 19:15:18 -050027
28 // const image = tf.node.decodePng()
29
30 // const result = await model.classify(image)
31
32 return { nsfw: false };
pineafan813bdf42022-07-24 10:39:10 +010033}
34
pineafan02ba0232022-07-24 22:16:15 +010035export async function testMalware(link: string): Promise<MalwareSchema> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050036 const [p, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000037 const alreadyHaveCheck = await client.database.scanCache.read(hash);
38 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data };
TheCodedProfb5e9d552023-01-29 15:43:26 -050039 const data = new URLSearchParams();
PineaFanb0d0c242023-02-05 10:59:45 +000040 const f = createReadStream(p);
TheCodedProfb5e9d552023-01-29 15:43:26 -050041 data.append("file", f.read(fs.statSync(p).size));
pineafan3a02ea32022-08-11 21:35:04 +010042 const result = await fetch("https://unscan.p.rapidapi.com/malware", {
43 method: "POST",
44 headers: {
45 "X-RapidAPI-Key": client.config.rapidApiKey,
46 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
47 },
48 body: data
49 })
Skyler Greyda16adf2023-03-05 10:22:12 +000050 .then((response) =>
51 response.status === 200 ? (response.json() as Promise<MalwareSchema>) : { safe: true, errored: true }
52 )
pineafan3a02ea32022-08-11 21:35:04 +010053 .catch((err) => {
54 console.error(err);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050055 return { safe: true, errored: true };
pineafan3a02ea32022-08-11 21:35:04 +010056 });
TheCodedProf5b53a8c2023-02-03 15:40:26 -050057 if (!result.errored) {
58 client.database.scanCache.write(hash, result.safe);
59 }
pineafan3a02ea32022-08-11 21:35:04 +010060 return { safe: result.safe };
61}
62
63export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
Skyler Greyda16adf2023-03-05 10:22:12 +000064 const alreadyHaveCheck = await client.database.scanCache.read(link);
65 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] };
TheCodedProfb5e9d552023-01-29 15:43:26 -050066 const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", {
pineafan3a02ea32022-08-11 21:35:04 +010067 method: "POST",
68 headers: {
69 "X-RapidAPI-Key": client.config.rapidApiKey,
70 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
71 },
72 body: `{"link":"${link}"}`
73 })
74 .then((response) => response.json() as Promise<MalwareSchema>)
75 .catch((err) => {
76 console.error(err);
77 return { safe: true, tags: [] };
78 });
TheCodedProfb5e9d552023-01-29 15:43:26 -050079 client.database.scanCache.write(link, scanned.safe ?? true, []);
pineafan3a02ea32022-08-11 21:35:04 +010080 return {
81 safe: scanned.safe ?? true,
82 tags: scanned.tags ?? []
83 };
pineafan813bdf42022-07-24 10:39:10 +010084}
85
TheCodedProfb5e9d552023-01-29 15:43:26 -050086export async function saveAttachment(link: string): Promise<[string, string]> {
Skyler Greyda16adf2023-03-05 10:22:12 +000087 const image = await (await fetch(link)).arrayBuffer();
Skyler Grey75ea9172022-08-06 10:22:23 +010088 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050089 const enc = new TextDecoder("utf-8");
90 writeFileSync(fileName, new DataView(image), "base64");
Skyler Greyda16adf2023-03-05 10:22:12 +000091 return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")];
pineafan813bdf42022-07-24 10:39:10 +010092}
93
pineafan813bdf42022-07-24 10:39:10 +010094const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 PHISHING: "Links designed to trick users into clicking on them.",
96 DATING: "Dating sites.",
97 TRACKERS: "Websites that store or track personal information.",
98 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +010099 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 AMP: "AMP pages. (AMP is a technology that allows websites to be served by Google. Read more on /privacy)",
pineafan813bdf42022-07-24 10:39:10 +0100101 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100102 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 PORN: "Websites that include pornography.",
104 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100105 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100107 RANSOMWARE: "Websites which download a program that can steal your data and make you pay to get it back.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
109 SCAMS: "Sites which are designed to trick you into doing something.",
110 TORRENT: "Websites that download torrent files.",
111 HATE: "Websites that spread hate towards groups or individuals.",
112 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +0100113};
pineafan813bdf42022-07-24 10:39:10 +0100114export { linkTypes };
115
pineafan63fc5e22022-08-04 22:04:10 +0100116export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 const links =
118 message.content.match(
119 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
120 ) ?? [];
121 const detections: { tags: string[]; safe: boolean }[] = [];
122 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +0100123 let returned;
pineafan813bdf42022-07-24 10:39:10 +0100124 try {
Skyler Grey11236ba2022-08-08 21:13:33 +0100125 if (element.match(/https?:\/\/[a-zA-Z]+\.?discord(app)?\.(com|net)\/?/)) return; // Also matches discord.net, not enough of a bug
pineafan63fc5e22022-08-04 22:04:10 +0100126 returned = await testLink(element);
127 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +0100129 return;
130 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +0100132 });
133 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 const detectionsTypes = detections
135 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +0100136 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 if (type) return type;
138 // if (!element.safe) return "UNSAFE"
139 return undefined;
140 })
141 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100142 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100143}
144
pineafan63fc5e22022-08-04 22:04:10 +0100145export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100146 try {
TheCodedProfb5e9d552023-01-29 15:43:26 -0500147 return (await testNSFW(element)).nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100148 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100149 return false;
pineafan813bdf42022-07-24 10:39:10 +0100150 }
151}
152
Skyler Grey11236ba2022-08-08 21:13:33 +0100153export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100154 if (element.height === null || element.width === null) return true;
155 if (element.height < 20 || element.width < 20) return false;
156 return true;
pineafan813bdf42022-07-24 10:39:10 +0100157}
158
pineafan63fc5e22022-08-04 22:04:10 +0100159export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100160 try {
pineafan63fc5e22022-08-04 22:04:10 +0100161 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100162 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100163 return true;
pineafan813bdf42022-07-24 10:39:10 +0100164 }
165}
166
Skyler Grey11236ba2022-08-08 21:13:33 +0100167export function TestString(string: string, soft: string[], strict: string[]): object | null {
Skyler Grey75ea9172022-08-06 10:22:23 +0100168 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100169 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100170 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100171 }
172 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100173 for (const word of soft) {
174 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100175 if (word2 === word) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100176 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100177 }
178 }
179 }
pineafan63fc5e22022-08-04 22:04:10 +0100180 return null;
pineafan813bdf42022-07-24 10:39:10 +0100181}
182
pineafan63fc5e22022-08-04 22:04:10 +0100183export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100184 const text = await Tesseract.recognize(url, {
185 lang: "eng",
186 oem: 1,
187 psm: 3
188 });
pineafan813bdf42022-07-24 10:39:10 +0100189 return text;
190}