blob: 192be257bf226952f33ff0eaef75d5f3ce1b2db4 [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";
pineafan1e462ab2023-03-07 21:34:06 +00008// import * as nsfwjs from "nsfwjs";
9// import * as clamscan from "clamscan";
10// import * 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
pineafan1e462ab2023-03-07 21:34:06 +000024// const model = await nsfwjs.load();
TheCodedProfd8ef1f32023-03-06 19:15:18 -050025
pineafan02ba0232022-07-24 22:16:15 +010026export async function testNSFW(link: string): Promise<NSFWSchema> {
pineafan1e462ab2023-03-07 21:34:06 +000027 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
pineafan1e462ab2023-03-07 21:34:06 +0000170export function TestString(
171 string: string,
172 soft: string[],
173 strict: string[],
174 enabled?: boolean
175): { word: string; type: string } | null {
pineafan6de4da52023-03-07 20:43:44 +0000176 if (!enabled) return null;
Skyler Grey75ea9172022-08-06 10:22:23 +0100177 for (const word of strict) {
pineafan813bdf42022-07-24 10:39:10 +0100178 if (string.toLowerCase().includes(word)) {
Skyler Grey75ea9172022-08-06 10:22:23 +0100179 return { word: word, type: "strict" };
pineafan813bdf42022-07-24 10:39:10 +0100180 }
181 }
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 for (const word of soft) {
183 for (const word2 of string.match(/[a-z]+/gi) ?? []) {
pineafane23c4ec2022-07-27 21:56:27 +0100184 if (word2 === word) {
pineafan6de4da52023-03-07 20:43:44 +0000185 return { word: word, type: "soft" };
pineafan813bdf42022-07-24 10:39:10 +0100186 }
187 }
188 }
pineafan63fc5e22022-08-04 22:04:10 +0100189 return null;
pineafan813bdf42022-07-24 10:39:10 +0100190}
191
pineafan63fc5e22022-08-04 22:04:10 +0100192export async function TestImage(url: string): Promise<string | null> {
Skyler Grey75ea9172022-08-06 10:22:23 +0100193 const text = await Tesseract.recognize(url, {
194 lang: "eng",
195 oem: 1,
196 psm: 3
197 });
pineafan813bdf42022-07-24 10:39:10 +0100198 return text;
199}
pineafan6de4da52023-03-07 20:43:44 +0000200
201export async function doMemberChecks(member: Discord.GuildMember, guild: Discord.Guild): Promise<void> {
202 if (member.user.bot) return;
203 const guildData = await client.database.guilds.read(guild.id);
204 if (!guildData.logging.staff.channel) return;
pineafan1e462ab2023-03-07 21:34:06 +0000205 const [loose, strict] = [guildData.filters.wordFilter.words.loose, guildData.filters.wordFilter.words.strict];
pineafan6de4da52023-03-07 20:43:44 +0000206 // Does the username contain filtered words
207 const usernameCheck = TestString(member.user.username, loose, strict, guildData.filters.wordFilter.enabled);
208 // Does the nickname contain filtered words
209 const nicknameCheck = TestString(member.nickname ?? "", loose, strict, guildData.filters.wordFilter.enabled);
210 // Does the profile picture contain filtered words
pineafan1e462ab2023-03-07 21:34:06 +0000211 const avatarTextCheck = TestString(
212 (await TestImage(member.user.displayAvatarURL({ forceStatic: true }))) ?? "",
213 loose,
214 strict,
215 guildData.filters.wordFilter.enabled
216 );
pineafan6de4da52023-03-07 20:43:44 +0000217 // Is the profile picture NSFW
pineafan1e462ab2023-03-07 21:34:06 +0000218 const avatarCheck =
219 guildData.filters.images.NSFW && (await NSFWCheck(member.user.displayAvatarURL({ forceStatic: true })));
pineafan6de4da52023-03-07 20:43:44 +0000220 // Does the username contain an invite
pineafan1e462ab2023-03-07 21:34:06 +0000221 const inviteCheck =
222 guildData.filters.invite.enabled && member.user.username.match(/discord\.gg\/[a-zA-Z0-9]+/gi) !== null;
pineafan6de4da52023-03-07 20:43:44 +0000223 // Does the nickname contain an invite
pineafan1e462ab2023-03-07 21:34:06 +0000224 const nicknameInviteCheck =
225 guildData.filters.invite.enabled && member.nickname?.match(/discord\.gg\/[a-zA-Z0-9]+/gi) !== null;
pineafan6de4da52023-03-07 20:43:44 +0000226
pineafan1e462ab2023-03-07 21:34:06 +0000227 if (
228 usernameCheck !== null ||
229 nicknameCheck !== null ||
230 avatarCheck ||
231 inviteCheck ||
232 nicknameInviteCheck ||
233 avatarTextCheck !== null
234 ) {
pineafan6de4da52023-03-07 20:43:44 +0000235 const infractions = [];
236 if (usernameCheck !== null) {
237 infractions.push(`Username contains a ${usernameCheck.type}ly filtered word (${usernameCheck.word})`);
pineafan1e462ab2023-03-07 21:34:06 +0000238 }
239 if (nicknameCheck !== null) {
pineafan6de4da52023-03-07 20:43:44 +0000240 infractions.push(`Nickname contains a ${nicknameCheck.type}ly filtered word (${nicknameCheck.word})`);
pineafan1e462ab2023-03-07 21:34:06 +0000241 }
242 if (avatarCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000243 infractions.push("Profile picture is NSFW");
pineafan1e462ab2023-03-07 21:34:06 +0000244 }
245 if (inviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000246 infractions.push("Username contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000247 }
248 if (nicknameInviteCheck) {
pineafan6de4da52023-03-07 20:43:44 +0000249 infractions.push("Nickname contains an invite");
pineafan1e462ab2023-03-07 21:34:06 +0000250 }
251 if (avatarTextCheck !== null) {
252 infractions.push(
253 `Profile picture contains a ${avatarTextCheck.type}ly filtered word: ${avatarTextCheck.word}`
254 );
pineafan6de4da52023-03-07 20:43:44 +0000255 }
256 if (infractions.length === 0) return;
257 // This is bad - Warn in the staff notifications channel
258 const filter = getEmojiByName("ICONS.FILTER");
259 const channel = guild.channels.cache.get(guildData.logging.staff.channel) as Discord.TextChannel;
260 const embed = new EmojiEmbed()
261 .setTitle("Member Flagged")
262 .setEmoji("ICONS.FLAGS.RED")
263 .setStatus("Danger")
pineafan1e462ab2023-03-07 21:34:06 +0000264 .setDescription(
265 `**Member:** ${member.user.username} (<@${member.user.id}>)\n\n` +
266 infractions.map((element) => `${filter} ${element}`).join("\n")
267 );
pineafan6de4da52023-03-07 20:43:44 +0000268 await channel.send({
269 embeds: [embed],
pineafan1e462ab2023-03-07 21:34:06 +0000270 components: [
271 new ActionRowBuilder<ButtonBuilder>().addComponents(
272 ...[
273 new ButtonBuilder()
274 .setCustomId(`mod:warn:${member.user.id}`)
275 .setLabel("Warn")
276 .setStyle(ButtonStyle.Primary),
277 new ButtonBuilder()
278 .setCustomId(`mod:mute:${member.user.id}`)
279 .setLabel("Mute")
280 .setStyle(ButtonStyle.Primary),
281 new ButtonBuilder()
282 .setCustomId(`mod:kick:${member.user.id}`)
283 .setLabel("Kick")
284 .setStyle(ButtonStyle.Danger),
285 new ButtonBuilder()
286 .setCustomId(`mod:ban:${member.user.id}`)
287 .setLabel("Ban")
288 .setStyle(ButtonStyle.Danger)
289 ].concat(
290 usernameCheck !== null || nicknameCheck !== null || avatarTextCheck !== null
291 ? [
292 new ButtonBuilder()
293 .setCustomId(`mod:nickname:${member.user.id}`)
294 .setLabel("Change Name")
295 .setStyle(ButtonStyle.Primary)
296 ]
297 : []
298 )
299 )
300 ]
pineafan6de4da52023-03-07 20:43:44 +0000301 });
302 }
pineafan1e462ab2023-03-07 21:34:06 +0000303}