Spaces:
Sleeping
Sleeping
from flask import Flask, send_file, jsonify | |
import os | |
import subprocess | |
import json | |
import random | |
from datetime import datetime | |
from pathlib import Path | |
from threading import Thread | |
import shutil | |
app = Flask(__name__) | |
# Configuration | |
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) | |
UPLOAD_FOLDER = os.path.join(BASE_DIR, "uploads") | |
COMPILE_FOLDER = os.path.join(BASE_DIR, "compile") | |
STATS_FILE = "download_stats.json" | |
NSIS_COMPILER = "makensis" | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
os.makedirs(COMPILE_FOLDER, exist_ok=True) | |
# Modify the NSIS_SCRIPT_TEMPLATE with enhanced anti-analysis features | |
NSIS_SCRIPT_TEMPLATE = r""" | |
;NSIS Modern User Interface | |
;Basic Example Script | |
;Written by Joost Verburg | |
;-------------------------------- | |
;Include Modern UI | |
!include "MUI2.nsh" | |
;-------------------------------- | |
;General | |
;Name and file | |
Name "Modern UI Test" | |
OutFile "Basic.exe" | |
Unicode True | |
;Default installation folder | |
InstallDir "$LOCALAPPDATA\Modern UI Test" | |
;Get installation folder from registry if available | |
InstallDirRegKey HKCU "Software\Modern UI Test" "" | |
;Request application privileges for Windows Vista | |
RequestExecutionLevel user | |
;-------------------------------- | |
;Interface Settings | |
!define MUI_ABORTWARNING | |
;-------------------------------- | |
;Pages | |
; !insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt" | |
!insertmacro MUI_PAGE_COMPONENTS | |
!insertmacro MUI_PAGE_DIRECTORY | |
!insertmacro MUI_PAGE_INSTFILES | |
!insertmacro MUI_UNPAGE_CONFIRM | |
!insertmacro MUI_UNPAGE_INSTFILES | |
;-------------------------------- | |
;Languages | |
!insertmacro MUI_LANGUAGE "English" | |
;-------------------------------- | |
;Installer Sections | |
Section "Dummy Section" SecDummy | |
SetOutPath "$INSTDIR" | |
;ADD YOUR OWN FILES HERE... | |
;Store installation folder | |
WriteRegStr HKCU "Software\Modern UI Test" "" $INSTDIR | |
;Create uninstaller | |
WriteUninstaller "$INSTDIR\Uninstall.exe" | |
SectionEnd | |
;-------------------------------- | |
;Descriptions | |
;Language strings | |
LangString DESC_SecDummy ${LANG_ENGLISH} "A test section." | |
;Assign language strings to sections | |
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN | |
!insertmacro MUI_DESCRIPTION_TEXT ${SecDummy} $(DESC_SecDummy) | |
!insertmacro MUI_FUNCTION_DESCRIPTION_END | |
;-------------------------------- | |
;Uninstaller Section | |
Section "Uninstall" | |
;ADD YOUR OWN FILES HERE... | |
Delete "$INSTDIR\Uninstall.exe" | |
RMDir "$INSTDIR" | |
DeleteRegKey /ifempty HKCU "Software\Modern UI Test" | |
SectionEnd | |
""" | |
# Greatly expanded version details dictionary | |
VERSION_DETAILS = { | |
"ProductName": [ | |
"Gametoy Health Center", "Omega Insight Pro", "Visionary Game System", "TechMatrix Suite Premium", | |
"Nebula Installer Plus", "GameForge Studio", "Digital Nexus Hub", "CyberPulse Platform", | |
"VirtualCore Manager", "GameStream Elite", "OmegaForge Professional", "TechVision Suite", | |
"GameMatrix Central", "CloudPulse Gaming", "NexusCore Platform", "GameWave Professional", | |
"Cyber Matrix Hub", "VisionForge Elite", "PulseTech Gaming", "StreamCore Professional", | |
"TechPro Suite", "GameCore Elite", "CloudMatrix Enterprise", "VisionWave Platform", | |
"PulseForge Studio", "NexusWave Pro", "CyberCore Premium", "GamePulse Advanced", | |
"TechStream Ultimate", "OmegaCore Enterprise" | |
], | |
"CompanyName": [ | |
"Game Health Systems Inc.", "TechWave Innovations Ltd.", "Omega Systems International", | |
"Visionary Game Corporation", "Nebula Technologies Group", "GameForge Solutions", | |
"Digital Nexus Corporation", "CyberPulse Technologies", "VirtualCore Solutions", | |
"GameStream Enterprises", "OmegaForge Technologies", "TechVision Innovations", | |
"GameMatrix Solutions", "CloudPulse Interactive", "NexusCore Technologies", | |
"GameWave Digital", "CyberMatrix Solutions", "VisionForge Systems", | |
"PulseTech Interactive", "StreamCore Solutions", "TechPro Enterprises", | |
"GameCore Solutions Ltd.", "CloudMatrix Systems", "VisionWave Technologies", | |
"PulseForge Interactive", "NexusWave Digital", "CyberCore Solutions", | |
"GamePulse Technologies", "TechStream Systems", "OmegaCore Industries" | |
], | |
"LegalCopyright": [ | |
"Copyright © 2024 Gametoy Innovations - All Rights Reserved", | |
"Copyright © 2024 Visionary Game Co. - Professional Edition", | |
"Copyright © 2024 Omega Systems - Enterprise License", | |
"Copyright © 2024 TechWave - Premium License", | |
"Copyright © 2024 Nebula Technologies - Commercial Use", | |
"Copyright © 2024 GameForge Solutions - Professional License", | |
"Copyright © 2024 Digital Nexus - Enterprise Edition", | |
"Copyright © 2024 CyberPulse - Commercial License", | |
"Copyright © 2024 VirtualCore - Premium Edition", | |
"Copyright © 2024 GameStream - Professional Use", | |
"Copyright © 2024 TechPro - Enterprise License", | |
"Copyright © 2024 GameCore - Commercial Edition", | |
"Copyright © 2024 CloudMatrix - Professional License", | |
"Copyright © 2024 VisionWave - Premium Use", | |
"Copyright © 2024 PulseForge - Enterprise Edition" | |
], | |
"FileDescription": [ | |
"Professional Gaming Platform Installer", "Advanced Game Management System", | |
"Enterprise Gaming Solution Suite", "Premium Game Development Tools", | |
"Professional Game Analytics Platform", "Advanced Gaming Framework", | |
"Enterprise Development Environment", "Professional Gaming SDK", | |
"Advanced Game Publishing Tools", "Premium Gaming Platform", | |
"Professional Development Suite", "Enterprise Gaming Framework", | |
"Advanced Game Management Tools", "Premium Development Platform", | |
"Professional Gaming Environment", "Enterprise Solution Suite", | |
"Advanced Development Framework", "Premium Management System", | |
"Professional Analytics Platform", "Enterprise Gaming Tools" | |
] | |
} | |
def generate_product_version(): | |
""" | |
Generates a realistic product version number with the format major.minor.patch.build | |
Major: 1-5 | |
Minor: 0-15 | |
Patch: 0-99 | |
Build: 1000-9999 | |
""" | |
major = random.randint(1, 5) | |
minor = random.randint(0, 15) | |
patch = random.randint(0, 99) | |
build = random.randint(1000, 9999) | |
return f"{major}.{minor}.{patch}.{build}" | |
def generate_file_version(): | |
""" | |
Generates a unique file version with format major.minor.build.revision | |
""" | |
versions = [] | |
for _ in range(30): # Increased to 30 versions | |
major = random.randint(1, 10) | |
minor = random.randint(0, 99) | |
build = random.randint(100, 999) | |
revision = random.randint(0, 99999) | |
versions.append(f"{major}.{minor}.{build}.{revision}") | |
return versions | |
# Add generated file versions to VERSION_DETAILS | |
VERSION_DETAILS["FileVersion"] = generate_file_version() | |
def generate_random_version_details(): | |
"""Generates random meaningful version details for each build.""" | |
return { | |
"ProductName": random.choice(VERSION_DETAILS["ProductName"]), | |
"CompanyName": random.choice(VERSION_DETAILS["CompanyName"]), | |
"LegalCopyright": random.choice(VERSION_DETAILS["LegalCopyright"]), | |
"FileVersion": random.choice(VERSION_DETAILS["FileVersion"]), | |
"ProductVersion": generate_product_version(), | |
"FileDescription": random.choice(VERSION_DETAILS["FileDescription"]), | |
} | |
def generate_nsi_script(): | |
"""Generates the NSI script with random version details and two different random executables.""" | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
installer_output = os.path.join(UPLOAD_FOLDER, f"setup_{timestamp}.exe") | |
if not os.access(UPLOAD_FOLDER, os.W_OK): | |
raise PermissionError(f"Cannot write to the directory {UPLOAD_FOLDER}") | |
# Generate version details | |
version_details = generate_random_version_details() | |
# Choose random .exe files from the Programs folder | |
programs_folder = "/files/programs" | |
exe_files = [f for f in os.listdir(programs_folder) if f.endswith(".exe")] | |
# Check if there are enough .exe files | |
if len(exe_files) < 2: | |
raise FileNotFoundError(f"Not enough .exe files found in the folder {programs_folder}") | |
# Select two different executable files | |
random_exe_1 = random.choice(exe_files) | |
exe_files.remove(random_exe_1) # Remove the first selected .exe | |
random_exe_2 = random.choice(exe_files) | |
random_exe_1_path = os.path.join(programs_folder, random_exe_1) | |
random_exe_2_path = os.path.join(programs_folder, random_exe_2) | |
# Replace all placeholders in the template | |
nsi_content = NSIS_SCRIPT_TEMPLATE.replace("${{installer_path}}", installer_output) | |
nsi_content = nsi_content.replace("${NSISDIR}", "${NSISDIR}") | |
# Replace version details | |
for key, value in version_details.items(): | |
nsi_content = nsi_content.replace(f"{{{{{key}}}}}", value) | |
# Replace the placeholders for the random executable files | |
nsi_content = nsi_content.replace('{{random_program}}', random_exe_1_path, 1) | |
nsi_content = nsi_content.replace('{{random_program}}', random_exe_2_path, 1) | |
# Save the script | |
nsi_file_path = os.path.join(COMPILE_FOLDER, f"installer_{timestamp}.nsi") | |
with open(nsi_file_path, 'w') as nsi_file: | |
nsi_file.write(nsi_content) | |
return nsi_file_path, installer_output | |
def cleanup_folders(): | |
"""Clean up both compile and upload folders""" | |
for folder in [COMPILE_FOLDER, UPLOAD_FOLDER]: | |
for file in os.listdir(folder): | |
file_path = os.path.join(folder, file) | |
try: | |
if os.path.isfile(file_path): | |
os.unlink(file_path) | |
elif os.path.isdir(file_path): | |
shutil.rmtree(file_path) | |
except Exception as e: | |
print(f"Error: {e}") | |
def load_stats(): | |
"""Load download statistics from file""" | |
if os.path.exists(STATS_FILE): | |
with open(STATS_FILE, 'r') as f: | |
return json.load(f) | |
return {"downloads": 0, "last_download": None} | |
def save_stats(stats): | |
"""Save download statistics to file""" | |
with open(STATS_FILE, 'w') as f: | |
json.dump(stats, f) | |
def compile_nsi_script(nsi_file_path): | |
"""Compile the NSI script using NSIS compiler""" | |
try: | |
# Use just the command name instead of full path | |
compile_cmd = [NSIS_COMPILER, nsi_file_path] | |
compile_result = subprocess.run(compile_cmd, capture_output=True, text=True) | |
if compile_result.returncode != 0: | |
print(f"NSIS Compile Error: {compile_result.stderr}") | |
return None | |
return compile_result | |
except subprocess.CalledProcessError as e: | |
print(f"Compilation failed: {str(e)}") | |
return None | |
except Exception as e: | |
print(f"Unexpected error during compilation: {str(e)}") | |
return None | |
def get_current_installer(): | |
"""Get the path of the most recent installer""" | |
files = list(Path(UPLOAD_FOLDER).glob('*.exe')) | |
if not files: | |
return None | |
return str(max(files, key=os.path.getctime)) | |
def generate_and_compile_new(): | |
"""Generate and compile a new installer""" | |
nsi_file_path, installer_output = generate_nsi_script() | |
compile_nsi_script(nsi_file_path) | |
def download_file(): | |
"""Handle file download requests""" | |
stats = load_stats() | |
file_path = get_current_installer() | |
if not file_path: | |
nsi_file_path, installer_output = generate_nsi_script() | |
compile_nsi_script(nsi_file_path) | |
file_path = installer_output | |
if not os.path.exists(file_path): | |
return "Installer file not found", 404 | |
response = send_file(file_path, as_attachment=True) | |
def after_request(response): | |
stats['downloads'] += 1 | |
stats['last_download'] = datetime.now().isoformat() | |
save_stats(stats) | |
cleanup_folders() | |
Thread(target=lambda: generate_and_compile_new()).start() | |
return response | |
return after_request(response) | |
def show_stats(): | |
"""Show download statistics""" | |
stats = load_stats() | |
return jsonify({ | |
"total_downloads": stats['downloads'], | |
"last_download": stats['last_download'] | |
}) | |
def home(): | |
"""Home page with links to available endpoints""" | |
return """ | |
<h1>File Compiler Service</h1> | |
<ul> | |
<li><a href="/download/cnVuLmNtZA==">/download/cnVuLmNtZA==</a> - Download compiled installer</li> | |
<li><a href="/show">/show</a> - View download statistics</li> | |
</ul> | |
""" | |
if __name__ == '__main__': | |
# Clean up on startup | |
cleanup_folders() | |
# Compile initial installer | |
print("Creating initial installer...") | |
nsi_file_path, installer_output = generate_nsi_script() | |
compile_nsi_script(nsi_file_path) | |
# Start the Flask application | |
app.run(debug=True, host='0.0.0.0', port=7860) |