Merge pull request #30 from idle-org/features/test_centralisation

Features/test centralisation
This commit is contained in:
Maximilien Rigaut 2022-06-27 04:04:33 +02:00 committed by GitHub
commit bf09d81afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 17863 additions and 11922 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,7 @@ import discordHelper
import time
# All modules must be loaded before the main program is run.
from modules import spidey_test, test_template # noqa # Set for pyinstaller
from modules import test_size, test_adler, test_sha, test_spidey, test_line_count # noqa # Set for pyinstaller
if __name__ == "__main__":
try:
@ -26,7 +26,7 @@ if __name__ == "__main__":
# Will always try to load discordHelper/discordHelper.json
run_db = os.path.join(directory_path, "test_results_found.json")
argv = rf"--all --autodetect --launch --continue --timeout 25 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ") # noqa E501
argv = rf"--all --autodetect --printrate 2 --launch --continue --timeout 25 {db_path}--gen-data {run_db} --db discordHelper/test_results.json".split(" ") # noqa E501
# print(" > Starting the discordHelper with the following arguments:")
# print(" > " + " ".join(argv))

0
modules/__init__.py Normal file
View File

View File

@ -1,70 +0,0 @@
"""
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.TestWalkTemplateNoLogs):
def __init__(self, thread_parameters):
"""
Simple test runner, compare adler32 to known good values.
"""
super().__init__(thread_parameters)
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"
if not self.bad_database:
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')} "
f"bytes, but it is {adler32(open(path, 'rb').read())} bytes."
)
else:
self.is_unknown = True
if not self.args.continue_on_error:
return self.finish(
"problem",
f"{short_path} is supposed to be {self.get_expected_result(path, 'adler32')} "
f"bytes, but it is {adler32(open(path, 'rb').read())} bytes."
)
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
elif self.is_unknown:
return self.finish(
"problem",
error_msg
)
return self.finish("success", "Your discord is not infected.")

View File

@ -27,16 +27,17 @@ def parse(_args):
parser.add_argument("--gen-data", "--generate-data", "--gen", nargs=1, default="", help="Generate data 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("--printrate", "--refresh", default=5, type=float, 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')
parser.add_argument('--adler', "--adler32", action='store_true', help='Runs the adler32 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")
parser.add_argument('--file-hash', action='store_true', help="Test the hash of the files in the discord folder")
parser.add_argument('--file-hash', '--sha', action='store_true', help="Test the hash of the files in the discord folder")
parser.add_argument('--file-path', action='store_true', help="Test the path of the files in the discord folder")
parser.add_argument('--file-name', action='store_true', help="Test the name of the files in the discord folder")
parser.add_argument('--file-extension', action='store_true', help="Test the extension of the files in the discord folder")
@ -47,7 +48,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

@ -8,7 +8,6 @@ import time
from collections import deque
from modules.internal_io import global_status, thread_parameters
from modules import size_test, adler_test # noqa # Used to force import of modules by pyinstaller
"""
All modules that can be tested
@ -20,10 +19,11 @@ 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"],
"spidey": ["test_spidey", "SpideyTest"],
"file_size": ["test_size", "SizeCheck"],
"adler": ["test_adler", "Adler32"],
"file_hash": ["test_sha", "Sha256"],
"line_count": ["test_line_count", "LineCount"],
}
PROGRAM_VERSION = "1.0.0"
@ -87,9 +87,10 @@ class TestRunner:
self.main_path = agnpath.main_path
self.version = agnpath.version
self.start_time = start_time
self.selected_tests = []
self.is_infected = 0 # Only set to 1 if the certainty is high enough
self.is_unknown = 0 # Unknown if the file is infected or not
self.is_unknown = 1 # Unknown if the file is infected or not
self.threat_level = 0 # The threat level of the infection, 0 is the lowest, can only be increased
self.errors = 0
self.bad_database = False # Set to true if the database is not valid
@ -149,7 +150,7 @@ class TestRunner:
self.base_data = {}
return self.base_data
def check_database(self):
def check_database(self): # TODO: All of this can be refactored with a function
"""
Check if the database is valid set self.bad_database to True if it is not
"""
@ -194,6 +195,16 @@ class TestRunner:
if "tests" not in self.base_data:
print(colored(" > The database is missing the test results", "red"))
self.bad_database = True
else:
for test_name in self.selected_tests:
found_one_value = False
for file_name, test_names in self.base_data["tests"].items():
if test_name in test_names:
found_one_value = True
break # At least one test is in the database
if not found_one_value:
print(colored(f" > The database is missing the test {test_name}", "red"))
self.bad_database = True
def update_module_list(self):
"""
@ -209,6 +220,7 @@ class TestRunner:
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])
self.selected_tests.append(values[1])
except AttributeError as e:
print(f"The module {module_name} was not loaded because it was not specified in the config.")
@ -361,7 +373,10 @@ class TestRunner:
for test_name, test in self.dict_of_processes.items():
progress += test.progress
progress = int(progress / nb_tests)
if nb_tests == 0:
progress = 100
else:
progress = int(progress / nb_tests)
nb_tests_ran = nb_tests_success + nb_tests_failure + nb_tests_skipped + nb_tests_error
nb_test_running = nb_tests - nb_tests_ran
@ -446,11 +461,12 @@ def print_status(status, timer_start):
else:
mon, sec = divmod(ellapsed_time * 100 / status.progress, 60)
hr, mon = divmod(mon, 60)
estimated_time = "%d:%02d:%02d" % (hr, mon, sec)
sec = round(sec, 0) +1
estimated_time = f"{hr:n}:{mon:02n}:{sec:02.0n}"
print(colored(
f" > Overall progress: {status.progress}%"
f" in {time.time() - timer_start:.2f} seconds"
f" estimaded time ({estimated_time})", color
f" estimaded total time ({estimated_time})", color
))
@ -508,7 +524,7 @@ def print_final_status(testrunner, start_time, args):
Prints the final status
"""
status = testrunner.get_status()
if status.tests_failed == 0 and status.tests_error == 0:
if status.tests_failed == 0 and status.tests_error == 0 and status.tests_total != 0:
color = "green"
print(CLEAR_CODE)
elif status.tests_failed > 0:

View File

@ -1,60 +0,0 @@
import os
from modules import test_template
class SizeTest(test_template.TestWalkTemplateNoLogs):
def __init__(self, thread_parameters):
"""
Simple test runner, compare file size to known good values.
"""
super().__init__(thread_parameters)
self.is_unknown = False
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 = self.agnostic_path.size
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"
# print(self.bad_database)
if not self.bad_database:
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')}"
f" bytes, but it is {os.path.getsize(path)} bytes.")
else:
self.is_unknown = True
if not self.args.continue_on_error:
return self.finish(
"problem",
f"{short_path} is supposed to be {self.get_expected_result(path, 'size')}"
f" bytes, but it is {os.path.getsize(path)} bytes.")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
elif self.is_unknown:
return self.finish(
"problem",
error_msg
)
return self.finish("success", "Your discord is not infected.")

26
modules/test_adler.py Normal file
View File

@ -0,0 +1,26 @@
"""
Fast adler32 checksum implementation.
"""
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 Adler32(test_template.TestWalkTemplateSimpleFunction):
def __init__(self, thread_parameters):
"""
Simple test runner, compare adler32 to known good values.
"""
super().__init__(thread_parameters, apply_adler, "")

View File

@ -0,0 +1,13 @@
"""
Test against the line count of the files in the discord folder.
"""
from modules import test_template
class LineCount(test_template.TestWalkTemplateSimpleFunction):
def __init__(self, thread_parameters):
"""
Simple test runner, compare line count to known good values.
"""
super().__init__(thread_parameters, lambda path: len(open(path, "rb").readlines()), " lines")

25
modules/test_sha.py Normal file
View File

@ -0,0 +1,25 @@
"""
Sha256 hash function.
"""
from hashlib import sha256
from modules import test_template
def sha256_hash(path):
"""
Returns the sha256 hash of the data.
"""
with open(path, "rb") as f:
return sha256(f.read()).hexdigest()
class Sha256(test_template.TestWalkTemplateSimpleFunction):
"""
Sha256 hash function.
"""
def __init__(self, thread_parameters):
"""
Sha256 hash function.
"""
super().__init__(thread_parameters, sha256_hash, "")

11
modules/test_size.py Normal file
View File

@ -0,0 +1,11 @@
import os
from modules import test_template
class SizeCheck(test_template.TestWalkTemplateSimpleFunction):
def __init__(self, thread_parameters):
"""
Simple test runner, compare file size to known good values.
"""
super().__init__(thread_parameters, os.path.getsize, " bytes")

View File

@ -48,7 +48,7 @@ class SpideyTest(test_template.TestTemplate):
"If you've recently downloaded a .exe directly sent from someone, "
"do also consider a clean windows reinstall."
)
self.add_to_new_data("global_tests", "Spidey", "passed")
self.progress = 100
return self.finish("success", "Your discord is not infected.")

View File

@ -1,6 +1,7 @@
"""
A global template for all tests.
"""
import os
from modules import internal_io
@ -166,6 +167,54 @@ class TestTemplate:
if self.status_code == "running":
return self.finish("skipped")
def add_to_new_data(self, entry, testname, value):
"""
Adds a new entry to the database.
:param entry: The entry to add
:type entry: str
:param testname: The name of the test
:type testname: str
:param value: The value of the entry
:type value: str
"""
if entry not in self.new_data:
self.new_data[entry] = {}
self.new_data[entry][testname] = value
return value
def get_from_new_data(self, entry, testname):
"""
Gets the value of an entry from the database.
:param entry: The entry to get
:type entry: str
:param testname: The name of the test
:type testname: str
:return: The value of the entry
:rtype: str
"""
if entry in self.new_data:
if testname in self.new_data[entry]:
return self.new_data[entry][testname]
return None
def get_from_database(self, entry, testname):
"""
Gets the value of an entry from the database.
:param entry: The entry to get
:type entry: str
:param testname: The name of the test
:type testname: str
:return: The value of the entry
:rtype: str
"""
if self.test_data:
path = self.agnostic_path.get_short_path(entry)
if "tests" in self.test_data:
if entry in self.test_data["tests"]:
if testname in self.test_data["tests"][entry]:
return self.test_data["tests"][entry][testname]
return None
class TestWalkTemplate(TestTemplate):
def __init__(self, thread_parameters):
@ -175,57 +224,6 @@ class TestWalkTemplate(TestTemplate):
super().__init__(thread_parameters)
self.to_skip = []
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
"""
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
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
"""
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]
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:
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):
"""
The main runner function, must be implemented by the child class.
@ -255,10 +253,37 @@ class TestWalkTemplate(TestTemplate):
"""
Walks the given path and returns a list of all files and directories.
:return: List of all files
:return:
"""
return self.agnostic_path.all_files
def add_test_result(self, path: str, testname: str, result: any):
"""
Adds a test result to the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
:param result: Result of the test
"""
path = self.agnostic_path.get_short_path(path)
return self.add_to_new_data(path, testname, result)
def get_test_data(self, path, testname):
"""
Gets the test data from the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
"""
return self.get_from_new_data(self.agnostic_path.get_short_path(path), testname)
def get_expected_result(self, path, testname):
"""
Gets the expected result from the data dictionary.
:param path: Path to the file being tested
:param testname: Name of the test
"""
if self.test_data:
return self.get_from_database(self.agnostic_path.get_short_path(path), testname)
return None
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.
@ -303,3 +328,70 @@ class TestWalkTemplateNoLogs(TestWalkTemplate):
"""
super().__init__(thread_parameters)
self.to_skip = [r"modules\discord_dispatch-1\discord_dispatch\dispatch.log"]
class TestWalkTemplateSimpleFunction(TestWalkTemplateNoLogs):
def __init__(self, thread_parameters, function, unit_test):
"""
A test template that runs a simple function on every file, and compares the result with the expected data.
:param thread_parameters: Thread parameters
:type thread_parameters: dict
:param function: Function to use on every file
:type function: function
:param unit_test: A descriptor of what the test returns
:type unit_test: str
"""
super().__init__(thread_parameters)
self.function = function
self.test_name = self.name()
self.unit_test = unit_test
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 = self.agnostic_path.size
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, self.test_name, self.function, (), {}):
res = self.get_test_data(path, self.test_name)
self.add_failure(path, f"Test {self.name()} failed on {short_path}")
error_msg += f"{short_path} is " \
f"({res}{self.unit_test}) not ({self.get_expected_result(path, self.test_name)}" \
f"{self.unit_test}).\n"
# print(self.bad_database)
if not self.bad_database:
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, self.test_name)}"
f" {self.unit_test}, but it is {self.get_expected_result(path, self.test_name)}"
f"{self.unit_test}.")
else:
self.is_unknown = True
if not self.args.continue_on_error:
return self.finish(
"problem",
f"{short_path} is supposed to be {self.get_expected_result(path, self.test_name)}"
f"{self.unit_test}, but it is {os.path.getsize(path)}{self.unit_test}")
self.progress = 100
if self.is_infected:
return self.finish(
"failure",
error_msg
)
elif self.is_unknown:
return self.finish(
"problem",
error_msg
)
return self.finish("success", "Your discord is not infected.")