Skip to content

Commit 877d03d

Browse files
Fixed _debug.py
Made it faster and cleaner, removed weird complexity issues, re-added the file check to the health check class, and fixed some minor bugs, fixed also _dev.py where now it gets all files and not code files for the health check, fixed many bugs related to this, and made the Get.list_of_files have a param for extensions to be optional, which is all on default Signed-off-by: Shahm Najeeb <[email protected]>
1 parent 4631dcb commit 877d03d

File tree

5 files changed

+178
-121
lines changed

5 files changed

+178
-121
lines changed

CODE/Logicytics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def generate_execution_list() -> list | list[str] | list[str | Any]:
210210
Returns:
211211
list: The execution list of scripts to be executed.
212212
"""
213-
execution_list = Get.list_of_files(".")
213+
execution_list = Get.list_of_files(".", extensions=(".py", ".exe", ".ps1", ".bat"))
214214
execution_list.remove("sensitive_data_miner.py")
215215
execution_list.remove("dir_list.py")
216216
execution_list.remove("tree.ps1")
@@ -239,7 +239,9 @@ def generate_execution_list() -> list | list[str] | list[str | Any]:
239239

240240
if ACTION == "modded":
241241
# Add all files in MODS to execution list
242-
execution_list = Get.list_of_files("../MODS", execution_list)
242+
execution_list = Get.list_of_files("../MODS",
243+
extensions=(".py", ".exe", ".ps1", ".bat"),
244+
append_file_list=execution_list)
243245

244246
if ACTION == "depth":
245247
log.warning("This flag will use clunky and huge scripts, and so may take a long time, but reap great rewards.")

CODE/_debug.py

Lines changed: 150 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import configparser
4-
import os.path
4+
import os
55
import platform
66
import subprocess
77
import sys
@@ -17,197 +17,237 @@
1717

1818

1919
class HealthCheck:
20-
@log_debug.function
21-
def get_online_config(self) -> bool | tuple[str, str, str]:
20+
@staticmethod
21+
def check_files(directory: str, required_files: list[str]) -> tuple[str, str]:
22+
"""
23+
Checks if all required files are present in the directory and its subdirectories.
24+
25+
Args:
26+
directory (str): Path to the directory to check.
27+
required_files (list[str]): List of required file names with relative paths.
28+
29+
Returns:
30+
tuple[str, str]: Status message and severity level.
31+
"""
32+
try:
33+
log_debug.debug(f"Checking directory: {directory}")
34+
if not os.path.exists(directory):
35+
log_debug.error(f"Directory {directory} does not exist.")
36+
return f"Directory {directory} does not exist.", "ERROR"
37+
38+
# Gather all files with relative paths
39+
actual_files = []
40+
for root, _, files in os.walk(directory):
41+
for file in files:
42+
relative_path = os.path.relpath(os.path.join(root, file), start=directory)
43+
actual_files.append(
44+
relative_path.replace("\\", "/").replace('"', '')) # Normalize paths for comparison
45+
46+
log_debug.debug(f"Actual files found: {actual_files}")
47+
48+
# Track missing and extra files
49+
missing_files = []
50+
extra_files = []
51+
52+
# Normalize required files
53+
normalized_required_files = [required_file.strip().replace("\\", "/").replace('"', '') for required_file in
54+
required_files]
55+
56+
# Check for missing files
57+
for required_file in normalized_required_files:
58+
if required_file not in actual_files:
59+
missing_files.append(required_file)
60+
61+
log_debug.debug(f"Missing files: {missing_files}")
62+
63+
# Check for extra files
64+
for actual_file in actual_files:
65+
if actual_file not in normalized_required_files:
66+
extra_files.append(actual_file)
67+
68+
log_debug.debug(f"Extra files: {extra_files}")
69+
70+
if missing_files:
71+
return f"Missing files: {', '.join(missing_files)}", "ERROR"
72+
if extra_files:
73+
return f"Extra files found: {', '.join(extra_files)}", "WARNING"
74+
return "All required files are present.", "INFO"
75+
76+
except Exception as e:
77+
log_debug.error(f"Unexpected error during file check: {e}")
78+
return f"Unexpected error during file check: {e}", "ERROR"
79+
80+
@staticmethod
81+
def get_online_config() -> dict | None:
2282
"""
23-
Retrieves configuration data from a remote repository and compares it with the local configuration.
83+
Retrieves configuration data from a remote repository.
2484
2585
Returns:
26-
bool: False if a connection error occurs, otherwise a tuple containing version check and file check results.
27-
tuple[tuple[str, str, str], tuple[str, str, str]]: A tuple containing version check and file check results.
86+
dict: Parsed configuration data if successful.
87+
None: If there was an error fetching the configuration.
2888
"""
2989
try:
3090
url = "https://raw.githubusercontent.com/DefinetlyNotAI/Logicytics/main/CODE/config.ini"
3191
config = configparser.ConfigParser()
3292
config.read_string(requests.get(url, timeout=15).text)
33-
except requests.exceptions.ConnectionError:
34-
log_debug.warning("No connection found")
35-
return False
36-
version_check = self.__compare_versions(VERSION, config["System Settings"]["version"])
37-
38-
return version_check
93+
return config
94+
except requests.exceptions.RequestException as e:
95+
log_debug.warning(f"Connection error: {e}")
96+
return None
3997

4098
@staticmethod
41-
def __compare_versions(
42-
local_version: str, remote_version: str
43-
) -> tuple[str, str, str]:
99+
def compare_versions(local_version: str, remote_version: str) -> tuple[str, str, str]:
44100
"""
45-
Compares the local version with the remote version and returns a tuple containing a comparison result message,
46-
a version information message, and a severity level.
101+
Compares local and remote versions.
47102
48103
Args:
49-
local_version (str): The version number of the local system.
50-
remote_version (str): The version number of the remote repository.
104+
local_version (str): Local version.
105+
remote_version (str): Remote version.
51106
52107
Returns:
53-
tuple[str, str, str]: A tuple containing a comparison result message, a version information message, and a severity level.
108+
tuple[str, str, str]: Comparison result, version details, and severity level.
54109
"""
55110
if local_version == remote_version:
56111
return "Version is up to date.", f"Your Version: {local_version}", "INFO"
57-
elif local_version > remote_version:
112+
if local_version > remote_version:
58113
return (
59114
"Version is ahead of the repository.",
60115
f"Your Version: {local_version}, Repository Version: {remote_version}",
61116
"WARNING",
62117
)
63-
else:
64-
return (
65-
"Version is behind the repository.",
66-
f"Your Version: {local_version}, Repository Version: {remote_version}",
67-
"ERROR",
68-
)
118+
return (
119+
"Version is behind the repository.",
120+
f"Your Version: {local_version}, Repository Version: {remote_version}",
121+
"ERROR",
122+
)
69123

70124

71125
class DebugCheck:
72126
@staticmethod
73-
@log_debug.function
74127
def sys_internal_binaries(path: str) -> tuple[str, str]:
75128
"""
76-
Checks the contents of the given path and determines the status of the SysInternal Binaries.
129+
Checks the SysInternal Binaries in the given directory.
77130
78131
Args:
79-
path (str): The path to the directory containing the SysInternal Binaries.
132+
path (str): Directory path.
80133
81134
Returns:
82-
tuple[str, str]: A tuple containing a status message and a severity level.
83-
The status message indicates the result of the check.
84-
The severity level is either "INFO", "WARNING", or "ERROR".
85-
86-
Raises:
87-
FileNotFoundError: If the given path does not exist.
88-
Exception: If an unexpected error occurs during the check.
135+
tuple[str, str]: Status message and severity level.
89136
"""
90137
try:
138+
if not os.path.exists(path):
139+
raise FileNotFoundError("Directory does not exist")
140+
91141
contents = os.listdir(path)
92142
log_debug.debug(str(contents))
143+
144+
has_zip = any(file.endswith(".zip") for file in contents)
145+
has_exe = any(file.endswith(".exe") for file in contents)
146+
93147
if any(file.endswith(".ignore") for file in contents):
94148
return "A `.sys.ignore` file was found - Ignoring", "WARNING"
95-
if any(file.endswith(".zip") for file in contents) and not any(
96-
file.endswith(".exe") for file in contents
97-
):
98-
return "Only zip files - Missing EXE's due to no `ignore` file", "ERROR"
99-
elif any(file.endswith(".zip") for file in contents) and any(
100-
file.endswith(".exe") for file in contents
101-
):
149+
if has_zip and not has_exe:
150+
return "Only zip files - Missing EXEs due to no `ignore` file", "ERROR"
151+
if has_zip and has_exe:
102152
return "Both zip and exe files - All good", "INFO"
103-
else:
104-
return (
105-
"SysInternal Binaries Not Found: Missing Files - Corruption detected",
106-
"ERROR",
107-
)
108-
except FileNotFoundError:
109-
return (
110-
"SysInternal Binaries Not Found: Missing Directory- Corruption detected",
111-
"ERROR",
112-
)
153+
154+
return "SysInternal Binaries Not Found: Missing Files - Corruption detected", "ERROR"
113155
except Exception as e:
114-
return f"An Unexpected error occurred: {e}", "ERROR"
156+
return f"Unexpected error: {e}", "ERROR"
115157

116158
@staticmethod
117-
@log_debug.function
118159
def execution_policy() -> bool:
119160
"""
120-
Checks the current PowerShell execution policy.
161+
Checks if the execution policy is unrestricted.
121162
122163
Returns:
123-
bool: True if the execution policy is unrestricted, False otherwise.
164+
bool: True if unrestricted, False otherwise.
124165
"""
125-
result = subprocess.run(
126-
["powershell", "-Command", "Get-ExecutionPolicy"],
127-
capture_output=True,
128-
text=True,
129-
)
130-
return result.stdout.strip().lower() == "unrestricted"
166+
try:
167+
result = subprocess.run(
168+
["powershell", "-Command", "Get-ExecutionPolicy"],
169+
capture_output=True,
170+
text=True,
171+
)
172+
return result.stdout.strip().lower() == "unrestricted"
173+
except Exception as e:
174+
log_debug.error(f"Failed to check execution policy: {e}")
175+
return False
131176

132177
@staticmethod
133-
@log_debug.function
134178
def cpu_info() -> tuple[str, str, str]:
135179
"""
136-
Retrieves information about the CPU.
180+
Retrieves CPU details.
137181
138182
Returns:
139-
tuple[str, str, str]: A tuple containing the CPU architecture, vendor ID, and model.
183+
tuple[str, str, str]: Architecture, vendor ID, and model.
140184
"""
141185
return (
142-
"CPU Architecture: " + platform.machine(),
143-
"CPU Vendor ID: " + platform.system(),
144-
"CPU Model: " + f"{platform.release()} {platform.version()}",
186+
f"CPU Architecture: {platform.machine()}",
187+
f"CPU Vendor ID: {platform.system()}",
188+
f"CPU Model: {platform.release()} {platform.version()}",
145189
)
146190

147191

148-
@log_debug.function
149192
def debug():
150193
"""
151-
Performs a series of system checks and logs the results.
194+
Executes system checks and logs results.
152195
"""
153196
# Clear Debug Log
154197
log_path = "../ACCESS/LOGS/DEBUG/DEBUG.LOG"
155198
if os.path.exists(log_path):
156199
os.remove(log_path)
157200

158-
# Check File integrity (Online)
159-
online_config = HealthCheck().get_online_config()
160-
if online_config:
161-
version_tuple = online_config
162-
log_debug.string(version_tuple[0], version_tuple[2])
163-
log_debug.raw(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] > DATA: | {version_tuple[1] + ' ' * (153 - len(version_tuple[1])) + '|'}")
201+
# Online Configuration Check
202+
config = HealthCheck.get_online_config()
203+
if config:
204+
version_check = HealthCheck.compare_versions(VERSION, config["System Settings"]["version"])
205+
log_debug.string(version_check[0], version_check[2])
206+
log_debug.raw(f"[{datetime.now():%Y-%m-%d %H:%M:%S}] > DATA: {version_check[1]}")
164207

165-
# Check SysInternal Binaries
166-
message, type = DebugCheck.sys_internal_binaries("SysInternal_Suite")
167-
log_debug.string(message, type)
208+
# File Integrity Check
209+
required_files = config["System Settings"].get("files", "").split(",")
210+
message, severity = HealthCheck.check_files(".", required_files)
211+
log_debug.string(message, severity)
168212

169-
# Check Admin
170-
log_debug.info("Admin privileges found" if Check.admin() else "Admin privileges not found")
213+
# SysInternal Binaries Check
214+
message, severity = DebugCheck.sys_internal_binaries("SysInternal_Suite")
215+
log_debug.string(message, severity)
171216

172-
# Check UAC
217+
# System Checks
218+
log_debug.info("Admin privileges found" if Check.admin() else "Admin privileges not found")
173219
log_debug.info("UAC enabled" if Check.uac() else "UAC disabled")
174-
175-
# Log Execution Paths
176220
log_debug.info(f"Execution path: {psutil.__file__}")
177221
log_debug.info(f"Global execution path: {sys.executable}")
178222
log_debug.info(f"Local execution path: {sys.prefix}")
223+
log_debug.info(
224+
"Running in a virtual environment" if sys.prefix != sys.base_prefix else "Not running in a virtual environment")
179225

180-
# Check if running in a virtual environment
181-
log_debug.info("Running in a virtual environment" if sys.prefix != sys.base_prefix else "Not running in a virtual environment")
182-
183-
# Check Execution Policy
184-
log_debug.info("Execution policy is unrestricted" if DebugCheck.execution_policy() else "Execution policy is not unrestricted")
226+
# Execution Policy Check
227+
log_debug.info(
228+
"Execution policy is unrestricted" if DebugCheck.execution_policy() else "Execution policy is not unrestricted")
185229

186-
# Get Python Version
230+
# Python Version Check
231+
python_version = sys.version.split()[0]
187232
try:
188-
major, minor = map(int, sys.version.split()[0].split(".")[:2])
189-
if major == 3 and minor == 11:
190-
log_debug.info(f"Python Version Used: {sys.version.split()[0]} - Perfect")
233+
major, minor = map(int, python_version.split(".")[:2])
234+
if (major, minor) == (3, 11):
235+
log_debug.info(f"Python Version: {python_version} - Perfect")
191236
elif major == 3:
192-
log_debug.warning(f"Python Version Used: {sys.version.split()[0]} - Recommended Version is: 3.11.X")
237+
log_debug.warning(f"Python Version: {python_version} - Recommended: 3.11.x")
193238
else:
194-
log_debug.error(f"Python Version Used: {sys.version.split()[0]} - Incompatible Version")
239+
log_debug.error(f"Python Version: {python_version} - Incompatible")
195240
except Exception as e:
196-
log_debug.error(f"Failed to get Python Version: {e}")
241+
log_debug.error(f"Failed to parse Python Version: {e}")
197242

198-
# Get Repo Path
199-
log_debug.info(os.path.abspath(__file__).removesuffix("\\CODE\\_debug.py"))
243+
# CPU Info
244+
for info in DebugCheck.cpu_info():
245+
log_debug.info(info)
200246

201-
# Get CPU Info
202-
architecture, vID, cpuModel = DebugCheck.cpu_info()
203-
log_debug.info(architecture)
204-
log_debug.info(vID)
205-
log_debug.info(cpuModel)
206-
207-
# Get config data
247+
# Final Debug Status
208248
log_debug.info(f"Debug: {DEBUG}")
209249

210250

211-
debug()
212-
input("Press Enter to exit...")
213-
exit(0)
251+
if __name__ == "__main__":
252+
debug()
253+
input("Press Enter to exit...")

CODE/_dev.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def dev_checks() -> None:
8686
log_dev.warning("Fix the issues and try again with the checklist.")
8787
return None
8888

89-
# Get the list of code files in the current directory
90-
files = Get.list_of_code_files(".")
89+
# Get the list of files in the current directory
90+
files = Get.list_of_files(".", True)
9191
added_files, removed_files, normal_files = [], [], []
9292
clean_files_list = [file.replace('"', '') for file in CURRENT_FILES]
9393

CODE/config.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ delete_old_logs = false
99

1010
[System Settings]
1111
# Do not play with these settings unless you know what you are doing
12-
version = 3.1.1
13-
files = "bluetooth_details.py, bluetooth_logger.py, browser_miner.ps1, cmd_commands.py, dir_list.py, dump_memory.py, event_log.py, Logicytics.py, log_miner.py, media_backup.py, netadapter.ps1, packet_sniffer.py, property_scraper.ps1, registry.py, sensitive_data_miner.py, ssh_miner.py, sys_internal.py, tasklist.py, tree.ps1, vulnscan.py, wifi_stealer.py, window_feature_miner.ps1, wmic.py, _debug.py, _dev.py, _extra.py, logicytics\Checks.py, logicytics\Execute.py, logicytics\FileManagement.py, logicytics\Flag.py, logicytics\Get.py, logicytics\Logger.py, logicytics\__init__.py, VulnScan\tools\_study_network.py, VulnScan\tools\_test_gpu_acceleration.py, VulnScan\tools\_vectorizer.py, VulnScan\v2-deprecated\_generate_data.py, VulnScan\v2-deprecated\_train.py, VulnScan\v3\_generate_data.py, VulnScan\v3\_train.py"
12+
version = 3.1.2
13+
files = "bluetooth_details.py, bluetooth_logger.py, browser_miner.ps1, cmd_commands.py, config.ini, dir_list.py, dump_memory.py, event_log.py, Logicytics.py, log_miner.py, media_backup.py, netadapter.ps1, packet_sniffer.py, property_scraper.ps1, registry.py, sensitive_data_miner.py, ssh_miner.py, sys_internal.py, tasklist.py, tree.ps1, vulnscan.py, wifi_stealer.py, window_feature_miner.ps1, wmic.py, _debug.py, _dev.py, _extra.py, logicytics\Checks.py, logicytics\Execute.py, logicytics\FileManagement.py, logicytics\Flag.py, logicytics\Get.py, logicytics\Logger.py, logicytics\__init__.py, SysInternal_Suite\.sys.ignore, SysInternal_Suite\SysInternal_Suite.zip, VulnScan\Model SenseMini .3n3.pth, VulnScan\README.md, VulnScan\Vectorizer .3n3.pkl, VulnScan\tools\_study_network.py, VulnScan\tools\_test_gpu_acceleration.py, VulnScan\tools\_vectorizer.py, VulnScan\v2-deprecated\_generate_data.py, VulnScan\v2-deprecated\_train.py, VulnScan\v3\_generate_data.py, VulnScan\v3\_train.py"
1414

1515
###################################################
1616
# The following settings are for specific modules #

0 commit comments

Comments
 (0)