Merge pull request #5 from idle-org/dev-dan

Dev dan
This commit is contained in:
Maximilien Rigaut 2022-06-21 21:53:35 +02:00 committed by GitHub
commit 55aaaebfd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 140 additions and 111 deletions

View File

@ -1,109 +0,0 @@
import os
import re
import sys
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 = False
self.detections = 0
self.test_run = {}
# self.paths = self.check_paths(*self.get_path())
# self.spidey_check()
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

60
modules/spidey_test.py Normal file
View File

@ -0,0 +1,60 @@
import os
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
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

77
modules/test_interface.py Normal file
View File

@ -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

View File

@ -2,7 +2,7 @@
""" Main program """
from modules import spidey_detector, agnostic_paths
from modules import test_interface, agnostic_paths
import argparse
import sys
@ -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")
@ -34,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)