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

Features/win release
This commit is contained in:
Maximilien Rigaut 2022-06-24 04:38:43 +02:00 committed by GitHub
commit c1aab19fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 4626 additions and 77 deletions

1
.gitignore vendored
View File

@ -131,3 +131,4 @@ dmypy.json
/.idea/*
databases/*

View File

@ -1,19 +0,0 @@
global:
name: Windows Base
version: 1.0
files:
/etc/passwd:
sha256: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"
type: file
size: "1024"
permissions: "0644"
owner: "root"
group: "root"
date: "2018-01-01T00:00:00Z"
executable: "true"
symlink: "false"
md5: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"
sha1: "6c3d65f8b8b8f5a8f62b60c0e15d5d084d3b20f470f5d8b3d26bfb3c350d37a3"

4399
databases/windows_base.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,7 @@
""" Main program """
from modules import test_interface, agnostic_paths, argument_parser
from modules import main_runner, agnostic_paths, argument_parser
import sys
@ -13,7 +13,7 @@ def main(argm):
:type argm: argparse.Namespace
"""
agnostic_path = agnostic_paths.AgnosticPaths(argm.ptb, argm.force_path)
test_runner = test_interface.run_check(argm, agnostic_path)
test_runner = main_runner.run_check(argm, agnostic_path)
if __name__ == "__main__":

View File

@ -1,10 +1,11 @@
"""
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
@ -12,8 +13,9 @@ from modules import spidey_test, test_template
if __name__ == "__main__":
argv = sys.argv[1:]
if len(argv) <= 1:
argv += "--spidey --launch --test-walk --continue --gen-data new.yaml".split(" ")
if len(argv) == 0:
user_path = os.path.join(os.path.expanduser("~"), "Documents", "DiscordHelper")
argv = f"--all --launch --continue --timeout 12 --gen-data {user_path}/new_test_data_{int(time.time())}.yaml".split(" ")
args = discordHelper.argument_parser.parse(argv)
discordHelper.main(args)

View File

@ -58,7 +58,13 @@ class AgnosticPaths:
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")
if self.ptb == 'PTB':
return base_path+"PTB"
else:
if os.path.exists(base_path):
return base_path
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")

View File

@ -6,10 +6,26 @@ def parse(_args):
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')
# 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")
# 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")
@ -21,14 +37,8 @@ def parse(_args):
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=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", 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("--continue_on_error", "--continue", action="store_true", help="Continue the analysis in case of failure")
parser.add_argument("--test-walk", action="store_true", help="Test walk function")
return parser.parse_args(_args)

View File

@ -1,3 +1,5 @@
import json
import os
import sys
import importlib
import threading
@ -12,11 +14,36 @@ argparse argument : [module_name, class_name]
"""
_TEST_MODULES = {
"spidey": ["spidey_test", "SpideyTest"],
"test_walk": ["test_template", "TestWalkTemplate"],
#"test_walk": ["test_template", "TestWalkTemplate"],
}
PROGRAM_VERSION = "1.0.0"
POST_RUN_ALLOWED = False
queue_lock = threading.Lock()
CLEAR_CODE = r"""
_______ _
(_______) |
_ | | _____ _____ ____
| | | || ___ (____ |/ ___)
| |_____| || ____/ ___ | |
\______)\_)_____)_____|_|
"""
INFECTED_CODE = r"""
_ ___ _
| | / __) _ | |
| |____ _| |__ _____ ____ _| |_ _____ __| |
| | _ (_ __) ___ |/ ___|_ _) ___ |/ _ |
| | | | || | | ____( (___ | |_| ____( (_| |
|_|_| |_||_| |_____)\____) \__)_____)\____|
"""
if os.name == "nt":
CLEAR_CODE = r"" + CLEAR_CODE + r""
INFECTED_CODE = r"" + INFECTED_CODE + r""
class TestRunner:
def __init__(self, args, agnpath):
@ -58,7 +85,7 @@ class TestRunner:
module_name = None
try:
for module_name, values in _TEST_MODULES.items():
if self.args.__getattribute__(module_name):
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])
@ -77,14 +104,13 @@ class TestRunner:
"""
self.list_of_tests = []
for module_name, test_class in self.loaded_test_classes.items():
for i in range(10):
print(f"Starting test {module_name}")
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests, queue_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()
print(f"Starting test {module_name}")
tc = test_class(self.args, self.agnostic_path, None, self.finished_tests, queue_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)
@ -200,11 +226,37 @@ class TestRunner:
data = {}
queue_lock.acquire()
for test in self.finished_tests:
data[test.name] = test.data
for key, value in test.data.items():
if key not in data:
data[key] = value
else:
data[key].update(value)
# data.update(test.data)
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 run_check(args, agnpath):
"""
Runs all operations selected in the config
@ -220,9 +272,29 @@ def run_check(args, agnpath):
time.sleep(0.5)
print("The test sequence is now finished")
print(tr.get_status()) # TODO: Print the final status, test still running are actually skipped
status = tr.get_status()
print(status) # TODO: Print the final status, test still running are actually skipped
if status.tests_failed > 0:
print(INFECTED_CODE)
tr.is_infected = True
else:
print(CLEAR_CODE)
tr.is_infected = False
if args.gen_data:
print("Generating data...")
# print(tr.get_test_data()) # TODO: Transorm the data into a yaml or json file
time.sleep(2)
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("Making folder...")
os.makedirs(args.gen_data)
print("Exporting data...") # 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)
print(f"Exiting in {args.timeout} seconds...")
time.sleep(args.timeout)
sys.exit(tr.get_exit_code())

View File

@ -10,15 +10,6 @@ class SpideyTest(test_template.TestTemplate):
"""
super().__init__(args, agnpath, test_data, queue, queue_lock)
def slowdown(self):
"""
This function is only used for testing.
:return:
"""
print("Spidey test is running slowly...")
import random, time
time.sleep(random.randint(1, 5))
def run_test(self):
"""
Counts the number of lines in both index.js files. If it's more than one, it's almost certain your discord
@ -27,7 +18,7 @@ class SpideyTest(test_template.TestTemplate):
:rtype: bool
"""
self.set_status("running")
self.slowdown() # This function is only used for testing
# self.slowdown() # This function is only used for testing
paths = self.get_path()
self.is_infected = False
lines = 0

View File

@ -40,7 +40,7 @@ class TestTemplate:
self.set_status("idle")
self.queue_lock = queue_lock
self.failure_dict = {}
self.data = {}
self.new_data = {}
def run_test(self):
"""
@ -69,7 +69,7 @@ class TestTemplate:
:return: Status at the time of call.
:rtype: internal_io.test_status
"""
return internal_io.test_status(self.name(), self.status_code, self.status, self.failure_dict, self.data)
return internal_io.test_status(self.name(), self.status_code, self.status, self.failure_dict, self.new_data)
@staticmethod
def get_status_from_code(code):
@ -141,11 +141,62 @@ class TestWalkTemplate(TestTemplate):
:type test_data: str
"""
super().__init__(args, agnpath, test_data, queue, queue_lock)
self.data = {}
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:
@ -154,6 +205,7 @@ class TestWalkTemplate(TestTemplate):
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:
@ -190,22 +242,13 @@ class TestWalkTemplate(TestTemplate):
:type kwargs: dict
"""
try:
self.data[path] = function(path, *args, **kwargs)
if not self.test_data:
raise TestDataError("No test data given.")
if path not in self.test_data["files"]:
raise TestDataError("No entry in test data for path: " + str(path))
if entry_name not in self.test_data["files"][path]:
raise TestDataError("No entry in test data for path: " + str(path) + " and entry name: " + str(entry_name))
data_origin = self.test_data["files"][path][entry_name]
self.data[path] = function(path, *args, **kwargs)
if data_origin != self.data[path]:
return False
return True
except TestDataError as e:
if not self.args.continue_on_error:
self.set_status("error", "Error: " + str(e))
raise e
finally:
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

44
winspec.spec Normal file
View File

@ -0,0 +1,44 @@
# -*- 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,
)