blob: f00e82c6f49c3390556c31c8db1e5bab92edae03 [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";
pineafan6de4da52023-03-07 20:43:44 +000011import EmojiEmbed from "../utils/generateEmojiEmbed.js";
12import getEmojiByName from "../utils/getEmojiByName.js";
13import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
pineafan813bdf42022-07-24 10:39:10 +010014
Skyler Grey75ea9172022-08-06 10:22:23 +010015interface NSFWSchema {
16 nsfw: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050017 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010018}
19interface MalwareSchema {
20 safe: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050021 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010022}
pineafan813bdf42022-07-24 10:39:10 +010023
TheCodedProfd8ef1f32023-03-06 19:15:18 -050024const model = await nsfwjs.load();
25
pineafan02ba0232022-07-24 22:16:15 +010026export async function testNSFW(link: string): Promise<NSFWSchema> {
TheCodedProfd8ef1f32023-03-06 19:15:18 -050027 const [fileName, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000028 const alreadyHaveCheck = await client.database.scanCache.read(hash);
29 if (alreadyHaveCheck) return { nsfw: alreadyHaveCheck.data };
TheCodedProfd8ef1f32023-03-06 19:15:18 -050030
31 // const image = tf.node.decodePng()
32
33 // const result = await model.classify(image)
34
35 return { nsfw: false };
pineafan813bdf42022-07-24 10:39:10 +010036}
37
pineafan02ba0232022-07-24 22:16:15 +010038export async function testMalware(link: string): Promise<MalwareSchema> {
TheCodedProfb5e9d552023-01-29 15:43:26 -050039 const [p, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000040 const alreadyHaveCheck = await client.database.scanCache.read(hash);
41 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data };
TheCodedProfb5e9d552023-01-29 15:43:26 -050042 const data = new URLSearchParams();
PineaFanb0d0c242023-02-05 10:59:45 +000043 const f = createReadStream(p);
TheCodedProfb5e9d552023-01-29 15:43:26 -050044 data.append("file", f.read(fs.statSync(p).size));
pineafan3a02ea32022-08-11 21:35:04 +010045 const result = await fetch("https://unscan.p.rapidapi.com/malware", {
46 method: "POST",
47 headers: {
48 "X-RapidAPI-Key": client.config.rapidApiKey,
49 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
50 },
51 body: data
52 })
Skyler Greyda16adf2023-03-05 10:22:12 +000053 .then((response) =>
54 response.status === 200 ? (response.json() as Promise<MalwareSchema>) : { safe: true, errored: true }
55 )
pineafan3a02ea32022-08-11 21:35:04 +010056 .catch((err) => {
57 console.error(err);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050058 return { safe: true, errored: true };
pineafan3a02ea32022-08-11 21:35:04 +010059 });
TheCodedProf5b53a8c2023-02-03 15:40:26 -050060 if (!result.errored) {
61 client.database.scanCache.write(hash, result.safe);
62 }
pineafan3a02ea32022-08-11 21:35:04 +010063 return { safe: result.safe };
64}
65
66export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
Skyler Greyda16adf2023-03-05 10:22:12 +000067 const alreadyHaveCheck = await client.database.scanCache.read(link);
68 if (alreadyHaveCheck) return { safe: alreadyHaveCheck.data, tags: [] };
TheCodedProfb5e9d552023-01-29 15:43:26 -050069 const scanned: { safe?: boolean; tags?: string[] } = await fetch("https://unscan.p.rapidapi.com/link", {
pineafan3a02ea32022-08-11 21:35:04 +010070 method: "POST",
71 headers: {
72 "X-RapidAPI-Key": client.config.rapidApiKey,
73 "X-RapidAPI-Host": "unscan.p.rapidapi.com"
74 },
75 body: `{"link":"${link}"}`
76 })
77 .then((response) => response.json() as Promise<MalwareSchema>)
78 .catch((err) => {
79 console.error(err);
80 return { safe: true, tags: [] };
81 });
TheCodedProfb5e9d552023-01-29 15:43:26 -050082 client.database.scanCache.write(link, scanned.safe ?? true, []);
pineafan3a02ea32022-08-11 21:35:04 +010083 return {
84 safe: scanned.safe ?? true,
85 tags: scanned.tags ?? []
86 };
pineafan813bdf42022-07-24 10:39:10 +010087}
88
TheCodedProfb5e9d552023-01-29 15:43:26 -050089export async function saveAttachment(link: string): Promise<[string, string]> {
Skyler Greyda16adf2023-03-05 10:22:12 +000090 const image = await (await fetch(link)).arrayBuffer();
Skyler Grey75ea9172022-08-06 10:22:23 +010091 const fileName = generateFileName(link.split("/").pop()!.split(".").pop()!);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050092 const enc = new TextDecoder("utf-8");
93 writeFileSync(fileName, new DataView(image), "base64");
Skyler Greyda16adf2023-03-05 10:22:12 +000094 return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")];
pineafan813bdf42022-07-24 10:39:10 +010095}
96
pineafan813bdf42022-07-24 10:39:10 +010097const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +010098 PHISHING: "Links designed to trick users into clicking on them.",
99 DATING: "Dating sites.",
100 TRACKERS: "Websites that store or track personal information.",
101 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100102 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 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 +0100104 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100105 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 PORN: "Websites that include pornography.",
107 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100108 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 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 +0100111 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
112 SCAMS: "Sites which are designed to trick you into doing something.",
113 TORRENT: "Websites that download torrent files.",
114 HATE: "Websites that spread hate towards groups or individuals.",
115 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +0100116};
pineafan813bdf42022-07-24 10:39:10 +0100117export { linkTypes };
118
pineafan63fc5e22022-08-04 22:04:10 +0100119export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100120 const links =
121 message.content.match(
122 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
123 ) ?? [];
124 const detections: { tags: string[]; safe: boolean }[] = [];
125 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +0100126 let returned;
pineafan813bdf42022-07-24 10:39:10 +0100127 try {
Skyler Grey11236ba2022-08-08 21:13:33 +0100128 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 +0100129 returned = await testLink(element);
130 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +0100132 return;
133 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +0100135 });
136 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100137 const detectionsTypes = detections
138 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +0100139 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 if (type) return type;
141 // if (!element.safe) return "UNSAFE"
142 return undefined;
143 })
144 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100145 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100146}
147
pineafan63fc5e22022-08-04 22:04:10 +0100148export async function NSFWCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100149 try {
TheCodedProfb5e9d552023-01-29 15:43:26 -0500150 return (await testNSFW(element)).nsfw;
pineafan813bdf42022-07-24 10:39:10 +0100151 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100152 return false;
pineafan813bdf42022-07-24 10:39:10 +0100153 }
154}
155
Skyler Grey11236ba2022-08-08 21:13:33 +0100156export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100157 if (element.height === null || element.width === null) return true;
158 if (element.height < 20 || element.width < 20) return false;
159 return true;
pineafan813bdf42022-07-24 10:39:10 +0100160}
161
pineafan63fc5e22022-08-04 22:04:10 +0100162export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100163 try {
pineafan63fc5e22022-08-04 22:04:10 +0100164 return (await testMalware(element)).safe;
pineafan813bdf42022-07-24 10:39:10 +0100165 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100166 return true;
pineafan813bdf42022-07-24 10:39:10 +0100167 }
168}
169
pineafan6de4da52023-03-07 20:43:44 +0000170export function TestString(string: string, soft: string[], strict: string[], enabled?: boolean): {word: string, type: string} | null {
171 if (!enabled) return null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100172 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100173 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100174 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100175 }
176 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 for (const word of soft) {
178 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100179 if (word2 === word) {
pineafan6de4da52023-03-07 20:43:44 +0000180 return { word: word, type: "soft" };
pineafan813bdf42022-07-24 10:39:10 +0100181 }
182 }
183 }
pineafan63fc5e22022-08-04 22:04:10 +0100184 return null;
pineafan813bdf42022-07-24 10:39:10 +0100185}
186
pineafan63fc5e22022-08-04 22:04:10 +0100187export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100188 const text = await Tesseract.recognize(url, {
189 lang: "eng",
190 oem: 1,
191 psm: 3
192 });
pineafan813bdf42022-07-24 10:39:10 +0100193 return text;
194}
pineafan6de4da52023-03-07 20:43:44 +0000195
196export async function doMemberChecks(member: Discord.GuildMember, guild: Discord.Guild): Promise<void> {
197 if (member.user.bot) return;
198 const guildData = await client.database.guilds.read(guild.id);
199 if (!guildData.logging.staff.channel) return;
200 const [ loose, strict ] = [guildData.filters.wordFilter.words.loose, guildData.filters.wordFilter.words.strict];
201 // Does the username contain filtered words
202 const usernameCheck = TestString(member.user.username, loose, strict, guildData.filters.wordFilter.enabled);
203 // Does the nickname contain filtered words
204 const nicknameCheck = TestString(member.nickname ?? "", loose, strict, guildData.filters.wordFilter.enabled);
205 // Does the profile picture contain filtered words
206 const avatarTextCheck = TestString(await TestImage(member.user.displayAvatarURL({ forceStatic: true })) ?? "", loose, strict, guildData.filters.wordFilter.enabled);
207 // Is the profile picture NSFW
208 const avatarCheck = guildData.filters.images.NSFW && await NSFWCheck(member.user.displayAvatarURL({ forceStatic: true }));
209 // Does the username contain an invite
210 const inviteCheck = guildData.filters.invite.enabled && member.user.username.match(/discord\.gg\/[a-zA-Z0-9]+/gi) !== null;
211 // Does the nickname contain an invite
212 const nicknameInviteCheck = guildData.filters.invite.enabled && member.nickname?.match(/discord\.gg\/[a-zA-Z0-9]+/gi) !== null;
213
214 if (usernameCheck !== null || nicknameCheck !== null || avatarCheck || inviteCheck || nicknameInviteCheck || avatarTextCheck !== null) {
215 const infractions = [];
216 if (usernameCheck !== null) {
217 infractions.push(`Username contains a ${usernameCheck.type}ly filtered word (${usernameCheck.word})`);
218 } if (nicknameCheck !== null) {
219 infractions.push(`Nickname contains a ${nicknameCheck.type}ly filtered word (${nicknameCheck.word})`);
220 } if (avatarCheck) {
221 infractions.push("Profile picture is NSFW");
222 } if (inviteCheck) {
223 infractions.push("Username contains an invite");
224 } if (nicknameInviteCheck) {
225 infractions.push("Nickname contains an invite");
226 } if (avatarTextCheck !== null) {
227 infractions.push(`Profile picture contains a ${avatarTextCheck.type}ly filtered word: ${avatarTextCheck.word}`);
228 }
229 if (infractions.length === 0) return;
230 // This is bad - Warn in the staff notifications channel
231 const filter = getEmojiByName("ICONS.FILTER");
232 const channel = guild.channels.cache.get(guildData.logging.staff.channel) as Discord.TextChannel;
233 const embed = new EmojiEmbed()
234 .setTitle("Member Flagged")
235 .setEmoji("ICONS.FLAGS.RED")
236 .setStatus("Danger")
237 .setDescription(`**Member:** ${member.user.username} (<@${member.user.id}>)\n\n` +
238 infractions.map((element) => `${filter} ${element}`).join("\n")
239 )
240 await channel.send({
241 embeds: [embed],
242 components: [new ActionRowBuilder<ButtonBuilder>().addComponents(...[
243 new ButtonBuilder()
244 .setCustomId(`mod:warn:${member.user.id}`)
245 .setLabel("Warn")
246 .setStyle(ButtonStyle.Primary),
247 new ButtonBuilder()
248 .setCustomId(`mod:mute:${member.user.id}`)
249 .setLabel("Mute")
250 .setStyle(ButtonStyle.Primary),
251 new ButtonBuilder()
252 .setCustomId(`mod:kick:${member.user.id}`)
253 .setLabel("Kick")
254 .setStyle(ButtonStyle.Danger),
255 new ButtonBuilder()
256 .setCustomId(`mod:ban:${member.user.id}`)
257 .setLabel("Ban")
258 .setStyle(ButtonStyle.Danger),
259 ].concat((usernameCheck !== null || nicknameCheck !== null || avatarTextCheck !== null) ? [
260 new ButtonBuilder()
261 .setCustomId(`mod:nickname:${member.user.id}`)
262 .setLabel("Change Name")
263 .setStyle(ButtonStyle.Primary)
264 ] : []))]
265 });
266 }
267}