blob: f64a520dda8435298aa61b88458e10d109fc4811 [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";
Skyler Grey32179982023-03-07 23:59:06 +00008import * as nsfwjs from "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[],
182 enabled?: boolean
183): { 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> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100201 const text = await Tesseract.recognize(url, {
202 lang: "eng",
203 oem: 1,
204 psm: 3
205 });
pineafan813bdf42022-07-24 10:39:10 +0100206 return text;
207}
pineafan6de4da52023-03-07 20:43:44 +0000208
Skyler Grey0d885222023-03-08 21:46:37 +0000209export async function doMemberChecks(member: Discord.GuildMember): Promise<void> {
pineafan6de4da52023-03-07 20:43:44 +0000210 if (member.user.bot) return;
Skyler Grey0d885222023-03-08 21:46:37 +0000211 const guild = member.guild;
pineafan6de4da52023-03-07 20:43:44 +0000212 const guildData = await client.database.guilds.read(guild.id);
213 if (!guildData.logging.staff.channel) return;
pineafan1e462ab2023-03-07 21:34:06 +0000214 const [loose, strict] = [guildData.filters.wordFilter.words.loose, guildData.filters.wordFilter.words.strict];
pineafan6de4da52023-03-07 20:43:44 +0000215 // Does the username contain filtered words
216 const usernameCheck = TestString(member.user.username, loose, strict, guildData.filters.wordFilter.enabled);
217 // Does the nickname contain filtered words
218 const nicknameCheck = TestString(member.nickname ?? "", loose, strict, guildData.filters.wordFilter.enabled);
219 // Does the profile picture contain filtered words
pineafan1e462ab2023-03-07 21:34:06 +0000220 const avatarTextCheck = TestString(
Skyler Greye9c3ef62023-03-09 14:09:00 +0000221 (await TestImage(member.displayAvatarURL({ forceStatic: true }))) ?? "",
pineafan1e462ab2023-03-07 21:34:06 +0000222 loose,
223 strict,
224 guildData.filters.wordFilter.enabled
225 );
pineafan6de4da52023-03-07 20:43:44 +0000226 // Is the profile picture NSFW
Skyler Grey0d885222023-03-08 21:46:37 +0000227 const avatar = member.displayAvatarURL({ extension: "png", size: 1024, forceStatic: true });
Skyler Grey74169642023-03-09 11:59:09 +0000228 const avatarCheck = guildData.filters.images.NSFW && (await NSFWCheck(avatar));
pineafan6de4da52023-03-07 20:43:44 +0000229 // Does the username contain an invite
Skyler Grey14432712023-03-07 23:40:50 +0000230 const inviteCheck = guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.user.username);
pineafan6de4da52023-03-07 20:43:44 +0000231 // Does the nickname contain an invite
pineafan1e462ab2023-03-07 21:34:06 +0000232 const nicknameInviteCheck =
Skyler Grey14432712023-03-07 23:40:50 +0000233 guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.nickname ?? "");
pineafan1e462ab2023-03-07 21:34:06 +0000234 if (
235 usernameCheck !== null ||
236 nicknameCheck !== null ||
237 avatarCheck ||
238 inviteCheck ||
239 nicknameInviteCheck ||
240 avatarTextCheck !== null
241 ) {
pineafan6de4da52023-03-07 20:43:44 +0000242 const infractions = [];
243 if (usernameCheck !== null) {
244 infractions.push(`Username contains a ${usernameCheck.type}ly filtered word (${usernameCheck.word})`);
pineafan1e462ab2023-03-07 21:34:06 +0000245 }
246 if (nicknameCheck !== null) {
pineafan6de4da52023-03-07 20:43:44 +0000247 infractions.push(`Nickname contains a ${nicknameCheck.type}ly filtered word (${nicknameCheck.word})`);
pineafan1e462ab2023-03-07 21:34:06 +0000248 }
249 if (avatarCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000250 infractions.push("Profile picture is NSFW");
pineafan1e462ab2023-03-07 21:34:06 +0000251 }
252 if (inviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000253 infractions.push("Username contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000254 }
255 if (nicknameInviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000256 infractions.push("Nickname contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000257 }
258 if (avatarTextCheck !== null) {
259 infractions.push(
Skyler Greye9c3ef62023-03-09 14:09:00 +0000260 `Profile picture contains a ${avatarTextCheck.type}ly filtered word (${avatarTextCheck.word})`
pineafan1e462ab2023-03-07 21:34:06 +0000261 );
pineafan6de4da52023-03-07 20:43:44 +0000262 }
263 if (infractions.length === 0) return;
264 // This is bad - Warn in the staff notifications channel
265 const filter = getEmojiByName("ICONS.FILTER");
266 const channel = guild.channels.cache.get(guildData.logging.staff.channel) as Discord.TextChannel;
267 const embed = new EmojiEmbed()
268 .setTitle("Member Flagged")
269 .setEmoji("ICONS.FLAGS.RED")
270 .setStatus("Danger")
pineafan1e462ab2023-03-07 21:34:06 +0000271 .setDescription(
272 `**Member:** ${member.user.username} (<@${member.user.id}>)\n\n` +
273 infractions.map((element) => `${filter} ${element}`).join("\n")
274 );
TheCodedProf764e6c22023-03-11 16:07:09 -0500275 const buttons = [
276 new ButtonBuilder()
277 .setCustomId(`mod:warn:${member.user.id}`)
278 .setLabel("Warn")
279 .setStyle(ButtonStyle.Primary),
280 new ButtonBuilder()
281 .setCustomId(`mod:mute:${member.user.id}`)
282 .setLabel("Mute")
283 .setStyle(ButtonStyle.Primary),
TheCodedProf1cfa1ae2023-03-11 16:07:37 -0500284 new ButtonBuilder().setCustomId(`mod:kick:${member.user.id}`).setLabel("Kick").setStyle(ButtonStyle.Danger),
285 new ButtonBuilder().setCustomId(`mod:ban:${member.user.id}`).setLabel("Ban").setStyle(ButtonStyle.Danger)
286 ];
287 if (usernameCheck !== null || nicknameCheck !== null)
288 buttons.concat([
289 new ButtonBuilder()
290 .setCustomId(`mod:nickname:${member.user.id}`)
291 .setLabel("Change Name")
292 .setStyle(ButtonStyle.Primary)
293 ]);
294 if (avatarCheck || avatarTextCheck !== null)
295 buttons.concat([
296 new ButtonBuilder().setURL(member.displayAvatarURL()).setLabel("View Avatar").setStyle(ButtonStyle.Link)
297 ]);
298 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}