Merge pull request #21 from idle-org/features/win_release

Features/win release
This commit is contained in:
Maximilien Rigaut 2022-06-25 06:50:15 +02:00 committed by GitHub
commit 33ec564658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 4676 additions and 2991 deletions

File diff suppressed because it is too large Load Diff

View File

@ -24,8 +24,9 @@ if __name__ == "__main__":
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(" ")
# Will always try to load discordHelper/discordHelper.json # TODO : Document this feature.
run_db = os.path.join(directory_path, "test_results_found.json")
argv = rf"--all --autodetect --launch --continue --timeout 10 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ")
# print(" > Starting the discordHelper with the following arguments:")
# print(" > " + " ".join(argv))

51
modules/adler_test.py Normal file
View File

@ -0,0 +1,51 @@
"""
Fast adler32 checksum implementation.
"""
import os
from modules import test_template
from zlib import adler32
def apply_adler(path):
"""
Apply the adler32 checksum to a file.
:param path: The path to the file.
:type path: str
:return: The adler32 checksum.
:rtype: int
"""
with open(path, "rb") as f:
return hex(adler32(f.read()))
class AdlerTest(test_template.TestWalkTemplate):
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
"""
Simple test runner, compare adler32 to known good values.
"""
super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock)
def run_test(self):
self.set_status("running")
size = len(self.walk())
error_msg = ""
for i, path in enumerate(self.walk()):
self.progress = int(100*i/size)
short_path = self.agnostic_path.get_short_path(path)
if os.path.isfile(path):
if not self.compare(path, "adler32", apply_adler, (), {}):
res = self.get_test_data(path, "adler32")
self.add_failure(path, f"Test {self.name()} failed on {short_path}")
error_msg += f"{short_path} is " \
f"({res}) not ({self.get_expected_result(path, 'adler32')} b).\n"
self.is_infected = True
if not self.args.continue_on_error:
return self.finish("failure", f"{short_path} is supposed to be {self.get_expected_result(path, 'adler32')} bytes, but it is {adler32(open(path, 'rb').read())} bytes.")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
return self.finish("success", "Your discord is not infected.")

View File

@ -19,6 +19,7 @@ class AgnosticPaths:
self.args = args
self.ptb = args.ptb
self.os = self.get_os()
self.default_database = ""
if args.force_path is not None:
self.force_path = args.force_path[0]
else:
@ -39,10 +40,13 @@ class AgnosticPaths:
platform = sys.platform
if platform.startswith('win'):
self.os = 'windows'
self.default_database = 'databases/windows_base.json'
elif platform.startswith('linux'):
self.os = 'linux'
self.default_database = 'databases/linux_base.json'
elif platform.startswith('darwin'):
self.os = 'mac'
self.default_database = 'databases/mac_base.json'
else:
raise OSError("This script can only check for Spideys on Windows, Linux and Mac.")
return self.os
@ -63,13 +67,17 @@ class AgnosticPaths:
if os.path.exists(ptb_path):
self.ptb = "PTB"
self.args.ptb = "PTB"
self.default_database = "databases/windows_ptb.json"
return ptb_path
else:
self.default_database = "databases/windows_base.json"
return base_path
if self.ptb == 'PTB':
self.default_database = "databases/windows_bas.jsone"
return base_path+"PTB"
else:
return base_path+"PTB" # Try to find the PTB version
self.default_database = "databases/windows_ptb.json"
return base_path # Try to find the default version
elif self.os == 'linux':
return os.path.join("/", "opt", "discord")
# return os.path.join("usr","share","discord")
@ -166,7 +174,16 @@ class AgnosticPaths:
return [os.path.join(dirpath, filename)
for dirpath, dirnames, filenames in os.walk(self(*args))
for filename in filenames]
def get_short_path(self, path):
"""
Get the short path of the given path
:param path: The path
:return: The short path
:rtype: str
"""
return os.path.relpath(path, self.main_path)
def __repr__(self):
"""
:return: A representation of the AgnosticPath

View File

@ -24,9 +24,11 @@ def parse(_args):
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("--database", "--db", type=str, 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")
parser.add_argument("--size", "--window-size", default=110, type=int, help="Size of the message window")
parser.add_argument("--max-shown", default=5, type=int, help="Maximum number of errors shown")
# Tests
parser.add_argument('--spidey', action='store_true', help='Runs the spidey test')
@ -43,5 +45,6 @@ def parse(_args):
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")
parser.add_argument("--max-errors", default=5, type=int, help="Maximum number of errors before the analysis is stopped")
return parser.parse_args(_args)

View File

@ -5,22 +5,24 @@ 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
from modules.internal_io import global_status
from modules import size_test, adler_test
"""
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
from termcolor import colored
os.system('color')
# red, green, yellow, blue, magenta, cyan, white.
_TEST_MODULES = {
"spidey": ["spidey_test", "SpideyTest"],
# "test_walk": ["test_template", "TestWalkTemplate"],
"size_test": ["size_test", "SizeTest"],
"adler_test": ["adler_test", "AdlerTest"],
}
PROGRAM_VERSION = "1.0.0"
@ -86,10 +88,43 @@ class TestRunner:
self.dict_of_processes = {} # To get a more granular view of the processes
self.update_module_list()
self.base_data = {}
self.finished_tests = deque()
self.open_base_data()
self.exec_all_tests()
def open_base_data(self):
"""
Opens the base data file
"""
default_database = resource_path(self.agnostic_path.default_database)
if not self.args.database:
self.args.database = default_database
if os.path.exists(self.args.database):
print(colored(f" > Opening specified database {self.args.database}", "green")) # TODO : Only if verbose
with open(resource_path(self.args.database), "r") as f:
self.base_data = json.load(f)
return self.base_data
elif os.path.exists(resource_path(default_database)): # Try opening the default database
print(colored(
"\n > The database file specified does not exist, "
"trying to open the default one for your os...\n", "yellow"
))
with open(resource_path(default_database), "r") as f:
self.base_data = json.load(f)
return self.base_data
else:
print(colored(
"\n > The database file does not exist, and the default "
"one for your os does not exist either.\n", "red"
))
self.base_data = {}
return self.base_data
def update_module_list(self):
"""
Gets the list of modules to be loaded (in accordance to the config)
@ -115,8 +150,6 @@ class TestRunner:
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():
@ -124,7 +157,7 @@ class TestRunner:
tc = test_class(
self.args,
self.agnostic_path,
None,
self.base_data,
self.finished_tests,
queue_lock,
self.dict_of_processes,
@ -152,15 +185,13 @@ class TestRunner:
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")
# print("Controller still up")
try:
if input() == "":
global POST_RUN_ALLOWED, RUN_OVER
@ -356,51 +387,114 @@ def run_check(args, agnpath):
:param agnpath: Annostic path to the discord folder
:return: None
"""
# Initialize the test runner
print(LOGO)
start = time.time()
tr = TestRunner(args, agnpath, start_time=start) # All operation are executed in parallel and the user can stop them
# Poll information from the tests
POLLRATE, PRINT_RATE, COUNTER = args.pollrate, args.printrate, 0
COUNTER_MAX = int(PRINT_RATE / POLLRATE)
# Main test polling loop
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
print_status(tr.get_status(), start)
time.sleep(POLLRATE)
COUNTER += 1
# Print the final status
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
# Final status
print_final_status(tr, start, args)
# Export the data
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)
gen_save_data(args, tr)
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))
# Sleep for a while to let the user see the final status
time.sleep(args.timeout)
sys.exit(tr.get_exit_code())
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
def print_final_status(testrunner, start_time, args):
"""
Prints the final status
"""
status = testrunner.get_status()
if status.tests_failed == 0:
color = "green"
print(CLEAR_CODE)
else:
color = "red"
print(INFECTED_CODE)
print(colored(f"\nThe test sequence is now finished, DiscordHelper ran for {round(time.time() - start_time,2)} seconds", color))
SIZE = args.size
for test, test_object in testrunner.dict_of_processes.items():
if test_object.status_code == "success":
print(colored(f" > {fill_it(SIZE,test,'SUCCESS')}", "green"))
elif test_object.status_code == "failure":
print(colored(f" > {fill_it(SIZE,test,'FAILURE')}", "red"))
error_message = fit_it_under(test_object.status ,SIZE-3, ' > ')
if args.max_shown != -1: # TODO : Correct this abomination
table = error_message.split("\n")
error_message = "\n".join(table[:args.max_shown] + [f" > ... {len(table) - args.max_shown} more lines"])
print(colored(f"{error_message}", "red"))
else:
print(colored(f" > {fill_it(SIZE,test,test_object.status_code.upper())}", "yellow"))
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))
def fill_it(size, string1, string2):
"""
Fills a string with a character
"""
return string1 + max(size - len(string1)-len(string2), 0) * "." + string2
def fit_it_under(message, size, start_line=""):
"""
Fits a message under a size
"""
final_message = ""
for line in message.split('\n'):
if len(line) > size:
final_message += start_line + line[:size] + "\n"
final_message += fit_it_under(line[size:], size, start_line)
else:
final_message += start_line + line + "\n"
# final_message += start_line + line + '\n'
# if len(message) > size:
# return message[:size-3] + "..."
return final_message
def gen_save_data(args, test_runner):
"""
Generates the save data
"""
print(colored("\n > Generating data...", "blue"))
data = test_runner.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"))
main_data = export_data(test_runner, data)
# print(main_data)
with open(file_path, "w") as f:
json.dump(main_data, f, indent=4)

44
modules/size_test.py Normal file
View File

@ -0,0 +1,44 @@
import asyncio
import os
import time
from modules import test_template
class SizeTest(test_template.TestWalkTemplate):
def __init__(self, args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock):
"""
Simple test runner, compare file size to known good values.
"""
super().__init__(args, agnpath, test_data, queue, queue_lock, dict_process, dict_process_lock)
def run_test(self):
"""
Walk all files and check their size.
:return: Whether any of the files are larger than the expected size.
:rtype: bool
"""
self.set_status("running")
size = len(self.walk())
error_msg = ""
for i, path in enumerate(self.walk()):
self.progress = int(100*i/size)
short_path = self.agnostic_path.get_short_path(path)
if os.path.isfile(path):
if not self.compare(path, "size", os.path.getsize, (), {}):
res = self.get_test_data(path, "adler32")
self.add_failure(path, f"Test {self.name()} failed on {short_path}")
error_msg += f"{short_path} is " \
f"({res} b) not ({self.get_expected_result(path, 'size')} b).\n"
self.is_infected = True
if not self.args.continue_on_error:
return self.finish("failure", f"{short_path} is supposed to be {self.get_expected_result(path, 'size')} bytes, but it is {os.path.getsize(path)} bytes.")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
return self.finish("success", "Your discord is not infected.")

View File

@ -40,16 +40,17 @@ class SpideyTest(test_template.TestTemplate):
if lines > 1:
self.is_infected = True
# 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.")
if self.is_infected:
return self.finish(
"failure",
"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."
)
self.progress = 100
return self.finish("success")
return self.finish("success", "Your discord is not infected.")
def get_path(self):
"""

View File

@ -21,7 +21,7 @@ class TestTemplate:
: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
:type test_data: dict
: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
@ -51,6 +51,7 @@ class TestTemplate:
self.dict_process_lock.acquire()
self.dict_process[self.name()] = self
self.dict_process_lock.release()
self.init_test_data()
def run_test(self):
"""
@ -60,6 +61,11 @@ class TestTemplate:
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 init_test_data(self):
if self.test_data is None and not self.args.continue_on_error:
self.set_status("problem", "No test data given.")
return self.finish()
def name(self):
"""
:return: Name of the test
@ -159,8 +165,8 @@ class TestWalkTemplate(TestTemplate):
: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.")
# if test_data is None:
# self.set_status("problem", "No test data given.")
def add_test_result(self, path, testname, result):
"""
@ -173,6 +179,7 @@ class TestWalkTemplate(TestTemplate):
:type result: any
"""
path = self.agnostic_path.get_short_path(path)
if path not in self.new_data:
self.new_data[path] = {}
self.new_data[path][testname] = result
@ -188,6 +195,7 @@ class TestWalkTemplate(TestTemplate):
:return: Test data
:rtype: any
"""
path = self.agnostic_path.get_short_path(path)
if path in self.new_data:
if testname in self.new_data[path]:
return self.new_data[path][testname]
@ -204,9 +212,11 @@ class TestWalkTemplate(TestTemplate):
: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]
path = self.agnostic_path.get_short_path(path)
if "tests" in self.test_data:
if path in self.test_data["tests"]:
if testname in self.test_data["tests"][path]:
return self.test_data["tests"][path][testname]
return None
def run_test(self):
@ -217,13 +227,7 @@ class TestWalkTemplate(TestTemplate):
"""
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:
@ -245,7 +249,7 @@ class TestWalkTemplate(TestTemplate):
"""
return self.agnostic_path.walk_all_files(self.agnostic_path())
def compare(self, path, entry_name, function, args, kwargs):
def compare(self, path, entry_name, function, args, kwargs, cmp_function=None):
"""
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
@ -258,6 +262,8 @@ class TestWalkTemplate(TestTemplate):
:type args: list
:param kwargs: Keyword arguments to use on the function
:type kwargs: dict
:param cmp_function: Function compare both results
:type cmp_function: function
"""
data_origin = self.add_test_result(path, entry_name, function(path, *args, **kwargs))
@ -266,7 +272,12 @@ class TestWalkTemplate(TestTemplate):
self.set_status("problem", f"No expected data found for {path}")
return False
if data_origin != expected_data:
if cmp_function is None:
if data_origin == expected_data:
return True
return False
else:
if cmp_function(data_origin, expected_data):
return True
return False
return True