From e799252122d85c94007cd32867f58ad90d466c72 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Tue, 21 Jun 2022 20:27:09 +0200 Subject: [PATCH 1/2] Started the module loading --- modules/spidey_detector.py | 21 ++++++++++++++++++--- modules/spidey_test.py | 16 ++++++++++++++++ spideyDetector.py | 1 + 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 modules/spidey_test.py diff --git a/modules/spidey_detector.py b/modules/spidey_detector.py index 11bf972..fb1d62c 100644 --- a/modules/spidey_detector.py +++ b/modules/spidey_detector.py @@ -2,6 +2,10 @@ import os import re import sys +_TEST_MODULES = { # argparse argument : [module_name, class_name] + "spidey": ["spidey_test", "spidey"], +} + class SpideyDetector: def __init__(self, args, agnpath): @@ -18,13 +22,24 @@ class SpideyDetector: self.main_path = agnpath.main_path self.latest_version = agnpath.latest_version # self.is_windows = self.check_os() - self.is_infected = False + self.is_infected = 0 self.detections = 0 self.test_run = {} - # self.paths = self.check_paths(*self.get_path()) - # self.spidey_check() + self.loaded_modules = self.get_module_list() + self.spidey_check() + def get_module_list(self): + """ + Gets the list of modules to be loaded (in accordance to the config) + :return: List of modules + :rtype: list + """ + modules = [] + for module in _TEST_MODULES: + + if module[0] in self.loaded_modules: + modules.append(module[1]) def check_os(self): # noqa if not sys.platform.startswith('win'): raise OSError("This script can only check for Spideys on Windows.") diff --git a/modules/spidey_test.py b/modules/spidey_test.py new file mode 100644 index 0000000..7353fa9 --- /dev/null +++ b/modules/spidey_test.py @@ -0,0 +1,16 @@ +class SpideyTest: + def __init__(self, args, agnpath, test_data): + """ + Simple test runner, for testing the detection of Spidey viruses. + :param args: List of arguments + :type args: argparse.Namespace + :param agnpath: The path to the discord folder + :type agnpath: agnostic_path.AgnosticPath + :param test_data: The data to be tested + :type test_data: str + """ + self.ptb = args.ptb + self.agnostic_path = agnpath + self.os_name = agnpath.os + self.main_path = agnpath.main_path + self.test_data = test_data diff --git a/spideyDetector.py b/spideyDetector.py index 8ccab05..8dc783a 100644 --- a/spideyDetector.py +++ b/spideyDetector.py @@ -16,6 +16,7 @@ def parse(_args): parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.add_argument('-d', '--debug', action='store_true', help='Debug mode') parser.add_argument('-s', '--silent', action='store_true', help='Silent mode') + parser.add_argument('--spidey', action='store_true', help='Runs the spidey test') parser.add_argument('--line-count', action='store_true', help='Test all known files for line count') parser.add_argument('--file-count', action='store_true', help="Test the number of files in the discord folder") parser.add_argument('--file-size', action='store_true', help="Test the size of the files in the discord folder") From d93111a152821c96498bea8b47095f7f832d8806 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Tue, 21 Jun 2022 21:53:07 +0200 Subject: [PATCH 2/2] Added test module loading --- modules/spidey_detector.py | 124 ------------------------------------- modules/spidey_test.py | 44 +++++++++++++ modules/test_interface.py | 77 +++++++++++++++++++++++ spideyDetector.py | 4 +- 4 files changed, 123 insertions(+), 126 deletions(-) delete mode 100644 modules/spidey_detector.py create mode 100644 modules/test_interface.py diff --git a/modules/spidey_detector.py b/modules/spidey_detector.py deleted file mode 100644 index fb1d62c..0000000 --- a/modules/spidey_detector.py +++ /dev/null @@ -1,124 +0,0 @@ -import os -import re -import sys - -_TEST_MODULES = { # argparse argument : [module_name, class_name] - "spidey": ["spidey_test", "spidey"], -} - - -class SpideyDetector: - def __init__(self, args, agnpath): - """ - Main spidey runner - :param args: List of arguments - :type args: argparse.Namespace - :param agnpath: The path to the discord folder - :type agnpath: agnostic_path.AgnosticPath - """ - self.ptb = args.ptb - self.agnostic_path = agnpath - self.os_name = agnpath.os - self.main_path = agnpath.main_path - self.latest_version = agnpath.latest_version - # self.is_windows = self.check_os() - self.is_infected = 0 - self.detections = 0 - self.test_run = {} - - self.loaded_modules = self.get_module_list() - self.spidey_check() - - def get_module_list(self): - """ - Gets the list of modules to be loaded (in accordance to the config) - :return: List of modules - :rtype: list - """ - modules = [] - for module in _TEST_MODULES: - - if module[0] in self.loaded_modules: - modules.append(module[1]) - def check_os(self): # noqa - if not sys.platform.startswith('win'): - raise OSError("This script can only check for Spideys on Windows.") - return True - - def get_path(self): - """ - Gets the path of the two folders we need to check - :return: Two paths. - :rtype: str - """ - pre_version_path = os.path.join(os.path.expanduser('~'), "AppData", "Local", f"Discord{self.ptb}") - version = self.get_version(pre_version_path) - discord_modules = os.path.join(pre_version_path, version, - "modules", "discord_modules-1", "discord_modules") - discord_desktop_core = os.path.join(pre_version_path, version, - "modules", "discord_desktop_core-1", "discord_desktop_core") - return discord_modules, discord_desktop_core - - def get_version(self, pre_version_path): # noqa I swear this one is not static :( - """ - Looks for the version file in the discord folder - :param pre_version_path: the /discord/ path. - :type pre_version_path: str - :return: The folder in /discord/ that's only made of ints and dots. - :rtype: str - """ - version = None - for element in os.listdir(pre_version_path): - if re.match(r"app-[\d\.]+", element): - version = element - if not version: - raise FileNotFoundError("Could not find discord version folder.") - return version - - def check_paths(self, discord_modules, discord_desktop_core): # noqa - if not os.path.isfile(os.path.join(discord_modules, 'index.js')): - raise FileNotFoundError(f"Could not find index.js in {discord_modules}") - if not os.path.isfile(os.path.join(discord_desktop_core, 'index.js')): - raise FileNotFoundError(f"Could not find index.js in {discord_desktop_core}") - return discord_modules, discord_desktop_core - - def spidey_check(self): - """ - Counts the number of lines in both index.js files. If it's more than one, it's almost certain your discord - is corrupted. - :return: Whether any of the index.js is more than one line - :rtype: bool - """ - lines = 0 - with open(os.path.join(self.paths[0], "index.js")) as f: - for line in f: - lines = lines + 1 - if lines > 1: - self.is_infected = True - - lines = 0 - with open(os.path.join(self.paths[1], "index.js")) as f: - for line in f: - lines = lines + 1 - if lines > 1: - self.is_infected = True - - if self.is_infected: - print("\n!!! WARNING !!!\n" - "Unless you've installed a plugin you really trust, chances are your discord install " - "is corrupted. We suggest uninstalling discord and deleting all its files before reinstalling.\n" - "If you've recently downloaded a .exe directly sent from someone, " - "do also consider a clean windows reinstall.") - return self.is_infected - - -def run_check(args, agnpath): - spd = SpideyDetector(args, agnpath) - if not spd.is_infected: - return 0 - else: - return 1 - - -if __name__ == "__main__": - pass diff --git a/modules/spidey_test.py b/modules/spidey_test.py index 7353fa9..43d68cb 100644 --- a/modules/spidey_test.py +++ b/modules/spidey_test.py @@ -1,3 +1,6 @@ +import os + + class SpideyTest: def __init__(self, args, agnpath, test_data): """ @@ -14,3 +17,44 @@ class SpideyTest: self.os_name = agnpath.os self.main_path = agnpath.main_path self.test_data = test_data + + def run_test(self): + """ + Counts the number of lines in both index.js files. If it's more than one, it's almost certain your discord + is corrupted. + :return: Whether any of the index.js is more than one line + :rtype: bool + """ + paths = self.get_path() + self.is_infected = False + lines = 0 + with open(os.path.join(paths[0], "index.js")) as f: + for line in f: + lines += 1 + if lines > 1: + self.is_infected = True + + lines = 0 + with open(os.path.join(paths[1], "index.js")) as f: + for line in f: + lines += 1 + if lines > 1: + self.is_infected = True + + if self.is_infected: + print("\n!!! WARNING !!!\n" + "Unless you've installed a plugin you really trust, chances are your discord install " + "is corrupted. We suggest uninstalling discord and deleting all its files before reinstalling.\n" + "If you've recently downloaded a .exe directly sent from someone, " + "do also consider a clean windows reinstall.") + return self.is_infected + + def get_path(self): + """ + Gets the path of the two folders we need to check + :return: Two paths. + :rtype: str + """ + discord_modules = self.agnostic_path("modules", "discord_modules-1", "discord_modules") + discord_desktop_core = self.agnostic_path("modules", "discord_desktop_core-1", "discord_desktop_core") + return discord_modules, discord_desktop_core diff --git a/modules/test_interface.py b/modules/test_interface.py new file mode 100644 index 0000000..0a58a33 --- /dev/null +++ b/modules/test_interface.py @@ -0,0 +1,77 @@ +import os +import re +import sys +import importlib + +_TEST_MODULES = { # argparse argument : [module_name, class_name] + "spidey": ["spidey_test", "SpideyTest"], +} + + +class SpideyDetector: + def __init__(self, args, agnpath): + """ + Main spidey runner + :param args: List of arguments + :type args: argparse.Namespace + :param agnpath: The path to the discord folder + :type agnpath: agnostic_path.AgnosticPath + """ + self.ptb = args.ptb + self.args = args + self.agnostic_path = agnpath + self.os_name = agnpath.os + self.main_path = agnpath.main_path + self.version = agnpath.version + # self.is_windows = self.check_os() + self.is_infected = 0 + self.detections = 0 + self.test_run = {} + + self.loaded_test_modules = {} + self.loaded_test_classes = {} + self.update_module_list() + self.exec_all_tests() + + def update_module_list(self): + """ + Gets the list of modules to be loaded (in accordance to the config) + :return: List of modules + :rtype: list + """ + modules = {} + classes = {} + try: + for module_name, values in _TEST_MODULES.items(): + print(values[0]) + if self.args.__getattribute__(module_name): + modules[values[0]] = importlib.import_module(f"modules.{values[0]}") + classes[values[0]] = getattr(modules[values[0]], values[1]) + + except AttributeError: + print(f"The module {module_name} was not loaded because it was not specified in the config.") + + self.loaded_test_modules.update(modules) + self.loaded_test_classes.update(classes) + + def exec_all_tests(self): + """ + Executes all the tests + :return: + """ + for module_name, test_class in self.loaded_test_classes.items(): + tc = test_class(self.args, self.agnostic_path, None) + tc.run_test() + print(tc) + + +def run_check(args, agnpath): + spd = SpideyDetector(args, agnpath) + if not spd.is_infected: + return 0 + else: + return 1 + + +if __name__ == "__main__": + pass \ No newline at end of file diff --git a/spideyDetector.py b/spideyDetector.py index 8dc783a..72198a1 100644 --- a/spideyDetector.py +++ b/spideyDetector.py @@ -2,7 +2,7 @@ """ Main program """ -from modules import spidey_detector, agnostic_paths +from modules import test_interface, agnostic_paths import argparse import sys @@ -35,7 +35,7 @@ if __name__ == "__main__": args = parse(sys.argv[1:]) agnostic_path = agnostic_paths.AgnosticPaths(ptb=args.ptb) try: - spd = spidey_detector.run_check(args, agnostic_path) + spd = test_interface.run_check(args, agnostic_path) except FileNotFoundError as er: print(er)