blob: eae3cb3a5b694516e17a93cb9d7211e02088e2f3 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import fetch from "node-fetch";
Skyler Grey32179982023-03-07 23:59:06 +00002import { writeFileSync } 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";
TheCodedProf30219372023-06-11 14:15:30 -04008import * as nsfwjs from "@clicks/nsfwjs";
Skyler Grey0a4846c2023-03-08 00:32:01 +00009import ClamScan from "clamscan";
Skyler Grey62da9bf2023-03-08 00:11:00 +000010import * 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";
Skyler Greyd1157312023-03-08 10:07:38 +000014import config from "../config/main.js";
Skyler Greyea0937b2023-03-09 00:36:38 +000015import gm from "gm";
pineafan813bdf42022-07-24 10:39:10 +010016
Skyler Grey75ea9172022-08-06 10:22:23 +010017interface NSFWSchema {
18 nsfw: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050019 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010020}
21interface MalwareSchema {
Skyler Grey0d885222023-03-08 21:46:37 +000022 malware: boolean;
TheCodedProf5b53a8c2023-02-03 15:40:26 -050023 errored?: boolean;
Skyler Grey75ea9172022-08-06 10:22:23 +010024}
pineafan813bdf42022-07-24 10:39:10 +010025
Skyler Grey7a966df2023-03-09 12:53:57 +000026const nsfw_model = await nsfwjs.load("file://dist/reflex/nsfwjs/example/nsfw_demo/public/model/", { size: 299 });
Skyler Greyd1157312023-03-08 10:07:38 +000027const clamscanner = await new ClamScan().init({
28 clamdscan: {
Skyler Grey21f52292023-03-10 17:58:30 +000029 socket: "socket" in config.clamav ? (config.clamav.socket as string) : false,
30 host: "host" in config.clamav ? (config.clamav.host as string) : false,
31 port: "port" in config.clamav ? (config.clamav.port as number) : false
Skyler Greyd1157312023-03-08 10:07:38 +000032 }
33});
TheCodedProfd8ef1f32023-03-06 19:15:18 -050034
Skyler Grey74169642023-03-09 11:59:09 +000035export async function testNSFW(url: string): Promise<NSFWSchema> {
36 const [fileStream, hash] = await streamAttachment(url);
Skyler Greyda16adf2023-03-05 10:22:12 +000037 const alreadyHaveCheck = await client.database.scanCache.read(hash);
Skyler Greyea0937b2023-03-09 00:36:38 +000038 if (alreadyHaveCheck && "nsfw" in alreadyHaveCheck!) {
39 return { nsfw: alreadyHaveCheck.nsfw };
40 }
TheCodedProfd8ef1f32023-03-06 19:15:18 -050041
Skyler Greyea0937b2023-03-09 00:36:38 +000042 const converted = (await new Promise((resolve, reject) =>
43 gm(fileStream)
44 .command("convert")
45 .toBuffer("PNG", (err, buf) => {
46 if (err) return reject(err);
47 resolve(buf);
48 })
49 )) as Buffer;
TheCodedProfd8ef1f32023-03-06 19:15:18 -050050
Skyler Grey74169642023-03-09 11:59:09 +000051 const img = tf.node.decodeImage(converted, 3, undefined, false) as tf.Tensor3D;
Skyler Grey0d885222023-03-08 21:46:37 +000052
53 const predictions = (await nsfw_model.classify(img, 1))[0]!;
Skyler Grey74169642023-03-09 11:59:09 +000054 img.dispose();
TheCodedProfd8ef1f32023-03-06 19:15:18 -050055
Skyler Greyd1157312023-03-08 10:07:38 +000056 const nsfw = predictions.className === "Hentai" || predictions.className === "Porn";
57 await client.database.scanCache.write(hash, "nsfw", nsfw);
58
59 return { nsfw };
pineafan813bdf42022-07-24 10:39:10 +010060}
61
pineafan02ba0232022-07-24 22:16:15 +010062export async function testMalware(link: string): Promise<MalwareSchema> {
Skyler Grey0a4846c2023-03-08 00:32:01 +000063 const [fileName, hash] = await saveAttachment(link);
Skyler Greyda16adf2023-03-05 10:22:12 +000064 const alreadyHaveCheck = await client.database.scanCache.read(hash);
Skyler Grey0d885222023-03-08 21:46:37 +000065 if (alreadyHaveCheck?.malware !== undefined) return { malware: alreadyHaveCheck.malware };
Skyler Greyd1157312023-03-08 10:07:38 +000066 let malware;
Skyler Grey0a4846c2023-03-08 00:32:01 +000067 try {
Skyler Greyd1157312023-03-08 10:07:38 +000068 malware = (await clamscanner.scanFile(fileName)).isInfected;
Skyler Grey0a4846c2023-03-08 00:32:01 +000069 } catch (e) {
PineappleFan7056a3d2023-03-30 20:52:45 +010070 return { malware: false };
Skyler Grey0a4846c2023-03-08 00:32:01 +000071 }
Skyler Greyf4f21c42023-03-08 14:36:29 +000072 await client.database.scanCache.write(hash, "malware", malware);
Skyler Grey0d885222023-03-08 21:46:37 +000073 return { malware };
pineafan3a02ea32022-08-11 21:35:04 +010074}
75
76export async function testLink(link: string): Promise<{ safe: boolean; tags: string[] }> {
Skyler Greyda16adf2023-03-05 10:22:12 +000077 const alreadyHaveCheck = await client.database.scanCache.read(link);
Skyler Grey0d885222023-03-08 21:46:37 +000078 if (alreadyHaveCheck?.bad_link !== undefined)
79 return { safe: alreadyHaveCheck.bad_link, tags: alreadyHaveCheck.tags ?? [] };
80 return { safe: true, tags: [] };
81 // const scanned: { safe?: boolean; tags?: string[] } = {}
82 // await client.database.scanCache.write(link, "bad_link", scanned.safe ?? true, scanned.tags ?? []);
83 // return {
84 // safe: scanned.safe ?? true,
85 // tags: scanned.tags ?? []
86 // };
pineafan813bdf42022-07-24 10:39:10 +010087}
88
Skyler Grey0d885222023-03-08 21:46:37 +000089export async function streamAttachment(link: string): Promise<[Buffer, string]> {
Skyler Greyda16adf2023-03-05 10:22:12 +000090 const image = await (await fetch(link)).arrayBuffer();
Skyler Grey32179982023-03-07 23:59:06 +000091 const enc = new TextDecoder("utf-8");
Skyler Grey0d885222023-03-08 21:46:37 +000092 const buf = Buffer.from(image);
93 return [buf, createHash("sha512").update(enc.decode(image), "base64").digest("base64")];
Skyler Grey32179982023-03-07 23:59:06 +000094}
95
96export async function saveAttachment(link: string): Promise<[string, string]> {
97 const image = await (await fetch(link)).arrayBuffer();
Skyler Greyf4f21c42023-03-08 14:36:29 +000098 const fileName = await generateFileName(link.split("/").pop()!.split(".").pop()!);
TheCodedProf5b53a8c2023-02-03 15:40:26 -050099 const enc = new TextDecoder("utf-8");
Skyler Grey0d885222023-03-08 21:46:37 +0000100 writeFileSync(fileName, new DataView(image));
Skyler Grey32179982023-03-07 23:59:06 +0000101 return [fileName, createHash("sha512").update(enc.decode(image), "base64").digest("base64")];
pineafan813bdf42022-07-24 10:39:10 +0100102}
103
pineafan813bdf42022-07-24 10:39:10 +0100104const linkTypes = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100105 PHISHING: "Links designed to trick users into clicking on them.",
106 DATING: "Dating sites.",
107 TRACKERS: "Websites that store or track personal information.",
108 ADVERTISEMENTS: "Websites only for ads.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100109 FACEBOOK: "Facebook pages. (Facebook has a number of dangerous trackers. Read more on /privacy)",
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 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 +0100111 "FACEBOOK TRACKERS": "Websites that include trackers from Facebook.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100112 "IP GRABBERS": "Websites that store your IP address, which shows your approximate location.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100113 PORN: "Websites that include pornography.",
114 GAMBLING: "Gambling sites, often scams.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100115 MALWARE: "Websites which download files designed to break or slow down your device.",
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 PIRACY: "Sites which include illegally downloaded material.",
Skyler Grey11236ba2022-08-08 21:13:33 +0100117 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 +0100118 REDIRECTS: "Sites like bit.ly which could redirect to a malicious site.",
119 SCAMS: "Sites which are designed to trick you into doing something.",
120 TORRENT: "Websites that download torrent files.",
121 HATE: "Websites that spread hate towards groups or individuals.",
122 JUNK: "Websites that are designed to make you waste time."
pineafan63fc5e22022-08-04 22:04:10 +0100123};
pineafan813bdf42022-07-24 10:39:10 +0100124export { linkTypes };
125
pineafan63fc5e22022-08-04 22:04:10 +0100126export async function LinkCheck(message: Discord.Message): Promise<string[]> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 const links =
128 message.content.match(
129 /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi
130 ) ?? [];
131 const detections: { tags: string[]; safe: boolean }[] = [];
132 const promises: Promise<void>[] = links.map(async (element) => {
pineafan63fc5e22022-08-04 22:04:10 +0100133 let returned;
pineafan813bdf42022-07-24 10:39:10 +0100134 try {
Skyler Grey11236ba2022-08-08 21:13:33 +0100135 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 +0100136 returned = await testLink(element);
137 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +0100138 detections.push({ tags: [], safe: true });
pineafan63fc5e22022-08-04 22:04:10 +0100139 return;
140 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 detections.push({ tags: returned.tags, safe: returned.safe });
pineafan813bdf42022-07-24 10:39:10 +0100142 });
143 await Promise.all(promises);
Skyler Grey75ea9172022-08-06 10:22:23 +0100144 const detectionsTypes = detections
145 .map((element) => {
Skyler Grey11236ba2022-08-08 21:13:33 +0100146 const type = Object.keys(linkTypes).find((type) => element.tags.includes(type));
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 if (type) return type;
148 // if (!element.safe) return "UNSAFE"
149 return undefined;
150 })
151 .filter((element) => element !== undefined);
pineafan63fc5e22022-08-04 22:04:10 +0100152 return detectionsTypes as string[];
pineafan813bdf42022-07-24 10:39:10 +0100153}
154
Skyler Grey74169642023-03-09 11:59:09 +0000155export async function NSFWCheck(url: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100156 try {
Skyler Grey74169642023-03-09 11:59:09 +0000157 return (await testNSFW(url)).nsfw;
Skyler Grey0d885222023-03-08 21:46:37 +0000158 } catch (e) {
Skyler Greyea0937b2023-03-09 00:36:38 +0000159 console.log(e);
pineafan63fc5e22022-08-04 22:04:10 +0100160 return false;
pineafan813bdf42022-07-24 10:39:10 +0100161 }
162}
163
Skyler Grey11236ba2022-08-08 21:13:33 +0100164export async function SizeCheck(element: { height: number | null; width: number | null }): Promise<boolean> {
pineafan63fc5e22022-08-04 22:04:10 +0100165 if (element.height === null || element.width === null) return true;
166 if (element.height < 20 || element.width < 20) return false;
167 return true;
pineafan813bdf42022-07-24 10:39:10 +0100168}
169
pineafan63fc5e22022-08-04 22:04:10 +0100170export async function MalwareCheck(element: string): Promise<boolean> {
pineafan813bdf42022-07-24 10:39:10 +0100171 try {
Skyler Grey0d885222023-03-08 21:46:37 +0000172 return (await testMalware(element)).malware;
pineafan813bdf42022-07-24 10:39:10 +0100173 } catch {
pineafan63fc5e22022-08-04 22:04:10 +0100174 return true;
pineafan813bdf42022-07-24 10:39:10 +0100175 }
176}
177
pineafan1e462ab2023-03-07 21:34:06 +0000178export function TestString(
179 string: string,
180 soft: string[],
181 strict: string[],
TheCodedProf258f5372023-04-22 20:22:10 -0400182 enabled: boolean = true
pineafan1e462ab2023-03-07 21:34:06 +0000183): { word: string; type: string } | null {
pineafan6de4da52023-03-07 20:43:44 +0000184 if (!enabled) return null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100185 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100186 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100187 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100188 }
189 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100190 for (const word of soft) {
191 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100192 if (word2 === word) {
pineafan6de4da52023-03-07 20:43:44 +0000193 return { word: word, type: "soft" };
pineafan813bdf42022-07-24 10:39:10 +0100194 }
195 }
196 }
pineafan63fc5e22022-08-04 22:04:10 +0100197 return null;
pineafan813bdf42022-07-24 10:39:10 +0100198}
199
pineafan63fc5e22022-08-04 22:04:10 +0100200export async function TestImage(url: string): Promise<string | null> {
TheCodedProfca387af2023-04-07 14:42:41 -0400201 try {
202 const text = await Tesseract.recognize(url, {
203 lang: "eng",
204 oem: 1,
205 psm: 3
206 });
207 return text;
208 } catch {
209 return null;
210 }
pineafan813bdf42022-07-24 10:39:10 +0100211}
pineafan6de4da52023-03-07 20:43:44 +0000212
Skyler Grey0d885222023-03-08 21:46:37 +0000213export async function doMemberChecks(member: Discord.GuildMember): Promise<void> {
pineafan6de4da52023-03-07 20:43:44 +0000214 if (member.user.bot) return;
Skyler Grey0d885222023-03-08 21:46:37 +0000215 const guild = member.guild;
pineafan6de4da52023-03-07 20:43:44 +0000216 const guildData = await client.database.guilds.read(guild.id);
217 if (!guildData.logging.staff.channel) return;
pineafan1e462ab2023-03-07 21:34:06 +0000218 const [loose, strict] = [guildData.filters.wordFilter.words.loose, guildData.filters.wordFilter.words.strict];
pineafan6de4da52023-03-07 20:43:44 +0000219 // Does the username contain filtered words
pineafan6de4da52023-03-07 20:43:44 +0000220 // Does the nickname contain filtered words
Samuel Shuerteb13a1b2023-04-10 14:30:47 -0400221 let nameCheck;
TheCodedProf122b0622023-04-21 22:45:53 -0400222 if (member.nickname) {
TheCodedProf3a648702023-04-22 20:23:15 -0400223 nameCheck = TestString(member.nickname, loose, strict, guildData.filters.wordFilter.enabled);
Samuel Shuerteb13a1b2023-04-10 14:30:47 -0400224 } else {
225 nameCheck = TestString(member.user.username, loose, strict, guildData.filters.wordFilter.enabled);
226 }
pineafan6de4da52023-03-07 20:43:44 +0000227 // Does the profile picture contain filtered words
pineafan1e462ab2023-03-07 21:34:06 +0000228 const avatarTextCheck = TestString(
Skyler Greye9c3ef62023-03-09 14:09:00 +0000229 (await TestImage(member.displayAvatarURL({ forceStatic: true }))) ?? "",
pineafan1e462ab2023-03-07 21:34:06 +0000230 loose,
231 strict,
232 guildData.filters.wordFilter.enabled
233 );
pineafan6de4da52023-03-07 20:43:44 +0000234 // Is the profile picture NSFW
Skyler Grey0d885222023-03-08 21:46:37 +0000235 const avatar = member.displayAvatarURL({ extension: "png", size: 1024, forceStatic: true });
Skyler Grey74169642023-03-09 11:59:09 +0000236 const avatarCheck = guildData.filters.images.NSFW && (await NSFWCheck(avatar));
pineafan6de4da52023-03-07 20:43:44 +0000237 // Does the username contain an invite
Skyler Grey14432712023-03-07 23:40:50 +0000238 const inviteCheck = guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.user.username);
pineafan6de4da52023-03-07 20:43:44 +0000239 // Does the nickname contain an invite
pineafan1e462ab2023-03-07 21:34:06 +0000240 const nicknameInviteCheck =
Skyler Grey14432712023-03-07 23:40:50 +0000241 guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.nickname ?? "");
TheCodedProf122b0622023-04-21 22:45:53 -0400242 if (nameCheck !== null || avatarCheck || inviteCheck || nicknameInviteCheck || avatarTextCheck !== null) {
pineafan6de4da52023-03-07 20:43:44 +0000243 const infractions = [];
Samuel Shuertf5457142023-04-10 14:34:15 -0400244 if (nameCheck !== null) {
245 infractions.push(`Name contains a ${nameCheck.type}ly filtered word (${nameCheck.word})`);
pineafan1e462ab2023-03-07 21:34:06 +0000246 }
247 if (avatarCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000248 infractions.push("Profile picture is NSFW");
pineafan1e462ab2023-03-07 21:34:06 +0000249 }
250 if (inviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000251 infractions.push("Username contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000252 }
253 if (nicknameInviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000254 infractions.push("Nickname contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000255 }
256 if (avatarTextCheck !== null) {
257 infractions.push(
Skyler Greye9c3ef62023-03-09 14:09:00 +0000258 `Profile picture contains a ${avatarTextCheck.type}ly filtered word (${avatarTextCheck.word})`
pineafan1e462ab2023-03-07 21:34:06 +0000259 );
pineafan6de4da52023-03-07 20:43:44 +0000260 }
261 if (infractions.length === 0) return;
262 // This is bad - Warn in the staff notifications channel
263 const filter = getEmojiByName("ICONS.FILTER");
264 const channel = guild.channels.cache.get(guildData.logging.staff.channel) as Discord.TextChannel;
265 const embed = new EmojiEmbed()
266 .setTitle("Member Flagged")
267 .setEmoji("ICONS.FLAGS.RED")
268 .setStatus("Danger")
pineafan1e462ab2023-03-07 21:34:06 +0000269 .setDescription(
270 `**Member:** ${member.user.username} (<@${member.user.id}>)\n\n` +
271 infractions.map((element) => `${filter} ${element}`).join("\n")
272 );
TheCodedProf764e6c22023-03-11 16:07:09 -0500273 const buttons = [
274 new ButtonBuilder()
275 .setCustomId(`mod:warn:${member.user.id}`)
276 .setLabel("Warn")
277 .setStyle(ButtonStyle.Primary),
278 new ButtonBuilder()
279 .setCustomId(`mod:mute:${member.user.id}`)
280 .setLabel("Mute")
281 .setStyle(ButtonStyle.Primary),
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500282 new ButtonBuilder().setCustomId(`mod:kick:${member.user.id}`).setLabel("Kick").setStyle(ButtonStyle.Danger),
283 new ButtonBuilder().setCustomId(`mod:ban:${member.user.id}`).setLabel("Ban").setStyle(ButtonStyle.Danger)
284 ];
pineafan4de16122023-09-06 21:00:19 +0100285 if (nameCheck !== null) {
286 buttons.push(
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500287 new ButtonBuilder()
288 .setCustomId(`mod:nickname:${member.user.id}`)
289 .setLabel("Change Name")
290 .setStyle(ButtonStyle.Primary)
pineafan4de16122023-09-06 21:00:19 +0100291 );
292 }
293 if (avatarCheck || avatarTextCheck !== null) {
294 buttons.push(
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500295 new ButtonBuilder().setURL(member.displayAvatarURL()).setLabel("View Avatar").setStyle(ButtonStyle.Link)
pineafan4de16122023-09-06 21:00:19 +0100296 );
297 }
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500298 const components: ActionRowBuilder<ButtonBuilder>[] = [];
TheCodedProf764e6c22023-03-11 16:07:09 -0500299
300 for (let i = 0; i < buttons.length; i += 5) {
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500301 components.push(
302 new ActionRowBuilder<ButtonBuilder>().addComponents(
303 buttons.slice(i, Math.min(buttons.length - 1, i + 5))
304 )
305 );
TheCodedProf764e6c22023-03-11 16:07:09 -0500306 }
307
pineafan6de4da52023-03-07 20:43:44 +0000308 await channel.send({
309 embeds: [embed],
TheCodedProf764e6c22023-03-11 16:07:09 -0500310 components: components
pineafan6de4da52023-03-07 20:43:44 +0000311 });
312 }
pineafan1e462ab2023-03-07 21:34:06 +0000313}