From c22a32b57d7e29f708dd9a5e125b3a4c7b3bea75 Mon Sep 17 00:00:00 2001 From: FloatingMilkshake Date: Sat, 11 Jan 2025 17:05:11 -0500 Subject: [PATCH 1/2] Add support for Greek lookalikes to list checks --- Checks/ListChecks.cs | 42 ++++++++++++++++++++++++++++++++++++++++-- Events/MessageEvent.cs | 4 +++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Checks/ListChecks.cs b/Checks/ListChecks.cs index 6e75a749..6409509d 100644 --- a/Checks/ListChecks.cs +++ b/Checks/ListChecks.cs @@ -6,7 +6,7 @@ public class ListChecks { // Map of Cyrillic to Latin characters, to catch attempted bypasses using Cyrillic lookalikes // is - public static Dictionary alphabetMap = new() + public static Dictionary cyrillicAlphabetMap = new() { { "А", "A" }, { "В", "B" }, @@ -49,11 +49,49 @@ public class ListChecks { "у", "y" }, { "У", "y" } }; + + // Map of Greek to Latin characters, to catch attempted bypasses using Greek lookalikes + // is + public static Dictionary greekAlphabetMap = new() + { + { "Α", "A" }, + { "Β", "B" }, + { "Ε", "E" }, + { "Η", "H" }, + { "Ι", "I" }, + { "Κ", "K" }, + { "Μ", "M" }, + { "Ν", "N" }, + { "Ο", "O" }, + { "Ρ", "P" }, + { "Τ", "T" }, + { "Χ", "X" }, + { "Υ", "Y" }, + { "Ζ", "Z" }, + { "α", "a" }, + { "β", "b" }, + { "ε", "e" }, + { "η", "h" }, + { "ι", "i" }, + { "κ", "k" }, + { "μ", "m" }, + { "ν", "n" }, + { "ο", "o" }, + { "ρ", "p" }, + { "τ", "t" }, + { "χ", "x" }, + { "υ", "y" }, + { "ζ", "z" }, + }; public static (bool success, string? flaggedWord) CheckForNaughtyWords(string input, WordListJson naughtyWordList) { // Replace any Cyrillic letters found in message with Latin characters, if in the dictionary - foreach (var letter in alphabetMap) + foreach (var letter in cyrillicAlphabetMap) + input = input.Replace(letter.Key, letter.Value); + + // and Greek letters + foreach (var letter in greekAlphabetMap) input = input.Replace(letter.Key, letter.Value); string[] naughtyWords = naughtyWordList.Words; diff --git a/Events/MessageEvent.cs b/Events/MessageEvent.cs index 79ef460a..1c79a88a 100644 --- a/Events/MessageEvent.cs +++ b/Events/MessageEvent.cs @@ -646,7 +646,9 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe // attempted to ping @everyone/@here var msgContent = message.Content; - foreach (var letter in Checks.ListChecks.alphabetMap) + foreach (var letter in Checks.ListChecks.cyrillicAlphabetMap) + msgContent = msgContent.Replace(letter.Key, letter.Value); + foreach (var letter in Checks.ListChecks.greekAlphabetMap) msgContent = msgContent.Replace(letter.Key, letter.Value); if (Program.cfgjson.EveryoneFilter && !member.Roles.Any(role => Program.cfgjson.EveryoneExcludedRoles.Contains(role.Id)) && !Program.cfgjson.EveryoneExcludedChannels.Contains(channel.Id) && (msgContent.Contains("@everyone") || msgContent.Contains("@here"))) { From d7fbd0c592c426b688f3c58b0cb76ab2a7ac5dd3 Mon Sep 17 00:00:00 2001 From: Erisa A Date: Sun, 12 Jan 2025 20:00:57 +0000 Subject: [PATCH 2/2] Unify lookalike mappings --- Checks/ListChecks.cs | 26 +++++++++----------------- Events/MessageEvent.cs | 4 +--- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Checks/ListChecks.cs b/Checks/ListChecks.cs index 6409509d..dd07c9b1 100644 --- a/Checks/ListChecks.cs +++ b/Checks/ListChecks.cs @@ -4,10 +4,10 @@ namespace Cliptok.Checks { public class ListChecks { - // Map of Cyrillic to Latin characters, to catch attempted bypasses using Cyrillic lookalikes - // is - public static Dictionary cyrillicAlphabetMap = new() + // Map of lookalike to Latin characters, to catch attempted bypasses using different language lookalikes + public static Dictionary lookalikeAlphabetMap = new() { + // is { "А", "A" }, { "В", "B" }, { "С", "C" }, @@ -47,13 +47,9 @@ public class ListChecks { "ѡ", "w" }, { "х", "x" }, { "у", "y" }, - { "У", "y" } - }; - - // Map of Greek to Latin characters, to catch attempted bypasses using Greek lookalikes - // is - public static Dictionary greekAlphabetMap = new() - { + { "У", "y" }, + + // is { "Α", "A" }, { "Β", "B" }, { "Ε", "E" }, @@ -83,17 +79,13 @@ public class ListChecks { "υ", "y" }, { "ζ", "z" }, }; - + public static (bool success, string? flaggedWord) CheckForNaughtyWords(string input, WordListJson naughtyWordList) { - // Replace any Cyrillic letters found in message with Latin characters, if in the dictionary - foreach (var letter in cyrillicAlphabetMap) + // Replace any lookalike letters found in message with Latin characters, if in the dictionary + foreach (var letter in lookalikeAlphabetMap) input = input.Replace(letter.Key, letter.Value); - // and Greek letters - foreach (var letter in greekAlphabetMap) - input = input.Replace(letter.Key, letter.Value); - string[] naughtyWords = naughtyWordList.Words; input = input.Replace("\0", ""); if (naughtyWordList.WholeWord) diff --git a/Events/MessageEvent.cs b/Events/MessageEvent.cs index 1c79a88a..609d58be 100644 --- a/Events/MessageEvent.cs +++ b/Events/MessageEvent.cs @@ -646,9 +646,7 @@ public static async Task MessageHandlerAsync(DiscordClient client, MockDiscordMe // attempted to ping @everyone/@here var msgContent = message.Content; - foreach (var letter in Checks.ListChecks.cyrillicAlphabetMap) - msgContent = msgContent.Replace(letter.Key, letter.Value); - foreach (var letter in Checks.ListChecks.greekAlphabetMap) + foreach (var letter in Checks.ListChecks.lookalikeAlphabetMap) msgContent = msgContent.Replace(letter.Key, letter.Value); if (Program.cfgjson.EveryoneFilter && !member.Roles.Any(role => Program.cfgjson.EveryoneExcludedRoles.Contains(role.Id)) && !Program.cfgjson.EveryoneExcludedChannels.Contains(channel.Id) && (msgContent.Contains("@everyone") || msgContent.Contains("@here"))) {