Merge pull request #13 from idle-org/feature/arg_parse

Multithreading and some reporting
This commit is contained in:
Maximilien Rigaut 2022-06-23 15:23:21 +02:00 committed by GitHub
commit 10a5ef09f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 215 additions and 84 deletions

13
discordHelper.py Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/python3
""" Main program """
from modules import test_interface, agnostic_paths, argument_parser
import sys
if __name__ == "__main__":
args = argument_parser.parse(sys.argv[1:])
agnostic_path = agnostic_paths.AgnosticPaths(ptb=args.ptb, force_path=args.force_path)
test_interface.run_check(args, agnostic_path)

View File

@ -11,16 +11,19 @@ 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, ptb, force_path=None):
"""
: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 force_path: An alternative discord install path, usefull to test the script on a non standard install
:type force_path: str
"""
self.ptb = ptb
self.os = self.get_os()
self.force_path = force_path[0]
if force_path is not None:
self.force_path = force_path[0]
else:
self.force_path = None
self.main_path = self._get_path()
self.version = self.get_version()
self._update_path()
@ -47,12 +50,12 @@ 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}")

View File

@ -1,13 +1,7 @@
#!/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'
@ -28,26 +22,13 @@ def parse(_args): # TODO: Move this to a arg_parsing module
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("--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("--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("--gen-data", "--generate-data", "--gen", action="store_true", help="Generate data for the analysis")
parser.add_argument("--gen-data-path", "--generate-data-path", "--gen-path", nargs=1, default="", help="Path to generate the data in")
parser.add_argument("--database", "--db", nargs=1, default="", help="Alternative database for the analysis")
parser.add_argument("--continue", action="store_true", help="Continue the analysis in case of failure")
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)

17
modules/internal_io.py Normal file
View File

@ -0,0 +1,17 @@
"""
Internal interfacting objects.
"""
from collections import namedtuple
global_status = namedtuple('global_status', 'total_tests, tests_finished, tests_failed, tests_running, tests_skipped', defaults=(0, 0, 0, 0, 0))
test_status = namedtuple('test_status', 'name, status, message', defaults=(None, None, None))
return_code_dict = {
2: "The test was not run",
3: "The test is running",
0: "The test is a success",
1: "Test has failed",
4: "Test found problems",
}

View File

@ -1,8 +1,10 @@
import asyncio
import os
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):
"""
Simple test runner, for testing the detection of Spidey viruses.
:param args: List of arguments
@ -11,12 +13,10 @@ class SpideyTest:
:type agnpath: agnostic_path.AgnosticPath
:param test_data: The data to be tested
:type test_data: str
:param queue: The queue to put the status in
:type queue: queue.Queue
"""
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)
def run_test(self):
"""
@ -25,6 +25,9 @@ class SpideyTest:
:return: Whether any of the index.js is more than one line
:rtype: bool
"""
import random, time
# Wait a random amount of time to make sure the files are loaded
time.sleep(random.randint(1, 5))
paths = self.get_path()
self.is_infected = False
lines = 0
@ -47,7 +50,12 @@ class SpideyTest:
"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
print("Spidey test finished")
self.set_status(0)
status = self.get_status()
self.queue.append(status)
return self.get_status_code()
def get_path(self):
"""
@ -58,21 +66,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(), "")

View File

@ -1,7 +1,10 @@
import os
import re
import sys
import importlib
import threading
import time
import queue
from collections import deque
from modules.internal_io import global_status, test_status, return_code_dict
"""
All modules that can be tested
@ -11,6 +14,7 @@ argparse argument : [module_name, class_name]
_TEST_MODULES = {
"spidey": ["spidey_test", "SpideyTest"],
}
POST_RUN_ALLOWED = False
class TestRunner:
@ -35,7 +39,11 @@ class TestRunner:
self.loaded_test_modules = {}
self.loaded_test_classes = {}
self.list_of_tests = []
self.update_module_list()
self.finished_tests = deque()
self.exec_all_tests()
def update_module_list(self):
@ -46,9 +54,9 @@ class TestRunner:
"""
modules = {}
classes = {}
module_name = None
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])
@ -61,23 +69,134 @@ class TestRunner:
def exec_all_tests(self):
"""
Executes all the tests (later in parallel), get their result as they finishes
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():
tc = test_class(self.args, self.agnostic_path, None)
tc.run_test()
print(tc)
for i in range(10):
print(f"Starting test {module_name}")
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests)
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()
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:
# 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("Press enter to stop the tests: ")
while True:
if input() == "":
if not self.is_infected:
global POST_RUN_ALLOWED
POST_RUN_ALLOWED = True
return
def post_test_runner(self):
"""
Runs the post test runner if all tests are finished
"""
global POST_RUN_ALLOWED
if self.args.launch:
while True:
if POST_RUN_ALLOWED:
print("Running post test runner")
return 0
time.sleep(0.5)
return 0
else:
return 1
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()
# When all tests are finished or the user stopped the tests, we can proceed
if not self.is_infected:
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
"""
global POST_RUN_ALLOWED
if POST_RUN_ALLOWED:
for t in self.list_of_tests:
if t.is_alive():
return 2 # 2 means that the user stopped the tests before the end
return self.is_infected # The user didn't stop the tests
return -1 # The program is not finished yet
def get_status(self):
"""
Gets the status of the tests
:return: The status
:rtype: str
"""
nb_tests = len(self.list_of_tests)
nb_tests_success = 0
nb_tests_failure = 0
nb_tests_skipped = 0
for test in self.finished_tests:
print(f"Test {test.name} finished with status {test.status}")
if test.status == 0:
nb_tests_success += 1
elif test.status == 1:
nb_tests_failure += 1
elif test.status == 2:
nb_tests_skipped += 1
nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped
nb_test_running = nb_tests - nb_tests_ran
return global_status(
total_tests=nb_test_running,
tests_finished=nb_tests_ran,
tests_failed=nb_tests_failure,
tests_running=nb_test_running,
tests_skipped=nb_tests_skipped,
)
if __name__ == "__main__":
pass
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
"""
tr = TestRunner(args, agnpath) # All operation are executed in parallel and the user can stop them
print("Waiting for the end of the tests")
while tr.get_exit_code() == -1:
print(tr.get_status())
time.sleep(0.5)
sys.exit(tr.get_exit_code())

View File

@ -1,5 +1,12 @@
"""
A global template for all tests.
"""
from modules import internal_io
class TestTemplate:
def __init__(self, args, agnpath, test_data):
def __init__(self, args, agnpath, test_data, queue):
"""
Simple test template.
:param args: List of arguments
@ -17,13 +24,14 @@ class TestTemplate:
self.is_infected = False
self.status_code = 0
self.status = "Test not yet initialized."
self.return_code_dict = {
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",
}
self.queue = queue
def name(self):
"""
:return: Name of the test
:rtype: str
"""
return str(self.__class__.__name__)
def get_status_code(self):
"""
@ -37,15 +45,15 @@ class TestTemplate:
:return: Status at the time of call.
:rtype: str
"""
return self.status
return internal_io.test_status(self.name(), self.status_code, self.status)
@static
@staticmethod
def get_status_from_code(code):
"""
:return: Description of the status code
:rtype: str
"""
return self.return_code_dict[code]
return internal_io.return_code_dict[code]
def set_status(self, statuscode: int):
"""