commit
08ca0b1858
|
|
@ -131,3 +131,5 @@ dmypy.json
|
|||
|
||||
|
||||
/.idea/*
|
||||
databases/*
|
||||
/discordHelper/discordHelper_last_run.json.yaml
|
||||
|
|
|
|||
31
README.md
31
README.md
|
|
@ -1,2 +1,29 @@
|
|||
# SpideyDetector
|
||||
A python script to detect whether your discord was infected.
|
||||
# DiscordHelper
|
||||
|
||||
A python script to detect whether your discord was tempered with in any noticeable way.
|
||||
## Functionnement
|
||||
|
||||
DiscordHelper will provide a fast analysis over known malwares and progress it's analysis toward a complete sha1 checksum comparison against a known good install.
|
||||
|
||||
If the integrity can't be verified, the helper will propose to upload a new database with all computed data as a new staple to compare to (this functionnality is mostly useful if you have a custom/modded Discord).
|
||||
|
||||
# WARNING
|
||||
Due to the evolutive nature of viruses, only a new, clean install of DiscordHelper will warranty a total detection of all threats, since a virus will always try to temper all methods of detecting it.
|
||||
|
||||
# DISCLAMER
|
||||
While this program aims to provide a complete coverage over Discord Threats, there are many ways to temper with discord that cannot be covered in this program, mainly side-dll tempering and all forms of RAT and keyloggers. DiscordHelper cannot and will not try to analyse the rest of your operating system, we leave this intrusive behaviour to other anti-viruses.
|
||||
|
||||
## Security and trust
|
||||
We highly encourage all users to check, or have someone check our code.
|
||||
We do not analyse the content of your data, we do not send any data to our servers,
|
||||
and we do not try to temper in any way to your install, even to fix it.
|
||||
|
||||
--------
|
||||
|
||||
```
|
||||
__ \ _) | | | |
|
||||
| | | __| __| _ \ __| _` | | | _ \ | __ \ _ \ __|
|
||||
| | | \__ \ ( ( | | ( | ___ | __/ | | | __/ |
|
||||
____/ _| ____/ \___| \___/ _| \__,_| _| _| \___| _| .__/ \___| _|
|
||||
_|
|
||||
```
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
""" Main program """
|
||||
|
||||
from modules import main_runner, agnostic_paths, argument_parser
|
||||
import sys
|
||||
|
||||
|
||||
def main(argm):
|
||||
"""
|
||||
Main program
|
||||
:param argm: List of arguments
|
||||
:type argm: argparse.Namespace
|
||||
"""
|
||||
agnostic_path = agnostic_paths.AgnosticPaths(argm)
|
||||
test_runner = main_runner.run_check(argm, agnostic_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = argument_parser.parse(sys.argv[1:])
|
||||
main(args)
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
"""
|
||||
A windows executable for the discordHelper.py script.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import discordHelper
|
||||
import time
|
||||
|
||||
# All modules must be loaded before the main program is run.
|
||||
from modules import spidey_test, test_template
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
argv = sys.argv[1:]
|
||||
if len(argv) == 0:
|
||||
directory_path = os.path.join(os.getcwd(), "discordHelper")
|
||||
db_path = os.path.join(directory_path, "windows_base.json")
|
||||
argv = ["-p", db_path]
|
||||
if os.path.exists(db_path):
|
||||
# print(" > A user database was found it will be used instead of the bundled one.")
|
||||
# print(" > If you want to use the bundled database, delete the file: " + db_path)
|
||||
db_path = "--database " + db_path + " "
|
||||
else:
|
||||
db_path = ""
|
||||
# print(" > No user database was found, the bundled one will be used.")
|
||||
|
||||
run_db = os.path.join(directory_path, "discordHelper_last_run.json")
|
||||
argv = f"--all --autodetect --launch --continue --timeout 1 {db_path}--gen-data {run_db}.yaml".split(" ")
|
||||
|
||||
# print(" > Starting the discordHelper with the following arguments:")
|
||||
# print(" > " + " ".join(argv))
|
||||
|
||||
args = discordHelper.argument_parser.parse(argv)
|
||||
|
||||
discordHelper.main(args)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print(" > An error occured, please check the console for more information...")
|
||||
time.sleep(5)
|
||||
raise SystemExit
|
||||
|
|
@ -11,16 +11,18 @@ class AgnosticPaths:
|
|||
An agnostic path object, containing all methods to access the files and folders in a standard discord install
|
||||
"""
|
||||
|
||||
def __init__(self, ptb, force_path=""):
|
||||
def __init__(self, args):
|
||||
"""
|
||||
:param ptb: Whether the discord is a public beta install (ptb="PTB") or not (ptb="")
|
||||
:type ptb: str
|
||||
:param forced_path: An alternative discord install path, usefull to test the script on a non standard install
|
||||
:type forced_path: str
|
||||
:param args: The arguments passed to the script
|
||||
:type args: argparse.Namespace
|
||||
"""
|
||||
self.ptb = ptb
|
||||
self.args = args
|
||||
self.ptb = args.ptb
|
||||
self.os = self.get_os()
|
||||
self.force_path = force_path[0]
|
||||
if args.force_path is not None:
|
||||
self.force_path = args.force_path[0]
|
||||
else:
|
||||
self.force_path = None
|
||||
self.main_path = self._get_path()
|
||||
self.version = self.get_version()
|
||||
self._update_path()
|
||||
|
|
@ -47,15 +49,27 @@ class AgnosticPaths:
|
|||
|
||||
def _get_path(self):
|
||||
"""
|
||||
Return the base path of the install, note that the self.main_path is not returned,
|
||||
Return the base path of the install, note that the self.main_path is not returned,
|
||||
and this method is meant to be used at init only
|
||||
:return: Base path of the install (minus the version on windows)
|
||||
:return: Base path of the install (minus the version on Windows)
|
||||
:rtype: str
|
||||
"""
|
||||
if self.force_path != "":
|
||||
if self.force_path is not None:
|
||||
return self.force_path
|
||||
if self.os == 'windows':
|
||||
return os.path.join(os.path.expanduser('~'), "AppData", "Local", f"Discord{self.ptb}")
|
||||
base_path = os.path.join(os.path.expanduser('~'), "AppData", "Local", f"Discord")
|
||||
ptb_path = base_path+"PTB"
|
||||
if self.args.autodetect:
|
||||
if os.path.exists(ptb_path):
|
||||
self.ptb = "PTB"
|
||||
self.args.ptb = "PTB"
|
||||
return ptb_path
|
||||
else:
|
||||
return base_path
|
||||
if self.ptb == 'PTB':
|
||||
return base_path+"PTB"
|
||||
else:
|
||||
return base_path+"PTB" # Try to find the PTB version
|
||||
elif self.os == 'linux':
|
||||
return os.path.join("/", "opt", "discord")
|
||||
# return os.path.join("usr","share","discord")
|
||||
|
|
@ -141,6 +155,17 @@ class AgnosticPaths:
|
|||
:rtype: str
|
||||
"""
|
||||
return self.main_path
|
||||
|
||||
def walk_all_files(self, *args):
|
||||
"""
|
||||
Walks all files in the given path
|
||||
:param args: The path
|
||||
:return: A list of all files in the given path
|
||||
:rtype: list[str]
|
||||
"""
|
||||
return [os.path.join(dirpath, filename)
|
||||
for dirpath, dirnames, filenames in os.walk(self(*args))
|
||||
for filename in filenames]
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
|
|
@ -155,13 +180,3 @@ class AgnosticPaths:
|
|||
:rtype: str
|
||||
"""
|
||||
return self.main_path
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
path = AgnosticPaths("")
|
||||
print(path)
|
||||
print(path.version)
|
||||
print(path.list())
|
||||
print(path())
|
||||
print(path("resources", "bootstrap")) # Return the main_path plus resources, bootstrap
|
||||
print(path.list_dir_files("resources", "bootstrap")) # List the base_path/resources/bootstrap and return [[folders], [files]]
|
||||
|
|
|
|||
|
|
@ -1,21 +1,34 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
""" Main program """
|
||||
|
||||
from modules import test_interface, agnostic_paths
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
|
||||
def parse(_args): # TODO: Move this to a arg_parsing module
|
||||
def parse(_args):
|
||||
parser = argparse.ArgumentParser(
|
||||
prog="Spidey Detector",
|
||||
description='Checks for corrupt discord install'
|
||||
)
|
||||
parser.add_argument('--ptb', action='store_const', const="PTB", default="", help='Use Public Test Version')
|
||||
|
||||
parser.add_argument('--ptb', action='store_const', const="PTB", default="", help='Force use of Public Test Version')
|
||||
parser.add_argument('--autodetect', action='store_true', help='Try to autodetect the discord folder, will override --ptb')
|
||||
|
||||
# General arguments
|
||||
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("--continue_on_error", "--continue", action="store_true", help="Continue the analysis in case of failure")
|
||||
parser.add_argument("--timeout", default=1, type=int, help="Timeout for the tests")
|
||||
|
||||
# Post actions
|
||||
parser.add_argument('--launch', action='store_true', help="Launch the discord client after the tests are done")
|
||||
|
||||
# IO arguments
|
||||
parser.add_argument("--force-path", nargs=1, default=None, help="Bypass the automatic path discovery with given path")
|
||||
parser.add_argument("--only-known-paths", "--fast", action='store_true', help="Only run the check on the known paths")
|
||||
parser.add_argument("--gen-data", "--generate-data", "--gen", nargs=1, default="", help="Generate data for the analysis")
|
||||
parser.add_argument("--database", "--db", nargs=1, default="", help="Alternative database for the analysis")
|
||||
parser.add_argument("--pollrate", "--poll", default=0.1, type=float, help="Refresh rate of the status, 0.1 is the default")
|
||||
parser.add_argument("--printrate", "-refresh", default=5, type=int, help="Print the tests every X seconds")
|
||||
|
||||
# Tests
|
||||
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")
|
||||
|
|
@ -27,24 +40,8 @@ def parse(_args): # TODO: Move this to a arg_parsing module
|
|||
parser.add_argument('--file-date', action='store_true', help="Test the date of the files in the discord folder")
|
||||
parser.add_argument('--analyze', action='store_true', help="Analyze the files in the discord folder for known js obfuscators")
|
||||
parser.add_argument('--all', action='store_true', help="Run all tests in parallel, and wait for a potential interupt")
|
||||
parser.add_argument('--launch', action='store_true', help="Launch the discord client after the tests are done")
|
||||
parser.add_argument("--force-path", nargs=1, default="", help="Bypass the automatic path discovery with given path")
|
||||
parser.add_argument("--no-eval", action="store_true", help="Check for the eval function wich is a HUGE red flag")
|
||||
parser.add_argument("--const-abuse", "--no-const", action="store_true", help="Check for the abuse of the const: keyword, wich is massively used for obfucation")
|
||||
parser.add_argument("--test-walk", action="store_true", help="Test walk function")
|
||||
|
||||
return parser.parse_args(_args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = parse(sys.argv[1:])
|
||||
agnostic_path = agnostic_paths.AgnosticPaths(ptb=args.ptb, force_path=args.force_path)
|
||||
try:
|
||||
spd = test_interface.run_check(args, agnostic_path)
|
||||
|
||||
except FileNotFoundError as er:
|
||||
print(er)
|
||||
sys.exit(1)
|
||||
except OSError as er:
|
||||
print(er.strerror)
|
||||
sys.exit(1)
|
||||
|
||||
if spd == 0:
|
||||
print("TestRunner could not find anything wrong with your discord install!") # TODO: Everything should be in the run_all_tests()
|
||||
sys.exit(spd)
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
"""
|
||||
Internal interfacting objects.
|
||||
"""
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
global_status = namedtuple(
|
||||
'global_status',
|
||||
'tests_total, tests_finished, tests_success, tests_failed, tests_running, tests_skipped, tests_error, progress',
|
||||
defaults=(0, 0, 0, 0, 0, 0, 0, 0)
|
||||
)
|
||||
test_status = namedtuple(
|
||||
'test_status', 'name, status, message, failure_dict, data, progress',
|
||||
defaults=(None, None, None, {}, {}, 0)
|
||||
)
|
||||
return_code_dict = {
|
||||
"skipped": "The test was not run",
|
||||
"running": "The test is running",
|
||||
"success": "The test is a success",
|
||||
"failure": "Test has failed",
|
||||
"problems": "Test found problems",
|
||||
"idle": "Test is idle",
|
||||
}
|
||||
|
|
@ -0,0 +1,406 @@
|
|||
import json
|
||||
import os
|
||||
import sys
|
||||
import importlib
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
from datetime import datetime
|
||||
|
||||
from modules.internal_io import global_status # , test_status, return_code_dict
|
||||
|
||||
"""
|
||||
All modules that can be tested
|
||||
Associate the arguments passed to the command line with the modules to be loaded
|
||||
argparse argument : [module_name, class_name]
|
||||
"""
|
||||
from termcolor import colored, cprint
|
||||
os.system('color')
|
||||
# red, green, yellow, blue, magenta, cyan, white.
|
||||
|
||||
_TEST_MODULES = {
|
||||
"spidey": ["spidey_test", "SpideyTest"],
|
||||
# "test_walk": ["test_template", "TestWalkTemplate"],
|
||||
}
|
||||
|
||||
PROGRAM_VERSION = "1.0.0"
|
||||
POST_RUN_ALLOWED = False # TODO : Remove this line, only use is_infected
|
||||
RUN_OVER = False
|
||||
|
||||
queue_lock = threading.Lock()
|
||||
processes_lock = threading.Lock()
|
||||
|
||||
CLEAR_CODE = colored(r"""
|
||||
_______ _
|
||||
(_______) |
|
||||
_ | | _____ _____ ____
|
||||
| | | || ___ (____ |/ ___)
|
||||
| |_____| || ____/ ___ | |
|
||||
\______)\_)_____)_____|_|
|
||||
""", 'green')
|
||||
|
||||
INFECTED_CODE = colored(r"""
|
||||
_ ___ _
|
||||
| | / __) _ | |
|
||||
| |____ _| |__ _____ ____ _| |_ _____ __| |
|
||||
| | _ (_ __) ___ |/ ___|_ _) ___ |/ _ |
|
||||
| | | | || | | ____( (___ | |_| ____( (_| |
|
||||
|_|_| |_||_| |_____)\____) \__)_____)\____|
|
||||
""", 'red')
|
||||
|
||||
LOGO = colored(r"""
|
||||
__ \ _) | | | |
|
||||
| | | __| __| _ \ __| _` | | | _ \ | __ \ _ \ __|
|
||||
| | | \__ \ ( ( | | ( | ___ | __/ | | | __/ |
|
||||
____/ _| ____/ \___| \___/ _| \__,_| _| _| \___| _| .__/ \___| _|
|
||||
_|
|
||||
""", "cyan")
|
||||
|
||||
|
||||
class TestRunner:
|
||||
def __init__(self, args, agnpath, start_time):
|
||||
"""
|
||||
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
|
||||
:param start_time: The time the test started
|
||||
:type start_time: float
|
||||
"""
|
||||
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.start_time = start_time
|
||||
|
||||
self.is_infected = 0
|
||||
self.detections = 0
|
||||
self.test_run = {}
|
||||
|
||||
self.loaded_test_modules = {}
|
||||
self.loaded_test_classes = {}
|
||||
self.list_of_tests = []
|
||||
self.dict_of_processes = {} # To get a more granular view of the processes
|
||||
self.update_module_list()
|
||||
|
||||
self.finished_tests = deque()
|
||||
|
||||
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 = {}
|
||||
module_name = None
|
||||
try:
|
||||
for module_name, values in _TEST_MODULES.items():
|
||||
if self.args.all or 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 as e:
|
||||
print(f"The module {module_name} was not loaded because it was not specified in the config.")
|
||||
print(e)
|
||||
|
||||
self.loaded_test_modules.update(modules)
|
||||
self.loaded_test_classes.update(classes)
|
||||
|
||||
def exec_all_tests(self):
|
||||
"""
|
||||
Executes all the tests, get their result as they finishes
|
||||
:return: The number of test ran, success, failures, skipped (with ran = success+failures+skip)
|
||||
:rtype: (int,int,int,int) # TODO: Change this to a nametuple
|
||||
"""
|
||||
self.list_of_tests = []
|
||||
for module_name, test_class in self.loaded_test_classes.items():
|
||||
print(colored(f" > Starting test {module_name}...", "blue"))
|
||||
tc = test_class(
|
||||
self.args,
|
||||
self.agnostic_path,
|
||||
None,
|
||||
self.finished_tests,
|
||||
queue_lock,
|
||||
self.dict_of_processes,
|
||||
processes_lock
|
||||
)
|
||||
tc = threading.Thread(target=tc.run_test, args=())
|
||||
tc.name = str(module_name)
|
||||
tc.daemon = True
|
||||
self.list_of_tests.append(tc)
|
||||
tc.start()
|
||||
|
||||
# Launch a controller to allow the user to stop all tests
|
||||
# controller = threading.Thread(target=self.test_launcher)
|
||||
# Allow the user to stop the tests
|
||||
tc = threading.Thread(target=self.controller_test, args=())
|
||||
tc.daemon = True
|
||||
tc.start()
|
||||
|
||||
# Await the end of the tests or the user interruption
|
||||
tc = threading.Thread(target=self.post_test_runner, args=())
|
||||
tc.start()
|
||||
|
||||
# Wait for the end of the tests
|
||||
tc = threading.Thread(target=self.test_waiter, args=())
|
||||
tc.daemon = True
|
||||
tc.start()
|
||||
|
||||
return self.is_infected, self.detections, self.test_run
|
||||
|
||||
def controller_test(self):
|
||||
"""
|
||||
Kills the tests if the user press enter
|
||||
"""
|
||||
print(colored("\n > All test started, press ENTER to abort...\n", "blue"))
|
||||
while True:
|
||||
#print("Controller still up")
|
||||
try:
|
||||
if input() == "":
|
||||
global POST_RUN_ALLOWED, RUN_OVER
|
||||
print(colored("\n > Aborting tests...\n", "red"))
|
||||
if not self.is_infected:
|
||||
POST_RUN_ALLOWED = True
|
||||
RUN_OVER = True
|
||||
return
|
||||
except KeyboardInterrupt:
|
||||
RUN_OVER = True
|
||||
return
|
||||
except:
|
||||
sys.exit()
|
||||
|
||||
def post_test_runner(self):
|
||||
"""
|
||||
Runs the post test runner if all tests are finished
|
||||
"""
|
||||
global POST_RUN_ALLOWED, RUN_OVER
|
||||
if self.args.launch:
|
||||
while True:
|
||||
if RUN_OVER:
|
||||
if POST_RUN_ALLOWED and self.is_infected == 0: # Second part is redundant
|
||||
print(colored("\n > Running post test runner...", "green"))
|
||||
return 0
|
||||
return 1
|
||||
time.sleep(0.1)
|
||||
return 0
|
||||
|
||||
def test_waiter(self):
|
||||
"""
|
||||
Waits for the end of the tests
|
||||
:return:
|
||||
"""
|
||||
for t in self.list_of_tests: # Can only proceed if all tests are finished
|
||||
t.join()
|
||||
self.get_status()
|
||||
# time.sleep(0.5)
|
||||
# When all tests are finished or the user stopped the tests, we can proceed
|
||||
if self.is_infected == 0:
|
||||
global POST_RUN_ALLOWED
|
||||
POST_RUN_ALLOWED = True
|
||||
|
||||
def get_exit_code(self):
|
||||
"""
|
||||
Gets the exit code of the tests
|
||||
:return: The exit code
|
||||
:rtype: int
|
||||
"""
|
||||
status = self.get_status()
|
||||
if status.tests_finished == status.tests_total:
|
||||
if status.tests_failed != 0:
|
||||
exit_code = 1 # There is one failure, so the test failed
|
||||
elif status.tests_success == status.tests_total:
|
||||
exit_code = 0 # All tests passed
|
||||
else:
|
||||
# print("Something went wrong, please report this bug.")
|
||||
# print(status)
|
||||
exit_code = 2 # Something went wrong
|
||||
else:
|
||||
global RUN_OVER
|
||||
if RUN_OVER:
|
||||
print(colored("\n > Tests aborted, please check the logs.", "red"))
|
||||
exit_code = 3
|
||||
else:
|
||||
exit_code = -1 # The tests are not finished
|
||||
return exit_code
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
Gets the status of the tests
|
||||
:return: The status
|
||||
:rtype: internal_io.global_status
|
||||
"""
|
||||
queue_lock.acquire(timeout=0.5)
|
||||
nb_tests = len(self.list_of_tests)
|
||||
nb_tests_success = 0
|
||||
nb_tests_failure = 0
|
||||
nb_tests_skipped = 0
|
||||
progress = 0
|
||||
for test in self.finished_tests:
|
||||
if test.status == "success":
|
||||
nb_tests_success += 1
|
||||
elif test.status == "failure":
|
||||
nb_tests_failure += 1
|
||||
self.is_infected += 1
|
||||
elif test.status == "skipped":
|
||||
nb_tests_skipped += 1
|
||||
elif test.status == "problem":
|
||||
nb_tests_failure += 1
|
||||
# progress += test.progress
|
||||
queue_lock.release()
|
||||
|
||||
for test_name, test in self.dict_of_processes.items():
|
||||
progress += test.progress
|
||||
progress = int(progress / nb_tests)
|
||||
|
||||
nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped
|
||||
nb_test_running = nb_tests - nb_tests_ran
|
||||
return global_status(
|
||||
tests_total=nb_tests,
|
||||
tests_finished=nb_tests_ran,
|
||||
tests_success=nb_tests_success,
|
||||
tests_failed=nb_tests_failure,
|
||||
tests_running=nb_test_running,
|
||||
tests_skipped=nb_tests_skipped,
|
||||
tests_error=nb_tests_failure,
|
||||
progress=progress
|
||||
)
|
||||
|
||||
def get_test_data(self):
|
||||
"""
|
||||
Gets the test data
|
||||
:return: The test data
|
||||
:rtype: dict
|
||||
"""
|
||||
data = {}
|
||||
queue_lock.acquire()
|
||||
for test in self.finished_tests:
|
||||
for key, value in test.data.items():
|
||||
if key not in data:
|
||||
data[key] = value
|
||||
else:
|
||||
data[key].update(value)
|
||||
queue_lock.release()
|
||||
return data
|
||||
|
||||
|
||||
def export_data(test_runner, data):
|
||||
"""
|
||||
Exports the test data
|
||||
:param test_runner: The test runner
|
||||
:type test_runner: TestRunner
|
||||
:param data: The test data
|
||||
:type data: dict
|
||||
"""
|
||||
main_data = {
|
||||
"global":
|
||||
{
|
||||
"os": test_runner.os_name,
|
||||
"ptb": test_runner.ptb,
|
||||
"discord_version": test_runner.version,
|
||||
"program_version": PROGRAM_VERSION,
|
||||
},
|
||||
"tests": data,
|
||||
}
|
||||
return main_data
|
||||
|
||||
|
||||
def print_status(status, timer_start):
|
||||
"""
|
||||
Prints the status
|
||||
:param status: The status
|
||||
:type status: internal_io.global_status
|
||||
:param timer_start: The start time of the tests
|
||||
:type timer_start: float
|
||||
"""
|
||||
if status.tests_failed != 0:
|
||||
color = "red"
|
||||
else:
|
||||
color = "green"
|
||||
|
||||
print(colored("\n > Status:", color))
|
||||
if status.tests_total == 0:
|
||||
print(colored(" > No test found", "red"))
|
||||
return
|
||||
|
||||
print(colored(
|
||||
f" > Test finished {status.tests_finished}/{status.tests_total}"
|
||||
f": {status.tests_success} success, "
|
||||
f"{status.tests_failed} failed, "
|
||||
f"{status.tests_error} problem", color
|
||||
))
|
||||
if status.tests_running != 0:
|
||||
ellapsed_time = time.time() - timer_start
|
||||
if status.progress == 0:
|
||||
estimated_time = "∞"
|
||||
else:
|
||||
mon, sec = divmod(ellapsed_time * 100 / status.progress, 60)
|
||||
hr, mon = divmod(mon, 60)
|
||||
estimated_time = "%d:%02d:%02d" % (hr, mon, sec)
|
||||
print(colored(
|
||||
f" > Overall progress: {status.progress}%"
|
||||
f" in {time.time() - timer_start:.2f} seconds"
|
||||
f" estimaded time ({estimated_time})", color
|
||||
))
|
||||
|
||||
|
||||
def run_check(args, agnpath):
|
||||
"""
|
||||
Runs all operations selected in the config
|
||||
:param args: List of arguments
|
||||
:param agnpath: Annostic path to the discord folder
|
||||
:return: None
|
||||
"""
|
||||
print(LOGO)
|
||||
start = time.time()
|
||||
tr = TestRunner(args, agnpath, start_time=start) # All operation are executed in parallel and the user can stop them
|
||||
|
||||
POLLRATE, PRINT_RATE, COUNTER = args.pollrate, args.printrate, 0
|
||||
COUNTER_MAX = int(PRINT_RATE / POLLRATE)
|
||||
|
||||
while tr.get_exit_code() == -1:
|
||||
# print("Waiting for the end of the tests")
|
||||
if (COUNTER % COUNTER_MAX) == 0:
|
||||
print_status(tr.get_status(), start) # TODO : Print status in a more readable way
|
||||
time.sleep(POLLRATE)
|
||||
COUNTER += 1
|
||||
|
||||
global RUN_OVER
|
||||
RUN_OVER = True
|
||||
|
||||
status = tr.get_status()
|
||||
if status.tests_failed > 0:
|
||||
print(INFECTED_CODE)
|
||||
# tr.is_infected += 1
|
||||
color = "red"
|
||||
else:
|
||||
print(CLEAR_CODE)
|
||||
color = "green"
|
||||
# tr.is_infected = False
|
||||
|
||||
print(colored(f"\nThe test sequence is now finished, the program ran for {round(time.time() - start,2)} seconds", color))
|
||||
print_status(status, start) # TODO: Print the final status, test still running are actually skipped
|
||||
|
||||
if args.gen_data:
|
||||
print(colored("\n > Generating data...", "blue"))
|
||||
data = tr.get_test_data()
|
||||
file_path = os.path.join(os.path.join("databases", args.gen_data[0]))
|
||||
folder = os.path.dirname(file_path)
|
||||
if not os.path.exists(folder):
|
||||
print(colored(" > Making folder...", "blue"))
|
||||
os.makedirs(folder)
|
||||
print(colored(f" > Exporting the test data to {file_path}", "blue")) # TODO : Advertize where the data is exported
|
||||
main_data = export_data(tr, data)
|
||||
# print(main_data)
|
||||
with open(file_path, "w") as f:
|
||||
json.dump(main_data, f, indent=4)
|
||||
|
||||
msg_error = "sucessfully" if status.tests_failed == 0 else f"with {status.tests_error} errors"
|
||||
print(colored(f"\nThe program executed {msg_error}, exiting in {args.timeout} seconds...", color))
|
||||
time.sleep(args.timeout)
|
||||
sys.exit(tr.get_exit_code())
|
||||
|
|
@ -1,22 +1,16 @@
|
|||
import asyncio
|
||||
import os
|
||||
import time
|
||||
|
||||
from modules import test_template
|
||||
|
||||
|
||||
class SpideyTest:
|
||||
def __init__(self, args, agnpath, test_data):
|
||||
class SpideyTest(test_template.TestTemplate):
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
|
||||
"""
|
||||
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
|
||||
super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock)
|
||||
|
||||
def run_test(self):
|
||||
"""
|
||||
|
|
@ -25,29 +19,37 @@ class SpideyTest:
|
|||
:return: Whether any of the index.js is more than one line
|
||||
:rtype: bool
|
||||
"""
|
||||
self.set_status("running")
|
||||
|
||||
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
|
||||
lines += 1 # TODO: you don't actually need to count the lines, you can just check if it's more than 1
|
||||
if lines > 1:
|
||||
self.is_infected = True
|
||||
|
||||
self.progress = 50
|
||||
# time.sleep(5)
|
||||
lines = 0
|
||||
|
||||
with open(os.path.join(paths[1], "index.js")) as f:
|
||||
for line in f:
|
||||
lines += 1
|
||||
lines += 1 # TODO: you don't actually need to count the lines, you can just check if it's more than 1
|
||||
if lines > 1:
|
||||
self.is_infected = True
|
||||
|
||||
if self.is_infected:
|
||||
print("\n!!! WARNING !!!\n"
|
||||
# self.is_infected = True
|
||||
if self.is_infected: # TODO: set a status here, explaining and why the test failed, the scary part will be handled by the main program
|
||||
return self.finish("failure", "\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
|
||||
|
||||
self.progress = 100
|
||||
return self.finish("success")
|
||||
|
||||
def get_path(self):
|
||||
"""
|
||||
|
|
@ -58,21 +60,3 @@ class SpideyTest:
|
|||
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
|
||||
|
||||
def get_status_code(self):
|
||||
"""
|
||||
The status code of the test
|
||||
:return: 0: The test was not run, 1: The test is running, 2: The test is a success, 3: Test has failed, 4: Test found problems
|
||||
:rtype: int
|
||||
"""
|
||||
# TODO: Add this to a global_dict
|
||||
return 0
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
Return the status of the test, as a string, as the test is ran
|
||||
:return: "Test is running", "Test was not run", "Test was run and nothing was detected", "Test was run and some problems were detected (list of problems)"
|
||||
:rtype: (int, str)
|
||||
"""
|
||||
# TODO: Make use of a global_dict
|
||||
return (self.get_status_code(), "")
|
||||
|
|
|
|||
|
|
@ -1,83 +0,0 @@
|
|||
import os
|
||||
import re
|
||||
import sys
|
||||
import importlib
|
||||
|
||||
"""
|
||||
All modules that can be tested
|
||||
Associate the arguments passed to the command line with the modules to be loaded
|
||||
argparse argument : [module_name, class_name]
|
||||
"""
|
||||
_TEST_MODULES = {
|
||||
"spidey": ["spidey_test", "SpideyTest"],
|
||||
}
|
||||
|
||||
|
||||
class TestRunner:
|
||||
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_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 (later in parallel), get their result as they finishes
|
||||
:return: The number of test ran, success, failures, skipped (with ran = success+failures+skip)
|
||||
:rtype: (int,int,int,int) # TODO: Change this to a nametuple
|
||||
"""
|
||||
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): # Todo, skip the is_infected check and defer everything to the exec_all_tests
|
||||
spd = TestRunner(args, agnpath)
|
||||
if not spd.is_infected:
|
||||
return 0
|
||||
else:
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pass
|
||||
|
|
@ -0,0 +1,272 @@
|
|||
"""
|
||||
A global template for all tests.
|
||||
"""
|
||||
|
||||
from modules import internal_io
|
||||
|
||||
|
||||
class TestDataError(Exception):
|
||||
"""
|
||||
Raised when the data is not valid.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class TestTemplate:
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
|
||||
"""
|
||||
Simple test template.
|
||||
: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
|
||||
:param queue: The queue to put the test status in
|
||||
:type queue: queue.deque
|
||||
:param queue_lock: The lock to use when putting the test status in the queue
|
||||
:type queue_lock: threading.Lock
|
||||
:param dict_process: The dictionary of processes
|
||||
:type dict_process: dict
|
||||
:param dict_process_lock: The lock to use when putting the process in the dictionary
|
||||
:type dict_process_lock: threading.Lock
|
||||
"""
|
||||
self.args = args
|
||||
self.ptb = args.ptb
|
||||
self.agnostic_path = agnpath
|
||||
self.os_name = agnpath.os
|
||||
self.main_path = agnpath.main_path
|
||||
self.test_data = test_data
|
||||
self.is_infected = False
|
||||
self.status_code = "idle"
|
||||
self.status = "Test not yet initialized."
|
||||
self.queue = queue
|
||||
self.set_status("idle")
|
||||
self.queue_lock = queue_lock
|
||||
self.failure_dict = {}
|
||||
self.new_data = {}
|
||||
self.progress = 0
|
||||
self.dict_process = dict_process
|
||||
self.dict_process_lock = dict_process_lock
|
||||
self.dict_process_lock.acquire()
|
||||
self.dict_process[self.name()] = self
|
||||
self.dict_process_lock.release()
|
||||
|
||||
def run_test(self):
|
||||
"""
|
||||
The main runner function, must be implemented by the child class.
|
||||
During the execution of the test, the status code and status MUST be updated.
|
||||
"""
|
||||
self.set_status("running", "The test will begin shortly, please wait...")
|
||||
self.set_status("sucess", "The test was a success but kinda not a success.")
|
||||
|
||||
def name(self):
|
||||
"""
|
||||
:return: Name of the test
|
||||
:rtype: str
|
||||
"""
|
||||
return str(self.__class__.__name__)
|
||||
|
||||
def get_status_code(self):
|
||||
"""
|
||||
:return: Status code at the time of call.
|
||||
:rtype: str
|
||||
"""
|
||||
return self.status_code
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
:return: Status at the time of call.
|
||||
:rtype: internal_io.test_status
|
||||
"""
|
||||
return internal_io.test_status(
|
||||
name=self.name(),
|
||||
status=self.status_code,
|
||||
message=self.status,
|
||||
failure_dict=self.failure_dict,
|
||||
data=self.new_data,
|
||||
progress=self.progress,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_status_from_code(code):
|
||||
"""
|
||||
:param code: The status code to get the status from.
|
||||
:type code: str
|
||||
:return: Description of the status code
|
||||
:rtype: str
|
||||
"""
|
||||
return internal_io.return_code_dict[code]
|
||||
|
||||
def set_status(self, statuscode: str, message: str = None):
|
||||
"""
|
||||
Sets a new status code.
|
||||
:param statuscode: Status code to set
|
||||
:param message: Message to set
|
||||
:type statuscode: str
|
||||
:return: new status code
|
||||
:rtype: str
|
||||
"""
|
||||
self.status_code = statuscode
|
||||
if message is not None:
|
||||
self.status = message
|
||||
else:
|
||||
self.status = self.get_status_from_code(statuscode)
|
||||
return self.status_code
|
||||
|
||||
def finish(self, statuscode: str = None, message: str = None):
|
||||
"""
|
||||
Finishes the test.
|
||||
"""
|
||||
if message is not None:
|
||||
self.status = message
|
||||
if statuscode is not None:
|
||||
self.status_code = statuscode
|
||||
|
||||
self.queue_lock.acquire()
|
||||
# self.progress = 1
|
||||
self.queue.append(self.get_status())
|
||||
self.queue_lock.release()
|
||||
return self.get_status_code()
|
||||
|
||||
def add_failure(self, path, message):
|
||||
"""
|
||||
Adds a failure to the failure dictionary.
|
||||
:param path: Path to the file that failed
|
||||
:type path: str
|
||||
:param message: Message to add
|
||||
:type message: str
|
||||
"""
|
||||
self.failure_dict[path] = message
|
||||
|
||||
def __del__(self):
|
||||
"""
|
||||
Destructor.
|
||||
"""
|
||||
if self.status_code == "running":
|
||||
return self.finish("skipped")
|
||||
|
||||
|
||||
class TestWalkTemplate(TestTemplate):
|
||||
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
|
||||
"""
|
||||
Simple test template.
|
||||
: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
|
||||
"""
|
||||
super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock)
|
||||
if test_data is None:
|
||||
self.set_status("problem", "No test data given.")
|
||||
|
||||
def add_test_result(self, path, testname, result):
|
||||
"""
|
||||
Adds a test result to the data dictionary.
|
||||
:param path: Path to the file that was tested
|
||||
:type path: str
|
||||
:param testname: Name of the test
|
||||
:type testname: str
|
||||
:param result: Result of the test
|
||||
:type result: any
|
||||
"""
|
||||
|
||||
if path not in self.new_data:
|
||||
self.new_data[path] = {}
|
||||
self.new_data[path][testname] = result
|
||||
return result
|
||||
|
||||
def get_test_data(self, path, testname):
|
||||
"""
|
||||
Gets the test data from the data dictionary.
|
||||
:param path: Path to the file that was tested
|
||||
:type path: str
|
||||
:param testname: Name of the test
|
||||
:type testname: str
|
||||
:return: Test data
|
||||
:rtype: any
|
||||
"""
|
||||
if path in self.new_data:
|
||||
if testname in self.new_data[path]:
|
||||
return self.new_data[path][testname]
|
||||
return None
|
||||
|
||||
def get_expected_result(self, path, testname):
|
||||
"""
|
||||
Gets the expected result from the data dictionary.
|
||||
:param path: Path to the file that was tested
|
||||
:type path: str
|
||||
:param testname: Name of the test
|
||||
:type testname: str
|
||||
:return: Expected result
|
||||
:rtype: any
|
||||
"""
|
||||
if self.test_data:
|
||||
if path in self.test_data:
|
||||
if testname in self.test_data[path]:
|
||||
return self.test_data[path][testname]
|
||||
return None
|
||||
|
||||
def run_test(self):
|
||||
"""
|
||||
The main runner function, must be implemented by the child class.
|
||||
Here is an example of how to use the add_test_result function.
|
||||
:return:
|
||||
"""
|
||||
self.set_status("running", "The test will begin shortly, please wait...")
|
||||
|
||||
if self.test_data is None and not self.args.continue_on_error:
|
||||
self.set_status("problem", "No test data given.")
|
||||
return self.finish()
|
||||
|
||||
self.set_status("running", "The test will begin shortly, please wait...")
|
||||
for path in self.walk():
|
||||
print(path)
|
||||
if not self.compare(path, "test", lambda x: True, [], {}):
|
||||
self.add_failure(path, f"Test {self.name()} failed on {path}")
|
||||
if not self.args.continue_on_error:
|
||||
self.set_status("failure", f"Test {self.name()} failed on {path}")
|
||||
return self.finish()
|
||||
|
||||
if len(self.failure_dict) > 0:
|
||||
self.set_status("failure", f"Test {self.name()} failed on {len(self.failure_dict)} files.")
|
||||
else:
|
||||
self.set_status("success", f"Test {self.name()} passed.")
|
||||
|
||||
return self.finish()
|
||||
|
||||
def walk(self):
|
||||
"""
|
||||
Walks the given path and returns a list of all files and directories.
|
||||
:return: List of all files and directories
|
||||
:return:
|
||||
"""
|
||||
return self.agnostic_path.walk_all_files(self.agnostic_path())
|
||||
|
||||
def compare(self, path, entry_name, function, args, kwargs):
|
||||
"""
|
||||
Use the function on the given path, with given arguments and compare it with the entry in the test data.
|
||||
:param path: Path to the file or directory
|
||||
:type path: agnostic_path.AgnosticPath
|
||||
:param entry_name: Name of the entry in the test data
|
||||
:type entry_name: str
|
||||
:param function: Function to use on the path
|
||||
:type function: function
|
||||
:param args: Arguments to use on the function
|
||||
:type args: list
|
||||
:param kwargs: Keyword arguments to use on the function
|
||||
:type kwargs: dict
|
||||
"""
|
||||
|
||||
data_origin = self.add_test_result(path, entry_name, function(path, *args, **kwargs))
|
||||
expected_data = self.get_expected_result(path, entry_name)
|
||||
if expected_data is None and not self.args.continue_on_error:
|
||||
self.set_status("problem", f"No expected data found for {path}")
|
||||
return False
|
||||
|
||||
if data_origin != expected_data:
|
||||
return False
|
||||
return True
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 316 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 371 KiB |
|
|
@ -0,0 +1,45 @@
|
|||
# -*- mode: python ; coding: utf-8 -*-
|
||||
|
||||
|
||||
block_cipher = None
|
||||
|
||||
|
||||
a = Analysis(
|
||||
['discordHelperWin.py'],
|
||||
pathex=[],
|
||||
binaries=[],
|
||||
datas=[('databases/*', 'databases/')],
|
||||
hiddenimports=[],
|
||||
hookspath=[],
|
||||
hooksconfig={},
|
||||
runtime_hooks=[],
|
||||
excludes=[],
|
||||
win_no_prefer_redirects=False,
|
||||
win_private_assemblies=False,
|
||||
cipher=block_cipher,
|
||||
noarchive=False,
|
||||
)
|
||||
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
|
||||
|
||||
exe = EXE(
|
||||
pyz,
|
||||
a.scripts,
|
||||
a.binaries,
|
||||
a.zipfiles,
|
||||
a.datas,
|
||||
[],
|
||||
name='discordHelperWin',
|
||||
debug=False,
|
||||
bootloader_ignore_signals=False,
|
||||
strip=False,
|
||||
upx=True,
|
||||
upx_exclude=[],
|
||||
runtime_tmpdir=None,
|
||||
console=True,
|
||||
disable_windowed_traceback=False,
|
||||
argv_emulation=False,
|
||||
target_arch=None,
|
||||
codesign_identity=None,
|
||||
entitlements_file=None,
|
||||
icon='ressources/DiscordHelper.ico',
|
||||
)
|
||||
Reference in New Issue